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.
 
 
 
 
 
 

162 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. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include "cucul.h"
  19. #include "caca.h"
  20. static int refresh_screen(void);
  21. static cucul_canvas_t *cv, *image;
  22. static caca_display_t *dp;
  23. static int x = 0, y = 0;
  24. int main(int argc, char **argv)
  25. {
  26. int refresh = 1, file = 1;
  27. unsigned int iw = 0, ih = 0;
  28. if(argc < 2)
  29. {
  30. fprintf(stderr, "%s: missing argument (filename).\n", argv[0]);
  31. return 1;
  32. }
  33. cv = cucul_create_canvas(0, 0);
  34. if(!cv)
  35. return 1;
  36. dp = caca_create_display(cv);
  37. if(!dp)
  38. return 1;
  39. for(;;)
  40. {
  41. caca_event_t ev;
  42. unsigned int w, h;
  43. int dx = 0, dy = 0;
  44. if(!image)
  45. {
  46. cucul_buffer_t *b = cucul_load_file(argv[file]);
  47. if(!b)
  48. {
  49. fprintf(stderr, "%s: could not open `%s'.\n", argv[0], argv[1]);
  50. return 1;
  51. }
  52. image = cucul_import_canvas(b, "ansi");
  53. if(!image)
  54. {
  55. fprintf(stderr, "%s: invalid file `%s'.\n", argv[0], argv[1]);
  56. return 1;
  57. }
  58. cucul_free_buffer(b);
  59. ih = cucul_get_canvas_height(image);
  60. iw = cucul_get_canvas_width(image);
  61. x = y = 0;
  62. caca_set_display_title(dp, argv[file]);
  63. refresh = 1;
  64. }
  65. if(refresh)
  66. {
  67. refresh_screen();
  68. refresh = 0;
  69. }
  70. while(caca_get_event(dp, CACA_EVENT_ANY, &ev, -1))
  71. {
  72. switch(ev.type)
  73. {
  74. case CACA_EVENT_QUIT:
  75. goto quit;
  76. case CACA_EVENT_KEY_PRESS:
  77. switch(ev.data.key.ch)
  78. {
  79. case CACA_KEY_LEFT: dx -= 2; break;
  80. case CACA_KEY_RIGHT: dx += 2; break;
  81. case CACA_KEY_UP: dy--; break;
  82. case CACA_KEY_DOWN: dy++; break;
  83. case CACA_KEY_PAGEUP: dy -= 12; break;
  84. case CACA_KEY_PAGEDOWN: dy += 12; break;
  85. case CACA_KEY_ESCAPE:
  86. case 'q':
  87. goto quit;
  88. case 'n':
  89. file = file + 1 < argc ? file + 1 : 1;
  90. cucul_free_canvas(image);
  91. image = NULL;
  92. goto stopevents;
  93. case 'p':
  94. file = file > 1 ? file - 1 : argc - 1;
  95. cucul_free_canvas(image);
  96. image = NULL;
  97. goto stopevents;
  98. default:
  99. break;
  100. }
  101. case CACA_EVENT_RESIZE:
  102. refresh = 1;
  103. goto stopevents;
  104. default:
  105. break;
  106. }
  107. }
  108. stopevents:
  109. w = cucul_get_canvas_width(cv);
  110. h = cucul_get_canvas_height(cv);
  111. if(dx | dy)
  112. {
  113. refresh = 1;
  114. x += dx;
  115. y += dy;
  116. if(x < 0) x = 0; else if(x + w > iw) x = iw > w ? iw - w : 0;
  117. if(y < 0) y = 0; else if(y + h > ih) y = ih > h ? ih - h : 0;
  118. }
  119. }
  120. quit:
  121. /* Clean up */
  122. caca_free_display(dp);
  123. cucul_free_canvas(image);
  124. cucul_free_canvas(cv);
  125. return 0;
  126. }
  127. static int refresh_screen(void)
  128. {
  129. cucul_set_color(cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_DEFAULT);
  130. cucul_clear_canvas(cv);
  131. cucul_blit(cv, - x, - y, image, NULL);
  132. caca_refresh_display(dp);
  133. return 0;
  134. }