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 1048 2006-09-17 12:44:18Z jylam $ |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the Do What The Fuck You Want To |
---|
10 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
11 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
12 | */ |
---|
13 | |
---|
14 | #include "config.h" |
---|
15 | #include "common.h" |
---|
16 | #if !defined(__KERNEL__) |
---|
17 | # include <stdio.h> |
---|
18 | # include <string.h> |
---|
19 | # include <stdlib.h> |
---|
20 | #endif |
---|
21 | |
---|
22 | #include "cucul.h" |
---|
23 | #include "caca.h" |
---|
24 | |
---|
25 | static cucul_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 = cucul_create_canvas(0, 0); |
---|
36 | if(!cv) |
---|
37 | return 1; |
---|
38 | dp = caca_create_display(cv); |
---|
39 | if(!dp) |
---|
40 | return 1; |
---|
41 | |
---|
42 | h = cucul_get_canvas_height(cv) - 1; |
---|
43 | |
---|
44 | cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE); |
---|
45 | cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, " "); |
---|
46 | |
---|
47 | cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, " "); |
---|
48 | cucul_putstr(cv, 0, h, "type \"quit\" to exit"); |
---|
49 | |
---|
50 | caca_refresh_display(dp); |
---|
51 | |
---|
52 | events = malloc(h * sizeof(caca_event_t)); |
---|
53 | memset(events, 0, h * sizeof(caca_event_t)); |
---|
54 | |
---|
55 | for(quit = 0; quit < 4; ) |
---|
56 | { |
---|
57 | caca_event_t ev; |
---|
58 | static char const * quit_string[] = { "", "q", "qu", "qui", "quit" }; |
---|
59 | int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, -1); |
---|
60 | |
---|
61 | if(!ret) |
---|
62 | continue; |
---|
63 | |
---|
64 | do |
---|
65 | { |
---|
66 | /* "quit" quits */ |
---|
67 | if(ev.type & CACA_EVENT_KEY_PRESS) |
---|
68 | { |
---|
69 | int key = ev.data.key.ch; |
---|
70 | if((key == 'q' && quit == 0) || (key == 'u' && quit == 1) |
---|
71 | || (key == 'i' && quit == 2) || (key == 't' && quit == 3)) |
---|
72 | quit++; |
---|
73 | else if(key == 'q') |
---|
74 | quit = 1; |
---|
75 | else |
---|
76 | quit = 0; |
---|
77 | } |
---|
78 | |
---|
79 | memmove(events + 1, events, (h - 1) * sizeof(caca_event_t)); |
---|
80 | events[0] = ev; |
---|
81 | |
---|
82 | ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0); |
---|
83 | } |
---|
84 | while(ret); |
---|
85 | |
---|
86 | cucul_set_color(cv, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK); |
---|
87 | cucul_clear_canvas(cv); |
---|
88 | |
---|
89 | /* Print current event */ |
---|
90 | cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE); |
---|
91 | cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, " "); |
---|
92 | print_event(0, 0, events); |
---|
93 | |
---|
94 | cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, " "); |
---|
95 | cucul_printf(cv, 0, h, "type \"quit\" to exit: %s", quit_string[quit]); |
---|
96 | |
---|
97 | /* Print previous events */ |
---|
98 | cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLACK); |
---|
99 | for(i = 1; i < h && events[i].type; i++) |
---|
100 | print_event(0, i, events + i); |
---|
101 | |
---|
102 | caca_refresh_display(dp); |
---|
103 | } |
---|
104 | |
---|
105 | /* Clean up */ |
---|
106 | caca_free_display(dp); |
---|
107 | cucul_free_canvas(cv); |
---|
108 | |
---|
109 | return 0; |
---|
110 | } |
---|
111 | |
---|
112 | static void print_event(int x, int y, caca_event_t *ev) |
---|
113 | { |
---|
114 | int character; |
---|
115 | |
---|
116 | switch(ev->type) |
---|
117 | { |
---|
118 | case CACA_EVENT_NONE: |
---|
119 | cucul_printf(cv, x, y, "CACA_EVENT_NONE"); |
---|
120 | break; |
---|
121 | case CACA_EVENT_KEY_PRESS: |
---|
122 | character = ev->data.key.ch; |
---|
123 | cucul_printf(cv, x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%c)", character, |
---|
124 | (character > 0x1f && character < 0x80) ? character : '?'); |
---|
125 | break; |
---|
126 | case CACA_EVENT_KEY_RELEASE: |
---|
127 | character = ev->data.key.ch; |
---|
128 | cucul_printf(cv, x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%c)", character, |
---|
129 | (character > 0x1f && character < 0x80) ? character : '?'); |
---|
130 | break; |
---|
131 | case CACA_EVENT_MOUSE_MOTION: |
---|
132 | cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_MOTION %u %u", |
---|
133 | ev->data.mouse.x, ev->data.mouse.y); |
---|
134 | break; |
---|
135 | case CACA_EVENT_MOUSE_PRESS: |
---|
136 | cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_PRESS %u", |
---|
137 | ev->data.mouse.button); |
---|
138 | break; |
---|
139 | case CACA_EVENT_MOUSE_RELEASE: |
---|
140 | cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_RELEASE %u", |
---|
141 | ev->data.mouse.button); |
---|
142 | break; |
---|
143 | case CACA_EVENT_RESIZE: |
---|
144 | cucul_printf(cv, x, y, "CACA_EVENT_RESIZE %u %u", |
---|
145 | ev->data.resize.w, ev->data.resize.h); |
---|
146 | break; |
---|
147 | case CACA_EVENT_QUIT: |
---|
148 | cucul_printf(cv, x, y, "CACA_EVENT_QUIT"); |
---|
149 | break; |
---|
150 | default: |
---|
151 | cucul_printf(cv, x, y, "CACA_EVENT_UNKNOWN"); |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|