Changeset 2359 for neercs/trunk/src/main.c
- Timestamp:
- Jun 11, 2008, 6:00:26 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
neercs/trunk/src/main.c
r2358 r2359 297 297 if(mini) 298 298 { 299 draw_thumbnails(cv, screen_list, pty); 300 } 301 302 caca_refresh_display(dp); 303 } 304 305 eof = 1; 306 for(i = 0; i < screen_list->count; i++) 307 if(screen_list->screen[i]->fd >= 0) 308 eof = 0; 309 if(eof) 310 break; 311 } 312 313 /* Clean up */ 314 caca_free_display(dp); 315 cucul_free_canvas(cv); 316 for(i = 0; i < screen_list->count; i++) 317 { 318 destroy_screen(screen_list->screen[i]); 319 } 320 free(screen_list->screen); 321 free(screen_list); 322 323 324 return 0; 325 } 326 327 static int create_pty(char *cmd, unsigned int w, unsigned int h) 328 { 329 char **argv; 330 int fd; 331 pid_t pid; 332 333 pid = forkpty(&fd, NULL, NULL, NULL); 334 if(pid < 0) 335 { 336 fprintf(stderr, "forkpty() error\n"); 337 return -1; 338 } 339 else if(pid == 0) 340 { 341 set_tty_size(0, w, h); 342 putenv("CACA_DRIVER=slang"); 343 putenv("TERM=xterm"); 344 argv = malloc(2 * sizeof(char *)); 345 argv[0] = cmd; 346 argv[1] = NULL; 347 execvp(cmd, argv); 348 fprintf(stderr, "execvp() error\n"); 349 return -1; 350 } 351 352 fcntl(fd, F_SETFL, O_NDELAY); 353 return fd; 354 #if 0 355 fprintf(stderr, "forkpty() not available\n"); 356 return -1; 357 #endif 358 } 359 360 static int set_tty_size(int fd, unsigned int w, unsigned int h) 361 { 362 struct winsize ws; 363 364 memset(&ws, 0, sizeof(ws)); 365 ws.ws_row = h; 366 ws.ws_col = w; 367 ioctl(fd, TIOCSWINSZ, (char *)&ws); 368 369 return 0; 370 } 371 372 373 374 375 struct screen* create_screen(int w, int h, char *command) 376 { 377 struct screen *s = (struct screen*) malloc(sizeof(struct screen)); 378 379 s->cv = cucul_create_canvas(w, h); 380 s->init = 0; 381 382 383 s->buf = NULL; 384 s->total = 0; 385 s->fd = create_pty(command, w, h); 386 if(s->fd < 0) 387 { 388 cucul_free_canvas(s->cv); 389 free(s); 390 return NULL; 391 } 392 return s; 393 } 394 395 396 int add_screen(struct screen_list *list, struct screen *s) 397 { 398 if(list == NULL || s == NULL) return -1; 399 400 else 401 { 402 list->screen = (struct screen**) realloc(list->screen, 403 sizeof(sizeof(struct screen*)) 404 * (list->count+1)); 405 list->screen[list->count] = s; 406 list->count++; 407 } 408 409 return list->count-1; 410 } 411 412 413 int remove_screen(struct screen_list *list, int n) 414 { 415 if(n>list->count) return -1; 416 417 memmove(&list->screen[n], 418 &list->screen[n+1], 419 sizeof(struct screen*)*(list->count-(n+1))); 420 421 list->screen = (struct screen**) realloc(list->screen, 422 sizeof(sizeof(struct screen*)) 423 * (list->count)); 424 425 list->count--; 426 return 1; 427 } 428 429 int destroy_screen(struct screen *s) 430 { 431 free(s->buf); 432 cucul_free_canvas(s->cv); 433 free(s); 434 return 1; 435 } 436 437 438 439 void draw_thumbnails(cucul_canvas_t *cv, struct screen_list *screen_list, int pty) 440 { 299 441 char const * const *fonts; 300 442 cucul_dither_t *d; 301 443 cucul_font_t *f; 302 444 uint8_t *buf; 445 int i; 303 446 int miniw, minih; 304 447 … … 334 477 cucul_get_canvas_height(cv) - 6, 19, 6, d, buf); 335 478 cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE); 479 480 if(pty == i) 481 cucul_draw_cp437_box(cv,20 * i, 482 cucul_get_canvas_height(cv) - 6, 19, 6); 336 483 cucul_printf(cv, 20 * i, 337 484 cucul_get_canvas_height(cv) - 6, "(%i)", i); … … 343 490 free(buf); 344 491 } 345 } 346 347 caca_refresh_display(dp); 348 } 349 350 eof = 1; 351 for(i = 0; i < screen_list->count; i++) 352 if(screen_list->screen[i]->fd >= 0) 353 eof = 0; 354 if(eof) 355 break; 356 } 357 358 /* Clean up */ 359 caca_free_display(dp); 360 cucul_free_canvas(cv); 361 for(i = 0; i < screen_list->count; i++) 362 { 363 destroy_screen(screen_list->screen[i]); 364 } 365 free(screen_list->screen); 366 free(screen_list); 367 368 369 return 0; 370 } 371 372 static int create_pty(char *cmd, unsigned int w, unsigned int h) 373 { 374 char **argv; 375 int fd; 376 pid_t pid; 377 378 pid = forkpty(&fd, NULL, NULL, NULL); 379 if(pid < 0) 380 { 381 fprintf(stderr, "forkpty() error\n"); 382 return -1; 383 } 384 else if(pid == 0) 385 { 386 set_tty_size(0, w, h); 387 putenv("CACA_DRIVER=slang"); 388 putenv("TERM=xterm"); 389 argv = malloc(2 * sizeof(char *)); 390 argv[0] = cmd; 391 argv[1] = NULL; 392 execvp(cmd, argv); 393 fprintf(stderr, "execvp() error\n"); 394 return -1; 395 } 396 397 fcntl(fd, F_SETFL, O_NDELAY); 398 return fd; 399 #if 0 400 fprintf(stderr, "forkpty() not available\n"); 401 return -1; 402 #endif 403 } 404 405 static int set_tty_size(int fd, unsigned int w, unsigned int h) 406 { 407 struct winsize ws; 408 409 memset(&ws, 0, sizeof(ws)); 410 ws.ws_row = h; 411 ws.ws_col = w; 412 ioctl(fd, TIOCSWINSZ, (char *)&ws); 413 414 return 0; 415 } 416 417 418 419 420 struct screen* create_screen(int w, int h, char *command) 421 { 422 struct screen *s = (struct screen*) malloc(sizeof(struct screen)); 423 424 s->cv = cucul_create_canvas(w, h); 425 s->init = 0; 426 427 s->buf = NULL; 428 s->total = 0; 429 s->fd = create_pty(command, w, h); 430 if(s->fd < 0) 431 { 432 cucul_free_canvas(s->cv); 433 free(s); 434 return NULL; 435 } 436 return s; 437 } 438 439 440 int add_screen(struct screen_list *list, struct screen *s) 441 { 442 if(list == NULL || s == NULL) 443 { 444 return -1; 445 } 446 else 447 { 448 list->screen = (struct screen**) realloc(list->screen, 449 sizeof(sizeof(struct screen*)) 450 * (list->count+1)); 451 list->screen[list->count] = s; 452 list->count++; 453 } 454 455 return list->count-1; 456 } 457 458 459 int remove_screen(struct screen_list *list, int n) 460 { 461 if(n>list->count) return -1; 462 463 memmove(&list->screen[n], 464 &list->screen[n+1], 465 sizeof(struct screen*)*(list->count-(n+1))); 466 467 list->screen = (struct screen**) realloc(list->screen, 468 sizeof(sizeof(struct screen*)) 469 * (list->count)); 470 471 list->count--; 472 return 1; 473 } 474 475 int destroy_screen(struct screen *s) 476 { 477 free(s->buf); 478 cucul_free_canvas(s->cv); 479 free(s); 480 return 1; 481 } 492 493 }
Note: See TracChangeset
for help on using the changeset viewer.