No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

swallow.c 3.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * swallow swallow another libcaca application
  3. * Copyright (c) 2006-2009 Sam Hocevar <sam@hocevar.net>
  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_canvas_from_memory(app, buf[i],
  77. total[i], "caca");
  78. if(bytes[i] > 0)
  79. {
  80. total[i] -= bytes[i];
  81. memmove(buf[i], buf[i] + bytes[i], total[i]);
  82. caca_blit(cv, (w + 2) * (i / 2) + 1,
  83. (h + 2) * (i % 2) + 2, app, NULL);
  84. caca_refresh_display(dp);
  85. }
  86. else if(bytes[i] == 0)
  87. {
  88. buf[i] = realloc(buf[i], total[i] + 128);
  89. fread(buf[i] + total[i], 128, 1, f[i]);
  90. total[i] += 128;
  91. }
  92. else
  93. {
  94. fprintf(stderr, "%s: corrupted input %i\n", argv[0], i);
  95. return -1;
  96. }
  97. }
  98. }
  99. /* Clean up */
  100. caca_free_display(dp);
  101. caca_free_canvas(cv);
  102. caca_free_canvas(app);
  103. return 0;
  104. }