neuswc

patched neuswc library (tracked)
git clone https://git.daat.foo/neuswc.git
Log | Files | Refs | README | LICENSE

seat_ws.c (15012B)


      1 /* swc: libswc/seat_ws.c
      2  *
      3  * Copyright (c) 2013, 2014 Michael Forney
      4  * Copyright (c) 2019 Nia Alarie
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a copy
      7  * of this software and associated documentation files (the "Software"), to deal
      8  * in the Software without restriction, including without limitation the rights
      9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     10  * copies of the Software, and to permit persons to whom the Software is
     11  * furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included in
     14  * all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     22  * SOFTWARE.
     23  */
     24 
     25 #include "compositor.h"
     26 #include "data_device.h"
     27 #include "event.h"
     28 #include "internal.h"
     29 #include "keyboard.h"
     30 #include "launch.h"
     31 #include "pointer.h"
     32 #include "screen.h"
     33 #include "seat.h"
     34 #include "surface.h"
     35 #include "util.h"
     36 #include "wscons/atKeynames.h"
     37 #include "wscons/bsd_KbdMap.h"
     38 
     39 #include <errno.h>
     40 #include <fcntl.h>
     41 #include <stdbool.h>
     42 #include <stdio.h>
     43 #include <stdlib.h>
     44 #include <string.h>
     45 #include <unistd.h>
     46 
     47 #include <dev/wscons/wsconsio.h>
     48 #include <dev/wscons/wsksymdef.h>
     49 #include <sys/ioctl.h>
     50 #include <time.h>
     51 
     52 #ifdef __OpenBSD__
     53 struct ws_keyboards;
     54 struct ws_keyboards *
     55 ws_keyboards_open(struct keyboard **, const bool *, kbd_t *);
     56 void
     57 ws_keyboards_reset(struct ws_keyboards *);
     58 void
     59 ws_keyboards_close(struct ws_keyboards *);
     60 #endif
     61 
     62 /* Map wscons encodings to libxkbcommon layout names. */
     63 struct ws_xkb_map {
     64 	const int ws;
     65 	const char *const xkb;
     66 };
     67 
     68 static const struct ws_xkb_map ws_xkb_encodings[] = {
     69     {KB_UK, "gb"}, {KB_BE, "be"},
     70 #ifdef KB_CZ
     71     {KB_CZ, "cz"},
     72 #endif
     73     {KB_DK, "dk"}, {KB_NL, "nl"}, {KB_DE, "de"},
     74 #ifdef KB_GR
     75     {KB_GR, "gr"},
     76 #endif
     77     {KB_HU, "hu"}, {KB_IT, "it"}, {KB_JP, "jp"}, {KB_NO, "no"}, {KB_PL, "pl"},
     78     {KB_PT, "pt"}, {KB_RU, "ru"}, {KB_ES, "es"}, {KB_SV, "sv"}, {KB_SG, "sg"},
     79     {KB_TR, "tr"}, {KB_UA, "ua"}, {-1, NULL}};
     80 
     81 struct seat {
     82 	struct swc_seat base;
     83 
     84 	char *name;
     85 	uint32_t capabilities;
     86 
     87 	int mouse_fd;
     88 	bool ignore;
     89 #ifdef __OpenBSD__
     90 	struct ws_keyboards *keyboards;
     91 #else
     92 	int kbd_fd;
     93 	unsigned kbd_type;
     94 #endif
     95 
     96 	struct xkb_rule_names names;
     97 
     98 	struct wl_event_source *mouse_source;
     99 #ifndef __OpenBSD__
    100 	struct wl_event_source *kbd_source;
    101 #endif
    102 
    103 	struct wl_listener swc_listener;
    104 
    105 	struct wl_listener keyboard_focus_listener;
    106 	struct pointer pointer;
    107 	struct wl_listener data_device_listener;
    108 
    109 	struct wl_global *global;
    110 	struct wl_list resources;
    111 };
    112 
    113 static void
    114 handle_keyboard_focus_event(struct wl_listener *listener, void *data)
    115 {
    116 	struct seat *seat =
    117 	    wl_container_of(listener, seat, keyboard_focus_listener);
    118 	struct event *ev = data;
    119 	struct input_focus_event_data *event_data = ev->data;
    120 
    121 	if (ev->type != INPUT_FOCUS_EVENT_CHANGED) {
    122 		return;
    123 	}
    124 
    125 	if (event_data->new) {
    126 		struct wl_client *client =
    127 		    wl_resource_get_client(event_data->new->surface->resource);
    128 
    129 		/* Offer the selection to the new focus. */
    130 		data_device_offer_selection(seat->base.data_device, client);
    131 	}
    132 }
    133 
    134 static void
    135 handle_data_device_event(struct wl_listener *listener, void *data)
    136 {
    137 	struct seat *seat = wl_container_of(listener, seat, data_device_listener);
    138 	struct event *ev = data;
    139 
    140 	if (ev->type != DATA_DEVICE_EVENT_SELECTION_CHANGED) {
    141 		return;
    142 	}
    143 
    144 	if (seat->base.keyboard->focus.client) {
    145 		data_device_offer_selection(seat->base.data_device,
    146 		                            seat->base.keyboard->focus.client);
    147 	}
    148 }
    149 
    150 static void
    151 handle_swc_event(struct wl_listener *listener, void *data)
    152 {
    153 	struct seat *seat = wl_container_of(listener, seat, swc_listener);
    154 	struct event *ev = data;
    155 
    156 	switch (ev->type) {
    157 	case SWC_EVENT_DEACTIVATED:
    158 		seat->ignore = true;
    159 		keyboard_reset(seat->base.keyboard);
    160 #ifdef __OpenBSD__
    161 		ws_keyboards_reset(seat->keyboards);
    162 #endif
    163 		break;
    164 	case SWC_EVENT_ACTIVATED:
    165 		seat->ignore = false;
    166 		break;
    167 	}
    168 }
    169 
    170 /* Wayland Seat Interface */
    171 static void
    172 get_pointer(struct wl_client *client, struct wl_resource *resource, uint32_t id)
    173 {
    174 	struct seat *seat = wl_resource_get_user_data(resource);
    175 
    176 	pointer_bind(&seat->pointer, client, wl_resource_get_version(resource), id);
    177 }
    178 
    179 static void
    180 get_keyboard(struct wl_client *client, struct wl_resource *resource,
    181              uint32_t id)
    182 {
    183 	struct seat *seat = wl_resource_get_user_data(resource);
    184 
    185 	keyboard_bind(seat->base.keyboard, client,
    186 	              wl_resource_get_version(resource), id);
    187 }
    188 
    189 static void
    190 get_touch(struct wl_client *client, struct wl_resource *resource, uint32_t id)
    191 {
    192 	/* XXX: Implement */
    193 }
    194 
    195 static struct wl_seat_interface seat_impl = {
    196     .get_pointer = get_pointer,
    197     .get_keyboard = get_keyboard,
    198     .get_touch = get_touch,
    199     .release = destroy_resource,
    200 };
    201 
    202 static void
    203 bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
    204 {
    205 	struct seat *seat = data;
    206 	struct wl_resource *resource;
    207 
    208 	if (version > 5) {
    209 		version = 5;
    210 	}
    211 
    212 	resource = wl_resource_create(client, &wl_seat_interface, version, id);
    213 	wl_resource_set_implementation(resource, &seat_impl, seat,
    214 	                               &remove_resource);
    215 	wl_list_insert(&seat->resources, wl_resource_get_link(resource));
    216 
    217 	if (version >= 2) {
    218 		wl_seat_send_name(resource, seat->name);
    219 	}
    220 
    221 	wl_seat_send_capabilities(resource, seat->capabilities);
    222 }
    223 
    224 int
    225 ws_to_xkb(unsigned type, int key)
    226 {
    227 	const uint8_t *map;
    228 	size_t count;
    229 
    230 	switch (type) {
    231 	case WSKBD_TYPE_PC_XT:
    232 	case WSKBD_TYPE_PC_AT:
    233 		map = wsXtMap;
    234 		count = sizeof(wsXtMap);
    235 		break;
    236 	case WSKBD_TYPE_USB:
    237 #ifdef WSKBD_TYPE_MAPLE
    238 	case WSKBD_TYPE_MAPLE:
    239 #endif
    240 		map = wsUsbMap;
    241 		count = sizeof(wsUsbMap);
    242 		break;
    243 	default:
    244 		return -1;
    245 	}
    246 
    247 	if (key < 0 || (size_t)key >= count || map[key] == KEY_NOTUSED) {
    248 		return -1;
    249 	}
    250 	return map[key];
    251 }
    252 
    253 static int
    254 wsmouse_to_evdev(int button)
    255 {
    256 	/* The right and middle mouse buttons must be swapped. */
    257 	switch (button) {
    258 	case 1: /* Middle */
    259 		return 0x112;
    260 	case 2: /* Right */
    261 		return 0x111;
    262 	default:
    263 		return button + 0x110;
    264 	}
    265 }
    266 
    267 static int
    268 handle_ws_data(int fd, uint32_t mask, void *data)
    269 {
    270 	struct seat *seat = data;
    271 	struct wscons_event ev;
    272 
    273 	while (!seat->ignore && read(fd, &ev, sizeof(ev)) == sizeof(ev)) {
    274 		uint32_t state, time;
    275 		int key;
    276 		wl_fixed_t pos;
    277 
    278 		time =
    279 		    (uint32_t)(ev.time.tv_sec * 1000ULL + ev.time.tv_nsec / 1000000L);
    280 		switch (ev.type) {
    281 #ifndef __OpenBSD__
    282 		case WSCONS_EVENT_KEY_UP:
    283 		case WSCONS_EVENT_KEY_DOWN:
    284 			state = ev.type == WSCONS_EVENT_KEY_DOWN
    285 			            ? WL_KEYBOARD_KEY_STATE_PRESSED
    286 			            : WL_KEYBOARD_KEY_STATE_RELEASED;
    287 			key = ws_to_xkb(seat->kbd_type, ev.value);
    288 			if (key >= 0) {
    289 				keyboard_handle_key(seat->base.keyboard, time, key, state);
    290 			}
    291 			break;
    292 		case WSCONS_EVENT_ALL_KEYS_UP:
    293 			keyboard_reset(seat->base.keyboard);
    294 			break;
    295 #endif
    296 		case WSCONS_EVENT_MOUSE_UP:
    297 			state = WL_POINTER_BUTTON_STATE_RELEASED;
    298 			key = wsmouse_to_evdev(ev.value);
    299 			pointer_handle_button(seat->base.pointer, time, key, state);
    300 			break;
    301 		case WSCONS_EVENT_MOUSE_DOWN:
    302 			state = WL_POINTER_BUTTON_STATE_PRESSED;
    303 			key = wsmouse_to_evdev(ev.value);
    304 			pointer_handle_button(seat->base.pointer, time, key, state);
    305 			break;
    306 		case WSCONS_EVENT_MOUSE_DELTA_X:
    307 			pos = wl_fixed_from_int(ev.value);
    308 			pointer_handle_relative_motion(seat->base.pointer, time, pos, 0);
    309 			break;
    310 		case WSCONS_EVENT_MOUSE_DELTA_Y:
    311 			pos = wl_fixed_from_int(-ev.value);
    312 			pointer_handle_relative_motion(seat->base.pointer, time, 0, pos);
    313 			break;
    314 		case WSCONS_EVENT_MOUSE_DELTA_Z:
    315 			pos = wl_fixed_from_int(ev.value * 10);
    316 			pointer_handle_axis(
    317 			    seat->base.pointer, time, WL_POINTER_AXIS_VERTICAL_SCROLL,
    318 			    WL_POINTER_AXIS_SOURCE_WHEEL, pos, ev.value * 120);
    319 			break;
    320 		case WSCONS_EVENT_MOUSE_DELTA_W:
    321 			pos = wl_fixed_from_int(ev.value * 10);
    322 			pointer_handle_axis(
    323 			    seat->base.pointer, time, WL_POINTER_AXIS_HORIZONTAL_SCROLL,
    324 			    WL_POINTER_AXIS_SOURCE_WHEEL, pos, ev.value * 120);
    325 			break;
    326 		case WSCONS_EVENT_MOUSE_ABSOLUTE_X:
    327 			pos = wl_fixed_from_int(ev.value);
    328 			pointer_handle_absolute_motion(seat->base.pointer, time, pos, 0);
    329 			break;
    330 		case WSCONS_EVENT_MOUSE_ABSOLUTE_Y:
    331 			pos = wl_fixed_from_int(-ev.value);
    332 			pointer_handle_absolute_motion(seat->base.pointer, time, 0, pos);
    333 			break;
    334 		}
    335 		pointer_handle_frame(seat->base.pointer);
    336 	}
    337 	return 0;
    338 }
    339 
    340 static bool
    341 initialize_wscons(struct seat *seat)
    342 {
    343 #ifdef WSMOUSE_EVENT_VERSION
    344 	int mouse_ver = WSMOUSE_EVENT_VERSION;
    345 #endif
    346 #if !defined(__OpenBSD__) && defined(WSKBDIO_EVENT_VERSION)
    347 	int kbd_ver = WSKBDIO_EVENT_VERSION;
    348 #endif
    349 	int encoding_layout;
    350 	kbd_t encoding = 0;
    351 	unsigned i;
    352 
    353 	if ((seat->mouse_fd =
    354 	         launch_open_device("/dev/wsmouse", O_RDWR | O_NONBLOCK)) == -1) {
    355 		ERROR("Could not open mouse device\n");
    356 		goto error0;
    357 	}
    358 #ifdef __OpenBSD__
    359 	seat->keyboards =
    360 	    ws_keyboards_open(&seat->base.keyboard, &seat->ignore, &encoding);
    361 	if (!seat->keyboards) {
    362 		goto error1;
    363 	}
    364 #else
    365 	if ((seat->kbd_fd =
    366 	         launch_open_device("/dev/wskbd", O_RDWR | O_NONBLOCK)) == -1) {
    367 		ERROR("Could not open keyboard device\n");
    368 		goto error1;
    369 	}
    370 #endif
    371 
    372 #ifdef WSMOUSEIO_SETVERSION
    373 	(void)ioctl(seat->mouse_fd, WSMOUSEIO_SETVERSION, &mouse_ver);
    374 #endif
    375 #if !defined(__OpenBSD__) && defined(WSKBDIO_SETVERSION)
    376 	(void)ioctl(seat->kbd_fd, WSKBDIO_SETVERSION, &kbd_ver);
    377 #endif
    378 
    379 	/* set devices to nativemode to receive events */
    380 #ifdef WSMOUSEIO_SETMODE
    381 	{
    382 		int mode = WSMOUSE_COMPAT; /* use compat mode; it sends events */
    383 		if (ioctl(seat->mouse_fd, WSMOUSEIO_SETMODE, &mode) == -1) {
    384 			fprintf(stderr, "wscons: WSMOUSEIO_SETMODE failed: %s\n",
    385 			        strerror(errno));
    386 		}
    387 	}
    388 #endif /* WSMOUSEIO_SETMODE */
    389 #ifndef __OpenBSD__
    390 #ifdef WSKBDIO_SETMODE
    391 	{
    392 		int mode = WSKBD_TRANSLATED; /* use translated mode for key events */
    393 		if (ioctl(seat->kbd_fd, WSKBDIO_SETMODE, &mode) == -1) {
    394 			fprintf(stderr, "wscons: WSKBDIO_SETMODE failed: %s\n",
    395 			        strerror(errno));
    396 		}
    397 	}
    398 #endif /* WSKBDIO_SETMODE */
    399 
    400 	if (ioctl(seat->kbd_fd, WSKBDIO_GTYPE, &seat->kbd_type) == -1) {
    401 		ERROR("Could not get keyboard type\n");
    402 		goto error2;
    403 	}
    404 	/* TODO handle mixed keyboard types with wsmux?
    405 	   see: https://www.mail-archive.com/ports%40openbsd.org/msg134646.html */
    406 
    407 	if (ioctl(seat->kbd_fd, WSKBDIO_GETENCODING, &encoding) != -1) {
    408 #else
    409 	if (encoding) {
    410 #endif
    411 		encoding_layout = KB_ENCODING(encoding);
    412 		for (i = 0; ws_xkb_encodings[i].xkb != NULL; ++i) {
    413 			if (ws_xkb_encodings[i].ws == encoding_layout) {
    414 				seat->names.layout = ws_xkb_encodings[i].xkb;
    415 				break;
    416 			}
    417 		}
    418 		switch (KB_VARIANT(encoding)) {
    419 		case KB_NODEAD:
    420 			seat->names.variant = "nodeadkeys";
    421 			break;
    422 		case KB_SWAPCTRLCAPS:
    423 			seat->names.options = "ctrl:swapcaps";
    424 			break;
    425 		case KB_DVORAK:
    426 			seat->names.variant = "dvorak";
    427 			break;
    428 		case KB_COLEMAK:
    429 			seat->names.variant = "colemak";
    430 			break;
    431 		}
    432 	}
    433 
    434 	return true;
    435 #ifndef __OpenBSD__
    436 error2:
    437 	close(seat->kbd_fd);
    438 #endif
    439 error1:
    440 	close(seat->mouse_fd);
    441 #ifdef __OpenBSD__
    442 	seat->mouse_fd = -1;
    443 #endif
    444 error0:
    445 	return false;
    446 }
    447 
    448 struct swc_seat *
    449 seat_create(struct wl_display *display, const char *seat_name)
    450 {
    451 	struct seat *seat;
    452 
    453 	seat = calloc(1, sizeof(*seat));
    454 	if (!seat) {
    455 		goto error0;
    456 	}
    457 	seat->mouse_fd = -1;
    458 	memset(&seat->names, 0, sizeof(seat->names));
    459 	seat->names.rules = "base";
    460 	seat->names.model = "pc105";
    461 	seat->names.layout = "us";
    462 	seat->names.variant = "basic";
    463 
    464 	seat->name = strdup(seat_name);
    465 	if (!seat->name) {
    466 		ERROR("Could not allocate seat name string\n");
    467 		goto error1;
    468 	}
    469 
    470 	if (!initialize_wscons(seat)) {
    471 		goto error2;
    472 	}
    473 
    474 	/* TODO other way */
    475 	{
    476 		const char *s;
    477 		if ((s = getenv("XKB_DEFAULT_RULES")) && *s) {
    478 			seat->names.rules = s;
    479 		}
    480 		if ((s = getenv("XKB_DEFAULT_MODEL")) && *s) {
    481 			seat->names.model = s;
    482 		}
    483 		if ((s = getenv("XKB_DEFAULT_LAYOUT")) && *s) {
    484 			seat->names.layout = s;
    485 		}
    486 		if ((s = getenv("XKB_DEFAULT_VARIANT")) && *s) {
    487 			seat->names.variant = s;
    488 		}
    489 		if ((s = getenv("XKB_DEFAULT_OPTIONS")) && *s) {
    490 			seat->names.options = s;
    491 		}
    492 	}
    493 
    494 	seat->global =
    495 	    wl_global_create(display, &wl_seat_interface, 5, seat, &bind_seat);
    496 	if (!seat->global) {
    497 		goto error2;
    498 	}
    499 	seat->capabilities =
    500 	    WL_SEAT_CAPABILITY_KEYBOARD | WL_SEAT_CAPABILITY_POINTER;
    501 	wl_list_init(&seat->resources);
    502 
    503 	seat->swc_listener.notify = &handle_swc_event;
    504 	wl_signal_add(&swc.event_signal, &seat->swc_listener);
    505 
    506 	seat->base.data_device = data_device_create();
    507 	if (!seat->base.data_device) {
    508 		ERROR("Could not initialize data device\n");
    509 		goto error3;
    510 	}
    511 	seat->data_device_listener.notify = &handle_data_device_event;
    512 	wl_signal_add(&seat->base.data_device->event_signal,
    513 	              &seat->data_device_listener);
    514 
    515 	seat->base.keyboard = keyboard_create(&seat->names);
    516 	if (!seat->base.keyboard) {
    517 		ERROR("Could not initialize keyboard\n");
    518 		goto error4;
    519 	}
    520 	seat->keyboard_focus_listener.notify = handle_keyboard_focus_event;
    521 	wl_signal_add(&seat->base.keyboard->focus.event_signal,
    522 	              &seat->keyboard_focus_listener);
    523 
    524 	if (!pointer_initialize(&seat->pointer)) {
    525 		ERROR("Could not initialize pointer\n");
    526 		goto error5;
    527 	}
    528 	seat->base.pointer = &seat->pointer;
    529 
    530 #ifndef __OpenBSD__
    531 	seat->kbd_source = wl_event_loop_add_fd(
    532 	    swc.event_loop, seat->kbd_fd, WL_EVENT_READABLE, &handle_ws_data, seat);
    533 #endif
    534 	seat->mouse_source =
    535 	    wl_event_loop_add_fd(swc.event_loop, seat->mouse_fd, WL_EVENT_READABLE,
    536 	                         &handle_ws_data, seat);
    537 #ifdef __OpenBSD__
    538 	if (!seat->mouse_source) {
    539 		pointer_finalize(&seat->pointer);
    540 		goto error5;
    541 	}
    542 #endif
    543 
    544 	return &seat->base;
    545 
    546 error5:
    547 	keyboard_destroy(seat->base.keyboard);
    548 #ifdef __OpenBSD__
    549 	seat->base.keyboard = NULL;
    550 #endif
    551 error4:
    552 	data_device_destroy(seat->base.data_device);
    553 error3:
    554 	wl_global_destroy(seat->global);
    555 error2:
    556 #ifdef __OpenBSD__
    557 	ws_keyboards_close(seat->keyboards);
    558 	if (seat->mouse_fd >= 0) {
    559 		close(seat->mouse_fd);
    560 	}
    561 #endif
    562 	free(seat->name);
    563 error1:
    564 	free(seat);
    565 error0:
    566 	return NULL;
    567 }
    568 
    569 void
    570 seat_destroy(struct swc_seat *seat_base)
    571 {
    572 	struct seat *seat = wl_container_of(seat_base, seat, base);
    573 
    574 	wl_event_source_remove(seat->mouse_source);
    575 	close(seat->mouse_fd);
    576 	seat->mouse_fd = -1;
    577 #ifdef __OpenBSD__
    578 	ws_keyboards_close(seat->keyboards);
    579 #else
    580 	wl_event_source_remove(seat->kbd_source);
    581 	close(seat->kbd_fd);
    582 	seat->kbd_fd = -1;
    583 #endif
    584 
    585 	pointer_finalize(&seat->pointer);
    586 	keyboard_destroy(seat->base.keyboard);
    587 	data_device_destroy(seat->base.data_device);
    588 
    589 	wl_global_destroy(seat->global);
    590 	free(seat->name);
    591 	free(seat);
    592 }