summaryrefslogtreecommitdiffhomepage
path: root/lib/eos/jao-wayland.el
blob: 8de32cc9ede23424c0f8ef5b8372a80de6cf9c57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
;;; jao-wayland.el --- interacting with wayland compositors  -*- lexical-binding: t; -*-

;; Copyright (C) 2022  jao

;; Author: jao <mail@jao.io>
;; Keywords: convenience

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(require 'jao-shell)
(require 'jao-pdf)
(require 'jao-tracking)

;;; wayland
(defvar jao-wayland-enabled
  (string= "wayland" (or (getenv "XDG_SESSION_TYPE") "")))

(defsubst jao-wayland-type (&rest args)
  (apply #'jao-shell-exec* t "wtype" args))

;;; river
(defvar jao-river-enabled (jao-shell-running-p "river"))
(defun jao-river-enabled-p () jao-river-enabled)

(defsubst jao-river-to-ws (n)
  (jao-wayland-type "-M" "win" (format "%s" n)))

(defsubst jao-river-window-list ()
  (jao-shell-output "lswt -j" (lambda () (let ((json-false nil)) (json-read)))))

(defun jao-river-focused ()
  (seq-some (lambda (w) (and (alist-get 'activated w) w))
            (jao-river-window-list)))

(defsubst jao-river-get-focused-title ()
  (alist-get 'title (jao-river-focused)))

(defun jao-river-focus-window (title &optional rx)
  (let* ((ws (jao-river-window-list))
         (fltr (if rx #'string-match-p #'string=) )
         (w (seq-find (lambda (w) (funcall fltr title (alist-get 'title w "")))
                      ws)))
    (or (alist-get 'activated w)
        (seq-some (lambda (_ignored)
                    (jao-shell-exec "riverctl focus-view next" t)
                    (funcall fltr title (jao-river-get-focused-title)))
                  (and w ws)))))

(defun jao-river-zathura-to-org ()
  (let ((title (jao-river-get-focused-title)))
    (jao-river-to-ws 1)
    (jao-pdf-goto-zathura-org title t)))

(defun jao-river-zathura-kill-link ()
  (when-let* ((title (jao-river-get-focused-title))
              (lnk (jao-pdf-zathura-org-link title)))
    (jao-river-to-ws 1)
    (kill-new lnk)
    (message "Link to %s killed" title)))

(defun jao-river-find-zathura-window (file)
  (let ((frx (regexp-quote (file-name-nondirectory file))))
    (seq-some (lambda (w)
                (and (string-suffix-p ".zathura" (alist-get 'app_id w ""))
                     (string-match-p frx (alist-get 'title w ""))
                     w))
              (jao-river-window-list))))

(defun jao-river-open-with-zathura (file page)
  (let ((wd (jao-river-find-zathura-window file)))
    (jao-river-to-ws 3)
    (or (and wd (jao-river-focus-window (alist-get 'title wd)))
        (jao-shell-exec* "riverctl" "spawn" (jao-pdf-zathura-open-cmd file page)))
    (when page (sit-for 0.2) (jao-wayland-type (format "%dg" page)))))

(defun jao-river-set-wallpaper (f)
  (jao-shell-exec (concat "swaybg -m fill -i " f " &")))

(defun jao-river-restart-i3bar ()
  (interactive)
  (jao-shell-kill-p "i3bar-river")
  (jao-shell-exec "riverctl spawn i3bar-river")
  (sit-for 0.2)
  (jao-tracking-set-log ""))

(defun jao-river-toggle-emacs ()
  (if (member (jao-river-get-focused-title) '("emacsclient" "emacs"))
      (jao-shell-exec "riverctl focus-previous-tags")
    (jao-river-to-ws 1)
    (unless (jao-river-focus-window "^emacs\\(client\\)?" t)
      (jao-shell-exec* "riverctl" "spawn" "efoot"))))

;;; sway
(defun jao-sway-msg (msg)
  (shell-command (format "swaymsg '%s' >/dev/null" msg)))

(defmacro jao-def-swaymsg (name msg)
  `(defun ,(intern (format "jao-sway-%s" name)) ()
     (interactive)
     (jao-sway-msg ,msg)))

(jao-def-swaymsg firefox "[app_id=Firefox] focus")

(defvar jao-sway-enabled (jao-shell-running-p "sway"))

(defconst jao-sway-get-active-title
  "swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true).name'")

(defun jao-sway-get-active-title ()
  (let ((tl (jao-shell-string jao-sway-get-active-title)))
    (and (string-match "\"\\(.+\\)\"" tl) (match-string 1 tl))))

(defun jao-sway-zathura-org ()
  (jao-pdf-goto-zathura-org (jao-sway-get-active-title) t))

(defun jao-sway-open-with-zathura (file page)
  (let ((n (file-name-nondirectory file)))
    (jao-sway-msg "workspace number 3")
    (unless (= 0 (jao-sway-msg (format "[title=\"%s\"] focus" n)))
      (jao-shell-exec (jao-pdf-zathura-open-cmd file page)))
    (when page (sit-for 0.2) (jao-wayland-type (format "%dg" page)))))

(defun jao-sway-set-wallpaper (f)
  (jao-sway-msg (format "output * bg %s fill" f)))

(defun jao-sway-run-or-focus (cmd &optional ws)
  (if (jao-shell-running-p "firefox")
      (jao-sway-msg (format "[app_id=%s] focus" cmd))
    (jao-sway-msg (format "workspace %s" (or ws 2)))
    (start-process-shell-command cmd nil cmd)))

(defun jao-sway-run-or-focus-firefox ()
  (interactive)
  (jao-sway-run-or-focus "firefox"))

(provide 'jao-wayland)
;;; jao-wayland.el ends here