1 /*
2  * file event.h - xblast event queue
3  *
4  * $Id: event.c,v 1.10 2006/02/09 21:21:23 fzago Exp $
5  *
6  * Program XBLAST
7  * (C) by Oliver Vogel (e-mail: m.vogel@ndh.net)
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published
11  * by the Free Software Foundation; either version 2; or (at your option)
12  * any later version
13  *
14  * This program is distributed in the hope that it will be entertaining,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILTY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17  * Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.
21  * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23 
24 #include "xblast.h"
25 
26 /*
27  * local macros
28  */
29 #define EVENT_QUEUE_SIZE          128
30 
31 /*
32  * local types
33  */
34 typedef struct
35 {
36 	XBEventCode code;
37 	XBEventData data;
38 } XBEvent;
39 
40 /*
41  * local variables
42  */
43 static int eventPut = 0;
44 static int eventGet = 0;
45 static XBEvent eventQueue[EVENT_QUEUE_SIZE];
46 
47 #ifdef DEBUG_XBEVENT
48 /*
49  *
50  */
51 static const char *
EventName(XBEventCode eCode)52 EventName (XBEventCode eCode)
53 {
54 	switch (eCode) {
55 	case XBE_NONE:
56 		return "XBE_NONE";
57 	case XBE_POLL:
58 		return "XBE_POLL";
59 	case XBE_TIMER:
60 		return "XBE_TIMER";
61 	case XBE_SOCKET_READ:
62 		return "XBE_SOCKET_READ";
63 	case XBE_SOCKET_WRITE:
64 		return "XBE_SOCKET_WRITE";
65 	case XBE_MENU:
66 		return "XBE_MENU";
67 	case XBE_ASCII:
68 		return "XBE_ASCII";
69 	case XBE_CTRL:
70 		return "XBE_CTRL";
71 	case XBE_CHAT:
72 		return "XBE_CHAT";
73 	case XBE_KEYSYM:
74 		return "XBE_KEYSYM";
75 	case XBE_XBLAST:
76 		return "XBE_XBLAST";
77 	case XBE_KEYB_1:
78 		return "XBE_KEYB_1";
79 	case XBE_KEYB_2:
80 		return "XBE_KEYB_2";
81 	case XBE_JOYST_1:
82 		return "XBE_JOYST_1";
83 	case XBE_JOYST_2:
84 		return "XBE_JOYST_2";
85 	case XBE_MOUSE_1:
86 		return "XBE_MOUSE_1";
87 	case XBE_MOUSE_2:
88 		return "XBE_MOUSE_2";
89 	case XBE_MOUSE_3:
90 		return "XBE_MOUSE_3";
91 	case XBE_MOUSE_MOVE:
92 		return "XBE_MOUSE_MOVE";
93 	case XBE_SERVER:
94 		return "XBE_SERVER";
95 	default:
96 		return "XBE_?";
97 	}
98 }								/* EventName */
99 #endif
100 
101 /*
102  * Get next event from queue
103  */
104 XBEventCode
NextEvent(XBEventData * data)105 NextEvent (XBEventData * data)
106 {
107 	XBEventCode ecode;
108 	if (eventPut == eventGet) {
109 		ecode = XBE_NONE;
110 	}
111 	else {
112 		ecode = eventQueue[eventGet].code;
113 		*data = eventQueue[eventGet].data;
114 		eventGet++;
115 		if (eventGet >= EVENT_QUEUE_SIZE) {
116 			eventGet = 0;
117 		}
118 	}
119 #ifdef DEBUG_XBEVENT
120 	if (ecode != XBE_NONE && ecode != XBE_TIMER)
121 		Dbg_Out ("next event %s\n", EventName (ecode), data);
122 #endif
123 	return ecode;
124 }								/* NextEvent */
125 
126 /*
127  * Put event in queue
128  */
129 static XBBool
QueueEvent(XBEventCode ecode,XBEventData data)130 QueueEvent (XBEventCode ecode, XBEventData data)
131 {
132 	int newPut;
133 
134 	newPut = eventPut + 1;
135 	if (newPut >= EVENT_QUEUE_SIZE) {
136 		newPut = 0;
137 	}
138 	if (newPut == eventGet) {
139 		/* queue overflow abort */
140 		return XBFalse;
141 	}
142 	eventQueue[eventPut].code = ecode;
143 	eventQueue[eventPut].data = data;
144 	eventPut = newPut;
145 	/* that's all */
146 #ifdef DEBUG_XBEVENT
147 	if (ecode != XBE_NONE && ecode != XBE_TIMER)
148 		Dbg_Out ("queue event %s-%c\n", EventName (ecode), data);
149 #endif
150 	return XBTrue;
151 }								/* QueueEvent */
152 
153 /*
154  * Queue Event with no argument
155  */
156 XBBool
QueueEventVoid(XBEventCode code)157 QueueEventVoid (XBEventCode code)
158 {
159 	XBEventData data;
160 	data.value = 0;
161 	return QueueEvent (code, data);
162 }								/* QueueEventVoid */
163 
164 /*
165  * Queue Event with integer argument
166  */
167 XBBool
QueueEventValue(XBEventCode code,int value)168 QueueEventValue (XBEventCode code, int value)
169 {
170 	XBEventData data;
171 	data.value = value;
172 #ifdef DEBUG_XBEVENT
173 	if (code != XBE_NONE && code != XBE_TIMER)
174 		Dbg_Out ("queue event value %s %c,\n", EventName (code), data.value);
175 #endif
176 	return QueueEvent (code, data);
177 }								/* QueueEventValue */
178 
179 /*
180  * Queue Event with integer argument
181  */
182 XBBool
QueueEventPointer(XBEventCode code,void * ptr)183 QueueEventPointer (XBEventCode code, void *ptr)
184 {
185 	XBEventData data;
186 	data.ptr = ptr;
187 #ifdef DEBUG_XBEVENT
188 	if (code != XBE_NONE && code != XBE_TIMER)
189 		Dbg_Out ("queue event pointer %s %c,\n", EventName (code), data.value);
190 #endif
191 	return QueueEvent (code, data);
192 }								/* QueueEventPointer */
193 
194 /*
195  * Queue Event with integer argument
196  */
197 XBBool
QueueEventPos(XBEventCode code,short x,short y)198 QueueEventPos (XBEventCode code, short x, short y)
199 {
200 	XBEventData data;
201 	data.pos.x = x;
202 	data.pos.y = y;
203 	return QueueEvent (code, data);
204 }								/* QueueEventValue */
205 
206 /*
207  * Queue Event with integer argument
208  */
209 XBBool
QueueEventAtom(XBEventCode code,XBAtom atom)210 QueueEventAtom (XBEventCode code, XBAtom atom)
211 {
212 	XBEventData data;
213 	data.atom = atom;
214 #ifdef DEBUG_XBEVENT
215 	if (code != XBE_NONE && code != XBE_TIMER)
216 		Dbg_Out ("queue event atom %s %c,\n", EventName (code), data.value);
217 #endif
218 	return QueueEvent (code, data);
219 }								/* QueueEventValue */
220