| 1 | /* |
|---|
| 2 | * neercs console-based window manager |
|---|
| 3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
|---|
| 4 | * 2008 Jean-Yves Lamoureux <jylam@lnxscene.org> |
|---|
| 5 | * 2008 Pascal Terjan <pterjan@linuxfr.org> |
|---|
| 6 | * All Rights Reserved |
|---|
| 7 | * |
|---|
| 8 | * $Id$ |
|---|
| 9 | * |
|---|
| 10 | * This program is free software. It comes without any warranty, to |
|---|
| 11 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 12 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 13 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 14 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | #include "config.h" |
|---|
| 18 | |
|---|
| 19 | #include <stdio.h> |
|---|
| 20 | #include <string.h> |
|---|
| 21 | #include <stdlib.h> |
|---|
| 22 | #include <unistd.h> |
|---|
| 23 | #include <fcntl.h> |
|---|
| 24 | #include <signal.h> |
|---|
| 25 | #include <sys/ioctl.h> |
|---|
| 26 | #include <sys/socket.h> |
|---|
| 27 | #include <sys/types.h> |
|---|
| 28 | #include <sys/wait.h> |
|---|
| 29 | #include <sys/time.h> |
|---|
| 30 | #include <time.h> |
|---|
| 31 | #include <pwd.h> |
|---|
| 32 | |
|---|
| 33 | #include <errno.h> |
|---|
| 34 | #include <caca.h> |
|---|
| 35 | |
|---|
| 36 | #include "neercs.h" |
|---|
| 37 | |
|---|
| 38 | static void server_init(struct screen_list *screen_list); |
|---|
| 39 | static int refresh_screen(struct screen_list *screen_list, int refresh); |
|---|
| 40 | static int handle_key(struct screen_list *screen_list, unsigned int c, |
|---|
| 41 | int refresh); |
|---|
| 42 | static int handle_attach(struct screen_list *screen_list, char *buf); |
|---|
| 43 | |
|---|
| 44 | static int send_to_client(const char *msg, struct screen_list *screen_list) |
|---|
| 45 | { |
|---|
| 46 | int ret; |
|---|
| 47 | if (!screen_list->socket[SOCK_CLIENT]) |
|---|
| 48 | connect_socket(screen_list, SOCK_CLIENT); |
|---|
| 49 | debug("Sending message (%.8s,%d) to client on socket %d", msg, strlen(msg), |
|---|
| 50 | screen_list->socket[SOCK_CLIENT]); |
|---|
| 51 | if (!screen_list->socket[SOCK_CLIENT]) |
|---|
| 52 | ret = -1; |
|---|
| 53 | else |
|---|
| 54 | ret = write(screen_list->socket[SOCK_CLIENT], msg, strlen(msg)); |
|---|
| 55 | if (ret < 0 && errno != EAGAIN) |
|---|
| 56 | { |
|---|
| 57 | fprintf(stderr, "Failed to send message to client: %s\n", |
|---|
| 58 | strerror(errno)); |
|---|
| 59 | if (screen_list->attached) |
|---|
| 60 | detach(screen_list); |
|---|
| 61 | } |
|---|
| 62 | return ret; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | static int set_title(struct screen_list *screen_list) |
|---|
| 66 | { |
|---|
| 67 | char buf[1024]; |
|---|
| 68 | int bytes; |
|---|
| 69 | char *title = NULL; |
|---|
| 70 | |
|---|
| 71 | if (screen_list->attached) |
|---|
| 72 | { |
|---|
| 73 | if (screen_list->pty < screen_list->count && |
|---|
| 74 | screen_list->screen[screen_list->pty]->title) |
|---|
| 75 | title = screen_list->screen[screen_list->pty]->title; |
|---|
| 76 | else |
|---|
| 77 | title = PACKAGE_STRING; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | if (screen_list->title) |
|---|
| 81 | { |
|---|
| 82 | if (!strcmp(screen_list->title, title)) |
|---|
| 83 | return 0; |
|---|
| 84 | free(screen_list->title); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | screen_list->title = strdup(title); |
|---|
| 88 | |
|---|
| 89 | bytes = snprintf(buf, sizeof(buf) - 1, "TITLE %s", title); |
|---|
| 90 | buf[bytes] = '\0'; |
|---|
| 91 | return send_to_client(buf, screen_list); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | static int set_cursor(int state, struct screen_list *screen_list) |
|---|
| 95 | { |
|---|
| 96 | char buf[16]; |
|---|
| 97 | int bytes; |
|---|
| 98 | |
|---|
| 99 | bytes = snprintf(buf, sizeof(buf) - 1, "CURSOR %d", state); |
|---|
| 100 | buf[bytes] = '\0'; |
|---|
| 101 | |
|---|
| 102 | return send_to_client(buf, screen_list); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | static int request_refresh(struct screen_list *screen_list) |
|---|
| 106 | { |
|---|
| 107 | #if defined HAVE_CACA_DIRTY_RECTANGLES |
|---|
| 108 | int ndirty = caca_get_dirty_rect_count(screen_list->cv); |
|---|
| 109 | if (!ndirty) |
|---|
| 110 | return 0; |
|---|
| 111 | #endif |
|---|
| 112 | if (!screen_list->socket[SOCK_CLIENT]) |
|---|
| 113 | connect_socket(screen_list, SOCK_CLIENT); |
|---|
| 114 | if (screen_list->socket[SOCK_CLIENT]) |
|---|
| 115 | { |
|---|
| 116 | size_t bufsize, towrite; |
|---|
| 117 | ssize_t written, ret; |
|---|
| 118 | socklen_t optlen = sizeof(bufsize); |
|---|
| 119 | size_t bytes; |
|---|
| 120 | void *buf; |
|---|
| 121 | char buf2[32]; |
|---|
| 122 | int x, y, i; |
|---|
| 123 | |
|---|
| 124 | getsockopt(screen_list->socket[SOCK_CLIENT], SOL_SOCKET, SO_SNDBUF, |
|---|
| 125 | &bufsize, &optlen); |
|---|
| 126 | bufsize /= 2; |
|---|
| 127 | debug("bufsize=%d", bufsize); |
|---|
| 128 | |
|---|
| 129 | #if defined HAVE_CACA_DIRTY_RECTANGLES |
|---|
| 130 | for (i = 0; i < ndirty; i++) |
|---|
| 131 | { |
|---|
| 132 | int w, h; |
|---|
| 133 | caca_get_dirty_rect(screen_list->cv, i, &x, &y, &w, &h); |
|---|
| 134 | debug("dirty @%d,%d %dx%d [%dx%d]", x, y, w, h, |
|---|
| 135 | caca_get_canvas_width(screen_list->cv), |
|---|
| 136 | caca_get_canvas_height(screen_list->cv)); |
|---|
| 137 | buf = |
|---|
| 138 | caca_export_area_to_memory(screen_list->cv, x, y, w, h, "caca", |
|---|
| 139 | &bytes); |
|---|
| 140 | #else |
|---|
| 141 | { |
|---|
| 142 | i = 0; |
|---|
| 143 | x = 0; |
|---|
| 144 | y = 0; |
|---|
| 145 | buf = caca_export_memory(screen_list->cv, "caca", &bytes); |
|---|
| 146 | #endif |
|---|
| 147 | debug("Requesting refresh for %d", bytes); |
|---|
| 148 | towrite = bytes; |
|---|
| 149 | written = 0; |
|---|
| 150 | sprintf(buf2, "UPDATE %10d %10d", x, y); |
|---|
| 151 | ret = |
|---|
| 152 | write(screen_list->socket[SOCK_CLIENT], buf2, |
|---|
| 153 | strlen(buf2) + 1); |
|---|
| 154 | if (ret < 29 && errno != EAGAIN) |
|---|
| 155 | { |
|---|
| 156 | free(buf); |
|---|
| 157 | return -1; |
|---|
| 158 | } |
|---|
| 159 | while (towrite > 0) |
|---|
| 160 | { |
|---|
| 161 | ssize_t n; |
|---|
| 162 | debug("Wrote %d, %d remaining", written, towrite); |
|---|
| 163 | /* Block to write the end of the message */ |
|---|
| 164 | fcntl(screen_list->socket[SOCK_CLIENT], F_SETFL, 0); |
|---|
| 165 | n = write(screen_list->socket[SOCK_CLIENT], |
|---|
| 166 | (char *)buf + written, |
|---|
| 167 | towrite > bufsize ? bufsize : towrite); |
|---|
| 168 | if (n < 0) |
|---|
| 169 | { |
|---|
| 170 | debug("Can't refresh (%s), with %d bytes (out of %d)", |
|---|
| 171 | strerror(errno), |
|---|
| 172 | towrite > bufsize ? bufsize : towrite, towrite); |
|---|
| 173 | return -1; |
|---|
| 174 | } |
|---|
| 175 | written += n; |
|---|
| 176 | towrite -= n; |
|---|
| 177 | } |
|---|
| 178 | fcntl(screen_list->socket[SOCK_CLIENT], F_SETFL, O_NONBLOCK); |
|---|
| 179 | free(buf); |
|---|
| 180 | } |
|---|
| 181 | sprintf(buf2, "REFRESH %10d %10d", caca_get_cursor_x(screen_list->cv), |
|---|
| 182 | caca_get_cursor_y(screen_list->cv)); |
|---|
| 183 | /* FIXME check value of r */ |
|---|
| 184 | int r = |
|---|
| 185 | write(screen_list->socket[SOCK_CLIENT], buf2, strlen(buf2) + 1); |
|---|
| 186 | (void)r; |
|---|
| 187 | #if defined HAVE_CACA_DIRTY_RECTANGLES |
|---|
| 188 | caca_clear_dirty_rect_list(screen_list->cv); |
|---|
| 189 | #endif |
|---|
| 190 | } |
|---|
| 191 | return 0; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | int detach(struct screen_list *screen_list) |
|---|
| 195 | { |
|---|
| 196 | screen_list->attached = 0; |
|---|
| 197 | if (screen_list->lock_on_detach) |
|---|
| 198 | screen_list->locked = 1; |
|---|
| 199 | if (screen_list->socket[SOCK_CLIENT]) |
|---|
| 200 | { |
|---|
| 201 | send_to_client("DETACH", screen_list); |
|---|
| 202 | close(screen_list->socket[SOCK_CLIENT]); |
|---|
| 203 | screen_list->socket[SOCK_CLIENT] = 0; |
|---|
| 204 | } |
|---|
| 205 | return 0; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | static void server_main(struct screen_list *screen_list) |
|---|
| 209 | { |
|---|
| 210 | int i; |
|---|
| 211 | int eof = 0, refresh = 1; |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | screen_list->last_key_time = 0; |
|---|
| 216 | screen_list->attached = 0; |
|---|
| 217 | screen_list->command = 0; |
|---|
| 218 | screen_list->was_in_bell = 0; |
|---|
| 219 | screen_list->last_refresh_time = 0; |
|---|
| 220 | |
|---|
| 221 | server_init(screen_list); |
|---|
| 222 | |
|---|
| 223 | for (;;) |
|---|
| 224 | { |
|---|
| 225 | int quit = 0; |
|---|
| 226 | ssize_t n; |
|---|
| 227 | char buf[128]; |
|---|
| 228 | |
|---|
| 229 | /* Read program output */ |
|---|
| 230 | refresh |= update_screens_contents(screen_list); |
|---|
| 231 | |
|---|
| 232 | /* Check if we got something from the client */ |
|---|
| 233 | while (screen_list->socket[SOCK_SERVER] |
|---|
| 234 | && (n = |
|---|
| 235 | read(screen_list->socket[SOCK_SERVER], buf, |
|---|
| 236 | sizeof(buf) - 1)) > 0) |
|---|
| 237 | { |
|---|
| 238 | buf[n] = 0; |
|---|
| 239 | debug("Received command %s", buf); |
|---|
| 240 | if (!strncmp("ATTACH ", buf, 7)) |
|---|
| 241 | { |
|---|
| 242 | refresh |= handle_attach(screen_list, buf); |
|---|
| 243 | } |
|---|
| 244 | else if (!strncmp("QUIT", buf, 4)) |
|---|
| 245 | { |
|---|
| 246 | quit = 1; |
|---|
| 247 | } |
|---|
| 248 | else if (!strncmp("DELAY ", buf, 6)) |
|---|
| 249 | { |
|---|
| 250 | /* FIXME check the length before calling atoi */ |
|---|
| 251 | screen_list->delay = atoi(buf + 6); |
|---|
| 252 | } |
|---|
| 253 | else if (!strncmp("RESIZE ", buf, 7)) |
|---|
| 254 | { |
|---|
| 255 | caca_free_canvas(screen_list->cv); |
|---|
| 256 | /* FIXME check the length before calling atoi */ |
|---|
| 257 | screen_list->cv = |
|---|
| 258 | caca_create_canvas(atoi(buf + 7), atoi(buf + 18)); |
|---|
| 259 | screen_list->changed = 1; |
|---|
| 260 | refresh = 1; |
|---|
| 261 | } |
|---|
| 262 | else if (!strncmp("KEY ", buf, 4)) |
|---|
| 263 | { |
|---|
| 264 | unsigned int c = atoi(buf + 4); |
|---|
| 265 | refresh |= handle_key(screen_list, c, refresh); |
|---|
| 266 | } |
|---|
| 267 | else |
|---|
| 268 | { |
|---|
| 269 | fprintf(stderr, "Unknown command received: %s\n", buf); |
|---|
| 270 | } |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | /* No more screens, exit */ |
|---|
| 274 | if (!screen_list->count) |
|---|
| 275 | break; |
|---|
| 276 | |
|---|
| 277 | /* User requested to exit */ |
|---|
| 278 | if (quit) |
|---|
| 279 | break; |
|---|
| 280 | |
|---|
| 281 | /* Update each screen canvas */ |
|---|
| 282 | refresh |= update_terms(screen_list); |
|---|
| 283 | |
|---|
| 284 | /* Launch recurrents if any */ |
|---|
| 285 | refresh |= handle_recurrents(screen_list); |
|---|
| 286 | |
|---|
| 287 | /* Refresh screen */ |
|---|
| 288 | refresh |= refresh_screen(screen_list, refresh); |
|---|
| 289 | |
|---|
| 290 | eof = 1; |
|---|
| 291 | for (i = 0; i < screen_list->count; i++) |
|---|
| 292 | if (screen_list->screen[i]->fd >= 0) |
|---|
| 293 | eof = 0; |
|---|
| 294 | if (eof) |
|---|
| 295 | break; |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | detach(screen_list); |
|---|
| 299 | |
|---|
| 300 | free_screen_list(screen_list); |
|---|
| 301 | |
|---|
| 302 | exit(0); |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | static int refresh_screen(struct screen_list *screen_list, int refresh) |
|---|
| 306 | { |
|---|
| 307 | if (!screen_list->attached) |
|---|
| 308 | { |
|---|
| 309 | /* No need to refresh Don't use the CPU too much Would be better to |
|---|
| 310 | select on terms + socket */ |
|---|
| 311 | sleep(1); |
|---|
| 312 | } |
|---|
| 313 | else |
|---|
| 314 | { |
|---|
| 315 | long long unsigned int current_time = get_us(); |
|---|
| 316 | long long int tdiff = |
|---|
| 317 | (current_time - screen_list->last_refresh_time) / 1000; |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | if (screen_list->force_refresh) |
|---|
| 321 | { |
|---|
| 322 | wm_refresh(screen_list); |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | /* Draw lock window */ |
|---|
| 326 | if (screen_list->locked) |
|---|
| 327 | { |
|---|
| 328 | draw_lock(screen_list); |
|---|
| 329 | refresh = 1; |
|---|
| 330 | } |
|---|
| 331 | else if ((current_time - screen_list->last_key_time >= |
|---|
| 332 | screen_list->autolock_timeout)) |
|---|
| 333 | { |
|---|
| 334 | screen_list->locked = 1; |
|---|
| 335 | refresh = 1; |
|---|
| 336 | } |
|---|
| 337 | else if ((current_time - screen_list->last_key_time >= |
|---|
| 338 | screen_list->screensaver_timeout)) |
|---|
| 339 | { |
|---|
| 340 | if (!screen_list->in_screensaver) |
|---|
| 341 | { |
|---|
| 342 | screensaver_init(screen_list); |
|---|
| 343 | screen_list->in_screensaver = 1; |
|---|
| 344 | set_cursor(0, screen_list); |
|---|
| 345 | } |
|---|
| 346 | draw_screensaver(screen_list); |
|---|
| 347 | refresh = 1; |
|---|
| 348 | } |
|---|
| 349 | else if (refresh || screen_list->was_in_bell) |
|---|
| 350 | { |
|---|
| 351 | if (tdiff >= screen_list->delay) |
|---|
| 352 | { |
|---|
| 353 | screen_list->was_in_bell = screen_list->in_bell; |
|---|
| 354 | refresh_screens(screen_list); |
|---|
| 355 | set_title(screen_list); |
|---|
| 356 | refresh = 1; |
|---|
| 357 | } |
|---|
| 358 | } |
|---|
| 359 | if (refresh) |
|---|
| 360 | { |
|---|
| 361 | if (tdiff >= screen_list->delay) |
|---|
| 362 | { |
|---|
| 363 | request_refresh(screen_list); |
|---|
| 364 | refresh = 0; |
|---|
| 365 | screen_list->last_refresh_time = current_time; |
|---|
| 366 | } |
|---|
| 367 | else |
|---|
| 368 | { |
|---|
| 369 | debug("Skipping refresh (%lld < %d)", tdiff, |
|---|
| 370 | screen_list->delay); |
|---|
| 371 | } |
|---|
| 372 | } |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | return 1; |
|---|
| 376 | } |
|---|
| 377 | |
|---|
| 378 | static void server_init(struct screen_list *screen_list) |
|---|
| 379 | { |
|---|
| 380 | int i; |
|---|
| 381 | /* Create socket and bind it */ |
|---|
| 382 | create_socket(screen_list, SOCK_SERVER); |
|---|
| 383 | |
|---|
| 384 | /* Connect to the client */ |
|---|
| 385 | connect_socket(screen_list, SOCK_CLIENT); |
|---|
| 386 | |
|---|
| 387 | screen_list->width = screen_list->height = 80; |
|---|
| 388 | |
|---|
| 389 | /* Create main canvas */ |
|---|
| 390 | screen_list->cv = caca_create_canvas(screen_list->width, |
|---|
| 391 | screen_list->height |
|---|
| 392 | + screen_list->mini * 6 |
|---|
| 393 | + screen_list->status); |
|---|
| 394 | |
|---|
| 395 | if (!screen_list->to_grab && !screen_list->to_start) |
|---|
| 396 | { |
|---|
| 397 | add_screen(screen_list, |
|---|
| 398 | create_screen(screen_list->width, |
|---|
| 399 | screen_list->height, |
|---|
| 400 | screen_list->default_shell)); |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | /* Attach processes */ |
|---|
| 404 | if (screen_list->to_grab) |
|---|
| 405 | { |
|---|
| 406 | for (i = 0; screen_list->to_grab[i]; i++) |
|---|
| 407 | { |
|---|
| 408 | add_screen(screen_list, |
|---|
| 409 | create_screen_grab(screen_list->width, |
|---|
| 410 | screen_list->height, |
|---|
| 411 | screen_list->to_grab[i])); |
|---|
| 412 | } |
|---|
| 413 | free(screen_list->to_grab); |
|---|
| 414 | screen_list->to_grab = NULL; |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | /* Launch command line processes */ |
|---|
| 418 | if (screen_list->to_start) |
|---|
| 419 | { |
|---|
| 420 | for (i = 0; screen_list->to_start[i]; i++) |
|---|
| 421 | { |
|---|
| 422 | add_screen(screen_list, |
|---|
| 423 | create_screen(screen_list->width, |
|---|
| 424 | screen_list->height, |
|---|
| 425 | screen_list->to_start[i])); |
|---|
| 426 | free(screen_list->to_start[i]); |
|---|
| 427 | } |
|---|
| 428 | free(screen_list->to_start); |
|---|
| 429 | screen_list->to_start = NULL; |
|---|
| 430 | } |
|---|
| 431 | |
|---|
| 432 | screen_list->last_key_time = get_us(); |
|---|
| 433 | } |
|---|
| 434 | |
|---|
| 435 | static int handle_attach(struct screen_list *screen_list, char *buf) |
|---|
| 436 | { |
|---|
| 437 | /* If we were attached to someone else, detach first */ |
|---|
| 438 | if (screen_list->attached) |
|---|
| 439 | detach(screen_list); |
|---|
| 440 | screen_list->attached = 1; |
|---|
| 441 | caca_free_canvas(screen_list->cv); |
|---|
| 442 | screen_list->cv = caca_create_canvas(atoi(buf + 7), atoi(buf + 18)); |
|---|
| 443 | screen_list->delay = atoi(buf + 29); |
|---|
| 444 | screen_list->changed = 1; |
|---|
| 445 | return 1; |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| 448 | static int handle_key(struct screen_list *screen_list, unsigned int c, |
|---|
| 449 | int refresh) |
|---|
| 450 | { |
|---|
| 451 | char *str = NULL; |
|---|
| 452 | int size = 0; |
|---|
| 453 | /* CTRL-A has been pressed before, handle this as a command, except that |
|---|
| 454 | CTRL-A a sends literal CTRL-A */ |
|---|
| 455 | |
|---|
| 456 | |
|---|
| 457 | if (screen_list->help) |
|---|
| 458 | { |
|---|
| 459 | if (c == CACA_KEY_ESCAPE || c == 'h') |
|---|
| 460 | { |
|---|
| 461 | screen_list->help = 0; |
|---|
| 462 | screen_list->changed = 1; |
|---|
| 463 | refresh = 1; |
|---|
| 464 | return 1; |
|---|
| 465 | } |
|---|
| 466 | else |
|---|
| 467 | { |
|---|
| 468 | return 0; |
|---|
| 469 | } |
|---|
| 470 | } |
|---|
| 471 | |
|---|
| 472 | if (screen_list->command && (c != 'a')) |
|---|
| 473 | { |
|---|
| 474 | screen_list->command = 0; |
|---|
| 475 | refresh |= handle_command_input(screen_list, c); |
|---|
| 476 | } |
|---|
| 477 | else |
|---|
| 478 | { |
|---|
| 479 | /* Not in command mode */ |
|---|
| 480 | screen_list->last_key_time = get_us(); |
|---|
| 481 | set_cursor(1, screen_list); |
|---|
| 482 | |
|---|
| 483 | /* Kill screensaver */ |
|---|
| 484 | if (screen_list->in_screensaver) |
|---|
| 485 | { |
|---|
| 486 | screensaver_kill(screen_list); |
|---|
| 487 | screen_list->in_screensaver = 0; |
|---|
| 488 | screen_list->changed = 1; |
|---|
| 489 | refresh = 1; |
|---|
| 490 | return refresh; |
|---|
| 491 | } |
|---|
| 492 | /* Handle lock window */ |
|---|
| 493 | if (screen_list->locked) |
|---|
| 494 | { |
|---|
| 495 | refresh |= update_lock(c, screen_list); |
|---|
| 496 | screen_list->changed = 1; |
|---|
| 497 | } |
|---|
| 498 | else if (screen_list->window_list) |
|---|
| 499 | { |
|---|
| 500 | refresh |= update_window_list(c, screen_list); |
|---|
| 501 | screen_list->changed = 1; |
|---|
| 502 | } |
|---|
| 503 | else |
|---|
| 504 | { |
|---|
| 505 | switch (c) |
|---|
| 506 | { |
|---|
| 507 | case 0x01: // CACA_KEY_CTRL_A: |
|---|
| 508 | screen_list->command = 1; |
|---|
| 509 | break; |
|---|
| 510 | default: |
|---|
| 511 | /* CTRL-A a sends literal CTRL-A */ |
|---|
| 512 | if (screen_list->command && (c == 'a')) |
|---|
| 513 | { |
|---|
| 514 | c = 0x01; |
|---|
| 515 | } |
|---|
| 516 | /* Normal key, convert it if needed */ |
|---|
| 517 | str = convert_input_ansi(&c, &size); |
|---|
| 518 | /* FIXME check value of r */ |
|---|
| 519 | int r = write(screen_list->screen[screen_list->pty]->fd, str, |
|---|
| 520 | size); |
|---|
| 521 | (void)r; |
|---|
| 522 | break; |
|---|
| 523 | } |
|---|
| 524 | } |
|---|
| 525 | } |
|---|
| 526 | return refresh; |
|---|
| 527 | } |
|---|
| 528 | |
|---|
| 529 | |
|---|
| 530 | int start_server(struct screen_list *screen_list) |
|---|
| 531 | { |
|---|
| 532 | pid_t pid; |
|---|
| 533 | |
|---|
| 534 | pid = fork(); |
|---|
| 535 | if (pid < 0) |
|---|
| 536 | { |
|---|
| 537 | perror("Failed to create child process"); |
|---|
| 538 | return -1; |
|---|
| 539 | } |
|---|
| 540 | if (pid == 0) |
|---|
| 541 | { |
|---|
| 542 | int fd; |
|---|
| 543 | close(0); |
|---|
| 544 | close(1); |
|---|
| 545 | close(2); |
|---|
| 546 | fd = open("/dev/null", O_RDWR, 0); |
|---|
| 547 | if (fd < 0) |
|---|
| 548 | { |
|---|
| 549 | perror("Failed to open /dev/null"); |
|---|
| 550 | return -2; |
|---|
| 551 | } |
|---|
| 552 | dup2(fd, 0); |
|---|
| 553 | #ifndef DEBUG |
|---|
| 554 | dup2(fd, 1); |
|---|
| 555 | dup2(fd, 2); |
|---|
| 556 | if (fd > 2) |
|---|
| 557 | close(fd); |
|---|
| 558 | #else |
|---|
| 559 | if (fd != 0) |
|---|
| 560 | close(fd); |
|---|
| 561 | fd = open("/tmp/neercs-debug.txt", O_TRUNC | O_RDWR | O_CREAT, |
|---|
| 562 | S_IRUSR | S_IWUSR); |
|---|
| 563 | dup2(fd, 1); |
|---|
| 564 | dup2(fd, 2); |
|---|
| 565 | if (fd > 2) |
|---|
| 566 | close(fd); |
|---|
| 567 | #endif |
|---|
| 568 | setsid(); |
|---|
| 569 | |
|---|
| 570 | server_main(screen_list); |
|---|
| 571 | /* Never returns */ |
|---|
| 572 | } |
|---|
| 573 | |
|---|
| 574 | return 0; |
|---|
| 575 | } |
|---|
| 576 | |
|---|
| 577 | long long get_us(void) |
|---|
| 578 | { |
|---|
| 579 | struct timeval tv; |
|---|
| 580 | gettimeofday(&tv, NULL); |
|---|
| 581 | return (tv.tv_sec * (1000000) + tv.tv_usec); |
|---|
| 582 | } |
|---|