seat_ws_openbsd.c (6055B)
1 /* swc: libswc/seat_ws_openbsd.c 2 * 3 * Copyright (c) 2013, 2014 Michael Forney 4 * Copyright (c) 2019 Nia Alarie 5 * Copyright (c) 2015 Martin Pieuchot <mpi@openbsd.org> 6 * 7 * Credits to libopeninput for parts of implementation: 8 * https://github.com/sizeofvoid/libopeninput 9 * 10 * Permission is hereby granted, free of charge, to any person obtaining a copy 11 * of this software and associated documentation files (the "Software"), to deal 12 * in the Software without restriction, including without limitation the rights 13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 * copies of the Software, and to permit persons to whom the Software is 15 * furnished to do so, subject to the following conditions: 16 * 17 * The above copyright notice and this permission notice (including the next 18 * paragraph) shall be included in all copies or substantial portions of the 19 * Software. 20 * 21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 27 * SOFTWARE. 28 */ 29 30 #include "internal.h" 31 #include "keyboard.h" 32 #include "launch.h" 33 #include "util.h" 34 #include <dev/wscons/wsconsio.h> 35 #include <errno.h> 36 #include <fcntl.h> 37 #include <stdbool.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <sys/ioctl.h> 42 #include <unistd.h> 43 #include <wayland-server.h> 44 45 struct ws_keyboards { 46 struct wl_list devices; 47 struct keyboard **keyboard; 48 const bool *ignore; 49 unsigned key_refs[256]; 50 }; 51 52 struct ws_keyboard { 53 struct wl_list link; 54 struct ws_keyboards *keyboards; 55 struct wl_event_source *source; 56 int fd; 57 unsigned type; 58 bool held[256]; 59 }; 60 61 int 62 ws_to_xkb(unsigned type, int key); 63 64 static void 65 release_keys(struct ws_keyboard *kbd) 66 { 67 struct ws_keyboards *keyboards = kbd->keyboards; 68 69 for (unsigned key = 0; key < 256; ++key) { 70 if (!kbd->held[key]) { 71 continue; 72 } 73 kbd->held[key] = false; 74 if (--keyboards->key_refs[key] == 0 && *keyboards->keyboard) { 75 keyboard_handle_key(*keyboards->keyboard, get_time(), key, 76 WL_KEYBOARD_KEY_STATE_RELEASED); 77 } 78 } 79 } 80 81 static void 82 close_keyboard(struct ws_keyboard *kbd) 83 { 84 if (kbd->source) { 85 wl_event_source_remove(kbd->source); 86 kbd->source = NULL; 87 } 88 if (kbd->fd >= 0) { 89 release_keys(kbd); 90 close(kbd->fd); 91 kbd->fd = -1; 92 } 93 } 94 95 static int 96 handle_keyboard_data(int fd, uint32_t mask, void *data) 97 { 98 struct ws_keyboard *kbd = data; 99 struct ws_keyboards *keyboards = kbd->keyboards; 100 struct wscons_event ev; 101 ssize_t n; 102 int key; 103 uint32_t time; 104 105 if (mask & (WL_EVENT_ERROR | WL_EVENT_HANGUP)) { 106 close_keyboard(kbd); 107 return 0; 108 } 109 110 while (!*keyboards->ignore) { 111 n = read(fd, &ev, sizeof(ev)); 112 if (n != sizeof(ev)) { 113 if (n == 0 || (n < 0 && errno != EAGAIN && errno != EINTR)) { 114 close_keyboard(kbd); 115 } 116 break; 117 } 118 if (ev.type == WSCONS_EVENT_ALL_KEYS_UP) { 119 release_keys(kbd); 120 continue; 121 } 122 if (ev.type != WSCONS_EVENT_KEY_DOWN && 123 ev.type != WSCONS_EVENT_KEY_UP) { 124 continue; 125 } 126 key = ws_to_xkb(kbd->type, ev.value); 127 if (key < 0) { 128 continue; 129 } 130 time = 131 (uint32_t)(ev.time.tv_sec * 1000ULL + ev.time.tv_nsec / 1000000L); 132 if (ev.type == WSCONS_EVENT_KEY_DOWN && !kbd->held[key]) { 133 kbd->held[key] = true; 134 if (++keyboards->key_refs[key] == 1) { 135 keyboard_handle_key(*keyboards->keyboard, time, key, 136 WL_KEYBOARD_KEY_STATE_PRESSED); 137 } 138 } else if (ev.type == WSCONS_EVENT_KEY_UP && kbd->held[key]) { 139 kbd->held[key] = false; 140 if (--keyboards->key_refs[key] == 0) { 141 keyboard_handle_key(*keyboards->keyboard, time, key, 142 WL_KEYBOARD_KEY_STATE_RELEASED); 143 } 144 } 145 } 146 return 0; 147 } 148 149 void 150 ws_keyboards_reset(struct ws_keyboards *keyboards) 151 { 152 struct ws_keyboard *kbd; 153 154 memset(keyboards->key_refs, 0, sizeof(keyboards->key_refs)); 155 wl_list_for_each(kbd, &keyboards->devices, link) 156 memset(kbd->held, 0, sizeof(kbd->held)); 157 } 158 159 void 160 ws_keyboards_close(struct ws_keyboards *keyboards) 161 { 162 struct ws_keyboard *kbd, *next; 163 164 if (!keyboards) { 165 return; 166 } 167 wl_list_for_each_safe(kbd, next, &keyboards->devices, link) 168 { 169 close_keyboard(kbd); 170 wl_list_remove(&kbd->link); 171 free(kbd); 172 } 173 free(keyboards); 174 } 175 176 struct ws_keyboards * 177 ws_keyboards_open(struct keyboard **keyboard, const bool *ignore, 178 kbd_t *encoding) 179 { 180 struct ws_keyboards *keyboards; 181 struct ws_keyboard *kbd; 182 char path[32]; 183 184 keyboards = calloc(1, sizeof(*keyboards)); 185 if (!keyboards) { 186 return NULL; 187 } 188 wl_list_init(&keyboards->devices); 189 keyboards->keyboard = keyboard; 190 keyboards->ignore = ignore; 191 192 for (int i = 0; i < 10; ++i) { 193 snprintf(path, sizeof(path), "/dev/wskbd%d", i); 194 kbd = calloc(1, sizeof(*kbd)); 195 if (!kbd) { 196 break; 197 } 198 kbd->keyboards = keyboards; 199 kbd->fd = launch_open_device(path, O_RDWR | O_NONBLOCK); 200 if (kbd->fd < 0) { 201 free(kbd); 202 continue; 203 } 204 if (ioctl(kbd->fd, WSKBDIO_GTYPE, &kbd->type) == -1 || 205 (kbd->type != WSKBD_TYPE_PC_XT && kbd->type != WSKBD_TYPE_PC_AT && 206 kbd->type != WSKBD_TYPE_USB)) { 207 close_keyboard(kbd); 208 free(kbd); 209 continue; 210 } 211 if (!*encoding) { 212 kbd_t layout; 213 if (ioctl(kbd->fd, WSKBDIO_GETENCODING, &layout) == 0) { 214 *encoding = layout; 215 } 216 } 217 218 kbd->source = 219 wl_event_loop_add_fd(swc.event_loop, kbd->fd, WL_EVENT_READABLE, 220 handle_keyboard_data, kbd); 221 if (!kbd->source) { 222 close_keyboard(kbd); 223 free(kbd); 224 continue; 225 } 226 wl_list_insert(&keyboards->devices, &kbd->link); 227 fprintf(stderr, "wscons: opened %s (type %u)\n", path, kbd->type); 228 } 229 if (wl_list_empty(&keyboards->devices)) { 230 ERROR("Could not open any keyboard device\n"); 231 ws_keyboards_close(keyboards); 232 return NULL; 233 } 234 return keyboards; 235 }