efficient vim config
[dotfiles/.git] / .local / lib / python3.9 / site-packages / pywal / wallpaper.py
1 """Set the wallpaper."""
2 import ctypes
3 import logging
4 import os
5 import shutil
6 import subprocess
7 import urllib.parse
8
9 from .settings import CACHE_DIR, HOME, OS
10 from . import util
11
12
13 def get_desktop_env():
14     """Identify the current running desktop environment."""
15     desktop = os.environ.get("XDG_CURRENT_DESKTOP")
16     if desktop:
17         return desktop
18
19     desktop = os.environ.get("DESKTOP_SESSION")
20     if desktop:
21         return desktop
22
23     desktop = os.environ.get("GNOME_DESKTOP_SESSION_ID")
24     if desktop:
25         return "GNOME"
26
27     desktop = os.environ.get("MATE_DESKTOP_SESSION_ID")
28     if desktop:
29         return "MATE"
30
31     desktop = os.environ.get("SWAYSOCK")
32     if desktop:
33         return "SWAY"
34
35     desktop = os.environ.get("DESKTOP_STARTUP_ID")
36     if desktop and "awesome" in desktop:
37         return "AWESOME"
38
39     return None
40
41
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])
46
47
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])
52
53     elif shutil.which("nitrogen"):
54         util.disown(["nitrogen", "--set-zoom-fill", img])
55
56     elif shutil.which("bgs"):
57         util.disown(["bgs", "-z", img])
58
59     elif shutil.which("hsetroot"):
60         util.disown(["hsetroot", "-fill", img])
61
62     elif shutil.which("habak"):
63         util.disown(["habak", "-mS", img])
64
65     elif shutil.which("display"):
66         util.disown(["display", "-backdrop", "-window", "root", img])
67
68     else:
69         logging.error("No wallpaper setter found.")
70         return
71
72
73 def set_desktop_wallpaper(desktop, img):
74     """Set the wallpaper for the desktop environment."""
75     desktop = str(desktop).lower()
76
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)
81
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)])
86
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)])
91
92     elif "mate" in desktop:
93         util.disown(["gsettings", "set", "org.mate.background",
94                      "picture-filename", img])
95
96     elif "sway" in desktop:
97         util.disown(["swaymsg", "output", "*", "bg", img, "fill"])
98
99     elif "awesome" in desktop:
100         util.disown(["awesome-client", "gears.wallpaper.maximized(", img, ")"])
101
102     else:
103         set_wm_wallpaper(img)
104
105
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])
111
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
115     # used instead.
116     subprocess.call(["killall", "Dock"])
117
118
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)
126     else:
127         ctypes.windll.user32.SystemParametersInfoA(20, 0, img, 3)
128
129
130 def change(img):
131     """Set the wallpaper."""
132     if not os.path.isfile(img):
133         return
134
135     desktop = get_desktop_env()
136
137     if OS == "Darwin":
138         set_mac_wallpaper(img)
139
140     elif OS == "Windows":
141         set_win_wallpaper(img)
142
143     else:
144         set_desktop_wallpaper(desktop, img)
145
146     logging.info("Set the new wallpaper.")
147
148
149 def get(cache_dir=CACHE_DIR):
150     """Get the current wallpaper."""
151     current_wall = os.path.join(cache_dir, "wal")
152
153     if os.path.isfile(current_wall):
154         return util.read_file(current_wall)[0]
155
156     return "None"