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