about summary refs log tree commit diff
path: root/exwm-manage.el
blob: 8bf118e2f0ee3d984e0eeff0178fce25f9cff059 (plain) (blame)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
;;; exwm-manage.el --- Window Management Module for  -*- lexical-binding: t -*-
;;;                    EXWM

;; Copyright (C) 2015 Chris Feng

;; Author: Chris Feng <chris.w.feng@gmail.com>
;; Keywords: unix

;; This file is not part of GNU Emacs.

;; This file 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 file 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 file.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; This is the fundamental module of EXWM that deals with window management.

;;; Code:

(require 'exwm-input)

(defvar exwm-manage-finish-hook nil
  "Normal hook run after a window is just managed, in the context of the
corresponding buffer.")

(defun exwm-manage--update-geometry (id &optional force)
  "Update window geometry."
  (exwm--with-current-id id
    (unless (and exwm--geometry (not force))
      (let ((reply (xcb:+request-unchecked+reply exwm--connection
                       (make-instance 'xcb:GetGeometry :drawable id))))
        (when reply                     ;nil when destroyed
          (setq exwm--geometry reply))))))

(defvar exwm-manage--manage-window-queue nil
  "List of window IDs to prevent race conditions.")

(defun exwm-manage--manage-window (id)
  "Manage window ID."
  (exwm--log "Try to manage #x%x" id)
  (setq exwm-input--focus-lock t)
  (catch 'return
    ;; Ensure it's not managed
    (when (or (assoc id exwm--id-buffer-alist)
              (memq id exwm-manage--manage-window-queue))
      (exwm--log "#x%x is already managed" id)
      (throw 'return 'managed))
    (push id exwm-manage--manage-window-queue) ;prevent reentering
    ;; Ensure it's alive
    (when (xcb:+request-checked+request-check exwm--connection
              (make-instance 'xcb:ChangeWindowAttributes
                             :window id :value-mask xcb:CW:EventMask
                             :event-mask exwm--client-event-mask))
      (setq exwm-manage--manage-window-queue
            (delq id exwm-manage--manage-window-queue)) ;cleanup
      (throw 'return 'dead))
    (setq exwm-manage--manage-window-queue
          (delq id exwm-manage--manage-window-queue)) ;cleanup (late enough)
    (with-current-buffer (generate-new-buffer "*EXWM*")
      (push `(,id . ,(current-buffer)) exwm--id-buffer-alist)
      (exwm-mode)
      (setq exwm--id id)
      (exwm--update-window-type id)
      (exwm--update-class id)
      (exwm-manage--update-geometry id)
      ;; No need to manage (please check OverrideRedirect outside)
      (when (or
             (not
              (or (not exwm-window-type)
                  (memq xcb:Atom:_NET_WM_WINDOW_TYPE_UTILITY exwm-window-type)
                  (memq xcb:Atom:_NET_WM_WINDOW_TYPE_DIALOG exwm-window-type)
                  (memq xcb:Atom:_NET_WM_WINDOW_TYPE_NORMAL exwm-window-type)))
             ;; For Java applications
             (and exwm-instance-name
                  (string-prefix-p "sun-awt-X11-" exwm-instance-name)
                  (not (string-suffix-p "XFramePeer" exwm-instance-name))))
        (exwm--log "No need to manage #x%x" id)
        ;; Remove all events
        (xcb:+request-checked+request-check exwm--connection
            (make-instance 'xcb:ChangeWindowAttributes
                           :window id :value-mask xcb:CW:EventMask
                           :event-mask xcb:EventMask:NoEvent))
        ;; The window needs to be mapped
        (xcb:+request exwm--connection
            (make-instance xcb:MapWindow :window id))
        (with-slots (x y width height) exwm--geometry
          ;; Reparent to virtual root (essential)
          (xcb:+request exwm--connection
              (make-instance 'xcb:ReparentWindow
                             :window id
                             :parent (frame-parameter exwm-workspace--current
                                                      'exwm-window-id)
                             :x x :y y))
          ;; Center window of type _NET_WM_WINDOW_TYPE_SPLASH
          (when (memq xcb:Atom:_NET_WM_WINDOW_TYPE_SPLASH exwm-window-type)
            (xcb:+request exwm--connection
                (make-instance 'xcb:ConfigureWindow
                               :window id
                               :value-mask (logior xcb:ConfigWindow:X
                                                   xcb:ConfigWindow:Y)
                               :x (/ (- (x-display-pixel-width) width) 2)
                               :y (/ (- (x-display-pixel-height) height) 2)))))
        (xcb:flush exwm--connection)
        (setq kill-buffer-query-functions nil)
        (setq exwm--id-buffer-alist (assq-delete-all id exwm--id-buffer-alist))
        (kill-buffer (current-buffer))
        (throw 'return 'ignored))
      ;; Manage the window
      (exwm--log "Manage #x%x" id)
      (xcb:+request exwm--connection    ;remove border
          (make-instance 'xcb:ConfigureWindow
                         :window id :value-mask xcb:ConfigWindow:BorderWidth
                         :border-width 0))
      (xcb:+request exwm--connection ;grab buttons for set focus/move/resize
          (make-instance 'xcb:GrabButton
                         :owner-events 0 :grab-window id
                         :event-mask xcb:EventMask:ButtonPress
                         :pointer-mode xcb:GrabMode:Sync
                         :keyboard-mode xcb:GrabMode:Async
                         :confine-to xcb:Window:None
                         :cursor xcb:Cursor:None
                         :button xcb:ButtonIndex:Any
                         :modifiers xcb:ModMask:Any))
      (xcb:flush exwm--connection)
      (exwm--update-title id)
      (exwm--update-transient-for id)
      (exwm--update-normal-hints id)
      (exwm--update-hints id)
      (exwm--update-protocols id)
      (exwm--update-state id)
      (if (or exwm-transient-for exwm--fixed-size
              (memq xcb:Atom:_NET_WM_WINDOW_TYPE_UTILITY exwm-window-type)
              (memq xcb:Atom:_NET_WM_WINDOW_TYPE_DIALOG exwm-window-type))
          (exwm-floating--set-floating id)
        (exwm-floating--unset-floating id))
      (exwm-input-grab-keyboard id)
      (exwm-workspace--update-switch-history)
      (setq exwm-input--focus-lock nil) ;unlocked in advance
      (exwm--with-current-id id
        (run-hooks 'exwm-manage-finish-hook))))
  (setq exwm-input--focus-lock nil))

(defun exwm-manage--unmanage-window (id &optional withdraw-only)
  "Unmanage window ID."
  (let ((buffer (exwm--id->buffer id)))
    (exwm--log "Unmanage #x%x (buffer: %s)" id buffer)
    (setq exwm--id-buffer-alist (assq-delete-all id exwm--id-buffer-alist))
    (when (buffer-live-p buffer)
      (with-current-buffer buffer
        (exwm-workspace--update-switch-history)
        ;;
        (when withdraw-only
          ;; Reparent back to root
          (xcb:+request exwm--connection
              (make-instance 'xcb:ChangeWindowAttributes
                             :window id :value-mask xcb:CW:EventMask
                             :event-mask xcb:EventMask:NoEvent))
          (let (x y geometry geometry-parent)
            (if (not exwm--floating-frame)
                (setq x 0 y 0)          ;the position does not matter
              (setq geometry-parent
                    (xcb:+request-unchecked+reply exwm--connection
                        (make-instance 'xcb:GetGeometry
                                       :drawable
                                       (frame-parameter exwm--floating-frame
                                                        'exwm-outer-id)))
                    geometry (xcb:+request-unchecked+reply exwm--connection
                                 (make-instance 'xcb:GetGeometry
                                                :drawable id)))
              (if (not (and geometry-parent geometry))
                  (setq x 0 y 0)        ;e.g. have been destroyed
                (setq x (+ (slot-value geometry-parent 'x)
                           (slot-value geometry 'x))
                      y (+ (slot-value geometry-parent 'y)
                           (slot-value geometry 'y)))))
            (xcb:+request exwm--connection
                (make-instance 'xcb:ReparentWindow
                               :window id :parent exwm--root :x x :y y)))
          ;; Delete WM_STATE property
          (xcb:+request exwm--connection
              (make-instance 'xcb:DeleteProperty
                             :window id :property xcb:Atom:WM_STATE))
          (xcb:flush exwm--connection))
        (setq kill-buffer-query-functions nil)
        (let ((floating exwm--floating-frame))
          (kill-buffer)
          (when floating
            (if (eq 'exwm-mode
                    (with-current-buffer
                        (window-buffer
                         (frame-first-window exwm-workspace--current))
                      major-mode))
                ;; Input focus is to be set on a window
                (x-focus-frame exwm-workspace--current)
              ;; Set input focus on a frame
              (select-frame-set-input-focus exwm-workspace--current))))))))

(defun exwm-manage--scan ()
  "Search for existing windows and try to manage them."
  (let* ((tree (xcb:+request-unchecked+reply exwm--connection
                   (make-instance 'xcb:QueryTree :window exwm--root))))
    (dolist (i (slot-value tree 'children))
      (with-slots (override-redirect map-state)
          (xcb:+request-unchecked+reply exwm--connection
              (make-instance 'xcb:GetWindowAttributes :window i))
        (when (and (= 0 override-redirect) (= xcb:MapState:Viewable map-state))
          (exwm-manage--manage-window i))))))

(defvar exwm-manage--ping-lock nil
  "Non-nil indicates EXWM is pinging a window.")
(defvar exwm-manage-ping-timeout 3 "Seconds to wait before killing a client.")

(defun exwm-manage--close-window (id &optional buffer)
  "Close window ID in a proper way."
  (catch 'return
    (unless buffer (setq buffer (exwm--id->buffer id)))
    ;; Destroy the client window if it does not support WM_DELETE_WINDOW
    (unless (and (buffer-live-p buffer)
                 (with-current-buffer buffer
                   (memq xcb:Atom:WM_DELETE_WINDOW exwm--protocols)))
      (xcb:+request exwm--connection
          (make-instance 'xcb:DestroyWindow :window id))
      (xcb:flush exwm--connection)
      (throw 'return nil))
    ;; Try to close the window with WM_DELETE_WINDOW client message
    (xcb:+request exwm--connection
        (make-instance 'xcb:icccm:SendEvent
                       :destination id
                       :event (xcb:marshal
                               (make-instance 'xcb:icccm:WM_DELETE_WINDOW
                                              :window id)
                               exwm--connection)))
    (xcb:flush exwm--connection)
    ;; Try to determine if the client stop responding
    ;; FIXME: check
    (with-current-buffer buffer
      (when (memq xcb:Atom:_NET_WM_PING exwm--protocols)
        (setq exwm-manage--ping-lock t)
        (xcb:+request exwm--connection
            (make-instance 'xcb:SendEvent
                           :propagate 0 :destination id
                           :event-mask xcb:EventMask:NoEvent
                           :event (xcb:marshal
                                   (make-instance 'xcb:ewmh:_NET_WM_PING
                                                  :window id :timestamp 0
                                                  :client-window id)
                                   exwm--connection)))
        (xcb:flush exwm--connection)
        (with-timeout (exwm-manage-ping-timeout
                       (if (yes-or-no-p (format "\
`%s' is not responding. Would you like to kill it? " (buffer-name buffer)))
                           (progn (exwm-manage--kill-client id)
                                  (throw 'return nil))
                         (throw 'return nil)))
          (while (and exwm-manage--ping-lock
                      (exwm--id->buffer id)) ;may have be destroyed
            (accept-process-output nil 0.1)))
        (throw 'return nil)))
    (throw 'return nil)))

(defun exwm-manage--kill-client (&optional id)
  "Kill an X client."
  (interactive)
  (unless id (setq id (exwm--buffer->id (current-buffer))))
  (let ((pid (slot-value
              (xcb:+request-unchecked+reply exwm--connection
                  (make-instance 'xcb:ewmh:get-_NET_WM_PID :window id))
              'value)))
    (if pid
        (signal-process pid 'SIGKILL)
      (xcb:+request exwm--connection
          (make-instance 'xcb:KillClient :resource id))
      (xcb:flush exwm--connection))))

(defun exwm-manage--on-ConfigureRequest (data synthetic)
  "Handle ConfigureRequest event."
  (let ((obj (make-instance 'xcb:ConfigureRequest))
        buffer edges)
    (xcb:unmarshal obj data)
    (with-slots (window x y width height border-width) obj
      (if (setq buffer (exwm--id->buffer window))
          ;; Send client message for managed windows
          (with-current-buffer buffer
            (setq edges
                  (if exwm--fullscreen
                      (list 0 0
                            (x-display-pixel-width) (x-display-pixel-height))
                    (or exwm--floating-edges
                        (window-inside-absolute-pixel-edges
                         (get-buffer-window)))))
            (xcb:+request exwm--connection
                (make-instance 'xcb:SendEvent
                               :propagate 0 :destination window
                               :event-mask xcb:EventMask:StructureNotify
                               :event (xcb:marshal
                                       (make-instance
                                        'xcb:ConfigureNotify
                                        :event window :window window
                                        :above-sibling xcb:Window:None
                                        :x (elt edges 0) :y (elt edges 1)
                                        :width (- (elt edges 2) (elt edges 0))
                                        :height (- (elt edges 3) (elt edges 1))
                                        :border-width 0 :override-redirect 0)
                                       exwm--connection))))
        ;; Configure unmanaged windows
        (xcb:+request exwm--connection
            (make-instance 'xcb:ConfigureWindow
                           :window window
                           :value-mask (logior xcb:ConfigWindow:X
                                               xcb:ConfigWindow:Y
                                               xcb:ConfigWindow:Width
                                               xcb:ConfigWindow:Height
                                               xcb:ConfigWindow:BorderWidth)
                           :x x :y y :width width :height height
                           :border-width border-width)))))
  (xcb:flush exwm--connection))

(defun exwm-manage--on-MapRequest (data synthetic)
  "Handle MapRequest event."
  (let ((obj (make-instance 'xcb:MapRequest)))
    (xcb:unmarshal obj data)
    (exwm-manage--manage-window (slot-value obj 'window))))

(defun exwm-manage--on-UnmapNotify (data synthetic)
  "Handle UnmapNotify event."
  (unless synthetic
    (let ((obj (make-instance 'xcb:UnmapNotify)))
      (xcb:unmarshal obj data)
      (exwm-manage--unmanage-window (slot-value obj 'window) t))))

(defun exwm-manage--on-DestroyNotify (data synthetic)
  "Handle DestroyNotify event."
  (unless synthetic
    (let ((obj (make-instance 'xcb:DestroyNotify)))
      (xcb:unmarshal obj data)
      (exwm-manage--unmanage-window (slot-value obj 'window)))))

(defun exwm-manage--init ()
  "Initialize manage module."
  (xcb:+event exwm--connection 'xcb:ConfigureRequest
              'exwm-manage--on-ConfigureRequest)
  (xcb:+event exwm--connection 'xcb:MapRequest 'exwm-manage--on-MapRequest)
  (xcb:+event exwm--connection 'xcb:UnmapNotify 'exwm-manage--on-UnmapNotify)
  (xcb:+event exwm--connection 'xcb:DestroyNotify
              'exwm-manage--on-DestroyNotify))



(provide 'exwm-manage)

;;; exwm-manage.el ends here