| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* Linux console keyboard driver. |
| 12 |
* |
| 13 |
* By Marek Habersack, modified by George Foot. |
| 14 |
* |
| 15 |
* Modified for the new keyboard API by Peter Wang. |
| 16 |
* |
| 17 |
* See readme.txt for copyright information. |
| 18 |
* |
| 19 |
*--- |
| 20 |
* Recommended reading: |
| 21 |
* |
| 22 |
* - "Kernel Korner: The Linux keyboard driver" by Andries E. Brouwer |
| 23 |
* http://www.linuxjournal.com/article.php?sid=1080 |
| 24 |
* |
| 25 |
* - Console Programming HOWTO |
| 26 |
* |
| 27 |
* TODO: diacriticals |
| 28 |
*/ |
| 29 |
|
| 30 |
|
| 31 |
#define ALLEGRO_NO_COMPATIBILITY |
| 32 |
|
| 33 |
#include <fcntl.h> |
| 34 |
#include <linux/input.h> |
| 35 |
#include <linux/kd.h> |
| 36 |
#include <linux/keyboard.h> |
| 37 |
#include <linux/vt.h> |
| 38 |
#include <signal.h> |
| 39 |
#include <stdlib.h> |
| 40 |
#include <string.h> |
| 41 |
#include <sys/ioctl.h> |
| 42 |
#include <sys/time.h> |
| 43 |
#include <sys/types.h> |
| 44 |
#include <termios.h> |
| 45 |
#include <unistd.h> |
| 46 |
|
| 47 |
#include "allegro5/allegro.h" |
| 48 |
#include "allegro5/internal/aintern.h" |
| 49 |
#include "allegro5/internal/aintern_driver.h" |
| 50 |
#include "allegro5/internal/aintern_events.h" |
| 51 |
#include "allegro5/internal/aintern_keyboard.h" |
| 52 |
#include "allegro5/platform/aintlnx.h" |
| 53 |
#include "allegro5/platform/aintunix.h" |
| 54 |
|
| 55 |
|
| 56 |
ALLEGRO_DEBUG_CHANNEL("keyboard") |
| 57 |
|
| 58 |
|
| 59 |
#define PREFIX_I "al-ckey INFO: " |
| 60 |
#define PREFIX_W "al-ckey WARNING: " |
| 61 |
#define PREFIX_E "al-ckey ERROR: " |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
typedef struct ALLEGRO_KEYBOARD_LINUX |
| 66 |
{ |
| 67 |
ALLEGRO_KEYBOARD parent; |
| 68 |
int fd; |
| 69 |
struct termios startup_termio; |
| 70 |
struct termios work_termio; |
| 71 |
int startup_kbmode; |
| 72 |
ALLEGRO_KEYBOARD_STATE state; |
| 73 |
unsigned int modifiers; |
| 74 |
// Quit if Ctrl-Alt-Del is pressed. |
| 75 |
bool three_finger_flag; |
| 76 |
// Whether to let the LED lights if the . |
| 77 |
bool key_led_flag; |
| 78 |
} ALLEGRO_KEYBOARD_LINUX; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
/* the one and only keyboard object */ |
| 83 |
static ALLEGRO_KEYBOARD_LINUX the_keyboard; |
| 84 |
|
| 85 |
/* the pid to kill when three finger saluting */ |
| 86 |
static pid_t main_pid; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
/* forward declarations */ |
| 91 |
static bool lkeybd_init_keyboard(void); |
| 92 |
static void lkeybd_exit_keyboard(void); |
| 93 |
static ALLEGRO_KEYBOARD *lkeybd_get_keyboard(void); |
| 94 |
static bool lkeybd_set_keyboard_leds(int leds); |
| 95 |
static void lkeybd_get_keyboard_state(ALLEGRO_KEYBOARD_STATE *ret_state); |
| 96 |
static void lkeybd_clear_keyboard_state(void); |
| 97 |
|
| 98 |
static void process_new_data(void *unused); |
| 99 |
static void process_character(unsigned char ch); |
| 100 |
static void handle_key_press(int mycode, unsigned int ascii); |
| 101 |
static void handle_key_release(int mycode); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
/* the driver vtable */ |
| 106 |
#define KEYDRV_LINUX AL_ID('L','N','X','C') |
| 107 |
|
| 108 |
static ALLEGRO_KEYBOARD_DRIVER keydrv_linux = |
| 109 |
{ |
| 110 |
KEYDRV_LINUX, |
| 111 |
"", |
| 112 |
"", |
| 113 |
"Linux console keyboard", |
| 114 |
lkeybd_init_keyboard, |
| 115 |
lkeybd_exit_keyboard, |
| 116 |
lkeybd_get_keyboard, |
| 117 |
lkeybd_set_keyboard_leds, |
| 118 |
NULL, /* const char *keycode_to_name(int keycode) */ |
| 119 |
lkeybd_get_keyboard_state, |
| 120 |
lkeybd_clear_keyboard_state |
| 121 |
}; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
/* list the available drivers */ |
| 126 |
_AL_DRIVER_INFO _al_linux_keyboard_driver_list[] = |
| 127 |
{ |
| 128 |
{ KEYDRV_LINUX, &keydrv_linux, true }, |
| 129 |
{ 0, NULL, 0 } |
| 130 |
}; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
/* |
| 135 |
* Various ugly things. |
| 136 |
*/ |
| 137 |
|
| 138 |
|
| 139 |
#define KB_MODIFIERS (ALLEGRO_KEYMOD_SHIFT | ALLEGRO_KEYMOD_CTRL | ALLEGRO_KEYMOD_ALT | \ |
| 140 |
ALLEGRO_KEYMOD_ALTGR | ALLEGRO_KEYMOD_LWIN | ALLEGRO_KEYMOD_RWIN | \ |
| 141 |
ALLEGRO_KEYMOD_MENU) |
| 142 |
|
| 143 |
#define KB_LED_FLAGS (ALLEGRO_KEYMOD_SCROLLLOCK | ALLEGRO_KEYMOD_NUMLOCK | \ |
| 144 |
ALLEGRO_KEYMOD_CAPSLOCK) |
| 145 |
|
| 146 |
|
| 147 |
/* lookup table for converting kernel keycodes into Allegro format */ |
| 148 |
static unsigned char kernel_to_mycode[128] = |
| 149 |
{ |
| 150 |
/* 0x00 */ 0, ALLEGRO_KEY_ESCAPE, ALLEGRO_KEY_1, ALLEGRO_KEY_2, |
| 151 |
/* 0x04 */ ALLEGRO_KEY_3, ALLEGRO_KEY_4, ALLEGRO_KEY_5, ALLEGRO_KEY_6, |
| 152 |
/* 0x08 */ ALLEGRO_KEY_7, ALLEGRO_KEY_8, ALLEGRO_KEY_9, ALLEGRO_KEY_0, |
| 153 |
/* 0x0C */ ALLEGRO_KEY_MINUS, ALLEGRO_KEY_EQUALS, ALLEGRO_KEY_BACKSPACE, ALLEGRO_KEY_TAB, |
| 154 |
/* 0x10 */ ALLEGRO_KEY_Q, ALLEGRO_KEY_W, ALLEGRO_KEY_E, ALLEGRO_KEY_R, |
| 155 |
/* 0x14 */ ALLEGRO_KEY_T, ALLEGRO_KEY_Y, ALLEGRO_KEY_U, ALLEGRO_KEY_I, |
| 156 |
/* 0x18 */ ALLEGRO_KEY_O, ALLEGRO_KEY_P, ALLEGRO_KEY_OPENBRACE, ALLEGRO_KEY_CLOSEBRACE, |
| 157 |
/* 0x1C */ ALLEGRO_KEY_ENTER, ALLEGRO_KEY_LCTRL, ALLEGRO_KEY_A, ALLEGRO_KEY_S, |
| 158 |
/* 0x20 */ ALLEGRO_KEY_D, ALLEGRO_KEY_F, ALLEGRO_KEY_G, ALLEGRO_KEY_H, |
| 159 |
/* 0x24 */ ALLEGRO_KEY_J, ALLEGRO_KEY_K, ALLEGRO_KEY_L, ALLEGRO_KEY_SEMICOLON, |
| 160 |
/* 0x28 */ ALLEGRO_KEY_QUOTE, ALLEGRO_KEY_TILDE, ALLEGRO_KEY_LSHIFT, ALLEGRO_KEY_BACKSLASH, |
| 161 |
/* 0x2C */ ALLEGRO_KEY_Z, ALLEGRO_KEY_X, ALLEGRO_KEY_C, ALLEGRO_KEY_V, |
| 162 |
/* 0x30 */ ALLEGRO_KEY_B, ALLEGRO_KEY_N, ALLEGRO_KEY_M, ALLEGRO_KEY_COMMA, |
| 163 |
/* 0x34 */ ALLEGRO_KEY_FULLSTOP, ALLEGRO_KEY_SLASH, ALLEGRO_KEY_RSHIFT, ALLEGRO_KEY_PAD_ASTERISK, |
| 164 |
/* 0x38 */ ALLEGRO_KEY_ALT, ALLEGRO_KEY_SPACE, ALLEGRO_KEY_CAPSLOCK, ALLEGRO_KEY_F1, |
| 165 |
/* 0x3C */ ALLEGRO_KEY_F2, ALLEGRO_KEY_F3, ALLEGRO_KEY_F4, ALLEGRO_KEY_F5, |
| 166 |
/* 0x40 */ ALLEGRO_KEY_F6, ALLEGRO_KEY_F7, ALLEGRO_KEY_F8, ALLEGRO_KEY_F9, |
| 167 |
/* 0x44 */ ALLEGRO_KEY_F10, ALLEGRO_KEY_NUMLOCK, ALLEGRO_KEY_SCROLLLOCK, ALLEGRO_KEY_PAD_7, |
| 168 |
/* 0x48 */ ALLEGRO_KEY_PAD_8, ALLEGRO_KEY_PAD_9, ALLEGRO_KEY_PAD_MINUS, ALLEGRO_KEY_PAD_4, |
| 169 |
/* 0x4C */ ALLEGRO_KEY_PAD_5, ALLEGRO_KEY_PAD_6, ALLEGRO_KEY_PAD_PLUS, ALLEGRO_KEY_PAD_1, |
| 170 |
/* 0x50 */ ALLEGRO_KEY_PAD_2, ALLEGRO_KEY_PAD_3, ALLEGRO_KEY_PAD_0, ALLEGRO_KEY_PAD_DELETE, |
| 171 |
/* 0x54 */ ALLEGRO_KEY_PRINTSCREEN, 0, ALLEGRO_KEY_BACKSLASH2, ALLEGRO_KEY_F11, |
| 172 |
/* 0x58 */ ALLEGRO_KEY_F12, 0, 0, 0, |
| 173 |
/* 0x5C */ 0, 0, 0, 0, |
| 174 |
/* 0x60 */ ALLEGRO_KEY_PAD_ENTER, ALLEGRO_KEY_RCTRL, ALLEGRO_KEY_PAD_SLASH, ALLEGRO_KEY_PRINTSCREEN, |
| 175 |
/* 0x64 */ ALLEGRO_KEY_ALTGR, ALLEGRO_KEY_PAUSE, ALLEGRO_KEY_HOME, ALLEGRO_KEY_UP, |
| 176 |
/* 0x68 */ ALLEGRO_KEY_PGUP, ALLEGRO_KEY_LEFT, ALLEGRO_KEY_RIGHT, ALLEGRO_KEY_END, |
| 177 |
/* 0x6C */ ALLEGRO_KEY_DOWN, ALLEGRO_KEY_PGDN, ALLEGRO_KEY_INSERT, ALLEGRO_KEY_DELETE, |
| 178 |
/* 0x70 */ 0, 0, 0, 0, |
| 179 |
/* 0x74 */ 0, 0, 0, ALLEGRO_KEY_PAUSE, |
| 180 |
/* 0x78 */ 0, 0, 0, 0, |
| 181 |
/* 0x7C */ 0, ALLEGRO_KEY_LWIN, ALLEGRO_KEY_RWIN, ALLEGRO_KEY_MENU |
| 182 |
|
| 183 |
/* "Two keys are unusual in the sense that their keycode is not constant, |
| 184 |
* but depends on modifiers. The PrintScrn key will yield keycode 84 when |
| 185 |
* combined with either Alt key, but keycode 99 otherwise. The Pause key |
| 186 |
* will yield keycode 101 when combined with either Ctrl key, but keycode |
| 187 |
* 119 otherwise. (This has historic reasons, but might change, to free |
| 188 |
* keycodes 99 and 119 for other purposes.)" |
| 189 |
*/ |
| 190 |
}; |
| 191 |
|
| 192 |
|
| 193 |
/* convert kernel keycodes for numpad keys into ASCII characters */ |
| 194 |
#define NUM_PAD_KEYS 17 |
| 195 |
static const char pad_asciis[NUM_PAD_KEYS] = |
| 196 |
{ |
| 197 |
'0','1','2','3','4','5','6','7','8','9', |
| 198 |
'+','-','*','/','\r',',','.' |
| 199 |
}; |
| 200 |
static const char pad_asciis_no_numlock[NUM_PAD_KEYS] = |
| 201 |
{ |
| 202 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 203 |
'+', '-', '*', '/', '\r', 0, 0 |
| 204 |
}; |
| 205 |
|
| 206 |
|
| 207 |
/* convert Allegro format keycodes into ALLEGRO_KEYMOD_* */ |
| 208 |
static const unsigned int modifier_table[ALLEGRO_KEY_MAX - ALLEGRO_KEY_MODIFIERS] = |
| 209 |
{ |
| 210 |
ALLEGRO_KEYMOD_SHIFT, ALLEGRO_KEYMOD_SHIFT, ALLEGRO_KEYMOD_CTRL, |
| 211 |
ALLEGRO_KEYMOD_CTRL, ALLEGRO_KEYMOD_ALT, ALLEGRO_KEYMOD_ALTGR, |
| 212 |
ALLEGRO_KEYMOD_LWIN, ALLEGRO_KEYMOD_RWIN, ALLEGRO_KEYMOD_MENU, |
| 213 |
ALLEGRO_KEYMOD_SCROLLLOCK, ALLEGRO_KEYMOD_NUMLOCK, ALLEGRO_KEYMOD_CAPSLOCK |
| 214 |
}; |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
/* keycode_to_char: [fdwatch thread] |
| 219 |
* Helper function. |
| 220 |
* KEYCODE is a Linux kernel keycode, not an Allegro keycode. |
| 221 |
* |
| 222 |
* In the process of doing the translation, we may find that the user |
| 223 |
* has pressed a key that for VT switching. In that case a negative |
| 224 |
* number is returned, the absolute value of which is the VT to |
| 225 |
* switch to. Yes, ugly. |
| 226 |
*/ |
| 227 |
static int keycode_to_char(int keycode) |
| 228 |
{ |
| 229 |
const unsigned int modifiers = the_keyboard.modifiers; |
| 230 |
struct kbentry kbe; |
| 231 |
int keymap; |
| 232 |
int ascii; |
| 233 |
|
| 234 |
/* build kernel keymap number */ |
| 235 |
keymap = 0; |
| 236 |
if (modifiers & ALLEGRO_KEYMOD_SHIFT) keymap |= 1; |
| 237 |
if (modifiers & ALLEGRO_KEYMOD_ALTGR) keymap |= 2; |
| 238 |
if (modifiers & ALLEGRO_KEYMOD_CTRL) keymap |= 4; |
| 239 |
if (modifiers & ALLEGRO_KEYMOD_ALT) keymap |= 8; |
| 240 |
|
| 241 |
/* map keycode to type and value */ |
| 242 |
kbe.kb_table = keymap; |
| 243 |
kbe.kb_index = keycode; |
| 244 |
ioctl(the_keyboard.fd, KDGKBENT, &kbe); |
| 245 |
|
| 246 |
if (keycode == KEY_BACKSPACE) |
| 247 |
ascii = 8; |
| 248 |
else { |
| 249 |
if (kbe.kb_value == K_NOSUCHMAP) { |
| 250 |
/* invalid keymaps */ |
| 251 |
/* FIXME: Maybe we should just redo the */ |
| 252 |
/* ioctl with keymap 0? */ |
| 253 |
ascii = 0; |
| 254 |
} |
| 255 |
else { |
| 256 |
/* most keys come here */ |
| 257 |
ascii = KVAL(kbe.kb_value); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
/* finally do some things based on key type */ |
| 262 |
switch (KTYP(kbe.kb_value)) { |
| 263 |
|
| 264 |
case KT_CONS: |
| 265 |
/* VT switch key -- return a negative number */ |
| 266 |
return -( KVAL(kbe.kb_value)+1 ); |
| 267 |
|
| 268 |
case KT_LETTER: |
| 269 |
/* apply capslock translation. */ |
| 270 |
if (modifiers & ALLEGRO_KEYMOD_CAPSLOCK) |
| 271 |
return ascii ^ 0x20; |
| 272 |
else |
| 273 |
return ascii; |
| 274 |
|
| 275 |
case KT_LATIN: |
| 276 |
case KT_ASCII: |
| 277 |
return ascii; |
| 278 |
|
| 279 |
case KT_PAD: |
| 280 |
{ |
| 281 |
int val = KVAL(kbe.kb_value); |
| 282 |
if (modifiers & ALLEGRO_KEYMOD_NUMLOCK) { |
| 283 |
if ((val >= 0) && (val < NUM_PAD_KEYS)) |
| 284 |
ascii = pad_asciis[val]; |
| 285 |
} else { |
| 286 |
if ((val >= 0) && (val < NUM_PAD_KEYS)) |
| 287 |
ascii = pad_asciis_no_numlock[val]; |
| 288 |
} |
| 289 |
return ascii; |
| 290 |
} |
| 291 |
|
| 292 |
case KT_SPEC: |
| 293 |
if (keycode == KEY_ENTER) |
| 294 |
return '\r'; |
| 295 |
else |
| 296 |
return 0; |
| 297 |
|
| 298 |
default: |
| 299 |
/* dunno */ |
| 300 |
return 0; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
/* lkeybd_init_keyboard: [primary thread] |
| 307 |
* Initialise the keyboard driver. |
| 308 |
*/ |
| 309 |
static bool lkeybd_init_keyboard(void) |
| 310 |
{ |
| 311 |
bool can_restore_termio_and_kbmode = false; |
| 312 |
|
| 313 |
memset(&the_keyboard, 0, sizeof the_keyboard); |
| 314 |
|
| 315 |
/* |
| 316 |
if (__al_linux_use_console()) |
| 317 |
return false; |
| 318 |
*/ |
| 319 |
the_keyboard.fd = open("/dev/tty", O_RDWR); |
| 320 |
|
| 321 |
/* Save the current terminal attributes, which we will restore when |
| 322 |
* we close up shop. |
| 323 |
*/ |
| 324 |
if (tcgetattr(the_keyboard.fd, &the_keyboard.startup_termio) != 0) { |
| 325 |
goto Error; |
| 326 |
} |
| 327 |
|
| 328 |
/* Save previous keyboard mode (probably XLATE). */ |
| 329 |
if (ioctl(the_keyboard.fd, KDGKBMODE, &the_keyboard.startup_kbmode) != 0) { |
| 330 |
//goto Error; |
| 331 |
} |
| 332 |
|
| 333 |
can_restore_termio_and_kbmode = false; |
| 334 |
|
| 335 |
/* Set terminal attributes we need. |
| 336 |
* |
| 337 |
* Input modes (c_iflag): We want to disable: |
| 338 |
* - stripping bytes to 7 bits |
| 339 |
* - ignoring of carriage returns |
| 340 |
* - translating of carriage returns to newlines, and vice versa |
| 341 |
* - start/stop control on input and output |
| 342 |
* |
| 343 |
* Control modes (c_cflag): We want 8 bits per byte. |
| 344 |
* |
| 345 |
* Local modes (c_lflag: We want to disable: |
| 346 |
* - canonical (line by line) input |
| 347 |
* - echoing input back to the display |
| 348 |
* - interpretation of signal characters |
| 349 |
* |
| 350 |
* The c_iflag, c_lflag settings come from svgalib. Allegro 4 |
| 351 |
* simply set them to 0, which is a bit crude. |
| 352 |
*/ |
| 353 |
the_keyboard.work_termio = the_keyboard.startup_termio; |
| 354 |
the_keyboard.work_termio.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON); |
| 355 |
the_keyboard.work_termio.c_cflag &= ~CSIZE; |
| 356 |
the_keyboard.work_termio.c_cflag |= CS8; |
| 357 |
the_keyboard.work_termio.c_lflag &= ~(ICANON | ECHO | ISIG); |
| 358 |
|
| 359 |
if (tcsetattr(the_keyboard.fd, TCSANOW, &the_keyboard.work_termio) != 0) { |
| 360 |
goto Error; |
| 361 |
} |
| 362 |
|
| 363 |
/* Set the keyboard mode to mediumraw. */ |
| 364 |
if (ioctl(the_keyboard.fd, KDSKBMODE, K_MEDIUMRAW) != 0) { |
| 365 |
//goto Error; |
| 366 |
} |
| 367 |
|
| 368 |
the_keyboard.three_finger_flag = true; |
| 369 |
the_keyboard.key_led_flag = true; |
| 370 |
|
| 371 |
const char *value = al_get_config_value(al_get_system_config(), |
| 372 |
"keyboard", "enable_three_finger_exit"); |
| 373 |
if (value) { |
| 374 |
the_keyboard.three_finger_flag = !strncmp(value, "true", 4); |
| 375 |
} |
| 376 |
value = al_get_config_value(al_get_system_config(), |
| 377 |
"keyboard", "enable_key_led_toggle"); |
| 378 |
if (value) { |
| 379 |
the_keyboard.key_led_flag = !strncmp(value, "true", 4); |
| 380 |
} |
| 381 |
|
| 382 |
ALLEGRO_DEBUG("Three finger flag enabled: %s\n", |
| 383 |
the_keyboard.three_finger_flag ? "true" : "false"); |
| 384 |
ALLEGRO_DEBUG("Key LED toggle enabled: %s\n", |
| 385 |
the_keyboard.key_led_flag ? "true" : "false"); |
| 386 |
|
| 387 |
/* Initialise the keyboard object for use as an event source. */ |
| 388 |
_al_event_source_init(&the_keyboard.parent.es); |
| 389 |
|
| 390 |
/* Start watching for data on the fd. */ |
| 391 |
_al_unix_start_watching_fd(the_keyboard.fd, process_new_data, NULL); |
| 392 |
|
| 393 |
/* Get the pid, which we use for the three finger salute */ |
| 394 |
main_pid = getpid(); |
| 395 |
|
| 396 |
return true; |
| 397 |
|
| 398 |
Error: |
| 399 |
|
| 400 |
if (can_restore_termio_and_kbmode) { |
| 401 |
tcsetattr(the_keyboard.fd, TCSANOW, &the_keyboard.startup_termio); |
| 402 |
ioctl(the_keyboard.fd, KDSKBMODE, the_keyboard.startup_kbmode); |
| 403 |
} |
| 404 |
|
| 405 |
close(the_keyboard.fd); |
| 406 |
|
| 407 |
/* |
| 408 |
__al_linux_leave_console(); |
| 409 |
*/ |
| 410 |
|
| 411 |
return false; |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
/* lkeybd_exit_keyboard: [primary thread] |
| 417 |
* Shut down the keyboard driver. |
| 418 |
*/ |
| 419 |
static void lkeybd_exit_keyboard(void) |
| 420 |
{ |
| 421 |
_al_unix_stop_watching_fd(the_keyboard.fd); |
| 422 |
|
| 423 |
_al_event_source_free(&the_keyboard.parent.es); |
| 424 |
|
| 425 |
/* Restore terminal attrs, keyboard mode, and reset the LED mode. */ |
| 426 |
tcsetattr(the_keyboard.fd, TCSANOW, &the_keyboard.startup_termio); |
| 427 |
ioctl(the_keyboard.fd, KDSKBMODE, the_keyboard.startup_kbmode); |
| 428 |
ioctl(the_keyboard.fd, KDSETLED, 8); |
| 429 |
|
| 430 |
close(the_keyboard.fd); |
| 431 |
|
| 432 |
//__al_linux_leave_console(); |
| 433 |
|
| 434 |
/* This may help catch bugs in the user program, since the pointer |
| 435 |
* we return to the user is always the same. |
| 436 |
*/ |
| 437 |
memset(&the_keyboard, 0, sizeof the_keyboard); |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
/* lkeybd_get_keyboard: |
| 443 |
* Returns the address of a ALLEGRO_KEYBOARD structure representing the keyboard. |
| 444 |
*/ |
| 445 |
static ALLEGRO_KEYBOARD *lkeybd_get_keyboard(void) |
| 446 |
{ |
| 447 |
return (ALLEGRO_KEYBOARD *)&the_keyboard; |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
/* lkeybd_set_keyboard_leds: |
| 453 |
* Updates the LED state. |
| 454 |
*/ |
| 455 |
static bool lkeybd_set_keyboard_leds(int leds) |
| 456 |
{ |
| 457 |
int val = 0; |
| 458 |
|
| 459 |
if (leds & ALLEGRO_KEYMOD_SCROLLLOCK) val |= LED_SCR; |
| 460 |
if (leds & ALLEGRO_KEYMOD_NUMLOCK) val |= LED_NUM; |
| 461 |
if (leds & ALLEGRO_KEYMOD_CAPSLOCK) val |= LED_CAP; |
| 462 |
|
| 463 |
return (ioctl(the_keyboard.fd, KDSETLED, val) == 0) ? true : false; |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
/* lkeybd_get_keyboard_state: [primary thread] |
| 469 |
* Copy the current keyboard state into RET_STATE, with any necessary locking. |
| 470 |
*/ |
| 471 |
static void lkeybd_get_keyboard_state(ALLEGRO_KEYBOARD_STATE *ret_state) |
| 472 |
{ |
| 473 |
_al_event_source_lock(&the_keyboard.parent.es); |
| 474 |
{ |
| 475 |
*ret_state = the_keyboard.state; |
| 476 |
} |
| 477 |
_al_event_source_unlock(&the_keyboard.parent.es); |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
/* lkeybd_clear_keyboard_state: [primary thread] |
| 483 |
* Clear the current keyboard state, with any necessary locking. |
| 484 |
*/ |
| 485 |
static void lkeybd_clear_keyboard_state(void) |
| 486 |
{ |
| 487 |
_al_event_source_lock(&the_keyboard.parent.es); |
| 488 |
{ |
| 489 |
memset(&the_keyboard.state, 0, sizeof(the_keyboard.state)); |
| 490 |
} |
| 491 |
_al_event_source_unlock(&the_keyboard.parent.es); |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
/* process_new_data: [fdwatch thread] |
| 497 |
* Process new data arriving in the keyboard's fd. |
| 498 |
*/ |
| 499 |
static void process_new_data(void *unused) |
| 500 |
{ |
| 501 |
_al_event_source_lock(&the_keyboard.parent.es); |
| 502 |
{ |
| 503 |
unsigned char buf[128]; |
| 504 |
size_t bytes_read; |
| 505 |
unsigned int ch; |
| 506 |
|
| 507 |
bytes_read = read(the_keyboard.fd, &buf, sizeof(buf)); |
| 508 |
for (ch = 0; ch < bytes_read; ch++) |
| 509 |
process_character(buf[ch]); |
| 510 |
} |
| 511 |
_al_event_source_unlock(&the_keyboard.parent.es); |
| 512 |
|
| 513 |
(void)unused; |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
/* process_character: [fdwatch thread] |
| 519 |
* This is where most of the work gets done. CH is a byte read in |
| 520 |
* from the keyboard's fd. This function finds the Allegro equivalent |
| 521 |
* of that key code, figures out which ASCII character that key |
| 522 |
* generates (if any), and then calls the handle_key_press() and |
| 523 |
* handle_key_release() functions with that information. It also does |
| 524 |
* some things with modifier keys. |
| 525 |
*/ |
| 526 |
static void process_character(unsigned char ch) |
| 527 |
{ |
| 528 |
/* read kernel's keycode and convert to Allegro's keycode */ |
| 529 |
int keycode = ch & 0x7f; |
| 530 |
bool press = !(ch & 0x80); |
| 531 |
int mycode = kernel_to_mycode[keycode]; |
| 532 |
|
| 533 |
/* if the key was something weird we don't understand, skip it */ |
| 534 |
if (mycode == 0) |
| 535 |
return; |
| 536 |
|
| 537 |
/* process modifiers */ |
| 538 |
if (mycode >= ALLEGRO_KEY_MODIFIERS) { |
| 539 |
int flag = modifier_table[mycode - ALLEGRO_KEY_MODIFIERS]; |
| 540 |
if (press) { |
| 541 |
if (flag & KB_MODIFIERS) |
| 542 |
the_keyboard.modifiers |= flag; |
| 543 |
else if ((flag & KB_LED_FLAGS) && the_keyboard.key_led_flag) |
| 544 |
the_keyboard.modifiers ^= flag; |
| 545 |
} |
| 546 |
else { |
| 547 |
/* XXX: if the user presses LCTRL, then RCTRL, then releases |
| 548 |
* LCTRL, the ALLEGRO_KEYMOD_CTRL modifier should still be on. |
| 549 |
*/ |
| 550 |
if (flag & KB_MODIFIERS) |
| 551 |
the_keyboard.modifiers &= ~flag; |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
/* call the handlers */ |
| 556 |
if (press) { |
| 557 |
int ascii = keycode_to_char(keycode); |
| 558 |
|
| 559 |
/* switch VT if the user requested so */ |
| 560 |
if (ascii < 0) { |
| 561 |
int console = -ascii; |
| 562 |
int last_console; |
| 563 |
|
| 564 |
ioctl(the_keyboard.fd, VT_OPENQRY, &last_console); |
| 565 |
if (console < last_console) |
| 566 |
if (ioctl(the_keyboard.fd, VT_ACTIVATE, console) == 0) |
| 567 |
return; |
| 568 |
} |
| 569 |
|
| 570 |
handle_key_press(mycode, ascii); |
| 571 |
} |
| 572 |
else { |
| 573 |
handle_key_release(mycode); |
| 574 |
} |
| 575 |
|
| 576 |
/* three-finger salute for killing the program */ |
| 577 |
if ((the_keyboard.three_finger_flag) |
| 578 |
&& ((mycode == ALLEGRO_KEY_DELETE) || (mycode == ALLEGRO_KEY_END)) |
| 579 |
&& (the_keyboard.modifiers & ALLEGRO_KEYMOD_CTRL) |
| 580 |
&& (the_keyboard.modifiers & ALLEGRO_KEYMOD_ALT)) |
| 581 |
{ |
| 582 |
kill(main_pid, SIGTERM); |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
/* handle_key_press: [fdwatch thread] |
| 589 |
* Helper: stuff to do when a key is pressed. |
| 590 |
*/ |
| 591 |
static void handle_key_press(int mycode, unsigned int ascii) |
| 592 |
{ |
| 593 |
ALLEGRO_EVENT_TYPE event_type; |
| 594 |
ALLEGRO_EVENT event; |
| 595 |
|
| 596 |
event_type = (_AL_KEYBOARD_STATE_KEY_DOWN(the_keyboard.state, mycode) |
| 597 |
? ALLEGRO_EVENT_KEY_CHAR |
| 598 |
: ALLEGRO_EVENT_KEY_DOWN); |
| 599 |
|
| 600 |
/* Maintain the key_down array. */ |
| 601 |
_AL_KEYBOARD_STATE_SET_KEY_DOWN(the_keyboard.state, mycode); |
| 602 |
|
| 603 |
/* Generate key press/repeat events if necessary. */ |
| 604 |
if (!_al_event_source_needs_to_generate_event(&the_keyboard.parent.es)) |
| 605 |
return; |
| 606 |
|
| 607 |
event.keyboard.type = event_type; |
| 608 |
event.keyboard.timestamp = al_get_time(); |
| 609 |
event.keyboard.display = NULL; |
| 610 |
event.keyboard.keycode = mycode; |
| 611 |
event.keyboard.unichar = ascii; |
| 612 |
event.keyboard.modifiers = the_keyboard.modifiers; |
| 613 |
|
| 614 |
_al_event_source_emit_event(&the_keyboard.parent.es, &event); |
| 615 |
|
| 616 |
/* The first press should generate a KEY_CHAR also */ |
| 617 |
if (event_type == ALLEGRO_EVENT_KEY_DOWN) { |
| 618 |
event.keyboard.type = ALLEGRO_EVENT_KEY_CHAR; |
| 619 |
event.keyboard.timestamp = al_get_time(); |
| 620 |
_al_event_source_emit_event(&the_keyboard.parent.es, &event); |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
/* handle_key_release: [fdwatch thread] |
| 627 |
* Helper: stuff to do when a key is released. |
| 628 |
*/ |
| 629 |
static void handle_key_release(int mycode) |
| 630 |
{ |
| 631 |
ALLEGRO_EVENT event; |
| 632 |
|
| 633 |
/* This can happen, e.g. when we are switching back into a VT with |
| 634 |
* ALT+Fn, we only get the release event of the function key. |
| 635 |
*/ |
| 636 |
if (!_AL_KEYBOARD_STATE_KEY_DOWN(the_keyboard.state, mycode)) |
| 637 |
return; |
| 638 |
|
| 639 |
/* Maintain the key_down array. */ |
| 640 |
_AL_KEYBOARD_STATE_CLEAR_KEY_DOWN(the_keyboard.state, mycode); |
| 641 |
|
| 642 |
/* Generate key release events if necessary. */ |
| 643 |
if (!_al_event_source_needs_to_generate_event(&the_keyboard.parent.es)) |
| 644 |
return; |
| 645 |
|
| 646 |
event.keyboard.type = ALLEGRO_EVENT_KEY_UP; |
| 647 |
event.keyboard.timestamp = al_get_time(); |
| 648 |
event.keyboard.display = NULL; |
| 649 |
event.keyboard.keycode = mycode; |
| 650 |
event.keyboard.unichar = 0; |
| 651 |
event.keyboard.modifiers = the_keyboard.modifiers; |
| 652 |
|
| 653 |
_al_event_source_emit_event(&the_keyboard.parent.es, &event); |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
/* |
| 659 |
* Local Variables: |
| 660 |
* c-basic-offset: 3 |
| 661 |
* indent-tabs-mode: nil |
| 662 |
* End: |
| 663 |
*/ |