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 2821 2008-09-27 13:12:46Z 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 | |
---|
17 | #if !defined(__KERNEL__) |
---|
18 | # include <stdio.h> |
---|
19 | # include <string.h> |
---|
20 | # include <stdlib.h> |
---|
21 | #endif |
---|
22 | |
---|
23 | #include "caca.h" |
---|
24 | |
---|
25 | static caca_canvas_t *cv; |
---|
26 | static caca_display_t *dp; |
---|
27 | |
---|
28 | static void print_event(int, int, caca_event_t *); |
---|
29 | |
---|
30 | int main(int argc, char **argv) |
---|
31 | { |
---|
32 | caca_event_t *events; |
---|
33 | int i, h, quit; |
---|
34 | |
---|
35 | cv = caca_create_canvas(80, 24); |
---|
36 | if(cv == NULL) |
---|
37 | { |
---|
38 | printf("Failed to create canvas\n"); |
---|
39 | return 1; |
---|
40 | } |
---|
41 | |
---|
42 | dp = caca_create_display(cv); |
---|
43 | if(dp == NULL) |
---|
44 | { |
---|
45 | printf("Failed to create display\n"); |
---|
46 | return 1; |
---|
47 | } |
---|
48 | |
---|
49 | h = caca_get_canvas_height(cv) - 1; |
---|
50 | |
---|
51 | caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE); |
---|
52 | caca_draw_line(cv, 0, 0, caca_get_canvas_width(cv) - 1, 0, ' '); |
---|
53 | |
---|
54 | caca_draw_line(cv, 0, h, caca_get_canvas_width(cv) - 1, h, ' '); |
---|
55 | caca_put_str(cv, 0, h, "type \"quit\" to exit"); |
---|
56 | |
---|
57 | caca_refresh_display(dp); |
---|
58 | |
---|
59 | events = malloc(h * sizeof(caca_event_t)); |
---|
60 | memset(events, 0, h * sizeof(caca_event_t)); |
---|
61 | |
---|
62 | for(quit = 0; quit < 4; ) |
---|
63 | { |
---|
64 | caca_event_t ev; |
---|
65 | static char const * quit_string[] = { "", "q", "qu", "qui", "quit" }; |
---|
66 | int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, -1); |
---|
67 | |
---|
68 | if(!ret) |
---|
69 | continue; |
---|
70 | |
---|
71 | do |
---|
72 | { |
---|
73 | /* "quit" quits */ |
---|
74 | if(caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS) |
---|
75 | { |
---|
76 | int key = caca_get_event_key_ch(&ev); |
---|
77 | if((key == 'q' && quit == 0) || (key == 'u' && quit == 1) |
---|
78 | || (key == 'i' && quit == 2) || (key == 't' && quit == 3)) |
---|
79 | quit++; |
---|
80 | else if(key == 'q') |
---|
81 | quit = 1; |
---|
82 | else |
---|
83 | quit = 0; |
---|
84 | } |
---|
85 | |
---|
86 | memmove(events + 1, events, (h - 1) * sizeof(caca_event_t)); |
---|
87 | events[0] = ev; |
---|
88 | |
---|
89 | ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0); |
---|
90 | } |
---|
91 | while(ret); |
---|
92 | |
---|
93 | caca_set_color_ansi(cv, CACA_LIGHTGRAY, CACA_BLACK); |
---|
94 | caca_clear_canvas(cv); |
---|
95 | |
---|
96 | /* Print current event */ |
---|
97 | caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE); |
---|
98 | caca_draw_line(cv, 0, 0, caca_get_canvas_width(cv) - 1, 0, ' '); |
---|
99 | print_event(0, 0, events); |
---|
100 | |
---|
101 | caca_draw_line(cv, 0, h, caca_get_canvas_width(cv) - 1, h, ' '); |
---|
102 | caca_printf(cv, 0, h, "type \"quit\" to exit: %s", quit_string[quit]); |
---|
103 | |
---|
104 | /* Print previous events */ |
---|
105 | caca_set_color_ansi(cv, CACA_WHITE, CACA_BLACK); |
---|
106 | for(i = 1; i < h && caca_get_event_type(&events[i]); i++) |
---|
107 | print_event(0, i, events + i); |
---|
108 | |
---|
109 | caca_refresh_display(dp); |
---|
110 | } |
---|
111 | |
---|
112 | /* Clean up */ |
---|
113 | free(events); |
---|
114 | caca_free_display(dp); |
---|
115 | caca_free_canvas(cv); |
---|
116 | |
---|
117 | return 0; |
---|
118 | } |
---|
119 | |
---|
120 | static void print_event(int x, int y, caca_event_t *ev) |
---|
121 | { |
---|
122 | int character; |
---|
123 | |
---|
124 | switch(caca_get_event_type(ev)) |
---|
125 | { |
---|
126 | case CACA_EVENT_NONE: |
---|
127 | caca_printf(cv, x, y, "CACA_EVENT_NONE"); |
---|
128 | break; |
---|
129 | case CACA_EVENT_KEY_PRESS: |
---|
130 | character = caca_get_event_key_ch(ev); |
---|
131 | caca_printf(cv, x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%c)", character, |
---|
132 | (character > 0x1f && character < 0x80) ? character : '?'); |
---|
133 | break; |
---|
134 | case CACA_EVENT_KEY_RELEASE: |
---|
135 | character = caca_get_event_key_ch(ev); |
---|
136 | caca_printf(cv, x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%c)", character, |
---|
137 | (character > 0x1f && character < 0x80) ? character : '?'); |
---|
138 | break; |
---|
139 | case CACA_EVENT_MOUSE_MOTION: |
---|
140 | caca_printf(cv, x, y, "CACA_EVENT_MOUSE_MOTION %u %u", |
---|
141 | caca_get_event_mouse_x(ev), caca_get_event_mouse_y(ev)); |
---|
142 | break; |
---|
143 | case CACA_EVENT_MOUSE_PRESS: |
---|
144 | caca_printf(cv, x, y, "CACA_EVENT_MOUSE_PRESS %u", |
---|
145 | caca_get_event_mouse_button(ev)); |
---|
146 | break; |
---|
147 | case CACA_EVENT_MOUSE_RELEASE: |
---|
148 | caca_printf(cv, x, y, "CACA_EVENT_MOUSE_RELEASE %u", |
---|
149 | caca_get_event_mouse_button(ev)); |
---|
150 | break; |
---|
151 | case CACA_EVENT_RESIZE: |
---|
152 | caca_printf(cv, x, y, "CACA_EVENT_RESIZE %u %u", |
---|
153 | caca_get_event_resize_width(ev), |
---|
154 | caca_get_event_resize_height(ev)); |
---|
155 | break; |
---|
156 | case CACA_EVENT_QUIT: |
---|
157 | caca_printf(cv, x, y, "CACA_EVENT_QUIT"); |
---|
158 | break; |
---|
159 | default: |
---|
160 | caca_printf(cv, x, y, "CACA_EVENT_UNKNOWN"); |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|