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.

swallow.c 2.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. #include "config.h"
  14. #include "common.h"
  15. #if !defined(__KERNEL__)
  16. # include <stdio.h>
  17. # include <string.h>
  18. # include <stdlib.h>
  19. #endif
  20. #include "cucul.h"
  21. #include "caca.h"
  22. int main(int argc, char **argv)
  23. {
  24. char cmd[BUFSIZ];
  25. static cucul_canvas_t *cv, *app;
  26. static caca_display_t *dp;
  27. unsigned char *buf[4];
  28. long int bytes[4], total[4];
  29. FILE *f[4];
  30. int w, h, i;
  31. if(argc < 5)
  32. {
  33. fprintf(stderr, "usage: %s <cmd1> <cmd2> <cmd3> <cmd4>\n", argv[0]);
  34. return 1;
  35. }
  36. cv = cucul_create_canvas(0, 0);
  37. app = cucul_create_canvas(0, 0);
  38. dp = caca_create_display(cv);
  39. if(!dp)
  40. return 1;
  41. w = (cucul_get_canvas_width(cv) - 4) / 2;
  42. h = (cucul_get_canvas_height(cv) - 6) / 2;
  43. if(w < 0 || h < 0)
  44. return 1;
  45. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
  46. cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, ' ');
  47. cucul_printf(cv, cucul_get_canvas_width(cv) / 2 - 10, 0,
  48. "libcaca multiplexer");
  49. for(i = 0; i < 4; i++)
  50. {
  51. buf[i] = NULL;
  52. total[i] = bytes[i] = 0;
  53. sprintf(cmd, "CACA_DRIVER=raw CACA_GEOMETRY=%ix%i %s",
  54. w, h, argv[i + 1]);
  55. f[i] = popen(cmd, "r");
  56. if(!f[i])
  57. return 1;
  58. cucul_printf(cv, (w + 2) * (i / 2) + 1,
  59. (h + 2) * ((i % 2) + 1), "%s", argv[i + 1]);
  60. }
  61. for(;;)
  62. {
  63. caca_event_t ev;
  64. int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
  65. if(ret && ev.type & CACA_EVENT_KEY_PRESS)
  66. break;
  67. for(i = 0; i < 4; i++)
  68. {
  69. bytes[i] = cucul_import_memory(app, buf[i], total[i], "caca");
  70. if(bytes[i] > 0)
  71. {
  72. total[i] -= bytes[i];
  73. memmove(buf[i], buf[i] + bytes[i], total[i]);
  74. cucul_blit(cv, (w + 2) * (i / 2) + 1,
  75. (h + 2) * (i % 2) + 2, app, NULL);
  76. caca_refresh_display(dp);
  77. }
  78. else if(bytes[i] == 0)
  79. {
  80. buf[i] = realloc(buf[i], total[i] + 128);
  81. fread(buf[i] + total[i], 128, 1, f[i]);
  82. total[i] += 128;
  83. }
  84. else
  85. {
  86. fprintf(stderr, "%s: corrupted input %i\n", argv[0], i);
  87. return -1;
  88. }
  89. }
  90. }
  91. /* Clean up */
  92. caca_free_display(dp);
  93. cucul_free_canvas(cv);
  94. cucul_free_canvas(app);
  95. return 0;
  96. }