aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/dwm/input.c
blob: b035e9a6fca3b46a42f43304df9441ecbdb35a2e (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
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
#include "dwm.h"

/* local callbacks */
static void scroll(struct wl_listener *ev,  void *arg);
static void frame(struct wl_listener *ev,   void *arg);
static void click(struct wl_listener *ev,  void *arg);
static void relmove(struct wl_listener *ev, void *arg);
static void absmove(struct wl_listener *ev, void *arg);

static void setcursor(struct wl_listener *ev, void *arg);
static void setsel(struct wl_listener *ev, void *arg);
static void setpsel(struct wl_listener *ev, void *arg);

static void keypress(struct wl_listener *ev, void *arg);
static void modifier(struct wl_listener *ev, void *arg);

/* global variables */
Mouse mouse = {
    .mode    = 0,
    .manager = nil,
    .cursor  = nil,
    .ev = {
        .axis       = {.notify=scroll},
        .button     = {.notify=click},
        .frame      = {.notify=frame},
        .motion     = {.notify=relmove},
        .absmotion  = {.notify=absmove},
        .cursor     = {.notify=setcursor},
        .sel        = {.notify=setsel},
        .psel       = {.notify=setpsel},
    },
};

Grab grab = { 0 };

// -----------------------------------------------------------------------
// callback function implementations

/* input devices */
static
void
newkeyboard(struct wlr_input_device *dev)
{
    Keyboard *kb;
    struct xkb_context *ctx;
    struct xkb_keymap  *map;

    kb = dev->data = calloc(1, sizeof(*kb));
    kb->dev = dev;

    ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
    map = xkb_map_new_from_names(ctx, &xkb_rules, XKB_KEYMAP_COMPILE_NO_FLAGS);

    wlr_keyboard_set_keymap(dev->keyboard, map);
    xkb_keymap_unref(map);
    xkb_context_unref(ctx);

	wlr_keyboard_set_repeat_info(dev->keyboard, 25, 600);

    /* install callbacks */
    kb->ev.modifier.notify = modifier;
    kb->ev.keypress.notify = keypress;

	wl_signal_add(&dev->keyboard->events.modifiers, &kb->ev.modifier);
	wl_signal_add(&dev->keyboard->events.key, &kb->ev.keypress);

    /* NOTE: we only look at the last keyboard given right now */
	wlr_seat_set_keyboard(dwm.seat, dev);

	wl_list_insert(&dwm.keyboards, &kb->link);
}

static
void
newmouse(struct wlr_input_device *dev)
{
    /* NOTE: we only look at one mouse right now */
	wlr_cursor_attach_input_device(mouse.cursor, dev);
}

void
evยทnewinput(struct wl_listener *ev, void *arg)
{
    uint32 c;
    struct wlr_input_device *dev;

    dev = arg;
    switch (dev->type) {
    case WLR_INPUT_DEVICE_KEYBOARD:
        newkeyboard(dev);
        break;
    case WLR_INPUT_DEVICE_POINTER:
        newmouse(dev);
        break;
    default:
        ;
    }

    c = WL_SEAT_CAPABILITY_POINTER;
    if (!wl_list_empty(&dwm.keyboards))
        c |= WL_SEAT_CAPABILITY_KEYBOARD;
    wlr_seat_set_capabilities(dwm.seat, c);
}

/* mouse input */

static
void
scroll(struct wl_listener *ev, void *arg)
{
    struct wlr_event_pointer_axis *axis;

    axis = arg;
    wlr_seat_pointer_notify_axis(dwm.seat, 
            axis->time_msec,
            axis->orientation,
            axis->delta,
            axis->delta_discrete,
            axis->source);
}

void
click(struct wl_listener *ev, void *arg)
{
    uint32 mods;
    Client *c;
    const Button *b;
    struct wlr_surface  *surf;
    struct wlr_keyboard *keyb; 
    struct wlr_event_pointer_button *button;

    button = arg;
    switch (button->state) {
    case WLR_BUTTON_PRESSED:
        if ((c = clientat(mouse.cursor->x, mouse.cursor->y))) {
            surf = wlr_xdg_surface_surface_at(c->surf, 
                    mouse.cursor->x - c->dim.x - c->bw,
                    mouse.cursor->y - c->dim.y - c->bw,
                    nil, nil
            );
            setfocus(c, surf, 1);
        }

        keyb = wlr_seat_get_keyboard(dwm.seat);
        mods = wlr_keyboard_get_modifiers(keyb);
		for (b = buttons; b < arrend(buttons); b++) {
			if (CLEANMASK(mods) == CLEANMASK(b->mod) &&
					button->button == b->kind && b->func) {
				b->func(&b->arg);
				return;
			}
		}
        break;

    case WLR_BUTTON_RELEASED:
        if (mouse.mode != MouseNormal) {
            wlr_xcursor_manager_set_cursor_image(mouse.manager, "left_ptr", mouse.cursor);
            mouse.mode = MouseNormal;
            monitor = monitorat(mouse.cursor->x, mouse.cursor->y);
            setmonitor(grab.c, monitor, 0);
        }
    }

	wlr_seat_pointer_notify_button(dwm.seat, button->time_msec, button->button, button->state);
}

static
void
frame(struct wl_listener *ev, void *arg)
{
    wlr_seat_pointer_notify_frame(dwm.seat);
}

static
void
notify(uint32 time)
{
    Client *c;
    double sx, sy;
    struct wlr_surface *surf;

    if (sloppyfocus)
        monitor = monitorat(mouse.cursor->x, mouse.cursor->y);

    switch (mouse.mode) {
    case MouseMove:
        resize(grab.c, 
                mouse.cursor->x - grab.x, 
                mouse.cursor->y - grab.y, 
                grab.c->dim.width, grab.c->dim.height, 1);
        return;

    case MouseResize:
        resize(grab.c, 
                grab.c->dim.x, grab.c->dim.y, 
                mouse.cursor->x - grab.c->dim.x, 
                mouse.cursor->y - grab.c->dim.y, 1);
        return;

    case MouseNormal:
    default:
        ;
    }

    surf = nil;
	if ((c = clientat(mouse.cursor->x, mouse.cursor->y)))
		surf = wlr_xdg_surface_surface_at(c->surf, 
                mouse.cursor->x - c->dim.x - c->bw, 
                mouse.cursor->y - c->dim.y - c->bw, 
                &sx, &sy);

	if (!surf)
		wlr_xcursor_manager_set_cursor_image(mouse.manager, "left_ptr", mouse.cursor);

	pointerfocus(c, surf, sx, sy, time);
}

static
void
relmove(struct wl_listener *ev, void *arg)
{
    struct wlr_event_pointer_motion *mv;

    mv = arg;
    wlr_cursor_move(mouse.cursor, mv->device, mv->delta_x, mv->delta_y);
    notify(mv->time_msec);
}

static
void
absmove(struct wl_listener *ev, void *arg)
{
    struct wlr_event_pointer_motion_absolute *mv;

    mv = arg;
    wlr_cursor_warp_absolute(mouse.cursor, mv->device, mv->x, mv->y);
    notify(mv->time_msec);
}

/* cursor images */
static
void
setcursor(struct wl_listener *ev, void *arg)
{
    struct wlr_seat_pointer_request_set_cursor_event *cur;

    cur = arg;
    if (mouse.mode != MouseNormal)
        return;

    if (cur->seat_client == dwm.seat->pointer_state.focused_client)
        wlr_cursor_set_surface(mouse.cursor, cur->surface, cur->hotspot_x, cur->hotspot_y);
}

static
void
setpsel(struct wl_listener *ev, void *arg)
{
	struct wlr_seat_request_set_primary_selection_event *psel;

    psel = arg;
	wlr_seat_set_primary_selection(dwm.seat, psel->source, psel->serial);
}

static
void
setsel(struct wl_listener *ev, void *arg)
{
	struct wlr_seat_request_set_selection_event *sel;

    sel = arg;
	wlr_seat_set_selection(dwm.seat, sel->source, sel->serial);
}

/* keyboards */

static
int
dokey(uint32 mods, xkb_keysym_t sym)
{
    int h;
    const Key *k, *e;

    h = 0;
    for (k = keys, e = arrend(keys); k < e; k++)
        if (CLEANMASK(mods) == CLEANMASK(k->mod) && sym == k->sym && k->func) {
            k->func(&k->arg);
            h = 1;
        }

    return h;
}

static
void
modifier(struct wl_listener *ev, void *arg)
{
    Keyboard *kb;

    kb = wl_container_of(ev, kb, ev.modifier);
    wlr_seat_set_keyboard(dwm.seat, kb->dev);

    wlr_seat_keyboard_notify_modifiers(dwm.seat, &kb->dev->keyboard->modifiers);
}

static
void
keypress(struct wl_listener *ev, void *arg)
{
    Keyboard *kb;
    int i, n, hit;
    uint32 code, mods;
    const xkb_keysym_t *syms;
    struct wlr_event_keyboard_key *key;

    key = arg; 
    kb  = wl_container_of(ev, kb, ev.keypress);

    code = key->keycode + 8;
    n    = xkb_state_key_get_syms(kb->dev->keyboard->xkb_state, code, &syms);
    mods = wlr_keyboard_get_modifiers(kb->dev->keyboard);

    hit = 0;
    if (key->state == WLR_KEY_PRESSED)
        for (i = 0; i < n; i++)
            hit += dokey(mods, syms[i]); 

    /* if no binding found, pass the event to the client */
    if (!hit) {
        wlr_seat_set_keyboard(dwm.seat, kb->dev);
		wlr_seat_keyboard_notify_key(dwm.seat, key->time_msec, key->keycode, key->state);
    }
}