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.
 
 
 
 
 
 

154 lines
3.9 KiB

  1. /*
  2. * event event lister for libcaca
  3. * Copyright (c) 2004-2009 Sam Hocevar <sam@hocevar.net>
  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 <string.h>
  18. # include <stdlib.h>
  19. #endif
  20. #include "caca.h"
  21. static int refresh_screen(void);
  22. static caca_canvas_t *cv, *image;
  23. static caca_display_t *dp;
  24. static int x = 0, y = 0;
  25. int main(int argc, char **argv)
  26. {
  27. int refresh = 1, file = 1;
  28. unsigned int iw = 0, ih = 0;
  29. if(argc < 2)
  30. {
  31. fprintf(stderr, "%s: missing argument (filename).\n", argv[0]);
  32. return 1;
  33. }
  34. cv = caca_create_canvas(0, 0);
  35. if(!cv)
  36. return 1;
  37. dp = caca_create_display(cv);
  38. if(!dp)
  39. return 1;
  40. for(;;)
  41. {
  42. caca_event_t ev;
  43. unsigned int w, h;
  44. int dx = 0, dy = 0;
  45. if(!image)
  46. {
  47. image = caca_create_canvas(0, 0);
  48. if(caca_import_canvas_from_file(image, argv[file], "ansi") < 0)
  49. {
  50. fprintf(stderr, "%s: invalid file `%s'.\n", argv[0], argv[1]);
  51. return 1;
  52. }
  53. ih = caca_get_canvas_height(image);
  54. iw = caca_get_canvas_width(image);
  55. x = y = 0;
  56. caca_set_display_title(dp, argv[file]);
  57. refresh = 1;
  58. }
  59. if(refresh)
  60. {
  61. refresh_screen();
  62. refresh = 0;
  63. }
  64. while(caca_get_event(dp, CACA_EVENT_ANY, &ev, -1))
  65. {
  66. switch(caca_get_event_type(&ev))
  67. {
  68. case CACA_EVENT_QUIT:
  69. goto quit;
  70. case CACA_EVENT_KEY_PRESS:
  71. switch(caca_get_event_key_ch(&ev))
  72. {
  73. case CACA_KEY_LEFT: dx -= 2; break;
  74. case CACA_KEY_RIGHT: dx += 2; break;
  75. case CACA_KEY_UP: dy--; break;
  76. case CACA_KEY_DOWN: dy++; break;
  77. case CACA_KEY_PAGEUP: dy -= 12; break;
  78. case CACA_KEY_PAGEDOWN: dy += 12; break;
  79. case CACA_KEY_ESCAPE:
  80. case 'q':
  81. goto quit;
  82. case 'n':
  83. file = file + 1 < argc ? file + 1 : 1;
  84. caca_free_canvas(image);
  85. image = NULL;
  86. goto stopevents;
  87. case 'p':
  88. file = file > 1 ? file - 1 : argc - 1;
  89. caca_free_canvas(image);
  90. image = NULL;
  91. goto stopevents;
  92. default:
  93. break;
  94. }
  95. case CACA_EVENT_RESIZE:
  96. refresh = 1;
  97. goto stopevents;
  98. default:
  99. break;
  100. }
  101. }
  102. stopevents:
  103. w = caca_get_canvas_width(cv);
  104. h = caca_get_canvas_height(cv);
  105. if(dx | dy)
  106. {
  107. refresh = 1;
  108. x += dx;
  109. y += dy;
  110. if(x < 0) x = 0; else if(x + w > iw) x = iw > w ? iw - w : 0;
  111. if(y < 0) y = 0; else if(y + h > ih) y = ih > h ? ih - h : 0;
  112. }
  113. }
  114. quit:
  115. /* Clean up */
  116. caca_free_display(dp);
  117. caca_free_canvas(image);
  118. caca_free_canvas(cv);
  119. return 0;
  120. }
  121. static int refresh_screen(void)
  122. {
  123. caca_set_color_ansi(cv, CACA_DEFAULT, CACA_DEFAULT);
  124. caca_clear_canvas(cv);
  125. caca_blit(cv, - x, - y, image, NULL);
  126. caca_refresh_display(dp);
  127. return 0;
  128. }