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