您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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