Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

123 wiersze
3.1 KiB

  1. /*
  2. * swallow swallow another libcaca application
  3. * Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. #include "config.h"
  15. #if !defined(__KERNEL__)
  16. # include <stdio.h>
  17. # include <string.h>
  18. # include <stdlib.h>
  19. #endif
  20. #include "caca.h"
  21. int main(int argc, char **argv)
  22. {
  23. char cmd[BUFSIZ];
  24. static caca_canvas_t *cv, *app;
  25. static caca_display_t *dp;
  26. uint8_t *buf[4];
  27. long int bytes[4], total[4];
  28. FILE *f[4];
  29. int w, h, i;
  30. if(argc < 5)
  31. {
  32. fprintf(stderr, "usage: %s <cmd1> <cmd2> <cmd3> <cmd4>\n", argv[0]);
  33. return 1;
  34. }
  35. cv = caca_create_canvas(0, 0);
  36. app = caca_create_canvas(0, 0);
  37. dp = caca_create_display(cv);
  38. if(cv == NULL || app == NULL )
  39. {
  40. printf("Can't created canvas\n");
  41. return -1;
  42. }
  43. if(dp == NULL)
  44. {
  45. printf("Can't create display\n");
  46. return -1;
  47. }
  48. w = (caca_get_canvas_width(cv) - 4) / 2;
  49. h = (caca_get_canvas_height(cv) - 6) / 2;
  50. if(w < 0 || h < 0)
  51. return 1;
  52. caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE);
  53. caca_draw_line(cv, 0, 0, caca_get_canvas_width(cv) - 1, 0, ' ');
  54. caca_printf(cv, caca_get_canvas_width(cv) / 2 - 10, 0,
  55. "libcaca multiplexer");
  56. for(i = 0; i < 4; i++)
  57. {
  58. buf[i] = NULL;
  59. total[i] = bytes[i] = 0;
  60. sprintf(cmd, "CACA_DRIVER=raw CACA_GEOMETRY=%ix%i %s",
  61. w, h, argv[i + 1]);
  62. f[i] = popen(cmd, "r");
  63. if(!f[i])
  64. return 1;
  65. caca_printf(cv, (w + 2) * (i / 2) + 1,
  66. (h + 2) * ((i % 2) + 1), "%s", argv[i + 1]);
  67. }
  68. for(;;)
  69. {
  70. caca_event_t ev;
  71. int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
  72. if(ret && caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS)
  73. break;
  74. for(i = 0; i < 4; i++)
  75. {
  76. bytes[i] = caca_import_memory(app, buf[i], total[i], "caca");
  77. if(bytes[i] > 0)
  78. {
  79. total[i] -= bytes[i];
  80. memmove(buf[i], buf[i] + bytes[i], total[i]);
  81. caca_blit(cv, (w + 2) * (i / 2) + 1,
  82. (h + 2) * (i % 2) + 2, app, NULL);
  83. caca_refresh_display(dp);
  84. }
  85. else if(bytes[i] == 0)
  86. {
  87. buf[i] = realloc(buf[i], total[i] + 128);
  88. fread(buf[i] + total[i], 128, 1, f[i]);
  89. total[i] += 128;
  90. }
  91. else
  92. {
  93. fprintf(stderr, "%s: corrupted input %i\n", argv[0], i);
  94. return -1;
  95. }
  96. }
  97. }
  98. /* Clean up */
  99. caca_free_display(dp);
  100. caca_free_canvas(cv);
  101. caca_free_canvas(app);
  102. return 0;
  103. }