1 /*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 *
5 * http://www.hydrogen-music.org
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY, without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23 #include <hydrogen/event_queue.h>
24
25 namespace H2Core
26 {
27
28 EventQueue* EventQueue::__instance = nullptr;
29
create_instance()30 void EventQueue::create_instance()
31 {
32 if ( __instance == nullptr ) {
33 __instance = new EventQueue;
34 }
35 }
36
37 const char* EventQueue::__class_name = "EventQueue";
38
EventQueue()39 EventQueue::EventQueue()
40 : Object( __class_name )
41 , __read_index( 0 )
42 , __write_index( 0 )
43 {
44 __instance = this;
45
46 for ( int i = 0; i < MAX_EVENTS; ++i ) {
47 __events_buffer[ i ].type = EVENT_NONE;
48 __events_buffer[ i ].value = 0;
49 }
50 }
51
52
~EventQueue()53 EventQueue::~EventQueue()
54 {
55 // infoLog( "DESTROY" );
56 }
57
58
push_event(const EventType type,const int nValue)59 void EventQueue::push_event( const EventType type, const int nValue )
60 {
61 unsigned int nIndex = ++__write_index;
62 nIndex = nIndex % MAX_EVENTS;
63 Event ev;
64 ev.type = type;
65 ev.value = nValue;
66 // INFOLOG( QString( "[pushEvent] %1 : %2 %3" ).arg( nIndex ).arg( ev.type ).arg( ev.value ) );
67 __events_buffer[ nIndex ] = ev;
68 }
69
70
pop_event()71 Event EventQueue::pop_event()
72 {
73 if ( __read_index == __write_index ) {
74 Event ev;
75 ev.type = EVENT_NONE;
76 ev.value = 0;
77 return ev;
78 }
79 unsigned int nIndex = ++__read_index;
80 nIndex = nIndex % MAX_EVENTS;
81 // INFOLOG( QString( "[popEvent] %1 : %2 %3" ).arg( nIndex ).arg( __events_buffer[ nIndex ].type ).arg( __events_buffer[ nIndex ].value ) );
82 return __events_buffer[ nIndex ];
83 }
84
85 };
86