emacs-config/test.org

8.1 KiB

Changing the transparency of specific windows

  ;; Stack Trace
  ;; - https://www.google.com/search?client=firefox-b-1-d&q=change+window+transparency+compton
  ;; - https://github.com/chjj/compton/issues/219
  ;; - _NET_WM_WINDOW_OPACITY
  ;; - https://github.com/i3/i3/issues/2840
  ;; - Failure

  ;; - https://www.google.com/search?client=firefox-b-1-d&ei=Qn4OXarOFNG4tQX1yKrwAg&q=dynamically+set+window+transparency+compton&oq=dynamically+set+window+transparency+compton&gs_l=psy-ab.3...1302.1926..2080...0.0..0.121.655.6j1......0....1..gws-wiz.......0i71j33i22i29i30j35i304i39j33i160.w6gx35dRUJc
  ;; - https://www.reddit.com/r/unixporn/comments/3osw03/toggling_transparency_on_windows_in_compton_and/
  ;; - https://github.com/freedesktop/transset
  ;; - XChangeProperty
  ;; - https://gitlab.peach-bun.com/pinion/SFML/commit/135c1716e877464db720265f37316cbb54ef13f2?expanded=1&view=parallel
  ;; - xcb_change_property
  ;; - https://www.x.org/releases/current/doc/man/man3/xcb_change_property.3.xhtml

  ;; Also found this: https://xcb.freedesktop.org/XmlXcb/
  ;; C-h f xcb change property
  ;; Found xcb:ChangeProperty
  ;; Let the hacking begin

  ;; Found xcb:ewmh:-atoms
  ;; added opacity to list
  ;; called init function xcb:ewmh:init
  ;; found opacity is cardinal atom through xprop

  ;; example c call: https://github.com/ehntoo/unagi/blob/master/plugins/opacity.c#L71
  ;; True example: https://github.com/polybar/polybar/blob/ca4426a9620f4db05a0117282fbed3a32a14ec92/src/x11/ewmh.cpp#L168

  ;; reading from transset
  ;; So in C with Xlib, NWWO (my new abbreviation) is essentially an unsigned int.
  ;; max value is completely opaque, min is the opposite.
  ;; However, it's converted to a char array when passed to xlib xchangeproperty

  ;; xcb_change_property(conn->connection, XCB_PROP_MODE_REPLACE, win, _NET_WM_WINDOW_OPACITY, XCB_ATOM_CARDINAL, 32, 1, &values);

  (require 'dash)
  (require 'hydra)

  (defconst my/unsigned-int-max 4294967295)

  (defun my/int-to-char-array (int)
    (cl-labels ((thunk (int depth)
                       (if (= depth 4)
                           nil
                         (cons (% int 256)
                               (thunk (/ int 256)
                                      (1+ depth))))))
      (thunk int 0)))

  (defun my/char-array-to-int (arr)
    (let ((iter (1- (length arr)))
          (sum 0))
      (while (>= iter 0)
        (setf sum (+ (* 256 sum) (aref arr iter)))
        (decf iter))
      sum))

  (defun my/map-to-unsigned-int (percent)
    (round (* my/unsigned-int-max percent)))

  (defun my/map-to-percent (val)
    (/ val (float my/unsigned-int-max)))

  (defun my/set-transparency (window-id value)
    "`value` should be an float between 0 and and 1,"
    (->> value
         (my/map-to-unsigned-int)
         (my/int-to-char-array)
         (make-instance xcb:ChangeProperty
                        :mode xcb:PropMode:Replace :window window-id
                        :property xcb:Atom:_NET_WM_WINDOW_OPACITY :type xcb:Atom:CARDINAL
                        :format 32 :data-len 1 :data)
         (xcb:+request exwm--connection))
    (xcb:flush exwm--connection))

  (defun my/get-transparency (window-id)
    (let ((reply (->> window-id
                      (make-instance xcb:GetProperty :delete 0
                                     :property xcb:Atom:_NET_WM_WINDOW_OPACITY :type xcb:Atom:CARDINAL
                                     :long-offset 0 :long-length 1
                                     :window)
                      (xcb:+request-unchecked+reply exwm--connection))))
      (let ((char-arr (slot-value reply 'value)))
        (if (zerop (length char-arr))
            1.0
          (-> char-arr
              (my/char-array-to-int)
              (my/map-to-percent))))))

  (defun my/increase-window-transparency ()
    (interactive)
    (when-let (id (exwm--buffer->id (current-buffer)))
      (let* ((curr-trans (my/get-transparency id))
             (new-trans (+ curr-trans 0.1)))
        (my/set-transparency
         id (if (< 1 new-trans) 1 new-trans)))))

  (defun my/decrease-window-transparency ()
    (interactive)
    (when-let (id (exwm--buffer->id (current-buffer)))
      (let* ((curr-trans (my/get-transparency id))
             (new-trans (- curr-trans 0.1)))
        (my/set-transparency
         id (if (< new-trans 0) 0 new-trans)))))

  (defun my/reset-window-transparency ()
    (interactive)
    (when-let (id (exwm--buffer->id (current-buffer)))
      (my/set-transparency id 1.0)))

  (defhydra window-transparency-hydra (*window-map* "t")
    "Manage window splits"
    ("j" my/decrease-window-transparency)
    ("k" my/increase-window-transparency)
    ("r" my/reset-window-transparency)
    ("q" nil))

  (eval-and-compile
    (defconst xcb:ewmh:-atoms
      '( ;; Root Window Properties (and Related Messages)
        _NET_SUPPORTED
        _NET_CLIENT_LIST
        _NET_CLIENT_LIST_STACKING
        _NET_NUMBER_OF_DESKTOPS
        _NET_DESKTOP_GEOMETRY
        _NET_DESKTOP_VIEWPORT
        _NET_CURRENT_DESKTOP
        _NET_DESKTOP_NAMES
        _NET_ACTIVE_WINDOW
        _NET_WORKAREA
        _NET_SUPPORTING_WM_CHECK
        _NET_VIRTUAL_ROOTS
        _NET_DESKTOP_LAYOUT
        _NET_SHOWING_DESKTOP
        ;; Other Root Window Messages
        _NET_CLOSE_WINDOW
        _NET_MOVERESIZE_WINDOW
        _NET_WM_MOVERESIZE
        _NET_RESTACK_WINDOW
        _NET_REQUEST_FRAME_EXTENTS
        ;; Application Window Properties
        _NET_WM_NAME
        _NET_WM_VISIBLE_NAME
        _NET_WM_ICON_NAME
        _NET_WM_VISIBLE_ICON_NAME
        _NET_WM_DESKTOP
        _NET_WM_WINDOW_TYPE
        _NET_WM_STATE
        _NET_WM_ALLOWED_ACTIONS
        _NET_WM_STRUT
        _NET_WM_STRUT_PARTIAL
        _NET_WM_ICON_GEOMETRY
        _NET_WM_ICON
        _NET_WM_PID
        _NET_WM_HANDLED_ICONS
        _NET_WM_USER_TIME
        _NET_WM_USER_TIME_WINDOW
        _NET_FRAME_EXTENTS
        _NET_WM_OPAQUE_REGION
        _NET_WM_BYPASS_COMPOSITOR
        ;; Window Manager Protocols
        _NET_WM_PING
        _NET_WM_SYNC_REQUEST
        _NET_WM_SYNC_REQUEST_COUNTER
        _NET_WM_FULLSCREEN_MONITORS
        ;; Other Properties
        _NET_WM_FULL_PLACEMENT
        _NET_WM_CM_S0  ;_NET_WM_CM_Sn (n = 1, 2, ...) are left out here.
        ;; _NET_WM_WINDOW_TYPE hint
        _NET_WM_WINDOW_TYPE_DESKTOP
        _NET_WM_WINDOW_TYPE_DOCK
        _NET_WM_WINDOW_TYPE_TOOLBAR
        _NET_WM_WINDOW_TYPE_MENU
        _NET_WM_WINDOW_TYPE_UTILITY
        _NET_WM_WINDOW_TYPE_SPLASH
        _NET_WM_WINDOW_TYPE_DIALOG
        _NET_WM_WINDOW_TYPE_DROPDOWN_MENU
        _NET_WM_WINDOW_TYPE_POPUP_MENU
        _NET_WM_WINDOW_TYPE_TOOLTIP
        _NET_WM_WINDOW_TYPE_NOTIFICATION
        _NET_WM_WINDOW_TYPE_COMBO
        _NET_WM_WINDOW_TYPE_DND
        _NET_WM_WINDOW_TYPE_NORMAL
        ;; _NET_WM_STATE hint
        _NET_WM_STATE_MODAL
        _NET_WM_STATE_STICKY
        _NET_WM_STATE_MAXIMIZED_VERT
        _NET_WM_STATE_MAXIMIZED_HORZ
        _NET_WM_STATE_SHADED
        _NET_WM_STATE_SKIP_TASKBAR
        _NET_WM_STATE_SKIP_PAGER
        _NET_WM_STATE_HIDDEN
        _NET_WM_STATE_FULLSCREEN
        _NET_WM_STATE_ABOVE
        _NET_WM_STATE_BELOW
        _NET_WM_STATE_DEMANDS_ATTENTION
        _NET_WM_STATE_FOCUSED
        ;; _NET_WM_ACTION hint
        _NET_WM_ACTION_MOVE
        _NET_WM_ACTION_RESIZE
        _NET_WM_ACTION_MINIMIZE
        _NET_WM_ACTION_SHADE
        _NET_WM_ACTION_STICK
        _NET_WM_ACTION_MAXIMIZE_HORZ
        _NET_WM_ACTION_MAXIMIZE_VERT
        _NET_WM_ACTION_FULLSCREEN
        _NET_WM_ACTION_CHANGE_DESKTOP
        _NET_WM_ACTION_CLOSE
        _NET_WM_ACTION_ABOVE
        _NET_WM_ACTION_BELOW
        _NET_WM_WINDOW_OPACITY)
      "EWMH atoms.")

    (dolist (atom xcb:ewmh:-atoms)
      (eval `(defvar ,(intern (concat "xcb:Atom:" (symbol-name atom))) nil))))

  (xcb:ewmh:init exwm--connection t)