hornetseye-xorg 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/ext/rubytools.hh ADDED
@@ -0,0 +1,33 @@
1
+ /* HornetsEye - Computer Vision with Ruby
2
+ Copyright (C) 2006, 2007 Jan Wedekind
3
+
4
+ This program is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
+ #ifndef HORNETSEYE_RUBYTOOLS_HH
17
+ #define HORNETSEYE_RUBYTOOLS_HH
18
+
19
+ #include <complex>
20
+ #include "rubyinc.hh"
21
+
22
+ void checkType( VALUE rbValue, VALUE rbClass );
23
+
24
+ void checkStruct( VALUE rbValue, VALUE rbClass );
25
+
26
+ #define dataGetStruct(obj,klass,type,sval) { \
27
+ checkStruct( obj, klass ); \
28
+ Data_Get_Struct( obj, type, sval ); \
29
+ }
30
+
31
+ #include "rubytools.tcc"
32
+
33
+ #endif
data/ext/rubytools.tcc ADDED
@@ -0,0 +1,32 @@
1
+ /* HornetsEye - Computer Vision with Ruby
2
+ Copyright (C) 2006, 2007 Jan Wedekind
3
+
4
+ This program is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
+ #include <cassert>
17
+ #include "error.hh"
18
+
19
+ inline void checkType( VALUE rbValue, VALUE rbClass )
20
+ {
21
+ ERRORMACRO( rb_funcall( rbValue, rb_intern( "kind_of?" ), 1, rbClass ) ==
22
+ Qtrue, Error, ,
23
+ "Argument must be of class \"" << rb_class2name( rbClass )
24
+ << "\"." );
25
+ }
26
+
27
+ inline void checkStruct( VALUE rbValue, VALUE rbClass )
28
+ {
29
+ Check_Type( rbValue, T_DATA );
30
+ checkType( rbValue, rbClass );
31
+ }
32
+
data/ext/timer.cc ADDED
@@ -0,0 +1,40 @@
1
+ /* HornetsEye - Computer Vision with Ruby
2
+ Copyright (C) 2006, 2007 Jan Wedekind
3
+
4
+ This program is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
+ #include "timer.hh"
17
+
18
+ #ifndef timersub
19
+ # define timersub(a, b, result) \
20
+ do { \
21
+ (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
22
+ (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
23
+ if ((result)->tv_usec < 0) { \
24
+ --(result)->tv_sec; \
25
+ (result)->tv_usec += 1000000; \
26
+ } \
27
+ } while (0)
28
+ #endif
29
+
30
+ double Timer::elapsed(void) const
31
+ {
32
+ struct timeval actualTime;
33
+ gettimeofday( &actualTime, NULL );
34
+
35
+ // Use timersub-define, which does overflow-handling.
36
+ struct timeval difference;
37
+ timersub( &actualTime, &m_time, &difference );
38
+ return difference.tv_sec + difference.tv_usec * 1.0E-6;
39
+ }
40
+
data/ext/timer.hh ADDED
@@ -0,0 +1,37 @@
1
+ /* HornetsEye - Computer Vision with Ruby
2
+ Copyright (C) 2006, 2007 Jan Wedekind
3
+
4
+ This program is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
+ #ifndef HORNETSEYE_TIMER_HH
17
+ #define HORNETSEYE_TIMER_HH
18
+
19
+ #include <string>
20
+ #include <boost/shared_ptr.hpp>
21
+ #include <sys/time.h>
22
+
23
+ class Timer
24
+ {
25
+ public:
26
+ Timer(void) { reset(); }
27
+ void reset(void) {
28
+ gettimeofday( &m_time, NULL );
29
+ }
30
+ double elapsed(void) const;
31
+ protected:
32
+ struct timeval m_time;
33
+ };
34
+
35
+ typedef boost::shared_ptr< Timer > TimerPtr;
36
+
37
+ #endif
data/ext/x11display.cc ADDED
@@ -0,0 +1,269 @@
1
+ /* HornetsEye - Computer Vision with Ruby
2
+ Copyright (C) 2006, 2007, 2008, 2009 Jan Wedekind
3
+
4
+ This program is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
+ #ifndef NDEBUG
17
+ #include <iostream>
18
+ #endif
19
+ #include "timer.hh"
20
+ #include "x11display.hh"
21
+ #include "x11window.hh"
22
+ #include "rubytools.hh"
23
+
24
+ using namespace std;
25
+
26
+ // bool X11Display::x11ThreadsInitialised = false;
27
+
28
+ VALUE X11Display::cRubyClass = Qnil;
29
+
30
+ X11Display::X11Display( const std::string &name ) throw (Error):
31
+ m_display(NULL), m_quit( false )
32
+ {
33
+ // if ( !x11ThreadsInitialised ) {
34
+ // XInitThreads();
35
+ // x11ThreadsInitialised = true;
36
+ // }
37
+ m_display = XOpenDisplay( name.empty() ?
38
+ (const char *)NULL : name.c_str() );
39
+ ERRORMACRO( m_display != NULL, Error, , "Error opening X11-display "
40
+ << ( name.empty() ? "$DISPLAY" : name.c_str() ) << "." );
41
+ }
42
+
43
+ void X11Display::eventLoop( int timeout, bool resetQuit )
44
+ throw (Error)
45
+ {
46
+ if ( resetQuit )
47
+ m_quit = false;
48
+
49
+ if ( timeout < INT_MAX ) {
50
+
51
+ #ifndef NDEBUG
52
+ cerr << "Looping for " << timeout << " milliseconds" << endl;
53
+ #endif
54
+
55
+ Timer t;
56
+
57
+ // Remaining time in milliseconds.
58
+ int usecs_remaining;
59
+
60
+ do {
61
+
62
+ // Check for remaining time.
63
+ usecs_remaining = (int)( ( timeout * 0.001 - t.elapsed() ) * 1E+6 );
64
+ #ifndef NDEBUG
65
+ cerr << usecs_remaining << " microseconds remaining" << endl;
66
+ #endif
67
+ if ( usecs_remaining > 0 ) {
68
+ // Initialise timeout-variable.
69
+ struct timeval tval;
70
+ tval.tv_usec = usecs_remaining % 1000000;
71
+ tval.tv_sec = usecs_remaining / 1000000;
72
+
73
+ // Get file-descriptor to event-pipe.
74
+ int fd = ConnectionNumber( m_display );
75
+
76
+ // Create file-descriptor set.
77
+ fd_set fds;
78
+ FD_ZERO( &fds );
79
+ FD_SET( fd, &fds );
80
+
81
+ // Perform limited wait on event-pipe.
82
+ select( fd + 1, &fds, NULL, NULL, &tval );
83
+ };
84
+
85
+ // Empty event-pipe.
86
+ processEvents();
87
+
88
+ } while ( !m_quit && usecs_remaining > 0 );
89
+
90
+ } else {
91
+
92
+ #ifndef NDEBUG
93
+ cerr << "Entered event loop without timeout" << endl;
94
+ #endif
95
+
96
+ while ( !m_quit ) {
97
+
98
+ XEvent event;
99
+
100
+ // Wait for events.
101
+ // XMaskEvent( m_display, mask, &event );
102
+ XNextEvent( m_display, &event );
103
+
104
+ // Process the event.
105
+ handleEvent( event );
106
+
107
+ };
108
+ };
109
+
110
+ }
111
+
112
+ X11Display::~X11Display(void)
113
+ {
114
+ #ifndef NDEBUG
115
+ cerr << "Destroying X11-display." << endl;
116
+ #endif
117
+ XCloseDisplay( m_display );
118
+ }
119
+
120
+ int X11Display::width(void)
121
+ {
122
+ return DisplayWidth( m_display, DefaultScreen( m_display ) );
123
+ }
124
+
125
+ int X11Display::height(void)
126
+ {
127
+
128
+
129
+ return DisplayHeight( m_display, DefaultScreen( m_display ) );
130
+ }
131
+
132
+ static Bool alwaysTrue( Display *, XEvent *, XPointer )
133
+ {
134
+ return True;
135
+ }
136
+
137
+ void X11Display::processEvents(void) throw (Error)
138
+ {
139
+ // Check for pending events.
140
+ XEvent event;
141
+ //while ( XCheckMaskEvent( m_display, 0xFFFF, &event ) == True ) {
142
+ // handleEvent( event );
143
+ //};
144
+ #ifndef NDEBUG
145
+ cerr << "Processing pending events" << endl;
146
+ #endif
147
+ while ( XCheckIfEvent( m_display, &event, alwaysTrue, NULL ) )
148
+ handleEvent( event );
149
+ }
150
+
151
+ void X11Display::handleEvent( XEvent &event ) throw (Error)
152
+ {
153
+ #ifndef NDEBUG
154
+ cerr << "Getting event for window " << event.xany.window << endl;
155
+ #endif
156
+ // Find affected window.
157
+ map< Window, X11Window * >::iterator entry =
158
+ m_windows.find( event.xany.window );
159
+
160
+ // Forward event.
161
+ if ( entry != m_windows.end() )
162
+ entry->second->handleEvent( event );
163
+ }
164
+
165
+ void X11Display::addWindow( X11Window *win )
166
+ {
167
+ #ifndef NDEBUG
168
+ cerr << "Registering window: " << win->get() << endl;
169
+ #endif
170
+ m_windows[ win->get() ] = win;
171
+ }
172
+
173
+ void X11Display::removeWindow( X11Window *win )
174
+ {
175
+ #ifndef NDEBUG
176
+ cerr << "Removing window: " << win->get() << endl;
177
+ #endif
178
+ m_windows.erase( win->get() );
179
+ }
180
+
181
+ VALUE X11Display::registerRubyClass( VALUE module )
182
+ {
183
+ cRubyClass = rb_define_class_under( module, "X11Display", rb_cObject );
184
+ rb_define_const( cRubyClass, "INFINITE", INT2NUM(INT_MAX) );
185
+ rb_define_singleton_method( cRubyClass, "new",
186
+ RUBY_METHOD_FUNC( wrapNew ), 1 );
187
+ rb_define_method( cRubyClass, "width",
188
+ RUBY_METHOD_FUNC( wrapWidth ), 0 );
189
+ rb_define_method( cRubyClass, "height",
190
+ RUBY_METHOD_FUNC( wrapHeight ), 0 );
191
+ rb_define_method( cRubyClass, "event_loop",
192
+ RUBY_METHOD_FUNC( wrapEventLoop ), 1 );
193
+ rb_define_method( cRubyClass, "process_events",
194
+ RUBY_METHOD_FUNC( wrapProcessEvents ), 0 );
195
+ rb_define_method( cRubyClass, "status?",
196
+ RUBY_METHOD_FUNC( wrapStatus ), 0 );
197
+ rb_define_method( cRubyClass, "status=",
198
+ RUBY_METHOD_FUNC( wrapSetStatus ), 1 );
199
+ return cRubyClass;
200
+ }
201
+
202
+ void X11Display::deleteRubyObject( void *ptr )
203
+ {
204
+ delete (X11DisplayPtr *)ptr;
205
+ }
206
+
207
+ VALUE X11Display::wrapNew( VALUE rbClass, VALUE rbName )
208
+ {
209
+ VALUE retVal = Qnil;
210
+ try {
211
+ rb_check_type( rbName, T_STRING );
212
+ X11DisplayPtr ptr( new X11Display( StringValuePtr( rbName ) ) );
213
+ retVal = Data_Wrap_Struct( rbClass, 0, X11Display::deleteRubyObject,
214
+ new X11DisplayPtr( ptr ) );
215
+ } catch ( std::exception &e ) {
216
+ rb_raise( rb_eRuntimeError, "%s", e.what() );
217
+ };
218
+ return retVal;
219
+ }
220
+
221
+ VALUE X11Display::wrapWidth( VALUE rbSelf )
222
+ {
223
+ X11DisplayPtr *self; Data_Get_Struct( rbSelf, X11DisplayPtr, self );
224
+ return INT2NUM( (*self)->width() );
225
+ }
226
+
227
+ VALUE X11Display::wrapHeight( VALUE rbSelf )
228
+ {
229
+ X11DisplayPtr *self; Data_Get_Struct( rbSelf, X11DisplayPtr, self );
230
+ return INT2NUM( (*self)->height() );
231
+ }
232
+
233
+ VALUE X11Display::wrapEventLoop( VALUE rbSelf, VALUE rbTimeout )
234
+ {
235
+ try {
236
+ X11DisplayPtr *self; Data_Get_Struct( rbSelf, X11DisplayPtr, self );
237
+ (*self)->eventLoop( NUM2INT( rbTimeout ), true );
238
+ } catch ( std::exception &e ) {
239
+ rb_raise( rb_eRuntimeError, "%s", e.what() );
240
+ };
241
+ return rbSelf;
242
+ }
243
+
244
+ VALUE X11Display::wrapProcessEvents( VALUE rbSelf )
245
+ {
246
+ try {
247
+ X11DisplayPtr *self; Data_Get_Struct( rbSelf, X11DisplayPtr, self );
248
+ (*self)->processEvents();
249
+ } catch ( std::exception &e ) {
250
+ rb_raise( rb_eRuntimeError, "%s", e.what() );
251
+ };
252
+ return rbSelf;
253
+ }
254
+
255
+ VALUE X11Display::wrapStatus( VALUE rbSelf )
256
+ {
257
+ X11DisplayPtr *self; Data_Get_Struct( rbSelf, X11DisplayPtr, self );
258
+ bool retVal = !(*self)->quit();
259
+ VALUE rbRetVal = retVal ? Qtrue : Qfalse;
260
+ return rbRetVal;
261
+ }
262
+
263
+ VALUE X11Display::wrapSetStatus( VALUE rbSelf, VALUE rbStatus )
264
+ {
265
+ X11DisplayPtr *self; Data_Get_Struct( rbSelf, X11DisplayPtr, self );
266
+ (*self)->setQuit( rbStatus == Qfalse );
267
+ return rbSelf;
268
+ }
269
+
data/ext/x11display.hh ADDED
@@ -0,0 +1,64 @@
1
+ /* HornetsEye - Computer Vision with Ruby
2
+ Copyright (C) 2006, 2007, 2008, 2009 Jan Wedekind
3
+
4
+ This program is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
+ #ifndef HORNETSEYE_X11DISPLAY_HH
17
+ #define HORNETSEYE_X11DISPLAY_HH
18
+
19
+ #include <boost/shared_ptr.hpp>
20
+ #include <map>
21
+ #include "rubyinc.hh"
22
+ #include <X11/Xlib.h>
23
+ #include <X11/Xutil.h>
24
+ #include "error.hh"
25
+
26
+ class X11Window;
27
+
28
+ class X11Display
29
+ {
30
+ public:
31
+ X11Display( const std::string &name = "" )
32
+ throw (Error);
33
+ virtual ~X11Display(void);
34
+ Display *get(void) { return m_display; }
35
+ int width(void);
36
+ int height(void);
37
+ void eventLoop( int timeout = INT_MAX, bool resetQuit = false )
38
+ throw (Error);
39
+ void processEvents(void)
40
+ throw (Error);
41
+ void addWindow( X11Window *win );
42
+ void removeWindow( X11Window *win );
43
+ void setQuit( bool quit ) { m_quit = quit; }
44
+ bool quit(void) { return m_quit; }
45
+ static VALUE cRubyClass;
46
+ static VALUE registerRubyClass( VALUE module );
47
+ static void deleteRubyObject( void *ptr );
48
+ static VALUE wrapNew( VALUE rbClass, VALUE rbName );
49
+ static VALUE wrapWidth( VALUE rbSelf );
50
+ static VALUE wrapHeight( VALUE rbSelf );
51
+ static VALUE wrapEventLoop( VALUE rbSelf, VALUE rbTimeout );
52
+ static VALUE wrapProcessEvents( VALUE rbSelf );
53
+ static VALUE wrapStatus( VALUE rbSelf );
54
+ static VALUE wrapSetStatus( VALUE rbSelf, VALUE rbStatus );
55
+ protected:
56
+ void handleEvent( XEvent &event ) throw (Error);
57
+ Display *m_display;
58
+ std::map< Window, X11Window * > m_windows;
59
+ bool m_quit;
60
+ };
61
+
62
+ typedef boost::shared_ptr< X11Display > X11DisplayPtr;
63
+
64
+ #endif