added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / fxwt / fxwt_x.cpp
1 /*
2 This file is part of fxwt, the window system toolkit of 3dengfx.
3
4 Copyright (c) 2004, 2005 John Tsiombikas <nuclear@siggraph.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 /* main fxwt event handling and system abstraction.
22  *
23  * Author: John Tsiombikas 2004
24  */
25
26 #include "3dengfx_config.h"
27
28 #if GFX_LIBRARY == NATIVE && NATIVE_LIB == NATIVE_X11
29
30 #include "gfx_library.h"
31 #include "fxwt.hpp"
32 #include "3dengfx/3denginefx.hpp"
33 #include "common/err_msg.h"
34
35 #ifdef __unix__
36 #include <unistd.h>
37 #ifdef _POSIX_PRIORITY_SCHEDULING
38 #include <sched.h>
39 #endif  // _POSIX_PRIORITY_SCHEDULING
40 #endif  // __unix__
41
42 using std::list;
43 using namespace fxwt;
44
45 static void handle_event(const XEvent &event);
46
47 extern Display *fxwt_x_dpy;
48 extern ::Window fxwt_x_win;
49
50 Vector2 fxwt::get_mouse_pos_normalized() {
51         return Vector2(0, 0);
52 }
53
54 void fxwt::set_window_title(const char *title) {
55         XTextProperty tp_wname;
56         XStringListToTextProperty((char**)&title, 1, &tp_wname);
57         XSetWMName(fxwt_x_dpy, fxwt_x_win, &tp_wname);
58         XFree(tp_wname.value);
59 }
60
61 void fxwt::swap_buffers() {
62         glXSwapBuffers(fxwt_x_dpy, fxwt_x_win);
63 #ifdef _POSIX_PRIORITY_SCHEDULING
64         sched_yield();
65 #endif
66 }
67
68 int fxwt::main_loop() {
69         set_verbosity(3);
70
71         while(1) {
72                 if(!idle_handlers.empty()) {
73                         while(XPending(fxwt_x_dpy)) {
74                                 XEvent event;
75                                 XNextEvent(fxwt_x_dpy, &event);
76                                 handle_event(event);
77                         }
78                         
79                         list<void (*)()>::iterator iter = idle_handlers.begin();
80                         while(iter != idle_handlers.end()) {
81                                 (*iter++)();
82                         }
83                 } else {
84                         XEvent event;
85                         XNextEvent(fxwt_x_dpy, &event);
86                         handle_event(event);
87                 }
88         }
89
90         return 0;
91 }
92
93 static void handle_event(const XEvent &event) {
94         static int window_mapped;
95
96         switch(event.type) {
97         case MapNotify:
98                 window_mapped = 1;
99                 break;
100
101         case UnmapNotify:
102                 window_mapped = 0;
103                 break;
104
105         case Expose:
106                 if(window_mapped && event.xexpose.count == 0) {
107                         list<void (*)()>::iterator iter = disp_handlers.begin();
108                         while(iter != disp_handlers.end()) {
109                                 (*iter++)();
110                         }
111                 }
112                 break;
113
114         case ClientMessage:
115                 {
116                         char *atom_name = XGetAtomName(fxwt_x_dpy, event.xclient.message_type);
117                         if(!strcmp(atom_name, "WM_PROTOCOLS")) {
118                                 XFree(atom_name);
119                                 exit(0);
120                         }
121                         XFree(atom_name);
122                 }
123                 break;
124
125         case KeyPress:
126                 {
127                         KeySym keysym = XLookupKeysym((XKeyEvent*)&event.xkey, 0);
128                         //key_state[keysym] = 1;
129
130                         list<void (*)(int)>::iterator iter = keyb_handlers.begin();
131                         while(iter != keyb_handlers.end()) {
132                                 (*iter++)(keysym & 0xff);
133                         }
134                 }
135                 break;
136
137         case MotionNotify:
138                 {
139                         list<void (*)(int, int)>::iterator iter = motion_handlers.begin();
140                         while(iter != motion_handlers.end()) {
141                                 (*iter++)(event.xmotion.x, event.xmotion.y);
142                         }
143                 }
144                 break;
145
146
147         case ButtonPress:
148                 {
149                         bool state;
150                         if(1) {
151                                 state = true;
152                         } else {
153         case ButtonRelease:
154                                 state = false;
155                         }
156                         button_state[event.xbutton.button] = state;
157                         
158                         list<void (*)(int, int, int, int)>::iterator iter = button_handlers.begin();
159                         while(iter != button_handlers.end()) {
160                                 (*iter++)(event.xbutton.button, state, event.xbutton.x, event.xbutton.y);
161                         }
162                 }
163                 break;
164
165         default:
166                 break;
167         }
168 }
169
170 #endif  // GFX_LIBRARY == NATIVE && NATIVE_LIB == NATIVE_X11