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

// -----------------------------------------------------------------------
// keyboard

static
void
keymodifier(struct wl_listener *l, void *data)
{
    Keyboard *keyboard = wl_container_of(l, keyboard, event.modify);

    wlr_seat_set_keyboard(server.input.seat, keyboard->device);
    wlr_seat_keyboard_notify_modifiers(server.input.seat, &keyboard->device->keyboard->modifiers);
}

static
int
keybinding(uint32 modifier, xkb_keysym_t sym)
{
    Key *key;

    for(key=cfg·binding; key!=cfg·endbinding; ++key) {
        if(modifier == key->modifier && sym == key->sym && key->action){
            key->action(&key->arg);
            return 1;
        }
    }
    return 0;
}

static
void
keypress(struct wl_listener *l, void *data)
{
    int    i,h,n;
    uint32 keycode, modifier;
    const  xkb_keysym_t *syms;
    struct Keyboard *keyboard = wl_container_of(l, keyboard, event.press);
    struct wlr_event_keyboard_key *event = data;

    keycode = event->keycode + 8;

    h = 0;
    n = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state, keycode, &syms);

    modifier = wlr_keyboard_get_modifiers(keyboard->device->keyboard);
    if(event->state == WL_KEYBOARD_KEY_STATE_PRESSED) {
        for(i=0; i<n; i++)
            h=keybinding(modifier, syms[i]);
    }

    if(!h) {
        wlr_seat_set_keyboard(server.input.seat, keyboard->device);
        wlr_seat_keyboard_notify_key(server.input.seat, event->time_msec, event->keycode, event->state);
    }
}

static
void
free_keyboard(struct wl_listener *l, void *data)
{
    struct wlr_input_device *device = data;
    Keyboard *keyboard = device->data;

    /* XXX: debug
    wl_list_remove(&keyboard->link);
    wl_list_remove(&keyboard->event.modify.link);
    wl_list_remove(&keyboard->event.press.link);
    wl_list_remove(&keyboard->event.destroy.link);

    free(keyboard);
    */
}

static
void
make_keyboard(struct wlr_input_device *device)
{
    Keyboard *keyboard;
    struct xkb_context *context;
    struct xkb_keymap  *keymap;

    keyboard = device->data = calloc(1, sizeof(*keyboard));
    keyboard->device = device;

    context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
    keymap  = xkb_keymap_new_from_names(context, nil, XKB_KEYMAP_COMPILE_NO_FLAGS);

    wlr_keyboard_set_keymap(device->keyboard, keymap);

    xkb_keymap_unref(keymap);
    xkb_context_unref(context);

    wlr_keyboard_set_repeat_info(device->keyboard, cfg·repeat_rate, cfg·repeat_delay);

    keyboard->event.modify.notify = keymodifier;
    wl_signal_add(&device->keyboard->events.modifiers, &keyboard->event.modify);

    keyboard->event.press.notify = keypress;
    wl_signal_add(&device->keyboard->events.key, &keyboard->event.press);

    keyboard->event.destroy.notify = free_keyboard;
    wl_signal_add(&device->keyboard->events.destroy, &keyboard->event.destroy);

    wlr_seat_set_keyboard(server.input.seat, device);

    wl_list_insert(&server.input.keyboards, &keyboard->link);
}

// -----------------------------------------------------------------------
// cursor

static
void
focus_surface(Client *client, struct wlr_surface *surface, double sx, double sy, uint32 time)
{
    struct timespec now;
    int lift = time;

    if(client && !surface)
        surface = client->xdg->surface;

    if(!surface){
        wlr_seat_pointer_notify_clear_focus(server.input.seat);
        return;
    }

    if(!time) {
        clock_gettime(CLOCK_MONOTONIC, &now);
        time = now.tv_sec * 1000 + now.tv_nsec / 1000000;
    }

    if(surface == server.input.seat->pointer_state.focused_surface) {
        wlr_seat_pointer_notify_motion(server.input.seat, time, sx, sy);
        return;
    }

    wlr_seat_pointer_notify_enter(server.input.seat, surface, sx, sy);

    if(cfg·sloppyfocus && lift)
        focus(client, 0);
}

