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.
 
 
 
 
 
 

123 line
2.7 KiB

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