1 /* 2 * Copyright 2004-2010, Haiku. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Copyright 2002, Travis Geiselbrecht. All rights reserved. 6 * Distributed under the terms of the NewOS License. 7 */ 8 9 10 #include <ctype.h> 11 #include <dirent.h> 12 #include <errno.h> 13 #include <stdio.h> 14 #include <stdlib.h> 15 #include <string.h> 16 #include <termios.h> 17 #include <unistd.h> 18 19 #include <FindDirectory.h> 20 #include <image.h> 21 #include <InterfaceDefs.h> 22 #include <OS.h> 23 24 #include <keyboard_mouse_driver.h> 25 #include <Keymap.h> 26 27 28 struct console; 29 30 struct keyboard { 31 struct keyboard* next; 32 int device; 33 int target; 34 thread_id thread; 35 }; 36 37 struct console { 38 int console_fd; 39 thread_id console_writer; 40 41 struct keyboard* keyboards; 42 43 int tty_master_fd; 44 int tty_slave_fd; 45 int tty_num; 46 }; 47 48 49 struct console gConsole; 50 51 52 void 53 error(const char* message, ...) 54 { 55 char buffer[2048]; 56 57 va_list args; 58 va_start(args, message); 59 60 vsnprintf(buffer, sizeof(buffer), message, args); 61 62 va_end(args); 63 64 // put it out on stderr as well as to serial/syslog 65 fputs(buffer, stderr); 66 debug_printf("%s", buffer); 67 } 68 69 70 void 71 update_leds(int fd, uint32 modifiers) 72 { 73 char lockIO[3] = {0, 0, 0}; 74 75 if ((modifiers & B_NUM_LOCK) != 0) 76 lockIO[0] = 1; 77 if ((modifiers & B_CAPS_LOCK) != 0) 78 lockIO[1] = 1; 79 if ((modifiers & B_SCROLL_LOCK) != 0) 80 lockIO[2] = 1; 81 82 ioctl(fd, KB_SET_LEDS, &lockIO); 83 } 84 85 86 static int32 87 keyboard_reader(void* arg) 88 { 89 struct keyboard* keyboard = (struct keyboard*)arg; 90 uint8 activeDeadKey = 0; 91 uint32 modifiers = 0; 92 93 BKeymap keymap; 94 // Load current keymap from disk (we can't talk to the input server) 95 // TODO: find a better way (we shouldn't have to care about the on-disk 96 // location) 97 char path[PATH_MAX]; 98 status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, -1, false, 99 path, sizeof(path)); 100 if (status == B_OK) { 101 strlcat(path, "/Key_map", sizeof(path)); 102 status = keymap.SetTo(path); 103 } 104 if (status != B_OK) 105 keymap.SetToDefault(); 106 107 for (;;) { 108 raw_key_info rawKeyInfo; 109 if (ioctl(keyboard->device, KB_READ, &rawKeyInfo) != 0) 110 break; 111 112 uint32 keycode = rawKeyInfo.keycode; 113 bool isKeyDown = rawKeyInfo.is_keydown; 114 115 if (keycode == 0) 116 continue; 117 118 uint32 changedModifiers = keymap.Modifier(keycode); 119 bool isLock = (changedModifiers 120 & (B_CAPS_LOCK | B_NUM_LOCK | B_SCROLL_LOCK)) != 0; 121 if (changedModifiers != 0 && (!isLock || isKeyDown)) { 122 uint32 oldModifiers = modifiers; 123 124 if ((isKeyDown && !isLock) 125 || (isKeyDown && !(modifiers & changedModifiers))) 126 modifiers |= changedModifiers; 127 else { 128 modifiers &= ~changedModifiers; 129 130 // ensure that we don't clear a combined B_*_KEY when still 131 // one of the individual B_{LEFT|RIGHT}_*_KEY is pressed 132 if (modifiers & (B_LEFT_SHIFT_KEY | B_RIGHT_SHIFT_KEY)) 133 modifiers |= B_SHIFT_KEY; 134 if (modifiers & (B_LEFT_COMMAND_KEY | B_RIGHT_COMMAND_KEY)) 135 modifiers |= B_COMMAND_KEY; 136 if (modifiers & (B_LEFT_CONTROL_KEY | B_RIGHT_CONTROL_KEY)) 137 modifiers |= B_CONTROL_KEY; 138 if (modifiers & (B_LEFT_OPTION_KEY | B_RIGHT_OPTION_KEY)) 139 modifiers |= B_OPTION_KEY; 140 } 141 142 if (modifiers != oldModifiers) { 143 if (isLock) 144 update_leds(keyboard->device, modifiers); 145 } 146 } 147 148 uint8 newDeadKey = 0; 149 if (activeDeadKey == 0 || !isKeyDown) 150 newDeadKey = keymap.ActiveDeadKey(keycode, modifiers); 151 152 char* string = NULL; 153 int32 numBytes = 0; 154 if (newDeadKey == 0 && isKeyDown) { 155 keymap.GetChars(keycode, modifiers, activeDeadKey, &string, 156 &numBytes); 157 if (numBytes > 0) 158 write(keyboard->target, string, numBytes); 159 160 delete[] string; 161 } 162 163 if (newDeadKey == 0) { 164 if (isKeyDown && !modifiers && activeDeadKey != 0) { 165 // a dead key was completed 166 activeDeadKey = 0; 167 } 168 } else if (isKeyDown) { 169 // start of a dead key 170 activeDeadKey = newDeadKey; 171 } 172 } 173 174 return 0; 175 } 176 177 178 static int32 179 console_writer(void* arg) 180 { 181 struct console* con = (struct console*)arg; 182 183 for (;;) { 184 char buffer[1024]; 185 ssize_t length = read(con->tty_master_fd, buffer, sizeof(buffer)); 186 if (length < 0) 187 break; 188 189 write(con->console_fd, buffer, length); 190 } 191 192 return 0; 193 } 194 195 196 static void 197 stop_keyboards(struct console* con) 198 { 199 // close devices 200 201 for (struct keyboard* keyboard = con->keyboards; keyboard != NULL; 202 keyboard = keyboard->next) { 203 close(keyboard->device); 204 } 205 206 // wait for the threads 207 208 for (struct keyboard* keyboard = con->keyboards; keyboard != NULL;) { 209 struct keyboard* next = keyboard->next; 210 wait_for_thread(keyboard->thread, NULL); 211 212 delete keyboard; 213 keyboard = next; 214 } 215 216 con->keyboards = NULL; 217 } 218 219 220 /*! Opens the all keyboard drivers it finds starting from the given 221 location \a start that support the debugger extension. 222 */ 223 static struct keyboard* 224 open_keyboards(int target, const char* start, struct keyboard* previous) 225 { 226 DIR* dir = opendir(start); 227 if (dir == NULL) 228 return NULL; 229 230 struct keyboard* keyboard = previous; 231 232 while (true) { 233 dirent* entry = readdir(dir); 234 if (entry == NULL) 235 break; 236 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) 237 continue; 238 239 char path[PATH_MAX]; 240 strlcpy(path, start, sizeof(path)); 241 strlcat(path, "/", sizeof(path)); 242 strlcat(path, entry->d_name, sizeof(path)); 243 244 struct stat stat; 245 if (::stat(path, &stat) != 0) 246 continue; 247 248 if (S_ISDIR(stat.st_mode)) { 249 keyboard = open_keyboards(target, path, keyboard); 250 continue; 251 } 252 253 // Try to open it as a device 254 int fd = open(path, O_RDONLY); 255 if (fd >= 0) { 256 // Turn on debugger mode 257 if (ioctl(fd, KB_SET_DEBUG_READER, NULL, 0) == 0) { 258 keyboard = new ::keyboard(); 259 keyboard->device = fd; 260 keyboard->target = target; 261 keyboard->thread = spawn_thread(&keyboard_reader, path, 262 B_URGENT_DISPLAY_PRIORITY, keyboard); 263 if (keyboard->thread < 0) { 264 close(fd); 265 closedir(dir); 266 delete keyboard; 267 return NULL; 268 } 269 270 if (previous != NULL) 271 previous->next = keyboard; 272 273 resume_thread(keyboard->thread); 274 } else 275 close(fd); 276 } 277 } 278 279 closedir(dir); 280 return keyboard; 281 } 282 283 284 static int 285 start_console(struct console* con) 286 { 287 memset(con, 0, sizeof(struct console)); 288 con->console_fd = -1; 289 con->tty_master_fd = -1; 290 con->tty_slave_fd = -1; 291 con->console_writer = -1; 292 293 con->console_fd = open("/dev/console", O_WRONLY); 294 if (con->console_fd < 0) 295 return -2; 296 297 DIR* dir = opendir("/dev/pt"); 298 if (dir != NULL) { 299 struct dirent* entry; 300 char name[64]; 301 302 while ((entry = readdir(dir)) != NULL) { 303 if (entry->d_name[0] == '.') 304 continue; 305 306 snprintf(name, sizeof(name), "/dev/pt/%s", entry->d_name); 307 308 con->tty_master_fd = open(name, O_RDWR); 309 if (con->tty_master_fd >= 0) { 310 snprintf(name, sizeof(name), "/dev/tt/%s", entry->d_name); 311 312 con->tty_slave_fd = open(name, O_RDWR); 313 if (con->tty_slave_fd < 0) { 314 error("Could not open tty %s: %s!\n", name, 315 strerror(errno)); 316 close(con->tty_master_fd); 317 } else { 318 // set default mode 319 struct termios termios; 320 struct winsize size; 321 322 if (tcgetattr(con->tty_slave_fd, &termios) == 0) { 323 termios.c_iflag = ICRNL; 324 termios.c_oflag = OPOST | ONLCR; 325 termios.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHONL; 326 327 tcsetattr(con->tty_slave_fd, TCSANOW, &termios); 328 } 329 330 if (ioctl(con->console_fd, TIOCGWINSZ, &size, 331 sizeof(struct winsize)) == 0) { 332 // we got the window size from the console 333 ioctl(con->tty_slave_fd, TIOCSWINSZ, &size, 334 sizeof(struct winsize)); 335 } 336 } 337 break; 338 } 339 } 340 341 setenv("TTY", name, true); 342 } 343 344 if (con->tty_master_fd < 0 || con->tty_slave_fd < 0) 345 return -3; 346 347 con->keyboards 348 = open_keyboards(con->tty_master_fd, "/dev/input/keyboard", NULL); 349 if (con->keyboards == NULL) 350 return -4; 351 352 con->console_writer = spawn_thread(&console_writer, "console writer", 353 B_URGENT_DISPLAY_PRIORITY, con); 354 if (con->console_writer < 0) 355 return -5; 356 357 resume_thread(con->console_writer); 358 setenv("TERM", "xterm", true); 359 360 return 0; 361 } 362 363 364 static void 365 stop_console(struct console* con) 366 { 367 // close TTY FDs; this will also unblock the threads 368 close(con->tty_master_fd); 369 close(con->tty_slave_fd); 370 371 // close console and keyboards 372 close(con->console_fd); 373 wait_for_thread(con->console_writer, NULL); 374 375 stop_keyboards(con); 376 } 377 378 379 static pid_t 380 start_process(int argc, const char** argv, struct console* con) 381 { 382 int savedInput = dup(0); 383 int savedOutput = dup(1); 384 int savedError = dup(2); 385 386 dup2(con->tty_slave_fd, 0); 387 dup2(con->tty_slave_fd, 1); 388 dup2(con->tty_slave_fd, 2); 389 390 pid_t pid = load_image(argc, argv, (const char**)environ); 391 resume_thread(pid); 392 setpgid(pid, 0); 393 tcsetpgrp(con->tty_slave_fd, pid); 394 395 dup2(savedInput, 0); 396 dup2(savedOutput, 1); 397 dup2(savedError, 2); 398 close(savedInput); 399 close(savedOutput); 400 close(savedError); 401 402 return pid; 403 } 404 405 406 int 407 main(int argc, char** argv) 408 { 409 // we're a session leader 410 setsid(); 411 412 int err = start_console(&gConsole); 413 if (err < 0) { 414 error("consoled: error %d starting console.\n", err); 415 return err; 416 } 417 418 // move our stdin and stdout to the console 419 dup2(gConsole.tty_slave_fd, 0); 420 dup2(gConsole.tty_slave_fd, 1); 421 dup2(gConsole.tty_slave_fd, 2); 422 423 if (argc > 1) { 424 // a command was given: we run it only once 425 426 // get the command argument vector 427 int commandArgc = argc - 1; 428 const char** commandArgv = new const char*[commandArgc + 1]; 429 for (int i = 0; i < commandArgc; i++) 430 commandArgv[i] = argv[i + 1]; 431 432 commandArgv[commandArgc] = NULL; 433 434 // start the process 435 pid_t process = start_process(commandArgc, commandArgv, &gConsole); 436 437 status_t returnCode; 438 wait_for_thread(process, &returnCode); 439 } else { 440 // no command given: start a shell in an endless loop 441 for (;;) { 442 pid_t shellProcess; 443 status_t returnCode; 444 const char* shellArgv[] = { "/bin/sh", "--login", NULL }; 445 446 shellProcess = start_process(2, shellArgv, &gConsole); 447 448 wait_for_thread(shellProcess, &returnCode); 449 450 puts("Restart shell"); 451 } 452 } 453 454 stop_console(&gConsole); 455 456 return 0; 457 } 458 459