void
notify_move(uint32 time)
{
    double sx, sy;
    Client *client;
    struct wlr_box box;
    struct wlr_surface *surface;

    if(time) {
        wlr_idle_notify_activity(server.input.idle, server.input.seat);
        if(cfg·sloppyfocus)
            server.monitor.selected = monitor_at(server.cursor.dot->x, server.cursor.dot->y);
    }

    if(server.cursor.mode == CursorMove) {
        resize(server.grab.client,
            server.cursor.dot->x - server.grab.x,
            server.cursor.dot->y - server.grab.y,
            server.grab.client->geometry.width,
            server.grab.client->geometry.height,
            1
        );
        return;
    }

    if(server.cursor.mode == CursorResize) {
        wlr_xdg_surface_get_geometry(server.grab.client->xdg, &box);
        resize(server.grab.client,
            server.grab.box.x - box.x,
            server.grab.box.y - box.y,
            server.cursor.dot->x - server.grab.x - server.grab.box.x,
            server.cursor.dot->y - server.grab.y - server.grab.box.y,
            1
        );
        return;
    }

    /* otherwise, find the client under the pointer and send the event along. */
    client = client_at(server.cursor.dot->x, server.cursor.dot->y);
    if(!client) {
        wlr_xcursor_manager_set_cursor_image(server.cursor.manager, "left_ptr", server.cursor.dot);
        return;
    }

    surface = client_surface_at(
        client,
        server.cursor.dot->x - client->geometry.x - client->border,
        server.cursor.dot->y - client->geometry.y - client->border,
        &sx, &sy
    );

    focus_surface(client, surface, sx, sy, time);
}

void
cursor_move(struct wl_listener *l, void *data)
{
    struct wlr_event_pointer_motion *event = data;
    wlr_cursor_move(server.cursor.dot, event->device, event->delta_x, event->delta_y);
    notify_move(event->time_msec);
}

void
cursor_move_abs(struct wl_listener *l, void *data)
{
    struct wlr_event_pointer_motion_absolute *event = data;
    wlr_cursor_warp_absolute(server.cursor.dot, event->device, event->x, event->y);
    notify_move(event->time_msec);
}

void
cursor_button(struct wl_listener *l, void *data)
{
    Client  *client;
    uint32 modifier;
    Button *button;
    struct wlr_keyboard *keyboard;
    struct wlr_event_pointer_button *event = data;

    wlr_idle_notify_activity(server.input.idle, server.input.seat);

    switch(event->state) {
    case WLR_BUTTON_PRESSED:
        if((client=client_at(server.cursor.dot->x, server.cursor.dot->y)))
            focus(client,1);

        keyboard = wlr_seat_get_keyboard(server.input.seat);
        modifier = wlr_keyboard_get_modifiers(keyboard);
        for(button=cfg·button; button != cfg·endbutton; ++button) {
            if(modifier == button->modifier && event->button == button->code && button->function) {
                button->function(&button->arg);
                return;
            }
        }
        break;
    case WLR_BUTTON_RELEASED:
        if(server.cursor.mode != CursorNormal) {
            wlr_xcursor_manager_set_cursor_image(server.cursor.manager, "left_ptr", server.cursor.dot);
            server.cursor.mode = CursorNormal;
            /* Drop the window off on its new monitor */
            server.monitor.selected = monitor_at(server.cursor.dot->x, server.cursor.dot->y);
            attach(server.grab.client, server.monitor.selected, 0);
            return;
        }
    }

    wlr_seat_pointer_notify_button(server.input.seat, event->time_msec, event->button, event->state);
}

void
cursor_axis(struct wl_listener *l, void *data)
{
    struct wlr_event_pointer_axis *event = data;
    /* Notify the client with pointer focus of the axis event. */
    wlr_seat_pointer_notify_axis(server.input.seat,
            event->time_msec, event->orientation, event->delta,
            event->delta_discrete, event->source);
}

void
cursor_frame(struct wl_listener *l, void *data)
{
    wlr_seat_pointer_notify_frame(server.input.seat);
}

void
request_cursor(struct wl_listener *l, void *data)
{
    struct wlr_seat_pointer_request_set_cursor_event *event = data;
    struct wlr_seat_client *focused = server.input.seat->pointer_state.focused_client;
    if(focused == event->seat_client)
        wlr_cursor_set_surface(server.cursor.dot, event->surface, event->hotspot_x, event->hotspot_y);
}

void
request_set_selection(struct wl_listener *l, void *data)
{
    struct wlr_seat_request_set_selection_event *event = data;
    wlr_seat_set_selection(server.input.seat, event->source, event->serial);
}

static
void
make_pointer(struct wlr_input_device *device)
{
    wlr_cursor_attach_input_device(server.cursor.dot, device);
}

// -----------------------------------------------------------------------
// generic input

void
make_input(struct wl_listener *l, void *data)
{
    uint32 capability;
    struct wlr_input_device *device = data;

    switch(device->type) {
    case WLR_INPUT_DEVICE_KEYBOARD:
        make_keyboard(device);
        break;
    case WLR_INPUT_DEVICE_POINTER:
        make_pointer(device);
        /* fallthrough */
    default:
        break;
    }

    capability = WL_SEAT_CAPABILITY_POINTER;
    if(!wl_list_empty(&server.input.keyboards))
        capability |= WL_SEAT_CAPABILITY_KEYBOARD;
    wlr_seat_set_capabilities(server.input.seat, capability);
}