OISLIRCRingBuffer.h 7.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#include "OISConfig.h"
#ifdef OIS_LIRC_SUPPORT
/*
The zlib/libpng License

Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com)

This software is provided 'as-is', without any express or implied warranty. In no event will
the authors be held liable for any damages arising from the use of this software.

Ben Hymers's avatar
Ben Hymers committed
11
Permission is granted to anyone to use this software for any purpose, including commercial
12
13
14
applications, and to alter it and redistribute it freely, subject to the following
restrictions:

Ben Hymers's avatar
Ben Hymers committed
15
16
17
    1. The origin of this software must not be misrepresented; you must not claim that
		you wrote the original software. If you use this software in a product,
		an acknowledgment in the product documentation would be appreciated but is
18
19
		not required.

Ben Hymers's avatar
Ben Hymers committed
20
    2. Altered source versions must be plainly marked as such, and must not be
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
		misrepresented as being the original software.

    3. This notice may not be removed or altered from any source distribution.

 # ------------------------#
 # Original License follows:
 # ------------------------#

 * PortAudio Portable Real-Time Audio Library
 * Latest version at: http://www.audiomulch.com/portaudio/
 * <platform> Implementation
 * Copyright (c) 1999-2000 <author(s)>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * Any person wishing to distribute modifications to the Software is
 * requested to send the modifications to the original developer so that
 * they can be incorporated into the canonical version.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#ifndef OIS_LIRCRingBuffer_H
#define OIS_LIRCRingBuffer_H

#include "OISPrereqs.h"

namespace OIS
{
	struct LIRCEvent
	{
		//! high bit (0x80000000) will be on if button pushed, else it was released
		unsigned int button;
	};

	/// <summary>
	/// Ring Buffer (fifo) used to store 16bit pcm data
	/// </summary>
	class LIRCRingBuffer
	{
	private:
		//! Number of bytes in FIFO. Power of 2. Set by RingBuffer_Init
		int bufferSize;
		//! Used for wrapping indices with extra bit to distinguish full/empty.
		int bigMask;
		// Used for fitting indices to buffer.
		int smallMask;

		// Buffer holding the actual event buffers
		LIRCEvent *buffer;

		//! Index of next writable byte. Set by RingBuffer_AdvanceWriteIndex.
Ben Hymers's avatar
Ben Hymers committed
87
88
		volatile int writeIndex;

89
		//! Index of next readable byte. Set by RingBuffer_AdvanceReadIndex.
Ben Hymers's avatar
Ben Hymers committed
90
		volatile int readIndex;
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

	public:
		LIRCRingBuffer( unsigned int numEntries )
		{
			numEntries = RoundUpToNextPowerOf2( numEntries );

			//2 bytes per short
			bufferSize = (int)numEntries;
			buffer = new LIRCEvent[numEntries];

			Flush();

			bigMask = (int)(numEntries*2)-1;
			smallMask = (int)(numEntries)-1;
		}

		~LIRCRingBuffer()
		{
			delete buffer;
		}

		unsigned int RoundUpToNextPowerOf2( unsigned int n )
		{
			int numBits = 0;
Ben Hymers's avatar
Ben Hymers committed
115
			if( ((n-1) & n) == 0)
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
			return n; //Already Power of two.

			while( n > 0 )
			{
				n= n>>1;
				numBits++;
			}
			return (unsigned int)(1<<numBits);
		}


		int GetReadAvailable( )
		{
			return ( (writeIndex - readIndex) & bigMask );
		}


		int GetWriteAvailable( )
		{
			return ( bufferSize - GetReadAvailable());
		}


		int Write( LIRCEvent *data, int numEntries )
		{
			int size1 = 0, size2 = 0, numWritten;
			int data1Ptr = 0, data2Ptr = 0;
Ben Hymers's avatar
Ben Hymers committed
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
			numWritten = GetWriteRegions( numEntries, data1Ptr, size1, data2Ptr, size2 );

			if( size2 > 0 )
			{
				//copy to two parts
				memcpy( &buffer[data1Ptr], data, sizeof(LIRCEvent) * size1 );
				//Array.Copy( data, offsetPtr, buffer, data1Ptr, size1 );
				memcpy( &buffer[data2Ptr], &data[size1], sizeof(LIRCEvent) * size2 );
				//Array.Copy( data, offsetPtr + size1, buffer, data2Ptr, size2 );
			}
			else
			{	//Copy all continous
				memcpy( &buffer[data1Ptr], data, sizeof(LIRCEvent) * size1 );
				//Array.Copy( data, offsetPtr, buffer, data1Ptr, size1 );
			}
			AdvanceWriteIndex( numWritten );
			return numWritten;
		}


		/// <summary>
		/// Reads requested number of entries into sent array.
		/// Returns number written
		/// </summary>
		int Read( LIRCEvent *data, int numEntries )
		{
			int size1 = 0, size2 = 0, numRead, data1Ptr = 0, data2Ptr = 0;
Ben Hymers's avatar
Ben Hymers committed
171

172
			numRead = GetReadRegions( numEntries, data1Ptr, size1, data2Ptr, size2 );
Ben Hymers's avatar
Ben Hymers committed
173

174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
			if( size2 > 0 )
			{
				memcpy( data, &buffer[data1Ptr], sizeof(LIRCEvent) * size1 );
				//Array.Copy( buffer, data1Ptr, data, 0, size1 );
				memcpy( &data[size1], &buffer[data2Ptr], sizeof(LIRCEvent) * size2 );
				//Array.Copy( buffer, data2Ptr, data, size1, size2 );
			}
			else
				memcpy( data, &buffer[data1Ptr], sizeof(LIRCEvent) * size1 );
				//Array.Copy( buffer, data1Ptr, data, 0, size1 );

			AdvanceReadIndex( numRead );
			return numRead;
		}

	private:

		int GetWriteRegions( int numEntries, int &dataPtr1, int &sizePtr1,
							 int &dataPtr2, int &sizePtr2 )
		{
			int   index;
			int   available = GetWriteAvailable();
Ben Hymers's avatar
Ben Hymers committed
196
			if( numEntries > available )
197
				numEntries = available;
Ben Hymers's avatar
Ben Hymers committed
198

199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
			//Check to see if write is not contiguous.
			index = writeIndex & smallMask;
			if( (index + numEntries) > bufferSize )
			{
				//Write data in two blocks that wrap the buffer.
				int   firstHalf = bufferSize - index;
				dataPtr1 = index;//&buffer[index];
				sizePtr1 = firstHalf;
				dataPtr2 = 0;//&buffer[0];
				sizePtr2 = numEntries - firstHalf;
			}
			else
			{
				dataPtr1 = index;//&buffer[index];
				sizePtr1 = numEntries;
				dataPtr2 = 0;
				sizePtr2 = 0;
			}
			return numEntries;
		}
Ben Hymers's avatar
Ben Hymers committed
219

220
221
222
223
224

		int GetReadRegions( int numEntries, int &dataPtr1, int &sizePtr1, int &dataPtr2, int &sizePtr2 )
		{
			int   index;
			int   available = GetReadAvailable( );
Ben Hymers's avatar
Ben Hymers committed
225
			if( numEntries > available )
226
				numEntries = available;
Ben Hymers's avatar
Ben Hymers committed
227

228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
			// Check to see if read is not contiguous
			index = readIndex & smallMask;
			if( (index + numEntries) > bufferSize )
			{
				// Write data in two blocks that wrap the buffer
				int firstHalf = bufferSize - index;
				dataPtr1 = index;//&buffer[index];
				sizePtr1 = firstHalf;
				dataPtr2 = 0;//&buffer[0];
				sizePtr2 = numEntries - firstHalf;
			}
			else
			{
				dataPtr1 = index;//&buffer[index];
				sizePtr1 = numEntries;
				dataPtr2 = 0;
				sizePtr2 = 0;
			}
			return numEntries;
		}


		int AdvanceWriteIndex( int numEntries )
		{
			 return writeIndex = (writeIndex + numEntries) & bigMask;
		}


		int AdvanceReadIndex( int numEntries )
		{
			return readIndex = (readIndex + numEntries) & bigMask;
		}


		void Flush( )
		{
			writeIndex = readIndex = 0;
		}
	};
}
#endif //#define OIS_LIRCRingBuffer_H
#endif