1 | /* |
---|
2 | * event event lister for libcaca |
---|
3 | * Copyright (c) 2004 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: event.c 349 2004-01-13 22:33:09Z sam $ |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU Lesser General Public |
---|
10 | * License as published by the Free Software Foundation; either |
---|
11 | * version 2 of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | * Lesser General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU Lesser General Public |
---|
19 | * License along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
---|
21 | * 02111-1307 USA |
---|
22 | */ |
---|
23 | |
---|
24 | #include "config.h" |
---|
25 | |
---|
26 | #include <stdio.h> |
---|
27 | #include <string.h> |
---|
28 | #include <stdlib.h> |
---|
29 | |
---|
30 | #include "caca.h" |
---|
31 | |
---|
32 | static void print_event(int, int, unsigned int); |
---|
33 | |
---|
34 | int main(int argc, char **argv) |
---|
35 | { |
---|
36 | int *events; |
---|
37 | int i, h, quit; |
---|
38 | |
---|
39 | if(caca_init()) |
---|
40 | return 1; |
---|
41 | |
---|
42 | h = caca_get_height() - 1; |
---|
43 | |
---|
44 | caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE); |
---|
45 | caca_draw_line(0, 0, caca_get_width() - 1, 0, ' '); |
---|
46 | |
---|
47 | caca_draw_line(0, h, caca_get_width() - 1, h, ' '); |
---|
48 | caca_putstr(0, h, "type \"quit\" to exit"); |
---|
49 | |
---|
50 | caca_refresh(); |
---|
51 | |
---|
52 | events = malloc(h * sizeof(int)); |
---|
53 | memset(events, 0, h * sizeof(int)); |
---|
54 | |
---|
55 | for(quit = 0; quit < 4; ) |
---|
56 | { |
---|
57 | static char const * quit_string[] = { "", "q", "qu", "qui", "quit" }; |
---|
58 | unsigned int event = caca_wait_event(CACA_EVENT_ANY); |
---|
59 | |
---|
60 | if(!event) |
---|
61 | continue; |
---|
62 | |
---|
63 | do |
---|
64 | { |
---|
65 | /* "quit" quits */ |
---|
66 | if(event & CACA_EVENT_KEY_PRESS) |
---|
67 | { |
---|
68 | int key = event & ~CACA_EVENT_KEY_PRESS; |
---|
69 | if((key == 'q' && quit == 0) || (key == 'u' && quit == 1) |
---|
70 | || (key == 'i' && quit == 2) || (key == 't' && quit == 3)) |
---|
71 | quit++; |
---|
72 | else if(key == 'q') |
---|
73 | quit = 1; |
---|
74 | else |
---|
75 | quit = 0; |
---|
76 | } |
---|
77 | |
---|
78 | memmove(events + 1, events, (h - 1) * sizeof(int)); |
---|
79 | events[0] = event; |
---|
80 | |
---|
81 | event = caca_get_event(CACA_EVENT_ANY); |
---|
82 | } |
---|
83 | while(event); |
---|
84 | |
---|
85 | caca_clear(); |
---|
86 | |
---|
87 | /* Print current event */ |
---|
88 | caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE); |
---|
89 | caca_draw_line(0, 0, caca_get_width() - 1, 0, ' '); |
---|
90 | print_event(0, 0, events[0]); |
---|
91 | |
---|
92 | caca_draw_line(0, h, caca_get_width() - 1, h, ' '); |
---|
93 | caca_printf(0, h, "type \"quit\" to exit: %s", quit_string[quit]); |
---|
94 | |
---|
95 | /* Print previous events */ |
---|
96 | caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLACK); |
---|
97 | for(i = 1; i < h && events[i]; i++) |
---|
98 | print_event(0, i, events[i]); |
---|
99 | |
---|
100 | caca_refresh(); |
---|
101 | } |
---|
102 | |
---|
103 | /* Clean up */ |
---|
104 | caca_end(); |
---|
105 | |
---|
106 | return 0; |
---|
107 | } |
---|
108 | |
---|
109 | static void print_event(int x, int y, unsigned int event) |
---|
110 | { |
---|
111 | int character; |
---|
112 | |
---|
113 | switch(event & 0xff000000) |
---|
114 | { |
---|
115 | case CACA_EVENT_NONE: |
---|
116 | caca_printf(x, y, "CACA_EVENT_NONE"); |
---|
117 | break; |
---|
118 | case CACA_EVENT_KEY_PRESS: |
---|
119 | character = event & 0x00ffffff; |
---|
120 | caca_printf(x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%c)", character, |
---|
121 | (character > 0x20 && character < 0x80) ? character : '?'); |
---|
122 | break; |
---|
123 | case CACA_EVENT_KEY_RELEASE: |
---|
124 | character = event & 0x00ffffff; |
---|
125 | caca_printf(x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%c)", character, |
---|
126 | (character > 0x20 && character < 0x80) ? character : '?'); |
---|
127 | break; |
---|
128 | case CACA_EVENT_MOUSE_MOTION: |
---|
129 | caca_printf(x, y, "CACA_EVENT_MOUSE_MOTION %u %u", |
---|
130 | (event & 0x00fff000) >> 12, event & 0x00000fff); |
---|
131 | break; |
---|
132 | case CACA_EVENT_MOUSE_PRESS: |
---|
133 | caca_printf(x, y, "CACA_EVENT_MOUSE_PRESS %u", |
---|
134 | event & 0x00ffffff); |
---|
135 | break; |
---|
136 | case CACA_EVENT_MOUSE_RELEASE: |
---|
137 | caca_printf(x, y, "CACA_EVENT_MOUSE_RELEASE %u", |
---|
138 | event & 0x00ffffff); |
---|
139 | break; |
---|
140 | case CACA_EVENT_RESIZE: |
---|
141 | caca_printf(x, y, "CACA_EVENT_RESIZE"); |
---|
142 | break; |
---|
143 | default: |
---|
144 | caca_printf(x, y, "CACA_EVENT_UNKNOWN"); |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|