1 | /* |
---|
2 | * libcaca Colour ASCII-Art library |
---|
3 | * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * This library is free software; you can redistribute it and/or |
---|
7 | * modify it under the terms of the Do What The Fuck You Want To |
---|
8 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
9 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
10 | */ |
---|
11 | |
---|
12 | /** \file event.c |
---|
13 | * \version \$Id: event.c 681 2006-03-23 18:36:59Z sam $ |
---|
14 | * \author Sam Hocevar <sam@zoy.org> |
---|
15 | * \brief Event handling |
---|
16 | * |
---|
17 | * This file contains event handling functions for keyboard and mouse input. |
---|
18 | */ |
---|
19 | |
---|
20 | #include "config.h" |
---|
21 | |
---|
22 | #if !defined(__KERNEL__) |
---|
23 | # include <stdio.h> |
---|
24 | #endif |
---|
25 | |
---|
26 | #include "cucul.h" |
---|
27 | #include "cucul_internals.h" |
---|
28 | #include "caca.h" |
---|
29 | #include "caca_internals.h" |
---|
30 | |
---|
31 | static int _get_next_event(caca_t *, struct caca_event *); |
---|
32 | static int _lowlevel_event(caca_t *, struct caca_event *); |
---|
33 | |
---|
34 | #if !defined(_DOXYGEN_SKIP_ME) |
---|
35 | /* If no new key was pressed after AUTOREPEAT_THRESHOLD usec, assume the |
---|
36 | * key was released */ |
---|
37 | #define AUTOREPEAT_THRESHOLD 200000 |
---|
38 | /* Start repeating key after AUTOREPEAT_TRIGGER usec and send keypress |
---|
39 | * events every AUTOREPEAT_RATE usec. */ |
---|
40 | #define AUTOREPEAT_TRIGGER 300000 |
---|
41 | #define AUTOREPEAT_RATE 100000 |
---|
42 | #endif |
---|
43 | |
---|
44 | /** \brief Get the next mouse or keyboard input event. |
---|
45 | * |
---|
46 | * This function polls the event queue for mouse or keyboard events matching |
---|
47 | * the event mask and returns the first matching event. Non-matching events |
---|
48 | * are discarded. \c event_mask must have a non-zero value. This function is |
---|
49 | * non-blocking and returns zero if no more events are pending in the queue. |
---|
50 | * See also caca_wait_event() for a blocking version of this function. |
---|
51 | * |
---|
52 | * \param event_mask Bitmask of requested events. |
---|
53 | * \return The next matching event in the queue, or 0 if no event is pending. |
---|
54 | */ |
---|
55 | int caca_get_event(caca_t *kk, unsigned int event_mask, struct caca_event *ev) |
---|
56 | { |
---|
57 | if(!event_mask) |
---|
58 | return 0; |
---|
59 | |
---|
60 | for( ; ; ) |
---|
61 | { |
---|
62 | int ret = _get_next_event(kk, ev); |
---|
63 | |
---|
64 | if(!ret || ev->type & event_mask) |
---|
65 | return ret; |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | /** \brief Wait for the next mouse or keyboard input event. |
---|
70 | * |
---|
71 | * This function returns the first mouse or keyboard event in the queue |
---|
72 | * that matches the event mask. If no event is pending, it blocks until a |
---|
73 | * matching event is received. \c event_mask must have a non-zero value. |
---|
74 | * See also caca_get_event() for a non-blocking version of this function. |
---|
75 | * |
---|
76 | * \param event_mask Bitmask of requested events. |
---|
77 | * \return The next event in the queue. |
---|
78 | */ |
---|
79 | int caca_wait_event(caca_t *kk, unsigned int event_mask, struct caca_event *ev) |
---|
80 | { |
---|
81 | if(!event_mask) |
---|
82 | return 0; |
---|
83 | |
---|
84 | for( ; ; ) |
---|
85 | { |
---|
86 | int ret = _get_next_event(kk, ev); |
---|
87 | |
---|
88 | if(ret && (ev->type & event_mask)) |
---|
89 | return ret; |
---|
90 | |
---|
91 | _caca_sleep(10000); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | /** \brief Return the X mouse coordinate. |
---|
96 | * |
---|
97 | * This function returns the X coordinate of the mouse position last time |
---|
98 | * it was detected. This function is not reliable if the ncurses or S-Lang |
---|
99 | * drivers are being used, because mouse position is only detected when |
---|
100 | * the mouse is clicked. Other drivers such as X11 work well. |
---|
101 | * |
---|
102 | * \return The X mouse coordinate. |
---|
103 | */ |
---|
104 | unsigned int caca_get_mouse_x(caca_t *kk) |
---|
105 | { |
---|
106 | if(kk->mouse.x >= kk->qq->width) |
---|
107 | kk->mouse.x = kk->qq->width - 1; |
---|
108 | |
---|
109 | return kk->mouse.x; |
---|
110 | } |
---|
111 | |
---|
112 | /** \brief Return the Y mouse coordinate. |
---|
113 | * |
---|
114 | * This function returns the Y coordinate of the mouse position last time |
---|
115 | * it was detected. This function is not reliable if the ncurses or S-Lang |
---|
116 | * drivers are being used, because mouse position is only detected when |
---|
117 | * the mouse is clicked. Other drivers such as X11 work well. |
---|
118 | * |
---|
119 | * \return The Y mouse coordinate. |
---|
120 | */ |
---|
121 | unsigned int caca_get_mouse_y(caca_t *kk) |
---|
122 | { |
---|
123 | if(kk->mouse.y >= kk->qq->height) |
---|
124 | kk->mouse.y = kk->qq->height - 1; |
---|
125 | |
---|
126 | return kk->mouse.y; |
---|
127 | } |
---|
128 | |
---|
129 | /* |
---|
130 | * XXX: The following functions are local. |
---|
131 | */ |
---|
132 | |
---|
133 | static int _get_next_event(caca_t *kk, struct caca_event *ev) |
---|
134 | { |
---|
135 | #if defined(USE_SLANG) || defined(USE_NCURSES) |
---|
136 | unsigned int ticks; |
---|
137 | #endif |
---|
138 | int ret; |
---|
139 | |
---|
140 | /* If we are about to return a resize event, acknowledge it */ |
---|
141 | if(kk->resize.resized) |
---|
142 | { |
---|
143 | kk->resize.resized = 0; |
---|
144 | _caca_handle_resize(kk); |
---|
145 | ev->type = CACA_EVENT_RESIZE; |
---|
146 | ev->data.resize.w = kk->qq->width; |
---|
147 | ev->data.resize.h = kk->qq->height; |
---|
148 | return 1; |
---|
149 | } |
---|
150 | |
---|
151 | ret = _lowlevel_event(kk, ev); |
---|
152 | |
---|
153 | #if defined(USE_SLANG) |
---|
154 | if(kk->drv.driver != CACA_DRIVER_SLANG) |
---|
155 | #endif |
---|
156 | #if defined(USE_NCURSES) |
---|
157 | if(kk->drv.driver != CACA_DRIVER_NCURSES) |
---|
158 | #endif |
---|
159 | return ret; |
---|
160 | |
---|
161 | #if defined(USE_SLANG) || defined(USE_NCURSES) |
---|
162 | /* Simulate long keypresses using autorepeat features */ |
---|
163 | ticks = _caca_getticks(&kk->events.key_timer); |
---|
164 | kk->events.last_key_ticks += ticks; |
---|
165 | kk->events.autorepeat_ticks += ticks; |
---|
166 | |
---|
167 | /* Handle autorepeat */ |
---|
168 | if(kk->events.last_key_event.type |
---|
169 | && kk->events.autorepeat_ticks > AUTOREPEAT_TRIGGER |
---|
170 | && kk->events.autorepeat_ticks > AUTOREPEAT_THRESHOLD |
---|
171 | && kk->events.autorepeat_ticks > AUTOREPEAT_RATE) |
---|
172 | { |
---|
173 | _push_event(kk, ev); |
---|
174 | kk->events.autorepeat_ticks -= AUTOREPEAT_RATE; |
---|
175 | *ev = kk->events.last_key_event; |
---|
176 | return 1; |
---|
177 | } |
---|
178 | |
---|
179 | /* We are in autorepeat mode and the same key was just pressed, ignore |
---|
180 | * this event and return the next one by calling ourselves. */ |
---|
181 | if(ev->type == CACA_EVENT_KEY_PRESS |
---|
182 | && kk->events.last_key_event.type |
---|
183 | && ev->data.key.c == kk->events.last_key_event.data.key.c |
---|
184 | && ev->data.key.ucs4 == kk->events.last_key_event.data.key.ucs4) |
---|
185 | { |
---|
186 | kk->events.last_key_ticks = 0; |
---|
187 | return _get_next_event(kk, ev); |
---|
188 | } |
---|
189 | |
---|
190 | /* We are in autorepeat mode, but key has expired or a new key was |
---|
191 | * pressed - store our event and return a key release event first */ |
---|
192 | if(kk->events.last_key_event.type |
---|
193 | && (kk->events.last_key_ticks > AUTOREPEAT_THRESHOLD |
---|
194 | || (ev->type & CACA_EVENT_KEY_PRESS))) |
---|
195 | { |
---|
196 | _push_event(kk, ev); |
---|
197 | *ev = kk->events.last_key_event; |
---|
198 | ev->type = CACA_EVENT_KEY_RELEASE; |
---|
199 | kk->events.last_key_event.type = CACA_EVENT_NONE; |
---|
200 | return 1; |
---|
201 | } |
---|
202 | |
---|
203 | /* A new key was pressed, enter autorepeat mode */ |
---|
204 | if(ev->type & CACA_EVENT_KEY_PRESS) |
---|
205 | { |
---|
206 | kk->events.last_key_ticks = 0; |
---|
207 | kk->events.autorepeat_ticks = 0; |
---|
208 | kk->events.last_key_event = *ev; |
---|
209 | } |
---|
210 | |
---|
211 | return ev->type ? 1 : 0; |
---|
212 | #endif |
---|
213 | } |
---|
214 | |
---|
215 | static int _lowlevel_event(caca_t *kk, struct caca_event *ev) |
---|
216 | { |
---|
217 | #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) |
---|
218 | int ret = _pop_event(kk, ev); |
---|
219 | |
---|
220 | if(ret) |
---|
221 | return ret; |
---|
222 | #endif |
---|
223 | |
---|
224 | return kk->drv.get_event(kk, ev); |
---|
225 | } |
---|
226 | |
---|
227 | #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL) |
---|
228 | void _push_event(caca_t *kk, struct caca_event *ev) |
---|
229 | { |
---|
230 | if(!ev->type || kk->events.queue == EVENTBUF_LEN) |
---|
231 | return; |
---|
232 | kk->events.buf[kk->events.queue] = *ev; |
---|
233 | kk->events.queue++; |
---|
234 | } |
---|
235 | |
---|
236 | int _pop_event(caca_t *kk, struct caca_event *ev) |
---|
237 | { |
---|
238 | int i; |
---|
239 | |
---|
240 | if(kk->events.queue == 0) |
---|
241 | return 0; |
---|
242 | |
---|
243 | *ev = kk->events.buf[0]; |
---|
244 | for(i = 1; i < kk->events.queue; i++) |
---|
245 | kk->events.buf[i - 1] = kk->events.buf[i]; |
---|
246 | kk->events.queue--; |
---|
247 | |
---|
248 | return 1; |
---|
249 | } |
---|
250 | #endif |
---|
251 | |
---|