您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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