Debian Codegraph @

src/cacadraw.c

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