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 return NULL; 266 } 267 268 if (previous != NULL) 269 previous->next = keyboard; 270 271 resume_thread(keyboard->thread); 272 } else 273 close(fd); 274 } 275 } 276 277 closedir(dir); 278 return keyboard; 279 } 280 281 282 static int 283 start_console(struct console* con) 284 { 285 memset(con, 0, sizeof(struct console)); 286 con->console_fd = -1; 287 con->tty_master_fd = -1; 288 con->tty_slave_fd = -1; 289 con->console_writer = -1; 290 291 con->console_fd = open("/dev/console", O_WRONLY); 292 if (con->console_fd < 0) 293 return -2; 294 295 DIR* dir = opendir("/dev/pt"); 296 if (dir != NULL) { 297 struct dirent* entry; 298 char name[64]; 299 300 while ((entry = readdir(dir)) != NULL) { 301 if (entry->d_name[0] == '.') 302 continue; 303 304 snprintf(name, sizeof(name), "/dev/pt/%s", entry->d_name); 305 306 con->tty_master_fd = open(name, O_RDWR); 307 if (con->tty_master_fd >= 0) { 308 snprintf(name, sizeof(name), "/dev/tt/%s", entry->d_name); 309 310 con->tty_slave_fd = open(name, O_RDWR); 311 if (con->tty_slave_fd < 0) { 312 error("Could not open tty %s: %s!\n", name, 313 strerror(errno)); 314 close(con->tty_master_fd); 315 } else { 316 // set default mode 317 struct termios termios; 318 struct winsize size; 319 320 if (tcgetattr(con->tty_slave_fd, &termios) == 0) { 321 termios.c_iflag = ICRNL; 322 termios.c_oflag = OPOST | ONLCR; 323 termios.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHONL; 324 325 tcsetattr(con->tty_slave_fd, TCSANOW, &termios); 326 } 327 328 if (ioctl(con->console_fd, TIOCGWINSZ, &size, 329 sizeof(struct winsize)) == 0) { 330 // we got the window size from the console 331 ioctl(con->tty_slave_fd, TIOCSWINSZ, &size, 332 sizeof(struct winsize)); 333 } 334 } 335 break; 336 } 337 } 338 339 setenv("TTY", name, true); 340 } 341 342 if (con->tty_master_fd < 0 || con->tty_slave_fd < 0) 343 return -3; 344 345 con->keyboards 346 = open_keyboards(con->tty_master_fd, "/dev/input/keyboard", NULL); 347 if (con->keyboards == NULL) 348 return -4; 349 350 con->console_writer = spawn_thread(&console_writer, "console writer", 351 B_URGENT_DISPLAY_PRIORITY, con); 352 if (con->console_writer < 0) 353 return -5; 354 355 resume_thread(con->console_writer); 356 setenv("TERM", "xterm", true); 357 358 return 0; 359 } 360 361 362 static void 363 stop_console(struct console* con) 364 { 365 // close TTY FDs; this will also unblock the threads 366 close(con->tty_master_fd); 367 close(con->tty_slave_fd); 368 369 // close console and keyboards 370 close(con->console_fd); 371 wait_for_thread(con->console_writer, NULL); 372 373 stop_keyboards(con); 374 } 375 376 377 static pid_t 378 start_process(int argc, const char** argv, struct console* con) 379 { 380 int savedInput = dup(0); 381 int savedOutput = dup(1); 382 int savedError = dup(2); 383 384 dup2(con->tty_slave_fd, 0); 385 dup2(con->tty_slave_fd, 1); 386 dup2(con->tty_slave_fd, 2); 387 388 pid_t pid = load_image(argc, argv, (const char**)environ); 389 resume_thread(pid); 390 setpgid(pid, 0); 391 tcsetpgrp(con->tty_slave_fd, pid); 392 393 dup2(savedInput, 0); 394 dup2(savedOutput, 1); 395 dup2(savedError, 2); 396 close(savedInput); 397 close(savedOutput); 398 close(savedError); 399 400 return pid; 401 } 402 403 404 int 405 main(int argc, char** argv) 406 { 407 // we're a session leader 408 setsid(); 409 410 int err = start_console(&gConsole); 411 if (err < 0) { 412 error("consoled: error %d starting console.\n", err); 413 return err; 414 } 415 416 // move our stdin and stdout to the console 417 dup2(gConsole.tty_slave_fd, 0); 418 dup2(gConsole.tty_slave_fd, 1); 419 dup2(gConsole.tty_slave_fd, 2); 420 421 if (argc > 1) { 422 // a command was given: we run it only once 423 424 // get the command argument vector 425 int commandArgc = argc - 1; 426 const char** commandArgv = new const char*[commandArgc + 1]; 427 for (int i = 0; i < commandArgc; i++) 428 commandArgv[i] = argv[i + 1]; 429 430 commandArgv[commandArgc] = NULL; 431 432 // start the process 433 pid_t process = start_process(commandArgc, commandArgv, &gConsole); 434 435 status_t returnCode; 436 wait_for_thread(process, &returnCode); 437 } else { 438 // no command given: start a shell in an endless loop 439 for (;;) { 440 pid_t shellProcess; 441 status_t returnCode; 442 const char* shellArgv[] = { "/bin/sh", "--login", NULL }; 443 444 shellProcess = start_process(2, shellArgv, &gConsole); 445 446 wait_for_thread(shellProcess, &returnCode); 447 448 puts("Restart shell"); 449 } 450 } 451 452 stop_console(&gConsole); 453 454 return 0; 455 } 456 457