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 1823 2007-09-30 13:32:16Z 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 | #if !defined(__KERNEL__) |
---|
18 | # include <stdio.h> |
---|
19 | # include <string.h> |
---|
20 | # include <stdlib.h> |
---|
21 | #endif |
---|
22 | |
---|
23 | #include "cucul.h" |
---|
24 | #include "caca.h" |
---|
25 | |
---|
26 | static cucul_canvas_t *cv; |
---|
27 | static caca_display_t *dp; |
---|
28 | |
---|
29 | static void print_event(int, int, caca_event_t *); |
---|
30 | |
---|
31 | int main(int argc, char **argv) |
---|
32 | { |
---|
33 | caca_event_t *events; |
---|
34 | int i, h, quit; |
---|
35 | |
---|
36 | cv = cucul_create_canvas(80, 24); |
---|
37 | if(cv == NULL) |
---|
38 | { |
---|
39 | printf("Failed to create canvas\n"); |
---|
40 | return 1; |
---|
41 | } |
---|
42 | |
---|
43 | dp = caca_create_display(cv); |
---|
44 | if(dp == NULL) |
---|
45 | { |
---|
46 | printf("Failed to create display\n"); |
---|
47 | return 1; |
---|
48 | } |
---|
49 | |
---|
50 | h = cucul_get_canvas_height(cv) - 1; |
---|
51 | |
---|
52 | cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE); |
---|
53 | cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, ' '); |
---|
54 | |
---|
55 | cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, ' '); |
---|
56 | cucul_put_str(cv, 0, h, "type \"quit\" to exit"); |
---|
57 | |
---|
58 | caca_refresh_display(dp); |
---|
59 | |
---|
60 | events = malloc(h * sizeof(caca_event_t)); |
---|
61 | memset(events, 0, h * sizeof(caca_event_t)); |
---|
62 | |
---|
63 | for(quit = 0; quit < 4; ) |
---|
64 | { |
---|
65 | caca_event_t ev; |
---|
66 | static char const * quit_string[] = { "", "q", "qu", "qui", "quit" }; |
---|
67 | int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, -1); |
---|
68 | |
---|
69 | if(!ret) |
---|
70 | continue; |
---|
71 | |
---|
72 | do |
---|
73 | { |
---|
74 | /* "quit" quits */ |
---|
75 | if(ev.type & CACA_EVENT_KEY_PRESS) |
---|
76 | { |
---|
77 | int key = ev.data.key.ch; |
---|
78 | if((key == 'q' && quit == 0) || (key == 'u' && quit == 1) |
---|
79 | || (key == 'i' && quit == 2) || (key == 't' && quit == 3)) |
---|
80 | quit++; |
---|
81 | else if(key == 'q') |
---|
82 | quit = 1; |
---|
83 | else |
---|
84 | quit = 0; |
---|
85 | } |
---|
86 | |
---|
87 | memmove(events + 1, events, (h - 1) * sizeof(caca_event_t)); |
---|
88 | events[0] = ev; |
---|
89 | |
---|
90 | ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0); |
---|
91 | } |
---|
92 | while(ret); |
---|
93 | |
---|
94 | cucul_set_color_ansi(cv, CUCUL_LIGHTGRAY, CUCUL_BLACK); |
---|
95 | cucul_clear_canvas(cv); |
---|
96 | |
---|
97 | /* Print current event */ |
---|
98 | cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE); |
---|
99 | cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, ' '); |
---|
100 | print_event(0, 0, events); |
---|
101 | |
---|
102 | cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, ' '); |
---|
103 | cucul_printf(cv, 0, h, "type \"quit\" to exit: %s", quit_string[quit]); |
---|
104 | |
---|
105 | /* Print previous events */ |
---|
106 | cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLACK); |
---|
107 | for(i = 1; i < h && events[i].type; i++) |
---|
108 | print_event(0, i, events + i); |
---|
109 | |
---|
110 | caca_refresh_display(dp); |
---|
111 | } |
---|
112 | |
---|
113 | /* Clean up */ |
---|
114 | caca_free_display(dp); |
---|
115 | cucul_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(ev->type) |
---|
125 | { |
---|
126 | case CACA_EVENT_NONE: |
---|
127 | cucul_printf(cv, x, y, "CACA_EVENT_NONE"); |
---|
128 | break; |
---|
129 | case CACA_EVENT_KEY_PRESS: |
---|
130 | character = ev->data.key.ch; |
---|
131 | cucul_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 = ev->data.key.ch; |
---|
136 | cucul_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 | cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_MOTION %u %u", |
---|
141 | ev->data.mouse.x, ev->data.mouse.y); |
---|
142 | break; |
---|
143 | case CACA_EVENT_MOUSE_PRESS: |
---|
144 | cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_PRESS %u", |
---|
145 | ev->data.mouse.button); |
---|
146 | break; |
---|
147 | case CACA_EVENT_MOUSE_RELEASE: |
---|
148 | cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_RELEASE %u", |
---|
149 | ev->data.mouse.button); |
---|
150 | break; |
---|
151 | case CACA_EVENT_RESIZE: |
---|
152 | cucul_printf(cv, x, y, "CACA_EVENT_RESIZE %u %u", |
---|
153 | ev->data.resize.w, ev->data.resize.h); |
---|
154 | break; |
---|
155 | case CACA_EVENT_QUIT: |
---|
156 | cucul_printf(cv, x, y, "CACA_EVENT_QUIT"); |
---|
157 | break; |
---|
158 | default: |
---|
159 | cucul_printf(cv, x, y, "CACA_EVENT_UNKNOWN"); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|