summary refs log tree commit diff stats
path: root/lib/events.sls
blob: 9bb78eef1536fa8aeed5b0fb93372066bc5c2d7d (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
;;; (acdwm events) --- event handlers

(import (acdwm keys)
        (acdwm xlib)
        (acdwm util)
        (srfi 1))

(export make-drag drag? drag-type drag-event drag-window drag-attrs)
(define-record drag type event window attrs)

(define drag (make-parameter #f))
(define drag-types '(move resize))

(define-public (initialize-drag type event)
  (assert (memq type drag-types))
  (let ((win (and (buttonpress? event)
                  (xevent-xbutton-subwindow event))))
    (and win
         (drag (make-drag type event win (get-window-attributes win))))))

(define-public (cancel-drag)
  (free-xevent (drag-event (drag)))
  (free-xwindowattributes (drag-attrs (drag)))
  (drag #f))

(define-public (handle-events #!optional (dpy (get-display)))
  (let loop ((this (get-next-event dpy)))
    (dprint "handling a " (xevent-type-name (xevent-type this)))
    (cond
     ((keypress? this)
      (handle-keypress this dpy)
      (loop (get-next-event dpy)))
     ((buttonpress? this)
      (handle-buttonpress this)
      (loop (get-next-event dpy)))
     ((buttonrelease? this)
      (handle-buttonrelease this)
      (loop (get-next-event dpy)))
     ((motionnotify? this)
      (handle-motion this)
      (loop (get-next-event dpy)))
     ;; Make sure this is at the end!
     (else
      (loop (get-next-event dpy))))))

;;; Handlers

(define-public (handle-keypress event
                                #!optional (dpy (get-display)))
  (let ((binding (find-keybinding event)))
    (dprint "handle-keypress: " binding)
    (and binding ((keybind-proc binding) event dpy))))

(define-public (handle-buttonpress event
                                   #!optional (dpy (get-display)))
  (let ((binding (find-mousebinding event)))
    (dprint "handle-buttonpress: " binding)
    (and binding ((mousebind-proc binding) event dpy))))

(define-public (handle-buttonrelease event
                                     #!optional (dpy (get-display)))
  (dprint "handle-buttonrelease")
  (cancel-drag))

(define-public (handle-motion event
                              #!optional (dpy (get-display)))
  (dprint "handle-motion")
  (when (drag)
    (when (some? (drag-window (drag)))
      (let ((xd (- (xevent-xbutton-x_root event)
                   (xevent-xbutton-x_root (drag-event (drag)))))
            (yd (- (xevent-xbutton-y_root event)
                   (xevent-xbutton-y_root (drag-event (drag))))))
        (let ((xnew (+ (xwindowattributes-x (drag-attrs (drag)))
                       (if (eq? (drag-type (drag)) 'move) xd 0)))
              (ynew (+ (xwindowattributes-y (drag-attrs (drag)))
                       (if (eq? (drag-type (drag)) 'move) yd 0)))
              (wnew (max 1 (+ (xwindowattributes-width (drag-attrs (drag)))
                              (if (eq? (drag-type (drag)) 'resize) xd 0))))
              (hnew (max 1 (+ (xwindowattributes-height (drag-attrs (drag)))
                              (if (eq? (drag-type (drag)) 'resize) yd 0)))))
          (dprint wnew "x" hnew "+" xnew "+" ynew)
          (xmoveresizewindow dpy (drag-window (drag)) xnew ynew wnew hnew))))))