| 1 |
#ifdef DEBUG_X11 |
| 2 |
extern int _Xdebug; /* part of Xlib */ |
| 3 |
#endif |
| 4 |
|
| 5 |
#include <sys/time.h> |
| 6 |
#include <math.h> |
| 7 |
|
| 8 |
#include "allegro5/allegro.h" |
| 9 |
#include "allegro5/internal/aintern.h" |
| 10 |
#include "allegro5/internal/aintern_bitmap.h" |
| 11 |
#include "allegro5/internal/aintern_x.h" |
| 12 |
#include "allegro5/internal/aintern_xcursor.h" |
| 13 |
#include "allegro5/internal/aintern_xembed.h" |
| 14 |
#include "allegro5/internal/aintern_xevents.h" |
| 15 |
#include "allegro5/internal/aintern_xfullscreen.h" |
| 16 |
#include "allegro5/internal/aintern_xkeyboard.h" |
| 17 |
#include "allegro5/internal/aintern_xmouse.h" |
| 18 |
#include "allegro5/internal/aintern_xsystem.h" |
| 19 |
#include "allegro5/internal/aintern_xdnd.h" |
| 20 |
#include "allegro5/platform/aintunix.h" |
| 21 |
#include "allegro5/platform/aintxglx.h" |
| 22 |
|
| 23 |
#include "allegro5/allegro_x.h" |
| 24 |
|
| 25 |
#include "xicon.h" |
| 26 |
|
| 27 |
ALLEGRO_DEBUG_CHANNEL("system") |
| 28 |
|
| 29 |
static ALLEGRO_SYSTEM_INTERFACE *xglx_vt; |
| 30 |
|
| 31 |
ALLEGRO_BITMAP *_al_xwin_initial_icon = NULL; |
| 32 |
|
| 33 |
static bool xglx_inhibit_screensaver(bool inhibit); |
| 34 |
|
| 35 |
|
| 36 |
/* Function: al_x_set_initial_icon |
| 37 |
*/ |
| 38 |
bool al_x_set_initial_icon(ALLEGRO_BITMAP *bitmap) |
| 39 |
{ |
| 40 |
ALLEGRO_STATE state; |
| 41 |
al_store_state(&state, ALLEGRO_STATE_NEW_BITMAP_PARAMETERS); |
| 42 |
al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); |
| 43 |
al_destroy_bitmap(_al_xwin_initial_icon); |
| 44 |
_al_xwin_initial_icon = al_clone_bitmap(bitmap); |
| 45 |
al_restore_state(&state); |
| 46 |
return true; |
| 47 |
} |
| 48 |
|
| 49 |
static ALLEGRO_SYSTEM *xglx_initialize(int flags) |
| 50 |
{ |
| 51 |
Display *x11display; |
| 52 |
Display *gfxdisplay; |
| 53 |
ALLEGRO_SYSTEM_XGLX *s; |
| 54 |
|
| 55 |
(void)flags; |
| 56 |
|
| 57 |
#ifdef DEBUG_X11 |
| 58 |
_Xdebug = 1; |
| 59 |
#endif |
| 60 |
|
| 61 |
XInitThreads(); |
| 62 |
|
| 63 |
/* Get an X11 display handle. */ |
| 64 |
x11display = XOpenDisplay(0); |
| 65 |
if (x11display) { |
| 66 |
/* Never ask. */ |
| 67 |
gfxdisplay = XOpenDisplay(0); |
| 68 |
if (!gfxdisplay) { |
| 69 |
ALLEGRO_ERROR("XOpenDisplay failed second time.\n"); |
| 70 |
XCloseDisplay(x11display); |
| 71 |
return NULL; |
| 72 |
} |
| 73 |
} |
| 74 |
else { |
| 75 |
ALLEGRO_INFO("XOpenDisplay failed; assuming headless mode.\n"); |
| 76 |
gfxdisplay = NULL; |
| 77 |
} |
| 78 |
|
| 79 |
_al_unix_init_time(); |
| 80 |
|
| 81 |
s = al_calloc(1, sizeof *s); |
| 82 |
|
| 83 |
_al_mutex_init_recursive(&s->lock); |
| 84 |
_al_cond_init(&s->resized); |
| 85 |
s->inhibit_screensaver = false; |
| 86 |
s->screen_saver_query_available = false; |
| 87 |
|
| 88 |
_al_vector_init(&s->system.displays, sizeof (ALLEGRO_DISPLAY_XGLX *)); |
| 89 |
|
| 90 |
s->system.vt = xglx_vt; |
| 91 |
|
| 92 |
s->gfxdisplay = gfxdisplay; |
| 93 |
s->x11display = x11display; |
| 94 |
|
| 95 |
if (s->x11display) { |
| 96 |
ALLEGRO_INFO("XGLX driver connected to X11 (%s %d).\n", |
| 97 |
ServerVendor(s->x11display), VendorRelease(s->x11display)); |
| 98 |
ALLEGRO_INFO("X11 protocol version %d.%d.\n", |
| 99 |
ProtocolVersion(s->x11display), ProtocolRevision(s->x11display)); |
| 100 |
|
| 101 |
/* We need to put *some* atom into the ClientMessage we send for |
| 102 |
* faking mouse movements with al_set_mouse_xy - so let's ask X11 |
| 103 |
* for one here. |
| 104 |
*/ |
| 105 |
s->AllegroAtom = XInternAtom(x11display, "AllegroAtom", False); |
| 106 |
|
| 107 |
/* Message type for XEmbed protocol. */ |
| 108 |
s->XEmbedAtom = XInternAtom(x11display, "_XEMBED", False); |
| 109 |
|
| 110 |
_al_display_xglx_init_dnd_atoms(s); |
| 111 |
|
| 112 |
_al_thread_create(&s->xevents_thread, _al_xwin_background_thread, s); |
| 113 |
s->have_xevents_thread = true; |
| 114 |
ALLEGRO_INFO("events thread spawned.\n"); |
| 115 |
} |
| 116 |
|
| 117 |
const char *binding = al_get_config_value(al_get_system_config(), |
| 118 |
"keyboard", "toggle_mouse_grab_key"); |
| 119 |
if (binding) { |
| 120 |
s->toggle_mouse_grab_keycode = _al_parse_key_binding(binding, |
| 121 |
&s->toggle_mouse_grab_modifiers); |
| 122 |
if (s->toggle_mouse_grab_keycode) { |
| 123 |
ALLEGRO_DEBUG("Toggle mouse grab key: '%s'\n", binding); |
| 124 |
} |
| 125 |
else { |
| 126 |
ALLEGRO_WARN("Cannot parse key binding '%s'\n", binding); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
return &s->system; |
| 131 |
} |
| 132 |
|
| 133 |
static void xglx_shutdown_system(void) |
| 134 |
{ |
| 135 |
ALLEGRO_SYSTEM *s = al_get_system_driver(); |
| 136 |
ALLEGRO_SYSTEM_XGLX *sx = (void *)s; |
| 137 |
|
| 138 |
ALLEGRO_INFO("shutting down.\n"); |
| 139 |
|
| 140 |
if (sx->have_xevents_thread) { |
| 141 |
_al_thread_join(&sx->xevents_thread); |
| 142 |
sx->have_xevents_thread = false; |
| 143 |
} |
| 144 |
|
| 145 |
/* Close all open displays. */ |
| 146 |
while (_al_vector_size(&s->displays) > 0) { |
| 147 |
ALLEGRO_DISPLAY **dptr = _al_vector_ref(&s->displays, 0); |
| 148 |
ALLEGRO_DISPLAY *d = *dptr; |
| 149 |
al_destroy_display(d); |
| 150 |
} |
| 151 |
_al_vector_free(&s->displays); |
| 152 |
|
| 153 |
if (sx->inhibit_screensaver) { |
| 154 |
xglx_inhibit_screensaver(false); |
| 155 |
} |
| 156 |
|
| 157 |
// Makes sure we wait for any commands sent to the X server when destroying the displays. |
| 158 |
// Should make sure we don't shutdown before modes are restored. |
| 159 |
if (sx->x11display) { |
| 160 |
XSync(sx->x11display, False); |
| 161 |
} |
| 162 |
|
| 163 |
_al_xsys_mmon_exit(sx); |
| 164 |
|
| 165 |
if (sx->x11display) { |
| 166 |
XCloseDisplay(sx->x11display); |
| 167 |
sx->x11display = None; |
| 168 |
ALLEGRO_DEBUG("xsys: close x11display.\n"); |
| 169 |
} |
| 170 |
|
| 171 |
if (sx->gfxdisplay) { |
| 172 |
XCloseDisplay(sx->gfxdisplay); |
| 173 |
sx->gfxdisplay = None; |
| 174 |
} |
| 175 |
|
| 176 |
al_free(sx); |
| 177 |
} |
| 178 |
|
| 179 |
static ALLEGRO_DISPLAY_INTERFACE *xglx_get_display_driver(void) |
| 180 |
{ |
| 181 |
ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver(); |
| 182 |
_al_mutex_lock(&system->lock); |
| 183 |
/* _al_display_xglx_driver has global state. */ |
| 184 |
ALLEGRO_DISPLAY_INTERFACE *driver = _al_display_xglx_driver(); |
| 185 |
_al_mutex_unlock(&system->lock); |
| 186 |
return driver; |
| 187 |
} |
| 188 |
|
| 189 |
static ALLEGRO_KEYBOARD_DRIVER *xglx_get_keyboard_driver(void) |
| 190 |
{ |
| 191 |
return _al_xwin_keyboard_driver(); |
| 192 |
} |
| 193 |
|
| 194 |
static ALLEGRO_MOUSE_DRIVER *xglx_get_mouse_driver(void) |
| 195 |
{ |
| 196 |
return _al_xwin_mouse_driver(); |
| 197 |
} |
| 198 |
|
| 199 |
static ALLEGRO_JOYSTICK_DRIVER *xglx_get_joystick_driver(void) |
| 200 |
{ |
| 201 |
return _al_joystick_driver_list[0].driver; |
| 202 |
} |
| 203 |
|
| 204 |
static ALLEGRO_HAPTIC_DRIVER *xglx_get_haptic_driver(void) |
| 205 |
{ |
| 206 |
return _al_haptic_driver_list[0].driver; |
| 207 |
} |
| 208 |
|
| 209 |
static ALLEGRO_TOUCH_INPUT_DRIVER *xglx_get_touch_driver(void) |
| 210 |
{ |
| 211 |
return _al_touch_input_driver_list[0].driver; |
| 212 |
} |
| 213 |
|
| 214 |
static int xglx_get_num_video_adapters(void) |
| 215 |
{ |
| 216 |
ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver(); |
| 217 |
|
| 218 |
return _al_xglx_get_num_video_adapters(system); |
| 219 |
} |
| 220 |
|
| 221 |
static bool xglx_get_monitor_info(int adapter, ALLEGRO_MONITOR_INFO *info) |
| 222 |
{ |
| 223 |
ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver(); |
| 224 |
|
| 225 |
return _al_xglx_get_monitor_info(system, adapter, info); |
| 226 |
} |
| 227 |
|
| 228 |
static bool xglx_get_cursor_position(int *ret_x, int *ret_y) |
| 229 |
{ |
| 230 |
ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver(); |
| 231 |
Window root = RootWindow(system->x11display, 0); |
| 232 |
Window child; |
| 233 |
int wx, wy; |
| 234 |
unsigned int mask; |
| 235 |
|
| 236 |
_al_mutex_lock(&system->lock); |
| 237 |
XQueryPointer(system->x11display, root, &root, &child, ret_x, ret_y, |
| 238 |
&wx, &wy, &mask); |
| 239 |
_al_mutex_unlock(&system->lock); |
| 240 |
return true; |
| 241 |
} |
| 242 |
|
| 243 |
static bool xglx_inhibit_screensaver(bool inhibit) |
| 244 |
{ |
| 245 |
ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver(); |
| 246 |
int temp, version_min, version_max; |
| 247 |
|
| 248 |
#ifdef ALLEGRO_XWINDOWS_WITH_XSCREENSAVER |
| 249 |
if (!XScreenSaverQueryExtension(system->x11display, &temp, &temp) || |
| 250 |
!XScreenSaverQueryVersion(system->x11display, &version_max, &version_min) || |
| 251 |
version_max < 1 || (version_max == 1 && version_min < 1)) { |
| 252 |
system->screen_saver_query_available = false; |
| 253 |
} |
| 254 |
else { |
| 255 |
system->screen_saver_query_available = true; |
| 256 |
|
| 257 |
/* X11 maintains a counter on the number of identical calls to |
| 258 |
* XScreenSaverSuspend for a given display. So, only call it if 'inhibit' is |
| 259 |
* different to the previous invocation; otherwise we'd need to call it an |
| 260 |
* identical number of times with the negated value if there were a change. |
| 261 |
*/ |
| 262 |
if (inhibit != system->inhibit_screensaver) { |
| 263 |
XScreenSaverSuspend(system->x11display, inhibit); |
| 264 |
} |
| 265 |
} |
| 266 |
#else |
| 267 |
(void)temp; |
| 268 |
(void)version_min; |
| 269 |
(void)version_max; |
| 270 |
#endif |
| 271 |
|
| 272 |
system->inhibit_screensaver = inhibit; |
| 273 |
return true; |
| 274 |
} |
| 275 |
|
| 276 |
static int xglx_get_num_display_modes(void) |
| 277 |
{ |
| 278 |
int adapter = al_get_new_display_adapter(); |
| 279 |
ALLEGRO_SYSTEM_XGLX *s = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver(); |
| 280 |
|
| 281 |
return _al_xglx_get_num_display_modes(s, adapter); |
| 282 |
} |
| 283 |
|
| 284 |
static ALLEGRO_DISPLAY_MODE *xglx_get_display_mode(int mode, ALLEGRO_DISPLAY_MODE *dm) |
| 285 |
{ |
| 286 |
int adapter = al_get_new_display_adapter(); |
| 287 |
ALLEGRO_SYSTEM_XGLX *s = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver(); |
| 288 |
|
| 289 |
return _al_xglx_get_display_mode(s, adapter, mode, dm); |
| 290 |
} |
| 291 |
|
| 292 |
static int xglx_get_monitor_dpi(int adapter) |
| 293 |
{ |
| 294 |
ALLEGRO_MONITOR_INFO info; |
| 295 |
ALLEGRO_SYSTEM_XGLX *s = (void *)al_get_system_driver(); |
| 296 |
int x2_mm; |
| 297 |
int y2_mm; |
| 298 |
int dpi_hori; |
| 299 |
int dpi_vert; |
| 300 |
|
| 301 |
if(!_al_xglx_get_monitor_info(s, adapter, &info)) { |
| 302 |
return 0; |
| 303 |
} |
| 304 |
|
| 305 |
x2_mm = DisplayWidthMM(s->x11display, DefaultScreen(s->x11display)); |
| 306 |
y2_mm = DisplayHeightMM(s->x11display, DefaultScreen(s->x11display)); |
| 307 |
|
| 308 |
dpi_hori = (info.x2 - info.x1) / (_AL_INCHES_PER_MM * x2_mm); |
| 309 |
dpi_vert = (info.y2 - info.y1) / (_AL_INCHES_PER_MM * y2_mm); |
| 310 |
|
| 311 |
return sqrt(dpi_hori * dpi_vert); |
| 312 |
} |
| 313 |
|
| 314 |
/* Internal function to get a reference to this driver. */ |
| 315 |
ALLEGRO_SYSTEM_INTERFACE *_al_system_xglx_driver(void) |
| 316 |
{ |
| 317 |
if (xglx_vt) |
| 318 |
return xglx_vt; |
| 319 |
|
| 320 |
xglx_vt = al_calloc(1, sizeof *xglx_vt); |
| 321 |
|
| 322 |
xglx_vt->id = ALLEGRO_SYSTEM_ID_XGLX; |
| 323 |
xglx_vt->initialize = xglx_initialize; |
| 324 |
xglx_vt->get_display_driver = xglx_get_display_driver; |
| 325 |
xglx_vt->get_keyboard_driver = xglx_get_keyboard_driver; |
| 326 |
xglx_vt->get_mouse_driver = xglx_get_mouse_driver; |
| 327 |
xglx_vt->get_joystick_driver = xglx_get_joystick_driver; |
| 328 |
xglx_vt->get_haptic_driver = xglx_get_haptic_driver; |
| 329 |
xglx_vt->get_touch_input_driver = xglx_get_touch_driver; |
| 330 |
xglx_vt->get_num_display_modes = xglx_get_num_display_modes; |
| 331 |
xglx_vt->get_display_mode = xglx_get_display_mode; |
| 332 |
xglx_vt->shutdown_system = xglx_shutdown_system; |
| 333 |
xglx_vt->get_num_video_adapters = xglx_get_num_video_adapters; |
| 334 |
xglx_vt->get_monitor_info = xglx_get_monitor_info; |
| 335 |
xglx_vt->get_monitor_dpi = xglx_get_monitor_dpi; |
| 336 |
xglx_vt->create_mouse_cursor = _al_xwin_create_mouse_cursor; |
| 337 |
xglx_vt->destroy_mouse_cursor = _al_xwin_destroy_mouse_cursor; |
| 338 |
xglx_vt->get_cursor_position = xglx_get_cursor_position; |
| 339 |
xglx_vt->grab_mouse = _al_xwin_grab_mouse; |
| 340 |
xglx_vt->ungrab_mouse = _al_xwin_ungrab_mouse; |
| 341 |
xglx_vt->get_path = _al_unix_get_path; |
| 342 |
xglx_vt->inhibit_screensaver = xglx_inhibit_screensaver; |
| 343 |
xglx_vt->get_time = _al_unix_get_time; |
| 344 |
xglx_vt->rest = _al_unix_rest; |
| 345 |
xglx_vt->init_timeout = _al_unix_init_timeout; |
| 346 |
|
| 347 |
return xglx_vt; |
| 348 |
} |
| 349 |
|
| 350 |
/* vim: set sts=3 sw=3 et: */ |