| 1 |
#include <sys/time.h> |
| 2 |
|
| 3 |
#include "allegro5/allegro.h" |
| 4 |
#include "allegro5/platform/aintunix.h" |
| 5 |
#include "allegro5/internal/aintern_x.h" |
| 6 |
#include "allegro5/internal/aintern_xclipboard.h" |
| 7 |
#include "allegro5/internal/aintern_xdisplay.h" |
| 8 |
#include "allegro5/internal/aintern_xembed.h" |
| 9 |
#include "allegro5/internal/aintern_xevents.h" |
| 10 |
#include "allegro5/internal/aintern_xfullscreen.h" |
| 11 |
#include "allegro5/internal/aintern_xkeyboard.h" |
| 12 |
#include "allegro5/internal/aintern_xmouse.h" |
| 13 |
#include "allegro5/internal/aintern_xsystem.h" |
| 14 |
#include "allegro5/internal/aintern_xtouch.h" |
| 15 |
#include "allegro5/internal/aintern_xdnd.h" |
| 16 |
|
| 17 |
#ifdef ALLEGRO_RASPBERRYPI |
| 18 |
#include "allegro5/internal/aintern_raspberrypi.h" |
| 19 |
#define ALLEGRO_SYSTEM_XGLX ALLEGRO_SYSTEM_RASPBERRYPI |
| 20 |
#define ALLEGRO_DISPLAY_XGLX ALLEGRO_DISPLAY_RASPBERRYPI |
| 21 |
#endif |
| 22 |
|
| 23 |
ALLEGRO_DEBUG_CHANNEL("xevents") |
| 24 |
|
| 25 |
/* Handle an X11 close button event. [X11 thread] |
| 26 |
* Only called from the event handler with the system locked. |
| 27 |
*/ |
| 28 |
void _al_display_xglx_closebutton(ALLEGRO_DISPLAY *d, XEvent *xevent) |
| 29 |
{ |
| 30 |
ALLEGRO_EVENT_SOURCE *es = &d->es; |
| 31 |
(void)xevent; |
| 32 |
|
| 33 |
_al_event_source_lock(es); |
| 34 |
if (_al_event_source_needs_to_generate_event(es)) { |
| 35 |
ALLEGRO_EVENT event; |
| 36 |
event.display.type = ALLEGRO_EVENT_DISPLAY_CLOSE; |
| 37 |
event.display.timestamp = al_get_time(); |
| 38 |
_al_event_source_emit_event(es, &event); |
| 39 |
} |
| 40 |
_al_event_source_unlock(es); |
| 41 |
} |
| 42 |
|
| 43 |
static void process_x11_event(ALLEGRO_SYSTEM_XGLX *s, XEvent event) |
| 44 |
{ |
| 45 |
unsigned int i; |
| 46 |
ALLEGRO_DISPLAY_XGLX *d = NULL; |
| 47 |
|
| 48 |
/* With many windows, it's bad to loop through them all, but typically |
| 49 |
* we have one or at most two or so. |
| 50 |
*/ |
| 51 |
for (i = 0; i < _al_vector_size(&s->system.displays); i++) { |
| 52 |
ALLEGRO_DISPLAY_XGLX **dptr = _al_vector_ref(&s->system.displays, i); |
| 53 |
d = *dptr; |
| 54 |
if (d->window == event.xany.window) { |
| 55 |
break; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
if (!d) { |
| 60 |
/* The display was probably destroyed already. */ |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
switch (event.type) { |
| 65 |
case KeyPress: |
| 66 |
_al_xwin_keyboard_handler(&event.xkey, &d->display); |
| 67 |
break; |
| 68 |
case KeyRelease: |
| 69 |
_al_xwin_keyboard_handler(&event.xkey, &d->display); |
| 70 |
break; |
| 71 |
case MotionNotify: |
| 72 |
_al_xwin_mouse_motion_notify_handler( |
| 73 |
event.xmotion.x, event.xmotion.y, &d->display); |
| 74 |
break; |
| 75 |
case ButtonPress: |
| 76 |
_al_xwin_mouse_button_press_handler(event.xbutton.button, |
| 77 |
&d->display); |
| 78 |
break; |
| 79 |
case ButtonRelease: |
| 80 |
_al_xwin_mouse_button_release_handler(event.xbutton.button, |
| 81 |
&d->display); |
| 82 |
break; |
| 83 |
case ClientMessage: |
| 84 |
if (event.xclient.message_type == s->AllegroAtom) { |
| 85 |
d->mouse_warp = true; |
| 86 |
break; |
| 87 |
} |
| 88 |
if (d->wm_delete_window_atom != None && |
| 89 |
(Atom)event.xclient.data.l[0] == d->wm_delete_window_atom) |
| 90 |
{ |
| 91 |
_al_display_xglx_closebutton(&d->display, &event); |
| 92 |
break; |
| 93 |
} |
| 94 |
if (_al_display_xglx_handle_drag_and_drop(s, d, &event)) { |
| 95 |
break; |
| 96 |
} |
| 97 |
#ifndef ALLEGRO_RASPBERRYPI |
| 98 |
if (event.xclient.message_type == s->XEmbedAtom) { |
| 99 |
const long xtime = event.xclient.data.l[0]; |
| 100 |
const long major = event.xclient.data.l[1]; |
| 101 |
const long detail = event.xclient.data.l[2]; |
| 102 |
const long data1 = event.xclient.data.l[3]; |
| 103 |
const long data2 = event.xclient.data.l[4]; |
| 104 |
|
| 105 |
(void)xtime; |
| 106 |
(void)detail; |
| 107 |
(void)data2; |
| 108 |
|
| 109 |
switch (major) { |
| 110 |
case XEMBED_EMBEDDED_NOTIFY: |
| 111 |
d->embedder_window = data1; |
| 112 |
ALLEGRO_INFO("XEmbed begin: embedder window = %ld\n", data1); |
| 113 |
break; |
| 114 |
case XEMBED_FOCUS_IN: |
| 115 |
ALLEGRO_DEBUG("XEmbed focus in\n"); |
| 116 |
_al_xwin_display_switch_handler_inner(&d->display, true); |
| 117 |
_al_xwin_keyboard_switch_handler(&d->display, true); |
| 118 |
break; |
| 119 |
case XEMBED_FOCUS_OUT: |
| 120 |
ALLEGRO_DEBUG("XEmbed focus out\n"); |
| 121 |
_al_xwin_display_switch_handler_inner(&d->display, false); |
| 122 |
_al_xwin_keyboard_switch_handler(&d->display, false); |
| 123 |
break; |
| 124 |
} |
| 125 |
break; |
| 126 |
} |
| 127 |
break; |
| 128 |
case EnterNotify: |
| 129 |
_al_xwin_mouse_switch_handler(&d->display, &event.xcrossing); |
| 130 |
break; |
| 131 |
case LeaveNotify: |
| 132 |
_al_xwin_mouse_switch_handler(&d->display, &event.xcrossing); |
| 133 |
break; |
| 134 |
case FocusIn: |
| 135 |
_al_xwin_display_switch_handler(&d->display, &event.xfocus); |
| 136 |
_al_xwin_keyboard_switch_handler(&d->display, true); |
| 137 |
break; |
| 138 |
case FocusOut: |
| 139 |
_al_xwin_display_switch_handler(&d->display, &event.xfocus); |
| 140 |
_al_xwin_keyboard_switch_handler(&d->display, false); |
| 141 |
break; |
| 142 |
case ConfigureNotify: |
| 143 |
_al_xglx_display_configure_event(&d->display, &event); |
| 144 |
d->resize_count++; |
| 145 |
_al_cond_signal(&s->resized); |
| 146 |
break; |
| 147 |
case MapNotify: |
| 148 |
d->display.flags &= ~ALLEGRO_MINIMIZED; |
| 149 |
d->is_mapped = true; |
| 150 |
_al_cond_signal(&d->mapped); |
| 151 |
break; |
| 152 |
case UnmapNotify: |
| 153 |
d->display.flags |= ALLEGRO_MINIMIZED; |
| 154 |
break; |
| 155 |
case Expose: |
| 156 |
if (d->display.flags & ALLEGRO_GENERATE_EXPOSE_EVENTS) { |
| 157 |
_al_xwin_display_expose(&d->display, &event.xexpose); |
| 158 |
} |
| 159 |
break; |
| 160 |
case ReparentNotify: |
| 161 |
if (event.xreparent.parent == RootWindow(s->x11display, d->xscreen)) { |
| 162 |
ALLEGRO_INFO("XEmbed protocol finished.\n"); |
| 163 |
d->embedder_window = None; |
| 164 |
} |
| 165 |
break; |
| 166 |
|
| 167 |
case SelectionNotify: |
| 168 |
if (_al_display_xglx_handle_drag_and_drop_selection(s, d, &event)) |
| 169 |
break; |
| 170 |
_al_xwin_display_selection_notify(&d->display, &event.xselection); |
| 171 |
d->is_selectioned = true; |
| 172 |
_al_cond_signal(&d->selectioned); |
| 173 |
break; |
| 174 |
|
| 175 |
case SelectionRequest: |
| 176 |
_al_xwin_display_selection_request(&d->display, &event.xselectionrequest); |
| 177 |
break; |
| 178 |
|
| 179 |
default: |
| 180 |
_al_x_handle_touch_event(s, d, &event); |
| 181 |
_al_xglx_handle_mmon_event(s, d, &event); |
| 182 |
break; |
| 183 |
#endif |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
void _al_xwin_background_thread(_AL_THREAD *self, void *arg) |
| 188 |
{ |
| 189 |
ALLEGRO_SYSTEM_XGLX *s = arg; |
| 190 |
XEvent event; |
| 191 |
double last_reset_screensaver_time = 0.0; |
| 192 |
|
| 193 |
while (!_al_get_thread_should_stop(self)) { |
| 194 |
/* Note: |
| 195 |
* Most older X11 implementations are not thread-safe no matter what, so |
| 196 |
* we simply cannot sit inside a blocking XNextEvent from another thread |
| 197 |
* if another thread also uses X11 functions. |
| 198 |
* |
| 199 |
* The usual use of XNextEvent is to only call it from the main thread. We |
| 200 |
* could of course do this for A5, just needs some slight adjustments to |
| 201 |
* the events system (polling for an Allegro event would call a function |
| 202 |
* of the system driver). |
| 203 |
* |
| 204 |
* As an alternative, we can use locking. This however can never fully |
| 205 |
* work, as for example OpenGL implementations also will access X11, in a |
| 206 |
* way we cannot know and cannot control (and we can't require users to |
| 207 |
* only call graphics functions inside a lock). |
| 208 |
* |
| 209 |
* However, most X11 implementations are somewhat thread safe, and do |
| 210 |
* use locking quite a bit themselves, so locking mostly does work. |
| 211 |
* |
| 212 |
* (Yet another alternative might be to use a separate X11 display |
| 213 |
* connection for graphics output.) |
| 214 |
* |
| 215 |
*/ |
| 216 |
|
| 217 |
_al_mutex_lock(&s->lock); |
| 218 |
|
| 219 |
while (XEventsQueued(s->x11display, QueuedAfterFlush)) { |
| 220 |
XNextEvent(s->x11display, &event); |
| 221 |
process_x11_event(s, event); |
| 222 |
} |
| 223 |
|
| 224 |
/* The Xlib manual is particularly useless about the XResetScreenSaver() |
| 225 |
* function. Nevertheless, this does seem to work to inhibit the native |
| 226 |
* screensaver facility. Probably it won't do anything for other |
| 227 |
* systems, though. |
| 228 |
*/ |
| 229 |
if (!s->screen_saver_query_available && s->inhibit_screensaver) { |
| 230 |
double now = al_get_time(); |
| 231 |
if (now - last_reset_screensaver_time > 10.0) { |
| 232 |
XResetScreenSaver(s->x11display); |
| 233 |
last_reset_screensaver_time = now; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
_al_mutex_unlock(&s->lock); |
| 238 |
|
| 239 |
/* If no X11 events are there, unlock so other threads can run. We use |
| 240 |
* a select call to wake up when as soon as anything is available on |
| 241 |
* the X11 connection - and just for safety also wake up 10 times |
| 242 |
* a second regardless. |
| 243 |
*/ |
| 244 |
int x11_fd = ConnectionNumber(s->x11display); |
| 245 |
fd_set fdset; |
| 246 |
FD_ZERO(&fdset); |
| 247 |
FD_SET(x11_fd, &fdset); |
| 248 |
struct timeval small_time = {0, 100000}; /* 10 times a second */ |
| 249 |
select(x11_fd + 1, &fdset, NULL, NULL, &small_time); |
| 250 |
} |
| 251 |
} |