| 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 | |
|---|
| 33 | #if !defined HAVE_GETOPT_LONG |
|---|
| 34 | # include "mygetopt.h" |
|---|
| 35 | #elif defined HAVE_GETOPT_H |
|---|
| 36 | # include <getopt.h> |
|---|
| 37 | #endif |
|---|
| 38 | #if defined HAVE_GETOPT_LONG |
|---|
| 39 | # define mygetopt getopt_long |
|---|
| 40 | # define myoptind optind |
|---|
| 41 | # define myoptarg optarg |
|---|
| 42 | # define myoption option |
|---|
| 43 | #endif |
|---|
| 44 | #include <errno.h> |
|---|
| 45 | #include <cucul.h> |
|---|
| 46 | #include <caca.h> |
|---|
| 47 | |
|---|
| 48 | #include "neercs.h" |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | void version(void) |
|---|
| 52 | { |
|---|
| 53 | printf("%s\n", PACKAGE_STRING); |
|---|
| 54 | printf("Copyright (C) 2006, 2008 Sam Hocevar <sam@zoy.org>\n"); |
|---|
| 55 | printf(" Jean-Yves Lamoureux <jylam@lnxscene.org>\n\n"); |
|---|
| 56 | printf("This is free software. You may redistribute copies of it under the\n"); |
|---|
| 57 | printf("terms of the Do What The Fuck You Want To Public License, Version 2\n"); |
|---|
| 58 | printf("<http://sam.zoy.org/wtfpl/>.\n"); |
|---|
| 59 | printf("There is NO WARRANTY, to the extent permitted by law.\n"); |
|---|
| 60 | printf("\n"); |
|---|
| 61 | printf("For more informations, visit http://libcaca.zoy.org/wiki/neercs\n"); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | void usage(int argc, char **argv) |
|---|
| 65 | { |
|---|
| 66 | printf("%s\n", PACKAGE_STRING); |
|---|
| 67 | printf("Usage : %s [command1] [command2] ... [commandN]\n", argv[0]); |
|---|
| 68 | printf("Example : %s zsh top \n\n", argv[0]); |
|---|
| 69 | printf("Options :\n"); |
|---|
| 70 | printf("\t--config\t-c <file>\t\tuse given config file\n"); |
|---|
| 71 | printf("\t--pid\t\t-P <pid>\t\tgrab <pid>\n"); |
|---|
| 72 | printf("\t\t\t-r [session]\t\treattach to a detached neercs\n"); |
|---|
| 73 | printf("\t\t\t-R [session]\t\treattach if possible, otherwise start a new session\n"); |
|---|
| 74 | printf("\t\t\t-S <name>\t\tname this session <name> instead of <pid>\n"); |
|---|
| 75 | printf("\t--version\t-v \t\t\tdisplay version and exit\n"); |
|---|
| 76 | printf("\t--help\t\t-h \t\t\tthis help\n"); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | int main(int argc, char **argv) |
|---|
| 80 | { |
|---|
| 81 | struct screen_list *screen_list = NULL; |
|---|
| 82 | struct passwd *user_info; |
|---|
| 83 | char *user_path = NULL, *user_dir = NULL, *session_name = NULL; |
|---|
| 84 | int i, args, s=0; |
|---|
| 85 | int eof = 0, refresh = 1, command = 0; |
|---|
| 86 | long long unsigned int last_key_time = 0; |
|---|
| 87 | int mainret = 0; |
|---|
| 88 | int attach = 0, forceattach = 0; |
|---|
| 89 | int *to_grab = NULL; |
|---|
| 90 | int nb_to_grab = 0; |
|---|
| 91 | |
|---|
| 92 | screen_list = create_screen_list(); |
|---|
| 93 | screen_list->default_shell = getenv("SHELL"); |
|---|
| 94 | |
|---|
| 95 | args = argc -1; |
|---|
| 96 | if(screen_list->default_shell == NULL && args <= 0) |
|---|
| 97 | { |
|---|
| 98 | fprintf(stderr, "Environment variable SHELL not set and no arguments given. kthxbye.\n"); |
|---|
| 99 | return -1; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | if(args==0) |
|---|
| 103 | args = 1; |
|---|
| 104 | |
|---|
| 105 | /* Build local config file path */ |
|---|
| 106 | user_dir = getenv("HOME"); |
|---|
| 107 | if(!user_dir) |
|---|
| 108 | { |
|---|
| 109 | user_info = getpwuid(getuid()); |
|---|
| 110 | if(user_info) |
|---|
| 111 | { |
|---|
| 112 | user_dir = user_info->pw_dir; |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | if(user_dir) |
|---|
| 116 | { |
|---|
| 117 | user_path = malloc(strlen(user_dir) + strlen("/.neercsrc") + 1); |
|---|
| 118 | sprintf(user_path, "%s/%s", user_dir, ".neercsrc"); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | screen_list->recurrent_list = (struct recurrent_list*) malloc(sizeof(struct recurrent_list)); |
|---|
| 122 | screen_list->recurrent_list->recurrent = (struct recurrent**) malloc(sizeof(struct recurrent*)); |
|---|
| 123 | if(!screen_list->recurrent_list->recurrent) |
|---|
| 124 | { |
|---|
| 125 | fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); |
|---|
| 126 | return -1; |
|---|
| 127 | } |
|---|
| 128 | screen_list->recurrent_list->count = 0; |
|---|
| 129 | |
|---|
| 130 | for(;;) |
|---|
| 131 | { |
|---|
| 132 | int option_index = 0; |
|---|
| 133 | int pidopt; |
|---|
| 134 | static struct myoption long_options[] = |
|---|
| 135 | { |
|---|
| 136 | { "config", 1, NULL, 'c' }, |
|---|
| 137 | #if defined USE_GRAB |
|---|
| 138 | { "pid", 1, NULL, 'P' }, |
|---|
| 139 | #endif |
|---|
| 140 | { "help", 0, NULL, 'h' }, |
|---|
| 141 | { "version", 0, NULL, 'v' }, |
|---|
| 142 | }; |
|---|
| 143 | #if defined USE_GRAB |
|---|
| 144 | int c = mygetopt(argc, argv, "c:S:R::r::P:hv", long_options, &option_index); |
|---|
| 145 | #else |
|---|
| 146 | int c = mygetopt(argc, argv, "c:S:R::r::hv", long_options, &option_index); |
|---|
| 147 | #endif |
|---|
| 148 | if(c == -1) |
|---|
| 149 | break; |
|---|
| 150 | |
|---|
| 151 | switch(c) |
|---|
| 152 | { |
|---|
| 153 | case 'c': /* --config */ |
|---|
| 154 | if(user_path) |
|---|
| 155 | free(user_path); |
|---|
| 156 | user_path = strdup(myoptarg); |
|---|
| 157 | s+=2; |
|---|
| 158 | break; |
|---|
| 159 | case 'S': |
|---|
| 160 | if(screen_list->session_name) |
|---|
| 161 | free(screen_list->session_name); |
|---|
| 162 | screen_list->session_name = strdup(myoptarg); |
|---|
| 163 | s+=2; |
|---|
| 164 | break; |
|---|
| 165 | case 'P': /* --pid */ |
|---|
| 166 | pidopt = atoi(myoptarg); |
|---|
| 167 | if(pidopt <= 0) |
|---|
| 168 | { |
|---|
| 169 | fprintf(stderr, "Invalid pid %d\n", pidopt); |
|---|
| 170 | if(to_grab) |
|---|
| 171 | free(to_grab); |
|---|
| 172 | return -1; |
|---|
| 173 | } |
|---|
| 174 | if(!to_grab) |
|---|
| 175 | { |
|---|
| 176 | /* At most argc-1-s times -P <pid> */ |
|---|
| 177 | to_grab = (int *)malloc(((argc-1-s)/2)*sizeof(int)); |
|---|
| 178 | if(!to_grab) |
|---|
| 179 | { |
|---|
| 180 | fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); |
|---|
| 181 | return -1; |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | to_grab[nb_to_grab++] = pidopt; |
|---|
| 185 | s+=2; |
|---|
| 186 | break; |
|---|
| 187 | case 'r': |
|---|
| 188 | forceattach = 1; |
|---|
| 189 | case 'R': |
|---|
| 190 | if(attach) |
|---|
| 191 | { |
|---|
| 192 | fprintf(stderr, "Attaching can only be requested once\n"); |
|---|
| 193 | return -1; |
|---|
| 194 | } |
|---|
| 195 | if(myoptarg) |
|---|
| 196 | { |
|---|
| 197 | session_name = strdup(myoptarg); |
|---|
| 198 | s+=1; |
|---|
| 199 | } |
|---|
| 200 | attach = 1; |
|---|
| 201 | s+=1; |
|---|
| 202 | break; |
|---|
| 203 | case 'h': /* --help */ |
|---|
| 204 | usage(argc, argv); |
|---|
| 205 | return 0; |
|---|
| 206 | break; |
|---|
| 207 | case 'v': /* --version */ |
|---|
| 208 | version(); |
|---|
| 209 | return 0; |
|---|
| 210 | break; |
|---|
| 211 | default: |
|---|
| 212 | fprintf(stderr, "Unknown argument #%d\n", myoptind); |
|---|
| 213 | return -1; |
|---|
| 214 | break; |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | /* Read global configuration first */ |
|---|
| 219 | read_configuration_file("/etc/neercsrc", screen_list); |
|---|
| 220 | |
|---|
| 221 | /* Then local one */ |
|---|
| 222 | if(user_path) |
|---|
| 223 | { |
|---|
| 224 | read_configuration_file(user_path, screen_list); |
|---|
| 225 | free(user_path); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | if(attach) |
|---|
| 229 | { |
|---|
| 230 | char **sockets; |
|---|
| 231 | if(nb_to_grab || (argc-1 > s)) |
|---|
| 232 | { |
|---|
| 233 | fprintf(stderr, "-R can not be associated with commands or pids!\n"); |
|---|
| 234 | return -1; |
|---|
| 235 | } |
|---|
| 236 | sockets = list_sockets(screen_list->socket_dir, session_name); |
|---|
| 237 | if(!screen_list->session_name) |
|---|
| 238 | screen_list->session_name = session_name; |
|---|
| 239 | else |
|---|
| 240 | free(session_name); |
|---|
| 241 | if(sockets && sockets[0]) |
|---|
| 242 | { |
|---|
| 243 | request_attach(sockets[0]); |
|---|
| 244 | fprintf(stderr, "Failed to attach!\n"); |
|---|
| 245 | for(i=0; sockets[i]; i++) |
|---|
| 246 | free(sockets[i]); |
|---|
| 247 | free(sockets); |
|---|
| 248 | } |
|---|
| 249 | else |
|---|
| 250 | { |
|---|
| 251 | fprintf(stderr, "No socket found!\n"); |
|---|
| 252 | } |
|---|
| 253 | if(forceattach) |
|---|
| 254 | return -1; |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | /* Build default session name */ |
|---|
| 258 | if(!screen_list->session_name) |
|---|
| 259 | { |
|---|
| 260 | char mypid[32]; /* FIXME Compute the length of PID_MAX ? */ |
|---|
| 261 | snprintf(mypid, 31, "%d", getpid()); |
|---|
| 262 | mypid[31]= '\0'; |
|---|
| 263 | screen_list->session_name = strdup(mypid); |
|---|
| 264 | if(!screen_list->session_name) |
|---|
| 265 | { |
|---|
| 266 | fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); |
|---|
| 267 | return -1; |
|---|
| 268 | } |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | /* Create main canvas and associated caca window */ |
|---|
| 272 | screen_list->cv = cucul_create_canvas(0, 0); |
|---|
| 273 | screen_list->dp = caca_create_display(screen_list->cv); |
|---|
| 274 | if(!screen_list->dp) |
|---|
| 275 | return 1; |
|---|
| 276 | caca_set_cursor(screen_list->dp, 1); |
|---|
| 277 | |
|---|
| 278 | screen_list->width = cucul_get_canvas_width(screen_list->cv); |
|---|
| 279 | screen_list->height = cucul_get_canvas_height(screen_list->cv) - ((screen_list->mini*6) + (screen_list->status)); |
|---|
| 280 | |
|---|
| 281 | if(nb_to_grab == 0 && s == argc -1) |
|---|
| 282 | { |
|---|
| 283 | add_screen(screen_list, |
|---|
| 284 | create_screen(screen_list->width, |
|---|
| 285 | screen_list->height, |
|---|
| 286 | screen_list->default_shell)); |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | /* Attach processes */ |
|---|
| 290 | for(i=0; i<nb_to_grab; i++) |
|---|
| 291 | { |
|---|
| 292 | add_screen(screen_list,create_screen_grab(screen_list->width, screen_list->height, to_grab[i])); |
|---|
| 293 | } |
|---|
| 294 | free(to_grab); |
|---|
| 295 | |
|---|
| 296 | /* Launch command line processes */ |
|---|
| 297 | for(i=0; i<(argc-1) - s; i++) |
|---|
| 298 | { |
|---|
| 299 | add_screen(screen_list, create_screen(screen_list->width, screen_list->height, argv[i+s+1])); |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | /* Windows are in a temporary state, resize them to the right dimensions */ |
|---|
| 303 | update_windows_props(screen_list); |
|---|
| 304 | |
|---|
| 305 | last_key_time = get_us(); |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | for(;;) |
|---|
| 309 | { |
|---|
| 310 | caca_event_t ev; |
|---|
| 311 | int ret = 0; |
|---|
| 312 | |
|---|
| 313 | /* Read program output */ |
|---|
| 314 | refresh |= update_screens_contents(screen_list); |
|---|
| 315 | |
|---|
| 316 | /* If screen was attached, read its output */ |
|---|
| 317 | refresh |= read_socket(screen_list, screen_list->cv, &screen_list->dp); |
|---|
| 318 | |
|---|
| 319 | /* No more screens, exit */ |
|---|
| 320 | if(!screen_list->count) break; |
|---|
| 321 | |
|---|
| 322 | /* Update each screen canvas */ |
|---|
| 323 | refresh |= update_terms(screen_list); |
|---|
| 324 | |
|---|
| 325 | /* Get events, if any */ |
|---|
| 326 | if(screen_list->attached) |
|---|
| 327 | ret = caca_get_event(screen_list->dp, CACA_EVENT_ANY, &ev, 0); |
|---|
| 328 | else |
|---|
| 329 | sleep(1); |
|---|
| 330 | |
|---|
| 331 | if(ret && (caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS)) |
|---|
| 332 | { |
|---|
| 333 | unsigned int c = caca_get_event_key_ch(&ev); |
|---|
| 334 | char *str = NULL; |
|---|
| 335 | int size = 0; |
|---|
| 336 | /* CTRL-A has been pressed before, handle this as a command */ |
|---|
| 337 | if(command) |
|---|
| 338 | { |
|---|
| 339 | command = 0; |
|---|
| 340 | refresh |= handle_command_input(screen_list, c); |
|---|
| 341 | } |
|---|
| 342 | else |
|---|
| 343 | { |
|---|
| 344 | /* Not in command mode */ |
|---|
| 345 | last_key_time = get_us(); |
|---|
| 346 | caca_set_cursor(screen_list->dp, 1); |
|---|
| 347 | |
|---|
| 348 | /* Kill screensaver */ |
|---|
| 349 | if(screen_list->in_screensaver) |
|---|
| 350 | { |
|---|
| 351 | screensaver_kill(screen_list); |
|---|
| 352 | screen_list->in_screensaver = 0; |
|---|
| 353 | refresh = 1; |
|---|
| 354 | continue; |
|---|
| 355 | } |
|---|
| 356 | /* Handle lock window */ |
|---|
| 357 | if(screen_list->locked) |
|---|
| 358 | refresh |= update_lock(c, screen_list); |
|---|
| 359 | else |
|---|
| 360 | { |
|---|
| 361 | switch(c) |
|---|
| 362 | { |
|---|
| 363 | case 0x01: //CACA_KEY_CTRL_A: |
|---|
| 364 | command = 1; break; |
|---|
| 365 | case CACA_KEY_ESCAPE: |
|---|
| 366 | if(screen_list->help) |
|---|
| 367 | { |
|---|
| 368 | screen_list->help = 0; |
|---|
| 369 | refresh = 1; |
|---|
| 370 | break; |
|---|
| 371 | } |
|---|
| 372 | default: |
|---|
| 373 | /* Normal key, convert it if needed */ |
|---|
| 374 | str = convert_input_ansi(&c, &size); |
|---|
| 375 | write(screen_list->screen[screen_list->pty]->fd, str, size); |
|---|
| 376 | break; |
|---|
| 377 | } |
|---|
| 378 | } |
|---|
| 379 | } |
|---|
| 380 | } |
|---|
| 381 | /* Window resized */ |
|---|
| 382 | else if(ret && (caca_get_event_type(&ev) & CACA_EVENT_RESIZE)) |
|---|
| 383 | { |
|---|
| 384 | update_windows_props(screen_list); |
|---|
| 385 | cucul_clear_canvas(screen_list->cv); |
|---|
| 386 | refresh = 1; |
|---|
| 387 | } |
|---|
| 388 | /* Window closed */ |
|---|
| 389 | else if(ret && (caca_get_event_type(&ev) & CACA_EVENT_QUIT)) |
|---|
| 390 | { |
|---|
| 391 | refresh = 1; |
|---|
| 392 | break; |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| 395 | /* Launch reccurents if any */ |
|---|
| 396 | refresh |= handle_recurrents(screen_list); |
|---|
| 397 | |
|---|
| 398 | /* Resfresh screen */ |
|---|
| 399 | if(!screen_list->attached) |
|---|
| 400 | { |
|---|
| 401 | /* No need to refresh */ |
|---|
| 402 | } |
|---|
| 403 | /* Draw lock window */ |
|---|
| 404 | else if(screen_list->locked) |
|---|
| 405 | { |
|---|
| 406 | draw_lock(screen_list); |
|---|
| 407 | refresh = 1; |
|---|
| 408 | } |
|---|
| 409 | else |
|---|
| 410 | { |
|---|
| 411 | if((refresh || screen_list->in_bell) && |
|---|
| 412 | (get_us() - last_key_time < screen_list->screensaver_timeout)) |
|---|
| 413 | { |
|---|
| 414 | refresh_screens(screen_list); |
|---|
| 415 | if(screen_list->screen[screen_list->pty]->title) |
|---|
| 416 | caca_set_display_title(screen_list->dp, screen_list->screen[screen_list->pty]->title); |
|---|
| 417 | else |
|---|
| 418 | caca_set_display_title(screen_list->dp, PACKAGE_STRING); |
|---|
| 419 | refresh = 1; |
|---|
| 420 | |
|---|
| 421 | } |
|---|
| 422 | |
|---|
| 423 | if((get_us() - last_key_time > screen_list->screensaver_timeout)) |
|---|
| 424 | { |
|---|
| 425 | if(!screen_list->in_screensaver) |
|---|
| 426 | screensaver_init(screen_list); |
|---|
| 427 | screen_list->in_screensaver = 1; |
|---|
| 428 | |
|---|
| 429 | caca_set_cursor(screen_list->dp, 0); |
|---|
| 430 | draw_screensaver(screen_list); |
|---|
| 431 | refresh = 1; |
|---|
| 432 | } |
|---|
| 433 | } |
|---|
| 434 | |
|---|
| 435 | /* Refresh screen if needed */ |
|---|
| 436 | if(refresh) |
|---|
| 437 | { |
|---|
| 438 | caca_refresh_display(screen_list->dp); |
|---|
| 439 | refresh = 0; |
|---|
| 440 | } |
|---|
| 441 | |
|---|
| 442 | eof = 1; |
|---|
| 443 | for(i=0; i < screen_list->count; i++) |
|---|
| 444 | if(screen_list->screen[i]->fd >= 0) |
|---|
| 445 | eof = 0; |
|---|
| 446 | if(eof) |
|---|
| 447 | break; |
|---|
| 448 | } |
|---|
| 449 | |
|---|
| 450 | /* Clean up */ |
|---|
| 451 | if(screen_list->dp) |
|---|
| 452 | caca_free_display(screen_list->dp); |
|---|
| 453 | |
|---|
| 454 | cucul_free_canvas(screen_list->cv); |
|---|
| 455 | |
|---|
| 456 | for(i = 0; i < screen_list->count; i++) |
|---|
| 457 | { |
|---|
| 458 | destroy_screen(screen_list->screen[i]); |
|---|
| 459 | } |
|---|
| 460 | |
|---|
| 461 | if(screen_list->socket_path) { |
|---|
| 462 | unlink(screen_list->socket_path); |
|---|
| 463 | free(screen_list->socket_path); |
|---|
| 464 | } |
|---|
| 465 | |
|---|
| 466 | if(screen_list->socket) |
|---|
| 467 | close(screen_list->socket); |
|---|
| 468 | |
|---|
| 469 | if(screen_list->screen) free(screen_list->screen); |
|---|
| 470 | |
|---|
| 471 | |
|---|
| 472 | struct option *option = screen_list->config; |
|---|
| 473 | |
|---|
| 474 | while(option) |
|---|
| 475 | { |
|---|
| 476 | struct option *kromeugnon = option; |
|---|
| 477 | option = option->next; |
|---|
| 478 | if(kromeugnon->key) free(kromeugnon->key); |
|---|
| 479 | if(kromeugnon->value) free(kromeugnon->value); |
|---|
| 480 | free(kromeugnon); |
|---|
| 481 | } |
|---|
| 482 | |
|---|
| 483 | for(i=0; i<screen_list->recurrent_list->count; i++) |
|---|
| 484 | { |
|---|
| 485 | remove_recurrent(screen_list->recurrent_list, i); |
|---|
| 486 | i = 0; |
|---|
| 487 | } |
|---|
| 488 | |
|---|
| 489 | if(screen_list->recurrent_list->recurrent) free(screen_list->recurrent_list->recurrent); |
|---|
| 490 | if(screen_list->recurrent_list) free(screen_list->recurrent_list); |
|---|
| 491 | |
|---|
| 492 | if(screen_list->session_name) |
|---|
| 493 | free(screen_list->session_name); |
|---|
| 494 | |
|---|
| 495 | if(screen_list) |
|---|
| 496 | free(screen_list); |
|---|
| 497 | |
|---|
| 498 | return mainret; |
|---|
| 499 | } |
|---|
| 500 | |
|---|
| 501 | |
|---|
| 502 | |
|---|
| 503 | |
|---|
| 504 | struct screen_list *create_screen_list(void) |
|---|
| 505 | { |
|---|
| 506 | |
|---|
| 507 | struct screen_list *screen_list = NULL; |
|---|
| 508 | |
|---|
| 509 | /* Create screen list */ |
|---|
| 510 | screen_list = (struct screen_list*) malloc(sizeof(struct screen_list)); |
|---|
| 511 | if(!screen_list) |
|---|
| 512 | { |
|---|
| 513 | fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); |
|---|
| 514 | return NULL; |
|---|
| 515 | } |
|---|
| 516 | screen_list->screen = (struct screen**) malloc(sizeof(sizeof(struct screen*))); |
|---|
| 517 | if(!screen_list->screen) |
|---|
| 518 | { |
|---|
| 519 | fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); |
|---|
| 520 | free(screen_list); |
|---|
| 521 | return NULL; |
|---|
| 522 | } |
|---|
| 523 | |
|---|
| 524 | screen_list->count = 0; |
|---|
| 525 | screen_list->mini = 1; |
|---|
| 526 | screen_list->help = 0; |
|---|
| 527 | screen_list->status = 1; |
|---|
| 528 | screen_list->wm_type = WM_VSPLIT; |
|---|
| 529 | screen_list->in_bell = 0; |
|---|
| 530 | screen_list->pty = screen_list->prevpty = 0; |
|---|
| 531 | screen_list->dont_update_coords = 0; |
|---|
| 532 | screen_list->screensaver_timeout = (60) * 1000000; |
|---|
| 533 | screen_list->screensaver_data = NULL; |
|---|
| 534 | screen_list->in_screensaver = 0; |
|---|
| 535 | screen_list->locked = 0; |
|---|
| 536 | screen_list->lock_offset = 0; |
|---|
| 537 | screen_list->attached = 1; |
|---|
| 538 | screen_list->socket = 0; |
|---|
| 539 | screen_list->socket_dir = NULL; |
|---|
| 540 | screen_list->socket_path = NULL; |
|---|
| 541 | screen_list->session_name = NULL; |
|---|
| 542 | screen_list->default_shell = NULL; |
|---|
| 543 | |
|---|
| 544 | screen_list->recurrent_list = NULL; |
|---|
| 545 | screen_list->cv = NULL; |
|---|
| 546 | screen_list->dp = NULL; |
|---|
| 547 | |
|---|
| 548 | memset(screen_list->lockmsg, 0, 1024); |
|---|
| 549 | memset(screen_list->lockpass, 0, 1024); |
|---|
| 550 | |
|---|
| 551 | return screen_list; |
|---|
| 552 | } |
|---|
| 553 | |
|---|
| 554 | |
|---|
| 555 | |
|---|
| 556 | long long get_us(void) |
|---|
| 557 | { |
|---|
| 558 | struct timeval tv; |
|---|
| 559 | gettimeofday(&tv, NULL); |
|---|
| 560 | return (tv.tv_sec*(1000000) + tv.tv_usec); |
|---|
| 561 | } |
|---|