-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxeventloop.cpp
57 lines (47 loc) · 1.16 KB
/
xeventloop.cpp
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
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <X11/Xlib.h>
#include "exceptions.hpp"
#include "xeventloop.hpp"
static void *thread_helper( void *xeventloop )
{
XEventLoop *me = static_cast<XEventLoop *>( xeventloop );
ahabassert( me );
me->loop();
return NULL;
}
XEventLoop::XEventLoop( OpenGLDisplay *s_display )
: keys( 0 ),
repaints( 0 ),
display( s_display ),
live( true )
{
unixassert( pthread_mutex_init( &mutex, NULL ) );
pthread_create( &thread_handle, NULL, thread_helper, this );
}
void XEventLoop::loop( void )
{
XEvent ev;
XExposeEvent *expose = (XExposeEvent *)&ev;
XKeyEvent *key = (XKeyEvent *)&ev;
while ( 1 ) {
display->getevent( true, &ev );
{ MutexLock x( &mutex ); if ( !live ) return; }
if ( ev.type == Expose && expose->count == 0 ) {
repaints.enqueue( new Repaint() );
} else if ( ev.type == KeyPress ) {
KeySym keysym = XLookupKeysym( key, 0 );
keys.enqueue( new XKey( keysym ) );
}
}
}
XEventLoop::~XEventLoop()
{
{
MutexLock x( &mutex );
live = false;
}
display->makeevent();
pthread_join( thread_handle, NULL );
}