1 /*
2 * events.c
3 * Half baked internal event handling.
4 * AYM 1998-11-09
5 */
6
7
8 /*
9 This file is part of Yadex.
10
11 Yadex incorporates code from DEU 5.21 that was put in the public domain in
12 1994 by Rapha�l Quinet and Brendon Wyber.
13
14 The rest of Yadex is Copyright � 1997-2003 Andr� Majorel and others.
15
16 This program is free software; you can redistribute it and/or modify it under
17 the terms of the GNU General Public License as published by the Free Software
18 Foundation; either version 2 of the License, or (at your option) any later
19 version.
20
21 This program is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
23 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License along with
26 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
27 Place, Suite 330, Boston, MA 02111-1307, USA.
28 */
29
30
31 /*
32 This module sucks TOTALLY !
33 Issues :
34 - should functions that take an event type as a criterion
35 limit themselves to the last event or look the entire
36 queue ?
37 - should probably group all YK_* events under the type
38 YE_KEYPRESS and put the exact key number in a struct
39 field,
40 - and more...
41 */
42
43
44 #include "yadex.h"
45 #include "events.h"
46
47
48 // Number of cells in the event queue (actually a circular
49 // buffer). The queue can actually hold only that number
50 // minus one events.
51 #define Y_EVENT_QUEUE 100
52 static int event_queue[Y_EVENT_QUEUE];
53 // Index of the head and tail of the circular buffer.
54 // At any moment, the next event to get is in event_queue[head]
55 // and the next put will be put in event_queue[tail]
56 static int head = 0;
57 static int events_in_queue = 0;
58
59
60
61 /*
62 *
63 */
init_event()64 void init_event ()
65 {
66 head = 0;
67 events_in_queue = 0;
68 }
69
70
71 /*
72 *
73 */
send_event(int event)74 void send_event (int event)
75 {
76 if (events_in_queue == Y_EVENT_QUEUE)
77 fatal_error ("Event buffer full");
78 event_queue[(head + events_in_queue) % Y_EVENT_QUEUE] = event;
79 events_in_queue++;
80 }
81
82
83 /*
84 * has_event
85 * Is there any event at all ?
86 */
has_event()87 int has_event ()
88 {
89 return events_in_queue != 0;
90 }
91
92
93 /*
94 * has_event
95 * Is there an event of that type ?
96 */
has_event(int event)97 int has_event (int event)
98 {
99 return events_in_queue != 0
100 && event_queue[head] == event;
101 }
102
103
104 /*
105 * has_key_press_event
106 * Is there an YK_* event ?
107 * FIXME should create YE_KEY_PRESS and delete this function.
108 */
has_key_press_event()109 int has_key_press_event ()
110 {
111 return events_in_queue != 0
112 && event_queue[head]
113 && (event_queue[head] & ~ (YK_ALT | YK_CTRL | YK_SHIFT)) < YK__LAST;
114 }
115
116
117 /*
118 * get_event
119 * Get the next event
120 */
get_event()121 int get_event ()
122 {
123 if (events_in_queue == 0) // The buffer is empty
124 return 0;
125
126 int e = event_queue[head];
127 events_in_queue--;
128 head++;
129 if (head == Y_EVENT_QUEUE)
130 head = 0;
131 return e;
132 }
133
134
135
136
137