-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi3.el
103 lines (84 loc) · 2.68 KB
/
i3.el
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
(require 'bindat)
(require 'json)
(defconst i3-process-name "*i3-process*"
"Name of the buffer associated with i3
IPC process")
(defun i3-get-tree-layout ()
"Returns a layout tree. See i3 wm IPC
docs for details."
(i3-command 4))
(defun i3-get-workspaces ()
"Returns a list of current workspaces.
See i3 wm IPC docs for details."
(i3-command 1))
(defun i3-command (command &optional
payload)
"Sends command to i3 and returns the
response. See i3 wm IPC docs for
details."
(let ((proc (i3-get-or-make-client))
(length (length payload)))
(process-put proc 'response nil)
(process-put proc 'partial-response
nil)
(process-send-string proc (concat
(bindat-pack i3-msg-spec `((:magic .
"i3-ipc")
(:length . ,length)
(:command . ,command)))
payload))
(while (not (process-get proc
'response))
(accept-process-output proc))
(let ((response (process-get proc
'response)))
(process-put proc 'response nil)
(json-read-from-string response))))
(defun i3-get-or-make-client ()
"Creates or returns a current process
connected to i3 wm IPC."
(unless (boundp 'i3-client)
(setq i3-client (make-network-process
:name i3-process-name :buffer
(get-buffer-create i3-process-name)
:coding '(raw-text-unix . raw-text-unix)
:family 'local
:filter 'i3-response-filter
:sentinel 'i3-sentinel
:service (i3-chomp
(shell-command-to-string "i3
--get-socketpath")))))
i3-client)
;;; Internal functions and data
structures.
(defvar i3-msg-spec
'((:magic str 6)
(:length u32r)
(:command u32r)))
(defun i3-response-filter (proc resp)
(if (not (process-get proc
'partial-response))
(let ((length (cdr (assq :length
(bindat-unpack i3-msg-spec resp))))
(payload (substring resp
14)))
(process-put proc
'partial-response-length length)
(process-put proc
'partial-response payload))
(process-put proc 'partial-response
(concat (process-get proc
'partial-response) resp)))
(when (>= (length (process-get proc
'partial-response)) (process-get proc
'partial-response-length))
(process-put proc 'response
(process-get proc 'partial-response))
(process-put proc 'partial-response
nil)))
(defun i3-sentinel (proc event)
(setq i3-client nil))
(defun i3-chomp (string)
(replace-regexp-in-string "\n$" ""
string))
(provide 'i3)