Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

152 Zeilen
3.8 KiB

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