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.
 
 
 
 
 
 

155 lines
3.8 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. image = cucul_create_canvas(0, 0);
  49. if(cucul_import_file(image, argv[file], "ansi") < 0)
  50. {
  51. fprintf(stderr, "%s: invalid file `%s'.\n", argv[0], argv[1]);
  52. return 1;
  53. }
  54. ih = cucul_get_canvas_height(image);
  55. iw = cucul_get_canvas_width(image);
  56. x = y = 0;
  57. caca_set_display_title(dp, argv[file]);
  58. refresh = 1;
  59. }
  60. if(refresh)
  61. {
  62. refresh_screen();
  63. refresh = 0;
  64. }
  65. while(caca_get_event(dp, CACA_EVENT_ANY, &ev, -1))
  66. {
  67. switch(ev.type)
  68. {
  69. case CACA_EVENT_QUIT:
  70. goto quit;
  71. case CACA_EVENT_KEY_PRESS:
  72. switch(ev.data.key.ch)
  73. {
  74. case CACA_KEY_LEFT: dx -= 2; break;
  75. case CACA_KEY_RIGHT: dx += 2; break;
  76. case CACA_KEY_UP: dy--; break;
  77. case CACA_KEY_DOWN: dy++; break;
  78. case CACA_KEY_PAGEUP: dy -= 12; break;
  79. case CACA_KEY_PAGEDOWN: dy += 12; break;
  80. case CACA_KEY_ESCAPE:
  81. case 'q':
  82. goto quit;
  83. case 'n':
  84. file = file + 1 < argc ? file + 1 : 1;
  85. cucul_free_canvas(image);
  86. image = NULL;
  87. goto stopevents;
  88. case 'p':
  89. file = file > 1 ? file - 1 : argc - 1;
  90. cucul_free_canvas(image);
  91. image = NULL;
  92. goto stopevents;
  93. default:
  94. break;
  95. }
  96. case CACA_EVENT_RESIZE:
  97. refresh = 1;
  98. goto stopevents;
  99. default:
  100. break;
  101. }
  102. }
  103. stopevents:
  104. w = cucul_get_canvas_width(cv);
  105. h = cucul_get_canvas_height(cv);
  106. if(dx | dy)
  107. {
  108. refresh = 1;
  109. x += dx;
  110. y += dy;
  111. if(x < 0) x = 0; else if(x + w > iw) x = iw > w ? iw - w : 0;
  112. if(y < 0) y = 0; else if(y + h > ih) y = ih > h ? ih - h : 0;
  113. }
  114. }
  115. quit:
  116. /* Clean up */
  117. caca_free_display(dp);
  118. cucul_free_canvas(image);
  119. cucul_free_canvas(cv);
  120. return 0;
  121. }
  122. static int refresh_screen(void)
  123. {
  124. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_DEFAULT);
  125. cucul_clear_canvas(cv);
  126. cucul_blit(cv, - x, - y, image, NULL);
  127. caca_refresh_display(dp);
  128. return 0;
  129. }