選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

115 行
2.9 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 commes 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(!dp)
  41. return 1;
  42. w = (cucul_get_canvas_width(cv) - 4) / 2;
  43. h = (cucul_get_canvas_height(cv) - 6) / 2;
  44. if(w < 0 || h < 0)
  45. return 1;
  46. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
  47. cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, ' ');
  48. cucul_printf(cv, cucul_get_canvas_width(cv) / 2 - 10, 0,
  49. "libcaca multiplexer");
  50. for(i = 0; i < 4; i++)
  51. {
  52. buf[i] = NULL;
  53. total[i] = bytes[i] = 0;
  54. sprintf(cmd, "CACA_DRIVER=raw CACA_GEOMETRY=%ix%i %s",
  55. w, h, argv[i + 1]);
  56. f[i] = popen(cmd, "r");
  57. if(!f[i])
  58. return 1;
  59. cucul_printf(cv, (w + 2) * (i / 2) + 1,
  60. (h + 2) * ((i % 2) + 1), "%s", argv[i + 1]);
  61. }
  62. for(;;)
  63. {
  64. caca_event_t ev;
  65. int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
  66. if(ret && ev.type & CACA_EVENT_KEY_PRESS)
  67. break;
  68. for(i = 0; i < 4; i++)
  69. {
  70. bytes[i] = cucul_import_memory(app, buf[i], total[i], "caca");
  71. if(bytes[i] > 0)
  72. {
  73. total[i] -= bytes[i];
  74. memmove(buf[i], buf[i] + bytes[i], total[i]);
  75. cucul_blit(cv, (w + 2) * (i / 2) + 1,
  76. (h + 2) * (i % 2) + 2, app, NULL);
  77. caca_refresh_display(dp);
  78. }
  79. else if(bytes[i] == 0)
  80. {
  81. buf[i] = realloc(buf[i], total[i] + 128);
  82. fread(buf[i] + total[i], 128, 1, f[i]);
  83. total[i] += 128;
  84. }
  85. else
  86. {
  87. fprintf(stderr, "%s: corrupted input %i\n", argv[0], i);
  88. return -1;
  89. }
  90. }
  91. }
  92. /* Clean up */
  93. caca_free_display(dp);
  94. cucul_free_canvas(cv);
  95. cucul_free_canvas(app);
  96. return 0;
  97. }