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 int send_to_client(const char * msg, struct screen_list* screen_list) |
---|
39 | { |
---|
40 | int ret; |
---|
41 | if(!screen_list->socket[SOCK_CLIENT]) |
---|
42 | connect_socket(screen_list, SOCK_CLIENT); |
---|
43 | debug("Sending message (%.8s,%d) to client on socket %d", msg, strlen(msg), screen_list->socket[SOCK_CLIENT]); |
---|
44 | if(!screen_list->socket[SOCK_CLIENT]) |
---|
45 | ret = -1; |
---|
46 | else |
---|
47 | ret = write(screen_list->socket[SOCK_CLIENT], msg, strlen(msg)); |
---|
48 | if(ret < 0 && errno != EAGAIN) |
---|
49 | { |
---|
50 | fprintf(stderr, "Failed to send message to client: %s\n", strerror(errno)); |
---|
51 | if(screen_list->attached) |
---|
52 | detach(screen_list); |
---|
53 | } |
---|
54 | return ret; |
---|
55 | } |
---|
56 | |
---|
57 | static int set_title(struct screen_list* screen_list) |
---|
58 | { |
---|
59 | char buf[1024]; |
---|
60 | int bytes; |
---|
61 | char *title; |
---|
62 | |
---|
63 | if(screen_list->attached) |
---|
64 | { |
---|
65 | if(screen_list->pty < screen_list->count && |
---|
66 | screen_list->screen[screen_list->pty]->title) |
---|
67 | title = screen_list->screen[screen_list->pty]->title; |
---|
68 | else |
---|
69 | title = PACKAGE_STRING; |
---|
70 | } |
---|
71 | |
---|
72 | if(screen_list->title) |
---|
73 | { |
---|
74 | if(!strcmp(screen_list->title, title)) |
---|
75 | return 0; |
---|
76 | free(screen_list->title); |
---|
77 | } |
---|
78 | |
---|
79 | screen_list->title = strdup(title); |
---|
80 | |
---|
81 | bytes = snprintf(buf, sizeof(buf)-1, "TITLE %s", title); |
---|
82 | buf[bytes] = '\0'; |
---|
83 | return send_to_client(buf, screen_list); |
---|
84 | } |
---|
85 | |
---|
86 | static int set_cursor(int state, struct screen_list* screen_list) |
---|
87 | { |
---|
88 | char buf[16]; |
---|
89 | int bytes; |
---|
90 | |
---|
91 | bytes = snprintf(buf, sizeof(buf)-1, "CURSOR %d", state); |
---|
92 | buf[bytes] = '\0'; |
---|
93 | |
---|
94 | return send_to_client(buf, screen_list); |
---|
95 | } |
---|
96 | |
---|
97 | static int request_refresh(struct screen_list* screen_list) |
---|
98 | { |
---|
99 | size_t bytes; |
---|
100 | void *buf; |
---|
101 | |
---|
102 | buf = caca_export_memory (screen_list->cv, "caca", &bytes); |
---|
103 | if(!screen_list->socket[SOCK_CLIENT]) |
---|
104 | connect_socket(screen_list, SOCK_CLIENT); |
---|
105 | if(screen_list->socket[SOCK_CLIENT]) |
---|
106 | { |
---|
107 | size_t bufsize, towrite = bytes; |
---|
108 | ssize_t written = 0, ret; |
---|
109 | socklen_t optlen = sizeof(bufsize); |
---|
110 | debug("Requesting refresh for %d", bytes); |
---|
111 | |
---|
112 | getsockopt(screen_list->socket[SOCK_CLIENT], SOL_SOCKET, SO_SNDBUF, |
---|
113 | &bufsize, &optlen); |
---|
114 | bufsize /= 2; |
---|
115 | debug("bufsize=%d", bufsize); |
---|
116 | ret = write(screen_list->socket[SOCK_CLIENT], |
---|
117 | "REFRESH ", |
---|
118 | 8); |
---|
119 | if(ret <= 8 && errno != EAGAIN) |
---|
120 | { |
---|
121 | free(buf); |
---|
122 | return -1; |
---|
123 | } |
---|
124 | while(towrite > 0) |
---|
125 | { |
---|
126 | ssize_t n; |
---|
127 | debug("Wrote %d, %d remaining", written, towrite); |
---|
128 | /* Block to write the end of the message */ |
---|
129 | fcntl(screen_list->socket[SOCK_CLIENT], F_SETFL, 0); |
---|
130 | n = write(screen_list->socket[SOCK_CLIENT], |
---|
131 | (char *)buf + written, |
---|
132 | towrite > bufsize ? bufsize : towrite); |
---|
133 | if(n < 0) |
---|
134 | { |
---|
135 | debug("Can't refresh (%s), with %d bytes (out of %d)", strerror(errno), towrite > bufsize ? bufsize : towrite, towrite); |
---|
136 | return -1; |
---|
137 | } |
---|
138 | written += n; |
---|
139 | towrite -= n; |
---|
140 | } |
---|
141 | fcntl(screen_list->socket[SOCK_CLIENT], F_SETFL, O_NONBLOCK); |
---|
142 | } |
---|
143 | free(buf); |
---|
144 | return 0; |
---|
145 | } |
---|
146 | |
---|
147 | int detach(struct screen_list* screen_list) |
---|
148 | { |
---|
149 | screen_list->attached = 0; |
---|
150 | if(screen_list->socket[SOCK_CLIENT]) |
---|
151 | { |
---|
152 | send_to_client("DETACH", screen_list); |
---|
153 | close(screen_list->socket[SOCK_CLIENT]); |
---|
154 | screen_list->socket[SOCK_CLIENT] = 0; |
---|
155 | } |
---|
156 | return 0; |
---|
157 | } |
---|
158 | |
---|
159 | static int server_main(struct screen_list *screen_list) |
---|
160 | { |
---|
161 | int i; |
---|
162 | int eof = 0, refresh = 1, command = 0, was_in_bell = 0; |
---|
163 | long long unsigned int last_key_time = 0; |
---|
164 | long long unsigned int last_refresh_time = 0; |
---|
165 | int mainret = 0; |
---|
166 | |
---|
167 | screen_list->attached = 0; |
---|
168 | |
---|
169 | /* Create socket and bind it */ |
---|
170 | create_socket(screen_list, SOCK_SERVER); |
---|
171 | |
---|
172 | /* Connect to the client */ |
---|
173 | connect_socket(screen_list, SOCK_CLIENT); |
---|
174 | |
---|
175 | screen_list->width = screen_list->height = 80; |
---|
176 | |
---|
177 | /* Create main canvas */ |
---|
178 | screen_list->cv = caca_create_canvas(screen_list->width, |
---|
179 | screen_list->height |
---|
180 | + screen_list->mini*6 |
---|
181 | + screen_list->status); |
---|
182 | |
---|
183 | if(!screen_list->to_grab && !screen_list->to_start) |
---|
184 | { |
---|
185 | add_screen(screen_list, |
---|
186 | create_screen(screen_list->width, |
---|
187 | screen_list->height, |
---|
188 | screen_list->default_shell)); |
---|
189 | } |
---|
190 | |
---|
191 | /* Attach processes */ |
---|
192 | if(screen_list->to_grab) |
---|
193 | { |
---|
194 | for(i=0; screen_list->to_grab[i]; i++) |
---|
195 | { |
---|
196 | add_screen(screen_list, |
---|
197 | create_screen_grab(screen_list->width, |
---|
198 | screen_list->height, |
---|
199 | screen_list->to_grab[i])); |
---|
200 | } |
---|
201 | free(screen_list->to_grab); |
---|
202 | screen_list->to_grab = NULL; |
---|
203 | } |
---|
204 | |
---|
205 | /* Launch command line processes */ |
---|
206 | if(screen_list->to_start) |
---|
207 | { |
---|
208 | for(i=0; screen_list->to_start[i]; i++) |
---|
209 | { |
---|
210 | add_screen(screen_list, |
---|
211 | create_screen(screen_list->width, |
---|
212 | screen_list->height, |
---|
213 | screen_list->to_start[i])); |
---|
214 | free(screen_list->to_start[i]); |
---|
215 | } |
---|
216 | free(screen_list->to_start); |
---|
217 | screen_list->to_start = NULL; |
---|
218 | } |
---|
219 | |
---|
220 | last_key_time = get_us(); |
---|
221 | |
---|
222 | for(;;) |
---|
223 | { |
---|
224 | int quit = 0; |
---|
225 | ssize_t n; |
---|
226 | char buf[128]; |
---|
227 | |
---|
228 | /* Read program output */ |
---|
229 | refresh |= update_screens_contents(screen_list); |
---|
230 | |
---|
231 | /* Check if we got something from the client */ |
---|
232 | while (screen_list->socket[SOCK_SERVER] && (n = read(screen_list->socket[SOCK_SERVER], buf, sizeof(buf)-1)) > 0) |
---|
233 | { |
---|
234 | buf[n] = 0; |
---|
235 | debug("Received command %s", buf); |
---|
236 | if(!strncmp("ATTACH ", buf, 7)) |
---|
237 | { |
---|
238 | /* If we were attached to someone else, detach first */ |
---|
239 | if(screen_list->attached) |
---|
240 | detach(screen_list); |
---|
241 | screen_list->attached = 1; |
---|
242 | caca_free_canvas(screen_list->cv); |
---|
243 | screen_list->cv = caca_create_canvas(atoi(buf+7), atoi(buf+18)); |
---|
244 | screen_list->delay = atoi(buf+29); |
---|
245 | screen_list->width = caca_get_canvas_width(screen_list->cv); |
---|
246 | screen_list->height = caca_get_canvas_height(screen_list->cv) - ((screen_list->mini*6) + (screen_list->status)); |
---|
247 | update_windows_props(screen_list); |
---|
248 | caca_clear_canvas(screen_list->cv); |
---|
249 | refresh = 1; |
---|
250 | } |
---|
251 | else if(!strncmp("QUIT", buf, 4)) |
---|
252 | { |
---|
253 | quit = 1; |
---|
254 | } |
---|
255 | else if(!strncmp("DELAY ", buf, 6)) |
---|
256 | { |
---|
257 | screen_list->delay = atoi(buf+6); |
---|
258 | } |
---|
259 | else if(!strncmp("RESIZE ", buf, 7)) |
---|
260 | { |
---|
261 | caca_free_canvas(screen_list->cv); |
---|
262 | screen_list->cv = caca_create_canvas(atoi(buf+7), atoi(buf+18)); |
---|
263 | screen_list->width = caca_get_canvas_width(screen_list->cv); |
---|
264 | screen_list->height = caca_get_canvas_height(screen_list->cv) - ((screen_list->mini*6) + (screen_list->status)); |
---|
265 | screen_list->changed = 1; |
---|
266 | update_windows_props(screen_list); |
---|
267 | caca_clear_canvas(screen_list->cv); |
---|
268 | refresh = 1; |
---|
269 | } |
---|
270 | else if(!strncmp("KEY ", buf, 4)) |
---|
271 | { |
---|
272 | unsigned int c = atoi(buf+4); |
---|
273 | char *str = NULL; |
---|
274 | int size = 0; |
---|
275 | /* CTRL-A has been pressed before, handle this as a |
---|
276 | * command, except that CTRL-A a sends literal CTRL-A */ |
---|
277 | if(command && (c != 'a')) |
---|
278 | { |
---|
279 | command = 0; |
---|
280 | refresh |= handle_command_input(screen_list, c); |
---|
281 | } |
---|
282 | else |
---|
283 | { |
---|
284 | /* Not in command mode */ |
---|
285 | last_key_time = get_us(); |
---|
286 | set_cursor(1, screen_list); |
---|
287 | |
---|
288 | /* Kill screensaver */ |
---|
289 | if(screen_list->in_screensaver) |
---|
290 | { |
---|
291 | screensaver_kill(screen_list); |
---|
292 | screen_list->in_screensaver = 0; |
---|
293 | screen_list->changed = 1; |
---|
294 | refresh = 1; |
---|
295 | continue; |
---|
296 | } |
---|
297 | /* Handle lock window */ |
---|
298 | if(screen_list->locked) |
---|
299 | { |
---|
300 | refresh |= update_lock(c, screen_list); |
---|
301 | screen_list->changed = 1; |
---|
302 | } |
---|
303 | else if(screen_list->window_list) { |
---|
304 | refresh |= update_window_list(c, screen_list); |
---|
305 | screen_list->changed = 1; |
---|
306 | } |
---|
307 | else |
---|
308 | { |
---|
309 | switch(c) |
---|
310 | { |
---|
311 | case 0x01: //CACA_KEY_CTRL_A: |
---|
312 | command = 1; break; |
---|
313 | case CACA_KEY_ESCAPE: |
---|
314 | if(screen_list->help) |
---|
315 | { |
---|
316 | screen_list->help = 0; |
---|
317 | screen_list->changed = 1; |
---|
318 | refresh = 1; |
---|
319 | break; |
---|
320 | } |
---|
321 | default: |
---|
322 | /* CTRL-A a sends literal CTRL-A */ |
---|
323 | if (command && (c == 'a')) |
---|
324 | { |
---|
325 | c = 0x01; |
---|
326 | } |
---|
327 | /* Normal key, convert it if needed */ |
---|
328 | str = convert_input_ansi(&c, &size); |
---|
329 | write(screen_list->screen[screen_list->pty]->fd, str, size); |
---|
330 | break; |
---|
331 | } |
---|
332 | } |
---|
333 | } |
---|
334 | } |
---|
335 | else |
---|
336 | { |
---|
337 | fprintf(stderr, "Unknown command received: %s\n", buf); |
---|
338 | } |
---|
339 | } |
---|
340 | |
---|
341 | /* No more screens, exit */ |
---|
342 | if(!screen_list->count) break; |
---|
343 | |
---|
344 | /* User requested to exit */ |
---|
345 | if(quit) break; |
---|
346 | |
---|
347 | /* Update each screen canvas */ |
---|
348 | refresh |= update_terms(screen_list); |
---|
349 | |
---|
350 | /* Launch reccurents if any */ |
---|
351 | refresh |= handle_recurrents(screen_list); |
---|
352 | |
---|
353 | /* Refresh screen */ |
---|
354 | if(!screen_list->attached) |
---|
355 | { |
---|
356 | /* No need to refresh |
---|
357 | * Don't use the CPU too much |
---|
358 | * Would be better to select on terms + socket |
---|
359 | */ |
---|
360 | sleep(1); |
---|
361 | } |
---|
362 | else |
---|
363 | { |
---|
364 | long long unsigned int current_time = get_us(); |
---|
365 | long long int tdiff = (current_time - last_refresh_time)/1000; |
---|
366 | /* Draw lock window */ |
---|
367 | if(screen_list->locked) |
---|
368 | { |
---|
369 | draw_lock(screen_list); |
---|
370 | refresh = 1; |
---|
371 | } |
---|
372 | else if((current_time - last_key_time >= screen_list->autolock_timeout)) |
---|
373 | { |
---|
374 | screen_list->locked = 1; |
---|
375 | refresh = 1; |
---|
376 | } |
---|
377 | else if((current_time - last_key_time >= screen_list->screensaver_timeout)) |
---|
378 | { |
---|
379 | if(!screen_list->in_screensaver) |
---|
380 | { |
---|
381 | screensaver_init(screen_list); |
---|
382 | screen_list->in_screensaver = 1; |
---|
383 | set_cursor(0, screen_list); |
---|
384 | } |
---|
385 | draw_screensaver(screen_list); |
---|
386 | refresh = 1; |
---|
387 | } |
---|
388 | else if(refresh || was_in_bell) |
---|
389 | { |
---|
390 | if(tdiff >= screen_list->delay) { |
---|
391 | was_in_bell = screen_list->in_bell; |
---|
392 | refresh_screens(screen_list); |
---|
393 | set_title(screen_list); |
---|
394 | refresh = 1; |
---|
395 | } |
---|
396 | } |
---|
397 | if(refresh) |
---|
398 | { |
---|
399 | if(tdiff >= screen_list->delay) { |
---|
400 | request_refresh(screen_list); |
---|
401 | refresh = 0; |
---|
402 | last_refresh_time = current_time; |
---|
403 | } |
---|
404 | else |
---|
405 | debug("Skipping refresh (%d < %d)", tdiff, screen_list->delay); |
---|
406 | } |
---|
407 | } |
---|
408 | eof = 1; |
---|
409 | for(i=0; i < screen_list->count; i++) |
---|
410 | if(screen_list->screen[i]->fd >= 0) |
---|
411 | eof = 0; |
---|
412 | if(eof) |
---|
413 | break; |
---|
414 | } |
---|
415 | |
---|
416 | detach(screen_list); |
---|
417 | |
---|
418 | /* Clean up */ |
---|
419 | caca_free_canvas(screen_list->cv); |
---|
420 | |
---|
421 | for(i = 0; i < screen_list->count; i++) |
---|
422 | { |
---|
423 | destroy_screen(screen_list->screen[i]); |
---|
424 | } |
---|
425 | |
---|
426 | if(screen_list->socket_path[SOCK_SERVER]) |
---|
427 | { |
---|
428 | unlink(screen_list->socket_path[SOCK_SERVER]); |
---|
429 | free(screen_list->socket_path[SOCK_SERVER]); |
---|
430 | } |
---|
431 | |
---|
432 | if(screen_list->socket_path[SOCK_CLIENT]) |
---|
433 | free(screen_list->socket_path[SOCK_CLIENT]); |
---|
434 | |
---|
435 | if(screen_list->socket[SOCK_SERVER]) |
---|
436 | close(screen_list->socket[SOCK_SERVER]); |
---|
437 | |
---|
438 | if(screen_list->socket[SOCK_CLIENT]) |
---|
439 | close(screen_list->socket[SOCK_CLIENT]); |
---|
440 | |
---|
441 | if(screen_list->screen) free(screen_list->screen); |
---|
442 | |
---|
443 | for(i=0; i<screen_list->recurrent_list->count; i++) |
---|
444 | { |
---|
445 | remove_recurrent(screen_list->recurrent_list, i); |
---|
446 | i = 0; |
---|
447 | } |
---|
448 | |
---|
449 | if(screen_list->recurrent_list->recurrent) free(screen_list->recurrent_list->recurrent); |
---|
450 | if(screen_list->recurrent_list) free(screen_list->recurrent_list); |
---|
451 | |
---|
452 | if(screen_list->session_name) { |
---|
453 | free(screen_list->session_name); |
---|
454 | screen_list->session_name = NULL; |
---|
455 | } |
---|
456 | |
---|
457 | if(screen_list->title) |
---|
458 | free(screen_list->title); |
---|
459 | |
---|
460 | if(screen_list) { |
---|
461 | free(screen_list); |
---|
462 | screen_list = NULL; |
---|
463 | } |
---|
464 | |
---|
465 | return mainret; |
---|
466 | } |
---|
467 | |
---|
468 | int start_server(struct screen_list *screen_list) |
---|
469 | { |
---|
470 | pid_t pid; |
---|
471 | char * sess; |
---|
472 | |
---|
473 | pid = fork(); |
---|
474 | if(pid < 0) |
---|
475 | { |
---|
476 | perror("Failed to create child process"); |
---|
477 | return -1; |
---|
478 | } |
---|
479 | if(pid == 0) |
---|
480 | { |
---|
481 | int fd; |
---|
482 | close(0); |
---|
483 | close(1); |
---|
484 | close(2); |
---|
485 | fd = open("/dev/null", O_RDWR, 0); |
---|
486 | if(fd < 0) |
---|
487 | { |
---|
488 | perror("Failed to open /dev/null"); |
---|
489 | return -2; |
---|
490 | } |
---|
491 | dup2(fd, 0); |
---|
492 | #ifndef DEBUG |
---|
493 | dup2(fd, 1); |
---|
494 | dup2(fd, 2); |
---|
495 | if (fd > 2) |
---|
496 | close(fd); |
---|
497 | #else |
---|
498 | if(fd != 0) |
---|
499 | close(fd); |
---|
500 | fd = open("/tmp/neercs-debug.txt", O_TRUNC|O_RDWR|O_CREAT, S_IRUSR|S_IWUSR); |
---|
501 | dup2(fd, 1); |
---|
502 | dup2(fd, 2); |
---|
503 | if (fd > 2) |
---|
504 | close(fd); |
---|
505 | #endif |
---|
506 | setsid(); |
---|
507 | return server_main(screen_list); |
---|
508 | } |
---|
509 | create_socket(screen_list, SOCK_CLIENT); |
---|
510 | while((sess = connect_socket(screen_list, SOCK_SERVER)) == NULL) |
---|
511 | usleep(100); |
---|
512 | free(sess); |
---|
513 | |
---|
514 | /* Create main canvas and associated caca window */ |
---|
515 | screen_list->cv = caca_create_canvas(0, 0); |
---|
516 | screen_list->dp = caca_create_display(screen_list->cv); |
---|
517 | caca_set_display_time(screen_list->dp, screen_list->delay*1000); |
---|
518 | |
---|
519 | if(!screen_list->dp) |
---|
520 | return -3; |
---|
521 | caca_set_cursor(screen_list->dp, 1); |
---|
522 | |
---|
523 | request_attach(screen_list); |
---|
524 | |
---|
525 | return 0; |
---|
526 | } |
---|
527 | |
---|
528 | long long get_us(void) |
---|
529 | { |
---|
530 | struct timeval tv; |
---|
531 | gettimeofday(&tv, NULL); |
---|
532 | return (tv.tv_sec*(1000000) + tv.tv_usec); |
---|
533 | } |
---|