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.
 
 
 
 
 
 

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