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

124 行
2.6 KiB

  1. /*
  2. * cacaplay caca file player
  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. #if !defined(__KERNEL__)
  16. # include <stdio.h>
  17. # include <stdlib.h>
  18. # include <sys/types.h>
  19. # include <sys/stat.h>
  20. # include <fcntl.h>
  21. # include <string.h>
  22. # include <unistd.h>
  23. #endif
  24. #include "cucul.h"
  25. #include "caca.h"
  26. int main(int argc, char **argv)
  27. {
  28. cucul_canvas_t *cv, *app;
  29. caca_display_t *dp;
  30. uint8_t *buf = NULL;
  31. long int bytes = 0, total = 0;
  32. int fd;
  33. if(argc < 2 || !strcmp(argv[1], "-"))
  34. {
  35. fd = 0; /* use stdin */
  36. }
  37. else
  38. {
  39. fd = open(argv[1], O_RDONLY);
  40. if(fd < 0)
  41. {
  42. fprintf(stderr, "%s: could not open `%s'.\n", argv[0], argv[1]);
  43. return 1;
  44. }
  45. }
  46. cv = cucul_create_canvas(0, 0);
  47. app = cucul_create_canvas(0, 0);
  48. if(cv == NULL || app == NULL)
  49. {
  50. printf("Can't created canvas\n");
  51. return -1;
  52. }
  53. dp = caca_create_display(cv);
  54. if(dp == NULL)
  55. {
  56. printf("Can't create display\n");
  57. return -1;
  58. }
  59. for(;;)
  60. {
  61. caca_event_t ev;
  62. int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
  63. int eof = 0;
  64. if(ret && caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS)
  65. break;
  66. if(bytes == 0)
  67. {
  68. ssize_t n;
  69. buf = realloc(buf, total + 1);
  70. n = read(fd, buf + total, 1);
  71. if(n < 0)
  72. {
  73. fprintf(stderr, "%s: read error\n", argv[0]);
  74. return -1;
  75. }
  76. else if(n == 0)
  77. {
  78. eof = 1;
  79. }
  80. total += n;
  81. }
  82. bytes = cucul_import_memory(app, buf, total, "caca");
  83. if(bytes > 0)
  84. {
  85. total -= bytes;
  86. memmove(buf, buf + bytes, total);
  87. cucul_blit(cv, 0, 0, app, NULL);
  88. caca_refresh_display(dp);
  89. }
  90. else if(bytes < 0)
  91. {
  92. fprintf(stderr, "%s: corrupted caca file\n", argv[0]);
  93. break;
  94. }
  95. if(eof)
  96. break;
  97. }
  98. caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);
  99. /* Clean up */
  100. close(fd);
  101. caca_free_display(dp);
  102. cucul_free_canvas(cv);
  103. return 0;
  104. }