1 """Set the wallpaper."""
9 from .settings import CACHE_DIR, HOME, OS
13 def get_desktop_env():
14 """Identify the current running desktop environment."""
15 desktop = os.environ.get("XDG_CURRENT_DESKTOP")
19 desktop = os.environ.get("DESKTOP_SESSION")
23 desktop = os.environ.get("GNOME_DESKTOP_SESSION_ID")
27 desktop = os.environ.get("MATE_DESKTOP_SESSION_ID")
31 desktop = os.environ.get("SWAYSOCK")
35 desktop = os.environ.get("DESKTOP_STARTUP_ID")
36 if desktop and "awesome" in desktop:
42 def xfconf(path, img):
43 """Call xfconf to set the wallpaper on XFCE."""
44 util.disown(["xfconf-query", "--channel", "xfce4-desktop",
45 "--property", path, "--set", img])
48 def set_wm_wallpaper(img):
49 """Set the wallpaper for non desktop environments."""
50 if shutil.which("feh"):
51 util.disown(["feh", "--bg-fill", img])
53 elif shutil.which("nitrogen"):
54 util.disown(["nitrogen", "--set-zoom-fill", img])
56 elif shutil.which("bgs"):
57 util.disown(["bgs", "-z", img])
59 elif shutil.which("hsetroot"):
60 util.disown(["hsetroot", "-fill", img])
62 elif shutil.which("habak"):
63 util.disown(["habak", "-mS", img])
65 elif shutil.which("display"):
66 util.disown(["display", "-backdrop", "-window", "root", img])
69 logging.error("No wallpaper setter found.")
73 def set_desktop_wallpaper(desktop, img):
74 """Set the wallpaper for the desktop environment."""
75 desktop = str(desktop).lower()
77 if "xfce" in desktop or "xubuntu" in desktop:
78 # XFCE requires two commands since they differ between versions.
79 xfconf("/backdrop/screen0/monitor0/image-path", img)
80 xfconf("/backdrop/screen0/monitor0/workspace0/last-image", img)
82 elif "muffin" in desktop or "cinnamon" in desktop:
83 util.disown(["gsettings", "set",
84 "org.cinnamon.desktop.background",
85 "picture-uri", "file://" + urllib.parse.quote(img)])
87 elif "gnome" in desktop or "unity" in desktop:
88 util.disown(["gsettings", "set",
89 "org.gnome.desktop.background",
90 "picture-uri", "file://" + urllib.parse.quote(img)])
92 elif "mate" in desktop:
93 util.disown(["gsettings", "set", "org.mate.background",
94 "picture-filename", img])
96 elif "sway" in desktop:
97 util.disown(["swaymsg", "output", "*", "bg", img, "fill"])
99 elif "awesome" in desktop:
100 util.disown(["awesome-client", "gears.wallpaper.maximized(", img, ")"])
103 set_wm_wallpaper(img)
106 def set_mac_wallpaper(img):
107 """Set the wallpaper on macOS."""
108 db_file = "Library/Application Support/Dock/desktoppicture.db"
109 db_path = os.path.join(HOME, db_file)
110 subprocess.call(["sqlite3", db_path, "update data set value = '%s'" % img])
112 # Kill the dock to fix issues with cached wallpapers.
113 # macOS caches wallpapers and if a wallpaper is set that shares
114 # the filename with a cached wallpaper, the cached wallpaper is
116 subprocess.call(["killall", "Dock"])
119 def set_win_wallpaper(img):
120 """Set the wallpaper on Windows."""
121 # There's a different command depending on the architecture
122 # of Windows. We check the PROGRAMFILES envar since using
123 # platform is unreliable.
124 if "x86" in os.environ["PROGRAMFILES"]:
125 ctypes.windll.user32.SystemParametersInfoW(20, 0, img, 3)
127 ctypes.windll.user32.SystemParametersInfoA(20, 0, img, 3)
131 """Set the wallpaper."""
132 if not os.path.isfile(img):
135 desktop = get_desktop_env()
138 set_mac_wallpaper(img)
140 elif OS == "Windows":
141 set_win_wallpaper(img)
144 set_desktop_wallpaper(desktop, img)
146 logging.info("Set the new wallpaper.")
149 def get(cache_dir=CACHE_DIR):
150 """Get the current wallpaper."""
151 current_wall = os.path.join(cache_dir, "wal")
153 if os.path.isfile(current_wall):
154 return util.read_file(current_wall)[0]