chamesle-access (11703B)
1 #!/usr/bin/python3 2 # Chamesle Accessibility: screen reader, high contrast and large text, the same three on every desktop 3 # chamesle-access the window 4 # chamesle-access apply at sign-in: puts the saved settings back, the first time from what jiggity saved 5 # chamesle-access reader the screen reader on or off, what Super+Alt+S runs 6 # chamesle-access set KEY 0|1 reader, contrast or text 7 # chamesle-access env for window maker's session script: the theme and text size, as shell lines 8 import ast, gettext, os, subprocess, sys, time 9 10 gettext.textdomain("chamesle") 11 _ = gettext.gettext 12 13 SYSTEM = "/etc/chamesle/access" 14 CONF = os.path.join(os.environ.get("XDG_CONFIG_HOME") or os.path.expanduser("~/.config"), "chamesle/access") 15 CMD = "chamesle-access reader" 16 CUSTOM = "/org/cinnamon/desktop/keybindings/custom-keybindings/chamesle-reader/" 17 MATE_KEY = "/org/mate/desktop/keybindings/chamesle-reader/" 18 DPI = 120 19 20 21 def run(*cmd): 22 return subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True).stdout.strip() 23 24 25 def gs(schema, key, value): 26 run("gsettings", "set", schema, key, value) 27 28 29 def gs_get(schema, key): 30 return run("gsettings", "get", schema, key) 31 32 33 def load(path): 34 conf = {} 35 try: 36 for line in open(path): 37 k, _, v = line.strip().partition("=") 38 if k: 39 conf[k] = v 40 except OSError: 41 pass 42 return conf 43 44 45 def save(conf): 46 os.makedirs(os.path.dirname(CONF), exist_ok=True) 47 with open(CONF, "w") as f: 48 f.writelines("%s=%s\n" % kv for kv in conf.items()) 49 50 51 def desktop(): 52 d = os.environ.get("XDG_CURRENT_DESKTOP", "").upper() 53 for name in ("KDE", "CINNAMON", "MATE", "XFCE", "GNOME", "WINDOWMAKER"): 54 if name in d: 55 return name 56 return "" 57 58 59 # the sound server pipewire runs for pulseaudio apps. at sign-in it comes up next to us: orca's speech started 60 # before it goes to the sound card directly, and then pipewire can't have the card and there's no sound at all 61 def sound_server(wait=20): 62 sock = os.path.join(os.environ.get("XDG_RUNTIME_DIR") or "/run/user/%d" % os.getuid(), "pulse/native") 63 for _ in range(wait * 4): 64 if os.path.exists(sock): 65 return 66 time.sleep(0.25) 67 68 69 # orca itself, where the desktop doesn't start it 70 def orca(on): 71 if on: 72 sound_server() 73 subprocess.Popen(["orca", "--replace"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, 74 start_new_session=True) 75 else: 76 subprocess.run(["pkill", "-x", "orca"]) 77 78 79 def reader(on, login=False): 80 d = desktop() 81 if d == "GNOME": 82 gs("org.gnome.desktop.a11y.applications", "screen-reader-enabled", str(on).lower()) 83 return 84 if d == "KDE": 85 # kaccess runs orca on plasma, and Super+Alt+S is its shortcut. it reads the setting when it starts and only 86 # stops an orca it started itself: so the setting, then kaccess again 87 run("kwriteconfig6", "--file", "kaccessrc", "--group", "ScreenReader", "--key", "Enabled", str(on).lower()) 88 if login: 89 return 90 sound_server() 91 run("pkill", "-x", "orca") 92 run("pkill", "-x", "kaccess") 93 subprocess.Popen(["kaccess"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True) 94 plasma_shortcut() 95 return 96 if on or not login: 97 orca(on) 98 99 100 # the theme a desktop had, to go back to. saved once, when high contrast goes on 101 def theme(conf, on, get, put, value, key="theme"): 102 if on: 103 conf.setdefault(key, get()) 104 put(value) 105 elif key in conf: 106 put(conf.pop(key)) 107 108 109 def contrast(conf, on): 110 # gtk 4 and libadwaita apps read this one on every desktop, through the portal 111 gs("org.gnome.desktop.a11y.interface", "high-contrast", str(on).lower()) 112 d = desktop() 113 if d == "CINNAMON": 114 k = ("org.cinnamon.desktop.interface", "gtk-theme") 115 theme(conf, on, lambda: gs_get(*k), lambda v: gs(*k, v), "'HighContrast'") 116 elif d == "MATE": 117 k = ("org.mate.interface", "gtk-theme") 118 theme(conf, on, lambda: gs_get(*k), lambda v: gs(*k, v), "'HighContrast'") 119 elif d == "XFCE": 120 k = ("xfconf-query", "-c", "xsettings", "-p", "/Net/ThemeName") 121 theme(conf, on, lambda: run(*k), lambda v: run(*k, "-s", v), "HighContrast") 122 elif d == "KDE": 123 theme(conf, on, lambda: run("kreadconfig6", "--group", "General", "--key", "ColorScheme") or "BreezeLight", 124 lambda v: run("plasma-apply-colorscheme", v), "ChamesleHighContrast") 125 # the panel stays gray, black on gray is strong already and the tray icons are dark. 0.1 made it 126 # breeze-dark, where they hardly show: back to what it was 127 if not on and "panel" in conf: 128 run("plasma-apply-desktoptheme", conf.pop("panel")) 129 130 131 # "Inter,10,-1,5,..." with a bigger second field 132 def bigger(font): 133 f = font.split(",") 134 f[1] = str(round(float(f[1]) * 1.25)) 135 return ",".join(f) 136 137 138 # plasma 6 has no dpi setting left: the fonts themselves. what they were is saved 139 def plasma_fonts(conf, on): 140 for group, key in (("General", "font"), ("General", "fixed"), ("General", "menuFont"), ("General", "toolBarFont"), 141 ("General", "smallestReadableFont"), ("WM", "activeFont")): 142 name = "font-%s-%s" % (group, key) 143 if on: 144 old = run("kreadconfig6", "--group", group, "--key", key) 145 if old and name not in conf: 146 conf[name] = old 147 run("kwriteconfig6", "--notify", "--group", group, "--key", key, bigger(old)) 148 elif name in conf: 149 run("kwriteconfig6", "--notify", "--group", group, "--key", key, conf.pop(name)) 150 151 152 def text(conf, on): 153 d = desktop() 154 dpi = DPI if on else 96 155 if d == "GNOME": 156 gs("org.gnome.desktop.interface", "text-scaling-factor", "1.25" if on else "1.0") 157 elif d == "CINNAMON": 158 gs("org.cinnamon.desktop.interface", "text-scaling-factor", "1.25" if on else "1.0") 159 elif d == "MATE": 160 gs("org.mate.font-rendering", "dpi", str(dpi)) 161 elif d == "XFCE": 162 run("xfconf-query", "-c", "xsettings", "-p", "/Xft/DPI", "-n", "-t", "int", "-s", str(dpi)) 163 elif d == "KDE": 164 plasma_fonts(conf, on) 165 elif d == "WINDOWMAKER": 166 xdpi(dpi) 167 168 169 # window maker has no settings daemon: gtk and qt take the size from the X resources 170 def xdpi(dpi): 171 subprocess.run(["xrdb", "-merge"], input="Xft.dpi: %d\n" % dpi, text=True) 172 173 174 # plasma's is kaccess's, Meta+Alt+S by default but not bound to anything. set it, unless someone did already. 175 # kglobalaccel only takes it once kaccess has told it about the action, so it tries for a few seconds 176 KACCESS = "['kaccess', 'Toggle Screen Reader On and Off', '', '']" 177 META_ALT_S = 0x10000000 | 0x08000000 | 0x53 178 179 180 def plasma_shortcut(): 181 call = ["gdbus", "call", "--session", "-d", "org.kde.kglobalaccel", "-o", "/kglobalaccel", "-m"] 182 for _ in range(10): 183 keys = run(*call, "org.kde.KGlobalAccel.shortcutKeys", KACCESS) 184 if keys and "[]" not in keys: 185 return 186 run(*call, "org.kde.KGlobalAccel.setForeignShortcut", KACCESS, "[%d]" % META_ALT_S) 187 time.sleep(0.5) 188 189 190 # Super+Alt+S. gnome has it already, plasma has it switched off 191 def shortcut(): 192 d = desktop() 193 if d == "KDE": 194 plasma_shortcut() 195 if d == "XFCE": 196 run("xfconf-query", "-c", "xfce4-keyboard-shortcuts", "-p", "/commands/custom/<Alt><Super>s", 197 "-n", "-t", "string", "-s", CMD) 198 elif d == "MATE": 199 s = "org.mate.control-center.keybinding:" + MATE_KEY 200 gs(s, "name", repr(_("Screen reader"))) 201 gs(s, "action", "'%s'" % CMD) 202 gs(s, "binding", "'<Mod4><Alt>s'") 203 elif d == "CINNAMON": 204 s = "org.cinnamon.desktop.keybindings.custom-keybinding:" + CUSTOM 205 gs(s, "name", repr(_("Screen reader"))) 206 gs(s, "command", "'%s'" % CMD) 207 gs(s, "binding", "['<Super><Alt>s']") 208 gs("org.cinnamon.desktop.keybindings.media-keys", "screenreader", "@as []") 209 old = gs_get("org.cinnamon.desktop.keybindings", "custom-list") 210 if "chamesle-reader" not in old: 211 names = [] if old.startswith("@") else ast.literal_eval(old) 212 gs("org.cinnamon.desktop.keybindings", "custom-list", repr(names + ["chamesle-reader"])) 213 214 215 # gnome and plasma take Super+Alt+S themselves, so theirs is the truth about the reader. plasma doesn't save 216 # that toggle, so there it's whether orca runs, which says nothing yet at sign-in 217 def sync(conf, login=False): 218 d = desktop() 219 if d == "GNOME": 220 conf["reader"] = "1" if gs_get("org.gnome.desktop.a11y.applications", "screen-reader-enabled") == "true" else "0" 221 elif d == "KDE" and not login: 222 conf["reader"] = "1" if run("pgrep", "-x", "orca") else "0" 223 return conf 224 225 226 def change(key, on): 227 conf = load(CONF) 228 conf[key] = "1" if on else "0" 229 if key == "reader": 230 reader(on) 231 elif key == "contrast": 232 contrast(conf, on) 233 else: 234 text(conf, on) 235 save(conf) 236 237 238 # what's saved, or the first time what was picked at install 239 def saved(): 240 return load(CONF) if os.path.exists(CONF) else load(SYSTEM) 241 242 243 def env(): 244 conf = saved() 245 if conf.get("contrast") == "1": 246 print("export GTK_THEME=HighContrast") 247 248 249 def apply(): 250 first = not os.path.exists(CONF) 251 conf = load(SYSTEM) if first else sync(load(CONF), True) 252 shortcut() 253 # only what's on: off leaves the desktop's own settings alone. the reader last, it waits for the sound 254 if conf.get("contrast") == "1": 255 contrast(conf, True) 256 if conf.get("text") == "1": 257 text(conf, True) 258 save(conf) 259 if conf.get("reader") == "1": 260 reader(True, not first) 261 262 263 def window(): 264 import gi 265 gi.require_version("Gtk", "4.0") 266 gi.require_version("Adw", "1") 267 from gi.repository import Adw, Gtk 268 269 def activate(app): 270 conf = sync(load(CONF)) 271 win = Adw.ApplicationWindow(application=app, title=_("Accessibility"), default_width=520, default_height=420) 272 group = Adw.PreferencesGroup(description=_("Changes apply right away. Some apps show them when you open them again.")) 273 for key, title, sub in ( 274 ("reader", _("Screen reader"), _("Reads the screen aloud. Super+Alt+S turns it on or off anywhere.")), 275 ("contrast", _("High contrast"), _("Strong colors that are easier to tell apart.")), 276 ("text", _("Large text"), _("Makes text bigger everywhere."))): 277 row = Adw.SwitchRow(title=title, subtitle=sub, active=conf.get(key) == "1") 278 row.connect("notify::active", lambda r, _p, k=key: change(k, r.get_active())) 279 group.add(row) 280 page = Adw.PreferencesPage() 281 page.add(group) 282 box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) 283 box.append(Adw.HeaderBar()) 284 box.append(page) 285 page.set_vexpand(True) 286 win.set_content(box) 287 win.present() 288 289 app = Adw.Application(application_id="org.chamesle.Access") 290 app.connect("activate", activate) 291 app.run([]) 292 293 294 def main(): 295 a = sys.argv[1:] 296 if not a: 297 window() 298 elif a == ["apply"]: 299 apply() 300 elif a == ["env"]: 301 env() 302 elif a == ["reader"]: 303 change("reader", sync(load(CONF)).get("reader") != "1") 304 elif len(a) == 3 and a[0] == "set" and a[1] in ("reader", "contrast", "text") and a[2] in ("0", "1"): 305 change(a[1], a[2] == "1") 306 else: 307 sys.exit("usage: chamesle-access [apply | env | reader | set reader|contrast|text 0|1]") 308 309 310 if __name__ == "__main__": 311 main()