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.
 
 
 
 
 
 

164 line
4.0 KiB

  1. /*
  2. * event event lister for libcaca
  3. * Copyright (c) 2004 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. #include "config.h"
  14. #include "common.h"
  15. #if !defined(__KERNEL__)
  16. # include <stdio.h>
  17. # include <string.h>
  18. # include <stdlib.h>
  19. #endif
  20. #include "cucul.h"
  21. #include "caca.h"
  22. static int refresh_screen(void);
  23. static cucul_canvas_t *cv, *image;
  24. static caca_display_t *dp;
  25. static int x = 0, y = 0;
  26. int main(int argc, char **argv)
  27. {
  28. int refresh = 1, file = 1;
  29. unsigned int iw = 0, ih = 0;
  30. if(argc < 2)
  31. {
  32. fprintf(stderr, "%s: missing argument (filename).\n", argv[0]);
  33. return 1;
  34. }
  35. cv = cucul_create_canvas(0, 0);
  36. if(!cv)
  37. return 1;
  38. dp = caca_create_display(cv);
  39. if(!dp)
  40. return 1;
  41. for(;;)
  42. {
  43. caca_event_t ev;
  44. unsigned int w, h;
  45. int dx = 0, dy = 0;
  46. if(!image)
  47. {
  48. cucul_buffer_t *b = cucul_load_file(argv[file]);
  49. if(!b)
  50. {
  51. fprintf(stderr, "%s: could not open `%s'.\n", argv[0], argv[1]);
  52. return 1;
  53. }
  54. image = cucul_import_canvas(b, "ansi");
  55. if(!image)
  56. {
  57. fprintf(stderr, "%s: invalid file `%s'.\n", argv[0], argv[1]);
  58. return 1;
  59. }
  60. cucul_free_buffer(b);
  61. ih = cucul_get_canvas_height(image);
  62. iw = cucul_get_canvas_width(image);
  63. x = y = 0;
  64. caca_set_display_title(dp, argv[file]);
  65. refresh = 1;
  66. }
  67. if(refresh)
  68. {
  69. refresh_screen();
  70. refresh = 0;
  71. }
  72. while(caca_get_event(dp, CACA_EVENT_ANY, &ev, -1))
  73. {
  74. switch(ev.type)
  75. {
  76. case CACA_EVENT_QUIT:
  77. goto quit;
  78. case CACA_EVENT_KEY_PRESS:
  79. switch(ev.data.key.ch)
  80. {
  81. case CACA_KEY_LEFT: dx -= 2; break;
  82. case CACA_KEY_RIGHT: dx += 2; break;
  83. case CACA_KEY_UP: dy--; break;
  84. case CACA_KEY_DOWN: dy++; break;
  85. case CACA_KEY_PAGEUP: dy -= 12; break;
  86. case CACA_KEY_PAGEDOWN: dy += 12; break;
  87. case CACA_KEY_ESCAPE:
  88. case 'q':
  89. goto quit;
  90. case 'n':
  91. file = file + 1 < argc ? file + 1 : 1;
  92. cucul_free_canvas(image);
  93. image = NULL;
  94. goto stopevents;
  95. case 'p':
  96. file = file > 1 ? file - 1 : argc - 1;
  97. cucul_free_canvas(image);
  98. image = NULL;
  99. goto stopevents;
  100. default:
  101. break;
  102. }
  103. case CACA_EVENT_RESIZE:
  104. refresh = 1;
  105. goto stopevents;
  106. default:
  107. break;
  108. }
  109. }
  110. stopevents:
  111. w = cucul_get_canvas_width(cv);
  112. h = cucul_get_canvas_height(cv);
  113. if(dx | dy)
  114. {
  115. refresh = 1;
  116. x += dx;
  117. y += dy;
  118. if(x < 0) x = 0; else if(x + w > iw) x = iw > w ? iw - w : 0;
  119. if(y < 0) y = 0; else if(y + h > ih) y = ih > h ? ih - h : 0;
  120. }
  121. }
  122. quit:
  123. /* Clean up */
  124. caca_free_display(dp);
  125. cucul_free_canvas(image);
  126. cucul_free_canvas(cv);
  127. return 0;
  128. }
  129. static int refresh_screen(void)
  130. {
  131. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_DEFAULT);
  132. cucul_clear_canvas(cv);
  133. cucul_blit(cv, - x, - y, image, NULL);
  134. caca_refresh_display(dp);
  135. return 0;
  136. }