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.
 
 
 
 
 
 

115 lines
2.4 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 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 <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. dp = caca_create_display(cv);
  50. for(;;)
  51. {
  52. caca_event_t ev;
  53. int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
  54. int eof = 0;
  55. if(ret && ev.type & CACA_EVENT_KEY_PRESS)
  56. break;
  57. if(bytes == 0)
  58. {
  59. ssize_t n;
  60. buf = realloc(buf, total + 1);
  61. n = read(fd, buf + total, 1);
  62. if(n < 0)
  63. {
  64. fprintf(stderr, "%s: read error\n", argv[0]);
  65. return -1;
  66. }
  67. else if(n == 0)
  68. {
  69. eof = 1;
  70. }
  71. total += n;
  72. }
  73. bytes = cucul_import_memory(app, buf, total, "caca");
  74. if(bytes > 0)
  75. {
  76. total -= bytes;
  77. memmove(buf, buf + bytes, total);
  78. cucul_blit(cv, 0, 0, app, NULL);
  79. caca_refresh_display(dp);
  80. }
  81. else if(bytes < 0)
  82. {
  83. fprintf(stderr, "%s: corrupted caca file\n", argv[0]);
  84. break;
  85. }
  86. if(eof)
  87. break;
  88. }
  89. caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);
  90. /* Clean up */
  91. close(fd);
  92. caca_free_display(dp);
  93. cucul_free_canvas(cv);
  94. return 0;
  95. }