| 1 | /**************************************************************************/ |
| 2 | /* display_server_x11.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "display_server_x11.h" |
| 32 | |
| 33 | #ifdef X11_ENABLED |
| 34 | |
| 35 | #include "x11/detect_prime_x11.h" |
| 36 | #include "x11/key_mapping_x11.h" |
| 37 | |
| 38 | #include "core/config/project_settings.h" |
| 39 | #include "core/io/file_access.h" |
| 40 | #include "core/math/math_funcs.h" |
| 41 | #include "core/os/main_loop.h" |
| 42 | #include "core/string/print_string.h" |
| 43 | #include "core/string/ustring.h" |
| 44 | #include "core/version.h" |
| 45 | #include "drivers/png/png_driver_common.h" |
| 46 | #include "main/main.h" |
| 47 | |
| 48 | #include "servers/rendering/dummy/rasterizer_dummy.h" |
| 49 | |
| 50 | #if defined(VULKAN_ENABLED) |
| 51 | #include "servers/rendering/renderer_rd/renderer_compositor_rd.h" |
| 52 | #endif |
| 53 | |
| 54 | #if defined(GLES3_ENABLED) |
| 55 | #include "drivers/gles3/rasterizer_gles3.h" |
| 56 | #endif |
| 57 | |
| 58 | #ifdef ACCESSKIT_ENABLED |
| 59 | #include "drivers/accesskit/accessibility_driver_accesskit.h" |
| 60 | #endif |
| 61 | |
| 62 | #ifdef DBUS_ENABLED |
| 63 | #ifdef SOWRAP_ENABLED |
| 64 | #include "dbus-so_wrap.h" |
| 65 | #else |
| 66 | #include <dbus/dbus.h> |
| 67 | #endif |
| 68 | #endif |
| 69 | |
| 70 | #include <dlfcn.h> |
| 71 | #include <sys/stat.h> |
| 72 | #include <sys/types.h> |
| 73 | #include <unistd.h> |
| 74 | #include <climits> |
| 75 | #include <cstdio> |
| 76 | #include <cstdlib> |
| 77 | |
| 78 | #undef CursorShape |
| 79 | #include <X11/XKBlib.h> |
| 80 | |
| 81 | // ICCCM |
| 82 | #define WM_NormalState 1L // window normal state |
| 83 | #define WM_IconicState 3L // window minimized |
| 84 | // EWMH |
| 85 | #define _NET_WM_STATE_REMOVE 0L // remove/unset property |
| 86 | #define _NET_WM_STATE_ADD 1L // add/set property |
| 87 | |
| 88 | #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0L |
| 89 | #define _NET_WM_MOVERESIZE_SIZE_TOP 1L |
| 90 | #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2L |
| 91 | #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3L |
| 92 | #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4L |
| 93 | #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5L |
| 94 | #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6L |
| 95 | #define _NET_WM_MOVERESIZE_SIZE_LEFT 7L |
| 96 | #define _NET_WM_MOVERESIZE_MOVE 8L |
| 97 | |
| 98 | // 2.2 is the first release with multitouch |
| 99 | #define XINPUT_CLIENT_VERSION_MAJOR 2 |
| 100 | #define XINPUT_CLIENT_VERSION_MINOR 2 |
| 101 | |
| 102 | #define VALUATOR_ABSX 0 |
| 103 | #define VALUATOR_ABSY 1 |
| 104 | #define VALUATOR_PRESSURE 2 |
| 105 | #define VALUATOR_TILTX 3 |
| 106 | #define VALUATOR_TILTY 4 |
| 107 | |
| 108 | //#define DISPLAY_SERVER_X11_DEBUG_LOGS_ENABLED |
| 109 | #ifdef DISPLAY_SERVER_X11_DEBUG_LOGS_ENABLED |
| 110 | #define DEBUG_LOG_X11(...) printf(__VA_ARGS__) |
| 111 | #else |
| 112 | #define DEBUG_LOG_X11(...) |
| 113 | #endif |
| 114 | |
| 115 | static const double abs_resolution_mult = 10000.0; |
| 116 | static const double abs_resolution_range_mult = 10.0; |
| 117 | |
| 118 | struct MotifWmHints { |
| 119 | unsigned long flags; |
| 120 | unsigned long functions; |
| 121 | unsigned long decorations; |
| 122 | long input_mode; |
| 123 | unsigned long status; |
| 124 | }; |
| 125 | |
| 126 | enum { |
| 127 | MWM_HINTS_FUNCTIONS = (1L << 0), |
| 128 | MWM_HINTS_DECORATIONS = (1L << 1), |
| 129 | |
| 130 | MWM_FUNC_ALL = (1L << 0), |
| 131 | MWM_FUNC_RESIZE = (1L << 1), |
| 132 | MWM_FUNC_MOVE = (1L << 2), |
| 133 | MWM_FUNC_MINIMIZE = (1L << 3), |
| 134 | MWM_FUNC_MAXIMIZE = (1L << 4), |
| 135 | MWM_FUNC_CLOSE = (1L << 5), |
| 136 | |
| 137 | MWM_DECOR_ALL = (1L << 0), |
| 138 | MWM_DECOR_BORDER = (1L << 1), |
| 139 | MWM_DECOR_RESIZEH = (1L << 2), |
| 140 | MWM_DECOR_TITLE = (1L << 3), |
| 141 | MWM_DECOR_MENU = (1L << 4), |
| 142 | MWM_DECOR_MINIMIZE = (1L << 5), |
| 143 | MWM_DECOR_MAXIMIZE = (1L << 6), |
| 144 | }; |
| 145 | |
| 146 | static String get_atom_name(Display *p_disp, Atom p_atom) { |
| 147 | char *name = XGetAtomName(p_disp, p_atom); |
| 148 | ERR_FAIL_NULL_V_MSG(name, String(), "Atom is invalid."); |
| 149 | String ret = String::utf8(name); |
| 150 | XFree(name); |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | bool DisplayServerX11::has_feature(Feature p_feature) const { |
| 155 | switch (p_feature) { |
| 156 | #ifndef DISABLE_DEPRECATED |
| 157 | case FEATURE_GLOBAL_MENU: { |
| 158 | return (native_menu && native_menu->has_feature(NativeMenu::FEATURE_GLOBAL_MENU)); |
| 159 | } break; |
| 160 | #endif |
| 161 | case FEATURE_SUBWINDOWS: |
| 162 | #ifdef TOUCH_ENABLED |
| 163 | case FEATURE_TOUCHSCREEN: |
| 164 | #endif |
| 165 | case FEATURE_MOUSE: |
| 166 | case FEATURE_MOUSE_WARP: |
| 167 | case FEATURE_CLIPBOARD: |
| 168 | case FEATURE_CURSOR_SHAPE: |
| 169 | case FEATURE_CUSTOM_CURSOR_SHAPE: |
| 170 | case FEATURE_IME: |
| 171 | case FEATURE_WINDOW_TRANSPARENCY: |
| 172 | //case FEATURE_HIDPI: |
| 173 | case FEATURE_ICON: |
| 174 | //case FEATURE_NATIVE_ICON: |
| 175 | case FEATURE_SWAP_BUFFERS: |
| 176 | #ifdef DBUS_ENABLED |
| 177 | case FEATURE_KEEP_SCREEN_ON: |
| 178 | #endif |
| 179 | case FEATURE_CLIPBOARD_PRIMARY: |
| 180 | case FEATURE_WINDOW_EMBEDDING: |
| 181 | case FEATURE_WINDOW_DRAG: { |
| 182 | return true; |
| 183 | } break; |
| 184 | |
| 185 | //case FEATURE_NATIVE_DIALOG: |
| 186 | //case FEATURE_NATIVE_DIALOG_INPUT: |
| 187 | #ifdef DBUS_ENABLED |
| 188 | case FEATURE_NATIVE_DIALOG_FILE: |
| 189 | case FEATURE_NATIVE_DIALOG_FILE_EXTRA: |
| 190 | case FEATURE_NATIVE_DIALOG_FILE_MIME: { |
| 191 | return (portal_desktop && portal_desktop->is_supported() && portal_desktop->is_file_chooser_supported()); |
| 192 | } break; |
| 193 | case FEATURE_NATIVE_COLOR_PICKER: { |
| 194 | return (portal_desktop && portal_desktop->is_supported() && portal_desktop->is_screenshot_supported()); |
| 195 | } break; |
| 196 | #endif |
| 197 | case FEATURE_SCREEN_CAPTURE: { |
| 198 | return !xwayland; |
| 199 | } break; |
| 200 | |
| 201 | #ifdef SPEECHD_ENABLED |
| 202 | case FEATURE_TEXT_TO_SPEECH: { |
| 203 | return true; |
| 204 | } break; |
| 205 | #endif |
| 206 | |
| 207 | #ifdef ACCESSKIT_ENABLED |
| 208 | case FEATURE_ACCESSIBILITY_SCREEN_READER: { |
| 209 | return (accessibility_driver != nullptr); |
| 210 | } break; |
| 211 | #endif |
| 212 | |
| 213 | default: { |
| 214 | return false; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | String DisplayServerX11::get_name() const { |
| 220 | return "X11"; |
| 221 | } |
| 222 | |
| 223 | void DisplayServerX11::_update_real_mouse_position(const WindowData &wd) { |
| 224 | Window root_return, child_return; |
| 225 | int root_x, root_y, win_x, win_y; |
| 226 | unsigned int mask_return; |
| 227 | |
| 228 | Bool xquerypointer_result = XQueryPointer(x11_display, wd.x11_window, &root_return, &child_return, &root_x, &root_y, |
| 229 | &win_x, &win_y, &mask_return); |
| 230 | |
| 231 | if (xquerypointer_result) { |
| 232 | if (win_x > 0 && win_y > 0 && win_x <= wd.size.width && win_y <= wd.size.height) { |
| 233 | last_mouse_pos.x = win_x; |
| 234 | last_mouse_pos.y = win_y; |
| 235 | last_mouse_pos_valid = true; |
| 236 | Input::get_singleton()->set_mouse_position(last_mouse_pos); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | bool DisplayServerX11::_refresh_device_info() { |
| 242 | int event_base, error_base; |
| 243 | |
| 244 | print_verbose("XInput: Refreshing devices."); |
| 245 | |
| 246 | if (!XQueryExtension(x11_display, "XInputExtension", &xi.opcode, &event_base, &error_base)) { |
| 247 | print_verbose("XInput extension not available. Please upgrade your distribution."); |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | int xi_major_query = XINPUT_CLIENT_VERSION_MAJOR; |
| 252 | int xi_minor_query = XINPUT_CLIENT_VERSION_MINOR; |
| 253 | |
| 254 | if (XIQueryVersion(x11_display, &xi_major_query, &xi_minor_query) != Success) { |
| 255 | print_verbose(vformat("XInput 2 not available (server supports %d.%d).", xi_major_query, xi_minor_query)); |
| 256 | xi.opcode = 0; |
| 257 | return false; |
| 258 | } |
| 259 | |
| 260 | if (xi_major_query < XINPUT_CLIENT_VERSION_MAJOR || (xi_major_query == XINPUT_CLIENT_VERSION_MAJOR && xi_minor_query < XINPUT_CLIENT_VERSION_MINOR)) { |
| 261 | print_verbose(vformat("XInput %d.%d not available (server supports %d.%d). Touch input unavailable.", |
| 262 | XINPUT_CLIENT_VERSION_MAJOR, XINPUT_CLIENT_VERSION_MINOR, xi_major_query, xi_minor_query)); |
| 263 | } |
| 264 | |
| 265 | xi.absolute_devices.clear(); |
| 266 | xi.touch_devices.clear(); |
| 267 | xi.pen_inverted_devices.clear(); |
| 268 | xi.last_relative_time = 0; |
| 269 | |
| 270 | int dev_count; |
| 271 | XIDeviceInfo *info = XIQueryDevice(x11_display, XIAllDevices, &dev_count); |
| 272 | |
| 273 | for (int i = 0; i < dev_count; i++) { |
| 274 | XIDeviceInfo *dev = &info[i]; |
| 275 | if (!dev->enabled) { |
| 276 | continue; |
| 277 | } |
| 278 | if (!(dev->use == XISlavePointer || dev->use == XIFloatingSlave)) { |
| 279 | continue; |
| 280 | } |
| 281 | |
| 282 | bool direct_touch = false; |
| 283 | bool absolute_mode = false; |
| 284 | int resolution_x = 0; |
| 285 | int resolution_y = 0; |
| 286 | double abs_x_min = 0; |
| 287 | double abs_x_max = 0; |
| 288 | double abs_y_min = 0; |
| 289 | double abs_y_max = 0; |
| 290 | double pressure_min = 0; |
| 291 | double pressure_max = 0; |
| 292 | double tilt_x_min = 0; |
| 293 | double tilt_x_max = 0; |
| 294 | double tilt_y_min = 0; |
| 295 | double tilt_y_max = 0; |
| 296 | for (int j = 0; j < dev->num_classes; j++) { |
| 297 | #ifdef TOUCH_ENABLED |
| 298 | if (dev->classes[j]->type == XITouchClass && ((XITouchClassInfo *)dev->classes[j])->mode == XIDirectTouch) { |
| 299 | direct_touch = true; |
| 300 | } |
| 301 | #endif |
| 302 | if (dev->classes[j]->type == XIValuatorClass) { |
| 303 | XIValuatorClassInfo *class_info = (XIValuatorClassInfo *)dev->classes[j]; |
| 304 | |
| 305 | if (class_info->number == VALUATOR_ABSX && class_info->mode == XIModeAbsolute) { |
| 306 | resolution_x = class_info->resolution; |
| 307 | abs_x_min = class_info->min; |
| 308 | abs_x_max = class_info->max; |
| 309 | absolute_mode = true; |
| 310 | } else if (class_info->number == VALUATOR_ABSY && class_info->mode == XIModeAbsolute) { |
| 311 | resolution_y = class_info->resolution; |
| 312 | abs_y_min = class_info->min; |
| 313 | abs_y_max = class_info->max; |
| 314 | absolute_mode = true; |
| 315 | } else if (class_info->number == VALUATOR_PRESSURE && class_info->mode == XIModeAbsolute) { |
| 316 | pressure_min = class_info->min; |
| 317 | pressure_max = class_info->max; |
| 318 | } else if (class_info->number == VALUATOR_TILTX && class_info->mode == XIModeAbsolute) { |
| 319 | tilt_x_min = class_info->min; |
| 320 | tilt_x_max = class_info->max; |
| 321 | } else if (class_info->number == VALUATOR_TILTY && class_info->mode == XIModeAbsolute) { |
| 322 | tilt_y_min = class_info->min; |
| 323 | tilt_y_max = class_info->max; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | if (direct_touch) { |
| 328 | xi.touch_devices.push_back(dev->deviceid); |
| 329 | print_verbose("XInput: Using touch device: " + String(dev->name)); |
| 330 | } |
| 331 | if (absolute_mode) { |
| 332 | // If no resolution was reported, use the min/max ranges. |
| 333 | if (resolution_x <= 0) { |
| 334 | resolution_x = (abs_x_max - abs_x_min) * abs_resolution_range_mult; |
| 335 | } |
| 336 | if (resolution_y <= 0) { |
| 337 | resolution_y = (abs_y_max - abs_y_min) * abs_resolution_range_mult; |
| 338 | } |
| 339 | xi.absolute_devices[dev->deviceid] = Vector2(abs_resolution_mult / resolution_x, abs_resolution_mult / resolution_y); |
| 340 | print_verbose("XInput: Absolute pointing device: " + String(dev->name)); |
| 341 | } |
| 342 | |
| 343 | xi.pressure = 0; |
| 344 | xi.pen_pressure_range[dev->deviceid] = Vector2(pressure_min, pressure_max); |
| 345 | xi.pen_tilt_x_range[dev->deviceid] = Vector2(tilt_x_min, tilt_x_max); |
| 346 | xi.pen_tilt_y_range[dev->deviceid] = Vector2(tilt_y_min, tilt_y_max); |
| 347 | xi.pen_inverted_devices[dev->deviceid] = String(dev->name).findn("eraser") > 0; |
| 348 | } |
| 349 | |
| 350 | XIFreeDeviceInfo(info); |
| 351 | #ifdef TOUCH_ENABLED |
| 352 | if (!xi.touch_devices.size()) { |
| 353 | print_verbose("XInput: No touch devices found."); |
| 354 | } |
| 355 | #endif |
| 356 | |
| 357 | return true; |
| 358 | } |
| 359 | |
| 360 | void DisplayServerX11::_flush_mouse_motion() { |
| 361 | // Block events polling while flushing motion events. |
| 362 | MutexLock mutex_lock(events_mutex); |
| 363 | |
| 364 | for (uint32_t event_index = 0; event_index < polled_events.size(); ++event_index) { |
| 365 | XEvent &event = polled_events[event_index]; |
| 366 | if (XGetEventData(x11_display, &event.xcookie) && event.xcookie.type == GenericEvent && event.xcookie.extension == xi.opcode) { |
| 367 | XIDeviceEvent *event_data = (XIDeviceEvent *)event.xcookie.data; |
| 368 | if (event_data->evtype == XI_RawMotion) { |
| 369 | XFreeEventData(x11_display, &event.xcookie); |
| 370 | polled_events.remove_at(event_index--); |
| 371 | continue; |
| 372 | } |
| 373 | XFreeEventData(x11_display, &event.xcookie); |
| 374 | break; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | xi.relative_motion.x = 0; |
| 379 | xi.relative_motion.y = 0; |
| 380 | } |
| 381 | |
| 382 | #ifdef SPEECHD_ENABLED |
| 383 | |
| 384 | void DisplayServerX11::initialize_tts() const { |
| 385 | const_cast<DisplayServerX11 *>(this)->tts = memnew(TTS_Linux); |
| 386 | } |
| 387 | |
| 388 | bool DisplayServerX11::tts_is_speaking() const { |
| 389 | if (unlikely(!tts)) { |
| 390 | initialize_tts(); |
| 391 | } |
| 392 | ERR_FAIL_NULL_V(tts, false); |
| 393 | return tts->is_speaking(); |
| 394 | } |
| 395 | |
| 396 | bool DisplayServerX11::tts_is_paused() const { |
| 397 | if (unlikely(!tts)) { |
| 398 | initialize_tts(); |
| 399 | } |
| 400 | ERR_FAIL_NULL_V(tts, false); |
| 401 | return tts->is_paused(); |
| 402 | } |
| 403 | |
| 404 | TypedArray<Dictionary> DisplayServerX11::tts_get_voices() const { |
| 405 | if (unlikely(!tts)) { |
| 406 | initialize_tts(); |
| 407 | } |
| 408 | ERR_FAIL_NULL_V(tts, TypedArray<Dictionary>()); |
| 409 | return tts->get_voices(); |
| 410 | } |
| 411 | |
| 412 | void DisplayServerX11::tts_speak(const String &p_text, const String &p_voice, int p_volume, float p_pitch, float p_rate, int64_t p_utterance_id, bool p_interrupt) { |
| 413 | if (unlikely(!tts)) { |
| 414 | initialize_tts(); |
| 415 | } |
| 416 | ERR_FAIL_NULL(tts); |
| 417 | tts->speak(p_text, p_voice, p_volume, p_pitch, p_rate, p_utterance_id, p_interrupt); |
| 418 | } |
| 419 | |
| 420 | void DisplayServerX11::tts_pause() { |
| 421 | if (unlikely(!tts)) { |
| 422 | initialize_tts(); |
| 423 | } |
| 424 | ERR_FAIL_NULL(tts); |
| 425 | tts->pause(); |
| 426 | } |
| 427 | |
| 428 | void DisplayServerX11::tts_resume() { |
| 429 | if (unlikely(!tts)) { |
| 430 | initialize_tts(); |
| 431 | } |
| 432 | ERR_FAIL_NULL(tts); |
| 433 | tts->resume(); |
| 434 | } |
| 435 | |
| 436 | void DisplayServerX11::tts_stop() { |
| 437 | if (unlikely(!tts)) { |
| 438 | initialize_tts(); |
| 439 | } |
| 440 | ERR_FAIL_NULL(tts); |
| 441 | tts->stop(); |
| 442 | } |
| 443 | |
| 444 | #endif |
| 445 | |
| 446 | #ifdef DBUS_ENABLED |
| 447 | |
| 448 | bool DisplayServerX11::is_dark_mode_supported() const { |
| 449 | return portal_desktop && portal_desktop->is_supported() && portal_desktop->is_settings_supported(); |
| 450 | } |
| 451 | |
| 452 | bool DisplayServerX11::is_dark_mode() const { |
| 453 | if (!is_dark_mode_supported()) { |
| 454 | return false; |
| 455 | } |
| 456 | switch (portal_desktop->get_appearance_color_scheme()) { |
| 457 | case 1: |
| 458 | // Prefers dark theme. |
| 459 | return true; |
| 460 | case 2: |
| 461 | // Prefers light theme. |
| 462 | return false; |
| 463 | default: |
| 464 | // Preference unknown. |
| 465 | return false; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | Color DisplayServerX11::get_accent_color() const { |
| 470 | if (!portal_desktop) { |
| 471 | return Color(); |
| 472 | } |
| 473 | return portal_desktop->get_appearance_accent_color(); |
| 474 | } |
| 475 | |
| 476 | void DisplayServerX11::set_system_theme_change_callback(const Callable &p_callable) { |
| 477 | ERR_FAIL_COND(!portal_desktop); |
| 478 | portal_desktop->set_system_theme_change_callback(p_callable); |
| 479 | } |
| 480 | |
| 481 | Error DisplayServerX11::file_dialog_show(const String &p_title, const String &p_current_directory, const String &p_filename, bool p_show_hidden, FileDialogMode p_mode, const Vector<String> &p_filters, const Callable &p_callback, WindowID p_window_id) { |
| 482 | ERR_FAIL_COND_V(!portal_desktop, ERR_UNAVAILABLE); |
| 483 | WindowID window_id = p_window_id; |
| 484 | |
| 485 | if (!windows.has(window_id) || windows[window_id].is_popup) { |
| 486 | window_id = MAIN_WINDOW_ID; |
| 487 | } |
| 488 | |
| 489 | String xid = vformat("x11:%x", (uint64_t)windows[window_id].x11_window); |
| 490 | return portal_desktop->file_dialog_show(p_window_id, xid, p_title, p_current_directory, String(), p_filename, p_mode, p_filters, TypedArray<Dictionary>(), p_callback, false); |
| 491 | } |
| 492 | |
| 493 | Error DisplayServerX11::file_dialog_with_options_show(const String &p_title, const String &p_current_directory, const String &p_root, const String &p_filename, bool p_show_hidden, FileDialogMode p_mode, const Vector<String> &p_filters, const TypedArray<Dictionary> &p_options, const Callable &p_callback, WindowID p_window_id) { |
| 494 | ERR_FAIL_COND_V(!portal_desktop, ERR_UNAVAILABLE); |
| 495 | WindowID window_id = p_window_id; |
| 496 | |
| 497 | if (!windows.has(window_id) || windows[window_id].is_popup) { |
| 498 | window_id = MAIN_WINDOW_ID; |
| 499 | } |
| 500 | |
| 501 | String xid = vformat("x11:%x", (uint64_t)windows[window_id].x11_window); |
| 502 | return portal_desktop->file_dialog_show(p_window_id, xid, p_title, p_current_directory, p_root, p_filename, p_mode, p_filters, p_options, p_callback, true); |
| 503 | } |
| 504 | |
| 505 | #endif |
| 506 | |
| 507 | void DisplayServerX11::beep() const { |
| 508 | XBell(x11_display, 0); |
| 509 | } |
| 510 | |
| 511 | void DisplayServerX11::_mouse_update_mode() { |
| 512 | _THREAD_SAFE_METHOD_ |
| 513 | |
| 514 | MouseMode wanted_mouse_mode = mouse_mode_override_enabled |
| 515 | ? mouse_mode_override |
| 516 | : mouse_mode_base; |
| 517 | |
| 518 | if (wanted_mouse_mode == mouse_mode) { |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | if (mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED || mouse_mode == MOUSE_MODE_CONFINED_HIDDEN) { |
| 523 | XUngrabPointer(x11_display, CurrentTime); |
| 524 | } |
| 525 | |
| 526 | // The only modes that show a cursor are VISIBLE and CONFINED |
| 527 | bool show_cursor = (wanted_mouse_mode == MOUSE_MODE_VISIBLE || wanted_mouse_mode == MOUSE_MODE_CONFINED); |
| 528 | bool previously_shown = (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED); |
| 529 | |
| 530 | if (show_cursor && !previously_shown) { |
| 531 | WindowID window_id = get_window_at_screen_position(mouse_get_position()); |
| 532 | if (window_id != INVALID_WINDOW_ID && window_mouseover_id != window_id) { |
| 533 | if (window_mouseover_id != INVALID_WINDOW_ID) { |
| 534 | _send_window_event(windows[window_mouseover_id], WINDOW_EVENT_MOUSE_EXIT); |
| 535 | } |
| 536 | window_mouseover_id = window_id; |
| 537 | _send_window_event(windows[window_id], WINDOW_EVENT_MOUSE_ENTER); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 542 | if (show_cursor) { |
| 543 | XDefineCursor(x11_display, E.value.x11_window, cursors[current_cursor]); // show cursor |
| 544 | } else { |
| 545 | XDefineCursor(x11_display, E.value.x11_window, null_cursor); // hide cursor |
| 546 | } |
| 547 | } |
| 548 | mouse_mode = wanted_mouse_mode; |
| 549 | |
| 550 | if (mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED || mouse_mode == MOUSE_MODE_CONFINED_HIDDEN) { |
| 551 | //flush pending motion events |
| 552 | _flush_mouse_motion(); |
| 553 | WindowID window_id = _get_focused_window_or_popup(); |
| 554 | if (!windows.has(window_id)) { |
| 555 | window_id = MAIN_WINDOW_ID; |
| 556 | } |
| 557 | WindowData &window = windows[window_id]; |
| 558 | |
| 559 | if (XGrabPointer( |
| 560 | x11_display, window.x11_window, True, |
| 561 | ButtonPressMask | ButtonReleaseMask | PointerMotionMask, |
| 562 | GrabModeAsync, GrabModeAsync, window.x11_window, None, CurrentTime) != GrabSuccess) { |
| 563 | ERR_PRINT("NO GRAB"); |
| 564 | } |
| 565 | |
| 566 | if (mouse_mode == MOUSE_MODE_CAPTURED) { |
| 567 | center.x = window.size.width / 2; |
| 568 | center.y = window.size.height / 2; |
| 569 | |
| 570 | XWarpPointer(x11_display, None, window.x11_window, |
| 571 | 0, 0, 0, 0, (int)center.x, (int)center.y); |
| 572 | |
| 573 | Input::get_singleton()->set_mouse_position(center); |
| 574 | } |
| 575 | } else { |
| 576 | do_mouse_warp = false; |
| 577 | } |
| 578 | |
| 579 | XFlush(x11_display); |
| 580 | } |
| 581 | |
| 582 | void DisplayServerX11::mouse_set_mode(MouseMode p_mode) { |
| 583 | ERR_FAIL_INDEX(p_mode, MouseMode::MOUSE_MODE_MAX); |
| 584 | if (p_mode == mouse_mode_base) { |
| 585 | return; |
| 586 | } |
| 587 | mouse_mode_base = p_mode; |
| 588 | _mouse_update_mode(); |
| 589 | } |
| 590 | |
| 591 | DisplayServerX11::MouseMode DisplayServerX11::mouse_get_mode() const { |
| 592 | return mouse_mode; |
| 593 | } |
| 594 | |
| 595 | void DisplayServerX11::mouse_set_mode_override(MouseMode p_mode) { |
| 596 | ERR_FAIL_INDEX(p_mode, MouseMode::MOUSE_MODE_MAX); |
| 597 | if (p_mode == mouse_mode_override) { |
| 598 | return; |
| 599 | } |
| 600 | mouse_mode_override = p_mode; |
| 601 | _mouse_update_mode(); |
| 602 | } |
| 603 | |
| 604 | DisplayServerX11::MouseMode DisplayServerX11::mouse_get_mode_override() const { |
| 605 | return mouse_mode_override; |
| 606 | } |
| 607 | |
| 608 | void DisplayServerX11::mouse_set_mode_override_enabled(bool p_override_enabled) { |
| 609 | if (p_override_enabled == mouse_mode_override_enabled) { |
| 610 | return; |
| 611 | } |
| 612 | mouse_mode_override_enabled = p_override_enabled; |
| 613 | _mouse_update_mode(); |
| 614 | } |
| 615 | |
| 616 | bool DisplayServerX11::mouse_is_mode_override_enabled() const { |
| 617 | return mouse_mode_override_enabled; |
| 618 | } |
| 619 | |
| 620 | void DisplayServerX11::warp_mouse(const Point2i &p_position) { |
| 621 | _THREAD_SAFE_METHOD_ |
| 622 | |
| 623 | if (mouse_mode == MOUSE_MODE_CAPTURED) { |
| 624 | last_mouse_pos = p_position; |
| 625 | } else { |
| 626 | WindowID window_id = _get_focused_window_or_popup(); |
| 627 | if (!windows.has(window_id)) { |
| 628 | window_id = MAIN_WINDOW_ID; |
| 629 | } |
| 630 | |
| 631 | XWarpPointer(x11_display, None, windows[window_id].x11_window, |
| 632 | 0, 0, 0, 0, (int)p_position.x, (int)p_position.y); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | Point2i DisplayServerX11::mouse_get_position() const { |
| 637 | int number_of_screens = XScreenCount(x11_display); |
| 638 | for (int i = 0; i < number_of_screens; i++) { |
| 639 | Window root, child; |
| 640 | int root_x, root_y, win_x, win_y; |
| 641 | unsigned int mask; |
| 642 | if (XQueryPointer(x11_display, XRootWindow(x11_display, i), &root, &child, &root_x, &root_y, &win_x, &win_y, &mask)) { |
| 643 | XWindowAttributes root_attrs; |
| 644 | XGetWindowAttributes(x11_display, root, &root_attrs); |
| 645 | |
| 646 | return Vector2i(root_attrs.x + root_x, root_attrs.y + root_y); |
| 647 | } |
| 648 | } |
| 649 | return Vector2i(); |
| 650 | } |
| 651 | |
| 652 | BitField<MouseButtonMask> DisplayServerX11::mouse_get_button_state() const { |
| 653 | int number_of_screens = XScreenCount(x11_display); |
| 654 | for (int i = 0; i < number_of_screens; i++) { |
| 655 | Window root, child; |
| 656 | int root_x, root_y, win_x, win_y; |
| 657 | unsigned int mask; |
| 658 | if (XQueryPointer(x11_display, XRootWindow(x11_display, i), &root, &child, &root_x, &root_y, &win_x, &win_y, &mask)) { |
| 659 | BitField<MouseButtonMask> last_button_state = MouseButtonMask::NONE; |
| 660 | |
| 661 | if (mask & Button1Mask) { |
| 662 | last_button_state.set_flag(MouseButtonMask::LEFT); |
| 663 | } |
| 664 | if (mask & Button2Mask) { |
| 665 | last_button_state.set_flag(MouseButtonMask::MIDDLE); |
| 666 | } |
| 667 | if (mask & Button3Mask) { |
| 668 | last_button_state.set_flag(MouseButtonMask::RIGHT); |
| 669 | } |
| 670 | if (mask & Button4Mask) { |
| 671 | last_button_state.set_flag(MouseButtonMask::MB_XBUTTON1); |
| 672 | } |
| 673 | if (mask & Button5Mask) { |
| 674 | last_button_state.set_flag(MouseButtonMask::MB_XBUTTON2); |
| 675 | } |
| 676 | |
| 677 | return last_button_state; |
| 678 | } |
| 679 | } |
| 680 | return MouseButtonMask::NONE; |
| 681 | } |
| 682 | |
| 683 | void DisplayServerX11::clipboard_set(const String &p_text) { |
| 684 | _THREAD_SAFE_METHOD_ |
| 685 | |
| 686 | { |
| 687 | // The clipboard content can be accessed while polling for events. |
| 688 | MutexLock mutex_lock(events_mutex); |
| 689 | internal_clipboard = p_text; |
| 690 | } |
| 691 | |
| 692 | XSetSelectionOwner(x11_display, XA_PRIMARY, windows[MAIN_WINDOW_ID].x11_window, CurrentTime); |
| 693 | XSetSelectionOwner(x11_display, XInternAtom(x11_display, "CLIPBOARD", 0), windows[MAIN_WINDOW_ID].x11_window, CurrentTime); |
| 694 | } |
| 695 | |
| 696 | void DisplayServerX11::clipboard_set_primary(const String &p_text) { |
| 697 | _THREAD_SAFE_METHOD_ |
| 698 | if (!p_text.is_empty()) { |
| 699 | { |
| 700 | // The clipboard content can be accessed while polling for events. |
| 701 | MutexLock mutex_lock(events_mutex); |
| 702 | internal_clipboard_primary = p_text; |
| 703 | } |
| 704 | |
| 705 | XSetSelectionOwner(x11_display, XA_PRIMARY, windows[MAIN_WINDOW_ID].x11_window, CurrentTime); |
| 706 | XSetSelectionOwner(x11_display, XInternAtom(x11_display, "PRIMARY", 0), windows[MAIN_WINDOW_ID].x11_window, CurrentTime); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | Bool DisplayServerX11::_predicate_clipboard_selection(Display *display, XEvent *event, XPointer arg) { |
| 711 | if (event->type == SelectionNotify && event->xselection.requestor == *(Window *)arg) { |
| 712 | return True; |
| 713 | } else { |
| 714 | return False; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | Bool DisplayServerX11::_predicate_clipboard_incr(Display *display, XEvent *event, XPointer arg) { |
| 719 | if (event->type == PropertyNotify && event->xproperty.state == PropertyNewValue && event->xproperty.atom == *(Atom *)arg) { |
| 720 | return True; |
| 721 | } else { |
| 722 | return False; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | String DisplayServerX11::_clipboard_get_impl(Atom p_source, Window x11_window, Atom target) const { |
| 727 | String ret; |
| 728 | |
| 729 | Window selection_owner = XGetSelectionOwner(x11_display, p_source); |
| 730 | if (selection_owner == x11_window) { |
| 731 | static const char *target_type = "PRIMARY"; |
| 732 | if (p_source != None && get_atom_name(x11_display, p_source) == target_type) { |
| 733 | return internal_clipboard_primary; |
| 734 | } else { |
| 735 | return internal_clipboard; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | if (selection_owner != None) { |
| 740 | // Block events polling while processing selection events. |
| 741 | MutexLock mutex_lock(events_mutex); |
| 742 | |
| 743 | Atom selection = XA_PRIMARY; |
| 744 | XConvertSelection(x11_display, p_source, target, selection, |
| 745 | x11_window, CurrentTime); |
| 746 | |
| 747 | XFlush(x11_display); |
| 748 | |
| 749 | // Blocking wait for predicate to be True and remove the event from the queue. |
| 750 | XEvent event; |
| 751 | XIfEvent(x11_display, &event, _predicate_clipboard_selection, (XPointer)&x11_window); |
| 752 | |
| 753 | // Do not get any data, see how much data is there. |
| 754 | Atom type; |
| 755 | int format, result; |
| 756 | unsigned long len, bytes_left, dummy; |
| 757 | unsigned char *data; |
| 758 | XGetWindowProperty(x11_display, x11_window, |
| 759 | selection, // Tricky.. |
| 760 | 0, 0, // offset - len |
| 761 | 0, // Delete 0==FALSE |
| 762 | AnyPropertyType, // flag |
| 763 | &type, // return type |
| 764 | &format, // return format |
| 765 | &len, &bytes_left, // data length |
| 766 | &data); |
| 767 | |
| 768 | if (data) { |
| 769 | XFree(data); |
| 770 | } |
| 771 | |
| 772 | if (type == XInternAtom(x11_display, "INCR", 0)) { |
| 773 | // Data is going to be received incrementally. |
| 774 | DEBUG_LOG_X11("INCR selection started.\n"); |
| 775 | |
| 776 | LocalVector<uint8_t> incr_data; |
| 777 | uint32_t data_size = 0; |
| 778 | bool success = false; |
| 779 | |
| 780 | // Delete INCR property to notify the owner. |
| 781 | XDeleteProperty(x11_display, x11_window, type); |
| 782 | |
| 783 | // Process events from the queue. |
| 784 | bool done = false; |
| 785 | while (!done) { |
| 786 | if (!_wait_for_events()) { |
| 787 | // Error or timeout, abort. |
| 788 | break; |
| 789 | } |
| 790 | |
| 791 | // Non-blocking wait for next event and remove it from the queue. |
| 792 | XEvent ev; |
| 793 | while (XCheckIfEvent(x11_display, &ev, _predicate_clipboard_incr, (XPointer)&selection)) { |
| 794 | result = XGetWindowProperty(x11_display, x11_window, |
| 795 | selection, // selection type |
| 796 | 0, LONG_MAX, // offset - len |
| 797 | True, // delete property to notify the owner |
| 798 | AnyPropertyType, // flag |
| 799 | &type, // return type |
| 800 | &format, // return format |
| 801 | &len, &bytes_left, // data length |
| 802 | &data); |
| 803 | |
| 804 | DEBUG_LOG_X11("PropertyNotify: len=%lu, format=%i\n", len, format); |
| 805 | |
| 806 | if (result == Success) { |
| 807 | if (data && (len > 0)) { |
| 808 | uint32_t prev_size = incr_data.size(); |
| 809 | if (prev_size == 0) { |
| 810 | // First property contains initial data size. |
| 811 | unsigned long initial_size = *(unsigned long *)data; |
| 812 | incr_data.resize(initial_size); |
| 813 | } else { |
| 814 | // New chunk, resize to be safe and append data. |
| 815 | incr_data.resize(MAX(data_size + len, prev_size)); |
| 816 | memcpy(incr_data.ptr() + data_size, data, len); |
| 817 | data_size += len; |
| 818 | } |
| 819 | } else { |
| 820 | // Last chunk, process finished. |
| 821 | done = true; |
| 822 | success = true; |
| 823 | } |
| 824 | } else { |
| 825 | print_verbose("Failed to get selection data chunk."); |
| 826 | done = true; |
| 827 | } |
| 828 | |
| 829 | if (data) { |
| 830 | XFree(data); |
| 831 | } |
| 832 | |
| 833 | if (done) { |
| 834 | break; |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | if (success && (data_size > 0)) { |
| 840 | ret.append_utf8((const char *)incr_data.ptr(), data_size); |
| 841 | } |
| 842 | } else if (bytes_left > 0) { |
| 843 | // Data is ready and can be processed all at once. |
| 844 | result = XGetWindowProperty(x11_display, x11_window, |
| 845 | selection, 0, bytes_left, 0, |
| 846 | AnyPropertyType, &type, &format, |
| 847 | &len, &dummy, &data); |
| 848 | |
| 849 | if (result == Success) { |
| 850 | ret.append_utf8((const char *)data); |
| 851 | } else { |
| 852 | print_verbose("Failed to get selection data."); |
| 853 | } |
| 854 | |
| 855 | if (data) { |
| 856 | XFree(data); |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | return ret; |
| 862 | } |
| 863 | |
| 864 | Atom DisplayServerX11::_clipboard_get_image_target(Atom p_source, Window x11_window) const { |
| 865 | Atom target = XInternAtom(x11_display, "TARGETS", 0); |
| 866 | Atom png = XInternAtom(x11_display, "image/png", 0); |
| 867 | Atom *valid_targets = nullptr; |
| 868 | unsigned long atom_count = 0; |
| 869 | |
| 870 | Window selection_owner = XGetSelectionOwner(x11_display, p_source); |
| 871 | if (selection_owner != None && selection_owner != x11_window) { |
| 872 | // Block events polling while processing selection events. |
| 873 | MutexLock mutex_lock(events_mutex); |
| 874 | |
| 875 | Atom selection = XA_PRIMARY; |
| 876 | XConvertSelection(x11_display, p_source, target, selection, x11_window, CurrentTime); |
| 877 | |
| 878 | XFlush(x11_display); |
| 879 | |
| 880 | // Blocking wait for predicate to be True and remove the event from the queue. |
| 881 | XEvent event; |
| 882 | XIfEvent(x11_display, &event, _predicate_clipboard_selection, (XPointer)&x11_window); |
| 883 | // Do not get any data, see how much data is there. |
| 884 | Atom type; |
| 885 | int format, result; |
| 886 | unsigned long len, bytes_left, dummy; |
| 887 | XGetWindowProperty(x11_display, x11_window, |
| 888 | selection, // Tricky.. |
| 889 | 0, 0, // offset - len |
| 890 | 0, // Delete 0==FALSE |
| 891 | XA_ATOM, // flag |
| 892 | &type, // return type |
| 893 | &format, // return format |
| 894 | &len, &bytes_left, // data length |
| 895 | (unsigned char **)&valid_targets); |
| 896 | |
| 897 | if (valid_targets) { |
| 898 | XFree(valid_targets); |
| 899 | valid_targets = nullptr; |
| 900 | } |
| 901 | |
| 902 | if (type == XA_ATOM && bytes_left > 0) { |
| 903 | // Data is ready and can be processed all at once. |
| 904 | result = XGetWindowProperty(x11_display, x11_window, |
| 905 | selection, 0, bytes_left / 4, 0, |
| 906 | XA_ATOM, &type, &format, |
| 907 | &len, &dummy, (unsigned char **)&valid_targets); |
| 908 | if (result == Success) { |
| 909 | atom_count = len; |
| 910 | } else { |
| 911 | print_verbose("Failed to get selection data."); |
| 912 | return None; |
| 913 | } |
| 914 | } else { |
| 915 | return None; |
| 916 | } |
| 917 | } else { |
| 918 | return None; |
| 919 | } |
| 920 | for (unsigned long i = 0; i < atom_count; i++) { |
| 921 | Atom atom = valid_targets[i]; |
| 922 | if (atom == png) { |
| 923 | XFree(valid_targets); |
| 924 | return png; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | XFree(valid_targets); |
| 929 | return None; |
| 930 | } |
| 931 | |
| 932 | String DisplayServerX11::_clipboard_get(Atom p_source, Window x11_window) const { |
| 933 | String ret; |
| 934 | Atom utf8_atom = XInternAtom(x11_display, "UTF8_STRING", True); |
| 935 | if (utf8_atom != None) { |
| 936 | ret = _clipboard_get_impl(p_source, x11_window, utf8_atom); |
| 937 | } |
| 938 | if (ret.is_empty()) { |
| 939 | ret = _clipboard_get_impl(p_source, x11_window, XA_STRING); |
| 940 | } |
| 941 | return ret; |
| 942 | } |
| 943 | |
| 944 | String DisplayServerX11::clipboard_get() const { |
| 945 | _THREAD_SAFE_METHOD_ |
| 946 | |
| 947 | String ret; |
| 948 | ret = _clipboard_get(XInternAtom(x11_display, "CLIPBOARD", 0), windows[MAIN_WINDOW_ID].x11_window); |
| 949 | |
| 950 | if (ret.is_empty()) { |
| 951 | ret = _clipboard_get(XA_PRIMARY, windows[MAIN_WINDOW_ID].x11_window); |
| 952 | } |
| 953 | |
| 954 | return ret; |
| 955 | } |
| 956 | |
| 957 | String DisplayServerX11::clipboard_get_primary() const { |
| 958 | _THREAD_SAFE_METHOD_ |
| 959 | |
| 960 | String ret; |
| 961 | ret = _clipboard_get(XInternAtom(x11_display, "PRIMARY", 0), windows[MAIN_WINDOW_ID].x11_window); |
| 962 | |
| 963 | if (ret.is_empty()) { |
| 964 | ret = _clipboard_get(XA_PRIMARY, windows[MAIN_WINDOW_ID].x11_window); |
| 965 | } |
| 966 | |
| 967 | return ret; |
| 968 | } |
| 969 | |
| 970 | Ref<Image> DisplayServerX11::clipboard_get_image() const { |
| 971 | _THREAD_SAFE_METHOD_ |
| 972 | Atom clipboard = XInternAtom(x11_display, "CLIPBOARD", 0); |
| 973 | Window x11_window = windows[MAIN_WINDOW_ID].x11_window; |
| 974 | Ref<Image> ret; |
| 975 | Atom target = _clipboard_get_image_target(clipboard, x11_window); |
| 976 | if (target == None) { |
| 977 | return ret; |
| 978 | } |
| 979 | |
| 980 | Window selection_owner = XGetSelectionOwner(x11_display, clipboard); |
| 981 | |
| 982 | if (selection_owner != None && selection_owner != x11_window) { |
| 983 | // Block events polling while processing selection events. |
| 984 | MutexLock mutex_lock(events_mutex); |
| 985 | |
| 986 | // Identifier for the property the other window |
| 987 | // will send the converted data to. |
| 988 | Atom transfer_prop = XA_PRIMARY; |
| 989 | XConvertSelection(x11_display, |
| 990 | clipboard, // source selection |
| 991 | target, // format to convert to |
| 992 | transfer_prop, // output property |
| 993 | x11_window, CurrentTime); |
| 994 | |
| 995 | XFlush(x11_display); |
| 996 | |
| 997 | // Blocking wait for predicate to be True and remove the event from the queue. |
| 998 | XEvent event; |
| 999 | XIfEvent(x11_display, &event, _predicate_clipboard_selection, (XPointer)&x11_window); |
| 1000 | |
| 1001 | // Do not get any data, see how much data is there. |
| 1002 | Atom type; |
| 1003 | int format, result; |
| 1004 | unsigned long len, bytes_left, dummy; |
| 1005 | unsigned char *data; |
| 1006 | XGetWindowProperty(x11_display, x11_window, |
| 1007 | transfer_prop, // Property data is transferred through |
| 1008 | 0, 1, // offset, len (4 so we can get the size if INCR is used) |
| 1009 | 0, // Delete 0==FALSE |
| 1010 | AnyPropertyType, // flag |
| 1011 | &type, // return type |
| 1012 | &format, // return format |
| 1013 | &len, &bytes_left, // data length |
| 1014 | &data); |
| 1015 | |
| 1016 | if (type == XInternAtom(x11_display, "INCR", 0)) { |
| 1017 | ERR_FAIL_COND_V_MSG(len != 1, ret, "Incremental transfer initial value was not length."); |
| 1018 | |
| 1019 | // Data is going to be received incrementally. |
| 1020 | DEBUG_LOG_X11("INCR selection started.\n"); |
| 1021 | |
| 1022 | LocalVector<uint8_t> incr_data; |
| 1023 | uint32_t data_size = 0; |
| 1024 | bool success = false; |
| 1025 | |
| 1026 | // Initial response is the lower bound of the length of the transferred data. |
| 1027 | incr_data.resize(*(unsigned long *)data); |
| 1028 | XFree(data); |
| 1029 | data = nullptr; |
| 1030 | |
| 1031 | // Delete INCR property to notify the owner. |
| 1032 | XDeleteProperty(x11_display, x11_window, transfer_prop); |
| 1033 | |
| 1034 | // Process events from the queue. |
| 1035 | bool done = false; |
| 1036 | while (!done) { |
| 1037 | if (!_wait_for_events()) { |
| 1038 | // Error or timeout, abort. |
| 1039 | break; |
| 1040 | } |
| 1041 | // Non-blocking wait for next event and remove it from the queue. |
| 1042 | XEvent ev; |
| 1043 | while (XCheckIfEvent(x11_display, &ev, _predicate_clipboard_incr, (XPointer)&transfer_prop)) { |
| 1044 | result = XGetWindowProperty(x11_display, x11_window, |
| 1045 | transfer_prop, // output property |
| 1046 | 0, LONG_MAX, // offset - len |
| 1047 | True, // delete property to notify the owner |
| 1048 | AnyPropertyType, // flag |
| 1049 | &type, // return type |
| 1050 | &format, // return format |
| 1051 | &len, &bytes_left, // data length |
| 1052 | &data); |
| 1053 | |
| 1054 | DEBUG_LOG_X11("PropertyNotify: len=%lu, format=%i\n", len, format); |
| 1055 | |
| 1056 | if (result == Success) { |
| 1057 | if (data && (len > 0)) { |
| 1058 | uint32_t prev_size = incr_data.size(); |
| 1059 | // New chunk, resize to be safe and append data. |
| 1060 | incr_data.resize(MAX(data_size + len, prev_size)); |
| 1061 | memcpy(incr_data.ptr() + data_size, data, len); |
| 1062 | data_size += len; |
| 1063 | } else if (!(format == 0 && len == 0)) { |
| 1064 | // For unclear reasons the first GetWindowProperty always returns a length and format of 0. |
| 1065 | // Otherwise, last chunk, process finished. |
| 1066 | done = true; |
| 1067 | success = true; |
| 1068 | } |
| 1069 | } else { |
| 1070 | print_verbose("Failed to get selection data chunk."); |
| 1071 | done = true; |
| 1072 | } |
| 1073 | |
| 1074 | if (data) { |
| 1075 | XFree(data); |
| 1076 | data = nullptr; |
| 1077 | } |
| 1078 | |
| 1079 | if (done) { |
| 1080 | break; |
| 1081 | } |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | if (success && (data_size > 0)) { |
| 1086 | ret.instantiate(); |
| 1087 | PNGDriverCommon::png_to_image(incr_data.ptr(), incr_data.size(), false, ret); |
| 1088 | } |
| 1089 | } else if (bytes_left > 0) { |
| 1090 | if (data) { |
| 1091 | XFree(data); |
| 1092 | data = nullptr; |
| 1093 | } |
| 1094 | // Data is ready and can be processed all at once. |
| 1095 | result = XGetWindowProperty(x11_display, x11_window, |
| 1096 | transfer_prop, 0, bytes_left + 4, 0, |
| 1097 | AnyPropertyType, &type, &format, |
| 1098 | &len, &dummy, &data); |
| 1099 | if (result == Success) { |
| 1100 | ret.instantiate(); |
| 1101 | PNGDriverCommon::png_to_image((uint8_t *)data, bytes_left, false, ret); |
| 1102 | } else { |
| 1103 | print_verbose("Failed to get selection data."); |
| 1104 | } |
| 1105 | |
| 1106 | if (data) { |
| 1107 | XFree(data); |
| 1108 | } |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | return ret; |
| 1113 | } |
| 1114 | |
| 1115 | bool DisplayServerX11::clipboard_has_image() const { |
| 1116 | Atom target = _clipboard_get_image_target( |
| 1117 | XInternAtom(x11_display, "CLIPBOARD", 0), |
| 1118 | windows[MAIN_WINDOW_ID].x11_window); |
| 1119 | return target != None; |
| 1120 | } |
| 1121 | |
| 1122 | Bool DisplayServerX11::_predicate_clipboard_save_targets(Display *display, XEvent *event, XPointer arg) { |
| 1123 | if (event->xany.window == *(Window *)arg) { |
| 1124 | return (event->type == SelectionRequest) || |
| 1125 | (event->type == SelectionNotify); |
| 1126 | } else { |
| 1127 | return False; |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | void DisplayServerX11::_clipboard_transfer_ownership(Atom p_source, Window x11_window) const { |
| 1132 | _THREAD_SAFE_METHOD_ |
| 1133 | |
| 1134 | Window selection_owner = XGetSelectionOwner(x11_display, p_source); |
| 1135 | |
| 1136 | if (selection_owner != x11_window) { |
| 1137 | return; |
| 1138 | } |
| 1139 | |
| 1140 | // Block events polling while processing selection events. |
| 1141 | MutexLock mutex_lock(events_mutex); |
| 1142 | |
| 1143 | Atom clipboard_manager = XInternAtom(x11_display, "CLIPBOARD_MANAGER", False); |
| 1144 | Atom save_targets = XInternAtom(x11_display, "SAVE_TARGETS", False); |
| 1145 | XConvertSelection(x11_display, clipboard_manager, save_targets, None, |
| 1146 | x11_window, CurrentTime); |
| 1147 | |
| 1148 | // Process events from the queue. |
| 1149 | while (true) { |
| 1150 | if (!_wait_for_events()) { |
| 1151 | // Error or timeout, abort. |
| 1152 | break; |
| 1153 | } |
| 1154 | |
| 1155 | // Non-blocking wait for next event and remove it from the queue. |
| 1156 | XEvent ev; |
| 1157 | while (XCheckIfEvent(x11_display, &ev, _predicate_clipboard_save_targets, (XPointer)&x11_window)) { |
| 1158 | switch (ev.type) { |
| 1159 | case SelectionRequest: |
| 1160 | _handle_selection_request_event(&(ev.xselectionrequest)); |
| 1161 | break; |
| 1162 | |
| 1163 | case SelectionNotify: { |
| 1164 | if (ev.xselection.target == save_targets) { |
| 1165 | // Once SelectionNotify is received, we're done whether it succeeded or not. |
| 1166 | return; |
| 1167 | } |
| 1168 | |
| 1169 | break; |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | int DisplayServerX11::get_screen_count() const { |
| 1177 | _THREAD_SAFE_METHOD_ |
| 1178 | int count = 0; |
| 1179 | |
| 1180 | // Using Xinerama Extension |
| 1181 | int event_base, error_base; |
| 1182 | if (xinerama_ext_ok && XineramaQueryExtension(x11_display, &event_base, &error_base)) { |
| 1183 | XineramaScreenInfo *xsi = XineramaQueryScreens(x11_display, &count); |
| 1184 | XFree(xsi); |
| 1185 | } |
| 1186 | if (count == 0) { |
| 1187 | count = XScreenCount(x11_display); |
| 1188 | } |
| 1189 | |
| 1190 | return count; |
| 1191 | } |
| 1192 | |
| 1193 | int DisplayServerX11::get_primary_screen() const { |
| 1194 | int event_base, error_base; |
| 1195 | if (xinerama_ext_ok && XineramaQueryExtension(x11_display, &event_base, &error_base)) { |
| 1196 | return 0; |
| 1197 | } else { |
| 1198 | return XDefaultScreen(x11_display); |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | int DisplayServerX11::get_keyboard_focus_screen() const { |
| 1203 | int count = get_screen_count(); |
| 1204 | if (count < 2) { |
| 1205 | // Early exit with single monitor. |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
| 1209 | Window focus = 0; |
| 1210 | int revert_to = 0; |
| 1211 | |
| 1212 | XGetInputFocus(x11_display, &focus, &revert_to); |
| 1213 | if (focus) { |
| 1214 | Window focus_child = 0; |
| 1215 | int x = 0, y = 0; |
| 1216 | XTranslateCoordinates(x11_display, focus, DefaultRootWindow(x11_display), 0, 0, &x, &y, &focus_child); |
| 1217 | |
| 1218 | XWindowAttributes xwa; |
| 1219 | XGetWindowAttributes(x11_display, focus, &xwa); |
| 1220 | Rect2i window_rect = Rect2i(x, y, xwa.width, xwa.height); |
| 1221 | |
| 1222 | // Find which monitor has the largest overlap with the given window. |
| 1223 | int screen_index = 0; |
| 1224 | int max_area = 0; |
| 1225 | for (int i = 0; i < count; i++) { |
| 1226 | Rect2i screen_rect = _screen_get_rect(i); |
| 1227 | Rect2i intersection = screen_rect.intersection(window_rect); |
| 1228 | int area = intersection.get_area(); |
| 1229 | if (area > max_area) { |
| 1230 | max_area = area; |
| 1231 | screen_index = i; |
| 1232 | } |
| 1233 | } |
| 1234 | return screen_index; |
| 1235 | } |
| 1236 | |
| 1237 | return get_primary_screen(); |
| 1238 | } |
| 1239 | |
| 1240 | Rect2i DisplayServerX11::_screen_get_rect(int p_screen) const { |
| 1241 | Rect2i rect(0, 0, 0, 0); |
| 1242 | |
| 1243 | p_screen = _get_screen_index(p_screen); |
| 1244 | int screen_count = get_screen_count(); |
| 1245 | ERR_FAIL_INDEX_V(p_screen, screen_count, Rect2i()); |
| 1246 | |
| 1247 | // Using Xinerama Extension. |
| 1248 | bool found = false; |
| 1249 | int event_base, error_base; |
| 1250 | if (xinerama_ext_ok && XineramaQueryExtension(x11_display, &event_base, &error_base)) { |
| 1251 | int count; |
| 1252 | XineramaScreenInfo *xsi = XineramaQueryScreens(x11_display, &count); |
| 1253 | if (xsi) { |
| 1254 | if (count > 0) { |
| 1255 | // Check if screen is valid. |
| 1256 | if (p_screen < count) { |
| 1257 | rect.position.x = xsi[p_screen].x_org; |
| 1258 | rect.position.y = xsi[p_screen].y_org; |
| 1259 | rect.size.width = xsi[p_screen].width; |
| 1260 | rect.size.height = xsi[p_screen].height; |
| 1261 | found = true; |
| 1262 | } else { |
| 1263 | ERR_PRINT(vformat("Invalid screen index: %d (count: %d).", p_screen, count)); |
| 1264 | } |
| 1265 | } |
| 1266 | XFree(xsi); |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | if (!found) { |
| 1271 | int count = XScreenCount(x11_display); |
| 1272 | if (p_screen < count) { |
| 1273 | Window root = XRootWindow(x11_display, p_screen); |
| 1274 | XWindowAttributes xwa; |
| 1275 | XGetWindowAttributes(x11_display, root, &xwa); |
| 1276 | rect.position.x = xwa.x; |
| 1277 | rect.position.y = xwa.y; |
| 1278 | rect.size.width = xwa.width; |
| 1279 | rect.size.height = xwa.height; |
| 1280 | } else { |
| 1281 | ERR_PRINT(vformat("Invalid screen index: %d (count: %d).", p_screen, count)); |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | return rect; |
| 1286 | } |
| 1287 | |
| 1288 | Point2i DisplayServerX11::screen_get_position(int p_screen) const { |
| 1289 | _THREAD_SAFE_METHOD_ |
| 1290 | |
| 1291 | return _screen_get_rect(p_screen).position; |
| 1292 | } |
| 1293 | |
| 1294 | Size2i DisplayServerX11::screen_get_size(int p_screen) const { |
| 1295 | _THREAD_SAFE_METHOD_ |
| 1296 | |
| 1297 | return _screen_get_rect(p_screen).size; |
| 1298 | } |
| 1299 | |
| 1300 | // A Handler to avoid crashing on non-fatal X errors by default. |
| 1301 | // |
| 1302 | // The original X11 error formatter `_XPrintDefaultError` is defined here: |
| 1303 | // https://gitlab.freedesktop.org/xorg/lib/libx11/-/blob/e45ca7b41dcd3ace7681d6897505f85d374640f2/src/XlibInt.c#L1322 |
| 1304 | // It is not exposed through the API, accesses X11 internals, |
| 1305 | // and is much more complex, so this is a less complete simplified error X11 printer. |
| 1306 | int default_window_error_handler(Display *display, XErrorEvent *error) { |
| 1307 | static char message[1024]; |
| 1308 | XGetErrorText(display, error->error_code, message, sizeof(message)); |
| 1309 | |
| 1310 | ERR_PRINT(vformat("Unhandled XServer error: %s" |
| 1311 | "\n Major opcode of failed request: %d" |
| 1312 | "\n Serial number of failed request: %d" |
| 1313 | "\n Current serial number in output stream: %d", |
| 1314 | String::utf8(message), (uint64_t)error->request_code, (uint64_t)error->minor_code, (uint64_t)error->serial)); |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
| 1318 | bool g_bad_window = false; |
| 1319 | int bad_window_error_handler(Display *display, XErrorEvent *error) { |
| 1320 | if (error->error_code == BadWindow) { |
| 1321 | g_bad_window = true; |
| 1322 | } else { |
| 1323 | return default_window_error_handler(display, error); |
| 1324 | } |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | Rect2i DisplayServerX11::screen_get_usable_rect(int p_screen) const { |
| 1329 | _THREAD_SAFE_METHOD_ |
| 1330 | |
| 1331 | p_screen = _get_screen_index(p_screen); |
| 1332 | int screen_count = get_screen_count(); |
| 1333 | ERR_FAIL_INDEX_V(p_screen, screen_count, Rect2i()); |
| 1334 | |
| 1335 | bool is_multiscreen = screen_count > 1; |
| 1336 | |
| 1337 | // Use full monitor size as fallback. |
| 1338 | Rect2i rect = _screen_get_rect(p_screen); |
| 1339 | |
| 1340 | // There's generally only one screen reported by xlib even in multi-screen setup, |
| 1341 | // in this case it's just one virtual screen composed of all physical monitors. |
| 1342 | int x11_screen_count = ScreenCount(x11_display); |
| 1343 | Window x11_window = RootWindow(x11_display, p_screen < x11_screen_count ? p_screen : 0); |
| 1344 | |
| 1345 | Atom type; |
| 1346 | int format = 0; |
| 1347 | unsigned long remaining = 0; |
| 1348 | |
| 1349 | // Find active desktop for the root window. |
| 1350 | unsigned int desktop_index = 0; |
| 1351 | Atom desktop_prop = XInternAtom(x11_display, "_NET_CURRENT_DESKTOP", True); |
| 1352 | if (desktop_prop != None) { |
| 1353 | unsigned long desktop_len = 0; |
| 1354 | unsigned char *desktop_data = nullptr; |
| 1355 | if (XGetWindowProperty(x11_display, x11_window, desktop_prop, 0, LONG_MAX, False, XA_CARDINAL, &type, &format, &desktop_len, &remaining, &desktop_data) == Success) { |
| 1356 | if ((format == 32) && (desktop_len > 0) && desktop_data) { |
| 1357 | desktop_index = (unsigned int)desktop_data[0]; |
| 1358 | } |
| 1359 | if (desktop_data) { |
| 1360 | XFree(desktop_data); |
| 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | bool use_simple_method = true; |
| 1366 | |
| 1367 | // First check for GTK work area, which is more accurate for multi-screen setup. |
| 1368 | if (is_multiscreen) { |
| 1369 | // Use already calculated work area when available. |
| 1370 | Atom gtk_workareas_prop = XInternAtom(x11_display, "_GTK_WORKAREAS", False); |
| 1371 | if (gtk_workareas_prop != None) { |
| 1372 | char gtk_workarea_prop_name[32]; |
| 1373 | snprintf(gtk_workarea_prop_name, 32, "_GTK_WORKAREAS_D%d", desktop_index); |
| 1374 | Atom gtk_workarea_prop = XInternAtom(x11_display, gtk_workarea_prop_name, True); |
| 1375 | if (gtk_workarea_prop != None) { |
| 1376 | unsigned long workarea_len = 0; |
| 1377 | unsigned char *workarea_data = nullptr; |
| 1378 | if (XGetWindowProperty(x11_display, x11_window, gtk_workarea_prop, 0, LONG_MAX, False, XA_CARDINAL, &type, &format, &workarea_len, &remaining, &workarea_data) == Success) { |
| 1379 | if ((format == 32) && (workarea_len % 4 == 0) && workarea_data) { |
| 1380 | long *rect_data = (long *)workarea_data; |
| 1381 | for (uint32_t data_offset = 0; data_offset < workarea_len; data_offset += 4) { |
| 1382 | Rect2i workarea_rect; |
| 1383 | workarea_rect.position.x = rect_data[data_offset]; |
| 1384 | workarea_rect.position.y = rect_data[data_offset + 1]; |
| 1385 | workarea_rect.size.x = rect_data[data_offset + 2]; |
| 1386 | workarea_rect.size.y = rect_data[data_offset + 3]; |
| 1387 | |
| 1388 | // Intersect with actual monitor size to find the correct area, |
| 1389 | // because areas are not in the same order as screens from Xinerama. |
| 1390 | if (rect.grow(-1).intersects(workarea_rect)) { |
| 1391 | rect = rect.intersection(workarea_rect); |
| 1392 | XFree(workarea_data); |
| 1393 | return rect; |
| 1394 | } |
| 1395 | } |
| 1396 | } |
| 1397 | } |
| 1398 | if (workarea_data) { |
| 1399 | XFree(workarea_data); |
| 1400 | } |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | // Fallback to calculating work area by hand from struts. |
| 1405 | Atom client_list_prop = XInternAtom(x11_display, "_NET_CLIENT_LIST", True); |
| 1406 | if (client_list_prop != None) { |
| 1407 | unsigned long clients_len = 0; |
| 1408 | unsigned char *clients_data = nullptr; |
| 1409 | if (XGetWindowProperty(x11_display, x11_window, client_list_prop, 0, LONG_MAX, False, XA_WINDOW, &type, &format, &clients_len, &remaining, &clients_data) == Success) { |
| 1410 | if ((format == 32) && (clients_len > 0) && clients_data) { |
| 1411 | Window *windows_data = (Window *)clients_data; |
| 1412 | |
| 1413 | Rect2i desktop_rect; |
| 1414 | bool desktop_valid = false; |
| 1415 | |
| 1416 | // Get full desktop size. |
| 1417 | { |
| 1418 | Atom desktop_geometry_prop = XInternAtom(x11_display, "_NET_DESKTOP_GEOMETRY", True); |
| 1419 | if (desktop_geometry_prop != None) { |
| 1420 | unsigned long geom_len = 0; |
| 1421 | unsigned char *geom_data = nullptr; |
| 1422 | if (XGetWindowProperty(x11_display, x11_window, desktop_geometry_prop, 0, LONG_MAX, False, XA_CARDINAL, &type, &format, &geom_len, &remaining, &geom_data) == Success) { |
| 1423 | if ((format == 32) && (geom_len >= 2) && geom_data) { |
| 1424 | desktop_valid = true; |
| 1425 | long *size_data = (long *)geom_data; |
| 1426 | desktop_rect.size.x = size_data[0]; |
| 1427 | desktop_rect.size.y = size_data[1]; |
| 1428 | } |
| 1429 | } |
| 1430 | if (geom_data) { |
| 1431 | XFree(geom_data); |
| 1432 | } |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | // Get full desktop position. |
| 1437 | if (desktop_valid) { |
| 1438 | Atom desktop_viewport_prop = XInternAtom(x11_display, "_NET_DESKTOP_VIEWPORT", True); |
| 1439 | if (desktop_viewport_prop != None) { |
| 1440 | unsigned long viewport_len = 0; |
| 1441 | unsigned char *viewport_data = nullptr; |
| 1442 | if (XGetWindowProperty(x11_display, x11_window, desktop_viewport_prop, 0, LONG_MAX, False, XA_CARDINAL, &type, &format, &viewport_len, &remaining, &viewport_data) == Success) { |
| 1443 | if ((format == 32) && (viewport_len >= 2) && viewport_data) { |
| 1444 | desktop_valid = true; |
| 1445 | long *pos_data = (long *)viewport_data; |
| 1446 | desktop_rect.position.x = pos_data[0]; |
| 1447 | desktop_rect.position.y = pos_data[1]; |
| 1448 | } |
| 1449 | } |
| 1450 | if (viewport_data) { |
| 1451 | XFree(viewport_data); |
| 1452 | } |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | if (desktop_valid) { |
| 1457 | // Handle bad window errors silently because there's no other way to check |
| 1458 | // that one of the windows has been destroyed in the meantime. |
| 1459 | int (*oldHandler)(Display *, XErrorEvent *) = XSetErrorHandler(&bad_window_error_handler); |
| 1460 | |
| 1461 | for (unsigned long win_index = 0; win_index < clients_len; ++win_index) { |
| 1462 | g_bad_window = false; |
| 1463 | |
| 1464 | // Remove strut size from desktop size to get a more accurate result. |
| 1465 | bool strut_found = false; |
| 1466 | unsigned long strut_len = 0; |
| 1467 | unsigned char *strut_data = nullptr; |
| 1468 | Atom strut_partial_prop = XInternAtom(x11_display, "_NET_WM_STRUT_PARTIAL", True); |
| 1469 | if (strut_partial_prop != None) { |
| 1470 | if (XGetWindowProperty(x11_display, windows_data[win_index], strut_partial_prop, 0, LONG_MAX, False, XA_CARDINAL, &type, &format, &strut_len, &remaining, &strut_data) == Success) { |
| 1471 | strut_found = true; |
| 1472 | } |
| 1473 | } |
| 1474 | // Fallback to older strut property. |
| 1475 | if (!g_bad_window && !strut_found) { |
| 1476 | Atom strut_prop = XInternAtom(x11_display, "_NET_WM_STRUT", True); |
| 1477 | if (strut_prop != None) { |
| 1478 | if (XGetWindowProperty(x11_display, windows_data[win_index], strut_prop, 0, LONG_MAX, False, XA_CARDINAL, &type, &format, &strut_len, &remaining, &strut_data) == Success) { |
| 1479 | strut_found = true; |
| 1480 | } |
| 1481 | } |
| 1482 | } |
| 1483 | if (!g_bad_window && strut_found && (format == 32) && (strut_len >= 4) && strut_data) { |
| 1484 | use_simple_method = false; |
| 1485 | |
| 1486 | long *struts = (long *)strut_data; |
| 1487 | |
| 1488 | long left = struts[0]; |
| 1489 | long right = struts[1]; |
| 1490 | long top = struts[2]; |
| 1491 | long bottom = struts[3]; |
| 1492 | |
| 1493 | long left_start_y, left_end_y, right_start_y, right_end_y; |
| 1494 | long top_start_x, top_end_x, bottom_start_x, bottom_end_x; |
| 1495 | |
| 1496 | if (strut_len >= 12) { |
| 1497 | left_start_y = struts[4]; |
| 1498 | left_end_y = struts[5]; |
| 1499 | right_start_y = struts[6]; |
| 1500 | right_end_y = struts[7]; |
| 1501 | top_start_x = struts[8]; |
| 1502 | top_end_x = struts[9]; |
| 1503 | bottom_start_x = struts[10]; |
| 1504 | bottom_end_x = struts[11]; |
| 1505 | } else { |
| 1506 | left_start_y = 0; |
| 1507 | left_end_y = desktop_rect.size.y; |
| 1508 | right_start_y = 0; |
| 1509 | right_end_y = desktop_rect.size.y; |
| 1510 | top_start_x = 0; |
| 1511 | top_end_x = desktop_rect.size.x; |
| 1512 | bottom_start_x = 0; |
| 1513 | bottom_end_x = desktop_rect.size.x; |
| 1514 | } |
| 1515 | |
| 1516 | const Point2i &pos = desktop_rect.position; |
| 1517 | const Size2i &size = desktop_rect.size; |
| 1518 | |
| 1519 | Rect2i left_rect(pos.x, pos.y + left_start_y, left, left_end_y - left_start_y); |
| 1520 | if (left_rect.size.x > 0) { |
| 1521 | Rect2i intersection = rect.intersection(left_rect); |
| 1522 | if (intersection.has_area() && intersection.size.x < rect.size.x) { |
| 1523 | rect.position.x = left_rect.size.x; |
| 1524 | rect.size.x = rect.size.x - intersection.size.x; |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | Rect2i right_rect(pos.x + size.x - right, pos.y + right_start_y, right, right_end_y - right_start_y); |
| 1529 | if (right_rect.size.x > 0) { |
| 1530 | Rect2i intersection = rect.intersection(right_rect); |
| 1531 | if (intersection.has_area() && right_rect.size.x < rect.size.x) { |
| 1532 | rect.size.x = intersection.position.x - rect.position.x; |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | Rect2i top_rect(pos.x + top_start_x, pos.y, top_end_x - top_start_x, top); |
| 1537 | if (top_rect.size.y > 0) { |
| 1538 | Rect2i intersection = rect.intersection(top_rect); |
| 1539 | if (intersection.has_area() && intersection.size.y < rect.size.y) { |
| 1540 | rect.position.y = top_rect.size.y; |
| 1541 | rect.size.y = rect.size.y - intersection.size.y; |
| 1542 | } |
| 1543 | } |
| 1544 | |
| 1545 | Rect2i bottom_rect(pos.x + bottom_start_x, pos.y + size.y - bottom, bottom_end_x - bottom_start_x, bottom); |
| 1546 | if (bottom_rect.size.y > 0) { |
| 1547 | Rect2i intersection = rect.intersection(bottom_rect); |
| 1548 | if (intersection.has_area() && right_rect.size.y < rect.size.y) { |
| 1549 | rect.size.y = intersection.position.y - rect.position.y; |
| 1550 | } |
| 1551 | } |
| 1552 | } |
| 1553 | if (strut_data) { |
| 1554 | XFree(strut_data); |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | // Restore default error handler. |
| 1559 | XSetErrorHandler(oldHandler); |
| 1560 | } |
| 1561 | } |
| 1562 | } |
| 1563 | if (clients_data) { |
| 1564 | XFree(clients_data); |
| 1565 | } |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | // Single screen or fallback for multi screen. |
| 1570 | if (use_simple_method) { |
| 1571 | // Get desktop available size from the global work area. |
| 1572 | Atom workarea_prop = XInternAtom(x11_display, "_NET_WORKAREA", True); |
| 1573 | if (workarea_prop != None) { |
| 1574 | unsigned long workarea_len = 0; |
| 1575 | unsigned char *workarea_data = nullptr; |
| 1576 | if (XGetWindowProperty(x11_display, x11_window, workarea_prop, 0, LONG_MAX, False, XA_CARDINAL, &type, &format, &workarea_len, &remaining, &workarea_data) == Success) { |
| 1577 | if ((format == 32) && (workarea_len >= ((desktop_index + 1) * 4)) && workarea_data) { |
| 1578 | long *rect_data = (long *)workarea_data; |
| 1579 | int data_offset = desktop_index * 4; |
| 1580 | Rect2i workarea_rect; |
| 1581 | workarea_rect.position.x = rect_data[data_offset]; |
| 1582 | workarea_rect.position.y = rect_data[data_offset + 1]; |
| 1583 | workarea_rect.size.x = rect_data[data_offset + 2]; |
| 1584 | workarea_rect.size.y = rect_data[data_offset + 3]; |
| 1585 | |
| 1586 | // Intersect with actual monitor size to get a proper approximation in multi-screen setup. |
| 1587 | if (!is_multiscreen) { |
| 1588 | rect = workarea_rect; |
| 1589 | } else if (rect.intersects(workarea_rect)) { |
| 1590 | rect = rect.intersection(workarea_rect); |
| 1591 | } |
| 1592 | } |
| 1593 | } |
| 1594 | if (workarea_data) { |
| 1595 | XFree(workarea_data); |
| 1596 | } |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | return rect; |
| 1601 | } |
| 1602 | |
| 1603 | Rect2i DisplayServerX11::_screens_get_full_rect() const { |
| 1604 | Rect2i full_rect; |
| 1605 | |
| 1606 | int count = get_screen_count(); |
| 1607 | for (int i = 0; i < count; i++) { |
| 1608 | if (i == 0) { |
| 1609 | full_rect = _screen_get_rect(i); |
| 1610 | continue; |
| 1611 | } |
| 1612 | |
| 1613 | Rect2i screen_rect = _screen_get_rect(i); |
| 1614 | if (full_rect.position.x > screen_rect.position.x) { |
| 1615 | full_rect.size.x += full_rect.position.x - screen_rect.position.x; |
| 1616 | full_rect.position.x = screen_rect.position.x; |
| 1617 | } |
| 1618 | if (full_rect.position.y > screen_rect.position.y) { |
| 1619 | full_rect.size.y += full_rect.position.y - screen_rect.position.y; |
| 1620 | full_rect.position.y = screen_rect.position.y; |
| 1621 | } |
| 1622 | if (full_rect.position.x + full_rect.size.x < screen_rect.position.x + screen_rect.size.x) { |
| 1623 | full_rect.size.x = screen_rect.position.x + screen_rect.size.x - full_rect.position.x; |
| 1624 | } |
| 1625 | if (full_rect.position.y + full_rect.size.y < screen_rect.position.y + screen_rect.size.y) { |
| 1626 | full_rect.size.y = screen_rect.position.y + screen_rect.size.y - full_rect.position.y; |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | return full_rect; |
| 1631 | } |
| 1632 | |
| 1633 | int DisplayServerX11::screen_get_dpi(int p_screen) const { |
| 1634 | _THREAD_SAFE_METHOD_ |
| 1635 | |
| 1636 | p_screen = _get_screen_index(p_screen); |
| 1637 | int screen_count = get_screen_count(); |
| 1638 | ERR_FAIL_INDEX_V(p_screen, screen_count, 96); |
| 1639 | |
| 1640 | //Get physical monitor Dimensions through XRandR and calculate dpi |
| 1641 | Size2i sc = screen_get_size(p_screen); |
| 1642 | if (xrandr_ext_ok) { |
| 1643 | int count = 0; |
| 1644 | if (xrr_get_monitors) { |
| 1645 | xrr_monitor_info *monitors = xrr_get_monitors(x11_display, windows[MAIN_WINDOW_ID].x11_window, true, &count); |
| 1646 | if (p_screen < count) { |
| 1647 | double xdpi = sc.width / (double)monitors[p_screen].mwidth * 25.4; |
| 1648 | double ydpi = sc.height / (double)monitors[p_screen].mheight * 25.4; |
| 1649 | xrr_free_monitors(monitors); |
| 1650 | return (xdpi + ydpi) / 2; |
| 1651 | } |
| 1652 | xrr_free_monitors(monitors); |
| 1653 | } else if (p_screen == 0) { |
| 1654 | XRRScreenSize *sizes = XRRSizes(x11_display, 0, &count); |
| 1655 | if (sizes) { |
| 1656 | double xdpi = sc.width / (double)sizes[0].mwidth * 25.4; |
| 1657 | double ydpi = sc.height / (double)sizes[0].mheight * 25.4; |
| 1658 | return (xdpi + ydpi) / 2; |
| 1659 | } |
| 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | int width_mm = DisplayWidthMM(x11_display, p_screen); |
| 1664 | int height_mm = DisplayHeightMM(x11_display, p_screen); |
| 1665 | double xdpi = (width_mm ? sc.width / (double)width_mm * 25.4 : 0); |
| 1666 | double ydpi = (height_mm ? sc.height / (double)height_mm * 25.4 : 0); |
| 1667 | if (xdpi || ydpi) { |
| 1668 | return (xdpi + ydpi) / (xdpi && ydpi ? 2 : 1); |
| 1669 | } |
| 1670 | |
| 1671 | //could not get dpi |
| 1672 | return 96; |
| 1673 | } |
| 1674 | |
| 1675 | int get_image_errorhandler(Display *dpy, XErrorEvent *ev) { |
| 1676 | return 0; |
| 1677 | } |
| 1678 | |
| 1679 | Color DisplayServerX11::screen_get_pixel(const Point2i &p_position) const { |
| 1680 | Point2i pos = p_position; |
| 1681 | |
| 1682 | if (xwayland) { |
| 1683 | return Color(); |
| 1684 | } |
| 1685 | |
| 1686 | int (*old_handler)(Display *, XErrorEvent *) = XSetErrorHandler(&get_image_errorhandler); |
| 1687 | |
| 1688 | Color color; |
| 1689 | int number_of_screens = XScreenCount(x11_display); |
| 1690 | for (int i = 0; i < number_of_screens; i++) { |
| 1691 | Window root = XRootWindow(x11_display, i); |
| 1692 | XWindowAttributes root_attrs; |
| 1693 | XGetWindowAttributes(x11_display, root, &root_attrs); |
| 1694 | if ((pos.x >= root_attrs.x) && (pos.x <= root_attrs.x + root_attrs.width) && (pos.y >= root_attrs.y) && (pos.y <= root_attrs.y + root_attrs.height)) { |
| 1695 | XImage *image = XGetImage(x11_display, root, pos.x, pos.y, 1, 1, AllPlanes, XYPixmap); |
| 1696 | if (image) { |
| 1697 | XColor c; |
| 1698 | c.pixel = XGetPixel(image, 0, 0); |
| 1699 | XDestroyImage(image); |
| 1700 | XQueryColor(x11_display, XDefaultColormap(x11_display, i), &c); |
| 1701 | color = Color(float(c.red) / 65535.0, float(c.green) / 65535.0, float(c.blue) / 65535.0, 1.0); |
| 1702 | break; |
| 1703 | } |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | XSetErrorHandler(old_handler); |
| 1708 | |
| 1709 | return color; |
| 1710 | } |
| 1711 | |
| 1712 | Ref<Image> DisplayServerX11::screen_get_image(int p_screen) const { |
| 1713 | ERR_FAIL_INDEX_V(p_screen, get_screen_count(), Ref<Image>()); |
| 1714 | |
| 1715 | p_screen = _get_screen_index(p_screen); |
| 1716 | int screen_count = get_screen_count(); |
| 1717 | ERR_FAIL_INDEX_V(p_screen, screen_count, Ref<Image>()); |
| 1718 | |
| 1719 | if (xwayland) { |
| 1720 | return Ref<Image>(); |
| 1721 | } |
| 1722 | |
| 1723 | int (*old_handler)(Display *, XErrorEvent *) = XSetErrorHandler(&get_image_errorhandler); |
| 1724 | |
| 1725 | XImage *image = nullptr; |
| 1726 | |
| 1727 | bool found = false; |
| 1728 | int event_base, error_base; |
| 1729 | if (xinerama_ext_ok && XineramaQueryExtension(x11_display, &event_base, &error_base)) { |
| 1730 | int xin_count; |
| 1731 | XineramaScreenInfo *xsi = XineramaQueryScreens(x11_display, &xin_count); |
| 1732 | if (xsi) { |
| 1733 | if (xin_count > 0) { |
| 1734 | if (p_screen < xin_count) { |
| 1735 | int x_count = XScreenCount(x11_display); |
| 1736 | for (int i = 0; i < x_count; i++) { |
| 1737 | Window root = XRootWindow(x11_display, i); |
| 1738 | XWindowAttributes root_attrs; |
| 1739 | XGetWindowAttributes(x11_display, root, &root_attrs); |
| 1740 | if ((xsi[p_screen].x_org >= root_attrs.x) && (xsi[p_screen].x_org <= root_attrs.x + root_attrs.width) && (xsi[p_screen].y_org >= root_attrs.y) && (xsi[p_screen].y_org <= root_attrs.y + root_attrs.height)) { |
| 1741 | found = true; |
| 1742 | image = XGetImage(x11_display, root, xsi[p_screen].x_org, xsi[p_screen].y_org, xsi[p_screen].width, xsi[p_screen].height, AllPlanes, ZPixmap); |
| 1743 | break; |
| 1744 | } |
| 1745 | } |
| 1746 | } else { |
| 1747 | ERR_PRINT(vformat("Invalid screen index: %d (count: %d).", p_screen, xin_count)); |
| 1748 | } |
| 1749 | } |
| 1750 | XFree(xsi); |
| 1751 | } |
| 1752 | } |
| 1753 | if (!found) { |
| 1754 | int x_count = XScreenCount(x11_display); |
| 1755 | if (p_screen < x_count) { |
| 1756 | Window root = XRootWindow(x11_display, p_screen); |
| 1757 | |
| 1758 | XWindowAttributes root_attrs; |
| 1759 | XGetWindowAttributes(x11_display, root, &root_attrs); |
| 1760 | |
| 1761 | image = XGetImage(x11_display, root, root_attrs.x, root_attrs.y, root_attrs.width, root_attrs.height, AllPlanes, ZPixmap); |
| 1762 | } else { |
| 1763 | ERR_PRINT(vformat("Invalid screen index: %d (count: %d).", p_screen, x_count)); |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | XSetErrorHandler(old_handler); |
| 1768 | |
| 1769 | Ref<Image> img; |
| 1770 | if (image) { |
| 1771 | int width = image->width; |
| 1772 | int height = image->height; |
| 1773 | |
| 1774 | Vector<uint8_t> img_data; |
| 1775 | img_data.resize(height * width * 4); |
| 1776 | |
| 1777 | uint8_t *sr = (uint8_t *)image->data; |
| 1778 | uint8_t *wr = (uint8_t *)img_data.ptrw(); |
| 1779 | |
| 1780 | if (image->bits_per_pixel == 24 && image->red_mask == 0xff0000 && image->green_mask == 0x00ff00 && image->blue_mask == 0x0000ff) { |
| 1781 | for (int y = 0; y < height; y++) { |
| 1782 | for (int x = 0; x < width; x++) { |
| 1783 | wr[(y * width + x) * 4 + 0] = sr[(y * width + x) * 3 + 2]; |
| 1784 | wr[(y * width + x) * 4 + 1] = sr[(y * width + x) * 3 + 1]; |
| 1785 | wr[(y * width + x) * 4 + 2] = sr[(y * width + x) * 3 + 0]; |
| 1786 | wr[(y * width + x) * 4 + 3] = 255; |
| 1787 | } |
| 1788 | } |
| 1789 | } else if (image->bits_per_pixel == 24 && image->red_mask == 0x0000ff && image->green_mask == 0x00ff00 && image->blue_mask == 0xff0000) { |
| 1790 | for (int y = 0; y < height; y++) { |
| 1791 | for (int x = 0; x < width; x++) { |
| 1792 | wr[(y * width + x) * 4 + 0] = sr[(y * width + x) * 3 + 2]; |
| 1793 | wr[(y * width + x) * 4 + 1] = sr[(y * width + x) * 3 + 1]; |
| 1794 | wr[(y * width + x) * 4 + 2] = sr[(y * width + x) * 3 + 0]; |
| 1795 | wr[(y * width + x) * 4 + 3] = 255; |
| 1796 | } |
| 1797 | } |
| 1798 | } else if (image->bits_per_pixel == 32 && image->red_mask == 0xff0000 && image->green_mask == 0x00ff00 && image->blue_mask == 0x0000ff) { |
| 1799 | for (int y = 0; y < height; y++) { |
| 1800 | for (int x = 0; x < width; x++) { |
| 1801 | wr[(y * width + x) * 4 + 0] = sr[(y * width + x) * 4 + 2]; |
| 1802 | wr[(y * width + x) * 4 + 1] = sr[(y * width + x) * 4 + 1]; |
| 1803 | wr[(y * width + x) * 4 + 2] = sr[(y * width + x) * 4 + 0]; |
| 1804 | wr[(y * width + x) * 4 + 3] = 255; |
| 1805 | } |
| 1806 | } |
| 1807 | } else { |
| 1808 | String msg = vformat("XImage with RGB mask %x %x %x and depth %d is not supported.", (uint64_t)image->red_mask, (uint64_t)image->green_mask, (uint64_t)image->blue_mask, (int64_t)image->bits_per_pixel); |
| 1809 | XDestroyImage(image); |
| 1810 | ERR_FAIL_V_MSG(Ref<Image>(), msg); |
| 1811 | } |
| 1812 | img = Image::create_from_data(width, height, false, Image::FORMAT_RGBA8, img_data); |
| 1813 | XDestroyImage(image); |
| 1814 | } |
| 1815 | |
| 1816 | return img; |
| 1817 | } |
| 1818 | |
| 1819 | float DisplayServerX11::screen_get_refresh_rate(int p_screen) const { |
| 1820 | _THREAD_SAFE_METHOD_ |
| 1821 | |
| 1822 | ERR_FAIL_COND_V_MSG(!xrandr_ext_ok || !xrr_get_monitors, SCREEN_REFRESH_RATE_FALLBACK, "XRandR extension is not available."); |
| 1823 | |
| 1824 | p_screen = _get_screen_index(p_screen); |
| 1825 | int screen_count = get_screen_count(); |
| 1826 | ERR_FAIL_INDEX_V(p_screen, screen_count, SCREEN_REFRESH_RATE_FALLBACK); |
| 1827 | |
| 1828 | int target_x; |
| 1829 | int target_y; |
| 1830 | { |
| 1831 | int count = 0; |
| 1832 | xrr_monitor_info *monitors = xrr_get_monitors(x11_display, windows[MAIN_WINDOW_ID].x11_window, true, &count); |
| 1833 | ERR_FAIL_NULL_V(monitors, SCREEN_REFRESH_RATE_FALLBACK); |
| 1834 | if (count <= p_screen) { |
| 1835 | xrr_free_monitors(monitors); |
| 1836 | ERR_FAIL_V_MSG(SCREEN_REFRESH_RATE_FALLBACK, vformat("Invalid screen index: %d (count: %d).", p_screen, count)); |
| 1837 | } |
| 1838 | target_x = monitors[p_screen].x; |
| 1839 | target_y = monitors[p_screen].y; |
| 1840 | xrr_free_monitors(monitors); |
| 1841 | } |
| 1842 | |
| 1843 | XRRScreenResources *screen_res = XRRGetScreenResourcesCurrent(x11_display, windows[MAIN_WINDOW_ID].x11_window); |
| 1844 | ERR_FAIL_NULL_V(screen_res, SCREEN_REFRESH_RATE_FALLBACK); |
| 1845 | |
| 1846 | XRRModeInfo *mode_info = nullptr; |
| 1847 | for (int crtc = 0; crtc < screen_res->ncrtc; crtc++) { // Loop through outputs to find which one is currently outputting. |
| 1848 | XRRCrtcInfo *monitor_info = XRRGetCrtcInfo(x11_display, screen_res, screen_res->crtcs[crtc]); |
| 1849 | if (monitor_info->x != target_x || monitor_info->y != target_y || monitor_info->mode == None) { |
| 1850 | XRRFreeCrtcInfo(monitor_info); |
| 1851 | continue; |
| 1852 | } |
| 1853 | for (int mode = 0; mode < screen_res->nmode; mode++) { |
| 1854 | if (screen_res->modes[mode].id == monitor_info->mode) { |
| 1855 | mode_info = &screen_res->modes[mode]; |
| 1856 | } |
| 1857 | } |
| 1858 | XRRFreeCrtcInfo(monitor_info); |
| 1859 | break; |
| 1860 | } |
| 1861 | |
| 1862 | float result; |
| 1863 | if (mode_info) { |
| 1864 | // Snap to nearest 0.01 to stay consistent with other platforms. |
| 1865 | result = Math::snapped((float)mode_info->dotClock / ((float)mode_info->hTotal * (float)mode_info->vTotal), 0.01); |
| 1866 | } else { |
| 1867 | ERR_PRINT("An error occurred while trying to get the screen refresh rate."); |
| 1868 | result = SCREEN_REFRESH_RATE_FALLBACK; |
| 1869 | } |
| 1870 | |
| 1871 | XRRFreeScreenResources(screen_res); |
| 1872 | return result; |
| 1873 | } |
| 1874 | |
| 1875 | #ifdef DBUS_ENABLED |
| 1876 | void DisplayServerX11::screen_set_keep_on(bool p_enable) { |
| 1877 | if (screen_is_kept_on() == p_enable) { |
| 1878 | return; |
| 1879 | } |
| 1880 | |
| 1881 | if (portal_desktop && portal_desktop->is_inhibit_supported()) { |
| 1882 | if (p_enable) { |
| 1883 | // Attach the inhibit request to the main window, not the last focused window, |
| 1884 | // on the basis that inhibiting the screensaver is global state for the application. |
| 1885 | WindowID window_id = MAIN_WINDOW_ID; |
| 1886 | String xid = vformat("x11:%x", (uint64_t)windows[window_id].x11_window); |
| 1887 | keep_screen_on = portal_desktop->inhibit(xid); |
| 1888 | } else { |
| 1889 | portal_desktop->uninhibit(); |
| 1890 | keep_screen_on = false; |
| 1891 | } |
| 1892 | } else if (screensaver) { |
| 1893 | if (p_enable) { |
| 1894 | screensaver->inhibit(); |
| 1895 | } else { |
| 1896 | screensaver->uninhibit(); |
| 1897 | } |
| 1898 | |
| 1899 | keep_screen_on = p_enable; |
| 1900 | } |
| 1901 | } |
| 1902 | |
| 1903 | bool DisplayServerX11::screen_is_kept_on() const { |
| 1904 | return keep_screen_on; |
| 1905 | } |
| 1906 | #endif |
| 1907 | |
| 1908 | Vector<DisplayServer::WindowID> DisplayServerX11::get_window_list() const { |
| 1909 | _THREAD_SAFE_METHOD_ |
| 1910 | |
| 1911 | Vector<int> ret; |
| 1912 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 1913 | ret.push_back(E.key); |
| 1914 | } |
| 1915 | return ret; |
| 1916 | } |
| 1917 | |
| 1918 | DisplayServer::WindowID DisplayServerX11::create_sub_window(WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Rect2i &p_rect, bool p_exclusive, WindowID p_transient_parent) { |
| 1919 | _THREAD_SAFE_METHOD_ |
| 1920 | |
| 1921 | WindowID id = _create_window(p_mode, p_vsync_mode, p_flags, p_rect, 0); |
| 1922 | for (int i = 0; i < WINDOW_FLAG_MAX; i++) { |
| 1923 | if (p_flags & (1 << i)) { |
| 1924 | window_set_flag(WindowFlags(i), true, id); |
| 1925 | } |
| 1926 | } |
| 1927 | #ifdef RD_ENABLED |
| 1928 | if (rendering_device) { |
| 1929 | rendering_device->screen_create(id); |
| 1930 | } |
| 1931 | #endif |
| 1932 | |
| 1933 | if (p_transient_parent != INVALID_WINDOW_ID) { |
| 1934 | window_set_transient(id, p_transient_parent); |
| 1935 | } |
| 1936 | |
| 1937 | return id; |
| 1938 | } |
| 1939 | |
| 1940 | void DisplayServerX11::show_window(WindowID p_id) { |
| 1941 | _THREAD_SAFE_METHOD_ |
| 1942 | |
| 1943 | WindowData &wd = windows[p_id]; |
| 1944 | popup_open(p_id); |
| 1945 | |
| 1946 | DEBUG_LOG_X11("show_window: %lu (%u) \n", wd.x11_window, p_id); |
| 1947 | |
| 1948 | // Setup initial minimize/maximize state. |
| 1949 | // `_NET_WM_STATE` can be set directly when the window is unmapped. |
| 1950 | LocalVector<Atom> hints; |
| 1951 | if (wd.maximized) { |
| 1952 | hints.push_back(XInternAtom(x11_display, "_NET_WM_STATE_MAXIMIZED_VERT", False)); |
| 1953 | hints.push_back(XInternAtom(x11_display, "_NET_WM_STATE_MAXIMIZED_HORZ", False)); |
| 1954 | } |
| 1955 | if (wd.minimized) { |
| 1956 | hints.push_back(XInternAtom(x11_display, "_NET_WM_STATE_HIDDEN", False)); |
| 1957 | } |
| 1958 | XChangeProperty(x11_display, wd.x11_window, XInternAtom(x11_display, "_NET_WM_STATE", False), XA_ATOM, 32, PropModeReplace, (unsigned char *)hints.ptr(), hints.size()); |
| 1959 | |
| 1960 | _update_motif_wm_hints(p_id); |
| 1961 | |
| 1962 | XMapWindow(x11_display, wd.x11_window); |
| 1963 | XSync(x11_display, False); |
| 1964 | |
| 1965 | _validate_fullscreen_on_map(p_id); |
| 1966 | |
| 1967 | if (p_id == MAIN_WINDOW_ID) { |
| 1968 | // Get main window size for boot splash drawing. |
| 1969 | bool get_config_event = false; |
| 1970 | |
| 1971 | // Search the X11 event queue for ConfigureNotify events and process all that are currently queued early. |
| 1972 | { |
| 1973 | MutexLock mutex_lock(events_mutex); |
| 1974 | |
| 1975 | for (uint32_t event_index = 0; event_index < polled_events.size(); ++event_index) { |
| 1976 | XEvent &config_event = polled_events[event_index]; |
| 1977 | if (config_event.type == ConfigureNotify) { |
| 1978 | _window_changed(&config_event); |
| 1979 | get_config_event = true; |
| 1980 | } |
| 1981 | } |
| 1982 | XEvent config_event; |
| 1983 | while (XCheckTypedEvent(x11_display, ConfigureNotify, &config_event)) { |
| 1984 | _window_changed(&config_event); |
| 1985 | get_config_event = true; |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | // Estimate maximize/full screen window size, ConfigureNotify may arrive only after maximize animation is finished. |
| 1990 | if (!get_config_event && (wd.maximized || wd.fullscreen)) { |
| 1991 | int screen = window_get_current_screen(p_id); |
| 1992 | Size2i sz; |
| 1993 | if (wd.maximized) { |
| 1994 | sz = screen_get_usable_rect(screen).size; |
| 1995 | if (!wd.borderless) { |
| 1996 | Atom prop = XInternAtom(x11_display, "_NET_FRAME_EXTENTS", True); |
| 1997 | if (prop != None) { |
| 1998 | Atom type; |
| 1999 | int format; |
| 2000 | unsigned long len; |
| 2001 | unsigned long remaining; |
| 2002 | unsigned char *data = nullptr; |
| 2003 | if (XGetWindowProperty(x11_display, wd.x11_window, prop, 0, 4, False, AnyPropertyType, &type, &format, &len, &remaining, &data) == Success) { |
| 2004 | if (format == 32 && len == 4 && data) { |
| 2005 | long *extents = (long *)data; |
| 2006 | sz.width -= extents[0] + extents[1]; // left, right |
| 2007 | sz.height -= extents[2] + extents[3]; // top, bottom |
| 2008 | } |
| 2009 | XFree(data); |
| 2010 | } |
| 2011 | } |
| 2012 | } |
| 2013 | } else { |
| 2014 | sz = screen_get_size(screen); |
| 2015 | } |
| 2016 | if (sz == Size2i()) { |
| 2017 | return; |
| 2018 | } |
| 2019 | |
| 2020 | wd.size = sz; |
| 2021 | #if defined(RD_ENABLED) |
| 2022 | if (rendering_context) { |
| 2023 | rendering_context->window_set_size(p_id, sz.width, sz.height); |
| 2024 | } |
| 2025 | #endif |
| 2026 | #if defined(GLES3_ENABLED) |
| 2027 | if (gl_manager) { |
| 2028 | gl_manager->window_resize(p_id, sz.width, sz.height); |
| 2029 | } |
| 2030 | if (gl_manager_egl) { |
| 2031 | gl_manager_egl->window_resize(p_id, sz.width, sz.height); |
| 2032 | } |
| 2033 | #endif |
| 2034 | } |
| 2035 | } |
| 2036 | } |
| 2037 | |
| 2038 | void DisplayServerX11::delete_sub_window(WindowID p_id) { |
| 2039 | _THREAD_SAFE_METHOD_ |
| 2040 | |
| 2041 | ERR_FAIL_COND(!windows.has(p_id)); |
| 2042 | ERR_FAIL_COND_MSG(p_id == MAIN_WINDOW_ID, "Main window can't be deleted"); |
| 2043 | |
| 2044 | popup_close(p_id); |
| 2045 | |
| 2046 | WindowData &wd = windows[p_id]; |
| 2047 | |
| 2048 | DEBUG_LOG_X11("delete_sub_window: %lu (%u) \n", wd.x11_window, p_id); |
| 2049 | |
| 2050 | if (window_mouseover_id == p_id) { |
| 2051 | window_mouseover_id = INVALID_WINDOW_ID; |
| 2052 | _send_window_event(windows[p_id], WINDOW_EVENT_MOUSE_EXIT); |
| 2053 | } |
| 2054 | |
| 2055 | while (wd.transient_children.size()) { |
| 2056 | window_set_transient(*wd.transient_children.begin(), INVALID_WINDOW_ID); |
| 2057 | } |
| 2058 | |
| 2059 | if (wd.transient_parent != INVALID_WINDOW_ID) { |
| 2060 | window_set_transient(p_id, INVALID_WINDOW_ID); |
| 2061 | } |
| 2062 | |
| 2063 | #if defined(RD_ENABLED) |
| 2064 | if (rendering_device) { |
| 2065 | rendering_device->screen_free(p_id); |
| 2066 | } |
| 2067 | |
| 2068 | if (rendering_context) { |
| 2069 | rendering_context->window_destroy(p_id); |
| 2070 | } |
| 2071 | #endif |
| 2072 | #ifdef GLES3_ENABLED |
| 2073 | if (gl_manager) { |
| 2074 | gl_manager->window_destroy(p_id); |
| 2075 | } |
| 2076 | if (gl_manager_egl) { |
| 2077 | gl_manager_egl->window_destroy(p_id); |
| 2078 | } |
| 2079 | #endif |
| 2080 | |
| 2081 | #ifdef ACCESSKIT_ENABLED |
| 2082 | if (accessibility_driver) { |
| 2083 | accessibility_driver->window_destroy(p_id); |
| 2084 | } |
| 2085 | #endif |
| 2086 | |
| 2087 | if (wd.xic) { |
| 2088 | XDestroyIC(wd.xic); |
| 2089 | wd.xic = nullptr; |
| 2090 | } |
| 2091 | XDestroyWindow(x11_display, wd.x11_xim_window); |
| 2092 | #ifdef XKB_ENABLED |
| 2093 | if (xkb_loaded_v05p) { |
| 2094 | if (wd.xkb_state) { |
| 2095 | xkb_compose_state_unref(wd.xkb_state); |
| 2096 | wd.xkb_state = nullptr; |
| 2097 | } |
| 2098 | } |
| 2099 | #endif |
| 2100 | |
| 2101 | XUnmapWindow(x11_display, wd.x11_window); |
| 2102 | XDestroyWindow(x11_display, wd.x11_window); |
| 2103 | |
| 2104 | window_set_rect_changed_callback(Callable(), p_id); |
| 2105 | window_set_window_event_callback(Callable(), p_id); |
| 2106 | window_set_input_event_callback(Callable(), p_id); |
| 2107 | window_set_input_text_callback(Callable(), p_id); |
| 2108 | window_set_drop_files_callback(Callable(), p_id); |
| 2109 | |
| 2110 | windows.erase(p_id); |
| 2111 | |
| 2112 | if (last_focused_window == p_id) { |
| 2113 | last_focused_window = INVALID_WINDOW_ID; |
| 2114 | } |
| 2115 | } |
| 2116 | |
| 2117 | int64_t DisplayServerX11::window_get_native_handle(HandleType p_handle_type, WindowID p_window) const { |
| 2118 | ERR_FAIL_COND_V(!windows.has(p_window), 0); |
| 2119 | switch (p_handle_type) { |
| 2120 | case DISPLAY_HANDLE: { |
| 2121 | return (int64_t)x11_display; |
| 2122 | } |
| 2123 | case WINDOW_HANDLE: { |
| 2124 | return (int64_t)windows[p_window].x11_window; |
| 2125 | } |
| 2126 | case WINDOW_VIEW: { |
| 2127 | return 0; // Not supported. |
| 2128 | } |
| 2129 | #ifdef GLES3_ENABLED |
| 2130 | case OPENGL_CONTEXT: { |
| 2131 | if (gl_manager) { |
| 2132 | return (int64_t)gl_manager->get_glx_context(p_window); |
| 2133 | } |
| 2134 | if (gl_manager_egl) { |
| 2135 | return (int64_t)gl_manager_egl->get_context(p_window); |
| 2136 | } |
| 2137 | return 0; |
| 2138 | } |
| 2139 | case EGL_DISPLAY: { |
| 2140 | if (gl_manager_egl) { |
| 2141 | return (int64_t)gl_manager_egl->get_display(p_window); |
| 2142 | } |
| 2143 | return 0; |
| 2144 | } |
| 2145 | case EGL_CONFIG: { |
| 2146 | if (gl_manager_egl) { |
| 2147 | return (int64_t)gl_manager_egl->get_config(p_window); |
| 2148 | } |
| 2149 | return 0; |
| 2150 | } |
| 2151 | #endif |
| 2152 | default: { |
| 2153 | return 0; |
| 2154 | } |
| 2155 | } |
| 2156 | } |
| 2157 | |
| 2158 | void DisplayServerX11::window_attach_instance_id(ObjectID p_instance, WindowID p_window) { |
| 2159 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2160 | WindowData &wd = windows[p_window]; |
| 2161 | |
| 2162 | wd.instance_id = p_instance; |
| 2163 | } |
| 2164 | |
| 2165 | ObjectID DisplayServerX11::window_get_attached_instance_id(WindowID p_window) const { |
| 2166 | ERR_FAIL_COND_V(!windows.has(p_window), ObjectID()); |
| 2167 | const WindowData &wd = windows[p_window]; |
| 2168 | return wd.instance_id; |
| 2169 | } |
| 2170 | |
| 2171 | DisplayServerX11::WindowID DisplayServerX11::get_window_at_screen_position(const Point2i &p_position) const { |
| 2172 | WindowID found_window = INVALID_WINDOW_ID; |
| 2173 | WindowID parent_window = INVALID_WINDOW_ID; |
| 2174 | unsigned int focus_order = 0; |
| 2175 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 2176 | const WindowData &wd = E.value; |
| 2177 | |
| 2178 | // Discard windows with no focus. |
| 2179 | if (wd.focus_order == 0) { |
| 2180 | continue; |
| 2181 | } |
| 2182 | |
| 2183 | // Find topmost window which contains the given position. |
| 2184 | WindowID window_id = E.key; |
| 2185 | Rect2i win_rect = Rect2i(window_get_position(window_id), window_get_size(window_id)); |
| 2186 | if (win_rect.has_point(p_position)) { |
| 2187 | // For siblings, pick the window which was focused last. |
| 2188 | if ((parent_window != wd.transient_parent) || (wd.focus_order > focus_order)) { |
| 2189 | found_window = window_id; |
| 2190 | parent_window = wd.transient_parent; |
| 2191 | focus_order = wd.focus_order; |
| 2192 | } |
| 2193 | } |
| 2194 | } |
| 2195 | |
| 2196 | return found_window; |
| 2197 | } |
| 2198 | |
| 2199 | void DisplayServerX11::window_set_title(const String &p_title, WindowID p_window) { |
| 2200 | _THREAD_SAFE_METHOD_ |
| 2201 | |
| 2202 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2203 | WindowData &wd = windows[p_window]; |
| 2204 | |
| 2205 | XStoreName(x11_display, wd.x11_window, p_title.utf8().get_data()); |
| 2206 | |
| 2207 | Atom _net_wm_name = XInternAtom(x11_display, "_NET_WM_NAME", false); |
| 2208 | Atom utf8_string = XInternAtom(x11_display, "UTF8_STRING", false); |
| 2209 | if (_net_wm_name != None && utf8_string != None) { |
| 2210 | CharString utf8_title = p_title.utf8(); |
| 2211 | XChangeProperty(x11_display, wd.x11_window, _net_wm_name, utf8_string, 8, PropModeReplace, (unsigned char *)utf8_title.get_data(), utf8_title.length()); |
| 2212 | } |
| 2213 | } |
| 2214 | |
| 2215 | void DisplayServerX11::window_set_mouse_passthrough(const Vector<Vector2> &p_region, WindowID p_window) { |
| 2216 | _THREAD_SAFE_METHOD_ |
| 2217 | |
| 2218 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2219 | windows[p_window].mpath = p_region; |
| 2220 | _update_window_mouse_passthrough(p_window); |
| 2221 | } |
| 2222 | |
| 2223 | void DisplayServerX11::_update_window_mouse_passthrough(WindowID p_window) { |
| 2224 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2225 | ERR_FAIL_COND(!xshaped_ext_ok); |
| 2226 | |
| 2227 | const Vector<Vector2> region_path = windows[p_window].mpath; |
| 2228 | |
| 2229 | int event_base, error_base; |
| 2230 | const Bool ext_okay = XShapeQueryExtension(x11_display, &event_base, &error_base); |
| 2231 | if (ext_okay) { |
| 2232 | if (windows[p_window].mpass) { |
| 2233 | Region region = XCreateRegion(); |
| 2234 | XShapeCombineRegion(x11_display, windows[p_window].x11_window, ShapeInput, 0, 0, region, ShapeSet); |
| 2235 | XDestroyRegion(region); |
| 2236 | } else if (region_path.is_empty()) { |
| 2237 | XShapeCombineMask(x11_display, windows[p_window].x11_window, ShapeInput, 0, 0, None, ShapeSet); |
| 2238 | } else { |
| 2239 | XPoint *points = (XPoint *)memalloc(sizeof(XPoint) * region_path.size()); |
| 2240 | for (int i = 0; i < region_path.size(); i++) { |
| 2241 | points[i].x = region_path[i].x; |
| 2242 | points[i].y = region_path[i].y; |
| 2243 | } |
| 2244 | Region region = XPolygonRegion(points, region_path.size(), EvenOddRule); |
| 2245 | memfree(points); |
| 2246 | XShapeCombineRegion(x11_display, windows[p_window].x11_window, ShapeInput, 0, 0, region, ShapeSet); |
| 2247 | XDestroyRegion(region); |
| 2248 | } |
| 2249 | } |
| 2250 | } |
| 2251 | |
| 2252 | void DisplayServerX11::window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window) { |
| 2253 | _THREAD_SAFE_METHOD_ |
| 2254 | |
| 2255 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2256 | WindowData &wd = windows[p_window]; |
| 2257 | wd.rect_changed_callback = p_callable; |
| 2258 | } |
| 2259 | |
| 2260 | void DisplayServerX11::window_set_window_event_callback(const Callable &p_callable, WindowID p_window) { |
| 2261 | _THREAD_SAFE_METHOD_ |
| 2262 | |
| 2263 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2264 | WindowData &wd = windows[p_window]; |
| 2265 | wd.event_callback = p_callable; |
| 2266 | } |
| 2267 | |
| 2268 | void DisplayServerX11::window_set_input_event_callback(const Callable &p_callable, WindowID p_window) { |
| 2269 | _THREAD_SAFE_METHOD_ |
| 2270 | |
| 2271 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2272 | WindowData &wd = windows[p_window]; |
| 2273 | wd.input_event_callback = p_callable; |
| 2274 | } |
| 2275 | |
| 2276 | void DisplayServerX11::window_set_input_text_callback(const Callable &p_callable, WindowID p_window) { |
| 2277 | _THREAD_SAFE_METHOD_ |
| 2278 | |
| 2279 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2280 | WindowData &wd = windows[p_window]; |
| 2281 | wd.input_text_callback = p_callable; |
| 2282 | } |
| 2283 | |
| 2284 | void DisplayServerX11::window_set_drop_files_callback(const Callable &p_callable, WindowID p_window) { |
| 2285 | _THREAD_SAFE_METHOD_ |
| 2286 | |
| 2287 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2288 | WindowData &wd = windows[p_window]; |
| 2289 | wd.drop_files_callback = p_callable; |
| 2290 | } |
| 2291 | |
| 2292 | int DisplayServerX11::window_get_current_screen(WindowID p_window) const { |
| 2293 | _THREAD_SAFE_METHOD_ |
| 2294 | |
| 2295 | int count = get_screen_count(); |
| 2296 | if (count < 2) { |
| 2297 | // Early exit with single monitor. |
| 2298 | return 0; |
| 2299 | } |
| 2300 | |
| 2301 | ERR_FAIL_COND_V(!windows.has(p_window), INVALID_SCREEN); |
| 2302 | const WindowData &wd = windows[p_window]; |
| 2303 | |
| 2304 | const Rect2i window_rect(wd.position, wd.size); |
| 2305 | |
| 2306 | // Find which monitor has the largest overlap with the given window. |
| 2307 | int screen_index = 0; |
| 2308 | int max_area = 0; |
| 2309 | for (int i = 0; i < count; i++) { |
| 2310 | Rect2i screen_rect = _screen_get_rect(i); |
| 2311 | Rect2i intersection = screen_rect.intersection(window_rect); |
| 2312 | int area = intersection.get_area(); |
| 2313 | if (area > max_area) { |
| 2314 | max_area = area; |
| 2315 | screen_index = i; |
| 2316 | } |
| 2317 | } |
| 2318 | |
| 2319 | return screen_index; |
| 2320 | } |
| 2321 | |
| 2322 | void DisplayServerX11::gl_window_make_current(DisplayServer::WindowID p_window_id) { |
| 2323 | #if defined(GLES3_ENABLED) |
| 2324 | if (gl_manager) { |
| 2325 | gl_manager->window_make_current(p_window_id); |
| 2326 | } |
| 2327 | if (gl_manager_egl) { |
| 2328 | gl_manager_egl->window_make_current(p_window_id); |
| 2329 | } |
| 2330 | #endif |
| 2331 | } |
| 2332 | |
| 2333 | void DisplayServerX11::window_set_current_screen(int p_screen, WindowID p_window) { |
| 2334 | _THREAD_SAFE_METHOD_ |
| 2335 | |
| 2336 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2337 | |
| 2338 | p_screen = _get_screen_index(p_screen); |
| 2339 | int screen_count = get_screen_count(); |
| 2340 | ERR_FAIL_INDEX(p_screen, screen_count); |
| 2341 | |
| 2342 | if (window_get_current_screen(p_window) == p_screen) { |
| 2343 | return; |
| 2344 | } |
| 2345 | WindowData &wd = windows[p_window]; |
| 2346 | |
| 2347 | if (wd.embed_parent) { |
| 2348 | print_line("Embedded window can't be moved to another screen."); |
| 2349 | return; |
| 2350 | } |
| 2351 | |
| 2352 | if (window_get_mode(p_window) == WINDOW_MODE_FULLSCREEN || window_get_mode(p_window) == WINDOW_MODE_MAXIMIZED) { |
| 2353 | WindowMode current_mode = window_get_mode(p_window); |
| 2354 | window_set_mode(WINDOW_MODE_WINDOWED, p_window); |
| 2355 | Point2i position = screen_get_position(p_screen); |
| 2356 | Size2i size = screen_get_size(p_screen); |
| 2357 | XMoveResizeWindow(x11_display, wd.x11_window, position.x, position.y, size.x, size.y); |
| 2358 | window_set_mode(current_mode, p_window); |
| 2359 | } else { |
| 2360 | Rect2i srect = screen_get_usable_rect(p_screen); |
| 2361 | Point2i wpos = window_get_position(p_window) - screen_get_position(window_get_current_screen(p_window)); |
| 2362 | Size2i wsize = window_get_size(p_window); |
| 2363 | wpos += srect.position; |
| 2364 | if (srect != Rect2i()) { |
| 2365 | wpos = wpos.clamp(srect.position, srect.position + srect.size - wsize / 3); |
| 2366 | } |
| 2367 | window_set_position(wpos, p_window); |
| 2368 | } |
| 2369 | } |
| 2370 | |
| 2371 | void DisplayServerX11::window_set_transient(WindowID p_window, WindowID p_parent) { |
| 2372 | _THREAD_SAFE_METHOD_ |
| 2373 | |
| 2374 | ERR_FAIL_COND(p_window == p_parent); |
| 2375 | |
| 2376 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2377 | WindowData &wd_window = windows[p_window]; |
| 2378 | |
| 2379 | WindowID prev_parent = wd_window.transient_parent; |
| 2380 | ERR_FAIL_COND(prev_parent == p_parent); |
| 2381 | |
| 2382 | DEBUG_LOG_X11("window_set_transient: %lu (%u), prev_parent=%u, parent=%u\n", wd_window.x11_window, p_window, prev_parent, p_parent); |
| 2383 | |
| 2384 | ERR_FAIL_COND_MSG(wd_window.on_top, "Windows with the 'on top' can't become transient."); |
| 2385 | if (p_parent == INVALID_WINDOW_ID) { |
| 2386 | //remove transient |
| 2387 | |
| 2388 | ERR_FAIL_COND(prev_parent == INVALID_WINDOW_ID); |
| 2389 | ERR_FAIL_COND(!windows.has(prev_parent)); |
| 2390 | |
| 2391 | WindowData &wd_parent = windows[prev_parent]; |
| 2392 | |
| 2393 | wd_window.transient_parent = INVALID_WINDOW_ID; |
| 2394 | wd_parent.transient_children.erase(p_window); |
| 2395 | |
| 2396 | XSetTransientForHint(x11_display, wd_window.x11_window, None); |
| 2397 | |
| 2398 | XWindowAttributes xwa; |
| 2399 | XSync(x11_display, False); |
| 2400 | XGetWindowAttributes(x11_display, wd_parent.x11_window, &xwa); |
| 2401 | |
| 2402 | // Set focus to parent sub window to avoid losing all focus when closing a nested sub-menu. |
| 2403 | // RevertToPointerRoot is used to make sure we don't lose all focus in case |
| 2404 | // a subwindow and its parent are both destroyed. |
| 2405 | if (!wd_window.no_focus && !wd_window.is_popup && wd_window.focused) { |
| 2406 | if ((xwa.map_state == IsViewable) && !wd_parent.no_focus && !wd_window.is_popup && _window_focus_check()) { |
| 2407 | _set_input_focus(wd_parent.x11_window, RevertToPointerRoot); |
| 2408 | } |
| 2409 | } |
| 2410 | } else { |
| 2411 | ERR_FAIL_COND(!windows.has(p_parent)); |
| 2412 | ERR_FAIL_COND_MSG(prev_parent != INVALID_WINDOW_ID, "Window already has a transient parent"); |
| 2413 | WindowData &wd_parent = windows[p_parent]; |
| 2414 | |
| 2415 | wd_window.transient_parent = p_parent; |
| 2416 | wd_parent.transient_children.insert(p_window); |
| 2417 | |
| 2418 | XSetTransientForHint(x11_display, wd_window.x11_window, wd_parent.x11_window); |
| 2419 | } |
| 2420 | } |
| 2421 | |
| 2422 | // Helper method. Assumes that the window id has already been checked and exists. |
| 2423 | void DisplayServerX11::_update_size_hints(WindowID p_window) { |
| 2424 | WindowData &wd = windows[p_window]; |
| 2425 | WindowMode window_mode = window_get_mode(p_window); |
| 2426 | XSizeHints *xsh = XAllocSizeHints(); |
| 2427 | |
| 2428 | // Always set the position and size hints - they should be synchronized with the actual values after the window is mapped anyway |
| 2429 | xsh->flags |= PPosition | PSize | PWinGravity; |
| 2430 | xsh->x = wd.position.x; |
| 2431 | xsh->y = wd.position.y; |
| 2432 | xsh->width = wd.size.width; |
| 2433 | xsh->height = wd.size.height; |
| 2434 | xsh->win_gravity = StaticGravity; |
| 2435 | |
| 2436 | if (window_mode == WINDOW_MODE_FULLSCREEN || window_mode == WINDOW_MODE_EXCLUSIVE_FULLSCREEN) { |
| 2437 | // Do not set any other hints to prevent the window manager from ignoring the fullscreen flags |
| 2438 | } else if (window_get_flag(WINDOW_FLAG_RESIZE_DISABLED, p_window)) { |
| 2439 | // If resizing is disabled, use the forced size |
| 2440 | xsh->flags |= PMinSize | PMaxSize; |
| 2441 | xsh->min_width = wd.size.x; |
| 2442 | xsh->max_width = wd.size.x; |
| 2443 | xsh->min_height = wd.size.y; |
| 2444 | xsh->max_height = wd.size.y; |
| 2445 | } else { |
| 2446 | // Otherwise, just respect min_size and max_size |
| 2447 | if (wd.min_size != Size2i()) { |
| 2448 | xsh->flags |= PMinSize; |
| 2449 | xsh->min_width = wd.min_size.x; |
| 2450 | xsh->min_height = wd.min_size.y; |
| 2451 | } |
| 2452 | if (wd.max_size != Size2i()) { |
| 2453 | xsh->flags |= PMaxSize; |
| 2454 | xsh->max_width = wd.max_size.x; |
| 2455 | xsh->max_height = wd.max_size.y; |
| 2456 | } |
| 2457 | } |
| 2458 | |
| 2459 | XSetWMNormalHints(x11_display, wd.x11_window, xsh); |
| 2460 | XFree(xsh); |
| 2461 | } |
| 2462 | |
| 2463 | void DisplayServerX11::_update_motif_wm_hints(WindowID p_window) { |
| 2464 | Atom motif_wm_hints = XInternAtom(x11_display, "_MOTIF_WM_HINTS", True); |
| 2465 | if (motif_wm_hints == None) { |
| 2466 | return; |
| 2467 | } |
| 2468 | |
| 2469 | WindowData &wd = windows[p_window]; |
| 2470 | |
| 2471 | MotifWmHints hints = {}; |
| 2472 | hints.flags = MWM_HINTS_DECORATIONS; |
| 2473 | |
| 2474 | if (!wd.borderless) { |
| 2475 | hints.flags |= MWM_HINTS_FUNCTIONS; |
| 2476 | |
| 2477 | hints.decorations = MWM_DECOR_BORDER | MWM_DECOR_MENU | MWM_DECOR_TITLE; |
| 2478 | hints.functions = MWM_FUNC_MOVE | MWM_FUNC_CLOSE; |
| 2479 | |
| 2480 | if (!wd.no_min_btn) { |
| 2481 | hints.decorations |= MWM_DECOR_MINIMIZE; |
| 2482 | hints.functions |= MWM_FUNC_MINIMIZE; |
| 2483 | } |
| 2484 | if (!wd.no_max_btn) { |
| 2485 | hints.decorations |= MWM_DECOR_MAXIMIZE; |
| 2486 | hints.functions |= MWM_FUNC_MAXIMIZE; |
| 2487 | } |
| 2488 | if (!wd.resize_disabled) { |
| 2489 | hints.decorations |= MWM_DECOR_RESIZEH; |
| 2490 | hints.functions |= MWM_FUNC_RESIZE; |
| 2491 | } |
| 2492 | } |
| 2493 | |
| 2494 | XChangeProperty(x11_display, wd.x11_window, motif_wm_hints, motif_wm_hints, 32, PropModeReplace, (unsigned char *)&hints, 5); |
| 2495 | } |
| 2496 | |
| 2497 | void DisplayServerX11::_update_wm_state_hints(WindowID p_window) { |
| 2498 | WindowData &wd = windows[p_window]; |
| 2499 | |
| 2500 | Atom type; |
| 2501 | int format; |
| 2502 | unsigned long len; |
| 2503 | unsigned long remaining; |
| 2504 | unsigned char *data = nullptr; |
| 2505 | |
| 2506 | int result = XGetWindowProperty( |
| 2507 | x11_display, |
| 2508 | wd.x11_window, |
| 2509 | XInternAtom(x11_display, "_NET_WM_STATE", False), |
| 2510 | 0, |
| 2511 | 1024, |
| 2512 | False, |
| 2513 | XA_ATOM, |
| 2514 | &type, |
| 2515 | &format, |
| 2516 | &len, |
| 2517 | &remaining, |
| 2518 | &data); |
| 2519 | if (result != Success) { |
| 2520 | return; |
| 2521 | } |
| 2522 | |
| 2523 | LocalVector<Atom> hints; |
| 2524 | if (data) { |
| 2525 | hints.resize(len); |
| 2526 | Atom *atoms = (Atom *)data; |
| 2527 | for (unsigned long i = 0; i < len; i++) { |
| 2528 | hints[i] = atoms[i]; |
| 2529 | } |
| 2530 | XFree(data); |
| 2531 | } |
| 2532 | |
| 2533 | Atom fullscreen_atom = XInternAtom(x11_display, "_NET_WM_STATE_FULLSCREEN", False); |
| 2534 | Atom maximized_horz_atom = XInternAtom(x11_display, "_NET_WM_STATE_MAXIMIZED_HORZ", False); |
| 2535 | Atom maximized_vert_atom = XInternAtom(x11_display, "_NET_WM_STATE_MAXIMIZED_VERT", False); |
| 2536 | Atom hidden_atom = XInternAtom(x11_display, "_NET_WM_STATE_HIDDEN", False); |
| 2537 | |
| 2538 | wd.fullscreen = hints.has(fullscreen_atom); |
| 2539 | wd.maximized = hints.has(maximized_horz_atom) && hints.has(maximized_vert_atom); |
| 2540 | wd.minimized = hints.has(hidden_atom); |
| 2541 | } |
| 2542 | |
| 2543 | Point2i DisplayServerX11::window_get_position(WindowID p_window) const { |
| 2544 | _THREAD_SAFE_METHOD_ |
| 2545 | |
| 2546 | ERR_FAIL_COND_V(!windows.has(p_window), Point2i()); |
| 2547 | const WindowData &wd = windows[p_window]; |
| 2548 | |
| 2549 | return wd.position; |
| 2550 | } |
| 2551 | |
| 2552 | Point2i DisplayServerX11::window_get_position_with_decorations(WindowID p_window) const { |
| 2553 | _THREAD_SAFE_METHOD_ |
| 2554 | |
| 2555 | ERR_FAIL_COND_V(!windows.has(p_window), Size2i()); |
| 2556 | const WindowData &wd = windows[p_window]; |
| 2557 | |
| 2558 | if (wd.fullscreen) { |
| 2559 | return wd.position; |
| 2560 | } |
| 2561 | |
| 2562 | XWindowAttributes xwa; |
| 2563 | XSync(x11_display, False); |
| 2564 | XGetWindowAttributes(x11_display, wd.x11_window, &xwa); |
| 2565 | int x = wd.position.x; |
| 2566 | int y = wd.position.y; |
| 2567 | Atom prop = XInternAtom(x11_display, "_NET_FRAME_EXTENTS", True); |
| 2568 | if (prop != None) { |
| 2569 | Atom type; |
| 2570 | int format; |
| 2571 | unsigned long len; |
| 2572 | unsigned long remaining; |
| 2573 | unsigned char *data = nullptr; |
| 2574 | if (XGetWindowProperty(x11_display, wd.x11_window, prop, 0, 4, False, AnyPropertyType, &type, &format, &len, &remaining, &data) == Success) { |
| 2575 | if (format == 32 && len == 4 && data) { |
| 2576 | long *extents = (long *)data; |
| 2577 | x -= extents[0]; // left |
| 2578 | y -= extents[2]; // top |
| 2579 | } |
| 2580 | XFree(data); |
| 2581 | } |
| 2582 | } |
| 2583 | return Size2i(x, y); |
| 2584 | } |
| 2585 | |
| 2586 | void DisplayServerX11::window_set_position(const Point2i &p_position, WindowID p_window) { |
| 2587 | _THREAD_SAFE_METHOD_ |
| 2588 | |
| 2589 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2590 | WindowData &wd = windows[p_window]; |
| 2591 | |
| 2592 | if (wd.embed_parent) { |
| 2593 | print_line("Embedded window can't be moved."); |
| 2594 | return; |
| 2595 | } |
| 2596 | |
| 2597 | wd.position = p_position; |
| 2598 | XMoveWindow(x11_display, wd.x11_window, p_position.x, p_position.y); |
| 2599 | _update_real_mouse_position(wd); |
| 2600 | } |
| 2601 | |
| 2602 | void DisplayServerX11::window_set_max_size(const Size2i p_size, WindowID p_window) { |
| 2603 | _THREAD_SAFE_METHOD_ |
| 2604 | |
| 2605 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2606 | WindowData &wd = windows[p_window]; |
| 2607 | |
| 2608 | if (wd.embed_parent) { |
| 2609 | print_line("Embedded windows can't have a maximum size."); |
| 2610 | return; |
| 2611 | } |
| 2612 | |
| 2613 | if ((p_size != Size2i()) && ((p_size.x < wd.min_size.x) || (p_size.y < wd.min_size.y))) { |
| 2614 | ERR_PRINT("Maximum window size can't be smaller than minimum window size!"); |
| 2615 | return; |
| 2616 | } |
| 2617 | wd.max_size = p_size; |
| 2618 | |
| 2619 | _update_size_hints(p_window); |
| 2620 | XFlush(x11_display); |
| 2621 | } |
| 2622 | |
| 2623 | Size2i DisplayServerX11::window_get_max_size(WindowID p_window) const { |
| 2624 | _THREAD_SAFE_METHOD_ |
| 2625 | |
| 2626 | ERR_FAIL_COND_V(!windows.has(p_window), Size2i()); |
| 2627 | const WindowData &wd = windows[p_window]; |
| 2628 | |
| 2629 | return wd.max_size; |
| 2630 | } |
| 2631 | |
| 2632 | void DisplayServerX11::window_set_min_size(const Size2i p_size, WindowID p_window) { |
| 2633 | _THREAD_SAFE_METHOD_ |
| 2634 | |
| 2635 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2636 | WindowData &wd = windows[p_window]; |
| 2637 | |
| 2638 | if (wd.embed_parent) { |
| 2639 | print_line("Embedded windows can't have a minimum size."); |
| 2640 | return; |
| 2641 | } |
| 2642 | |
| 2643 | if ((p_size != Size2i()) && (wd.max_size != Size2i()) && ((p_size.x > wd.max_size.x) || (p_size.y > wd.max_size.y))) { |
| 2644 | ERR_PRINT("Minimum window size can't be larger than maximum window size!"); |
| 2645 | return; |
| 2646 | } |
| 2647 | wd.min_size = p_size; |
| 2648 | |
| 2649 | _update_size_hints(p_window); |
| 2650 | XFlush(x11_display); |
| 2651 | } |
| 2652 | |
| 2653 | Size2i DisplayServerX11::window_get_min_size(WindowID p_window) const { |
| 2654 | _THREAD_SAFE_METHOD_ |
| 2655 | |
| 2656 | ERR_FAIL_COND_V(!windows.has(p_window), Size2i()); |
| 2657 | const WindowData &wd = windows[p_window]; |
| 2658 | |
| 2659 | return wd.min_size; |
| 2660 | } |
| 2661 | |
| 2662 | void DisplayServerX11::window_set_size(const Size2i p_size, WindowID p_window) { |
| 2663 | _THREAD_SAFE_METHOD_ |
| 2664 | |
| 2665 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2666 | |
| 2667 | Size2i size = p_size; |
| 2668 | size = size.maxi(1); |
| 2669 | |
| 2670 | WindowData &wd = windows[p_window]; |
| 2671 | |
| 2672 | if (wd.embed_parent) { |
| 2673 | print_line("Embedded window can't be resized."); |
| 2674 | return; |
| 2675 | } |
| 2676 | |
| 2677 | if (wd.size.width == size.width && wd.size.height == size.height) { |
| 2678 | return; |
| 2679 | } |
| 2680 | |
| 2681 | XWindowAttributes xwa; |
| 2682 | XSync(x11_display, False); |
| 2683 | XGetWindowAttributes(x11_display, wd.x11_window, &xwa); |
| 2684 | int old_w = xwa.width; |
| 2685 | int old_h = xwa.height; |
| 2686 | |
| 2687 | // Update our videomode width and height |
| 2688 | wd.size = size; |
| 2689 | |
| 2690 | // Update the size hints first to make sure the window size can be set |
| 2691 | _update_size_hints(p_window); |
| 2692 | |
| 2693 | // Resize the window |
| 2694 | XResizeWindow(x11_display, wd.x11_window, size.x, size.y); |
| 2695 | |
| 2696 | for (int timeout = 0; timeout < 50; ++timeout) { |
| 2697 | XSync(x11_display, False); |
| 2698 | XGetWindowAttributes(x11_display, wd.x11_window, &xwa); |
| 2699 | |
| 2700 | if (old_w != xwa.width || old_h != xwa.height) { |
| 2701 | break; |
| 2702 | } |
| 2703 | |
| 2704 | OS::get_singleton()->delay_usec(10'000); |
| 2705 | } |
| 2706 | |
| 2707 | // Keep rendering context window size in sync |
| 2708 | #if defined(RD_ENABLED) |
| 2709 | if (rendering_context) { |
| 2710 | rendering_context->window_set_size(p_window, xwa.width, xwa.height); |
| 2711 | } |
| 2712 | #endif |
| 2713 | #if defined(GLES3_ENABLED) |
| 2714 | if (gl_manager) { |
| 2715 | gl_manager->window_resize(p_window, xwa.width, xwa.height); |
| 2716 | } |
| 2717 | if (gl_manager_egl) { |
| 2718 | gl_manager_egl->window_resize(p_window, xwa.width, xwa.height); |
| 2719 | } |
| 2720 | #endif |
| 2721 | } |
| 2722 | |
| 2723 | Size2i DisplayServerX11::window_get_size(WindowID p_window) const { |
| 2724 | _THREAD_SAFE_METHOD_ |
| 2725 | |
| 2726 | ERR_FAIL_COND_V(!windows.has(p_window), Size2i()); |
| 2727 | const WindowData &wd = windows[p_window]; |
| 2728 | return wd.size; |
| 2729 | } |
| 2730 | |
| 2731 | Size2i DisplayServerX11::window_get_size_with_decorations(WindowID p_window) const { |
| 2732 | _THREAD_SAFE_METHOD_ |
| 2733 | |
| 2734 | ERR_FAIL_COND_V(!windows.has(p_window), Size2i()); |
| 2735 | const WindowData &wd = windows[p_window]; |
| 2736 | |
| 2737 | if (wd.fullscreen) { |
| 2738 | return wd.size; |
| 2739 | } |
| 2740 | |
| 2741 | XWindowAttributes xwa; |
| 2742 | XSync(x11_display, False); |
| 2743 | XGetWindowAttributes(x11_display, wd.x11_window, &xwa); |
| 2744 | int w = xwa.width; |
| 2745 | int h = xwa.height; |
| 2746 | Atom prop = XInternAtom(x11_display, "_NET_FRAME_EXTENTS", True); |
| 2747 | if (prop != None) { |
| 2748 | Atom type; |
| 2749 | int format; |
| 2750 | unsigned long len; |
| 2751 | unsigned long remaining; |
| 2752 | unsigned char *data = nullptr; |
| 2753 | if (XGetWindowProperty(x11_display, wd.x11_window, prop, 0, 4, False, AnyPropertyType, &type, &format, &len, &remaining, &data) == Success) { |
| 2754 | if (format == 32 && len == 4 && data) { |
| 2755 | long *extents = (long *)data; |
| 2756 | w += extents[0] + extents[1]; // left, right |
| 2757 | h += extents[2] + extents[3]; // top, bottom |
| 2758 | } |
| 2759 | XFree(data); |
| 2760 | } |
| 2761 | } |
| 2762 | return Size2i(w, h); |
| 2763 | } |
| 2764 | |
| 2765 | // Just a helper to reduce code duplication in `window_is_maximize_allowed` |
| 2766 | // and `_set_wm_maximized`. |
| 2767 | bool DisplayServerX11::_window_maximize_check(WindowID p_window, const char *p_atom_name) const { |
| 2768 | ERR_FAIL_COND_V(!windows.has(p_window), false); |
| 2769 | const WindowData &wd = windows[p_window]; |
| 2770 | |
| 2771 | Atom property = XInternAtom(x11_display, p_atom_name, False); |
| 2772 | Atom type; |
| 2773 | int format; |
| 2774 | unsigned long len; |
| 2775 | unsigned long remaining; |
| 2776 | unsigned char *data = nullptr; |
| 2777 | bool retval = false; |
| 2778 | |
| 2779 | if (property == None) { |
| 2780 | return false; |
| 2781 | } |
| 2782 | |
| 2783 | int result = XGetWindowProperty( |
| 2784 | x11_display, |
| 2785 | wd.x11_window, |
| 2786 | property, |
| 2787 | 0, |
| 2788 | 1024, |
| 2789 | False, |
| 2790 | XA_ATOM, |
| 2791 | &type, |
| 2792 | &format, |
| 2793 | &len, |
| 2794 | &remaining, |
| 2795 | &data); |
| 2796 | |
| 2797 | if (result == Success && data) { |
| 2798 | Atom *atoms = (Atom *)data; |
| 2799 | Atom wm_act_max_horz; |
| 2800 | Atom wm_act_max_vert; |
| 2801 | bool checking_state = strcmp(p_atom_name, "_NET_WM_STATE") == 0; |
| 2802 | if (checking_state) { |
| 2803 | wm_act_max_horz = XInternAtom(x11_display, "_NET_WM_STATE_MAXIMIZED_HORZ", False); |
| 2804 | wm_act_max_vert = XInternAtom(x11_display, "_NET_WM_STATE_MAXIMIZED_VERT", False); |
| 2805 | } else { |
| 2806 | wm_act_max_horz = XInternAtom(x11_display, "_NET_WM_ACTION_MAXIMIZE_HORZ", False); |
| 2807 | wm_act_max_vert = XInternAtom(x11_display, "_NET_WM_ACTION_MAXIMIZE_VERT", False); |
| 2808 | } |
| 2809 | bool found_wm_act_max_horz = false; |
| 2810 | bool found_wm_act_max_vert = false; |
| 2811 | |
| 2812 | for (uint64_t i = 0; i < len; i++) { |
| 2813 | if (atoms[i] == wm_act_max_horz) { |
| 2814 | found_wm_act_max_horz = true; |
| 2815 | } |
| 2816 | if (atoms[i] == wm_act_max_vert) { |
| 2817 | found_wm_act_max_vert = true; |
| 2818 | } |
| 2819 | |
| 2820 | if (checking_state) { |
| 2821 | if (found_wm_act_max_horz && found_wm_act_max_vert) { |
| 2822 | retval = true; |
| 2823 | break; |
| 2824 | } |
| 2825 | } else { |
| 2826 | if (found_wm_act_max_horz || found_wm_act_max_vert) { |
| 2827 | retval = true; |
| 2828 | break; |
| 2829 | } |
| 2830 | } |
| 2831 | } |
| 2832 | |
| 2833 | XFree(data); |
| 2834 | } |
| 2835 | |
| 2836 | return retval; |
| 2837 | } |
| 2838 | |
| 2839 | bool DisplayServerX11::_window_minimize_check(WindowID p_window) const { |
| 2840 | const WindowData &wd = windows[p_window]; |
| 2841 | |
| 2842 | // Using EWMH instead of ICCCM, might work better for Wayland users. |
| 2843 | Atom property = XInternAtom(x11_display, "_NET_WM_STATE", True); |
| 2844 | Atom hidden = XInternAtom(x11_display, "_NET_WM_STATE_HIDDEN", True); |
| 2845 | if (property == None || hidden == None) { |
| 2846 | return false; |
| 2847 | } |
| 2848 | |
| 2849 | Atom type; |
| 2850 | int format; |
| 2851 | unsigned long len; |
| 2852 | unsigned long remaining; |
| 2853 | Atom *atoms = nullptr; |
| 2854 | |
| 2855 | int result = XGetWindowProperty( |
| 2856 | x11_display, |
| 2857 | wd.x11_window, |
| 2858 | property, |
| 2859 | 0, |
| 2860 | 32, |
| 2861 | False, |
| 2862 | XA_ATOM, |
| 2863 | &type, |
| 2864 | &format, |
| 2865 | &len, |
| 2866 | &remaining, |
| 2867 | (unsigned char **)&atoms); |
| 2868 | |
| 2869 | if (result == Success && atoms) { |
| 2870 | for (unsigned int i = 0; i < len; i++) { |
| 2871 | if (atoms[i] == hidden) { |
| 2872 | XFree(atoms); |
| 2873 | return true; |
| 2874 | } |
| 2875 | } |
| 2876 | XFree(atoms); |
| 2877 | } |
| 2878 | |
| 2879 | return false; |
| 2880 | } |
| 2881 | |
| 2882 | bool DisplayServerX11::_window_fullscreen_check(WindowID p_window) const { |
| 2883 | ERR_FAIL_COND_V(!windows.has(p_window), false); |
| 2884 | const WindowData &wd = windows[p_window]; |
| 2885 | |
| 2886 | // Using EWMH -- Extended Window Manager Hints |
| 2887 | Atom property = XInternAtom(x11_display, "_NET_WM_STATE", False); |
| 2888 | Atom type; |
| 2889 | int format; |
| 2890 | unsigned long len; |
| 2891 | unsigned long remaining; |
| 2892 | unsigned char *data = nullptr; |
| 2893 | bool retval = false; |
| 2894 | |
| 2895 | if (property == None) { |
| 2896 | return retval; |
| 2897 | } |
| 2898 | |
| 2899 | int result = XGetWindowProperty( |
| 2900 | x11_display, |
| 2901 | wd.x11_window, |
| 2902 | property, |
| 2903 | 0, |
| 2904 | 1024, |
| 2905 | False, |
| 2906 | XA_ATOM, |
| 2907 | &type, |
| 2908 | &format, |
| 2909 | &len, |
| 2910 | &remaining, |
| 2911 | &data); |
| 2912 | |
| 2913 | if (result == Success) { |
| 2914 | Atom *atoms = (Atom *)data; |
| 2915 | Atom wm_fullscreen = XInternAtom(x11_display, "_NET_WM_STATE_FULLSCREEN", False); |
| 2916 | for (uint64_t i = 0; i < len; i++) { |
| 2917 | if (atoms[i] == wm_fullscreen) { |
| 2918 | retval = true; |
| 2919 | break; |
| 2920 | } |
| 2921 | } |
| 2922 | XFree(data); |
| 2923 | } |
| 2924 | |
| 2925 | return retval; |
| 2926 | } |
| 2927 | |
| 2928 | void DisplayServerX11::_validate_fullscreen_on_map(WindowID p_window) { |
| 2929 | // Check if we applied any window modes that didn't take effect while unmapped |
| 2930 | const WindowData &wd = windows[p_window]; |
| 2931 | if (wd.fullscreen && !_window_fullscreen_check(p_window)) { |
| 2932 | _set_wm_fullscreen(p_window, true, wd.exclusive_fullscreen); |
| 2933 | } |
| 2934 | |
| 2935 | if (wd.on_top) { |
| 2936 | Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False); |
| 2937 | Atom wm_above = XInternAtom(x11_display, "_NET_WM_STATE_ABOVE", False); |
| 2938 | |
| 2939 | XClientMessageEvent xev; |
| 2940 | memset(&xev, 0, sizeof(xev)); |
| 2941 | xev.type = ClientMessage; |
| 2942 | xev.window = wd.x11_window; |
| 2943 | xev.message_type = wm_state; |
| 2944 | xev.format = 32; |
| 2945 | xev.data.l[0] = _NET_WM_STATE_ADD; |
| 2946 | xev.data.l[1] = wm_above; |
| 2947 | xev.data.l[3] = 1; |
| 2948 | XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&xev); |
| 2949 | } |
| 2950 | } |
| 2951 | |
| 2952 | bool DisplayServerX11::window_is_maximize_allowed(WindowID p_window) const { |
| 2953 | _THREAD_SAFE_METHOD_ |
| 2954 | return _window_maximize_check(p_window, "_NET_WM_ALLOWED_ACTIONS"); |
| 2955 | } |
| 2956 | |
| 2957 | void DisplayServerX11::_set_wm_maximized(WindowID p_window, bool p_enabled) { |
| 2958 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2959 | WindowData &wd = windows[p_window]; |
| 2960 | |
| 2961 | // Using EWMH -- Extended Window Manager Hints |
| 2962 | XEvent xev; |
| 2963 | Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False); |
| 2964 | Atom wm_max_horz = XInternAtom(x11_display, "_NET_WM_STATE_MAXIMIZED_HORZ", False); |
| 2965 | Atom wm_max_vert = XInternAtom(x11_display, "_NET_WM_STATE_MAXIMIZED_VERT", False); |
| 2966 | |
| 2967 | memset(&xev, 0, sizeof(xev)); |
| 2968 | xev.type = ClientMessage; |
| 2969 | xev.xclient.window = wd.x11_window; |
| 2970 | xev.xclient.message_type = wm_state; |
| 2971 | xev.xclient.format = 32; |
| 2972 | xev.xclient.data.l[0] = p_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE; |
| 2973 | xev.xclient.data.l[1] = wm_max_horz; |
| 2974 | xev.xclient.data.l[2] = wm_max_vert; |
| 2975 | |
| 2976 | XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev); |
| 2977 | |
| 2978 | if (p_enabled && window_is_maximize_allowed(p_window)) { |
| 2979 | // Wait for effective resizing (so the GLX context is too). |
| 2980 | // Give up after 0.5s, it's not going to happen on this WM. |
| 2981 | // https://github.com/godotengine/godot/issues/19978 |
| 2982 | for (int attempt = 0; window_get_mode(p_window) != WINDOW_MODE_MAXIMIZED && attempt < 50; attempt++) { |
| 2983 | OS::get_singleton()->delay_usec(10'000); |
| 2984 | } |
| 2985 | } |
| 2986 | wd.maximized = p_enabled; |
| 2987 | } |
| 2988 | |
| 2989 | void DisplayServerX11::_set_wm_minimized(WindowID p_window, bool p_enabled) { |
| 2990 | WindowData &wd = windows[p_window]; |
| 2991 | // Using ICCCM -- Inter-Client Communication Conventions Manual |
| 2992 | XEvent xev; |
| 2993 | Atom wm_change = XInternAtom(x11_display, "WM_CHANGE_STATE", False); |
| 2994 | |
| 2995 | memset(&xev, 0, sizeof(xev)); |
| 2996 | xev.type = ClientMessage; |
| 2997 | xev.xclient.window = wd.x11_window; |
| 2998 | xev.xclient.message_type = wm_change; |
| 2999 | xev.xclient.format = 32; |
| 3000 | xev.xclient.data.l[0] = p_enabled ? WM_IconicState : WM_NormalState; |
| 3001 | |
| 3002 | XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev); |
| 3003 | |
| 3004 | Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False); |
| 3005 | Atom wm_hidden = XInternAtom(x11_display, "_NET_WM_STATE_HIDDEN", False); |
| 3006 | |
| 3007 | memset(&xev, 0, sizeof(xev)); |
| 3008 | xev.type = ClientMessage; |
| 3009 | xev.xclient.window = wd.x11_window; |
| 3010 | xev.xclient.message_type = wm_state; |
| 3011 | xev.xclient.format = 32; |
| 3012 | xev.xclient.data.l[0] = p_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE; |
| 3013 | xev.xclient.data.l[1] = wm_hidden; |
| 3014 | |
| 3015 | XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev); |
| 3016 | wd.minimized = p_enabled; |
| 3017 | } |
| 3018 | |
| 3019 | void DisplayServerX11::_set_wm_fullscreen(WindowID p_window, bool p_enabled, bool p_exclusive) { |
| 3020 | ERR_FAIL_COND(!windows.has(p_window)); |
| 3021 | WindowData &wd = windows[p_window]; |
| 3022 | |
| 3023 | if (p_enabled) { |
| 3024 | // Set the window as resizable to prevent window managers to ignore the fullscreen state flag. |
| 3025 | _update_size_hints(p_window); |
| 3026 | } |
| 3027 | |
| 3028 | // Using EWMH -- Extended Window Manager Hints |
| 3029 | XEvent xev; |
| 3030 | Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False); |
| 3031 | Atom wm_fullscreen = XInternAtom(x11_display, "_NET_WM_STATE_FULLSCREEN", False); |
| 3032 | |
| 3033 | memset(&xev, 0, sizeof(xev)); |
| 3034 | xev.type = ClientMessage; |
| 3035 | xev.xclient.window = wd.x11_window; |
| 3036 | xev.xclient.message_type = wm_state; |
| 3037 | xev.xclient.format = 32; |
| 3038 | xev.xclient.data.l[0] = p_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE; |
| 3039 | xev.xclient.data.l[1] = wm_fullscreen; |
| 3040 | xev.xclient.data.l[2] = 0; |
| 3041 | |
| 3042 | XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev); |
| 3043 | |
| 3044 | // set bypass compositor hint |
| 3045 | Atom bypass_compositor = XInternAtom(x11_display, "_NET_WM_BYPASS_COMPOSITOR", False); |
| 3046 | unsigned long compositing_disable_on = 0; // Use default. |
| 3047 | if (p_enabled) { |
| 3048 | if (p_exclusive) { |
| 3049 | compositing_disable_on = 1; // Force composition OFF to reduce overhead. |
| 3050 | } else { |
| 3051 | compositing_disable_on = 2; // Force composition ON to allow popup windows. |
| 3052 | } |
| 3053 | } |
| 3054 | if (bypass_compositor != None) { |
| 3055 | XChangeProperty(x11_display, wd.x11_window, bypass_compositor, XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&compositing_disable_on, 1); |
| 3056 | } |
| 3057 | |
| 3058 | XFlush(x11_display); |
| 3059 | |
| 3060 | if (!p_enabled) { |
| 3061 | // Reset the non-resizable flags if we un-set these before. |
| 3062 | _update_size_hints(p_window); |
| 3063 | } |
| 3064 | } |
| 3065 | |
| 3066 | void DisplayServerX11::window_set_mode(WindowMode p_mode, WindowID p_window) { |
| 3067 | _THREAD_SAFE_METHOD_ |
| 3068 | |
| 3069 | ERR_FAIL_COND(!windows.has(p_window)); |
| 3070 | WindowData &wd = windows[p_window]; |
| 3071 | |
| 3072 | WindowMode old_mode = window_get_mode(p_window); |
| 3073 | if (old_mode == p_mode) { |
| 3074 | return; // do nothing |
| 3075 | } |
| 3076 | |
| 3077 | if (p_mode != WINDOW_MODE_WINDOWED && wd.embed_parent) { |
| 3078 | print_line("Embedded window only supports Windowed mode."); |
| 3079 | return; |
| 3080 | } |
| 3081 | |
| 3082 | // Remove all "extra" modes. |
| 3083 | switch (old_mode) { |
| 3084 | case WINDOW_MODE_WINDOWED: { |
| 3085 | //do nothing |
| 3086 | } break; |
| 3087 | case WINDOW_MODE_MINIMIZED: { |
| 3088 | _set_wm_minimized(p_window, false); |
| 3089 | } break; |
| 3090 | case WINDOW_MODE_EXCLUSIVE_FULLSCREEN: |
| 3091 | case WINDOW_MODE_FULLSCREEN: { |
| 3092 | // Only remove fullscreen when necessary. |
| 3093 | if (p_mode == WINDOW_MODE_WINDOWED || p_mode == WINDOW_MODE_MAXIMIZED) { |
| 3094 | wd.fullscreen = false; |
| 3095 | wd.exclusive_fullscreen = false; |
| 3096 | _set_wm_fullscreen(p_window, false, false); |
| 3097 | } |
| 3098 | } break; |
| 3099 | case WINDOW_MODE_MAXIMIZED: { |
| 3100 | // Varies between target modes, so do nothing here. |
| 3101 | } break; |
| 3102 | } |
| 3103 | |
| 3104 | switch (p_mode) { |
| 3105 | case WINDOW_MODE_WINDOWED: { |
| 3106 | if (wd.maximized) { |
| 3107 | _set_wm_maximized(p_window, false); |
| 3108 | } |
| 3109 | } break; |
| 3110 | case WINDOW_MODE_MINIMIZED: { |
| 3111 | _set_wm_minimized(p_window, true); |
| 3112 | } break; |
| 3113 | case WINDOW_MODE_EXCLUSIVE_FULLSCREEN: |
| 3114 | case WINDOW_MODE_FULLSCREEN: { |
| 3115 | if (window_get_flag(WINDOW_FLAG_ALWAYS_ON_TOP, p_window)) { |
| 3116 | _set_wm_maximized(p_window, true); |
| 3117 | } |
| 3118 | |
| 3119 | wd.fullscreen = true; |
| 3120 | if (p_mode == WINDOW_MODE_EXCLUSIVE_FULLSCREEN) { |
| 3121 | wd.exclusive_fullscreen = true; |
| 3122 | _set_wm_fullscreen(p_window, true, true); |
| 3123 | } else { |
| 3124 | wd.exclusive_fullscreen = false; |
| 3125 | _set_wm_fullscreen(p_window, true, false); |
| 3126 | } |
| 3127 | } break; |
| 3128 | case WINDOW_MODE_MAXIMIZED: { |
| 3129 | _set_wm_maximized(p_window, true); |
| 3130 | } break; |
| 3131 | } |
| 3132 | } |
| 3133 | |
| 3134 | DisplayServer::WindowMode DisplayServerX11::window_get_mode(WindowID p_window) const { |
| 3135 | _THREAD_SAFE_METHOD_ |
| 3136 | |
| 3137 | ERR_FAIL_COND_V(!windows.has(p_window), WINDOW_MODE_WINDOWED); |
| 3138 | const WindowData &wd = windows[p_window]; |
| 3139 | |
| 3140 | if (_window_minimize_check(p_window)) { |
| 3141 | return WINDOW_MODE_MINIMIZED; |
| 3142 | } |
| 3143 | |
| 3144 | if (wd.fullscreen) { |
| 3145 | if (wd.exclusive_fullscreen) { |
| 3146 | return WINDOW_MODE_EXCLUSIVE_FULLSCREEN; |
| 3147 | } |
| 3148 | return WINDOW_MODE_FULLSCREEN; |
| 3149 | } |
| 3150 | |
| 3151 | if (_window_maximize_check(p_window, "_NET_WM_STATE")) { |
| 3152 | return WINDOW_MODE_MAXIMIZED; |
| 3153 | } |
| 3154 | |
| 3155 | return WINDOW_MODE_WINDOWED; |
| 3156 | } |
| 3157 | |
| 3158 | void DisplayServerX11::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) { |
| 3159 | _THREAD_SAFE_METHOD_ |
| 3160 | |
| 3161 | ERR_FAIL_COND(!windows.has(p_window)); |
| 3162 | WindowData &wd = windows[p_window]; |
| 3163 | |
| 3164 | switch (p_flag) { |
| 3165 | case WINDOW_FLAG_MAXIMIZE_DISABLED: { |
| 3166 | wd.no_max_btn = p_enabled; |
| 3167 | _update_motif_wm_hints(p_window); |
| 3168 | |
| 3169 | XFlush(x11_display); |
| 3170 | } break; |
| 3171 | case WINDOW_FLAG_MINIMIZE_DISABLED: { |
| 3172 | wd.no_min_btn = p_enabled; |
| 3173 | _update_motif_wm_hints(p_window); |
| 3174 | |
| 3175 | XFlush(x11_display); |
| 3176 | } break; |
| 3177 | case WINDOW_FLAG_RESIZE_DISABLED: { |
| 3178 | if (p_enabled && wd.embed_parent) { |
| 3179 | print_line("Embedded window resize can't be disabled."); |
| 3180 | return; |
| 3181 | } |
| 3182 | |
| 3183 | wd.resize_disabled = p_enabled; |
| 3184 | _update_size_hints(p_window); |
| 3185 | _update_motif_wm_hints(p_window); |
| 3186 | |
| 3187 | XFlush(x11_display); |
| 3188 | } break; |
| 3189 | case WINDOW_FLAG_BORDERLESS: { |
| 3190 | wd.borderless = p_enabled; |
| 3191 | _update_motif_wm_hints(p_window); |
| 3192 | _update_window_mouse_passthrough(p_window); |
| 3193 | } break; |
| 3194 | case WINDOW_FLAG_ALWAYS_ON_TOP: { |
| 3195 | ERR_FAIL_COND_MSG(wd.transient_parent != INVALID_WINDOW_ID, "Can't make a window transient if the 'on top' flag is active."); |
| 3196 | if (p_enabled && wd.embed_parent) { |
| 3197 | print_line("Embedded window can't become on top."); |
| 3198 | return; |
| 3199 | } |
| 3200 | if (p_enabled && wd.fullscreen) { |
| 3201 | _set_wm_maximized(p_window, true); |
| 3202 | } |
| 3203 | |
| 3204 | Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False); |
| 3205 | Atom wm_above = XInternAtom(x11_display, "_NET_WM_STATE_ABOVE", False); |
| 3206 | |
| 3207 | XClientMessageEvent xev; |
| 3208 | memset(&xev, 0, sizeof(xev)); |
| 3209 | xev.type = ClientMessage; |
| 3210 | xev.window = wd.x11_window; |
| 3211 | xev.message_type = wm_state; |
| 3212 | xev.format = 32; |
| 3213 | xev.data.l[0] = p_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE; |
| 3214 | xev.data.l[1] = wm_above; |
| 3215 | xev.data.l[3] = 1; |
| 3216 | XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&xev); |
| 3217 | |
| 3218 | if (!p_enabled && !wd.fullscreen) { |
| 3219 | _set_wm_maximized(p_window, false); |
| 3220 | } |
| 3221 | wd.on_top = p_enabled; |
| 3222 | |
| 3223 | } break; |
| 3224 | case WINDOW_FLAG_TRANSPARENT: { |
| 3225 | wd.layered_window = p_enabled; |
| 3226 | } break; |
| 3227 | case WINDOW_FLAG_NO_FOCUS: { |
| 3228 | wd.no_focus = p_enabled; |
| 3229 | } break; |
| 3230 | case WINDOW_FLAG_MOUSE_PASSTHROUGH: { |
| 3231 | wd.mpass = p_enabled; |
| 3232 | _update_window_mouse_passthrough(p_window); |
| 3233 | } break; |
| 3234 | case WINDOW_FLAG_POPUP: { |
| 3235 | XWindowAttributes xwa; |
| 3236 | XSync(x11_display, False); |
| 3237 | XGetWindowAttributes(x11_display, wd.x11_window, &xwa); |
| 3238 | |
| 3239 | ERR_FAIL_COND_MSG(p_window == MAIN_WINDOW_ID, "Main window can't be popup."); |
| 3240 | ERR_FAIL_COND_MSG((xwa.map_state == IsViewable) && (wd.is_popup != p_enabled), "Popup flag can't changed while window is opened."); |
| 3241 | if (p_enabled && wd.embed_parent) { |
| 3242 | print_line("Embedded window can't be popup."); |
| 3243 | return; |
| 3244 | } |
| 3245 | wd.is_popup = p_enabled; |
| 3246 | } break; |
| 3247 | default: { |
| 3248 | } |
| 3249 | } |
| 3250 | } |
| 3251 | |
| 3252 | bool DisplayServerX11::window_get_flag(WindowFlags p_flag, WindowID p_window) const { |
| 3253 | _THREAD_SAFE_METHOD_ |
| 3254 | |
| 3255 | ERR_FAIL_COND_V(!windows.has(p_window), false); |
| 3256 | const WindowData &wd = windows[p_window]; |
| 3257 | |
| 3258 | switch (p_flag) { |
| 3259 | case WINDOW_FLAG_MAXIMIZE_DISABLED: { |
| 3260 | return wd.no_max_btn; |
| 3261 | } break; |
| 3262 | case WINDOW_FLAG_MINIMIZE_DISABLED: { |
| 3263 | return wd.no_min_btn; |
| 3264 | } break; |
| 3265 | case WINDOW_FLAG_RESIZE_DISABLED: { |
| 3266 | return wd.resize_disabled; |
| 3267 | } break; |
| 3268 | case WINDOW_FLAG_BORDERLESS: { |
| 3269 | return wd.borderless; |
| 3270 | } break; |
| 3271 | case WINDOW_FLAG_ALWAYS_ON_TOP: { |
| 3272 | return wd.on_top; |
| 3273 | } break; |
| 3274 | case WINDOW_FLAG_TRANSPARENT: { |
| 3275 | return wd.layered_window; |
| 3276 | } break; |
| 3277 | case WINDOW_FLAG_NO_FOCUS: { |
| 3278 | return wd.no_focus; |
| 3279 | } break; |
| 3280 | case WINDOW_FLAG_MOUSE_PASSTHROUGH: { |
| 3281 | return wd.mpass; |
| 3282 | } break; |
| 3283 | case WINDOW_FLAG_POPUP: { |
| 3284 | return wd.is_popup; |
| 3285 | } break; |
| 3286 | default: { |
| 3287 | } |
| 3288 | } |
| 3289 | |
| 3290 | return false; |
| 3291 | } |
| 3292 | |
| 3293 | void DisplayServerX11::window_request_attention(WindowID p_window) { |
| 3294 | _THREAD_SAFE_METHOD_ |
| 3295 | |
| 3296 | ERR_FAIL_COND(!windows.has(p_window)); |
| 3297 | const WindowData &wd = windows[p_window]; |
| 3298 | // Using EWMH -- Extended Window Manager Hints |
| 3299 | // |
| 3300 | // Sets the _NET_WM_STATE_DEMANDS_ATTENTION atom for WM_STATE |
| 3301 | // Will be unset by the window manager after user react on the request for attention |
| 3302 | |
| 3303 | XEvent xev; |
| 3304 | Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False); |
| 3305 | Atom wm_attention = XInternAtom(x11_display, "_NET_WM_STATE_DEMANDS_ATTENTION", False); |
| 3306 | |
| 3307 | memset(&xev, 0, sizeof(xev)); |
| 3308 | xev.type = ClientMessage; |
| 3309 | xev.xclient.window = wd.x11_window; |
| 3310 | xev.xclient.message_type = wm_state; |
| 3311 | xev.xclient.format = 32; |
| 3312 | xev.xclient.data.l[0] = _NET_WM_STATE_ADD; |
| 3313 | xev.xclient.data.l[1] = wm_attention; |
| 3314 | |
| 3315 | XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev); |
| 3316 | XFlush(x11_display); |
| 3317 | } |
| 3318 | |
| 3319 | void DisplayServerX11::window_move_to_foreground(WindowID p_window) { |
| 3320 | _THREAD_SAFE_METHOD_ |
| 3321 | |
| 3322 | ERR_FAIL_COND(!windows.has(p_window)); |
| 3323 | const WindowData &wd = windows[p_window]; |
| 3324 | |
| 3325 | XEvent xev; |
| 3326 | Atom net_active_window = XInternAtom(x11_display, "_NET_ACTIVE_WINDOW", False); |
| 3327 | |
| 3328 | memset(&xev, 0, sizeof(xev)); |
| 3329 | xev.type = ClientMessage; |
| 3330 | xev.xclient.window = wd.x11_window; |
| 3331 | xev.xclient.message_type = net_active_window; |
| 3332 | xev.xclient.format = 32; |
| 3333 | xev.xclient.data.l[0] = 1; |
| 3334 | xev.xclient.data.l[1] = CurrentTime; |
| 3335 | |
| 3336 | XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev); |
| 3337 | XFlush(x11_display); |
| 3338 | } |
| 3339 | |
| 3340 | DisplayServerX11::WindowID DisplayServerX11::get_focused_window() const { |
| 3341 | return last_focused_window; |
| 3342 | } |
| 3343 | |
| 3344 | bool DisplayServerX11::window_is_focused(WindowID p_window) const { |
| 3345 | _THREAD_SAFE_METHOD_ |
| 3346 | |
| 3347 | ERR_FAIL_COND_V(!windows.has(p_window), false); |
| 3348 | |
| 3349 | const WindowData &wd = windows[p_window]; |
| 3350 | |
| 3351 | return wd.focused; |
| 3352 | } |
| 3353 | |
| 3354 | bool DisplayServerX11::window_can_draw(WindowID p_window) const { |
| 3355 | //this seems to be all that is provided by X11 |
| 3356 | return window_get_mode(p_window) != WINDOW_MODE_MINIMIZED; |
| 3357 | } |
| 3358 | |
| 3359 | bool DisplayServerX11::can_any_window_draw() const { |
| 3360 | _THREAD_SAFE_METHOD_ |
| 3361 | |
| 3362 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 3363 | if (window_get_mode(E.key) != WINDOW_MODE_MINIMIZED) { |
| 3364 | return true; |
| 3365 | } |
| 3366 | } |
| 3367 | |
| 3368 | return false; |
| 3369 | } |
| 3370 | |
| 3371 | void DisplayServerX11::window_set_ime_active(const bool p_active, WindowID p_window) { |
| 3372 | _THREAD_SAFE_METHOD_ |
| 3373 | |
| 3374 | ERR_FAIL_COND(!windows.has(p_window)); |
| 3375 | WindowData &wd = windows[p_window]; |
| 3376 | |
| 3377 | if (!wd.xic) { |
| 3378 | return; |
| 3379 | } |
| 3380 | if (!wd.focused) { |
| 3381 | wd.ime_active = false; |
| 3382 | im_text = String(); |
| 3383 | im_selection = Vector2i(); |
| 3384 | return; |
| 3385 | } |
| 3386 | |
| 3387 | // Block events polling while changing input focus |
| 3388 | // because it triggers some event polling internally. |
| 3389 | if (p_active) { |
| 3390 | MutexLock mutex_lock(events_mutex); |
| 3391 | |
| 3392 | wd.ime_active = true; |
| 3393 | |
| 3394 | XMapWindow(x11_display, wd.x11_xim_window); |
| 3395 | |
| 3396 | XWindowAttributes xwa; |
| 3397 | XSync(x11_display, False); |
| 3398 | XGetWindowAttributes(x11_display, wd.x11_xim_window, &xwa); |
| 3399 | if (xwa.map_state == IsViewable && _window_focus_check()) { |
| 3400 | _set_input_focus(wd.x11_xim_window, RevertToParent); |
| 3401 | } |
| 3402 | XSetICFocus(wd.xic); |
| 3403 | } else { |
| 3404 | MutexLock mutex_lock(events_mutex); |
| 3405 | XUnsetICFocus(wd.xic); |
| 3406 | XUnmapWindow(x11_display, wd.x11_xim_window); |
| 3407 | wd.ime_active = false; |
| 3408 | |
| 3409 | im_text = String(); |
| 3410 | im_selection = Vector2i(); |
| 3411 | } |
| 3412 | } |
| 3413 | |
| 3414 | void DisplayServerX11::window_set_ime_position(const Point2i &p_pos, WindowID p_window) { |
| 3415 | _THREAD_SAFE_METHOD_ |
| 3416 | |
| 3417 | ERR_FAIL_COND(!windows.has(p_window)); |
| 3418 | WindowData &wd = windows[p_window]; |
| 3419 | |
| 3420 | if (!wd.xic) { |
| 3421 | return; |
| 3422 | } |
| 3423 | if (!wd.focused) { |
| 3424 | return; |
| 3425 | } |
| 3426 | |
| 3427 | if (wd.ime_active) { |
| 3428 | XWindowAttributes xwa; |
| 3429 | XSync(x11_display, False); |
| 3430 | XGetWindowAttributes(x11_display, wd.x11_xim_window, &xwa); |
| 3431 | if (xwa.map_state == IsViewable) { |
| 3432 | XMoveWindow(x11_display, wd.x11_xim_window, p_pos.x, p_pos.y); |
| 3433 | } |
| 3434 | } |
| 3435 | } |
| 3436 | |
| 3437 | int DisplayServerX11::accessibility_should_increase_contrast() const { |
| 3438 | #ifdef DBUS_ENABLED |
| 3439 | if (!portal_desktop) { |
| 3440 | return -1; |
| 3441 | } |
| 3442 | return portal_desktop->get_high_contrast(); |
| 3443 | #endif |
| 3444 | return -1; |
| 3445 | } |
| 3446 | |
| 3447 | int DisplayServerX11::accessibility_screen_reader_active() const { |
| 3448 | #ifdef DBUS_ENABLED |
| 3449 | if (atspi_monitor && atspi_monitor->is_supported()) { |
| 3450 | return atspi_monitor->is_active(); |
| 3451 | } |
| 3452 | #endif |
| 3453 | return -1; |
| 3454 | } |
| 3455 | |
| 3456 | Point2i DisplayServerX11::ime_get_selection() const { |
| 3457 | return im_selection; |
| 3458 | } |
| 3459 | |
| 3460 | String DisplayServerX11::ime_get_text() const { |
| 3461 | return im_text; |
| 3462 | } |
| 3463 | |
| 3464 | void DisplayServerX11::cursor_set_shape(CursorShape p_shape) { |
| 3465 | _THREAD_SAFE_METHOD_ |
| 3466 | |
| 3467 | ERR_FAIL_INDEX(p_shape, CURSOR_MAX); |
| 3468 | |
| 3469 | if (p_shape == current_cursor) { |
| 3470 | return; |
| 3471 | } |
| 3472 | |
| 3473 | if (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED) { |
| 3474 | if (cursors[p_shape] != None) { |
| 3475 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 3476 | XDefineCursor(x11_display, E.value.x11_window, cursors[p_shape]); |
| 3477 | } |
| 3478 | } else if (cursors[CURSOR_ARROW] != None) { |
| 3479 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 3480 | XDefineCursor(x11_display, E.value.x11_window, cursors[CURSOR_ARROW]); |
| 3481 | } |
| 3482 | } |
| 3483 | } |
| 3484 | |
| 3485 | current_cursor = p_shape; |
| 3486 | } |
| 3487 | |
| 3488 | DisplayServerX11::CursorShape DisplayServerX11::cursor_get_shape() const { |
| 3489 | return current_cursor; |
| 3490 | } |
| 3491 | |
| 3492 | void DisplayServerX11::cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) { |
| 3493 | _THREAD_SAFE_METHOD_ |
| 3494 | |
| 3495 | ERR_FAIL_INDEX(p_shape, CURSOR_MAX); |
| 3496 | |
| 3497 | if (p_cursor.is_valid()) { |
| 3498 | HashMap<CursorShape, Vector<Variant>>::Iterator cursor_c = cursors_cache.find(p_shape); |
| 3499 | |
| 3500 | if (cursor_c) { |
| 3501 | if (cursor_c->value[0] == p_cursor && cursor_c->value[1] == p_hotspot) { |
| 3502 | cursor_set_shape(p_shape); |
| 3503 | return; |
| 3504 | } |
| 3505 | |
| 3506 | cursors_cache.erase(p_shape); |
| 3507 | } |
| 3508 | |
| 3509 | Ref<Image> image = _get_cursor_image_from_resource(p_cursor, p_hotspot); |
| 3510 | ERR_FAIL_COND(image.is_null()); |
| 3511 | Vector2i texture_size = image->get_size(); |
| 3512 | |
| 3513 | // Create the cursor structure |
| 3514 | XcursorImage *cursor_image = XcursorImageCreate(texture_size.width, texture_size.height); |
| 3515 | XcursorUInt image_size = texture_size.width * texture_size.height; |
| 3516 | XcursorDim size = sizeof(XcursorPixel) * image_size; |
| 3517 | |
| 3518 | cursor_image->version = 1; |
| 3519 | cursor_image->size = size; |
| 3520 | cursor_image->xhot = p_hotspot.x; |
| 3521 | cursor_image->yhot = p_hotspot.y; |
| 3522 | |
| 3523 | // allocate memory to contain the whole file |
| 3524 | cursor_image->pixels = (XcursorPixel *)memalloc(size); |
| 3525 | |
| 3526 | for (XcursorPixel index = 0; index < image_size; index++) { |
| 3527 | int row_index = std::floor(index / texture_size.width); |
| 3528 | int column_index = index % int(texture_size.width); |
| 3529 | |
| 3530 | *(cursor_image->pixels + index) = image->get_pixel(column_index, row_index).to_argb32(); |
| 3531 | } |
| 3532 | |
| 3533 | ERR_FAIL_NULL(cursor_image->pixels); |
| 3534 | |
| 3535 | // Save it for a further usage |
| 3536 | cursors[p_shape] = XcursorImageLoadCursor(x11_display, cursor_image); |
| 3537 | |
| 3538 | Vector<Variant> params; |
| 3539 | params.push_back(p_cursor); |
| 3540 | params.push_back(p_hotspot); |
| 3541 | cursors_cache.insert(p_shape, params); |
| 3542 | |
| 3543 | if (p_shape == current_cursor) { |
| 3544 | if (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED) { |
| 3545 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 3546 | XDefineCursor(x11_display, E.value.x11_window, cursors[p_shape]); |
| 3547 | } |
| 3548 | } |
| 3549 | } |
| 3550 | |
| 3551 | memfree(cursor_image->pixels); |
| 3552 | XcursorImageDestroy(cursor_image); |
| 3553 | } else { |
| 3554 | // Reset to default system cursor |
| 3555 | if (cursor_img[p_shape]) { |
| 3556 | cursors[p_shape] = XcursorImageLoadCursor(x11_display, cursor_img[p_shape]); |
| 3557 | } |
| 3558 | |
| 3559 | cursors_cache.erase(p_shape); |
| 3560 | |
| 3561 | CursorShape c = current_cursor; |
| 3562 | current_cursor = CURSOR_MAX; |
| 3563 | cursor_set_shape(c); |
| 3564 | } |
| 3565 | } |
| 3566 | |
| 3567 | bool DisplayServerX11::get_swap_cancel_ok() { |
| 3568 | return swap_cancel_ok; |
| 3569 | } |
| 3570 | |
| 3571 | int DisplayServerX11::keyboard_get_layout_count() const { |
| 3572 | int _group_count = 0; |
| 3573 | XkbDescRec *kbd = XkbAllocKeyboard(); |
| 3574 | if (kbd) { |
| 3575 | kbd->dpy = x11_display; |
| 3576 | XkbGetControls(x11_display, XkbAllControlsMask, kbd); |
| 3577 | XkbGetNames(x11_display, XkbSymbolsNameMask, kbd); |
| 3578 | |
| 3579 | const Atom *groups = kbd->names->groups; |
| 3580 | if (kbd->ctrls != nullptr) { |
| 3581 | _group_count = kbd->ctrls->num_groups; |
| 3582 | } else { |
| 3583 | while (_group_count < XkbNumKbdGroups && groups[_group_count] != None) { |
| 3584 | _group_count++; |
| 3585 | } |
| 3586 | } |
| 3587 | XkbFreeKeyboard(kbd, 0, true); |
| 3588 | } |
| 3589 | return _group_count; |
| 3590 | } |
| 3591 | |
| 3592 | int DisplayServerX11::keyboard_get_current_layout() const { |
| 3593 | XkbStateRec state; |
| 3594 | XkbGetState(x11_display, XkbUseCoreKbd, &state); |
| 3595 | return state.group; |
| 3596 | } |
| 3597 | |
| 3598 | void DisplayServerX11::keyboard_set_current_layout(int p_index) { |
| 3599 | ERR_FAIL_INDEX(p_index, keyboard_get_layout_count()); |
| 3600 | XkbLockGroup(x11_display, XkbUseCoreKbd, p_index); |
| 3601 | } |
| 3602 | |
| 3603 | String DisplayServerX11::keyboard_get_layout_language(int p_index) const { |
| 3604 | String ret; |
| 3605 | XkbDescRec *kbd = XkbAllocKeyboard(); |
| 3606 | if (kbd) { |
| 3607 | kbd->dpy = x11_display; |
| 3608 | XkbGetControls(x11_display, XkbAllControlsMask, kbd); |
| 3609 | XkbGetNames(x11_display, XkbSymbolsNameMask, kbd); |
| 3610 | XkbGetNames(x11_display, XkbGroupNamesMask, kbd); |
| 3611 | |
| 3612 | int _group_count = 0; |
| 3613 | const Atom *groups = kbd->names->groups; |
| 3614 | if (kbd->ctrls != nullptr) { |
| 3615 | _group_count = kbd->ctrls->num_groups; |
| 3616 | } else { |
| 3617 | while (_group_count < XkbNumKbdGroups && groups[_group_count] != None) { |
| 3618 | _group_count++; |
| 3619 | } |
| 3620 | } |
| 3621 | |
| 3622 | Atom names = kbd->names->symbols; |
| 3623 | if (names != None) { |
| 3624 | Vector<String> info = get_atom_name(x11_display, names).split("+"); |
| 3625 | if (p_index >= 0 && p_index < _group_count) { |
| 3626 | if (p_index + 1 < info.size()) { |
| 3627 | ret = info[p_index + 1]; // Skip "pc" at the start and "inet"/"group" at the end of symbols. |
| 3628 | } else { |
| 3629 | ret = "en"; // No symbol for layout fallback to "en". |
| 3630 | } |
| 3631 | } else { |
| 3632 | ERR_PRINT("Index " + itos(p_index) + "is out of bounds (" + itos(_group_count) + ")."); |
| 3633 | } |
| 3634 | } |
| 3635 | XkbFreeKeyboard(kbd, 0, true); |
| 3636 | } |
| 3637 | return ret.substr(0, 2); |
| 3638 | } |
| 3639 | |
| 3640 | String DisplayServerX11::keyboard_get_layout_name(int p_index) const { |
| 3641 | String ret; |
| 3642 | XkbDescRec *kbd = XkbAllocKeyboard(); |
| 3643 | if (kbd) { |
| 3644 | kbd->dpy = x11_display; |
| 3645 | XkbGetControls(x11_display, XkbAllControlsMask, kbd); |
| 3646 | XkbGetNames(x11_display, XkbSymbolsNameMask, kbd); |
| 3647 | XkbGetNames(x11_display, XkbGroupNamesMask, kbd); |
| 3648 | |
| 3649 | int _group_count = 0; |
| 3650 | const Atom *groups = kbd->names->groups; |
| 3651 | if (kbd->ctrls != nullptr) { |
| 3652 | _group_count = kbd->ctrls->num_groups; |
| 3653 | } else { |
| 3654 | while (_group_count < XkbNumKbdGroups && groups[_group_count] != None) { |
| 3655 | _group_count++; |
| 3656 | } |
| 3657 | } |
| 3658 | |
| 3659 | if (p_index >= 0 && p_index < _group_count) { |
| 3660 | ret = get_atom_name(x11_display, groups[p_index]); |
| 3661 | } else { |
| 3662 | ERR_PRINT("Index " + itos(p_index) + "is out of bounds (" + itos(_group_count) + ")."); |
| 3663 | } |
| 3664 | XkbFreeKeyboard(kbd, 0, true); |
| 3665 | } |
| 3666 | return ret; |
| 3667 | } |
| 3668 | |
| 3669 | Key DisplayServerX11::keyboard_get_keycode_from_physical(Key p_keycode) const { |
| 3670 | Key modifiers = p_keycode & KeyModifierMask::MODIFIER_MASK; |
| 3671 | Key keycode_no_mod = p_keycode & KeyModifierMask::CODE_MASK; |
| 3672 | unsigned int xkeycode = KeyMappingX11::get_xlibcode(keycode_no_mod); |
| 3673 | KeySym xkeysym = XkbKeycodeToKeysym(x11_display, xkeycode, keyboard_get_current_layout(), 0); |
| 3674 | if (is_ascii_lower_case(xkeysym)) { |
| 3675 | xkeysym -= ('a' - 'A'); |
| 3676 | } |
| 3677 | |
| 3678 | Key key = KeyMappingX11::get_keycode(xkeysym); |
| 3679 | // If not found, fallback to QWERTY. |
| 3680 | // This should match the behavior of the event pump |
| 3681 | if (key == Key::NONE) { |
| 3682 | return p_keycode; |
| 3683 | } |
| 3684 | return (Key)(key | modifiers); |
| 3685 | } |
| 3686 | |
| 3687 | Key DisplayServerX11::keyboard_get_label_from_physical(Key p_keycode) const { |
| 3688 | Key modifiers = p_keycode & KeyModifierMask::MODIFIER_MASK; |
| 3689 | Key keycode_no_mod = p_keycode & KeyModifierMask::CODE_MASK; |
| 3690 | unsigned int xkeycode = KeyMappingX11::get_xlibcode(keycode_no_mod); |
| 3691 | KeySym xkeysym = XkbKeycodeToKeysym(x11_display, xkeycode, keyboard_get_current_layout(), 0); |
| 3692 | if (is_ascii_lower_case(xkeysym)) { |
| 3693 | xkeysym -= ('a' - 'A'); |
| 3694 | } |
| 3695 | |
| 3696 | Key key = KeyMappingX11::get_keycode(xkeysym); |
| 3697 | #ifdef XKB_ENABLED |
| 3698 | if (xkb_loaded_v08p) { |
| 3699 | char32_t chr = xkb_keysym_to_utf32(xkb_keysym_to_upper(xkeysym)); |
| 3700 | if (chr != 0) { |
| 3701 | String keysym = String::chr(chr); |
| 3702 | key = fix_key_label(keysym[0], KeyMappingX11::get_keycode(xkeysym)); |
| 3703 | } |
| 3704 | } |
| 3705 | #endif |
| 3706 | |
| 3707 | // If not found, fallback to QWERTY. |
| 3708 | // This should match the behavior of the event pump |
| 3709 | if (key == Key::NONE) { |
| 3710 | return p_keycode; |
| 3711 | } |
| 3712 | return (Key)(key | modifiers); |
| 3713 | } |
| 3714 | |
| 3715 | bool DisplayServerX11::color_picker(const Callable &p_callback) { |
| 3716 | #ifdef DBUS_ENABLED |
| 3717 | if (!portal_desktop) { |
| 3718 | return false; |
| 3719 | } |
| 3720 | WindowID window_id = last_focused_window; |
| 3721 | |
| 3722 | if (!windows.has(window_id)) { |
| 3723 | window_id = MAIN_WINDOW_ID; |
| 3724 | } |
| 3725 | |
| 3726 | String xid = vformat("x11:%x", (uint64_t)windows[window_id].x11_window); |
| 3727 | return portal_desktop->color_picker(xid, p_callback); |
| 3728 | #else |
| 3729 | return false; |
| 3730 | #endif |
| 3731 | } |
| 3732 | |
| 3733 | DisplayServerX11::Property DisplayServerX11::_read_property(Display *p_display, Window p_window, Atom p_property) { |
| 3734 | Atom actual_type = None; |
| 3735 | int actual_format = 0; |
| 3736 | unsigned long nitems = 0; |
| 3737 | unsigned long bytes_after = 0; |
| 3738 | unsigned char *ret = nullptr; |
| 3739 | |
| 3740 | // Keep trying to read the property until there are no bytes unread. |
| 3741 | if (p_property != None) { |
| 3742 | int read_bytes = 1024; |
| 3743 | do { |
| 3744 | if (ret != nullptr) { |
| 3745 | XFree(ret); |
| 3746 | } |
| 3747 | |
| 3748 | XGetWindowProperty(p_display, p_window, p_property, 0, read_bytes, False, AnyPropertyType, |
| 3749 | &actual_type, &actual_format, &nitems, &bytes_after, |
| 3750 | &ret); |
| 3751 | |
| 3752 | read_bytes *= 2; |
| 3753 | |
| 3754 | } while (bytes_after != 0); |
| 3755 | } |
| 3756 | |
| 3757 | Property p = { ret, actual_format, (int)nitems, actual_type }; |
| 3758 | |
| 3759 | return p; |
| 3760 | } |
| 3761 | |
| 3762 | static Atom pick_target_from_list(Display *p_display, const Atom *p_list, int p_count) { |
| 3763 | static const char *target_type = "text/uri-list"; |
| 3764 | |
| 3765 | for (int i = 0; i < p_count; i++) { |
| 3766 | Atom atom = p_list[i]; |
| 3767 | |
| 3768 | if (atom != None && get_atom_name(p_display, atom) == target_type) { |
| 3769 | return atom; |
| 3770 | } |
| 3771 | } |
| 3772 | return None; |
| 3773 | } |
| 3774 | |
| 3775 | static Atom pick_target_from_atoms(Display *p_disp, Atom p_t1, Atom p_t2, Atom p_t3) { |
| 3776 | static const char *target_type = "text/uri-list"; |
| 3777 | if (p_t1 != None && get_atom_name(p_disp, p_t1) == target_type) { |
| 3778 | return p_t1; |
| 3779 | } |
| 3780 | |
| 3781 | if (p_t2 != None && get_atom_name(p_disp, p_t2) == target_type) { |
| 3782 | return p_t2; |
| 3783 | } |
| 3784 | |
| 3785 | if (p_t3 != None && get_atom_name(p_disp, p_t3) == target_type) { |
| 3786 | return p_t3; |
| 3787 | } |
| 3788 | |
| 3789 | return None; |
| 3790 | } |
| 3791 | |
| 3792 | void DisplayServerX11::_get_key_modifier_state(unsigned int p_x11_state, Ref<InputEventWithModifiers> state) { |
| 3793 | state->set_shift_pressed((p_x11_state & ShiftMask)); |
| 3794 | state->set_ctrl_pressed((p_x11_state & ControlMask)); |
| 3795 | state->set_alt_pressed((p_x11_state & Mod1Mask /*|| p_x11_state&Mod5Mask*/)); //altgr should not count as alt |
| 3796 | state->set_meta_pressed((p_x11_state & Mod4Mask)); |
| 3797 | } |
| 3798 | |
| 3799 | void DisplayServerX11::_handle_key_event(WindowID p_window, XKeyEvent *p_event, LocalVector<XEvent> &p_events, uint32_t &p_event_index, bool p_echo) { |
| 3800 | WindowData &wd = windows[p_window]; |
| 3801 | // X11 functions don't know what const is |
| 3802 | XKeyEvent *xkeyevent = p_event; |
| 3803 | |
| 3804 | if (wd.ime_in_progress) { |
| 3805 | return; |
| 3806 | } |
| 3807 | if (wd.ime_suppress_next_keyup) { |
| 3808 | wd.ime_suppress_next_keyup = false; |
| 3809 | if (xkeyevent->type != KeyPress) { |
| 3810 | return; |
| 3811 | } |
| 3812 | } |
| 3813 | |
| 3814 | // This code was pretty difficult to write. |
| 3815 | // The docs stink and every toolkit seems to |
| 3816 | // do it in a different way. |
| 3817 | |
| 3818 | /* Phase 1, obtain a proper keysym */ |
| 3819 | |
| 3820 | // This was also very difficult to figure out. |
| 3821 | // You'd expect you could just use Keysym provided by |
| 3822 | // XKeycodeToKeysym to obtain internationalized |
| 3823 | // input.. WRONG!! |
| 3824 | // you must use XLookupString (???) which not only wastes |
| 3825 | // cycles generating an unnecessary string, but also |
| 3826 | // still works in half the cases. (won't handle deadkeys) |
| 3827 | // For more complex input methods (deadkeys and more advanced) |
| 3828 | // you have to use XmbLookupString (??). |
| 3829 | // So then you have to choose which of both results |
| 3830 | // you want to keep. |
| 3831 | // This is a real bizarreness and cpu waster. |
| 3832 | |
| 3833 | KeySym keysym_keycode = 0; // keysym used to find a keycode |
| 3834 | KeySym keysym_unicode = 0; // keysym used to find unicode |
| 3835 | |
| 3836 | // XLookupString returns keysyms usable as nice keycodes. |
| 3837 | char str[256] = {}; |
| 3838 | XKeyEvent xkeyevent_no_mod = *xkeyevent; |
| 3839 | xkeyevent_no_mod.state &= 0xFF00; |
| 3840 | XLookupString(xkeyevent, str, 255, &keysym_unicode, nullptr); |
| 3841 | XLookupString(&xkeyevent_no_mod, nullptr, 0, &keysym_keycode, nullptr); |
| 3842 | |
| 3843 | // Get a normalized keysym (ignoring modifiers like Shift/Ctrl). |
| 3844 | String keysym; |
| 3845 | #ifdef XKB_ENABLED |
| 3846 | if (xkb_loaded_v08p) { |
| 3847 | KeySym keysym_unicode_nm = 0; // keysym used to find unicode |
| 3848 | XLookupString(&xkeyevent_no_mod, nullptr, 0, &keysym_unicode_nm, nullptr); |
| 3849 | |
| 3850 | // Unicode codepoint corresponding to the pressed key. |
| 3851 | // Printable keys (letters, numbers, symbols) return a valid codepoint. |
| 3852 | u_int32_t unicode_cp = xkb_keysym_to_utf32(xkb_keysym_to_upper(keysym_unicode_nm)); |
| 3853 | |
| 3854 | // Non-printable keys (Ctrl, Home, CapsLock, F1, etc.) return 0, so we skip them. |
| 3855 | if (unicode_cp != 0) { |
| 3856 | keysym = String::chr(unicode_cp); |
| 3857 | } |
| 3858 | } |
| 3859 | #endif |
| 3860 | |
| 3861 | // Meanwhile, XLookupString returns keysyms useful for unicode. |
| 3862 | |
| 3863 | if (!xmbstring) { |
| 3864 | // keep a temporary buffer for the string |
| 3865 | xmbstring = (char *)memalloc(sizeof(char) * 8); |
| 3866 | xmblen = 8; |
| 3867 | } |
| 3868 | |
| 3869 | if (xkeyevent->type == KeyPress && wd.xic) { |
| 3870 | Status status; |
| 3871 | #ifdef X_HAVE_UTF8_STRING |
| 3872 | int utf8len = 8; |
| 3873 | char *utf8string = (char *)memalloc(sizeof(char) * utf8len); |
| 3874 | int utf8bytes = Xutf8LookupString(wd.xic, xkeyevent, utf8string, |
| 3875 | utf8len - 1, &keysym_unicode, &status); |
| 3876 | if (status == XBufferOverflow) { |
| 3877 | utf8len = utf8bytes + 1; |
| 3878 | utf8string = (char *)memrealloc(utf8string, utf8len); |
| 3879 | utf8bytes = Xutf8LookupString(wd.xic, xkeyevent, utf8string, |
| 3880 | utf8len - 1, &keysym_unicode, &status); |
| 3881 | } |
| 3882 | utf8string[utf8bytes] = '\0'; |
| 3883 | |
| 3884 | if (status == XLookupChars) { |
| 3885 | bool keypress = xkeyevent->type == KeyPress; |
| 3886 | |
| 3887 | Key keycode = Key::NONE; |
| 3888 | if (KeyMappingX11::is_sym_numpad(keysym_unicode)) { |
| 3889 | // Special case for numpad keys. |
| 3890 | keycode = KeyMappingX11::get_keycode(keysym_unicode); |
| 3891 | } |
| 3892 | |
| 3893 | if (keycode == Key::NONE) { |
| 3894 | keycode = KeyMappingX11::get_keycode(keysym_keycode); |
| 3895 | } |
| 3896 | |
| 3897 | Key physical_keycode = KeyMappingX11::get_scancode(xkeyevent->keycode); |
| 3898 | |
| 3899 | if (keycode >= Key::A + 32 && keycode <= Key::Z + 32) { |
| 3900 | keycode -= 'a' - 'A'; |
| 3901 | } |
| 3902 | |
| 3903 | String tmp = String::utf8(utf8string, utf8bytes); |
| 3904 | for (int i = 0; i < tmp.length(); i++) { |
| 3905 | Ref<InputEventKey> k; |
| 3906 | k.instantiate(); |
| 3907 | if (physical_keycode == Key::NONE && keycode == Key::NONE && tmp[i] == 0) { |
| 3908 | continue; |
| 3909 | } |
| 3910 | |
| 3911 | if (keycode == Key::NONE) { |
| 3912 | keycode = (Key)physical_keycode; |
| 3913 | } |
| 3914 | |
| 3915 | _get_key_modifier_state(xkeyevent->state, k); |
| 3916 | |
| 3917 | k->set_window_id(p_window); |
| 3918 | k->set_pressed(keypress); |
| 3919 | |
| 3920 | k->set_keycode(keycode); |
| 3921 | k->set_physical_keycode(physical_keycode); |
| 3922 | if (!keysym.is_empty()) { |
| 3923 | k->set_key_label(fix_key_label(keysym[0], keycode)); |
| 3924 | } else { |
| 3925 | k->set_key_label(keycode); |
| 3926 | } |
| 3927 | if (keypress) { |
| 3928 | k->set_unicode(fix_unicode(tmp[i])); |
| 3929 | } |
| 3930 | |
| 3931 | k->set_echo(false); |
| 3932 | |
| 3933 | if (k->get_keycode() == Key::BACKTAB) { |
| 3934 | //make it consistent across platforms. |
| 3935 | k->set_keycode(Key::TAB); |
| 3936 | k->set_physical_keycode(Key::TAB); |
| 3937 | k->set_shift_pressed(true); |
| 3938 | } |
| 3939 | |
| 3940 | Input::get_singleton()->parse_input_event(k); |
| 3941 | } |
| 3942 | memfree(utf8string); |
| 3943 | return; |
| 3944 | } |
| 3945 | memfree(utf8string); |
| 3946 | #else |
| 3947 | do { |
| 3948 | int mnbytes = XmbLookupString(xic, xkeyevent, xmbstring, xmblen - 1, &keysym_unicode, &status); |
| 3949 | xmbstring[mnbytes] = '\0'; |
| 3950 | |
| 3951 | if (status == XBufferOverflow) { |
| 3952 | xmblen = mnbytes + 1; |
| 3953 | xmbstring = (char *)memrealloc(xmbstring, xmblen); |
| 3954 | } |
| 3955 | } while (status == XBufferOverflow); |
| 3956 | #endif |
| 3957 | #ifdef XKB_ENABLED |
| 3958 | } else if (xkeyevent->type == KeyPress && wd.xkb_state && xkb_loaded_v05p) { |
| 3959 | xkb_compose_feed_result res = xkb_compose_state_feed(wd.xkb_state, keysym_unicode); |
| 3960 | if (res == XKB_COMPOSE_FEED_ACCEPTED) { |
| 3961 | if (xkb_compose_state_get_status(wd.xkb_state) == XKB_COMPOSE_COMPOSED) { |
| 3962 | bool keypress = xkeyevent->type == KeyPress; |
| 3963 | Key physical_keycode = KeyMappingX11::get_scancode(xkeyevent->keycode); |
| 3964 | KeyLocation key_location = KeyMappingX11::get_location(xkeyevent->keycode); |
| 3965 | |
| 3966 | Key keycode = Key::NONE; |
| 3967 | if (KeyMappingX11::is_sym_numpad(keysym_unicode)) { |
| 3968 | // Special case for numpad keys. |
| 3969 | keycode = KeyMappingX11::get_keycode(keysym_unicode); |
| 3970 | } |
| 3971 | |
| 3972 | if (keycode == Key::NONE) { |
| 3973 | keycode = KeyMappingX11::get_keycode(keysym_keycode); |
| 3974 | } |
| 3975 | |
| 3976 | if (keycode >= Key::A + 32 && keycode <= Key::Z + 32) { |
| 3977 | keycode -= 'a' - 'A'; |
| 3978 | } |
| 3979 | |
| 3980 | char str_xkb[256] = {}; |
| 3981 | int str_xkb_size = xkb_compose_state_get_utf8(wd.xkb_state, str_xkb, 255); |
| 3982 | |
| 3983 | String tmp = String::utf8(str_xkb, str_xkb_size); |
| 3984 | for (int i = 0; i < tmp.length(); i++) { |
| 3985 | Ref<InputEventKey> k; |
| 3986 | k.instantiate(); |
| 3987 | if (physical_keycode == Key::NONE && keycode == Key::NONE && tmp[i] == 0) { |
| 3988 | continue; |
| 3989 | } |
| 3990 | |
| 3991 | if (keycode == Key::NONE) { |
| 3992 | keycode = (Key)physical_keycode; |
| 3993 | } |
| 3994 | |
| 3995 | _get_key_modifier_state(xkeyevent->state, k); |
| 3996 | |
| 3997 | k->set_window_id(p_window); |
| 3998 | k->set_pressed(keypress); |
| 3999 | |
| 4000 | k->set_keycode(keycode); |
| 4001 | k->set_physical_keycode(physical_keycode); |
| 4002 | if (!keysym.is_empty()) { |
| 4003 | k->set_key_label(fix_key_label(keysym[0], keycode)); |
| 4004 | } else { |
| 4005 | k->set_key_label(keycode); |
| 4006 | } |
| 4007 | if (keypress) { |
| 4008 | k->set_unicode(fix_unicode(tmp[i])); |
| 4009 | } |
| 4010 | |
| 4011 | k->set_location(key_location); |
| 4012 | |
| 4013 | k->set_echo(false); |
| 4014 | |
| 4015 | if (k->get_keycode() == Key::BACKTAB) { |
| 4016 | //make it consistent across platforms. |
| 4017 | k->set_keycode(Key::TAB); |
| 4018 | k->set_physical_keycode(Key::TAB); |
| 4019 | k->set_shift_pressed(true); |
| 4020 | } |
| 4021 | |
| 4022 | Input::get_singleton()->parse_input_event(k); |
| 4023 | } |
| 4024 | return; |
| 4025 | } |
| 4026 | } |
| 4027 | #endif |
| 4028 | } |
| 4029 | |
| 4030 | /* Phase 2, obtain a Godot keycode from the keysym */ |
| 4031 | |
| 4032 | // KeyMappingX11 just translated the X11 keysym to a PIGUI |
| 4033 | // keysym, so it works in all platforms the same. |
| 4034 | |
| 4035 | Key keycode = Key::NONE; |
| 4036 | if (KeyMappingX11::is_sym_numpad(keysym_unicode) || KeyMappingX11::is_sym_numpad(keysym_keycode)) { |
| 4037 | // Special case for numpad keys. |
| 4038 | keycode = KeyMappingX11::get_keycode(keysym_unicode); |
| 4039 | } |
| 4040 | |
| 4041 | if (keycode == Key::NONE) { |
| 4042 | keycode = KeyMappingX11::get_keycode(keysym_keycode); |
| 4043 | } |
| 4044 | |
| 4045 | Key physical_keycode = KeyMappingX11::get_scancode(xkeyevent->keycode); |
| 4046 | |
| 4047 | KeyLocation key_location = KeyMappingX11::get_location(xkeyevent->keycode); |
| 4048 | |
| 4049 | /* Phase 3, obtain a unicode character from the keysym */ |
| 4050 | |
| 4051 | // KeyMappingX11 also translates keysym to unicode. |
| 4052 | // It does a binary search on a table to translate |
| 4053 | // most properly. |
| 4054 | char32_t unicode = keysym_unicode > 0 ? KeyMappingX11::get_unicode_from_keysym(keysym_unicode) : 0; |
| 4055 | |
| 4056 | /* Phase 4, determine if event must be filtered */ |
| 4057 | |
| 4058 | // This seems to be a side-effect of using XIM. |
| 4059 | // XFilterEvent looks like a core X11 function, |
| 4060 | // but it's actually just used to see if we must |
| 4061 | // ignore a deadkey, or events XIM determines |
| 4062 | // must not reach the actual gui. |
| 4063 | // Guess it was a design problem of the extension |
| 4064 | |
| 4065 | bool keypress = xkeyevent->type == KeyPress; |
| 4066 | |
| 4067 | if (physical_keycode == Key::NONE && keycode == Key::NONE && unicode == 0) { |
| 4068 | return; |
| 4069 | } |
| 4070 | |
| 4071 | if (keycode == Key::NONE) { |
| 4072 | keycode = (Key)physical_keycode; |
| 4073 | } |
| 4074 | |
| 4075 | /* Phase 5, determine modifier mask */ |
| 4076 | |
| 4077 | // No problems here, except I had no way to |
| 4078 | // know Mod1 was ALT and Mod4 was META (applekey/winkey) |
| 4079 | // just tried Mods until i found them. |
| 4080 | |
| 4081 | //print_verbose("mod1: "+itos(xkeyevent->state&Mod1Mask)+" mod 5: "+itos(xkeyevent->state&Mod5Mask)); |
| 4082 | |
| 4083 | Ref<InputEventKey> k; |
| 4084 | k.instantiate(); |
| 4085 | k->set_window_id(p_window); |
| 4086 | |
| 4087 | _get_key_modifier_state(xkeyevent->state, k); |
| 4088 | |
| 4089 | /* Phase 6, determine echo character */ |
| 4090 | |
| 4091 | // Echo characters in X11 are a keyrelease and a keypress |
| 4092 | // one after the other with the (almot) same timestamp. |
| 4093 | // To detect them, i compare to the next event in list and |
| 4094 | // check that their difference in time is below a threshold. |
| 4095 | |
| 4096 | if (xkeyevent->type != KeyPress) { |
| 4097 | p_echo = false; |
| 4098 | |
| 4099 | // make sure there are events pending, |
| 4100 | // so this call won't block. |
| 4101 | if (p_event_index + 1 < p_events.size()) { |
| 4102 | XEvent &peek_event = p_events[p_event_index + 1]; |
| 4103 | |
| 4104 | // I'm using a threshold of 5 msecs, |
| 4105 | // since sometimes there seems to be a little |
| 4106 | // jitter. I'm still not convinced that all this approach |
| 4107 | // is correct, but the xorg developers are |
| 4108 | // not very helpful today. |
| 4109 | |
| 4110 | #define ABSDIFF(x, y) (((x) < (y)) ? ((y) - (x)) : ((x) - (y))) |
| 4111 | ::Time threshold = ABSDIFF(peek_event.xkey.time, xkeyevent->time); |
| 4112 | #undef ABSDIFF |
| 4113 | if (peek_event.type == KeyPress && threshold < 5) { |
| 4114 | KeySym rk; |
| 4115 | XLookupString((XKeyEvent *)&peek_event, str, 256, &rk, nullptr); |
| 4116 | if (rk == keysym_keycode) { |
| 4117 | // Consume to next event. |
| 4118 | ++p_event_index; |
| 4119 | _handle_key_event(p_window, (XKeyEvent *)&peek_event, p_events, p_event_index, true); |
| 4120 | return; //ignore current, echo next |
| 4121 | } |
| 4122 | } |
| 4123 | |
| 4124 | // use the time from peek_event so it always works |
| 4125 | } |
| 4126 | |
| 4127 | // save the time to check for echo when keypress happens |
| 4128 | } |
| 4129 | |
| 4130 | /* Phase 7, send event to Window */ |
| 4131 | |
| 4132 | k->set_pressed(keypress); |
| 4133 | |
| 4134 | if (keycode >= Key::A + 32 && keycode <= Key::Z + 32) { |
| 4135 | keycode -= int('a' - 'A'); |
| 4136 | } |
| 4137 | |
| 4138 | k->set_keycode(keycode); |
| 4139 | k->set_physical_keycode((Key)physical_keycode); |
| 4140 | if (!keysym.is_empty()) { |
| 4141 | k->set_key_label(fix_key_label(keysym[0], keycode)); |
| 4142 | } else { |
| 4143 | k->set_key_label(keycode); |
| 4144 | } |
| 4145 | if (keypress) { |
| 4146 | k->set_unicode(fix_unicode(unicode)); |
| 4147 | } |
| 4148 | |
| 4149 | k->set_location(key_location); |
| 4150 | |
| 4151 | k->set_echo(p_echo); |
| 4152 | |
| 4153 | if (k->get_keycode() == Key::BACKTAB) { |
| 4154 | //make it consistent across platforms. |
| 4155 | k->set_keycode(Key::TAB); |
| 4156 | k->set_physical_keycode(Key::TAB); |
| 4157 | k->set_shift_pressed(true); |
| 4158 | } |
| 4159 | |
| 4160 | //don't set mod state if modifier keys are released by themselves |
| 4161 | //else event.is_action() will not work correctly here |
| 4162 | if (!k->is_pressed()) { |
| 4163 | if (k->get_keycode() == Key::SHIFT) { |
| 4164 | k->set_shift_pressed(false); |
| 4165 | } else if (k->get_keycode() == Key::CTRL) { |
| 4166 | k->set_ctrl_pressed(false); |
| 4167 | } else if (k->get_keycode() == Key::ALT) { |
| 4168 | k->set_alt_pressed(false); |
| 4169 | } else if (k->get_keycode() == Key::META) { |
| 4170 | k->set_meta_pressed(false); |
| 4171 | } |
| 4172 | } |
| 4173 | |
| 4174 | bool last_is_pressed = Input::get_singleton()->is_key_pressed(k->get_keycode()); |
| 4175 | if (k->is_pressed()) { |
| 4176 | if (last_is_pressed) { |
| 4177 | k->set_echo(true); |
| 4178 | } |
| 4179 | } |
| 4180 | |
| 4181 | Input::get_singleton()->parse_input_event(k); |
| 4182 | } |
| 4183 | |
| 4184 | Atom DisplayServerX11::_process_selection_request_target(Atom p_target, Window p_requestor, Atom p_property, Atom p_selection) const { |
| 4185 | if (p_target == XInternAtom(x11_display, "TARGETS", 0)) { |
| 4186 | // Request to list all supported targets. |
| 4187 | Atom data[9]; |
| 4188 | data[0] = XInternAtom(x11_display, "TARGETS", 0); |
| 4189 | data[1] = XInternAtom(x11_display, "SAVE_TARGETS", 0); |
| 4190 | data[2] = XInternAtom(x11_display, "MULTIPLE", 0); |
| 4191 | data[3] = XInternAtom(x11_display, "UTF8_STRING", 0); |
| 4192 | data[4] = XInternAtom(x11_display, "COMPOUND_TEXT", 0); |
| 4193 | data[5] = XInternAtom(x11_display, "TEXT", 0); |
| 4194 | data[6] = XA_STRING; |
| 4195 | data[7] = XInternAtom(x11_display, "text/plain;charset=utf-8", 0); |
| 4196 | data[8] = XInternAtom(x11_display, "text/plain", 0); |
| 4197 | |
| 4198 | XChangeProperty(x11_display, |
| 4199 | p_requestor, |
| 4200 | p_property, |
| 4201 | XA_ATOM, |
| 4202 | 32, |
| 4203 | PropModeReplace, |
| 4204 | (unsigned char *)&data, |
| 4205 | std_size(data)); |
| 4206 | return p_property; |
| 4207 | } else if (p_target == XInternAtom(x11_display, "SAVE_TARGETS", 0)) { |
| 4208 | // Request to check if SAVE_TARGETS is supported, nothing special to do. |
| 4209 | XChangeProperty(x11_display, |
| 4210 | p_requestor, |
| 4211 | p_property, |
| 4212 | XInternAtom(x11_display, "NULL", False), |
| 4213 | 32, |
| 4214 | PropModeReplace, |
| 4215 | nullptr, |
| 4216 | 0); |
| 4217 | return p_property; |
| 4218 | } else if (p_target == XInternAtom(x11_display, "UTF8_STRING", 0) || |
| 4219 | p_target == XInternAtom(x11_display, "COMPOUND_TEXT", 0) || |
| 4220 | p_target == XInternAtom(x11_display, "TEXT", 0) || |
| 4221 | p_target == XA_STRING || |
| 4222 | p_target == XInternAtom(x11_display, "text/plain;charset=utf-8", 0) || |
| 4223 | p_target == XInternAtom(x11_display, "text/plain", 0)) { |
| 4224 | // Directly using internal clipboard because we know our window |
| 4225 | // is the owner during a selection request. |
| 4226 | CharString clip; |
| 4227 | static const char *target_type = "PRIMARY"; |
| 4228 | if (p_selection != None && get_atom_name(x11_display, p_selection) == target_type) { |
| 4229 | clip = internal_clipboard_primary.utf8(); |
| 4230 | } else { |
| 4231 | clip = internal_clipboard.utf8(); |
| 4232 | } |
| 4233 | XChangeProperty(x11_display, |
| 4234 | p_requestor, |
| 4235 | p_property, |
| 4236 | p_target, |
| 4237 | 8, |
| 4238 | PropModeReplace, |
| 4239 | (unsigned char *)clip.get_data(), |
| 4240 | clip.length()); |
| 4241 | return p_property; |
| 4242 | } else { |
| 4243 | char *target_name = XGetAtomName(x11_display, p_target); |
| 4244 | print_verbose(vformat("Target '%s' not supported.", target_name)); |
| 4245 | if (target_name) { |
| 4246 | XFree(target_name); |
| 4247 | } |
| 4248 | return None; |
| 4249 | } |
| 4250 | } |
| 4251 | |
| 4252 | void DisplayServerX11::_handle_selection_request_event(XSelectionRequestEvent *p_event) const { |
| 4253 | XEvent respond; |
| 4254 | if (p_event->target == XInternAtom(x11_display, "MULTIPLE", 0)) { |
| 4255 | // Request for multiple target conversions at once. |
| 4256 | Atom atom_pair = XInternAtom(x11_display, "ATOM_PAIR", False); |
| 4257 | respond.xselection.property = None; |
| 4258 | |
| 4259 | Atom type; |
| 4260 | int format; |
| 4261 | unsigned long len; |
| 4262 | unsigned long remaining; |
| 4263 | unsigned char *data = nullptr; |
| 4264 | if (XGetWindowProperty(x11_display, p_event->requestor, p_event->property, 0, LONG_MAX, False, atom_pair, &type, &format, &len, &remaining, &data) == Success) { |
| 4265 | if ((len >= 2) && data) { |
| 4266 | Atom *targets = (Atom *)data; |
| 4267 | for (uint64_t i = 0; i < len; i += 2) { |
| 4268 | Atom target = targets[i]; |
| 4269 | Atom &property = targets[i + 1]; |
| 4270 | property = _process_selection_request_target(target, p_event->requestor, property, p_event->selection); |
| 4271 | } |
| 4272 | |
| 4273 | XChangeProperty(x11_display, |
| 4274 | p_event->requestor, |
| 4275 | p_event->property, |
| 4276 | atom_pair, |
| 4277 | 32, |
| 4278 | PropModeReplace, |
| 4279 | (unsigned char *)targets, |
| 4280 | len); |
| 4281 | |
| 4282 | respond.xselection.property = p_event->property; |
| 4283 | } |
| 4284 | XFree(data); |
| 4285 | } |
| 4286 | } else { |
| 4287 | // Request for target conversion. |
| 4288 | respond.xselection.property = _process_selection_request_target(p_event->target, p_event->requestor, p_event->property, p_event->selection); |
| 4289 | } |
| 4290 | |
| 4291 | respond.xselection.type = SelectionNotify; |
| 4292 | respond.xselection.display = p_event->display; |
| 4293 | respond.xselection.requestor = p_event->requestor; |
| 4294 | respond.xselection.selection = p_event->selection; |
| 4295 | respond.xselection.target = p_event->target; |
| 4296 | respond.xselection.time = p_event->time; |
| 4297 | |
| 4298 | XSendEvent(x11_display, p_event->requestor, True, NoEventMask, &respond); |
| 4299 | XFlush(x11_display); |
| 4300 | } |
| 4301 | |
| 4302 | int DisplayServerX11::_xim_preedit_start_callback(::XIM xim, ::XPointer client_data, |
| 4303 | ::XPointer call_data) { |
| 4304 | DisplayServerX11 *ds = reinterpret_cast<DisplayServerX11 *>(client_data); |
| 4305 | WindowID window_id = ds->_get_focused_window_or_popup(); |
| 4306 | WindowData &wd = ds->windows[window_id]; |
| 4307 | if (wd.ime_active) { |
| 4308 | wd.ime_in_progress = true; |
| 4309 | } |
| 4310 | |
| 4311 | return -1; // Allow preedit strings of any length (no limit). |
| 4312 | } |
| 4313 | |
| 4314 | void DisplayServerX11::_xim_preedit_done_callback(::XIM xim, ::XPointer client_data, |
| 4315 | ::XPointer call_data) { |
| 4316 | DisplayServerX11 *ds = reinterpret_cast<DisplayServerX11 *>(client_data); |
| 4317 | WindowID window_id = ds->_get_focused_window_or_popup(); |
| 4318 | WindowData &wd = ds->windows[window_id]; |
| 4319 | if (wd.ime_active) { |
| 4320 | wd.ime_in_progress = false; |
| 4321 | wd.ime_suppress_next_keyup = true; |
| 4322 | } |
| 4323 | } |
| 4324 | |
| 4325 | void DisplayServerX11::_xim_preedit_draw_callback(::XIM xim, ::XPointer client_data, |
| 4326 | ::XIMPreeditDrawCallbackStruct *call_data) { |
| 4327 | DisplayServerX11 *ds = reinterpret_cast<DisplayServerX11 *>(client_data); |
| 4328 | WindowID window_id = ds->_get_focused_window_or_popup(); |
| 4329 | WindowData &wd = ds->windows[window_id]; |
| 4330 | |
| 4331 | XIMText *xim_text = call_data->text; |
| 4332 | if (wd.ime_active) { |
| 4333 | if (xim_text != nullptr) { |
| 4334 | String changed_text; |
| 4335 | if (xim_text->encoding_is_wchar) { |
| 4336 | changed_text = String(xim_text->string.wide_char); |
| 4337 | } else { |
| 4338 | changed_text.append_utf8(xim_text->string.multi_byte); |
| 4339 | } |
| 4340 | |
| 4341 | if (call_data->chg_length < 0) { |
| 4342 | ds->im_text = ds->im_text.substr(0, call_data->chg_first) + changed_text; |
| 4343 | } else { |
| 4344 | ds->im_text = ds->im_text.substr(0, call_data->chg_first) + changed_text + ds->im_text.substr(call_data->chg_length); |
| 4345 | } |
| 4346 | |
| 4347 | // Find the start and end of the selection. |
| 4348 | int start = 0, count = 0; |
| 4349 | for (int i = 0; i < xim_text->length; i++) { |
| 4350 | if (xim_text->feedback[i] & XIMReverse) { |
| 4351 | if (count == 0) { |
| 4352 | start = i; |
| 4353 | count = 1; |
| 4354 | } else { |
| 4355 | count++; |
| 4356 | } |
| 4357 | } |
| 4358 | } |
| 4359 | if (count > 0) { |
| 4360 | ds->im_selection = Point2i(start + call_data->chg_first, count); |
| 4361 | } else { |
| 4362 | ds->im_selection = Point2i(call_data->caret, 0); |
| 4363 | } |
| 4364 | } else { |
| 4365 | ds->im_text = String(); |
| 4366 | ds->im_selection = Point2i(); |
| 4367 | } |
| 4368 | |
| 4369 | callable_mp((Object *)OS_Unix::get_singleton()->get_main_loop(), &Object::notification).call_deferred(MainLoop::NOTIFICATION_OS_IME_UPDATE, false); |
| 4370 | } |
| 4371 | } |
| 4372 | |
| 4373 | void DisplayServerX11::_xim_preedit_caret_callback(::XIM xim, ::XPointer client_data, |
| 4374 | ::XIMPreeditCaretCallbackStruct *call_data) { |
| 4375 | } |
| 4376 | |
| 4377 | void DisplayServerX11::_xim_destroy_callback(::XIM im, ::XPointer client_data, |
| 4378 | ::XPointer call_data) { |
| 4379 | DisplayServerX11 *ds = reinterpret_cast<DisplayServerX11 *>(client_data); |
| 4380 | ds->xim = nullptr; |
| 4381 | |
| 4382 | for (KeyValue<WindowID, WindowData> &E : ds->windows) { |
| 4383 | E.value.xic = nullptr; |
| 4384 | } |
| 4385 | } |
| 4386 | |
| 4387 | void DisplayServerX11::_window_changed(XEvent *event) { |
| 4388 | WindowID window_id = MAIN_WINDOW_ID; |
| 4389 | |
| 4390 | // Assign the event to the relevant window |
| 4391 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 4392 | if (event->xany.window == E.value.x11_window) { |
| 4393 | window_id = E.key; |
| 4394 | break; |
| 4395 | } |
| 4396 | } |
| 4397 | |
| 4398 | Rect2i new_rect; |
| 4399 | |
| 4400 | WindowData &wd = windows[window_id]; |
| 4401 | if (wd.x11_window != event->xany.window) { // Check if the correct window, in case it was not main window or anything else |
| 4402 | return; |
| 4403 | } |
| 4404 | |
| 4405 | { |
| 4406 | //the position in xconfigure is not useful here, obtain it manually |
| 4407 | int x = 0, y = 0; |
| 4408 | Window child; |
| 4409 | XTranslateCoordinates(x11_display, wd.x11_window, DefaultRootWindow(x11_display), 0, 0, &x, &y, &child); |
| 4410 | new_rect.position.x = x; |
| 4411 | new_rect.position.y = y; |
| 4412 | |
| 4413 | new_rect.size.width = event->xconfigure.width; |
| 4414 | new_rect.size.height = event->xconfigure.height; |
| 4415 | } |
| 4416 | |
| 4417 | if (new_rect == Rect2i(wd.position, wd.size)) { |
| 4418 | return; |
| 4419 | } |
| 4420 | |
| 4421 | wd.position = new_rect.position; |
| 4422 | wd.size = new_rect.size; |
| 4423 | |
| 4424 | #if defined(RD_ENABLED) |
| 4425 | if (rendering_context) { |
| 4426 | rendering_context->window_set_size(window_id, wd.size.width, wd.size.height); |
| 4427 | } |
| 4428 | #endif |
| 4429 | #if defined(GLES3_ENABLED) |
| 4430 | if (gl_manager) { |
| 4431 | gl_manager->window_resize(window_id, wd.size.width, wd.size.height); |
| 4432 | } |
| 4433 | if (gl_manager_egl) { |
| 4434 | gl_manager_egl->window_resize(window_id, wd.size.width, wd.size.height); |
| 4435 | } |
| 4436 | #endif |
| 4437 | |
| 4438 | if (wd.rect_changed_callback.is_valid()) { |
| 4439 | wd.rect_changed_callback.call(new_rect); |
| 4440 | } |
| 4441 | } |
| 4442 | |
| 4443 | DisplayServer::WindowID DisplayServerX11::_get_focused_window_or_popup() const { |
| 4444 | const List<WindowID>::Element *E = popup_list.back(); |
| 4445 | if (E) { |
| 4446 | return E->get(); |
| 4447 | } |
| 4448 | |
| 4449 | return last_focused_window; |
| 4450 | } |
| 4451 | |
| 4452 | void DisplayServerX11::_dispatch_input_events(const Ref<InputEvent> &p_event) { |
| 4453 | static_cast<DisplayServerX11 *>(get_singleton())->_dispatch_input_event(p_event); |
| 4454 | } |
| 4455 | |
| 4456 | void DisplayServerX11::_dispatch_input_event(const Ref<InputEvent> &p_event) { |
| 4457 | { |
| 4458 | List<WindowID>::Element *E = popup_list.back(); |
| 4459 | if (E && Object::cast_to<InputEventKey>(*p_event)) { |
| 4460 | // Redirect keyboard input to active popup. |
| 4461 | if (windows.has(E->get())) { |
| 4462 | Callable callable = windows[E->get()].input_event_callback; |
| 4463 | if (callable.is_valid()) { |
| 4464 | callable.call(p_event); |
| 4465 | } |
| 4466 | } |
| 4467 | return; |
| 4468 | } |
| 4469 | } |
| 4470 | |
| 4471 | Ref<InputEventFromWindow> event_from_window = p_event; |
| 4472 | if (event_from_window.is_valid() && event_from_window->get_window_id() != INVALID_WINDOW_ID) { |
| 4473 | // Send to a single window. |
| 4474 | if (windows.has(event_from_window->get_window_id())) { |
| 4475 | Callable callable = windows[event_from_window->get_window_id()].input_event_callback; |
| 4476 | if (callable.is_valid()) { |
| 4477 | callable.call(p_event); |
| 4478 | } |
| 4479 | } |
| 4480 | } else { |
| 4481 | // Send to all windows. Copy all pending callbacks, since callback can erase window. |
| 4482 | Vector<Callable> cbs; |
| 4483 | for (KeyValue<WindowID, WindowData> &E : windows) { |
| 4484 | Callable callable = E.value.input_event_callback; |
| 4485 | if (callable.is_valid()) { |
| 4486 | cbs.push_back(callable); |
| 4487 | } |
| 4488 | } |
| 4489 | for (const Callable &cb : cbs) { |
| 4490 | cb.call(p_event); |
| 4491 | } |
| 4492 | } |
| 4493 | } |
| 4494 | |
| 4495 | void DisplayServerX11::_send_window_event(const WindowData &wd, WindowEvent p_event) { |
| 4496 | if (wd.event_callback.is_valid()) { |
| 4497 | Variant event = int(p_event); |
| 4498 | wd.event_callback.call(event); |
| 4499 | } |
| 4500 | } |
| 4501 | |
| 4502 | void DisplayServerX11::_set_input_focus(Window p_window, int p_revert_to) { |
| 4503 | Window focused_window; |
| 4504 | int focus_ret_state; |
| 4505 | XGetInputFocus(x11_display, &focused_window, &focus_ret_state); |
| 4506 | |
| 4507 | // Only attempt to change focus if the window isn't already focused, in order to |
| 4508 | // prevent issues with Godot stealing input focus with alternative window managers. |
| 4509 | if (p_window != focused_window) { |
| 4510 | XSetInputFocus(x11_display, p_window, p_revert_to, CurrentTime); |
| 4511 | } |
| 4512 | } |
| 4513 | |
| 4514 | void DisplayServerX11::_poll_events_thread(void *ud) { |
| 4515 | DisplayServerX11 *display_server = static_cast<DisplayServerX11 *>(ud); |
| 4516 | display_server->_poll_events(); |
| 4517 | } |
| 4518 | |
| 4519 | Bool DisplayServerX11::_predicate_all_events(Display *display, XEvent *event, XPointer arg) { |
| 4520 | // Just accept all events. |
| 4521 | return True; |
| 4522 | } |
| 4523 | |
| 4524 | bool DisplayServerX11::_wait_for_events(int timeout_seconds, int timeout_microseconds) const { |
| 4525 | int x11_fd = ConnectionNumber(x11_display); |
| 4526 | fd_set in_fds; |
| 4527 | |
| 4528 | XFlush(x11_display); |
| 4529 | |
| 4530 | FD_ZERO(&in_fds); |
| 4531 | FD_SET(x11_fd, &in_fds); |
| 4532 | |
| 4533 | struct timeval tv; |
| 4534 | tv.tv_sec = timeout_seconds; |
| 4535 | tv.tv_usec = timeout_microseconds; |
| 4536 | |
| 4537 | // Wait for next event or timeout. |
| 4538 | int num_ready_fds = select(x11_fd + 1, &in_fds, nullptr, nullptr, &tv); |
| 4539 | |
| 4540 | if (num_ready_fds > 0) { |
| 4541 | // Event received. |
| 4542 | return true; |
| 4543 | } else { |
| 4544 | // Error or timeout. |
| 4545 | if (num_ready_fds < 0) { |
| 4546 | ERR_PRINT("_wait_for_events: select error: " + itos(errno)); |
| 4547 | } |
| 4548 | return false; |
| 4549 | } |
| 4550 | } |
| 4551 | |
| 4552 | void DisplayServerX11::_poll_events() { |
| 4553 | while (!events_thread_done.is_set()) { |
| 4554 | // Wait with a shorter timeout from the events thread to avoid delayed inputs. |
| 4555 | _wait_for_events(0, 1000); |
| 4556 | |
| 4557 | // Process events from the queue. |
| 4558 | { |
| 4559 | MutexLock mutex_lock(events_mutex); |
| 4560 | |
| 4561 | _check_pending_events(polled_events); |
| 4562 | } |
| 4563 | } |
| 4564 | } |
| 4565 | |
| 4566 | void DisplayServerX11::_check_pending_events(LocalVector<XEvent> &r_events) { |
| 4567 | // Flush to make sure to gather all pending events. |
| 4568 | XFlush(x11_display); |
| 4569 | |
| 4570 | // Non-blocking wait for next event and remove it from the queue. |
| 4571 | XEvent ev = {}; |
| 4572 | while (XCheckIfEvent(x11_display, &ev, _predicate_all_events, nullptr)) { |
| 4573 | // Check if the input manager wants to process the event. |
| 4574 | if (XFilterEvent(&ev, None)) { |
| 4575 | // Event has been filtered by the Input Manager, |
| 4576 | // it has to be ignored and a new one will be received. |
| 4577 | continue; |
| 4578 | } |
| 4579 | |
| 4580 | // Handle selection request events directly in the event thread, because |
| 4581 | // communication through the x server takes several events sent back and forth |
| 4582 | // and we don't want to block other programs while processing only one each frame. |
| 4583 | if (ev.type == SelectionRequest) { |
| 4584 | _handle_selection_request_event(&(ev.xselectionrequest)); |
| 4585 | continue; |
| 4586 | } |
| 4587 | |
| 4588 | r_events.push_back(ev); |
| 4589 | } |
| 4590 | } |
| 4591 | |
| 4592 | DisplayServer::WindowID DisplayServerX11::window_get_active_popup() const { |
| 4593 | const List<WindowID>::Element *E = popup_list.back(); |
| 4594 | if (E) { |
| 4595 | return E->get(); |
| 4596 | } else { |
| 4597 | return INVALID_WINDOW_ID; |
| 4598 | } |
| 4599 | } |
| 4600 | |
| 4601 | void DisplayServerX11::window_set_popup_safe_rect(WindowID p_window, const Rect2i &p_rect) { |
| 4602 | _THREAD_SAFE_METHOD_ |
| 4603 | |
| 4604 | ERR_FAIL_COND(!windows.has(p_window)); |
| 4605 | WindowData &wd = windows[p_window]; |
| 4606 | wd.parent_safe_rect = p_rect; |
| 4607 | } |
| 4608 | |
| 4609 | Rect2i DisplayServerX11::window_get_popup_safe_rect(WindowID p_window) const { |
| 4610 | _THREAD_SAFE_METHOD_ |
| 4611 | |
| 4612 | ERR_FAIL_COND_V(!windows.has(p_window), Rect2i()); |
| 4613 | const WindowData &wd = windows[p_window]; |
| 4614 | return wd.parent_safe_rect; |
| 4615 | } |
| 4616 | |
| 4617 | void DisplayServerX11::popup_open(WindowID p_window) { |
| 4618 | _THREAD_SAFE_METHOD_ |
| 4619 | |
| 4620 | bool has_popup_ancestor = false; |
| 4621 | WindowID transient_root = p_window; |
| 4622 | while (true) { |
| 4623 | WindowID parent = windows[transient_root].transient_parent; |
| 4624 | if (parent == INVALID_WINDOW_ID) { |
| 4625 | break; |
| 4626 | } else { |
| 4627 | transient_root = parent; |
| 4628 | if (windows[parent].is_popup) { |
| 4629 | has_popup_ancestor = true; |
| 4630 | break; |
| 4631 | } |
| 4632 | } |
| 4633 | } |
| 4634 | |
| 4635 | // Detect tooltips and other similar popups that shouldn't block input to their parent. |
| 4636 | bool ignores_input = window_get_flag(WINDOW_FLAG_NO_FOCUS, p_window) && window_get_flag(WINDOW_FLAG_MOUSE_PASSTHROUGH, p_window); |
| 4637 | |
| 4638 | WindowData &wd = windows[p_window]; |
| 4639 | if (wd.is_popup || (has_popup_ancestor && !ignores_input)) { |
| 4640 | // Find current popup parent, or root popup if new window is not transient. |
| 4641 | List<WindowID>::Element *C = nullptr; |
| 4642 | List<WindowID>::Element *E = popup_list.back(); |
| 4643 | while (E) { |
| 4644 | if (wd.transient_parent != E->get() || wd.transient_parent == INVALID_WINDOW_ID) { |
| 4645 | C = E; |
| 4646 | E = E->prev(); |
| 4647 | } else { |
| 4648 | break; |
| 4649 | } |
| 4650 | } |
| 4651 | if (C) { |
| 4652 | _send_window_event(windows[C->get()], DisplayServerX11::WINDOW_EVENT_CLOSE_REQUEST); |
| 4653 | } |
| 4654 | |
| 4655 | time_since_popup = OS::get_singleton()->get_ticks_msec(); |
| 4656 | popup_list.push_back(p_window); |
| 4657 | } |
| 4658 | } |
| 4659 | |
| 4660 | void DisplayServerX11::popup_close(WindowID p_window) { |
| 4661 | _THREAD_SAFE_METHOD_ |
| 4662 | |
| 4663 | List<WindowID>::Element *E = popup_list.find(p_window); |
| 4664 | while (E) { |
| 4665 | List<WindowID>::Element *F = E->next(); |
| 4666 | WindowID win_id = E->get(); |
| 4667 | popup_list.erase(E); |
| 4668 | |
| 4669 | if (win_id != p_window) { |
| 4670 | // Only request close on related windows, not this window. We are already processing it. |
| 4671 | _send_window_event(windows[win_id], DisplayServerX11::WINDOW_EVENT_CLOSE_REQUEST); |
| 4672 | } |
| 4673 | E = F; |
| 4674 | } |
| 4675 | } |
| 4676 | |
| 4677 | bool DisplayServerX11::mouse_process_popups() { |
| 4678 | _THREAD_SAFE_METHOD_ |
| 4679 | |
| 4680 | if (popup_list.is_empty()) { |
| 4681 | return false; |
| 4682 | } |
| 4683 | |
| 4684 | uint64_t delta = OS::get_singleton()->get_ticks_msec() - time_since_popup; |
| 4685 | if (delta < 250) { |
| 4686 | return false; |
| 4687 | } |
| 4688 | |
| 4689 | int number_of_screens = XScreenCount(x11_display); |
| 4690 | bool closed = false; |
| 4691 | for (int i = 0; i < number_of_screens; i++) { |
| 4692 | Window root, child; |
| 4693 | int root_x, root_y, win_x, win_y; |
| 4694 | unsigned int mask; |
| 4695 | if (XQueryPointer(x11_display, XRootWindow(x11_display, i), &root, &child, &root_x, &root_y, &win_x, &win_y, &mask)) { |
| 4696 | XWindowAttributes root_attrs; |
| 4697 | XGetWindowAttributes(x11_display, root, &root_attrs); |
| 4698 | Vector2i pos = Vector2i(root_attrs.x + root_x, root_attrs.y + root_y); |
| 4699 | if (mask != last_mouse_monitor_mask) { |
| 4700 | if (((mask & Button1Mask) || (mask & Button2Mask) || (mask & Button3Mask) || (mask & Button4Mask) || (mask & Button5Mask))) { |
| 4701 | List<WindowID>::Element *C = nullptr; |
| 4702 | List<WindowID>::Element *E = popup_list.back(); |
| 4703 | // Find top popup to close. |
| 4704 | while (E) { |
| 4705 | // Popup window area. |
| 4706 | Rect2i win_rect = Rect2i(window_get_position_with_decorations(E->get()), window_get_size_with_decorations(E->get())); |
| 4707 | // Area of the parent window, which responsible for opening sub-menu. |
| 4708 | Rect2i safe_rect = window_get_popup_safe_rect(E->get()); |
| 4709 | if (win_rect.has_point(pos)) { |
| 4710 | break; |
| 4711 | } else if (safe_rect != Rect2i() && safe_rect.has_point(pos)) { |
| 4712 | break; |
| 4713 | } else { |
| 4714 | C = E; |
| 4715 | E = E->prev(); |
| 4716 | } |
| 4717 | } |
| 4718 | if (C) { |
| 4719 | _send_window_event(windows[C->get()], DisplayServerX11::WINDOW_EVENT_CLOSE_REQUEST); |
| 4720 | closed = true; |
| 4721 | } |
| 4722 | } |
| 4723 | } |
| 4724 | last_mouse_monitor_mask = mask; |
| 4725 | } |
| 4726 | } |
| 4727 | return closed; |
| 4728 | } |
| 4729 | |
| 4730 | bool DisplayServerX11::_window_focus_check() { |
| 4731 | Window focused_window; |
| 4732 | int focus_ret_state; |
| 4733 | XGetInputFocus(x11_display, &focused_window, &focus_ret_state); |
| 4734 | |
| 4735 | bool has_focus = false; |
| 4736 | for (const KeyValue<int, DisplayServerX11::WindowData> &wid : windows) { |
| 4737 | if (wid.value.x11_window == focused_window || (wid.value.xic && wid.value.ime_active && wid.value.x11_xim_window == focused_window)) { |
| 4738 | has_focus = true; |
| 4739 | break; |
| 4740 | } |
| 4741 | } |
| 4742 | |
| 4743 | return has_focus; |
| 4744 | } |
| 4745 | |
| 4746 | void DisplayServerX11::process_events() { |
| 4747 | ERR_FAIL_COND(!Thread::is_main_thread()); |
| 4748 | |
| 4749 | _THREAD_SAFE_LOCK_ |
| 4750 | |
| 4751 | #ifdef DISPLAY_SERVER_X11_DEBUG_LOGS_ENABLED |
| 4752 | static int frame = 0; |
| 4753 | ++frame; |
| 4754 | #endif |
| 4755 | |
| 4756 | bool ignore_events = mouse_process_popups(); |
| 4757 | |
| 4758 | if (app_focused) { |
| 4759 | //verify that one of the windows has focus, else send focus out notification |
| 4760 | bool focus_found = false; |
| 4761 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 4762 | if (E.value.focused) { |
| 4763 | focus_found = true; |
| 4764 | break; |
| 4765 | } |
| 4766 | } |
| 4767 | |
| 4768 | if (!focus_found) { |
| 4769 | uint64_t delta = OS::get_singleton()->get_ticks_msec() - time_since_no_focus; |
| 4770 | |
| 4771 | if (delta > 250) { |
| 4772 | //X11 can go between windows and have no focus for a while, when creating them or something else. Use this as safety to avoid unnecessary focus in/outs. |
| 4773 | if (OS::get_singleton()->get_main_loop()) { |
| 4774 | DEBUG_LOG_X11("All focus lost, triggering NOTIFICATION_APPLICATION_FOCUS_OUT\n"); |
| 4775 | OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT); |
| 4776 | } |
| 4777 | app_focused = false; |
| 4778 | } |
| 4779 | } else { |
| 4780 | time_since_no_focus = OS::get_singleton()->get_ticks_msec(); |
| 4781 | } |
| 4782 | } |
| 4783 | |
| 4784 | do_mouse_warp = false; |
| 4785 | |
| 4786 | // Is the current mouse mode one where it needs to be grabbed. |
| 4787 | bool mouse_mode_grab = mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED || mouse_mode == MOUSE_MODE_CONFINED_HIDDEN; |
| 4788 | |
| 4789 | xi.pressure = 0; |
| 4790 | xi.tilt = Vector2(); |
| 4791 | xi.pressure_supported = false; |
| 4792 | |
| 4793 | LocalVector<XEvent> events; |
| 4794 | { |
| 4795 | // Block events polling while flushing events. |
| 4796 | MutexLock mutex_lock(events_mutex); |
| 4797 | events = polled_events; |
| 4798 | polled_events.clear(); |
| 4799 | } |
| 4800 | |
| 4801 | for (uint32_t event_index = 0; event_index < events.size(); ++event_index) { |
| 4802 | XEvent &event = events[event_index]; |
| 4803 | |
| 4804 | bool ime_window_event = false; |
| 4805 | WindowID window_id = MAIN_WINDOW_ID; |
| 4806 | |
| 4807 | // Assign the event to the relevant window |
| 4808 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 4809 | if (event.xany.window == E.value.x11_window) { |
| 4810 | window_id = E.key; |
| 4811 | break; |
| 4812 | } |
| 4813 | if (event.xany.window == E.value.x11_xim_window) { |
| 4814 | window_id = E.key; |
| 4815 | ime_window_event = true; |
| 4816 | break; |
| 4817 | } |
| 4818 | } |
| 4819 | |
| 4820 | if (XGetEventData(x11_display, &event.xcookie)) { |
| 4821 | if (event.xcookie.type == GenericEvent && event.xcookie.extension == xi.opcode) { |
| 4822 | XIDeviceEvent *event_data = (XIDeviceEvent *)event.xcookie.data; |
| 4823 | switch (event_data->evtype) { |
| 4824 | case XI_HierarchyChanged: |
| 4825 | case XI_DeviceChanged: { |
| 4826 | _refresh_device_info(); |
| 4827 | } break; |
| 4828 | case XI_RawMotion: { |
| 4829 | if (ime_window_event || ignore_events) { |
| 4830 | break; |
| 4831 | } |
| 4832 | XIRawEvent *raw_event = (XIRawEvent *)event_data; |
| 4833 | int device_id = raw_event->sourceid; |
| 4834 | |
| 4835 | // Determine the axis used (called valuators in XInput for some forsaken reason) |
| 4836 | // Mask is a bitmask indicating which axes are involved. |
| 4837 | // We are interested in the values of axes 0 and 1. |
| 4838 | if (raw_event->valuators.mask_len <= 0) { |
| 4839 | break; |
| 4840 | } |
| 4841 | |
| 4842 | const double *values = raw_event->raw_values; |
| 4843 | |
| 4844 | double rel_x = 0.0; |
| 4845 | double rel_y = 0.0; |
| 4846 | |
| 4847 | if (XIMaskIsSet(raw_event->valuators.mask, VALUATOR_ABSX)) { |
| 4848 | rel_x = *values; |
| 4849 | values++; |
| 4850 | } |
| 4851 | |
| 4852 | if (XIMaskIsSet(raw_event->valuators.mask, VALUATOR_ABSY)) { |
| 4853 | rel_y = *values; |
| 4854 | values++; |
| 4855 | } |
| 4856 | |
| 4857 | if (XIMaskIsSet(raw_event->valuators.mask, VALUATOR_PRESSURE)) { |
| 4858 | HashMap<int, Vector2>::Iterator pen_pressure = xi.pen_pressure_range.find(device_id); |
| 4859 | if (pen_pressure) { |
| 4860 | Vector2 pen_pressure_range = pen_pressure->value; |
| 4861 | if (pen_pressure_range != Vector2()) { |
| 4862 | xi.pressure_supported = true; |
| 4863 | xi.pressure = (*values - pen_pressure_range[0]) / |
| 4864 | (pen_pressure_range[1] - pen_pressure_range[0]); |
| 4865 | } |
| 4866 | } |
| 4867 | |
| 4868 | values++; |
| 4869 | } |
| 4870 | |
| 4871 | if (XIMaskIsSet(raw_event->valuators.mask, VALUATOR_TILTX)) { |
| 4872 | HashMap<int, Vector2>::Iterator pen_tilt_x = xi.pen_tilt_x_range.find(device_id); |
| 4873 | if (pen_tilt_x) { |
| 4874 | Vector2 pen_tilt_x_range = pen_tilt_x->value; |
| 4875 | if (pen_tilt_x_range[0] != 0 && *values < 0) { |
| 4876 | xi.tilt.x = *values / -pen_tilt_x_range[0]; |
| 4877 | } else if (pen_tilt_x_range[1] != 0) { |
| 4878 | xi.tilt.x = *values / pen_tilt_x_range[1]; |
| 4879 | } |
| 4880 | } |
| 4881 | |
| 4882 | values++; |
| 4883 | } |
| 4884 | |
| 4885 | if (XIMaskIsSet(raw_event->valuators.mask, VALUATOR_TILTY)) { |
| 4886 | HashMap<int, Vector2>::Iterator pen_tilt_y = xi.pen_tilt_y_range.find(device_id); |
| 4887 | if (pen_tilt_y) { |
| 4888 | Vector2 pen_tilt_y_range = pen_tilt_y->value; |
| 4889 | if (pen_tilt_y_range[0] != 0 && *values < 0) { |
| 4890 | xi.tilt.y = *values / -pen_tilt_y_range[0]; |
| 4891 | } else if (pen_tilt_y_range[1] != 0) { |
| 4892 | xi.tilt.y = *values / pen_tilt_y_range[1]; |
| 4893 | } |
| 4894 | } |
| 4895 | |
| 4896 | values++; |
| 4897 | } |
| 4898 | |
| 4899 | HashMap<int, bool>::Iterator pen_inverted = xi.pen_inverted_devices.find(device_id); |
| 4900 | if (pen_inverted) { |
| 4901 | xi.pen_inverted = pen_inverted->value; |
| 4902 | } |
| 4903 | |
| 4904 | // https://bugs.freedesktop.org/show_bug.cgi?id=71609 |
| 4905 | // http://lists.libsdl.org/pipermail/commits-libsdl.org/2015-June/000282.html |
| 4906 | if (raw_event->time == xi.last_relative_time && rel_x == xi.relative_motion.x && rel_y == xi.relative_motion.y) { |
| 4907 | break; // Flush duplicate to avoid overly fast motion |
| 4908 | } |
| 4909 | |
| 4910 | xi.old_raw_pos.x = xi.raw_pos.x; |
| 4911 | xi.old_raw_pos.y = xi.raw_pos.y; |
| 4912 | xi.raw_pos.x = rel_x; |
| 4913 | xi.raw_pos.y = rel_y; |
| 4914 | |
| 4915 | HashMap<int, Vector2>::Iterator abs_info = xi.absolute_devices.find(device_id); |
| 4916 | |
| 4917 | if (abs_info) { |
| 4918 | // Absolute mode device |
| 4919 | Vector2 mult = abs_info->value; |
| 4920 | |
| 4921 | xi.relative_motion.x += (xi.raw_pos.x - xi.old_raw_pos.x) * mult.x; |
| 4922 | xi.relative_motion.y += (xi.raw_pos.y - xi.old_raw_pos.y) * mult.y; |
| 4923 | } else { |
| 4924 | // Relative mode device |
| 4925 | xi.relative_motion.x = xi.raw_pos.x; |
| 4926 | xi.relative_motion.y = xi.raw_pos.y; |
| 4927 | } |
| 4928 | |
| 4929 | xi.last_relative_time = raw_event->time; |
| 4930 | } break; |
| 4931 | #ifdef TOUCH_ENABLED |
| 4932 | case XI_TouchBegin: |
| 4933 | case XI_TouchEnd: { |
| 4934 | if (ime_window_event || ignore_events) { |
| 4935 | break; |
| 4936 | } |
| 4937 | bool is_begin = event_data->evtype == XI_TouchBegin; |
| 4938 | |
| 4939 | int index = event_data->detail; |
| 4940 | Vector2 pos = Vector2(event_data->event_x, event_data->event_y); |
| 4941 | |
| 4942 | Ref<InputEventScreenTouch> st; |
| 4943 | st.instantiate(); |
| 4944 | st->set_window_id(window_id); |
| 4945 | st->set_index(index); |
| 4946 | st->set_position(pos); |
| 4947 | st->set_pressed(is_begin); |
| 4948 | |
| 4949 | if (is_begin) { |
| 4950 | if (xi.state.has(index)) { // Defensive |
| 4951 | break; |
| 4952 | } |
| 4953 | xi.state[index] = pos; |
| 4954 | if (xi.state.size() == 1) { |
| 4955 | // X11 may send a motion event when a touch gesture begins, that would result |
| 4956 | // in a spurious mouse motion event being sent to Godot; remember it to be able to filter it out |
| 4957 | xi.mouse_pos_to_filter = pos; |
| 4958 | } |
| 4959 | Input::get_singleton()->parse_input_event(st); |
| 4960 | } else { |
| 4961 | if (!xi.state.has(index)) { // Defensive |
| 4962 | break; |
| 4963 | } |
| 4964 | xi.state.erase(index); |
| 4965 | Input::get_singleton()->parse_input_event(st); |
| 4966 | } |
| 4967 | } break; |
| 4968 | |
| 4969 | case XI_TouchUpdate: { |
| 4970 | if (ime_window_event || ignore_events) { |
| 4971 | break; |
| 4972 | } |
| 4973 | |
| 4974 | int index = event_data->detail; |
| 4975 | Vector2 pos = Vector2(event_data->event_x, event_data->event_y); |
| 4976 | |
| 4977 | HashMap<int, Vector2>::Iterator curr_pos_elem = xi.state.find(index); |
| 4978 | if (!curr_pos_elem) { // Defensive |
| 4979 | break; |
| 4980 | } |
| 4981 | |
| 4982 | if (curr_pos_elem->value != pos) { |
| 4983 | Ref<InputEventScreenDrag> sd; |
| 4984 | sd.instantiate(); |
| 4985 | sd->set_window_id(window_id); |
| 4986 | sd->set_index(index); |
| 4987 | sd->set_position(pos); |
| 4988 | sd->set_relative(pos - curr_pos_elem->value); |
| 4989 | sd->set_relative_screen_position(sd->get_relative()); |
| 4990 | Input::get_singleton()->parse_input_event(sd); |
| 4991 | |
| 4992 | curr_pos_elem->value = pos; |
| 4993 | } |
| 4994 | } break; |
| 4995 | #endif |
| 4996 | } |
| 4997 | } |
| 4998 | } |
| 4999 | XFreeEventData(x11_display, &event.xcookie); |
| 5000 | |
| 5001 | switch (event.type) { |
| 5002 | case MapNotify: { |
| 5003 | DEBUG_LOG_X11("[%u] MapNotify window=%lu (%u) \n", frame, event.xmap.window, window_id); |
| 5004 | if (ime_window_event) { |
| 5005 | break; |
| 5006 | } |
| 5007 | |
| 5008 | const WindowData &wd = windows[window_id]; |
| 5009 | |
| 5010 | XWindowAttributes xwa; |
| 5011 | XSync(x11_display, False); |
| 5012 | XGetWindowAttributes(x11_display, wd.x11_window, &xwa); |
| 5013 | |
| 5014 | XFlush(x11_display); |
| 5015 | |
| 5016 | // Set focus when menu window is started. |
| 5017 | // RevertToPointerRoot is used to make sure we don't lose all focus in case |
| 5018 | // a subwindow and its parent are both destroyed. |
| 5019 | if ((xwa.map_state == IsViewable) && !wd.no_focus && !wd.is_popup && _window_focus_check()) { |
| 5020 | _set_input_focus(wd.x11_window, RevertToPointerRoot); |
| 5021 | } |
| 5022 | |
| 5023 | // Have we failed to set fullscreen while the window was unmapped? |
| 5024 | _validate_fullscreen_on_map(window_id); |
| 5025 | |
| 5026 | // On KDE Plasma, when the parent window of an embedded process is restored after being minimized, |
| 5027 | // only the embedded window receives the Map notification, causing it to |
| 5028 | // appear without its parent. |
| 5029 | if (wd.embed_parent) { |
| 5030 | XMapWindow(x11_display, wd.embed_parent); |
| 5031 | } |
| 5032 | } break; |
| 5033 | |
| 5034 | case Expose: { |
| 5035 | DEBUG_LOG_X11("[%u] Expose window=%lu (%u), count='%u' \n", frame, event.xexpose.window, window_id, event.xexpose.count); |
| 5036 | if (ime_window_event) { |
| 5037 | break; |
| 5038 | } |
| 5039 | |
| 5040 | windows[window_id].fullscreen = _window_fullscreen_check(window_id); |
| 5041 | |
| 5042 | Main::force_redraw(); |
| 5043 | } break; |
| 5044 | |
| 5045 | case LeaveNotify: { |
| 5046 | DEBUG_LOG_X11("[%u] LeaveNotify window=%lu (%u), mode='%u' \n", frame, event.xcrossing.window, window_id, event.xcrossing.mode); |
| 5047 | if (ime_window_event) { |
| 5048 | break; |
| 5049 | } |
| 5050 | |
| 5051 | if (!mouse_mode_grab && window_mouseover_id == window_id) { |
| 5052 | window_mouseover_id = INVALID_WINDOW_ID; |
| 5053 | _send_window_event(windows[window_id], WINDOW_EVENT_MOUSE_EXIT); |
| 5054 | } |
| 5055 | |
| 5056 | } break; |
| 5057 | |
| 5058 | case EnterNotify: { |
| 5059 | DEBUG_LOG_X11("[%u] EnterNotify window=%lu (%u), mode='%u' \n", frame, event.xcrossing.window, window_id, event.xcrossing.mode); |
| 5060 | if (ime_window_event) { |
| 5061 | break; |
| 5062 | } |
| 5063 | |
| 5064 | if (!mouse_mode_grab && window_mouseover_id != window_id) { |
| 5065 | if (window_mouseover_id != INVALID_WINDOW_ID) { |
| 5066 | _send_window_event(windows[window_mouseover_id], WINDOW_EVENT_MOUSE_EXIT); |
| 5067 | } |
| 5068 | window_mouseover_id = window_id; |
| 5069 | _send_window_event(windows[window_id], WINDOW_EVENT_MOUSE_ENTER); |
| 5070 | } |
| 5071 | } break; |
| 5072 | |
| 5073 | case FocusIn: { |
| 5074 | DEBUG_LOG_X11("[%u] FocusIn window=%lu (%u), mode='%u' \n", frame, event.xfocus.window, window_id, event.xfocus.mode); |
| 5075 | if (ime_window_event || (event.xfocus.detail == NotifyInferior)) { |
| 5076 | break; |
| 5077 | } |
| 5078 | |
| 5079 | WindowData &wd = windows[window_id]; |
| 5080 | last_focused_window = window_id; |
| 5081 | wd.focused = true; |
| 5082 | |
| 5083 | // Keep track of focus order for overlapping windows. |
| 5084 | static unsigned int focus_order = 0; |
| 5085 | wd.focus_order = ++focus_order; |
| 5086 | |
| 5087 | #ifdef ACCESSKIT_ENABLED |
| 5088 | if (accessibility_driver) { |
| 5089 | accessibility_driver->accessibility_set_window_focused(window_id, true); |
| 5090 | } |
| 5091 | #endif |
| 5092 | _send_window_event(wd, WINDOW_EVENT_FOCUS_IN); |
| 5093 | |
| 5094 | if (mouse_mode_grab) { |
| 5095 | // Show and update the cursor if confined and the window regained focus. |
| 5096 | |
| 5097 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 5098 | if (mouse_mode == MOUSE_MODE_CONFINED) { |
| 5099 | XUndefineCursor(x11_display, E.value.x11_window); |
| 5100 | } else if (mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED_HIDDEN) { // Or re-hide it. |
| 5101 | XDefineCursor(x11_display, E.value.x11_window, null_cursor); |
| 5102 | } |
| 5103 | |
| 5104 | XGrabPointer( |
| 5105 | x11_display, E.value.x11_window, True, |
| 5106 | ButtonPressMask | ButtonReleaseMask | PointerMotionMask, |
| 5107 | GrabModeAsync, GrabModeAsync, E.value.x11_window, None, CurrentTime); |
| 5108 | } |
| 5109 | } |
| 5110 | #ifdef TOUCH_ENABLED |
| 5111 | // Grab touch devices to avoid OS gesture interference |
| 5112 | /*for (int i = 0; i < xi.touch_devices.size(); ++i) { |
| 5113 | XIGrabDevice(x11_display, xi.touch_devices[i], x11_window, CurrentTime, None, XIGrabModeAsync, XIGrabModeAsync, False, &xi.touch_event_mask); |
| 5114 | }*/ |
| 5115 | #endif |
| 5116 | |
| 5117 | if (!app_focused) { |
| 5118 | if (OS::get_singleton()->get_main_loop()) { |
| 5119 | OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN); |
| 5120 | } |
| 5121 | app_focused = true; |
| 5122 | } |
| 5123 | } break; |
| 5124 | |
| 5125 | case FocusOut: { |
| 5126 | DEBUG_LOG_X11("[%u] FocusOut window=%lu (%u), mode='%u' \n", frame, event.xfocus.window, window_id, event.xfocus.mode); |
| 5127 | WindowData &wd = windows[window_id]; |
| 5128 | if (ime_window_event || (event.xfocus.detail == NotifyInferior)) { |
| 5129 | break; |
| 5130 | } |
| 5131 | if (wd.ime_active) { |
| 5132 | MutexLock mutex_lock(events_mutex); |
| 5133 | XUnsetICFocus(wd.xic); |
| 5134 | XUnmapWindow(x11_display, wd.x11_xim_window); |
| 5135 | wd.ime_active = false; |
| 5136 | im_text = String(); |
| 5137 | im_selection = Vector2i(); |
| 5138 | OS_Unix::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_OS_IME_UPDATE); |
| 5139 | } |
| 5140 | wd.focused = false; |
| 5141 | |
| 5142 | Input::get_singleton()->release_pressed_events(); |
| 5143 | #ifdef ACCESSKIT_ENABLED |
| 5144 | if (accessibility_driver) { |
| 5145 | accessibility_driver->accessibility_set_window_focused(window_id, false); |
| 5146 | } |
| 5147 | #endif |
| 5148 | _send_window_event(wd, WINDOW_EVENT_FOCUS_OUT); |
| 5149 | |
| 5150 | if (mouse_mode_grab) { |
| 5151 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 5152 | //dear X11, I try, I really try, but you never work, you do whatever you want. |
| 5153 | if (mouse_mode == MOUSE_MODE_CAPTURED) { |
| 5154 | // Show the cursor if we're in captured mode so it doesn't look weird. |
| 5155 | XUndefineCursor(x11_display, E.value.x11_window); |
| 5156 | } |
| 5157 | } |
| 5158 | XUngrabPointer(x11_display, CurrentTime); |
| 5159 | } |
| 5160 | #ifdef TOUCH_ENABLED |
| 5161 | // Ungrab touch devices so input works as usual while we are unfocused |
| 5162 | /*for (int i = 0; i < xi.touch_devices.size(); ++i) { |
| 5163 | XIUngrabDevice(x11_display, xi.touch_devices[i], CurrentTime); |
| 5164 | }*/ |
| 5165 | |
| 5166 | // Release every pointer to avoid sticky points |
| 5167 | for (const KeyValue<int, Vector2> &E : xi.state) { |
| 5168 | Ref<InputEventScreenTouch> st; |
| 5169 | st.instantiate(); |
| 5170 | st->set_index(E.key); |
| 5171 | st->set_window_id(window_id); |
| 5172 | st->set_position(E.value); |
| 5173 | Input::get_singleton()->parse_input_event(st); |
| 5174 | } |
| 5175 | xi.state.clear(); |
| 5176 | #endif |
| 5177 | } break; |
| 5178 | |
| 5179 | case ConfigureNotify: { |
| 5180 | DEBUG_LOG_X11("[%u] ConfigureNotify window=%lu (%u), event=%lu, above=%lu, override_redirect=%u \n", frame, event.xconfigure.window, window_id, event.xconfigure.event, event.xconfigure.above, event.xconfigure.override_redirect); |
| 5181 | if (event.xconfigure.window == windows[window_id].x11_xim_window) { |
| 5182 | break; |
| 5183 | } |
| 5184 | |
| 5185 | _window_changed(&event); |
| 5186 | } break; |
| 5187 | |
| 5188 | case PropertyNotify: { |
| 5189 | if (event.xproperty.atom == XInternAtom(x11_display, "_NET_WM_STATE", False)) { |
| 5190 | _update_wm_state_hints(window_id); |
| 5191 | } |
| 5192 | } break; |
| 5193 | |
| 5194 | case ButtonPress: |
| 5195 | case ButtonRelease: { |
| 5196 | if (ime_window_event || ignore_events) { |
| 5197 | break; |
| 5198 | } |
| 5199 | /* exit in case of a mouse button press */ |
| 5200 | last_timestamp = event.xbutton.time; |
| 5201 | if (mouse_mode == MOUSE_MODE_CAPTURED) { |
| 5202 | event.xbutton.x = last_mouse_pos.x; |
| 5203 | event.xbutton.y = last_mouse_pos.y; |
| 5204 | } |
| 5205 | |
| 5206 | Ref<InputEventMouseButton> mb; |
| 5207 | mb.instantiate(); |
| 5208 | |
| 5209 | mb->set_window_id(window_id); |
| 5210 | _get_key_modifier_state(event.xbutton.state, mb); |
| 5211 | mb->set_button_index((MouseButton)event.xbutton.button); |
| 5212 | if (mb->get_button_index() == MouseButton::RIGHT) { |
| 5213 | mb->set_button_index(MouseButton::MIDDLE); |
| 5214 | } else if (mb->get_button_index() == MouseButton::MIDDLE) { |
| 5215 | mb->set_button_index(MouseButton::RIGHT); |
| 5216 | } |
| 5217 | mb->set_position(Vector2(event.xbutton.x, event.xbutton.y)); |
| 5218 | mb->set_global_position(mb->get_position()); |
| 5219 | |
| 5220 | mb->set_pressed((event.type == ButtonPress)); |
| 5221 | |
| 5222 | if (mb->is_pressed() && mb->get_button_index() >= MouseButton::WHEEL_UP && mb->get_button_index() <= MouseButton::WHEEL_RIGHT) { |
| 5223 | MouseButtonMask mask = mouse_button_to_mask(mb->get_button_index()); |
| 5224 | BitField<MouseButtonMask> scroll_mask = mouse_get_button_state(); |
| 5225 | scroll_mask.set_flag(mask); |
| 5226 | mb->set_button_mask(scroll_mask); |
| 5227 | } else { |
| 5228 | mb->set_button_mask(mouse_get_button_state()); |
| 5229 | } |
| 5230 | |
| 5231 | const WindowData &wd = windows[window_id]; |
| 5232 | |
| 5233 | if (event.type == ButtonPress) { |
| 5234 | DEBUG_LOG_X11("[%u] ButtonPress window=%lu (%u), button_index=%u \n", frame, event.xbutton.window, window_id, mb->get_button_index()); |
| 5235 | |
| 5236 | // Ensure window focus on click. |
| 5237 | // RevertToPointerRoot is used to make sure we don't lose all focus in case |
| 5238 | // a subwindow and its parent are both destroyed. |
| 5239 | if (!wd.no_focus && !wd.is_popup) { |
| 5240 | _set_input_focus(wd.x11_window, RevertToPointerRoot); |
| 5241 | } |
| 5242 | |
| 5243 | uint64_t diff = OS::get_singleton()->get_ticks_usec() / 1000 - last_click_ms; |
| 5244 | |
| 5245 | if (mb->get_button_index() == last_click_button_index) { |
| 5246 | if (diff < 400 && Vector2(last_click_pos).distance_to(Vector2(event.xbutton.x, event.xbutton.y)) < 5) { |
| 5247 | last_click_ms = 0; |
| 5248 | last_click_pos = Point2i(-100, -100); |
| 5249 | last_click_button_index = MouseButton::NONE; |
| 5250 | mb->set_double_click(true); |
| 5251 | } |
| 5252 | |
| 5253 | } else if (mb->get_button_index() < MouseButton::WHEEL_UP || mb->get_button_index() > MouseButton::WHEEL_RIGHT) { |
| 5254 | last_click_button_index = mb->get_button_index(); |
| 5255 | } |
| 5256 | |
| 5257 | if (!mb->is_double_click()) { |
| 5258 | last_click_ms += diff; |
| 5259 | last_click_pos = Point2i(event.xbutton.x, event.xbutton.y); |
| 5260 | } |
| 5261 | } else { |
| 5262 | DEBUG_LOG_X11("[%u] ButtonRelease window=%lu (%u), button_index=%u \n", frame, event.xbutton.window, window_id, mb->get_button_index()); |
| 5263 | |
| 5264 | WindowID window_id_other = INVALID_WINDOW_ID; |
| 5265 | Window wd_other_x11_window; |
| 5266 | if (!wd.focused) { |
| 5267 | // Propagate the event to the focused window, |
| 5268 | // because it's received only on the topmost window. |
| 5269 | // Note: This is needed for drag & drop to work between windows, |
| 5270 | // because the engine expects events to keep being processed |
| 5271 | // on the same window dragging started. |
| 5272 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 5273 | if (E.value.focused) { |
| 5274 | if (E.key != window_id) { |
| 5275 | window_id_other = E.key; |
| 5276 | wd_other_x11_window = E.value.x11_window; |
| 5277 | } |
| 5278 | break; |
| 5279 | } |
| 5280 | } |
| 5281 | } |
| 5282 | |
| 5283 | if (window_id_other != INVALID_WINDOW_ID) { |
| 5284 | int x, y; |
| 5285 | Window child; |
| 5286 | XTranslateCoordinates(x11_display, wd.x11_window, wd_other_x11_window, event.xbutton.x, event.xbutton.y, &x, &y, &child); |
| 5287 | |
| 5288 | mb->set_window_id(window_id_other); |
| 5289 | mb->set_position(Vector2(x, y)); |
| 5290 | mb->set_global_position(mb->get_position()); |
| 5291 | } |
| 5292 | } |
| 5293 | |
| 5294 | Input::get_singleton()->parse_input_event(mb); |
| 5295 | |
| 5296 | } break; |
| 5297 | case MotionNotify: { |
| 5298 | if (ime_window_event || ignore_events) { |
| 5299 | break; |
| 5300 | } |
| 5301 | // The X11 API requires filtering one-by-one through the motion |
| 5302 | // notify events, in order to figure out which event is the one |
| 5303 | // generated by warping the mouse pointer. |
| 5304 | WindowID focused_window_id = _get_focused_window_or_popup(); |
| 5305 | if (!windows.has(focused_window_id)) { |
| 5306 | focused_window_id = MAIN_WINDOW_ID; |
| 5307 | } |
| 5308 | |
| 5309 | while (true) { |
| 5310 | if (mouse_mode == MOUSE_MODE_CAPTURED && event.xmotion.x == windows[focused_window_id].size.width / 2 && event.xmotion.y == windows[focused_window_id].size.height / 2) { |
| 5311 | //this is likely the warp event since it was warped here |
| 5312 | center = Vector2(event.xmotion.x, event.xmotion.y); |
| 5313 | break; |
| 5314 | } |
| 5315 | |
| 5316 | if (event_index + 1 < events.size()) { |
| 5317 | const XEvent &next_event = events[event_index + 1]; |
| 5318 | if (next_event.type == MotionNotify) { |
| 5319 | ++event_index; |
| 5320 | event = next_event; |
| 5321 | } else { |
| 5322 | break; |
| 5323 | } |
| 5324 | } else { |
| 5325 | break; |
| 5326 | } |
| 5327 | } |
| 5328 | |
| 5329 | last_timestamp = event.xmotion.time; |
| 5330 | |
| 5331 | // Motion is also simple. |
| 5332 | // A little hack is in order |
| 5333 | // to be able to send relative motion events. |
| 5334 | Point2i pos(event.xmotion.x, event.xmotion.y); |
| 5335 | |
| 5336 | // Avoidance of spurious mouse motion (see handling of touch) |
| 5337 | bool filter = false; |
| 5338 | // Adding some tolerance to match better Point2i to Vector2 |
| 5339 | if (xi.state.size() && Vector2(pos).distance_squared_to(xi.mouse_pos_to_filter) < 2) { |
| 5340 | filter = true; |
| 5341 | } |
| 5342 | // Invalidate to avoid filtering a possible legitimate similar event coming later |
| 5343 | xi.mouse_pos_to_filter = Vector2(1e10, 1e10); |
| 5344 | if (filter) { |
| 5345 | break; |
| 5346 | } |
| 5347 | |
| 5348 | const WindowData &wd = windows[window_id]; |
| 5349 | bool focused = wd.focused; |
| 5350 | |
| 5351 | if (mouse_mode == MOUSE_MODE_CAPTURED) { |
| 5352 | if (xi.relative_motion.x == 0 && xi.relative_motion.y == 0) { |
| 5353 | break; |
| 5354 | } |
| 5355 | |
| 5356 | Point2i new_center = pos; |
| 5357 | pos = last_mouse_pos + xi.relative_motion; |
| 5358 | center = new_center; |
| 5359 | do_mouse_warp = focused; // warp the cursor if we're focused in |
| 5360 | } |
| 5361 | |
| 5362 | if (!last_mouse_pos_valid) { |
| 5363 | last_mouse_pos = pos; |
| 5364 | last_mouse_pos_valid = true; |
| 5365 | } |
| 5366 | |
| 5367 | // Hackish but relative mouse motion is already handled in the RawMotion event. |
| 5368 | // RawMotion does not provide the absolute mouse position (whereas MotionNotify does). |
| 5369 | // Therefore, RawMotion cannot be the authority on absolute mouse position. |
| 5370 | // RawMotion provides more precision than MotionNotify, which doesn't sense subpixel motion. |
| 5371 | // Therefore, MotionNotify cannot be the authority on relative mouse motion. |
| 5372 | // This means we need to take a combined approach... |
| 5373 | Point2i rel; |
| 5374 | |
| 5375 | // Only use raw input if in capture mode. Otherwise use the classic behavior. |
| 5376 | if (mouse_mode == MOUSE_MODE_CAPTURED) { |
| 5377 | rel = xi.relative_motion; |
| 5378 | } else { |
| 5379 | rel = pos - last_mouse_pos; |
| 5380 | } |
| 5381 | |
| 5382 | // Reset to prevent lingering motion |
| 5383 | xi.relative_motion.x = 0; |
| 5384 | xi.relative_motion.y = 0; |
| 5385 | if (mouse_mode == MOUSE_MODE_CAPTURED) { |
| 5386 | pos = Point2i(windows[focused_window_id].size.width / 2, windows[focused_window_id].size.height / 2); |
| 5387 | } |
| 5388 | |
| 5389 | BitField<MouseButtonMask> last_button_state = MouseButtonMask::NONE; |
| 5390 | if (event.xmotion.state & Button1Mask) { |
| 5391 | last_button_state.set_flag(MouseButtonMask::LEFT); |
| 5392 | } |
| 5393 | if (event.xmotion.state & Button2Mask) { |
| 5394 | last_button_state.set_flag(MouseButtonMask::MIDDLE); |
| 5395 | } |
| 5396 | if (event.xmotion.state & Button3Mask) { |
| 5397 | last_button_state.set_flag(MouseButtonMask::RIGHT); |
| 5398 | } |
| 5399 | if (event.xmotion.state & Button4Mask) { |
| 5400 | last_button_state.set_flag(MouseButtonMask::MB_XBUTTON1); |
| 5401 | } |
| 5402 | if (event.xmotion.state & Button5Mask) { |
| 5403 | last_button_state.set_flag(MouseButtonMask::MB_XBUTTON2); |
| 5404 | } |
| 5405 | |
| 5406 | Ref<InputEventMouseMotion> mm; |
| 5407 | mm.instantiate(); |
| 5408 | |
| 5409 | mm->set_window_id(window_id); |
| 5410 | if (xi.pressure_supported) { |
| 5411 | mm->set_pressure(xi.pressure); |
| 5412 | } else { |
| 5413 | mm->set_pressure(bool(last_button_state.has_flag(MouseButtonMask::LEFT)) ? 1.0f : 0.0f); |
| 5414 | } |
| 5415 | mm->set_tilt(xi.tilt); |
| 5416 | mm->set_pen_inverted(xi.pen_inverted); |
| 5417 | |
| 5418 | _get_key_modifier_state(event.xmotion.state, mm); |
| 5419 | mm->set_button_mask(last_button_state); |
| 5420 | mm->set_position(pos); |
| 5421 | mm->set_global_position(pos); |
| 5422 | mm->set_velocity(Input::get_singleton()->get_last_mouse_velocity()); |
| 5423 | mm->set_screen_velocity(mm->get_velocity()); |
| 5424 | |
| 5425 | mm->set_relative(rel); |
| 5426 | mm->set_relative_screen_position(rel); |
| 5427 | |
| 5428 | last_mouse_pos = pos; |
| 5429 | |
| 5430 | // printf("rel: %d,%d\n", rel.x, rel.y ); |
| 5431 | // Don't propagate the motion event unless we have focus |
| 5432 | // this is so that the relative motion doesn't get messed up |
| 5433 | // after we regain focus. |
| 5434 | // Adjusted to parse the input event if the window is not focused allowing mouse hovering on the editor |
| 5435 | // the embedding process has focus. |
| 5436 | if (!focused) { |
| 5437 | // Propagate the event to the focused window, |
| 5438 | // because it's received only on the topmost window. |
| 5439 | // Note: This is needed for drag & drop to work between windows, |
| 5440 | // because the engine expects events to keep being processed |
| 5441 | // on the same window dragging started. |
| 5442 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 5443 | const WindowData &wd_other = E.value; |
| 5444 | if (wd_other.focused) { |
| 5445 | int x, y; |
| 5446 | Window child; |
| 5447 | XTranslateCoordinates(x11_display, wd.x11_window, wd_other.x11_window, event.xmotion.x, event.xmotion.y, &x, &y, &child); |
| 5448 | |
| 5449 | Point2i pos_focused(x, y); |
| 5450 | |
| 5451 | mm->set_window_id(E.key); |
| 5452 | mm->set_position(pos_focused); |
| 5453 | mm->set_global_position(pos_focused); |
| 5454 | mm->set_velocity(Input::get_singleton()->get_last_mouse_velocity()); |
| 5455 | |
| 5456 | break; |
| 5457 | } |
| 5458 | } |
| 5459 | } |
| 5460 | |
| 5461 | Input::get_singleton()->parse_input_event(mm); |
| 5462 | |
| 5463 | } break; |
| 5464 | case KeyPress: |
| 5465 | case KeyRelease: { |
| 5466 | if (ignore_events) { |
| 5467 | break; |
| 5468 | } |
| 5469 | #ifdef DISPLAY_SERVER_X11_DEBUG_LOGS_ENABLED |
| 5470 | if (event.type == KeyPress) { |
| 5471 | DEBUG_LOG_X11("[%u] KeyPress window=%lu (%u), keycode=%u, time=%lu \n", frame, event.xkey.window, window_id, event.xkey.keycode, event.xkey.time); |
| 5472 | } else { |
| 5473 | DEBUG_LOG_X11("[%u] KeyRelease window=%lu (%u), keycode=%u, time=%lu \n", frame, event.xkey.window, window_id, event.xkey.keycode, event.xkey.time); |
| 5474 | } |
| 5475 | #endif |
| 5476 | last_timestamp = event.xkey.time; |
| 5477 | |
| 5478 | // key event is a little complex, so |
| 5479 | // it will be handled in its own function. |
| 5480 | _handle_key_event(window_id, &event.xkey, events, event_index); |
| 5481 | } break; |
| 5482 | |
| 5483 | case SelectionNotify: |
| 5484 | if (ime_window_event) { |
| 5485 | break; |
| 5486 | } |
| 5487 | if (event.xselection.target == requested) { |
| 5488 | Property p = _read_property(x11_display, windows[window_id].x11_window, XInternAtom(x11_display, "PRIMARY", 0)); |
| 5489 | |
| 5490 | Vector<String> files = String((char *)p.data).split("\r\n", false); |
| 5491 | XFree(p.data); |
| 5492 | for (int i = 0; i < files.size(); i++) { |
| 5493 | files.write[i] = files[i].replace("file://", "").uri_file_decode(); |
| 5494 | } |
| 5495 | |
| 5496 | if (windows[window_id].drop_files_callback.is_valid()) { |
| 5497 | Variant v_files = files; |
| 5498 | const Variant *v_args[1] = { &v_files }; |
| 5499 | Variant ret; |
| 5500 | Callable::CallError ce; |
| 5501 | windows[window_id].drop_files_callback.callp((const Variant **)&v_args, 1, ret, ce); |
| 5502 | if (ce.error != Callable::CallError::CALL_OK) { |
| 5503 | ERR_PRINT(vformat("Failed to execute drop files callback: %s.", Variant::get_callable_error_text(windows[window_id].drop_files_callback, v_args, 1, ce))); |
| 5504 | } |
| 5505 | } |
| 5506 | |
| 5507 | //Reply that all is well. |
| 5508 | XClientMessageEvent m; |
| 5509 | memset(&m, 0, sizeof(m)); |
| 5510 | m.type = ClientMessage; |
| 5511 | m.display = x11_display; |
| 5512 | m.window = xdnd_source_window; |
| 5513 | m.message_type = xdnd_finished; |
| 5514 | m.format = 32; |
| 5515 | m.data.l[0] = windows[window_id].x11_window; |
| 5516 | m.data.l[1] = 1; |
| 5517 | m.data.l[2] = xdnd_action_copy; //We only ever copy. |
| 5518 | |
| 5519 | XSendEvent(x11_display, xdnd_source_window, False, NoEventMask, (XEvent *)&m); |
| 5520 | } |
| 5521 | break; |
| 5522 | |
| 5523 | case ClientMessage: |
| 5524 | if (ime_window_event) { |
| 5525 | break; |
| 5526 | } |
| 5527 | if ((unsigned int)event.xclient.data.l[0] == (unsigned int)wm_delete) { |
| 5528 | _send_window_event(windows[window_id], WINDOW_EVENT_CLOSE_REQUEST); |
| 5529 | } |
| 5530 | |
| 5531 | else if ((unsigned int)event.xclient.message_type == (unsigned int)xdnd_enter) { |
| 5532 | //File(s) have been dragged over the window, check for supported target (text/uri-list) |
| 5533 | xdnd_version = (event.xclient.data.l[1] >> 24); |
| 5534 | Window source = event.xclient.data.l[0]; |
| 5535 | bool more_than_3 = event.xclient.data.l[1] & 1; |
| 5536 | if (more_than_3) { |
| 5537 | Property p = _read_property(x11_display, source, XInternAtom(x11_display, "XdndTypeList", False)); |
| 5538 | requested = pick_target_from_list(x11_display, (Atom *)p.data, p.nitems); |
| 5539 | XFree(p.data); |
| 5540 | } else { |
| 5541 | requested = pick_target_from_atoms(x11_display, event.xclient.data.l[2], event.xclient.data.l[3], event.xclient.data.l[4]); |
| 5542 | } |
| 5543 | } else if ((unsigned int)event.xclient.message_type == (unsigned int)xdnd_position) { |
| 5544 | //xdnd position event, reply with an XDND status message |
| 5545 | //just depending on type of data for now |
| 5546 | XClientMessageEvent m; |
| 5547 | memset(&m, 0, sizeof(m)); |
| 5548 | m.type = ClientMessage; |
| 5549 | m.display = event.xclient.display; |
| 5550 | m.window = event.xclient.data.l[0]; |
| 5551 | m.message_type = xdnd_status; |
| 5552 | m.format = 32; |
| 5553 | m.data.l[0] = windows[window_id].x11_window; |
| 5554 | m.data.l[1] = (requested != None); |
| 5555 | m.data.l[2] = 0; //empty rectangle |
| 5556 | m.data.l[3] = 0; |
| 5557 | m.data.l[4] = xdnd_action_copy; |
| 5558 | |
| 5559 | XSendEvent(x11_display, event.xclient.data.l[0], False, NoEventMask, (XEvent *)&m); |
| 5560 | XFlush(x11_display); |
| 5561 | } else if ((unsigned int)event.xclient.message_type == (unsigned int)xdnd_drop) { |
| 5562 | if (requested != None) { |
| 5563 | xdnd_source_window = event.xclient.data.l[0]; |
| 5564 | if (xdnd_version >= 1) { |
| 5565 | XConvertSelection(x11_display, xdnd_selection, requested, XInternAtom(x11_display, "PRIMARY", 0), windows[window_id].x11_window, event.xclient.data.l[2]); |
| 5566 | } else { |
| 5567 | XConvertSelection(x11_display, xdnd_selection, requested, XInternAtom(x11_display, "PRIMARY", 0), windows[window_id].x11_window, CurrentTime); |
| 5568 | } |
| 5569 | } else { |
| 5570 | //Reply that we're not interested. |
| 5571 | XClientMessageEvent m; |
| 5572 | memset(&m, 0, sizeof(m)); |
| 5573 | m.type = ClientMessage; |
| 5574 | m.display = event.xclient.display; |
| 5575 | m.window = event.xclient.data.l[0]; |
| 5576 | m.message_type = xdnd_finished; |
| 5577 | m.format = 32; |
| 5578 | m.data.l[0] = windows[window_id].x11_window; |
| 5579 | m.data.l[1] = 0; |
| 5580 | m.data.l[2] = None; //Failed. |
| 5581 | XSendEvent(x11_display, event.xclient.data.l[0], False, NoEventMask, (XEvent *)&m); |
| 5582 | } |
| 5583 | } |
| 5584 | break; |
| 5585 | default: |
| 5586 | break; |
| 5587 | } |
| 5588 | } |
| 5589 | |
| 5590 | XFlush(x11_display); |
| 5591 | |
| 5592 | if (do_mouse_warp) { |
| 5593 | XWarpPointer(x11_display, None, windows[MAIN_WINDOW_ID].x11_window, |
| 5594 | 0, 0, 0, 0, (int)windows[MAIN_WINDOW_ID].size.width / 2, (int)windows[MAIN_WINDOW_ID].size.height / 2); |
| 5595 | |
| 5596 | /* |
| 5597 | Window root, child; |
| 5598 | int root_x, root_y; |
| 5599 | int win_x, win_y; |
| 5600 | unsigned int mask; |
| 5601 | XQueryPointer( x11_display, x11_window, &root, &child, &root_x, &root_y, &win_x, &win_y, &mask ); |
| 5602 | |
| 5603 | printf("Root: %d,%d\n", root_x, root_y); |
| 5604 | printf("Win: %d,%d\n", win_x, win_y); |
| 5605 | */ |
| 5606 | } |
| 5607 | |
| 5608 | #ifdef DBUS_ENABLED |
| 5609 | if (portal_desktop) { |
| 5610 | portal_desktop->process_callbacks(); |
| 5611 | } |
| 5612 | #endif |
| 5613 | |
| 5614 | _THREAD_SAFE_UNLOCK_ |
| 5615 | |
| 5616 | Input::get_singleton()->flush_buffered_events(); |
| 5617 | } |
| 5618 | |
| 5619 | void DisplayServerX11::release_rendering_thread() { |
| 5620 | #if defined(GLES3_ENABLED) |
| 5621 | if (gl_manager) { |
| 5622 | gl_manager->release_current(); |
| 5623 | } |
| 5624 | if (gl_manager_egl) { |
| 5625 | gl_manager_egl->release_current(); |
| 5626 | } |
| 5627 | #endif |
| 5628 | } |
| 5629 | |
| 5630 | void DisplayServerX11::swap_buffers() { |
| 5631 | #if defined(GLES3_ENABLED) |
| 5632 | if (gl_manager) { |
| 5633 | gl_manager->swap_buffers(); |
| 5634 | } |
| 5635 | if (gl_manager_egl) { |
| 5636 | gl_manager_egl->swap_buffers(); |
| 5637 | } |
| 5638 | #endif |
| 5639 | } |
| 5640 | |
| 5641 | void DisplayServerX11::_update_context(WindowData &wd) { |
| 5642 | XClassHint *classHint = XAllocClassHint(); |
| 5643 | |
| 5644 | if (classHint) { |
| 5645 | CharString name_str; |
| 5646 | switch (context) { |
| 5647 | case CONTEXT_EDITOR: |
| 5648 | name_str = "Godot_Editor"; |
| 5649 | break; |
| 5650 | case CONTEXT_PROJECTMAN: |
| 5651 | name_str = "Godot_ProjectList"; |
| 5652 | break; |
| 5653 | case CONTEXT_ENGINE: |
| 5654 | name_str = "Godot_Engine"; |
| 5655 | break; |
| 5656 | } |
| 5657 | |
| 5658 | CharString class_str; |
| 5659 | if (context == CONTEXT_ENGINE) { |
| 5660 | String config_name = GLOBAL_GET("application/config/name"); |
| 5661 | if (config_name.length() == 0) { |
| 5662 | class_str = "Godot_Engine"; |
| 5663 | } else { |
| 5664 | class_str = config_name.utf8(); |
| 5665 | } |
| 5666 | } else { |
| 5667 | class_str = "Godot"; |
| 5668 | } |
| 5669 | |
| 5670 | classHint->res_class = class_str.ptrw(); |
| 5671 | classHint->res_name = name_str.ptrw(); |
| 5672 | |
| 5673 | XSetClassHint(x11_display, wd.x11_window, classHint); |
| 5674 | XFree(classHint); |
| 5675 | } |
| 5676 | } |
| 5677 | |
| 5678 | void DisplayServerX11::set_context(Context p_context) { |
| 5679 | _THREAD_SAFE_METHOD_ |
| 5680 | |
| 5681 | context = p_context; |
| 5682 | |
| 5683 | for (KeyValue<WindowID, WindowData> &E : windows) { |
| 5684 | _update_context(E.value); |
| 5685 | } |
| 5686 | } |
| 5687 | |
| 5688 | bool DisplayServerX11::is_window_transparency_available() const { |
| 5689 | CharString net_wm_cm_name = vformat("_NET_WM_CM_S%d", XDefaultScreen(x11_display)).ascii(); |
| 5690 | Atom net_wm_cm = XInternAtom(x11_display, net_wm_cm_name.get_data(), False); |
| 5691 | if (net_wm_cm == None) { |
| 5692 | return false; |
| 5693 | } |
| 5694 | if (XGetSelectionOwner(x11_display, net_wm_cm) == None) { |
| 5695 | return false; |
| 5696 | } |
| 5697 | #if defined(RD_ENABLED) |
| 5698 | if (rendering_device && !rendering_device->is_composite_alpha_supported()) { |
| 5699 | return false; |
| 5700 | } |
| 5701 | #endif |
| 5702 | return OS::get_singleton()->is_layered_allowed(); |
| 5703 | } |
| 5704 | |
| 5705 | void DisplayServerX11::set_native_icon(const String &p_filename) { |
| 5706 | WARN_PRINT("Native icon not supported by this display server."); |
| 5707 | } |
| 5708 | |
| 5709 | bool g_set_icon_error = false; |
| 5710 | int set_icon_errorhandler(Display *dpy, XErrorEvent *ev) { |
| 5711 | g_set_icon_error = true; |
| 5712 | return 0; |
| 5713 | } |
| 5714 | |
| 5715 | void DisplayServerX11::set_icon(const Ref<Image> &p_icon) { |
| 5716 | _THREAD_SAFE_METHOD_ |
| 5717 | |
| 5718 | WindowData &wd = windows[MAIN_WINDOW_ID]; |
| 5719 | |
| 5720 | int (*oldHandler)(Display *, XErrorEvent *) = XSetErrorHandler(&set_icon_errorhandler); |
| 5721 | |
| 5722 | Atom net_wm_icon = XInternAtom(x11_display, "_NET_WM_ICON", False); |
| 5723 | |
| 5724 | if (p_icon.is_valid()) { |
| 5725 | ERR_FAIL_COND(p_icon->get_width() <= 0 || p_icon->get_height() <= 0); |
| 5726 | |
| 5727 | Ref<Image> img = p_icon->duplicate(); |
| 5728 | img->convert(Image::FORMAT_RGBA8); |
| 5729 | |
| 5730 | while (true) { |
| 5731 | int w = img->get_width(); |
| 5732 | int h = img->get_height(); |
| 5733 | |
| 5734 | if (g_set_icon_error) { |
| 5735 | g_set_icon_error = false; |
| 5736 | |
| 5737 | WARN_PRINT(vformat("Icon too large (%dx%d), attempting to downscale icon.", w, h)); |
| 5738 | |
| 5739 | int new_width, new_height; |
| 5740 | if (w > h) { |
| 5741 | new_width = w / 2; |
| 5742 | new_height = h * new_width / w; |
| 5743 | } else { |
| 5744 | new_height = h / 2; |
| 5745 | new_width = w * new_height / h; |
| 5746 | } |
| 5747 | |
| 5748 | w = new_width; |
| 5749 | h = new_height; |
| 5750 | |
| 5751 | if (!w || !h) { |
| 5752 | WARN_PRINT("Unable to set icon."); |
| 5753 | break; |
| 5754 | } |
| 5755 | |
| 5756 | img->resize(w, h, Image::INTERPOLATE_CUBIC); |
| 5757 | } |
| 5758 | |
| 5759 | // We're using long to have wordsize (32Bit build -> 32 Bits, 64 Bit build -> 64 Bits |
| 5760 | Vector<long> pd; |
| 5761 | |
| 5762 | pd.resize(2 + w * h); |
| 5763 | |
| 5764 | pd.write[0] = w; |
| 5765 | pd.write[1] = h; |
| 5766 | |
| 5767 | const uint8_t *r = img->get_data().ptr(); |
| 5768 | |
| 5769 | long *wr = &pd.write[2]; |
| 5770 | uint8_t const *pr = r; |
| 5771 | |
| 5772 | for (int i = 0; i < w * h; i++) { |
| 5773 | long v = 0; |
| 5774 | // A R G B |
| 5775 | v |= pr[3] << 24 | pr[0] << 16 | pr[1] << 8 | pr[2]; |
| 5776 | *wr++ = v; |
| 5777 | pr += 4; |
| 5778 | } |
| 5779 | |
| 5780 | if (net_wm_icon != None) { |
| 5781 | XChangeProperty(x11_display, wd.x11_window, net_wm_icon, XA_CARDINAL, 32, PropModeReplace, (unsigned char *)pd.ptr(), pd.size()); |
| 5782 | } |
| 5783 | |
| 5784 | if (!g_set_icon_error) { |
| 5785 | break; |
| 5786 | } |
| 5787 | } |
| 5788 | } else { |
| 5789 | XDeleteProperty(x11_display, wd.x11_window, net_wm_icon); |
| 5790 | } |
| 5791 | |
| 5792 | XFlush(x11_display); |
| 5793 | XSetErrorHandler(oldHandler); |
| 5794 | } |
| 5795 | |
| 5796 | void DisplayServerX11::window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window) { |
| 5797 | _THREAD_SAFE_METHOD_ |
| 5798 | #if defined(RD_ENABLED) |
| 5799 | if (rendering_context) { |
| 5800 | rendering_context->window_set_vsync_mode(p_window, p_vsync_mode); |
| 5801 | } |
| 5802 | #endif |
| 5803 | |
| 5804 | #if defined(GLES3_ENABLED) |
| 5805 | if (gl_manager) { |
| 5806 | gl_manager->set_use_vsync(p_vsync_mode != DisplayServer::VSYNC_DISABLED); |
| 5807 | } |
| 5808 | if (gl_manager_egl) { |
| 5809 | gl_manager_egl->set_use_vsync(p_vsync_mode != DisplayServer::VSYNC_DISABLED); |
| 5810 | } |
| 5811 | #endif |
| 5812 | } |
| 5813 | |
| 5814 | DisplayServer::VSyncMode DisplayServerX11::window_get_vsync_mode(WindowID p_window) const { |
| 5815 | _THREAD_SAFE_METHOD_ |
| 5816 | #if defined(RD_ENABLED) |
| 5817 | if (rendering_context) { |
| 5818 | return rendering_context->window_get_vsync_mode(p_window); |
| 5819 | } |
| 5820 | #endif |
| 5821 | #if defined(GLES3_ENABLED) |
| 5822 | if (gl_manager) { |
| 5823 | return gl_manager->is_using_vsync() ? DisplayServer::VSYNC_ENABLED : DisplayServer::VSYNC_DISABLED; |
| 5824 | } |
| 5825 | if (gl_manager_egl) { |
| 5826 | return gl_manager_egl->is_using_vsync() ? DisplayServer::VSYNC_ENABLED : DisplayServer::VSYNC_DISABLED; |
| 5827 | } |
| 5828 | #endif |
| 5829 | return DisplayServer::VSYNC_ENABLED; |
| 5830 | } |
| 5831 | |
| 5832 | void DisplayServerX11::window_start_drag(WindowID p_window) { |
| 5833 | _THREAD_SAFE_METHOD_ |
| 5834 | |
| 5835 | ERR_FAIL_COND(!windows.has(p_window)); |
| 5836 | WindowData &wd = windows[p_window]; |
| 5837 | |
| 5838 | if (wd.embed_parent) { |
| 5839 | return; // Embedded window. |
| 5840 | } |
| 5841 | |
| 5842 | XClientMessageEvent m; |
| 5843 | memset(&m, 0, sizeof(m)); |
| 5844 | |
| 5845 | XUngrabPointer(x11_display, CurrentTime); |
| 5846 | |
| 5847 | Window root_return, child_return; |
| 5848 | int root_x, root_y, win_x, win_y; |
| 5849 | unsigned int mask_return; |
| 5850 | |
| 5851 | Bool xquerypointer_result = XQueryPointer(x11_display, wd.x11_window, &root_return, &child_return, &root_x, &root_y, &win_x, &win_y, &mask_return); |
| 5852 | |
| 5853 | m.type = ClientMessage; |
| 5854 | m.window = wd.x11_window; |
| 5855 | m.message_type = XInternAtom(x11_display, "_NET_WM_MOVERESIZE", True); |
| 5856 | m.format = 32; |
| 5857 | if (xquerypointer_result) { |
| 5858 | m.data.l[0] = root_x; |
| 5859 | m.data.l[1] = root_y; |
| 5860 | m.data.l[3] = Button1; |
| 5861 | } |
| 5862 | m.data.l[2] = _NET_WM_MOVERESIZE_MOVE; |
| 5863 | m.data.l[4] = 1; // Source - normal application. |
| 5864 | |
| 5865 | XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&m); |
| 5866 | |
| 5867 | XSync(x11_display, 0); |
| 5868 | } |
| 5869 | |
| 5870 | void DisplayServerX11::window_start_resize(WindowResizeEdge p_edge, WindowID p_window) { |
| 5871 | _THREAD_SAFE_METHOD_ |
| 5872 | |
| 5873 | ERR_FAIL_INDEX(int(p_edge), WINDOW_EDGE_MAX); |
| 5874 | |
| 5875 | ERR_FAIL_COND(!windows.has(p_window)); |
| 5876 | WindowData &wd = windows[p_window]; |
| 5877 | |
| 5878 | if (wd.embed_parent) { |
| 5879 | return; // Embedded window. |
| 5880 | } |
| 5881 | |
| 5882 | XClientMessageEvent m; |
| 5883 | memset(&m, 0, sizeof(m)); |
| 5884 | |
| 5885 | XUngrabPointer(x11_display, CurrentTime); |
| 5886 | |
| 5887 | Window root_return, child_return; |
| 5888 | int root_x, root_y, win_x, win_y; |
| 5889 | unsigned int mask_return; |
| 5890 | |
| 5891 | Bool xquerypointer_result = XQueryPointer(x11_display, wd.x11_window, &root_return, &child_return, &root_x, &root_y, &win_x, &win_y, &mask_return); |
| 5892 | |
| 5893 | m.type = ClientMessage; |
| 5894 | m.window = wd.x11_window; |
| 5895 | m.message_type = XInternAtom(x11_display, "_NET_WM_MOVERESIZE", True); |
| 5896 | m.format = 32; |
| 5897 | if (xquerypointer_result) { |
| 5898 | m.data.l[0] = root_x; |
| 5899 | m.data.l[1] = root_y; |
| 5900 | m.data.l[3] = Button1; |
| 5901 | } |
| 5902 | |
| 5903 | switch (p_edge) { |
| 5904 | case DisplayServer::WINDOW_EDGE_TOP_LEFT: { |
| 5905 | m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; |
| 5906 | } break; |
| 5907 | case DisplayServer::WINDOW_EDGE_TOP: { |
| 5908 | m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_TOP; |
| 5909 | } break; |
| 5910 | case DisplayServer::WINDOW_EDGE_TOP_RIGHT: { |
| 5911 | m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; |
| 5912 | } break; |
| 5913 | case DisplayServer::WINDOW_EDGE_LEFT: { |
| 5914 | m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_LEFT; |
| 5915 | } break; |
| 5916 | case DisplayServer::WINDOW_EDGE_RIGHT: { |
| 5917 | m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_RIGHT; |
| 5918 | } break; |
| 5919 | case DisplayServer::WINDOW_EDGE_BOTTOM_LEFT: { |
| 5920 | m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; |
| 5921 | } break; |
| 5922 | case DisplayServer::WINDOW_EDGE_BOTTOM: { |
| 5923 | m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_BOTTOM; |
| 5924 | } break; |
| 5925 | case DisplayServer::WINDOW_EDGE_BOTTOM_RIGHT: { |
| 5926 | m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; |
| 5927 | } break; |
| 5928 | default: |
| 5929 | break; |
| 5930 | } |
| 5931 | m.data.l[4] = 1; // Source - normal application. |
| 5932 | |
| 5933 | XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&m); |
| 5934 | |
| 5935 | XSync(x11_display, 0); |
| 5936 | } |
| 5937 | |
| 5938 | pid_t get_window_pid(Display *p_display, Window p_window) { |
| 5939 | Atom atom = XInternAtom(p_display, "_NET_WM_PID", False); |
| 5940 | Atom actualType; |
| 5941 | int actualFormat; |
| 5942 | unsigned long nItems, bytesAfter; |
| 5943 | unsigned char *prop = nullptr; |
| 5944 | if (XGetWindowProperty(p_display, p_window, atom, 0, sizeof(pid_t), False, AnyPropertyType, |
| 5945 | &actualType, &actualFormat, &nItems, &bytesAfter, &prop) == Success) { |
| 5946 | if (nItems > 0) { |
| 5947 | pid_t pid = *(pid_t *)prop; |
| 5948 | XFree(prop); |
| 5949 | return pid; |
| 5950 | } |
| 5951 | } |
| 5952 | |
| 5953 | return 0; // PID not found. |
| 5954 | } |
| 5955 | |
| 5956 | Window find_window_from_process_id_internal(Display *p_display, pid_t p_process_id, Window p_window) { |
| 5957 | Window dummy; |
| 5958 | Window *children; |
| 5959 | unsigned int num_children; |
| 5960 | |
| 5961 | if (!XQueryTree(p_display, p_window, &dummy, &dummy, &children, &num_children)) { |
| 5962 | return 0; |
| 5963 | } |
| 5964 | |
| 5965 | for (unsigned int i = 0; i < num_children; i++) { |
| 5966 | const Window child = children[i]; |
| 5967 | if (get_window_pid(p_display, child) == p_process_id) { |
| 5968 | XFree(children); |
| 5969 | return child; |
| 5970 | } |
| 5971 | } |
| 5972 | |
| 5973 | // Then check children of children. |
| 5974 | for (unsigned int i = 0; i < num_children; i++) { |
| 5975 | Window wnd = find_window_from_process_id_internal(p_display, p_process_id, children[i]); |
| 5976 | if (wnd != 0) { |
| 5977 | XFree(children); |
| 5978 | return wnd; |
| 5979 | } |
| 5980 | } |
| 5981 | |
| 5982 | if (children) { |
| 5983 | XFree(children); |
| 5984 | } |
| 5985 | |
| 5986 | return 0; |
| 5987 | } |
| 5988 | |
| 5989 | Window find_window_from_process_id(Display *p_display, pid_t p_process_id) { |
| 5990 | // Handle bad window errors silently because while looping |
| 5991 | // windows can be destroyed, resulting in BadWindow errors. |
| 5992 | int (*oldHandler)(Display *, XErrorEvent *) = XSetErrorHandler(&bad_window_error_handler); |
| 5993 | |
| 5994 | const int screencount = XScreenCount(p_display); |
| 5995 | Window process_window = 0; |
| 5996 | |
| 5997 | for (int screen_index = 0; screen_index < screencount; screen_index++) { |
| 5998 | Window root = RootWindow(p_display, screen_index); |
| 5999 | |
| 6000 | Window wnd = find_window_from_process_id_internal(p_display, p_process_id, root); |
| 6001 | |
| 6002 | if (wnd != 0) { |
| 6003 | process_window = wnd; |
| 6004 | break; |
| 6005 | } |
| 6006 | } |
| 6007 | |
| 6008 | // Restore default error handler. |
| 6009 | XSetErrorHandler(oldHandler); |
| 6010 | |
| 6011 | return process_window; |
| 6012 | } |
| 6013 | |
| 6014 | Point2i DisplayServerX11::_get_window_position(Window p_window) const { |
| 6015 | int x = 0, y = 0; |
| 6016 | Window child; |
| 6017 | XTranslateCoordinates(x11_display, p_window, DefaultRootWindow(x11_display), 0, 0, &x, &y, &child); |
| 6018 | return Point2i(x, y); |
| 6019 | } |
| 6020 | |
| 6021 | Rect2i DisplayServerX11::_get_window_rect(Window p_window) const { |
| 6022 | XWindowAttributes xwa; |
| 6023 | XGetWindowAttributes(x11_display, p_window, &xwa); |
| 6024 | return Rect2i(xwa.x, xwa.y, xwa.width, xwa.height); |
| 6025 | } |
| 6026 | |
| 6027 | void DisplayServerX11::_set_window_taskbar_pager_enabled(Window p_window, bool p_enabled) { |
| 6028 | Atom wmState = XInternAtom(x11_display, "_NET_WM_STATE", False); |
| 6029 | Atom skipTaskbar = XInternAtom(x11_display, "_NET_WM_STATE_SKIP_TASKBAR", False); |
| 6030 | Atom skipPager = XInternAtom(x11_display, "_NET_WM_STATE_SKIP_PAGER", False); |
| 6031 | |
| 6032 | XClientMessageEvent xev; |
| 6033 | memset(&xev, 0, sizeof(xev)); |
| 6034 | xev.type = ClientMessage; |
| 6035 | xev.window = p_window; |
| 6036 | xev.message_type = wmState; |
| 6037 | xev.format = 32; |
| 6038 | xev.data.l[0] = p_enabled ? _NET_WM_STATE_REMOVE : _NET_WM_STATE_ADD; // When enabled, we must remove the skip. |
| 6039 | xev.data.l[1] = skipTaskbar; |
| 6040 | xev.data.l[2] = skipPager; |
| 6041 | xev.data.l[3] = 0; |
| 6042 | xev.data.l[4] = 0; |
| 6043 | |
| 6044 | // Send the client message to the root window. |
| 6045 | XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&xev); |
| 6046 | } |
| 6047 | |
| 6048 | Error DisplayServerX11::embed_process(WindowID p_window, OS::ProcessID p_pid, const Rect2i &p_rect, bool p_visible, bool p_grab_focus) { |
| 6049 | _THREAD_SAFE_METHOD_ |
| 6050 | |
| 6051 | ERR_FAIL_COND_V(!windows.has(p_window), FAILED); |
| 6052 | |
| 6053 | const WindowData &wd = windows[p_window]; |
| 6054 | |
| 6055 | DEBUG_LOG_X11("Starting embedding %ld to window %lu \n", p_pid, wd.x11_window); |
| 6056 | |
| 6057 | EmbeddedProcessData *ep = nullptr; |
| 6058 | if (embedded_processes.has(p_pid)) { |
| 6059 | ep = embedded_processes.get(p_pid); |
| 6060 | } else { |
| 6061 | // New process, trying to find the window. |
| 6062 | Window process_window = find_window_from_process_id(x11_display, p_pid); |
| 6063 | if (!process_window) { |
| 6064 | return ERR_DOES_NOT_EXIST; |
| 6065 | } |
| 6066 | DEBUG_LOG_X11("Process %ld window found: %lu \n", p_pid, process_window); |
| 6067 | ep = memnew(EmbeddedProcessData); |
| 6068 | ep->process_window = process_window; |
| 6069 | ep->visible = true; |
| 6070 | XSetTransientForHint(x11_display, process_window, wd.x11_window); |
| 6071 | _set_window_taskbar_pager_enabled(process_window, false); |
| 6072 | embedded_processes.insert(p_pid, ep); |
| 6073 | } |
| 6074 | |
| 6075 | // Handle bad window errors silently because just in case the embedded window was closed. |
| 6076 | int (*oldHandler)(Display *, XErrorEvent *) = XSetErrorHandler(&bad_window_error_handler); |
| 6077 | |
| 6078 | if (p_visible) { |
| 6079 | // Resize and move the window to match the desired rectangle. |
| 6080 | // X11 does not allow moving the window entirely outside the screen boundaries. |
| 6081 | // To ensure the window remains visible, we will resize it to fit within both the screen and the specified rectangle. |
| 6082 | Rect2i desired_rect = p_rect; |
| 6083 | |
| 6084 | // First resize the desired rect to fit inside all the screens without considering the |
| 6085 | // working area. |
| 6086 | Rect2i screens_full_rect = _screens_get_full_rect(); |
| 6087 | Vector2i screens_full_end = screens_full_rect.get_end(); |
| 6088 | if (desired_rect.position.x < screens_full_rect.position.x) { |
| 6089 | desired_rect.size.x = MAX(desired_rect.size.x - (screens_full_rect.position.x - desired_rect.position.x), 0); |
| 6090 | desired_rect.position.x = screens_full_rect.position.x; |
| 6091 | } |
| 6092 | if (desired_rect.position.x + desired_rect.size.x > screens_full_end.x) { |
| 6093 | desired_rect.size.x = MAX(screens_full_end.x - desired_rect.position.x, 0); |
| 6094 | } |
| 6095 | if (desired_rect.position.y < screens_full_rect.position.y) { |
| 6096 | desired_rect.size.y = MAX(desired_rect.size.y - (screens_full_rect.position.y - desired_rect.position.y), 0); |
| 6097 | desired_rect.position.y = screens_full_rect.position.y; |
| 6098 | } |
| 6099 | if (desired_rect.position.y + desired_rect.size.y > screens_full_end.y) { |
| 6100 | desired_rect.size.y = MAX(screens_full_end.y - desired_rect.position.y, 0); |
| 6101 | } |
| 6102 | |
| 6103 | // Second, for each screen, check if the desired rectangle is within a portion of the screen |
| 6104 | // that is outside the working area. Each screen can have a different working area |
| 6105 | // depending on top, bottom, or side panels. |
| 6106 | int desired_area = desired_rect.get_area(); |
| 6107 | int count = get_screen_count(); |
| 6108 | for (int i = 0; i < count; i++) { |
| 6109 | Rect2i screen_rect = _screen_get_rect(i); |
| 6110 | if (screen_rect.intersection(desired_rect).get_area() == 0) { |
| 6111 | continue; |
| 6112 | } |
| 6113 | |
| 6114 | // The desired rect is inside this screen. |
| 6115 | Rect2i screen_usable_rect = screen_get_usable_rect(i); |
| 6116 | int screen_usable_area = screen_usable_rect.intersection(desired_rect).get_area(); |
| 6117 | if (screen_usable_area == desired_area) { |
| 6118 | // The desired rect is fulling inside the usable rect of the screen. No need to resize. |
| 6119 | continue; |
| 6120 | } |
| 6121 | |
| 6122 | if (desired_rect.position.x >= screen_rect.position.x && desired_rect.position.x < screen_usable_rect.position.x) { |
| 6123 | int offset = screen_usable_rect.position.x - desired_rect.position.x; |
| 6124 | desired_rect.size.x = MAX(desired_rect.size.x - offset, 0); |
| 6125 | desired_rect.position.x += offset; |
| 6126 | } |
| 6127 | if (desired_rect.position.y >= screen_rect.position.y && desired_rect.position.y < screen_usable_rect.position.y) { |
| 6128 | int offset = screen_usable_rect.position.y - desired_rect.position.y; |
| 6129 | desired_rect.size.y = MAX(desired_rect.size.y - offset, 0); |
| 6130 | desired_rect.position.y += offset; |
| 6131 | } |
| 6132 | |
| 6133 | Vector2i desired_end = desired_rect.get_end(); |
| 6134 | Vector2i screen_end = screen_rect.get_end(); |
| 6135 | Vector2i screen_usable_end = screen_usable_rect.get_end(); |
| 6136 | if (desired_end.x > screen_usable_end.x && desired_end.x <= screen_end.x) { |
| 6137 | desired_rect.size.x = MAX(desired_rect.size.x - (desired_end.x - screen_usable_end.x), 0); |
| 6138 | } |
| 6139 | if (desired_end.y > screen_usable_end.y && desired_end.y <= screen_end.y) { |
| 6140 | desired_rect.size.y = MAX(desired_rect.size.y - (desired_end.y - screen_usable_end.y), 0); |
| 6141 | } |
| 6142 | } |
| 6143 | |
| 6144 | if (desired_rect.size.x <= 100 || desired_rect.size.y <= 100) { |
| 6145 | p_visible = false; |
| 6146 | } |
| 6147 | |
| 6148 | if (p_visible) { |
| 6149 | Rect2i current_process_window_rect = _get_window_rect(ep->process_window); |
| 6150 | if (current_process_window_rect != desired_rect) { |
| 6151 | DEBUG_LOG_X11("Embedding XMoveResizeWindow process %ld, window %lu to %d, %d, %d, %d \n", p_pid, wd.x11_window, desired_rect.position.x, desired_rect.position.y, desired_rect.size.x, desired_rect.size.y); |
| 6152 | XMoveResizeWindow(x11_display, ep->process_window, desired_rect.position.x, desired_rect.position.y, desired_rect.size.x, desired_rect.size.y); |
| 6153 | } |
| 6154 | } |
| 6155 | } |
| 6156 | |
| 6157 | if (ep->visible != p_visible) { |
| 6158 | if (p_visible) { |
| 6159 | XMapWindow(x11_display, ep->process_window); |
| 6160 | } else { |
| 6161 | XUnmapWindow(x11_display, ep->process_window); |
| 6162 | } |
| 6163 | ep->visible = p_visible; |
| 6164 | } |
| 6165 | |
| 6166 | if (p_grab_focus && p_visible) { |
| 6167 | Window focused_window = 0; |
| 6168 | int revert_to = 0; |
| 6169 | XGetInputFocus(x11_display, &focused_window, &revert_to); |
| 6170 | if (focused_window != ep->process_window) { |
| 6171 | // Be sure that the window is visible to prevent BadMatch error when calling XSetInputFocus on a not viewable window. |
| 6172 | XWindowAttributes attr; |
| 6173 | if (XGetWindowAttributes(x11_display, ep->process_window, &attr) && attr.map_state == IsViewable) { |
| 6174 | XSetInputFocus(x11_display, ep->process_window, RevertToParent, CurrentTime); |
| 6175 | } |
| 6176 | } |
| 6177 | } |
| 6178 | |
| 6179 | // Restore default error handler. |
| 6180 | XSetErrorHandler(oldHandler); |
| 6181 | return OK; |
| 6182 | } |
| 6183 | |
| 6184 | Error DisplayServerX11::request_close_embedded_process(OS::ProcessID p_pid) { |
| 6185 | _THREAD_SAFE_METHOD_ |
| 6186 | |
| 6187 | if (!embedded_processes.has(p_pid)) { |
| 6188 | return ERR_DOES_NOT_EXIST; |
| 6189 | } |
| 6190 | |
| 6191 | EmbeddedProcessData *ep = embedded_processes.get(p_pid); |
| 6192 | |
| 6193 | // Handle bad window errors silently because just in case the embedded window was closed. |
| 6194 | int (*oldHandler)(Display *, XErrorEvent *) = XSetErrorHandler(&bad_window_error_handler); |
| 6195 | |
| 6196 | // Check if the window is still valid. |
| 6197 | XWindowAttributes attr; |
| 6198 | if (XGetWindowAttributes(x11_display, ep->process_window, &attr)) { |
| 6199 | // Send the message to gracefully close the window. |
| 6200 | XEvent ev; |
| 6201 | memset(&ev, 0, sizeof(ev)); |
| 6202 | ev.xclient.type = ClientMessage; |
| 6203 | ev.xclient.window = ep->process_window; |
| 6204 | ev.xclient.message_type = XInternAtom(x11_display, "WM_PROTOCOLS", True); |
| 6205 | ev.xclient.format = 32; |
| 6206 | ev.xclient.data.l[0] = XInternAtom(x11_display, "WM_DELETE_WINDOW", False); |
| 6207 | ev.xclient.data.l[1] = CurrentTime; |
| 6208 | XSendEvent(x11_display, ep->process_window, False, NoEventMask, &ev); |
| 6209 | } |
| 6210 | |
| 6211 | // Restore default error handler. |
| 6212 | XSetErrorHandler(oldHandler); |
| 6213 | |
| 6214 | return OK; |
| 6215 | } |
| 6216 | |
| 6217 | Error DisplayServerX11::remove_embedded_process(OS::ProcessID p_pid) { |
| 6218 | _THREAD_SAFE_METHOD_ |
| 6219 | |
| 6220 | if (!embedded_processes.has(p_pid)) { |
| 6221 | return ERR_DOES_NOT_EXIST; |
| 6222 | } |
| 6223 | |
| 6224 | EmbeddedProcessData *ep = embedded_processes.get(p_pid); |
| 6225 | |
| 6226 | request_close_embedded_process(p_pid); |
| 6227 | |
| 6228 | embedded_processes.erase(p_pid); |
| 6229 | memdelete(ep); |
| 6230 | |
| 6231 | return OK; |
| 6232 | } |
| 6233 | |
| 6234 | OS::ProcessID DisplayServerX11::get_focused_process_id() { |
| 6235 | Window focused_window = 0; |
| 6236 | int revert_to = 0; |
| 6237 | |
| 6238 | XGetInputFocus(x11_display, &focused_window, &revert_to); |
| 6239 | |
| 6240 | if (focused_window == None) { |
| 6241 | return 0; |
| 6242 | } |
| 6243 | |
| 6244 | return get_window_pid(x11_display, focused_window); |
| 6245 | } |
| 6246 | |
| 6247 | Vector<String> DisplayServerX11::get_rendering_drivers_func() { |
| 6248 | Vector<String> drivers; |
| 6249 | |
| 6250 | #ifdef VULKAN_ENABLED |
| 6251 | drivers.push_back("vulkan"); |
| 6252 | #endif |
| 6253 | #ifdef GLES3_ENABLED |
| 6254 | drivers.push_back("opengl3"); |
| 6255 | drivers.push_back("opengl3_es"); |
| 6256 | #endif |
| 6257 | drivers.push_back("dummy"); |
| 6258 | |
| 6259 | return drivers; |
| 6260 | } |
| 6261 | |
| 6262 | DisplayServer *DisplayServerX11::create_func(const String &p_rendering_driver, WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Context p_context, int64_t p_parent_window, Error &r_error) { |
| 6263 | DisplayServer *ds = memnew(DisplayServerX11(p_rendering_driver, p_mode, p_vsync_mode, p_flags, p_position, p_resolution, p_screen, p_context, p_parent_window, r_error)); |
| 6264 | return ds; |
| 6265 | } |
| 6266 | |
| 6267 | void DisplayServerX11::_create_xic(WindowData &wd) { |
| 6268 | if (xim && xim_style) { |
| 6269 | // Block events polling while changing input focus |
| 6270 | // because it triggers some event polling internally. |
| 6271 | MutexLock mutex_lock(events_mutex); |
| 6272 | |
| 6273 | // Force on-the-spot for the over-the-spot style. |
| 6274 | if ((xim_style & XIMPreeditPosition) != 0) { |
| 6275 | xim_style &= ~XIMPreeditPosition; |
| 6276 | xim_style |= XIMPreeditCallbacks; |
| 6277 | } |
| 6278 | if ((xim_style & XIMPreeditCallbacks) != 0) { |
| 6279 | ::XIMCallback preedit_start_callback; |
| 6280 | preedit_start_callback.client_data = (::XPointer)(this); |
| 6281 | preedit_start_callback.callback = (::XIMProc)(void *)(_xim_preedit_start_callback); |
| 6282 | |
| 6283 | ::XIMCallback preedit_done_callback; |
| 6284 | preedit_done_callback.client_data = (::XPointer)(this); |
| 6285 | preedit_done_callback.callback = (::XIMProc)(_xim_preedit_done_callback); |
| 6286 | |
| 6287 | ::XIMCallback preedit_draw_callback; |
| 6288 | preedit_draw_callback.client_data = (::XPointer)(this); |
| 6289 | preedit_draw_callback.callback = (::XIMProc)(_xim_preedit_draw_callback); |
| 6290 | |
| 6291 | ::XIMCallback preedit_caret_callback; |
| 6292 | preedit_caret_callback.client_data = (::XPointer)(this); |
| 6293 | preedit_caret_callback.callback = (::XIMProc)(_xim_preedit_caret_callback); |
| 6294 | |
| 6295 | ::XVaNestedList preedit_attributes = XVaCreateNestedList(0, |
| 6296 | XNPreeditStartCallback, &preedit_start_callback, |
| 6297 | XNPreeditDoneCallback, &preedit_done_callback, |
| 6298 | XNPreeditDrawCallback, &preedit_draw_callback, |
| 6299 | XNPreeditCaretCallback, &preedit_caret_callback, |
| 6300 | (char *)nullptr); |
| 6301 | |
| 6302 | wd.xic = XCreateIC(xim, |
| 6303 | XNInputStyle, xim_style, |
| 6304 | XNClientWindow, wd.x11_xim_window, |
| 6305 | XNFocusWindow, wd.x11_xim_window, |
| 6306 | XNPreeditAttributes, preedit_attributes, |
| 6307 | (char *)nullptr); |
| 6308 | XFree(preedit_attributes); |
| 6309 | } else { |
| 6310 | wd.xic = XCreateIC(xim, |
| 6311 | XNInputStyle, xim_style, |
| 6312 | XNClientWindow, wd.x11_xim_window, |
| 6313 | XNFocusWindow, wd.x11_xim_window, |
| 6314 | (char *)nullptr); |
| 6315 | } |
| 6316 | |
| 6317 | long im_event_mask = 0; |
| 6318 | if (XGetICValues(wd.xic, XNFilterEvents, &im_event_mask, nullptr) != nullptr) { |
| 6319 | WARN_PRINT("XGetICValues couldn't obtain XNFilterEvents value."); |
| 6320 | XDestroyIC(wd.xic); |
| 6321 | wd.xic = nullptr; |
| 6322 | } |
| 6323 | if (wd.xic) { |
| 6324 | XUnsetICFocus(wd.xic); |
| 6325 | } else { |
| 6326 | WARN_PRINT("XCreateIC couldn't create wd.xic."); |
| 6327 | } |
| 6328 | } else { |
| 6329 | wd.xic = nullptr; |
| 6330 | WARN_PRINT("XCreateIC couldn't create wd.xic."); |
| 6331 | } |
| 6332 | } |
| 6333 | |
| 6334 | DisplayServerX11::WindowID DisplayServerX11::_create_window(WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Rect2i &p_rect, Window p_parent_window) { |
| 6335 | //Create window |
| 6336 | |
| 6337 | XVisualInfo visualInfo; |
| 6338 | bool vi_selected = false; |
| 6339 | |
| 6340 | #ifdef GLES3_ENABLED |
| 6341 | if (gl_manager) { |
| 6342 | Error err; |
| 6343 | visualInfo = gl_manager->get_vi(x11_display, err); |
| 6344 | ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, "Can't acquire visual info from display."); |
| 6345 | vi_selected = true; |
| 6346 | } |
| 6347 | if (gl_manager_egl) { |
| 6348 | XVisualInfo visual_info_template; |
| 6349 | int visual_id = gl_manager_egl->display_get_native_visual_id(x11_display); |
| 6350 | ERR_FAIL_COND_V_MSG(visual_id < 0, INVALID_WINDOW_ID, "Unable to get a visual id."); |
| 6351 | |
| 6352 | visual_info_template.visualid = (VisualID)visual_id; |
| 6353 | |
| 6354 | int number_of_visuals = 0; |
| 6355 | XVisualInfo *vi_list = XGetVisualInfo(x11_display, VisualIDMask, &visual_info_template, &number_of_visuals); |
| 6356 | ERR_FAIL_COND_V(number_of_visuals <= 0, INVALID_WINDOW_ID); |
| 6357 | |
| 6358 | visualInfo = vi_list[0]; |
| 6359 | |
| 6360 | XFree(vi_list); |
| 6361 | } |
| 6362 | #endif |
| 6363 | |
| 6364 | if (!vi_selected) { |
| 6365 | long visualMask = VisualScreenMask; |
| 6366 | int numberOfVisuals; |
| 6367 | XVisualInfo vInfoTemplate = {}; |
| 6368 | vInfoTemplate.screen = DefaultScreen(x11_display); |
| 6369 | XVisualInfo *vi_list = XGetVisualInfo(x11_display, visualMask, &vInfoTemplate, &numberOfVisuals); |
| 6370 | ERR_FAIL_NULL_V(vi_list, INVALID_WINDOW_ID); |
| 6371 | |
| 6372 | visualInfo = vi_list[0]; |
| 6373 | if (OS::get_singleton()->is_layered_allowed()) { |
| 6374 | for (int i = 0; i < numberOfVisuals; i++) { |
| 6375 | XRenderPictFormat *pict_format = XRenderFindVisualFormat(x11_display, vi_list[i].visual); |
| 6376 | if (!pict_format) { |
| 6377 | continue; |
| 6378 | } |
| 6379 | visualInfo = vi_list[i]; |
| 6380 | if (pict_format->direct.alphaMask > 0) { |
| 6381 | break; |
| 6382 | } |
| 6383 | } |
| 6384 | } |
| 6385 | XFree(vi_list); |
| 6386 | } |
| 6387 | |
| 6388 | Colormap colormap = XCreateColormap(x11_display, RootWindow(x11_display, visualInfo.screen), visualInfo.visual, AllocNone); |
| 6389 | |
| 6390 | XSetWindowAttributes windowAttributes = {}; |
| 6391 | windowAttributes.colormap = colormap; |
| 6392 | windowAttributes.background_pixel = 0xFFFFFFFF; |
| 6393 | windowAttributes.border_pixel = 0; |
| 6394 | windowAttributes.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask; |
| 6395 | |
| 6396 | unsigned long valuemask = CWBorderPixel | CWColormap | CWEventMask; |
| 6397 | |
| 6398 | if (OS::get_singleton()->is_layered_allowed()) { |
| 6399 | windowAttributes.background_pixmap = None; |
| 6400 | windowAttributes.background_pixel = 0; |
| 6401 | windowAttributes.border_pixmap = None; |
| 6402 | valuemask |= CWBackPixel; |
| 6403 | } |
| 6404 | |
| 6405 | WindowID id = window_id_counter++; |
| 6406 | WindowData &wd = windows[id]; |
| 6407 | |
| 6408 | if (p_flags & WINDOW_FLAG_NO_FOCUS_BIT) { |
| 6409 | wd.no_focus = true; |
| 6410 | } |
| 6411 | |
| 6412 | if (p_flags & WINDOW_FLAG_POPUP_BIT) { |
| 6413 | wd.is_popup = true; |
| 6414 | } |
| 6415 | |
| 6416 | // Setup for menu subwindows: |
| 6417 | // - override_redirect forces the WM not to interfere with the window, to avoid delays due to |
| 6418 | // handling decorations and placement. |
| 6419 | // On the other hand, focus changes need to be handled manually when this is set. |
| 6420 | // - save_under is a hint for the WM to keep the content of windows behind to avoid repaint. |
| 6421 | if (wd.no_focus) { |
| 6422 | windowAttributes.override_redirect = True; |
| 6423 | windowAttributes.save_under = True; |
| 6424 | valuemask |= CWOverrideRedirect | CWSaveUnder; |
| 6425 | } |
| 6426 | |
| 6427 | int rq_screen = get_screen_from_rect(p_rect); |
| 6428 | if (rq_screen < 0) { |
| 6429 | rq_screen = get_primary_screen(); // Requested window rect is outside any screen bounds. |
| 6430 | } |
| 6431 | |
| 6432 | Rect2i win_rect = p_rect; |
| 6433 | if (!p_parent_window) { |
| 6434 | // No parent. |
| 6435 | if (p_mode == WINDOW_MODE_FULLSCREEN || p_mode == WINDOW_MODE_EXCLUSIVE_FULLSCREEN) { |
| 6436 | Rect2i screen_rect = Rect2i(screen_get_position(rq_screen), screen_get_size(rq_screen)); |
| 6437 | |
| 6438 | win_rect = screen_rect; |
| 6439 | } else { |
| 6440 | Rect2i srect = screen_get_usable_rect(rq_screen); |
| 6441 | Point2i wpos = p_rect.position; |
| 6442 | wpos = wpos.clamp(srect.position, srect.position + srect.size - p_rect.size / 3); |
| 6443 | |
| 6444 | win_rect.position = wpos; |
| 6445 | } |
| 6446 | } |
| 6447 | |
| 6448 | // Position and size hints are set from these values before they are updated to the actual |
| 6449 | // window size, so we need to initialize them here. |
| 6450 | wd.position = win_rect.position; |
| 6451 | wd.size = win_rect.size; |
| 6452 | |
| 6453 | { |
| 6454 | wd.x11_window = XCreateWindow(x11_display, RootWindow(x11_display, visualInfo.screen), win_rect.position.x, win_rect.position.y, win_rect.size.width > 0 ? win_rect.size.width : 1, win_rect.size.height > 0 ? win_rect.size.height : 1, 0, visualInfo.depth, InputOutput, visualInfo.visual, valuemask, &windowAttributes); |
| 6455 | |
| 6456 | DEBUG_LOG_X11("CreateWindow window=%lu, parent: %lu \n", wd.x11_window, wd.parent); |
| 6457 | |
| 6458 | if (p_parent_window) { |
| 6459 | wd.embed_parent = p_parent_window; |
| 6460 | XSetTransientForHint(x11_display, wd.x11_window, p_parent_window); |
| 6461 | } |
| 6462 | |
| 6463 | XSetWindowAttributes window_attributes_ime = {}; |
| 6464 | window_attributes_ime.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask; |
| 6465 | |
| 6466 | wd.x11_xim_window = XCreateWindow(x11_display, wd.x11_window, 0, 0, 1, 1, 0, CopyFromParent, InputOnly, CopyFromParent, CWEventMask, &window_attributes_ime); |
| 6467 | #ifdef XKB_ENABLED |
| 6468 | if (dead_tbl && xkb_loaded_v05p) { |
| 6469 | wd.xkb_state = xkb_compose_state_new(dead_tbl, XKB_COMPOSE_STATE_NO_FLAGS); |
| 6470 | } |
| 6471 | #endif |
| 6472 | #ifdef ACCESSKIT_ENABLED |
| 6473 | if (accessibility_driver && !accessibility_driver->window_create(id, nullptr)) { |
| 6474 | if (OS::get_singleton()->is_stdout_verbose()) { |
| 6475 | ERR_PRINT("Can't create an accessibility adapter for window, accessibility support disabled!"); |
| 6476 | } |
| 6477 | memdelete(accessibility_driver); |
| 6478 | accessibility_driver = nullptr; |
| 6479 | } |
| 6480 | #endif |
| 6481 | // Enable receiving notification when the window is initialized (MapNotify) |
| 6482 | // so the focus can be set at the right time. |
| 6483 | if (!wd.no_focus && !wd.is_popup) { |
| 6484 | XSelectInput(x11_display, wd.x11_window, StructureNotifyMask); |
| 6485 | } |
| 6486 | |
| 6487 | //associate PID |
| 6488 | // make PID known to X11 |
| 6489 | { |
| 6490 | const long pid = OS::get_singleton()->get_process_id(); |
| 6491 | Atom net_wm_pid = XInternAtom(x11_display, "_NET_WM_PID", False); |
| 6492 | if (net_wm_pid != None) { |
| 6493 | XChangeProperty(x11_display, wd.x11_window, net_wm_pid, XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&pid, 1); |
| 6494 | } |
| 6495 | } |
| 6496 | |
| 6497 | long im_event_mask = 0; |
| 6498 | |
| 6499 | { |
| 6500 | XIEventMask all_event_mask; |
| 6501 | XSetWindowAttributes new_attr; |
| 6502 | |
| 6503 | new_attr.event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | |
| 6504 | ButtonReleaseMask | EnterWindowMask | |
| 6505 | LeaveWindowMask | PointerMotionMask | |
| 6506 | Button1MotionMask | |
| 6507 | Button2MotionMask | Button3MotionMask | |
| 6508 | Button4MotionMask | Button5MotionMask | |
| 6509 | ButtonMotionMask | KeymapStateMask | |
| 6510 | ExposureMask | VisibilityChangeMask | |
| 6511 | StructureNotifyMask | |
| 6512 | SubstructureNotifyMask | SubstructureRedirectMask | |
| 6513 | FocusChangeMask | PropertyChangeMask | |
| 6514 | ColormapChangeMask | OwnerGrabButtonMask | |
| 6515 | im_event_mask; |
| 6516 | |
| 6517 | XChangeWindowAttributes(x11_display, wd.x11_window, CWEventMask, &new_attr); |
| 6518 | |
| 6519 | static unsigned char all_mask_data[XIMaskLen(XI_LASTEVENT)] = {}; |
| 6520 | |
| 6521 | all_event_mask.deviceid = XIAllDevices; |
| 6522 | all_event_mask.mask_len = sizeof(all_mask_data); |
| 6523 | all_event_mask.mask = all_mask_data; |
| 6524 | |
| 6525 | XISetMask(all_event_mask.mask, XI_HierarchyChanged); |
| 6526 | |
| 6527 | #ifdef TOUCH_ENABLED |
| 6528 | if (xi.touch_devices.size()) { |
| 6529 | XISetMask(all_event_mask.mask, XI_TouchBegin); |
| 6530 | XISetMask(all_event_mask.mask, XI_TouchUpdate); |
| 6531 | XISetMask(all_event_mask.mask, XI_TouchEnd); |
| 6532 | XISetMask(all_event_mask.mask, XI_TouchOwnership); |
| 6533 | } |
| 6534 | #endif |
| 6535 | |
| 6536 | XISelectEvents(x11_display, wd.x11_window, &all_event_mask, 1); |
| 6537 | } |
| 6538 | |
| 6539 | /* set the titlebar name */ |
| 6540 | XStoreName(x11_display, wd.x11_window, "Godot"); |
| 6541 | XSetWMProtocols(x11_display, wd.x11_window, &wm_delete, 1); |
| 6542 | if (xdnd_aware != None) { |
| 6543 | XChangeProperty(x11_display, wd.x11_window, xdnd_aware, XA_ATOM, 32, PropModeReplace, (unsigned char *)&xdnd_version, 1); |
| 6544 | } |
| 6545 | |
| 6546 | _create_xic(wd); |
| 6547 | _update_context(wd); |
| 6548 | |
| 6549 | if (wd.is_popup || wd.no_focus || (wd.embed_parent && !kde5_embed_workaround)) { |
| 6550 | // Set Utility type to disable fade animations. |
| 6551 | Atom type_atom = XInternAtom(x11_display, "_NET_WM_WINDOW_TYPE_UTILITY", False); |
| 6552 | Atom wt_atom = XInternAtom(x11_display, "_NET_WM_WINDOW_TYPE", False); |
| 6553 | if (wt_atom != None && type_atom != None) { |
| 6554 | XChangeProperty(x11_display, wd.x11_window, wt_atom, XA_ATOM, 32, PropModeReplace, (unsigned char *)&type_atom, 1); |
| 6555 | } |
| 6556 | } else { |
| 6557 | Atom type_atom = XInternAtom(x11_display, "_NET_WM_WINDOW_TYPE_NORMAL", False); |
| 6558 | Atom wt_atom = XInternAtom(x11_display, "_NET_WM_WINDOW_TYPE", False); |
| 6559 | |
| 6560 | if (wt_atom != None && type_atom != None) { |
| 6561 | XChangeProperty(x11_display, wd.x11_window, wt_atom, XA_ATOM, 32, PropModeReplace, (unsigned char *)&type_atom, 1); |
| 6562 | } |
| 6563 | } |
| 6564 | |
| 6565 | if (p_parent_window) { |
| 6566 | // Disable the window in the taskbar and alt-tab. |
| 6567 | _set_window_taskbar_pager_enabled(wd.x11_window, false); |
| 6568 | } |
| 6569 | |
| 6570 | _update_size_hints(id); |
| 6571 | |
| 6572 | #if defined(RD_ENABLED) |
| 6573 | if (rendering_context) { |
| 6574 | union { |
| 6575 | #ifdef VULKAN_ENABLED |
| 6576 | RenderingContextDriverVulkanX11::WindowPlatformData vulkan; |
| 6577 | #endif |
| 6578 | } wpd; |
| 6579 | #ifdef VULKAN_ENABLED |
| 6580 | if (rendering_driver == "vulkan") { |
| 6581 | wpd.vulkan.window = wd.x11_window; |
| 6582 | wpd.vulkan.display = x11_display; |
| 6583 | } |
| 6584 | #endif |
| 6585 | Error err = rendering_context->window_create(id, &wpd); |
| 6586 | ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, vformat("Can't create a %s window", rendering_driver)); |
| 6587 | |
| 6588 | rendering_context->window_set_size(id, win_rect.size.width, win_rect.size.height); |
| 6589 | rendering_context->window_set_vsync_mode(id, p_vsync_mode); |
| 6590 | } |
| 6591 | #endif |
| 6592 | #ifdef GLES3_ENABLED |
| 6593 | if (gl_manager) { |
| 6594 | Error err = gl_manager->window_create(id, wd.x11_window, x11_display, win_rect.size.width, win_rect.size.height); |
| 6595 | ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, "Can't create an OpenGL window"); |
| 6596 | } |
| 6597 | if (gl_manager_egl) { |
| 6598 | Error err = gl_manager_egl->window_create(id, x11_display, &wd.x11_window, win_rect.size.width, win_rect.size.height); |
| 6599 | ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, "Failed to create an OpenGLES window."); |
| 6600 | } |
| 6601 | window_set_vsync_mode(p_vsync_mode, id); |
| 6602 | #endif |
| 6603 | |
| 6604 | //set_class_hint(x11_display, wd.x11_window); |
| 6605 | XFlush(x11_display); |
| 6606 | |
| 6607 | XSync(x11_display, False); |
| 6608 | //XSetErrorHandler(oldHandler); |
| 6609 | } |
| 6610 | |
| 6611 | window_set_mode(p_mode, id); |
| 6612 | |
| 6613 | //sync size |
| 6614 | { |
| 6615 | XWindowAttributes xwa; |
| 6616 | |
| 6617 | XSync(x11_display, False); |
| 6618 | XGetWindowAttributes(x11_display, wd.x11_window, &xwa); |
| 6619 | |
| 6620 | wd.position.x = xwa.x; |
| 6621 | wd.position.y = xwa.y; |
| 6622 | wd.size.width = xwa.width; |
| 6623 | wd.size.height = xwa.height; |
| 6624 | } |
| 6625 | |
| 6626 | //set cursor |
| 6627 | if (cursors[current_cursor] != None) { |
| 6628 | XDefineCursor(x11_display, wd.x11_window, cursors[current_cursor]); |
| 6629 | } |
| 6630 | |
| 6631 | return id; |
| 6632 | } |
| 6633 | |
| 6634 | static bool _is_xim_style_supported(const ::XIMStyle &p_style) { |
| 6635 | const ::XIMStyle supported_preedit = XIMPreeditCallbacks | XIMPreeditPosition | XIMPreeditNothing | XIMPreeditNone; |
| 6636 | const ::XIMStyle supported_status = XIMStatusNothing | XIMStatusNone; |
| 6637 | |
| 6638 | // Check preedit style is supported |
| 6639 | if ((p_style & supported_preedit) == 0) { |
| 6640 | return false; |
| 6641 | } |
| 6642 | |
| 6643 | // Check status style is supported |
| 6644 | if ((p_style & supported_status) == 0) { |
| 6645 | return false; |
| 6646 | } |
| 6647 | |
| 6648 | return true; |
| 6649 | } |
| 6650 | |
| 6651 | static ::XIMStyle _get_best_xim_style(const ::XIMStyle &p_style_a, const ::XIMStyle &p_style_b) { |
| 6652 | if (p_style_a == 0) { |
| 6653 | return p_style_b; |
| 6654 | } |
| 6655 | if (p_style_b == 0) { |
| 6656 | return p_style_a; |
| 6657 | } |
| 6658 | |
| 6659 | const ::XIMStyle preedit = XIMPreeditArea | XIMPreeditCallbacks | XIMPreeditPosition | XIMPreeditNothing | XIMPreeditNone; |
| 6660 | const ::XIMStyle status = XIMStatusArea | XIMStatusCallbacks | XIMStatusNothing | XIMStatusNone; |
| 6661 | |
| 6662 | ::XIMStyle a = p_style_a & preedit; |
| 6663 | ::XIMStyle b = p_style_b & preedit; |
| 6664 | if (a != b) { |
| 6665 | // Compare preedit styles. |
| 6666 | if ((a | b) & XIMPreeditCallbacks) { |
| 6667 | return a == XIMPreeditCallbacks ? p_style_a : p_style_b; |
| 6668 | } else if ((a | b) & XIMPreeditPosition) { |
| 6669 | return a == XIMPreeditPosition ? p_style_a : p_style_b; |
| 6670 | } else if ((a | b) & XIMPreeditArea) { |
| 6671 | return a == XIMPreeditArea ? p_style_a : p_style_b; |
| 6672 | } else if ((a | b) & XIMPreeditNothing) { |
| 6673 | return a == XIMPreeditNothing ? p_style_a : p_style_b; |
| 6674 | } |
| 6675 | } else { |
| 6676 | // Preedit styles are the same, compare status styles. |
| 6677 | a = p_style_a & status; |
| 6678 | b = p_style_b & status; |
| 6679 | |
| 6680 | if ((a | b) & XIMStatusCallbacks) { |
| 6681 | return a == XIMStatusCallbacks ? p_style_a : p_style_b; |
| 6682 | } else if ((a | b) & XIMStatusArea) { |
| 6683 | return a == XIMStatusArea ? p_style_a : p_style_b; |
| 6684 | } else if ((a | b) & XIMStatusNothing) { |
| 6685 | return a == XIMStatusNothing ? p_style_a : p_style_b; |
| 6686 | } |
| 6687 | } |
| 6688 | return p_style_a; |
| 6689 | } |
| 6690 | |
| 6691 | void DisplayServerX11::_xim_instantiate_callback(::Display *display, ::XPointer client_data, |
| 6692 | ::XPointer call_data) { |
| 6693 | DisplayServerX11 *ds = reinterpret_cast<DisplayServerX11 *>(client_data); |
| 6694 | |
| 6695 | ds->xim = XOpenIM(display, nullptr, nullptr, nullptr); |
| 6696 | |
| 6697 | if (ds->xim == nullptr) { |
| 6698 | WARN_PRINT("XOpenIM failed."); |
| 6699 | ds->xim_style = 0L; |
| 6700 | } else { |
| 6701 | ::XIMCallback im_destroy_callback; |
| 6702 | im_destroy_callback.client_data = client_data; |
| 6703 | im_destroy_callback.callback = (::XIMProc)(_xim_destroy_callback); |
| 6704 | if (XSetIMValues(ds->xim, XNDestroyCallback, &im_destroy_callback, |
| 6705 | nullptr) != nullptr) { |
| 6706 | WARN_PRINT("Error setting XIM destroy callback."); |
| 6707 | } |
| 6708 | |
| 6709 | ::XIMStyles *xim_styles = nullptr; |
| 6710 | ds->xim_style = 0L; |
| 6711 | char *imvalret = XGetIMValues(ds->xim, XNQueryInputStyle, &xim_styles, nullptr); |
| 6712 | if (imvalret != nullptr || xim_styles == nullptr) { |
| 6713 | fprintf(stderr, "Input method doesn't support any styles\n"); |
| 6714 | } |
| 6715 | |
| 6716 | if (xim_styles) { |
| 6717 | ds->xim_style = 0L; |
| 6718 | for (int i = 0; i < xim_styles->count_styles; i++) { |
| 6719 | const ::XIMStyle &style = xim_styles->supported_styles[i]; |
| 6720 | |
| 6721 | if (!_is_xim_style_supported(style)) { |
| 6722 | continue; |
| 6723 | } |
| 6724 | |
| 6725 | ds->xim_style = _get_best_xim_style(ds->xim_style, style); |
| 6726 | } |
| 6727 | |
| 6728 | XFree(xim_styles); |
| 6729 | } |
| 6730 | XFree(imvalret); |
| 6731 | } |
| 6732 | |
| 6733 | // The input method has been (re)started. |
| 6734 | for (KeyValue<WindowID, WindowData> &E : ds->windows) { |
| 6735 | ds->_create_xic(E.value); |
| 6736 | } |
| 6737 | } |
| 6738 | |
| 6739 | DisplayServerX11::DisplayServerX11(const String &p_rendering_driver, WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Context p_context, int64_t p_parent_window, Error &r_error) { |
| 6740 | KeyMappingX11::initialize(); |
| 6741 | |
| 6742 | String current_desk = OS::get_singleton()->get_environment("XDG_CURRENT_DESKTOP").to_lower(); |
| 6743 | String session_desk = OS::get_singleton()->get_environment("XDG_SESSION_DESKTOP").to_lower(); |
| 6744 | swap_cancel_ok = (current_desk.contains("kde") || session_desk.contains("kde") || current_desk.contains("lxqt") || session_desk.contains("lxqt")); |
| 6745 | |
| 6746 | xwayland = OS::get_singleton()->get_environment("XDG_SESSION_TYPE").to_lower() == "wayland"; |
| 6747 | kde5_embed_workaround = current_desk == "kde" && OS::get_singleton()->get_environment("KDE_SESSION_VERSION") == "5"; |
| 6748 | |
| 6749 | native_menu = memnew(NativeMenu); |
| 6750 | context = p_context; |
| 6751 | |
| 6752 | #ifdef SOWRAP_ENABLED |
| 6753 | #ifdef DEBUG_ENABLED |
| 6754 | int dylibloader_verbose = 1; |
| 6755 | #else |
| 6756 | int dylibloader_verbose = 0; |
| 6757 | #endif |
| 6758 | if (initialize_xlib(dylibloader_verbose) != 0) { |
| 6759 | r_error = ERR_UNAVAILABLE; |
| 6760 | ERR_FAIL_MSG("Can't load Xlib dynamically."); |
| 6761 | } |
| 6762 | |
| 6763 | if (initialize_xcursor(dylibloader_verbose) != 0) { |
| 6764 | r_error = ERR_UNAVAILABLE; |
| 6765 | ERR_FAIL_MSG("Can't load XCursor dynamically."); |
| 6766 | } |
| 6767 | #ifdef XKB_ENABLED |
| 6768 | bool xkb_loaded = (initialize_xkbcommon(dylibloader_verbose) == 0); |
| 6769 | xkb_loaded_v05p = xkb_loaded; |
| 6770 | if (!xkb_context_new || !xkb_compose_table_new_from_locale || !xkb_compose_table_unref || !xkb_context_unref || !xkb_compose_state_feed || !xkb_compose_state_unref || !xkb_compose_state_new || !xkb_compose_state_get_status || !xkb_compose_state_get_utf8) { |
| 6771 | xkb_loaded_v05p = false; |
| 6772 | print_verbose("Detected XKBcommon library version older than 0.5, dead key composition and Unicode key labels disabled."); |
| 6773 | } |
| 6774 | xkb_loaded_v08p = xkb_loaded; |
| 6775 | if (!xkb_keysym_to_utf32 || !xkb_keysym_to_upper) { |
| 6776 | xkb_loaded_v08p = false; |
| 6777 | print_verbose("Detected XKBcommon library version older than 0.8, Unicode key labels disabled."); |
| 6778 | } |
| 6779 | #endif |
| 6780 | if (initialize_xext(dylibloader_verbose) != 0) { |
| 6781 | r_error = ERR_UNAVAILABLE; |
| 6782 | ERR_FAIL_MSG("Can't load Xext dynamically."); |
| 6783 | } |
| 6784 | |
| 6785 | if (initialize_xinerama(dylibloader_verbose) != 0) { |
| 6786 | xinerama_ext_ok = false; |
| 6787 | } |
| 6788 | |
| 6789 | if (initialize_xrandr(dylibloader_verbose) != 0) { |
| 6790 | xrandr_ext_ok = false; |
| 6791 | } |
| 6792 | |
| 6793 | if (initialize_xrender(dylibloader_verbose) != 0) { |
| 6794 | r_error = ERR_UNAVAILABLE; |
| 6795 | ERR_FAIL_MSG("Can't load Xrender dynamically."); |
| 6796 | } |
| 6797 | |
| 6798 | if (initialize_xinput2(dylibloader_verbose) != 0) { |
| 6799 | r_error = ERR_UNAVAILABLE; |
| 6800 | ERR_FAIL_MSG("Can't load Xinput2 dynamically."); |
| 6801 | } |
| 6802 | #else |
| 6803 | #ifdef XKB_ENABLED |
| 6804 | bool xkb_loaded = true; |
| 6805 | xkb_loaded_v05p = true; |
| 6806 | xkb_loaded_v08p = true; |
| 6807 | #endif |
| 6808 | #endif |
| 6809 | |
| 6810 | #ifdef XKB_ENABLED |
| 6811 | if (xkb_loaded) { |
| 6812 | xkb_ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS); |
| 6813 | if (xkb_ctx) { |
| 6814 | const char *locale = getenv("LC_ALL"); |
| 6815 | if (!locale || !*locale) { |
| 6816 | locale = getenv("LC_CTYPE"); |
| 6817 | } |
| 6818 | if (!locale || !*locale) { |
| 6819 | locale = getenv("LANG"); |
| 6820 | } |
| 6821 | if (!locale || !*locale) { |
| 6822 | locale = "C"; |
| 6823 | } |
| 6824 | dead_tbl = xkb_compose_table_new_from_locale(xkb_ctx, locale, XKB_COMPOSE_COMPILE_NO_FLAGS); |
| 6825 | } |
| 6826 | } |
| 6827 | #endif |
| 6828 | |
| 6829 | Input::get_singleton()->set_event_dispatch_function(_dispatch_input_events); |
| 6830 | |
| 6831 | r_error = OK; |
| 6832 | |
| 6833 | #ifdef SOWRAP_ENABLED |
| 6834 | { |
| 6835 | if (!XcursorImageCreate || !XcursorImageLoadCursor || !XcursorImageDestroy || !XcursorGetDefaultSize || !XcursorGetTheme || !XcursorLibraryLoadImage) { |
| 6836 | // There's no API to check version, check if functions are available instead. |
| 6837 | ERR_PRINT("Unsupported Xcursor library version."); |
| 6838 | r_error = ERR_UNAVAILABLE; |
| 6839 | return; |
| 6840 | } |
| 6841 | } |
| 6842 | #endif |
| 6843 | |
| 6844 | for (int i = 0; i < CURSOR_MAX; i++) { |
| 6845 | cursors[i] = None; |
| 6846 | cursor_img[i] = nullptr; |
| 6847 | } |
| 6848 | |
| 6849 | XInitThreads(); //always use threads |
| 6850 | |
| 6851 | /** XLIB INITIALIZATION **/ |
| 6852 | x11_display = XOpenDisplay(nullptr); |
| 6853 | |
| 6854 | if (!x11_display) { |
| 6855 | ERR_PRINT("X11 Display is not available"); |
| 6856 | r_error = ERR_UNAVAILABLE; |
| 6857 | return; |
| 6858 | } |
| 6859 | |
| 6860 | if (xshaped_ext_ok) { |
| 6861 | int version_major = 0; |
| 6862 | int version_minor = 0; |
| 6863 | int rc = XShapeQueryVersion(x11_display, &version_major, &version_minor); |
| 6864 | print_verbose(vformat("Xshape %d.%d detected.", version_major, version_minor)); |
| 6865 | if (rc != 1 || version_major < 1) { |
| 6866 | xshaped_ext_ok = false; |
| 6867 | print_verbose("Unsupported Xshape library version."); |
| 6868 | } |
| 6869 | } |
| 6870 | |
| 6871 | if (xinerama_ext_ok) { |
| 6872 | int version_major = 0; |
| 6873 | int version_minor = 0; |
| 6874 | int rc = XineramaQueryVersion(x11_display, &version_major, &version_minor); |
| 6875 | print_verbose(vformat("Xinerama %d.%d detected.", version_major, version_minor)); |
| 6876 | if (rc != 1 || version_major < 1) { |
| 6877 | xinerama_ext_ok = false; |
| 6878 | print_verbose("Unsupported Xinerama library version."); |
| 6879 | } |
| 6880 | } |
| 6881 | |
| 6882 | if (xrandr_ext_ok) { |
| 6883 | int version_major = 0; |
| 6884 | int version_minor = 0; |
| 6885 | int rc = XRRQueryVersion(x11_display, &version_major, &version_minor); |
| 6886 | print_verbose(vformat("Xrandr %d.%d detected.", version_major, version_minor)); |
| 6887 | if (rc != 1 || (version_major == 1 && version_minor < 3) || (version_major < 1)) { |
| 6888 | xrandr_ext_ok = false; |
| 6889 | print_verbose("Unsupported Xrandr library version."); |
| 6890 | } |
| 6891 | } |
| 6892 | |
| 6893 | { |
| 6894 | int version_major = 0; |
| 6895 | int version_minor = 0; |
| 6896 | int rc = XRenderQueryVersion(x11_display, &version_major, &version_minor); |
| 6897 | print_verbose(vformat("Xrender %d.%d detected.", version_major, version_minor)); |
| 6898 | if (rc != 1 || (version_major == 0 && version_minor < 11)) { |
| 6899 | ERR_PRINT("Unsupported Xrender library version."); |
| 6900 | r_error = ERR_UNAVAILABLE; |
| 6901 | XCloseDisplay(x11_display); |
| 6902 | return; |
| 6903 | } |
| 6904 | } |
| 6905 | |
| 6906 | { |
| 6907 | int version_major = 2; // Report 2.2 as supported by engine, but should work with 2.1 or 2.0 library as well. |
| 6908 | int version_minor = 2; |
| 6909 | int rc = XIQueryVersion(x11_display, &version_major, &version_minor); |
| 6910 | print_verbose(vformat("Xinput %d.%d detected.", version_major, version_minor)); |
| 6911 | if (rc != Success || (version_major < 2)) { |
| 6912 | ERR_PRINT("Unsupported Xinput2 library version."); |
| 6913 | r_error = ERR_UNAVAILABLE; |
| 6914 | XCloseDisplay(x11_display); |
| 6915 | return; |
| 6916 | } |
| 6917 | } |
| 6918 | |
| 6919 | char *modifiers = nullptr; |
| 6920 | Bool xkb_dar = False; |
| 6921 | XAutoRepeatOn(x11_display); |
| 6922 | xkb_dar = XkbSetDetectableAutoRepeat(x11_display, True, nullptr); |
| 6923 | |
| 6924 | // Try to support IME if detectable auto-repeat is supported |
| 6925 | if (xkb_dar == True) { |
| 6926 | #ifdef X_HAVE_UTF8_STRING |
| 6927 | // Xutf8LookupString will be used later instead of XmbLookupString before |
| 6928 | // the multibyte sequences can be converted to unicode string. |
| 6929 | modifiers = XSetLocaleModifiers(""); |
| 6930 | #endif |
| 6931 | } |
| 6932 | |
| 6933 | if (modifiers == nullptr) { |
| 6934 | if (OS::get_singleton()->is_stdout_verbose()) { |
| 6935 | WARN_PRINT("IME is disabled."); |
| 6936 | } |
| 6937 | XSetLocaleModifiers("@im=none"); |
| 6938 | WARN_PRINT("Error setting locale modifiers."); |
| 6939 | } |
| 6940 | |
| 6941 | const char *err; |
| 6942 | int xrandr_major = 0; |
| 6943 | int xrandr_minor = 0; |
| 6944 | int event_base, error_base; |
| 6945 | xrandr_ext_ok = XRRQueryExtension(x11_display, &event_base, &error_base); |
| 6946 | xrandr_handle = dlopen("libXrandr.so.2", RTLD_LAZY); |
| 6947 | if (!xrandr_handle) { |
| 6948 | err = dlerror(); |
| 6949 | // For some arcane reason, NetBSD now ships libXrandr.so.3 while the rest of the world has libXrandr.so.2... |
| 6950 | // In case this happens for other X11 platforms in the future, let's give it a try too before failing. |
| 6951 | xrandr_handle = dlopen("libXrandr.so.3", RTLD_LAZY); |
| 6952 | if (!xrandr_handle) { |
| 6953 | fprintf(stderr, "could not load libXrandr.so.2, Error: %s\n", err); |
| 6954 | } |
| 6955 | } |
| 6956 | |
| 6957 | if (xrandr_handle) { |
| 6958 | XRRQueryVersion(x11_display, &xrandr_major, &xrandr_minor); |
| 6959 | if (((xrandr_major << 8) | xrandr_minor) >= 0x0105) { |
| 6960 | xrr_get_monitors = (xrr_get_monitors_t)dlsym(xrandr_handle, "XRRGetMonitors"); |
| 6961 | if (!xrr_get_monitors) { |
| 6962 | err = dlerror(); |
| 6963 | fprintf(stderr, "could not find symbol XRRGetMonitors\nError: %s\n", err); |
| 6964 | } else { |
| 6965 | xrr_free_monitors = (xrr_free_monitors_t)dlsym(xrandr_handle, "XRRFreeMonitors"); |
| 6966 | if (!xrr_free_monitors) { |
| 6967 | err = dlerror(); |
| 6968 | fprintf(stderr, "could not find XRRFreeMonitors\nError: %s\n", err); |
| 6969 | xrr_get_monitors = nullptr; |
| 6970 | } |
| 6971 | } |
| 6972 | } |
| 6973 | } |
| 6974 | |
| 6975 | if (!_refresh_device_info()) { |
| 6976 | OS::get_singleton()->alert("Your system does not support XInput 2.\n" |
| 6977 | "Please upgrade your distribution.", |
| 6978 | "Unable to initialize XInput"); |
| 6979 | r_error = ERR_UNAVAILABLE; |
| 6980 | return; |
| 6981 | } |
| 6982 | |
| 6983 | if (!XRegisterIMInstantiateCallback(x11_display, nullptr, nullptr, nullptr, (::XIDProc)(_xim_instantiate_callback), (::XPointer)this)) { |
| 6984 | WARN_PRINT("Error registering XIM instantiate callback."); |
| 6985 | _xim_instantiate_callback(x11_display, (::XPointer)this, nullptr); |
| 6986 | } |
| 6987 | |
| 6988 | /* Atom internment */ |
| 6989 | wm_delete = XInternAtom(x11_display, "WM_DELETE_WINDOW", true); |
| 6990 | // Set Xdnd (drag & drop) support. |
| 6991 | xdnd_aware = XInternAtom(x11_display, "XdndAware", False); |
| 6992 | xdnd_enter = XInternAtom(x11_display, "XdndEnter", False); |
| 6993 | xdnd_position = XInternAtom(x11_display, "XdndPosition", False); |
| 6994 | xdnd_status = XInternAtom(x11_display, "XdndStatus", False); |
| 6995 | xdnd_action_copy = XInternAtom(x11_display, "XdndActionCopy", False); |
| 6996 | xdnd_drop = XInternAtom(x11_display, "XdndDrop", False); |
| 6997 | xdnd_finished = XInternAtom(x11_display, "XdndFinished", False); |
| 6998 | xdnd_selection = XInternAtom(x11_display, "XdndSelection", False); |
| 6999 | |
| 7000 | #ifdef SPEECHD_ENABLED |
| 7001 | // Init TTS |
| 7002 | bool tts_enabled = GLOBAL_GET("audio/general/text_to_speech"); |
| 7003 | if (tts_enabled) { |
| 7004 | initialize_tts(); |
| 7005 | } |
| 7006 | #endif |
| 7007 | |
| 7008 | #ifdef ACCESSKIT_ENABLED |
| 7009 | if (accessibility_get_mode() != DisplayServer::AccessibilityMode::ACCESSIBILITY_DISABLED) { |
| 7010 | accessibility_driver = memnew(AccessibilityDriverAccessKit); |
| 7011 | if (accessibility_driver->init() != OK) { |
| 7012 | memdelete(accessibility_driver); |
| 7013 | accessibility_driver = nullptr; |
| 7014 | } |
| 7015 | } |
| 7016 | #endif |
| 7017 | |
| 7018 | //!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 7019 | //TODO - do Vulkan and OpenGL support checks, driver selection and fallback |
| 7020 | rendering_driver = p_rendering_driver; |
| 7021 | |
| 7022 | bool driver_found = false; |
| 7023 | String executable_name = OS::get_singleton()->get_executable_path().get_file(); |
| 7024 | |
| 7025 | // Initialize context and rendering device. |
| 7026 | |
| 7027 | if (rendering_driver == "dummy") { |
| 7028 | RasterizerDummy::make_current(); |
| 7029 | driver_found = true; |
| 7030 | } |
| 7031 | |
| 7032 | #if defined(RD_ENABLED) |
| 7033 | #if defined(VULKAN_ENABLED) |
| 7034 | if (rendering_driver == "vulkan") { |
| 7035 | rendering_context = memnew(RenderingContextDriverVulkanX11); |
| 7036 | } |
| 7037 | #endif // VULKAN_ENABLED |
| 7038 | |
| 7039 | if (rendering_context) { |
| 7040 | if (rendering_context->initialize() != OK) { |
| 7041 | memdelete(rendering_context); |
| 7042 | rendering_context = nullptr; |
| 7043 | #if defined(GLES3_ENABLED) |
| 7044 | bool fallback_to_opengl3 = GLOBAL_GET("rendering/rendering_device/fallback_to_opengl3"); |
| 7045 | if (fallback_to_opengl3 && rendering_driver != "opengl3") { |
| 7046 | WARN_PRINT("Your video card drivers seem not to support the required Vulkan version, switching to OpenGL 3."); |
| 7047 | rendering_driver = "opengl3"; |
| 7048 | OS::get_singleton()->set_current_rendering_method("gl_compatibility"); |
| 7049 | OS::get_singleton()->set_current_rendering_driver_name(rendering_driver); |
| 7050 | } else |
| 7051 | #endif // GLES3_ENABLED |
| 7052 | { |
| 7053 | r_error = ERR_CANT_CREATE; |
| 7054 | |
| 7055 | if (p_rendering_driver == "vulkan") { |
| 7056 | OS::get_singleton()->alert( |
| 7057 | vformat("Your video card drivers seem not to support the required Vulkan version.\n\n" |
| 7058 | "If possible, consider updating your video card drivers or using the OpenGL 3 driver.\n\n" |
| 7059 | "You can enable the OpenGL 3 driver by starting the engine from the\n" |
| 7060 | "command line with the command:\n\n \"%s\" --rendering-driver opengl3\n\n" |
| 7061 | "If you recently updated your video card drivers, try rebooting.", |
| 7062 | executable_name), |
| 7063 | "Unable to initialize Vulkan video driver"); |
| 7064 | } |
| 7065 | |
| 7066 | ERR_FAIL_MSG(vformat("Could not initialize %s", rendering_driver)); |
| 7067 | } |
| 7068 | } |
| 7069 | driver_found = true; |
| 7070 | } |
| 7071 | #endif // RD_ENABLED |
| 7072 | |
| 7073 | #if defined(GLES3_ENABLED) |
| 7074 | if (rendering_driver == "opengl3" || rendering_driver == "opengl3_es") { |
| 7075 | if (getenv("DRI_PRIME") == nullptr) { |
| 7076 | int use_prime = -1; |
| 7077 | |
| 7078 | if (getenv("PRIMUS_DISPLAY") || |
| 7079 | getenv("PRIMUS_libGLd") || |
| 7080 | getenv("PRIMUS_libGLa") || |
| 7081 | getenv("PRIMUS_libGL") || |
| 7082 | getenv("PRIMUS_LOAD_GLOBAL") || |
| 7083 | getenv("BUMBLEBEE_SOCKET")) { |
| 7084 | print_verbose("Optirun/primusrun detected. Skipping GPU detection"); |
| 7085 | use_prime = 0; |
| 7086 | } |
| 7087 | |
| 7088 | // Some tools use fake libGL libraries and have them override the real one using |
| 7089 | // LD_LIBRARY_PATH, so we skip them. *But* Steam also sets LD_LIBRARY_PATH for its |
| 7090 | // runtime and includes system `/lib` and `/lib64`... so ignore Steam. |
| 7091 | if (use_prime == -1 && getenv("LD_LIBRARY_PATH") && !getenv("STEAM_RUNTIME_LIBRARY_PATH")) { |
| 7092 | String ld_library_path(getenv("LD_LIBRARY_PATH")); |
| 7093 | Vector<String> libraries = ld_library_path.split(":"); |
| 7094 | |
| 7095 | for (int i = 0; i < libraries.size(); ++i) { |
| 7096 | if (FileAccess::exists(libraries[i] + "/libGL.so.1") || |
| 7097 | FileAccess::exists(libraries[i] + "/libGL.so")) { |
| 7098 | print_verbose("Custom libGL override detected. Skipping GPU detection"); |
| 7099 | use_prime = 0; |
| 7100 | } |
| 7101 | } |
| 7102 | } |
| 7103 | |
| 7104 | if (use_prime == -1) { |
| 7105 | print_verbose("Detecting GPUs, set DRI_PRIME in the environment to override GPU detection logic."); |
| 7106 | use_prime = DetectPrimeX11::detect_prime(); |
| 7107 | } |
| 7108 | |
| 7109 | if (use_prime) { |
| 7110 | print_line("Found discrete GPU, setting DRI_PRIME=1 to use it."); |
| 7111 | print_line("Note: Set DRI_PRIME=0 in the environment to disable Godot from using the discrete GPU."); |
| 7112 | setenv("DRI_PRIME", "1", 1); |
| 7113 | } |
| 7114 | } |
| 7115 | } |
| 7116 | if (rendering_driver == "opengl3") { |
| 7117 | gl_manager = memnew(GLManager_X11(p_resolution, GLManager_X11::GLES_3_0_COMPATIBLE)); |
| 7118 | if (gl_manager->initialize(x11_display) != OK || gl_manager->open_display(x11_display) != OK) { |
| 7119 | memdelete(gl_manager); |
| 7120 | gl_manager = nullptr; |
| 7121 | bool fallback = GLOBAL_GET("rendering/gl_compatibility/fallback_to_gles"); |
| 7122 | if (fallback) { |
| 7123 | WARN_PRINT("Your video card drivers seem not to support the required OpenGL version, switching to OpenGLES."); |
| 7124 | rendering_driver = "opengl3_es"; |
| 7125 | OS::get_singleton()->set_current_rendering_driver_name(rendering_driver); |
| 7126 | } else { |
| 7127 | r_error = ERR_UNAVAILABLE; |
| 7128 | |
| 7129 | OS::get_singleton()->alert( |
| 7130 | vformat("Your video card drivers seem not to support the required OpenGL 3.3 version.\n\n" |
| 7131 | "If possible, consider updating your video card drivers or using the Vulkan driver.\n\n" |
| 7132 | "You can enable the Vulkan driver by starting the engine from the\n" |
| 7133 | "command line with the command:\n\n \"%s\" --rendering-driver vulkan\n\n" |
| 7134 | "If you recently updated your video card drivers, try rebooting.", |
| 7135 | executable_name), |
| 7136 | "Unable to initialize OpenGL video driver"); |
| 7137 | |
| 7138 | ERR_FAIL_MSG("Could not initialize OpenGL."); |
| 7139 | } |
| 7140 | } else { |
| 7141 | driver_found = true; |
| 7142 | RasterizerGLES3::make_current(true); |
| 7143 | } |
| 7144 | } |
| 7145 | |
| 7146 | if (rendering_driver == "opengl3_es") { |
| 7147 | gl_manager_egl = memnew(GLManagerEGL_X11); |
| 7148 | if (gl_manager_egl->initialize() != OK || gl_manager_egl->open_display(x11_display) != OK) { |
| 7149 | memdelete(gl_manager_egl); |
| 7150 | gl_manager_egl = nullptr; |
| 7151 | r_error = ERR_UNAVAILABLE; |
| 7152 | |
| 7153 | OS::get_singleton()->alert( |
| 7154 | "Your video card drivers seem not to support the required OpenGL ES 3.0 version.\n\n" |
| 7155 | "If possible, consider updating your video card drivers.\n\n" |
| 7156 | "If you recently updated your video card drivers, try rebooting.", |
| 7157 | "Unable to initialize OpenGL ES video driver"); |
| 7158 | |
| 7159 | ERR_FAIL_MSG("Could not initialize OpenGL ES."); |
| 7160 | } |
| 7161 | driver_found = true; |
| 7162 | RasterizerGLES3::make_current(false); |
| 7163 | } |
| 7164 | |
| 7165 | #endif // GLES3_ENABLED |
| 7166 | |
| 7167 | if (!driver_found) { |
| 7168 | r_error = ERR_UNAVAILABLE; |
| 7169 | ERR_FAIL_MSG("Video driver not found."); |
| 7170 | } |
| 7171 | |
| 7172 | Point2i window_position; |
| 7173 | if (p_position != nullptr) { |
| 7174 | window_position = *p_position; |
| 7175 | } else { |
| 7176 | if (p_screen == SCREEN_OF_MAIN_WINDOW) { |
| 7177 | p_screen = SCREEN_PRIMARY; |
| 7178 | } |
| 7179 | Rect2i scr_rect = screen_get_usable_rect(p_screen); |
| 7180 | window_position = scr_rect.position + (scr_rect.size - p_resolution) / 2; |
| 7181 | } |
| 7182 | |
| 7183 | WindowID main_window = _create_window(p_mode, p_vsync_mode, p_flags, Rect2i(window_position, p_resolution), p_parent_window); |
| 7184 | if (main_window == INVALID_WINDOW_ID) { |
| 7185 | r_error = ERR_CANT_CREATE; |
| 7186 | return; |
| 7187 | } |
| 7188 | for (int i = 0; i < WINDOW_FLAG_MAX; i++) { |
| 7189 | if (p_flags & (1 << i)) { |
| 7190 | window_set_flag(WindowFlags(i), true, main_window); |
| 7191 | } |
| 7192 | } |
| 7193 | |
| 7194 | #if defined(RD_ENABLED) |
| 7195 | if (rendering_context) { |
| 7196 | rendering_device = memnew(RenderingDevice); |
| 7197 | if (rendering_device->initialize(rendering_context, MAIN_WINDOW_ID) != OK) { |
| 7198 | memdelete(rendering_device); |
| 7199 | rendering_device = nullptr; |
| 7200 | memdelete(rendering_context); |
| 7201 | rendering_context = nullptr; |
| 7202 | r_error = ERR_UNAVAILABLE; |
| 7203 | return; |
| 7204 | } |
| 7205 | rendering_device->screen_create(MAIN_WINDOW_ID); |
| 7206 | |
| 7207 | RendererCompositorRD::make_current(); |
| 7208 | } |
| 7209 | #endif // RD_ENABLED |
| 7210 | |
| 7211 | { |
| 7212 | //set all event master mask |
| 7213 | XIEventMask all_master_event_mask; |
| 7214 | static unsigned char all_master_mask_data[XIMaskLen(XI_LASTEVENT)] = {}; |
| 7215 | all_master_event_mask.deviceid = XIAllMasterDevices; |
| 7216 | all_master_event_mask.mask_len = sizeof(all_master_mask_data); |
| 7217 | all_master_event_mask.mask = all_master_mask_data; |
| 7218 | XISetMask(all_master_event_mask.mask, XI_DeviceChanged); |
| 7219 | XISetMask(all_master_event_mask.mask, XI_RawMotion); |
| 7220 | XISelectEvents(x11_display, DefaultRootWindow(x11_display), &all_master_event_mask, 1); |
| 7221 | } |
| 7222 | |
| 7223 | cursor_size = XcursorGetDefaultSize(x11_display); |
| 7224 | cursor_theme = XcursorGetTheme(x11_display); |
| 7225 | |
| 7226 | if (!cursor_theme) { |
| 7227 | print_verbose("XcursorGetTheme could not get cursor theme"); |
| 7228 | cursor_theme = "default"; |
| 7229 | } |
| 7230 | |
| 7231 | for (int i = 0; i < CURSOR_MAX; i++) { |
| 7232 | static const char *cursor_file[] = { |
| 7233 | "left_ptr", |
| 7234 | "xterm", |
| 7235 | "hand2", |
| 7236 | "cross", |
| 7237 | "watch", |
| 7238 | "left_ptr_watch", |
| 7239 | "fleur", |
| 7240 | "dnd-move", |
| 7241 | "crossed_circle", |
| 7242 | "v_double_arrow", |
| 7243 | "h_double_arrow", |
| 7244 | "size_bdiag", |
| 7245 | "size_fdiag", |
| 7246 | "move", |
| 7247 | "row_resize", |
| 7248 | "col_resize", |
| 7249 | "question_arrow" |
| 7250 | }; |
| 7251 | |
| 7252 | cursor_img[i] = XcursorLibraryLoadImage(cursor_file[i], cursor_theme, cursor_size); |
| 7253 | if (!cursor_img[i]) { |
| 7254 | const char *fallback = nullptr; |
| 7255 | |
| 7256 | switch (i) { |
| 7257 | case CURSOR_POINTING_HAND: |
| 7258 | fallback = "pointer"; |
| 7259 | break; |
| 7260 | case CURSOR_CROSS: |
| 7261 | fallback = "crosshair"; |
| 7262 | break; |
| 7263 | case CURSOR_WAIT: |
| 7264 | fallback = "wait"; |
| 7265 | break; |
| 7266 | case CURSOR_BUSY: |
| 7267 | fallback = "progress"; |
| 7268 | break; |
| 7269 | case CURSOR_DRAG: |
| 7270 | fallback = "grabbing"; |
| 7271 | break; |
| 7272 | case CURSOR_CAN_DROP: |
| 7273 | fallback = "hand1"; |
| 7274 | break; |
| 7275 | case CURSOR_FORBIDDEN: |
| 7276 | fallback = "forbidden"; |
| 7277 | break; |
| 7278 | case CURSOR_VSIZE: |
| 7279 | fallback = "ns-resize"; |
| 7280 | break; |
| 7281 | case CURSOR_HSIZE: |
| 7282 | fallback = "ew-resize"; |
| 7283 | break; |
| 7284 | case CURSOR_BDIAGSIZE: |
| 7285 | fallback = "fd_double_arrow"; |
| 7286 | break; |
| 7287 | case CURSOR_FDIAGSIZE: |
| 7288 | fallback = "bd_double_arrow"; |
| 7289 | break; |
| 7290 | case CURSOR_MOVE: |
| 7291 | cursor_img[i] = cursor_img[CURSOR_DRAG]; |
| 7292 | break; |
| 7293 | case CURSOR_VSPLIT: |
| 7294 | fallback = "sb_v_double_arrow"; |
| 7295 | break; |
| 7296 | case CURSOR_HSPLIT: |
| 7297 | fallback = "sb_h_double_arrow"; |
| 7298 | break; |
| 7299 | case CURSOR_HELP: |
| 7300 | fallback = "help"; |
| 7301 | break; |
| 7302 | } |
| 7303 | if (fallback != nullptr) { |
| 7304 | cursor_img[i] = XcursorLibraryLoadImage(fallback, cursor_theme, cursor_size); |
| 7305 | } |
| 7306 | } |
| 7307 | if (cursor_img[i]) { |
| 7308 | cursors[i] = XcursorImageLoadCursor(x11_display, cursor_img[i]); |
| 7309 | } else { |
| 7310 | print_verbose("Failed loading custom cursor: " + String(cursor_file[i])); |
| 7311 | } |
| 7312 | } |
| 7313 | |
| 7314 | { |
| 7315 | // Creating an empty/transparent cursor |
| 7316 | |
| 7317 | // Create 1x1 bitmap |
| 7318 | Pixmap cursormask = XCreatePixmap(x11_display, |
| 7319 | RootWindow(x11_display, DefaultScreen(x11_display)), 1, 1, 1); |
| 7320 | |
| 7321 | // Fill with zero |
| 7322 | XGCValues xgc; |
| 7323 | xgc.function = GXclear; |
| 7324 | GC gc = XCreateGC(x11_display, cursormask, GCFunction, &xgc); |
| 7325 | XFillRectangle(x11_display, cursormask, gc, 0, 0, 1, 1); |
| 7326 | |
| 7327 | // Color value doesn't matter. Mask zero means no foreground or background will be drawn |
| 7328 | XColor col = {}; |
| 7329 | |
| 7330 | Cursor cursor = XCreatePixmapCursor(x11_display, |
| 7331 | cursormask, // source (using cursor mask as placeholder, since it'll all be ignored) |
| 7332 | cursormask, // mask |
| 7333 | &col, &col, 0, 0); |
| 7334 | |
| 7335 | XFreePixmap(x11_display, cursormask); |
| 7336 | XFreeGC(x11_display, gc); |
| 7337 | |
| 7338 | if (cursor == None) { |
| 7339 | ERR_PRINT("FAILED CREATING CURSOR"); |
| 7340 | } |
| 7341 | |
| 7342 | null_cursor = cursor; |
| 7343 | } |
| 7344 | cursor_set_shape(CURSOR_BUSY); |
| 7345 | |
| 7346 | // Search the X11 event queue for ConfigureNotify events and process all |
| 7347 | // that are currently queued early, so we can get the final window size |
| 7348 | // for correctly drawing of the bootsplash. |
| 7349 | XEvent config_event; |
| 7350 | while (XCheckTypedEvent(x11_display, ConfigureNotify, &config_event)) { |
| 7351 | _window_changed(&config_event); |
| 7352 | } |
| 7353 | events_thread.start(_poll_events_thread, this); |
| 7354 | |
| 7355 | _update_real_mouse_position(windows[MAIN_WINDOW_ID]); |
| 7356 | |
| 7357 | #ifdef DBUS_ENABLED |
| 7358 | bool dbus_ok = true; |
| 7359 | #ifdef SOWRAP_ENABLED |
| 7360 | if (initialize_dbus(dylibloader_verbose) != 0) { |
| 7361 | print_verbose("Failed to load DBus library!"); |
| 7362 | dbus_ok = false; |
| 7363 | } |
| 7364 | #endif |
| 7365 | if (dbus_ok) { |
| 7366 | bool ver_ok = false; |
| 7367 | int version_major = 0; |
| 7368 | int version_minor = 0; |
| 7369 | int version_rev = 0; |
| 7370 | dbus_get_version(&version_major, &version_minor, &version_rev); |
| 7371 | ver_ok = (version_major == 1 && version_minor >= 10) || (version_major > 1); // 1.10.0 |
| 7372 | print_verbose(vformat("DBus %d.%d.%d detected.", version_major, version_minor, version_rev)); |
| 7373 | if (!ver_ok) { |
| 7374 | print_verbose("Unsupported DBus library version!"); |
| 7375 | dbus_ok = false; |
| 7376 | } |
| 7377 | } |
| 7378 | if (dbus_ok) { |
| 7379 | screensaver = memnew(FreeDesktopScreenSaver); |
| 7380 | portal_desktop = memnew(FreeDesktopPortalDesktop); |
| 7381 | atspi_monitor = memnew(FreeDesktopAtSPIMonitor); |
| 7382 | } |
| 7383 | #endif // DBUS_ENABLED |
| 7384 | |
| 7385 | screen_set_keep_on(GLOBAL_GET("display/window/energy_saving/keep_screen_on")); |
| 7386 | |
| 7387 | XSetErrorHandler(&default_window_error_handler); |
| 7388 | |
| 7389 | r_error = OK; |
| 7390 | } |
| 7391 | |
| 7392 | DisplayServerX11::~DisplayServerX11() { |
| 7393 | // Send owned clipboard data to clipboard manager before exit. |
| 7394 | Window x11_main_window = windows[MAIN_WINDOW_ID].x11_window; |
| 7395 | _clipboard_transfer_ownership(XA_PRIMARY, x11_main_window); |
| 7396 | _clipboard_transfer_ownership(XInternAtom(x11_display, "CLIPBOARD", 0), x11_main_window); |
| 7397 | |
| 7398 | events_thread_done.set(); |
| 7399 | events_thread.wait_to_finish(); |
| 7400 | |
| 7401 | if (native_menu) { |
| 7402 | memdelete(native_menu); |
| 7403 | native_menu = nullptr; |
| 7404 | } |
| 7405 | |
| 7406 | //destroy all windows |
| 7407 | for (KeyValue<WindowID, WindowData> &E : windows) { |
| 7408 | #if defined(RD_ENABLED) |
| 7409 | if (rendering_device) { |
| 7410 | rendering_device->screen_free(E.key); |
| 7411 | } |
| 7412 | |
| 7413 | if (rendering_context) { |
| 7414 | rendering_context->window_destroy(E.key); |
| 7415 | } |
| 7416 | #endif |
| 7417 | #ifdef GLES3_ENABLED |
| 7418 | if (gl_manager) { |
| 7419 | gl_manager->window_destroy(E.key); |
| 7420 | } |
| 7421 | if (gl_manager_egl) { |
| 7422 | gl_manager_egl->window_destroy(E.key); |
| 7423 | } |
| 7424 | #endif |
| 7425 | |
| 7426 | #ifdef ACCESSKIT_ENABLED |
| 7427 | if (accessibility_driver) { |
| 7428 | accessibility_driver->window_destroy(E.key); |
| 7429 | } |
| 7430 | #endif |
| 7431 | |
| 7432 | WindowData &wd = E.value; |
| 7433 | if (wd.xic) { |
| 7434 | XDestroyIC(wd.xic); |
| 7435 | wd.xic = nullptr; |
| 7436 | } |
| 7437 | XDestroyWindow(x11_display, wd.x11_xim_window); |
| 7438 | #ifdef XKB_ENABLED |
| 7439 | if (xkb_loaded_v05p) { |
| 7440 | if (wd.xkb_state) { |
| 7441 | xkb_compose_state_unref(wd.xkb_state); |
| 7442 | wd.xkb_state = nullptr; |
| 7443 | } |
| 7444 | } |
| 7445 | #endif |
| 7446 | XUnmapWindow(x11_display, wd.x11_window); |
| 7447 | XDestroyWindow(x11_display, wd.x11_window); |
| 7448 | } |
| 7449 | |
| 7450 | #ifdef XKB_ENABLED |
| 7451 | if (xkb_loaded_v05p) { |
| 7452 | if (dead_tbl) { |
| 7453 | xkb_compose_table_unref(dead_tbl); |
| 7454 | } |
| 7455 | if (xkb_ctx) { |
| 7456 | xkb_context_unref(xkb_ctx); |
| 7457 | } |
| 7458 | } |
| 7459 | #endif |
| 7460 | |
| 7461 | //destroy drivers |
| 7462 | #if defined(RD_ENABLED) |
| 7463 | if (rendering_device) { |
| 7464 | memdelete(rendering_device); |
| 7465 | rendering_device = nullptr; |
| 7466 | } |
| 7467 | |
| 7468 | if (rendering_context) { |
| 7469 | memdelete(rendering_context); |
| 7470 | rendering_context = nullptr; |
| 7471 | } |
| 7472 | #endif |
| 7473 | |
| 7474 | #ifdef GLES3_ENABLED |
| 7475 | if (gl_manager) { |
| 7476 | memdelete(gl_manager); |
| 7477 | gl_manager = nullptr; |
| 7478 | } |
| 7479 | if (gl_manager_egl) { |
| 7480 | memdelete(gl_manager_egl); |
| 7481 | gl_manager_egl = nullptr; |
| 7482 | } |
| 7483 | #endif |
| 7484 | |
| 7485 | if (xrandr_handle) { |
| 7486 | dlclose(xrandr_handle); |
| 7487 | } |
| 7488 | |
| 7489 | for (int i = 0; i < CURSOR_MAX; i++) { |
| 7490 | if (cursors[i] != None) { |
| 7491 | XFreeCursor(x11_display, cursors[i]); |
| 7492 | } |
| 7493 | if (cursor_img[i] != nullptr) { |
| 7494 | XcursorImageDestroy(cursor_img[i]); |
| 7495 | } |
| 7496 | } |
| 7497 | |
| 7498 | if (xim) { |
| 7499 | XCloseIM(xim); |
| 7500 | } |
| 7501 | |
| 7502 | XCloseDisplay(x11_display); |
| 7503 | if (xmbstring) { |
| 7504 | memfree(xmbstring); |
| 7505 | } |
| 7506 | #ifdef ACCESSKIT_ENABLED |
| 7507 | if (accessibility_driver) { |
| 7508 | memdelete(accessibility_driver); |
| 7509 | } |
| 7510 | #endif |
| 7511 | #ifdef SPEECHD_ENABLED |
| 7512 | if (tts) { |
| 7513 | memdelete(tts); |
| 7514 | } |
| 7515 | #endif |
| 7516 | |
| 7517 | #ifdef DBUS_ENABLED |
| 7518 | if (screensaver) { |
| 7519 | memdelete(screensaver); |
| 7520 | } |
| 7521 | if (portal_desktop) { |
| 7522 | memdelete(portal_desktop); |
| 7523 | } |
| 7524 | if (atspi_monitor) { |
| 7525 | memdelete(atspi_monitor); |
| 7526 | } |
| 7527 | #endif |
| 7528 | } |
| 7529 | |
| 7530 | void DisplayServerX11::register_x11_driver() { |
| 7531 | register_create_function("x11", create_func, get_rendering_drivers_func); |
| 7532 | } |
| 7533 | |
| 7534 | #endif // X11 enabled |