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