| 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/types.h> |
|---|
| 27 | #include <sys/wait.h> |
|---|
| 28 | #include <sys/time.h> |
|---|
| 29 | #include <time.h> |
|---|
| 30 | #include <pwd.h> |
|---|
| 31 | |
|---|
| 32 | #include <errno.h> |
|---|
| 33 | #include <caca.h> |
|---|
| 34 | |
|---|
| 35 | #include "neercs.h" |
|---|
| 36 | |
|---|
| 37 | static int send_to_client(const char * msg, struct screen_list* screen_list) |
|---|
| 38 | { |
|---|
| 39 | int ret; |
|---|
| 40 | if(!screen_list->c_socket) |
|---|
| 41 | connect_client(screen_list); |
|---|
| 42 | debug("Sending message (%s) to client on socket %d", msg, screen_list->c_socket); |
|---|
| 43 | if(!screen_list->c_socket) |
|---|
| 44 | ret = -1; |
|---|
| 45 | else |
|---|
| 46 | ret = write(screen_list->c_socket, msg, strlen(msg)); |
|---|
| 47 | if(ret < 0) |
|---|
| 48 | fprintf(stderr, "Failed to send message to client: %s\n", strerror(errno)); |
|---|
| 49 | return ret; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | static int set_title(const char * title, struct screen_list* screen_list) |
|---|
| 53 | { |
|---|
| 54 | char buf[1024]; |
|---|
| 55 | int bytes; |
|---|
| 56 | |
|---|
| 57 | bytes = snprintf(buf, sizeof(buf)-1, "TITLE %s", title); |
|---|
| 58 | buf[bytes] = '\0'; |
|---|
| 59 | |
|---|
| 60 | return send_to_client(buf, screen_list); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | static int set_cursor(int state, struct screen_list* screen_list) |
|---|
| 64 | { |
|---|
| 65 | char buf[16]; |
|---|
| 66 | int bytes; |
|---|
| 67 | |
|---|
| 68 | bytes = snprintf(buf, sizeof(buf)-1, "CURSOR %d", state); |
|---|
| 69 | buf[bytes] = '\0'; |
|---|
| 70 | |
|---|
| 71 | return send_to_client(buf, screen_list); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | static int request_refresh(struct screen_list* screen_list) |
|---|
| 75 | { |
|---|
| 76 | int bytes; |
|---|
| 77 | void *buf; |
|---|
| 78 | char *buf2; |
|---|
| 79 | debug("Resquesting refresh"); |
|---|
| 80 | buf = cucul_export_memory (screen_list->cv, "caca", &bytes); |
|---|
| 81 | buf2 = malloc(bytes+8); |
|---|
| 82 | memcpy(buf2, "REFRESH ", 8); |
|---|
| 83 | memcpy(buf2+8, buf, bytes); |
|---|
| 84 | if(!screen_list->c_socket) |
|---|
| 85 | connect_client(screen_list); |
|---|
| 86 | if(screen_list->c_socket) |
|---|
| 87 | write(screen_list->c_socket, buf2, bytes+8); |
|---|
| 88 | free(buf); |
|---|
| 89 | free(buf2); |
|---|
| 90 | return 0; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | int detach(struct screen_list* screen_list) |
|---|
| 94 | { |
|---|
| 95 | screen_list->attached = 0; |
|---|
| 96 | return send_to_client("DETACH", screen_list); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | int server_main(int *to_grab, char **to_start, struct screen_list *screen_list) |
|---|
| 100 | { |
|---|
| 101 | int i; |
|---|
| 102 | int eof = 0, refresh = 1, command = 0; |
|---|
| 103 | long long unsigned int last_key_time = 0; |
|---|
| 104 | int mainret = 0; |
|---|
| 105 | |
|---|
| 106 | screen_list->attached = 0; |
|---|
| 107 | |
|---|
| 108 | /* Create socket and bind it */ |
|---|
| 109 | create_server_socket(screen_list); |
|---|
| 110 | connect_client(screen_list); |
|---|
| 111 | |
|---|
| 112 | screen_list->width = screen_list->height = 10; |
|---|
| 113 | |
|---|
| 114 | /* Create main canvas */ |
|---|
| 115 | screen_list->cv = cucul_create_canvas(screen_list->width, |
|---|
| 116 | screen_list->height |
|---|
| 117 | + screen_list->mini*6 |
|---|
| 118 | + screen_list->status); |
|---|
| 119 | |
|---|
| 120 | if(!to_grab && !to_start) |
|---|
| 121 | { |
|---|
| 122 | add_screen(screen_list, |
|---|
| 123 | create_screen(screen_list->width, |
|---|
| 124 | screen_list->height, |
|---|
| 125 | screen_list->default_shell)); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | /* Attach processes */ |
|---|
| 129 | if(to_grab) |
|---|
| 130 | { |
|---|
| 131 | for(i=0; to_grab[i]; i++) |
|---|
| 132 | { |
|---|
| 133 | add_screen(screen_list, |
|---|
| 134 | create_screen_grab(screen_list->width, |
|---|
| 135 | screen_list->height, |
|---|
| 136 | to_grab[i])); |
|---|
| 137 | } |
|---|
| 138 | free(to_grab); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | /* Launch command line processes */ |
|---|
| 142 | if(to_start) |
|---|
| 143 | { |
|---|
| 144 | for(i=0; to_start[i]; i++) |
|---|
| 145 | { |
|---|
| 146 | add_screen(screen_list, |
|---|
| 147 | create_screen(screen_list->width, |
|---|
| 148 | screen_list->height, |
|---|
| 149 | to_start[i])); |
|---|
| 150 | } |
|---|
| 151 | free(to_start); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | /* Windows are in a temporary state, resize them to the right dimensions */ |
|---|
| 155 | update_windows_props(screen_list);/* FIXME after attach */ |
|---|
| 156 | |
|---|
| 157 | last_key_time = get_us(); |
|---|
| 158 | |
|---|
| 159 | for(;;) |
|---|
| 160 | { |
|---|
| 161 | caca_event_t ev; |
|---|
| 162 | int ret = 0; |
|---|
| 163 | int quit = 0; |
|---|
| 164 | ssize_t n; |
|---|
| 165 | char buf[4097]; |
|---|
| 166 | |
|---|
| 167 | /* Read program output */ |
|---|
| 168 | refresh |= update_screens_contents(screen_list); |
|---|
| 169 | |
|---|
| 170 | /* Check if we got something from the client */ |
|---|
| 171 | while (screen_list->s_socket && (n = read(screen_list->s_socket, buf, sizeof(buf)-1)) > 0) |
|---|
| 172 | { |
|---|
| 173 | buf[n] = 0; |
|---|
| 174 | debug("Received command %s", buf); |
|---|
| 175 | if(!strncmp("ATTACH ", buf, 7)) |
|---|
| 176 | { |
|---|
| 177 | screen_list->attached = 1; |
|---|
| 178 | cucul_free_canvas(screen_list->cv); |
|---|
| 179 | screen_list->cv = cucul_create_canvas(atoi(buf+7), atoi(buf+18)); |
|---|
| 180 | screen_list->width = cucul_get_canvas_width(screen_list->cv); |
|---|
| 181 | screen_list->height = cucul_get_canvas_height(screen_list->cv) - ((screen_list->mini*6) + (screen_list->status)); |
|---|
| 182 | update_windows_props(screen_list); |
|---|
| 183 | cucul_clear_canvas(screen_list->cv); |
|---|
| 184 | refresh = 1; |
|---|
| 185 | } |
|---|
| 186 | else if(!strncmp("QUIT", buf, 4)) |
|---|
| 187 | { |
|---|
| 188 | quit = 1; |
|---|
| 189 | } |
|---|
| 190 | else if(!strncmp("RESIZE ", buf, 7)) |
|---|
| 191 | { |
|---|
| 192 | cucul_free_canvas(screen_list->cv); |
|---|
| 193 | screen_list->cv = cucul_create_canvas(atoi(buf+7), atoi(buf+18)); |
|---|
| 194 | screen_list->width = cucul_get_canvas_width(screen_list->cv); |
|---|
| 195 | screen_list->height = cucul_get_canvas_height(screen_list->cv) - ((screen_list->mini*6) + (screen_list->status)); |
|---|
| 196 | update_windows_props(screen_list); |
|---|
| 197 | cucul_clear_canvas(screen_list->cv); |
|---|
| 198 | refresh = 1; |
|---|
| 199 | } |
|---|
| 200 | else if(!strncmp("KEY ", buf, 4)) |
|---|
| 201 | { |
|---|
| 202 | unsigned int c = atoi(buf+4); |
|---|
| 203 | char *str = NULL; |
|---|
| 204 | int size = 0; |
|---|
| 205 | /* CTRL-A has been pressed before, handle this as a |
|---|
| 206 | * command, except that CTRL-A a sends literal CTRL-A */ |
|---|
| 207 | if(command && (c != 'a')) |
|---|
| 208 | { |
|---|
| 209 | command = 0; |
|---|
| 210 | refresh |= handle_command_input(screen_list, c); |
|---|
| 211 | } |
|---|
| 212 | else |
|---|
| 213 | { |
|---|
| 214 | /* Not in command mode */ |
|---|
| 215 | last_key_time = get_us(); |
|---|
| 216 | set_cursor(1, screen_list); |
|---|
| 217 | |
|---|
| 218 | /* Kill screensaver */ |
|---|
| 219 | if(screen_list->in_screensaver) |
|---|
| 220 | { |
|---|
| 221 | screensaver_kill(screen_list); |
|---|
| 222 | screen_list->in_screensaver = 0; |
|---|
| 223 | refresh = 1; |
|---|
| 224 | continue; |
|---|
| 225 | } |
|---|
| 226 | /* Handle lock window */ |
|---|
| 227 | if(screen_list->locked) |
|---|
| 228 | refresh |= update_lock(c, screen_list); |
|---|
| 229 | else |
|---|
| 230 | { |
|---|
| 231 | switch(c) |
|---|
| 232 | { |
|---|
| 233 | case 0x01: //CACA_KEY_CTRL_A: |
|---|
| 234 | command = 1; break; |
|---|
| 235 | case CACA_KEY_ESCAPE: |
|---|
| 236 | if(screen_list->help) |
|---|
| 237 | { |
|---|
| 238 | screen_list->help = 0; |
|---|
| 239 | refresh = 1; |
|---|
| 240 | break; |
|---|
| 241 | } |
|---|
| 242 | default: |
|---|
| 243 | /* CTRL-A a sends literal CTRL-A */ |
|---|
| 244 | if (command && (c == 'a')) |
|---|
| 245 | { |
|---|
| 246 | c = 0x01; |
|---|
| 247 | } |
|---|
| 248 | /* Normal key, convert it if needed */ |
|---|
| 249 | str = convert_input_ansi(&c, &size); |
|---|
| 250 | write(screen_list->screen[screen_list->pty]->fd, str, size); |
|---|
| 251 | break; |
|---|
| 252 | } |
|---|
| 253 | } |
|---|
| 254 | } |
|---|
| 255 | } |
|---|
| 256 | else |
|---|
| 257 | { |
|---|
| 258 | fprintf(stderr, "Unknown command received: %s\n", buf); |
|---|
| 259 | } |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | /* No more screens, exit */ |
|---|
| 263 | if(!screen_list->count) break; |
|---|
| 264 | |
|---|
| 265 | /* User requested to exit */ |
|---|
| 266 | if(quit) break; |
|---|
| 267 | |
|---|
| 268 | /* Update each screen canvas */ |
|---|
| 269 | refresh |= update_terms(screen_list); |
|---|
| 270 | |
|---|
| 271 | /* Launch reccurents if any */ |
|---|
| 272 | refresh |= handle_recurrents(screen_list); |
|---|
| 273 | |
|---|
| 274 | /* Resfresh screen */ |
|---|
| 275 | if(!screen_list->attached) |
|---|
| 276 | { |
|---|
| 277 | /* No need to refresh |
|---|
| 278 | * Don't use the CPU too much |
|---|
| 279 | * Would be better to select on terms + socket |
|---|
| 280 | */ |
|---|
| 281 | sleep(1); |
|---|
| 282 | } |
|---|
| 283 | /* Draw lock window */ |
|---|
| 284 | else if(screen_list->locked) |
|---|
| 285 | { |
|---|
| 286 | draw_lock(screen_list); |
|---|
| 287 | refresh = 1; |
|---|
| 288 | } |
|---|
| 289 | else |
|---|
| 290 | { |
|---|
| 291 | if((refresh || screen_list->in_bell) && |
|---|
| 292 | (get_us() - last_key_time < screen_list->screensaver_timeout)) |
|---|
| 293 | { |
|---|
| 294 | refresh_screens(screen_list); |
|---|
| 295 | if(screen_list->attached) |
|---|
| 296 | if(screen_list->screen[screen_list->pty]->title) |
|---|
| 297 | set_title(screen_list->screen[screen_list->pty]->title, screen_list); |
|---|
| 298 | else |
|---|
| 299 | set_title(PACKAGE_STRING, screen_list); |
|---|
| 300 | refresh = 1; |
|---|
| 301 | |
|---|
| 302 | } |
|---|
| 303 | if((get_us() - last_key_time > screen_list->screensaver_timeout)) |
|---|
| 304 | { |
|---|
| 305 | if(!screen_list->in_screensaver) |
|---|
| 306 | { |
|---|
| 307 | screensaver_init(screen_list); |
|---|
| 308 | screen_list->in_screensaver = 1; |
|---|
| 309 | set_cursor(0, screen_list); |
|---|
| 310 | } |
|---|
| 311 | draw_screensaver(screen_list); |
|---|
| 312 | refresh = 1; |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | if((get_us() - last_key_time > screen_list->autolock_timeout)) |
|---|
| 316 | { |
|---|
| 317 | screen_list->locked = 1; |
|---|
| 318 | refresh = 1; |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | if(refresh) |
|---|
| 324 | { |
|---|
| 325 | if(screen_list->attached) |
|---|
| 326 | request_refresh(screen_list); |
|---|
| 327 | refresh = 0; |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | eof = 1; |
|---|
| 331 | for(i=0; i < screen_list->count; i++) |
|---|
| 332 | if(screen_list->screen[i]->fd >= 0) |
|---|
| 333 | eof = 0; |
|---|
| 334 | if(eof) |
|---|
| 335 | break; |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | detach(screen_list); |
|---|
| 339 | |
|---|
| 340 | /* Clean up */ |
|---|
| 341 | cucul_free_canvas(screen_list->cv); |
|---|
| 342 | |
|---|
| 343 | for(i = 0; i < screen_list->count; i++) |
|---|
| 344 | { |
|---|
| 345 | destroy_screen(screen_list->screen[i]); |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | if(screen_list->s_socket_path) |
|---|
| 349 | { |
|---|
| 350 | unlink(screen_list->s_socket_path); |
|---|
| 351 | free(screen_list->s_socket_path); |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | if(screen_list->c_socket_path) |
|---|
| 355 | free(screen_list->c_socket_path); |
|---|
| 356 | |
|---|
| 357 | if(screen_list->s_socket) |
|---|
| 358 | close(screen_list->s_socket); |
|---|
| 359 | |
|---|
| 360 | if(screen_list->c_socket) |
|---|
| 361 | close(screen_list->c_socket); |
|---|
| 362 | |
|---|
| 363 | if(screen_list->screen) free(screen_list->screen); |
|---|
| 364 | |
|---|
| 365 | for(i=0; i<screen_list->recurrent_list->count; i++) |
|---|
| 366 | { |
|---|
| 367 | remove_recurrent(screen_list->recurrent_list, i); |
|---|
| 368 | i = 0; |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | if(screen_list->recurrent_list->recurrent) free(screen_list->recurrent_list->recurrent); |
|---|
| 372 | if(screen_list->recurrent_list) free(screen_list->recurrent_list); |
|---|
| 373 | |
|---|
| 374 | if(screen_list->session_name) |
|---|
| 375 | free(screen_list->session_name); |
|---|
| 376 | |
|---|
| 377 | if(screen_list) |
|---|
| 378 | free(screen_list); |
|---|
| 379 | |
|---|
| 380 | return mainret; |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | long long get_us(void) |
|---|
| 384 | { |
|---|
| 385 | struct timeval tv; |
|---|
| 386 | gettimeofday(&tv, NULL); |
|---|
| 387 | return (tv.tv_sec*(1000000) + tv.tv_usec); |
|---|
| 388 | } |
|---|