Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mainqueue.c « src « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 012805db01d5daa5dd645bb4ca68d11ab17fee17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * $Id$
 *
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. The Blender
 * Foundation also sells licenses for use in proprietary software under
 * the Blender License.  See http://www.blender.org/BL/ for information
 * about this.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 */

/**
 * \file mainqueue.c
 * \brief Just the functions to maintain a central event queue.
 * 
 * Creates the functionality of a FIFO queue for events.
 *
 * \note The documentor (me) doesn't know the full description of 
 * the fields of the QEvent structure, and the parameters to the 
 * functions (event, val, ascii).  The comments should be updated
 * with more detailed descriptions of what is stored in each one.
 *
 * \warning This queue structure uses a method that assumes the presence
 * of allocated memory.  As well it doesn't de-allocate memory after
 * a read off of the queue, thereby causing a situation whereby memory
 * isn't being freed and can grow with out bound even though there is
 * a limit on the queue size.
 */

#include <stdlib.h>
#include <string.h>
#include "BIF_mainqueue.h"

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/**
 *	\struct QEvent
 *	\brief  This is the definition for the event queue datastructure
 */
typedef struct {
	/**
	 * \var unsigned short event
	 * \brief holds the event type
	 */
	unsigned short event;
	/**
	 * \var short val
	 */
	short val;
	/**
	* \var char ascii
	*/
	char ascii;
} QEvent;

/**
 * \var static QEvent mainqueue[MAXQUEUE]
 * \brief The Main Queue store.
 */
static QEvent mainqueue[MAXQUEUE];

/**
 * \var static unsigned int nevents=0
 * \brief The count of the events currently stored.
 */
static unsigned int nevents= 0;

/**
 * \brief Reads and removes events from the queue and returns the event type,
 * if the queue is empty return 0.
 * \param val the val of the event to read into.
 * \param ascii the buffer of the event to read into.
 * \return the event type or 0 if no event.
 *
 * Pops off the last item in the queue and returns the pieces in their
 * little parts. The last item was the oldest item in the queue.
 * 
 */
unsigned short mainqread(short *val, char *ascii)
{
	if (nevents) {
		nevents--;
		
		*val= mainqueue[nevents].val;
		*ascii= mainqueue[nevents].ascii;
		return mainqueue[nevents].event;
	} else
		return 0;
}

/**
 * \brief A short cut to mainqenter_ext setting ascii to 0
 */
void mainqenter(unsigned short event, short val)
{
	mainqenter_ext(event, val, 0);
}

/**
 * \brief Adds event to the beginning of the queue.
 * \param event the event type.
 * \param val the val of the event.
 * \param ascii the event characters.
 *
 * If the event isn't nothing, and if the queue still
 * has some room, then add to the queue.  Otherwise the
 * event is lost.
 */
void mainqenter_ext(unsigned short event, short val, char ascii)
{
	if (!event)
		return;

	if (nevents<MAXQUEUE) {
		memmove(mainqueue+1, mainqueue, sizeof(*mainqueue)*nevents);	
		mainqueue[0].event= event;
		mainqueue[0].val= val;
		mainqueue[0].ascii= ascii;
		
		nevents++;
	}
}

/**
 * \brief Pushes and event back on to the front of the queue.
 * \param event
 * \param val
 * \param ascii
 *
 * Pushes an event back onto the queue, possibly after a peek
 * at the item.  This method assumes that the memory has already
 * been allocated and should be mentioned in a precondition.
 *
 * \pre This method assumes that the memory is already allocated
 * for the event.
 */
void mainqpushback(unsigned short event, short val, char ascii)
{
	if (nevents<MAXQUEUE) {
		mainqueue[nevents].event= event;
		mainqueue[nevents].val= val;
		mainqueue[nevents].ascii= ascii;
		nevents++;
	}
}

/**
 * \brief Returns the event type from the last item in the queue
 * (the next one that would be popped off).  Probably used as a test
 * to see if the queue is empty or if a valid event is still around.
 * \return the event type of the last item in the queue
 */
unsigned short mainqtest()
{
	if (nevents)
		return mainqueue[nevents-1].event;
	else
		return 0;
}