You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

124 lines
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. #include "common.h"
  16. #if !defined(__KERNEL__)
  17. # include <stdio.h>
  18. # include <string.h>
  19. # include <stdlib.h>
  20. #endif
  21. #include "cucul.h"
  22. #include "caca.h"
  23. int main(int argc, char **argv)
  24. {
  25. char cmd[BUFSIZ];
  26. static cucul_canvas_t *cv, *app;
  27. static caca_display_t *dp;
  28. unsigned char *buf[4];
  29. long int bytes[4], total[4];
  30. FILE *f[4];
  31. int w, h, i;
  32. if(argc < 5)
  33. {
  34. fprintf(stderr, "usage: %s <cmd1> <cmd2> <cmd3> <cmd4>\n", argv[0]);
  35. return 1;
  36. }
  37. cv = cucul_create_canvas(0, 0);
  38. app = cucul_create_canvas(0, 0);
  39. dp = caca_create_display(cv);
  40. if(cv == NULL || app == NULL )
  41. {
  42. printf("Can't created canvas\n");
  43. return -1;
  44. }
  45. if(dp == NULL)
  46. {
  47. printf("Can't create display\n");
  48. return -1;
  49. }
  50. w = (cucul_get_canvas_width(cv) - 4) / 2;
  51. h = (cucul_get_canvas_height(cv) - 6) / 2;
  52. if(w < 0 || h < 0)
  53. return 1;
  54. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
  55. cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, ' ');
  56. cucul_printf(cv, cucul_get_canvas_width(cv) / 2 - 10, 0,
  57. "libcaca multiplexer");
  58. for(i = 0; i < 4; i++)
  59. {
  60. buf[i] = NULL;
  61. total[i] = bytes[i] = 0;
  62. sprintf(cmd, "CACA_DRIVER=raw CACA_GEOMETRY=%ix%i %s",
  63. w, h, argv[i + 1]);
  64. f[i] = popen(cmd, "r");
  65. if(!f[i])
  66. return 1;
  67. cucul_printf(cv, (w + 2) * (i / 2) + 1,
  68. (h + 2) * ((i % 2) + 1), "%s", argv[i + 1]);
  69. }
  70. for(;;)
  71. {
  72. caca_event_t ev;
  73. int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
  74. if(ret && caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS)
  75. break;
  76. for(i = 0; i < 4; i++)
  77. {
  78. bytes[i] = cucul_import_memory(app, buf[i], total[i], "caca");
  79. if(bytes[i] > 0)
  80. {
  81. total[i] -= bytes[i];
  82. memmove(buf[i], buf[i] + bytes[i], total[i]);
  83. cucul_blit(cv, (w + 2) * (i / 2) + 1,
  84. (h + 2) * (i % 2) + 2, app, NULL);
  85. caca_refresh_display(dp);
  86. }
  87. else if(bytes[i] == 0)
  88. {
  89. buf[i] = realloc(buf[i], total[i] + 128);
  90. fread(buf[i] + total[i], 128, 1, f[i]);
  91. total[i] += 128;
  92. }
  93. else
  94. {
  95. fprintf(stderr, "%s: corrupted input %i\n", argv[0], i);
  96. return -1;
  97. }
  98. }
  99. }
  100. /* Clean up */
  101. caca_free_display(dp);
  102. cucul_free_canvas(cv);
  103. cucul_free_canvas(app);
  104. return 0;
  105. }