Commit graph

421 commits

Author SHA1 Message Date
Frank Praznik 8cafde5ecc wayland: Add high-resolution event timestamp support
Add the protocol for high-resolution timestamp events and subscribe to them if available.

Event timestamps are now handled natively in nanoseconds as much as possible to avoid error-prone conversions.

Variables have been appended with _ms or _ns where appropriate, to avoid ambiguity.
2022-12-04 16:56:26 -08:00
Frank Praznik 1f40b9de96 wayland: Pass event timestamps to SDL
SDL events support system timestamps now, so pass them through.
2022-12-03 21:17:35 -08:00
Sam Lantinga 0a3262e819 Pass the OS event timestamp for keyboard, mouse, and touch events where possible
Currently implemented for Windows and Apple platforms
2022-12-02 12:37:41 -08:00
Sam Lantinga 8121bbd083 Convert ticks to 64-bit, added nanosecond precision to the API
Fixes https://github.com/libsdl-org/SDL/issues/5512
Fixes https://github.com/libsdl-org/SDL/issues/6731
2022-12-02 12:37:41 -08:00
Frank Praznik 378b1c286a Fix formatting on Wayland and Pipewire function signatures
Fixes the formatting on some function signatures that clang-format missed.
2022-12-01 15:13:28 -08:00
Pierre Wendling 3c501b963d
Clang-Tidy fixes (#6725) 2022-12-01 13:07:03 -08:00
Sam Lantinga 5750bcb174
Update for SDL3 coding style (#6717)
I updated .clang-format and ran clang-format 14 over the src and test directories to standardize the code base.

In general I let clang-format have it's way, and added markup to prevent formatting of code that would break or be completely unreadable if formatted.

The script I ran for the src directory is added as build-scripts/clang-format-src.sh

This fixes:
#6592
#6593
#6594
2022-11-30 12:51:59 -08:00
Sam Lantinga c5790359fd
Added precompiled header support for Visual Studio and Xcode (#6710)
Fixes https://github.com/libsdl-org/SDL/issues/6704
2022-11-29 18:34:15 -08:00
David Edmundson 6d2b74db66 Support wayland fractional scale protocol
The new protocol adds support for more native communication of
fractional scaling.

Everything in the wayland backend already existed only our fractional
scale was calculated implicitly through a combination of output size
guesswork for fullscreen windows.

This new protocol makes that explicit, providing a more robust solution
and a solution for non-fullscreen surfaces. The fallback code is still
left in place for now whilst compositors gain support.
2022-11-29 16:35:12 -05:00
Sylvain Becker 6a2200823c
Cleanup add brace (#6545)
* Add braces after if conditions

* More add braces after if conditions

* Add braces after while() conditions

* Fix compilation because of macro being modified

* Add braces to for loop

* Add braces after if/goto

* Move comments up

* Remove extra () in the 'return ...;' statements

* More remove extra () in the 'return ...;' statements

* More remove extra () in the 'return ...;' statements after merge

* Fix inconsistent patterns are xxx == NULL vs !xxx

* More "{}" for "if() break;"  and "if() continue;"

* More "{}" after if() short statement

* More "{}" after "if () return;" statement

* More fix inconsistent patterns are xxx == NULL vs !xxx

* Revert some modificaion on SDL_RLEaccel.c

* SDL_RLEaccel: no short statement

* Cleanup 'if' where the bracket is in a new line

* Cleanup 'while' where the bracket is in a new line

* Cleanup 'for' where the bracket is in a new line

* Cleanup 'else' where the bracket is in a new line
2022-11-27 08:38:43 -08:00
Sam Lantinga 0a48abc860 Switch header convention from #include "SDL.h" to #include <SDL3/SDLh>
I ran this script in the include directory:
```sh
sed -i '' -e 's,#include "\(SDL.*\)",#include <SDL3/\1>,' *.h
```

I ran this script in the src directory:
```sh
for i in ../include/SDL3/SDL*.h
do hdr=$(basename $i)
   if [ x"$(echo $hdr | egrep 'SDL_main|SDL_name|SDL_test|SDL_syswm|SDL_opengl|SDL_egl|SDL_vulkan')" != x ]; then
        find . -type f -exec sed -i '' -e 's,#include "\('$hdr'\)",#include <SDL3/\1>,' {} \;
    else
        find . -type f -exec sed -i '' -e '/#include "'$hdr'"/d' {} \;
    fi
done
```

Fixes https://github.com/libsdl-org/SDL/issues/6575
2022-11-26 22:15:18 -08:00
Frank Praznik 836eb22442 wayland: Fix build when not using the shared Wayland libraries
Explicitly include the Wayland protocol headers when statically linking against the Wayland libraries or older system headers might be used instead of the local versions.
2022-11-24 03:00:56 -08:00
Frank Praznik f47169fcba wayland: Fix libdecor_dispatch signature
The function returns an int, not a bool.
2022-11-23 15:16:37 -08:00
Sam Lantinga b0840eb32e Updated SDL_syswm.h for SDL 3.0
* The header is no longer dependent on SDL build configuration
* The structures are versioned separately from the rest of SDL
* SDL_GetWindowWMInfo() now returns a standard result code and is passed the version expected by the application
* Updated WhatsNew.txt and docs/README-migration.md with the first API changes in SDL 3.0
2022-11-23 14:05:59 -08:00
Ryan C. Gordon dcd9e21966
Merge branch 'main' into wip/angle-egl 2022-11-23 14:01:17 -05:00
Frank Praznik fe396e306e wayland: Use the cached window size when switching from non-floating to floating window state
When changing the window state from non-floating to floating (e.g. leaving fullscreen), libdecor can send bogus content sizes that are +/- the height of the window title bar and start 'walking' the window height in one direction or the other with every transition.

The floating window size is known, so use the cached value instead of the size reported by libdecor when restoring the floating state.
2022-11-19 09:15:27 -08:00
David Gow 81479d8784 wayland: keyboard: Cache text input parameters.
Some applications (and embarrassingly, testime is one of them) call
SDL_StartTextInput() or SDL_SetTextInputRect() every frame. On KDE/KWin
with fcitx5, this causes there to be several preedit events every frame
(particularly given some of the workarounds in Wayland_StartTextInput),
which slows testime down to an unusable crawl.

Instead, make SDL_StartTextInput() a no-op if text input is already
enabled, and cache the input rect, only changing it when the new rect is
actually different.

With these changes, we only get preedit events (and hence
SDL_TEXTEDITING events) when the preedit string actually changes. This
matches the behaviour under XWayland, and works very smoothly.
2022-11-18 07:18:36 -08:00
Frank Praznik 903301c6aa wayland: Always use integer scaling for cursors.
Cursors don't get fractionally scaled, so always scale system cursor sizes to the next whole integer.
2022-11-16 06:15:17 -08:00
Frank Praznik c855184765 wayland: Handle virtual keyboards that don't fit the X mapping
SDL is built around the concept of keyboards having a fixed layout with scancodes that correspond to physical keys no matter what linguistic layout is used. Virtual keyboards don't have this concept and can present an arbitrary layout of keys with arbitrary scancodes and names, which don't fit the SDL model. When one of these keyboards is encountered, it requires special handling: use the keysym of the pressed keys to derive their ANSI keyboard scancode equivalents for control keys and ASCII characters. All other characters are passed through as text events only.
2022-11-15 11:00:39 -08:00
Frank Praznik eef4d3c86a wayland: Clamp wl_seat version on older versions of libwayland
Clamp the wl_seat max version to 5 if being built against a version of libwayland below 1.21.0, or containers that bundle newer versions of SDL with older versions of libwayland can break if the compositor advertises support for a protocol version above 5.
2022-11-13 17:48:14 -06:00
Steven Noonan 0644042eb8
egl: implement public functions to obtain internal EGL handles
These functions allow applications to call EGL functions against the SDL
EGL context. For example, applications can use an EGL API loader via
SDL_EGL_GetCurrentDisplay and SDL_EGL_GetProcAddress, and can call
functions such as eglQuerySurface against the internal EGLSurface and
EGLDisplay.
2022-11-10 18:06:11 -08:00
Hubert Maier 5dc93451d2
JANITORIAL : Correct some more spelling mistakes (#6489) 2022-11-06 11:49:37 -08:00
David Edmundson 689218ebf5 Fix wayland reconnection paths
Most of this code is disabled out for now.

- For mouse cursors we have a wl_surface for both system and custom
cursors which needs recreating.
 - The other patch is about nullification after deletions
2022-11-04 11:13:30 -04:00
Guldoman 70a41f9bc2 wayland: Read window data only if window is valid 2022-11-01 05:14:05 -07:00
Frank Praznik 33a430056f wayland: Don't modify the mouse capture flag in relative mode
If relative mouse mode is explicitly enabled, don't modify the capture flag on button events or the window might report having lost mouse focus if a button is pressed while moving the cursor.
2022-10-31 10:13:58 -07:00
Ethan Lee 571ff1a3a9 wayland: Prepare cursor implementation for reconnect support
Co-authored-by: David Edmundson <kde@davidedmundson.co.uk>
2022-10-30 00:19:09 -04:00
Ethan Lee 9c8b1fd8b6 wayland: Cleanup work to aid reconnect support
Co-authored-by: David Edmundson <kde@davidedmundson.co.uk>
2022-10-29 22:41:42 -04:00
Frank Praznik 4223e6ac7a wayland: Early-out sooner when requesting fullscreen on a popup
Exit the fullscreen sequence sooner if it is requested that a popup window be fullscreen.

The surface commit formerly in this path is irrelevant and can be removed as previous changes made it so that SetFullscreen() is no longer called from anywhere except Wayland_SetWindowFullscreen().
2022-10-26 22:19:59 -04:00
Frank Praznik 30c2dac787 wayland: Remove duplicate #include statement 2022-10-25 17:03:25 -07:00
David Jacewicz 7c7cd2a605 Fix issue #6037 (incorrect modifier flags on Wayland) 2022-10-17 13:27:39 -07:00
Frank Praznik ed412c1385 wayland: Cleanup event source comments, headers, and error reporting
Replace instances of fprintf(stderr, ...) with SDL_SetError(), replace C++ comments with C style, use a uniform format for multi-line comments, and remove unused headers as poll and select aren't used in this file (the SDL function which calls them is used instead).
2022-10-16 08:40:50 -07:00
Frank Praznik 6204768653 wayland: Don't block on libdecor_dispatch()
libdecor_dispatch() needs to be called, as libdecor plugins might do some required internal processing within, however care must be taken to avoid double-blocking in the case of a timeout, which can occur if libdecor_dispatch() and the SDL event processing both work on the main Wayland queue. Additionally, assumptions that libdecor will always dispatch the main queue or not process zero-length queues (can occur if a wait is interrupted by the application queueing an event) should not be made, as this behavior is outside the control of SDL and can change.

SDL handles polling for Wayland events and then calls libdecor to do its internal processing and dispatch. If libdecor operates on the main queue, it will dispatch the queued events and the additional wl_display_dispatch_pending() call will be a NOP. If a libdecor plugin uses its own, separate queue, then the wl_display_dispatch_pending() call will ensure that the main queue is always dispatched.
2022-10-15 09:33:37 -07:00
Frank Praznik e89389ba0e wayland: Use MAP_PRIVATE when mapping the keyboard keymap file descriptor
Per the Wayland spec, this must be mapped with MAP_PRIVATE in version 7 of the protocol and higher.
2022-10-15 09:30:06 -07:00
Sam Lantinga 99f2a50394 X11 scancode mapping cleanup
* Consolidated scancode mapping tables into a single location for all backends
* Verified that the xfree86_scancode_table2 is largely identical to the Linux scancode table
* Updated the Linux scancode table with the latest kernel keycodes (still unmapped)
* Route X11 keysym -> scancode mapping through the linux scancode table (which a few hand-written exceptions), which will allow mappings to automatically get picked up as they are added in the Linux scancode table
* Disabled verbose reporting of missing keysym mappings, we have enough data for now
2022-10-13 22:41:47 -07:00
Ethan Lee be2cb00066 wayland: Check for the input handle before checking the keyboard handle 2022-10-11 11:16:52 -04:00
Frank Praznik 0b88e609bc wayland: Raise wl_seat maximum version to 8
Version 8 is required for supporting axis_value120 high-resolution scroll events.
2022-10-11 09:15:21 -04:00
Frank Praznik 6391ad970c wayland: Factor out common libdecor frame commit code
The pattern of:
 libdecor_state_new()
 libdecor_frame_commit()
 libdecor_state_free()

 was used in several places. Factor it out into a common function.
2022-10-09 08:50:54 -07:00
Frank Praznik b0a9396b00 wayland: Remove XDG surface geometry calls
These were needed to fix some buggy behavior regarding committing old buffer sizes when entering fullscreen that has since been corrected. Remove them.
2022-10-09 08:50:54 -07:00
Guldoman b91ddbc3e2 wayland: null-terminate drop data 2022-10-09 08:48:34 -07:00
Frank Praznik 914a65e098 wayland: Don't unset min/max values when entering fullscreen via a compositor event
If the compositor is entering fullscreen and hasn't removed any constraints itself, it's already too late at this point.  Remove the unnecessary call.

Restoring the limits when exiting fullscreen is still required, though, as they may have been removed when entering fullscreen via an SDL request.
2022-10-06 14:00:36 -04:00
Frank Praznik 69cf5fb0e1 wayland: Remove surface type helpers
These were remnants of a time before the surface type was explicitly stored, so they can be removed per the TODO note.
2022-10-06 14:00:36 -04:00
Frank Praznik c2b0c41c0a wayland: Set/unset the opaque regions on surfaces when transparency is toggled
Caches the SDL_HINT_VIDEO_EGL_ALLOW_TRANSPARENCY hint at init time and registers a callback, which is fired when the hint is changed during runtime and toggles the opaque region for existing surfaces.
2022-10-06 14:00:36 -04:00
Frank Praznik ea5958009c wayland: Set the damage buffer size when supported
The preferred method for setting the damage region on compositor protocol versions 4 and above is to use wl_surface.damage_buffer. Use this when available and only fall back to wl_surface.damage on older versions.

Bumps the highest supported version of wl_compositor to version 4.
2022-10-06 14:00:36 -04:00
Frank Praznik a8cb7bbe2f wayland: Add dedupe logic to window geometry configuration
Adds deduplication logic to ConfigureWindowGeometry() to avoid setting redundant backbuffer, viewport and surface opaque region dimensions. State is now only set when the window and/or backbuffer dimensions change.

This repurposes the viewport rect to always hold the actual size of the window, which can differ from the SDL size if things are being scaled. The SDL_Rect was removed in favor of two ints, as the x/y members of the struct were never used, so they just wasted space.

Since the internal variables always have the true window size, the width/height getter functions are no longer required and can be removed.
2022-10-06 14:00:36 -04:00
David Gow ad29875ee6 Wayland: Emulate mouse warp using relative mouse mode
Several games (including Source and GoldSrc games, and Bioshock
Infinite) attempt to "fake" relative mouse mode by repeatedly warping
the cursor to the centre of the screen. Since mouse warping is not
supported under Wayland, the viewport ends up "stuck" in a rectangular
area.

Detect this case (mouse warp while the cursor is not visible), and
enable relative mouse mode, which tracks the cursor position
independently, and so can Warp successfully.

This is behind the SDL_HINT_VIDEO_WAYLAND_EMULATE_MOUSE_WARP hint, which
is enabled by default, unless the application enables relative mouse
mode itself using SDL_SetRelativeMouseMode(SDL_TRUE).

Note that there is a behavoural difference, in that relative mouse mode
typically doesn't take mouse accelleration into account, but the
repeated-warping technique does, so mouse movement can seem very slow
with this (unless the game has its own mouse accelleration option, such
as in Portal 2).
2022-10-03 19:11:18 -07:00
Frank Praznik e17f0c283c wayland: Call the Wayland hide/show functions directly when changing decoration modes
Calling SDL_HideWindow() to destroy the window is a NOP if the SDL_WINDOW_SHOWN flag was never set. Bypass it and call the Wayland-specific function directly to ensure that the window is always destroyed before switching from server-side to client-side decorations, even if it hasn't been shown yet.

Likewise, call Wayland_ShowWindow() directly when the window isn't explicitly hidden to ensure that it is always recreated since the SDL_WINDOW_SHOWN flag won't be cleared, which, when set, renders SDL_ShowWindow() a NOP.
2022-09-28 13:21:10 -07:00
Frank Praznik 4a06cc0f75 wayland: Apply modifier changes to the text of repeated keys
The text component of a repeated keystroke is initially set when a key is first pressed and the cached value remains static until the repeated key is released and another repeatable key is pressed. If the state of a modifier such as shift or capslock is changed while a key is being repeated, the text emitted will not have the modifier applied to it until the repeated key is released and pressed again.

Update the text to be repeated by a key if a modifier is changed while a key is actively being repeated.
2022-09-23 13:40:51 -04:00
Frank Praznik 467c82c771 wayland: Add mappings for the Escape and NumLock keys
The XKB keysym to SDL keycode mappings were missing for the Escape and NumLock keys, which prevented them from being remapped. Add them to the table so that the remapping of these keys will work.
2022-09-23 10:30:14 -07:00
Frank Praznik 399cb2f0de wayland: Only clear the key repeat flag when the repeated key is released
If multiple keys were simultaneously depressed and one was being repeated, the repeat flag was being cleared when any of the pressed keys were released, even if the released key wasn't the one being repeated.

This tracks the key currently being repeated and only clears the repeat flag when the particular key being repeated is released.
2022-09-21 14:26:13 -07:00
DS ac5b9bc4ee
Add support for X11 primary selection (#6132)
X11 has a so-called primary selection, which you can use by marking text and middle-clicking elsewhere to copy the marked text.

There are 3 new API functions in `SDL_clipboard.h`, which work exactly like their clipboard equivalents.

## Test Instructions

* Run the tests (just a copy of the clipboard tests): `$ ./test/testautomation --filter Clipboard`
* Build and run this small application:
<details>
```C
#include <SDL.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void print_error(const char *where)
{
	const char *errstr = SDL_GetError();
	if (errstr == NULL || errstr[0] == '\0')
		return;
	fprintf(stderr, "SDL Error after '%s': %s\n", where, errstr);
	SDL_ClearError();
}

int main()
{
	char text_buf[256];

	srand(time(NULL));

	SDL_Init(SDL_INIT_VIDEO);
	print_error("SDL_INIT()");
	SDL_Window *window = SDL_CreateWindow("Primary Selection Test", SDL_WINDOWPOS_UNDEFINED,
			SDL_WINDOWPOS_UNDEFINED, 400, 400, SDL_WINDOW_SHOWN);
	print_error("SDL_CreateWindow()");
	SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
	print_error("SDL_CreateRenderer()");

	bool quit = false;
	unsigned int do_render = 0;
	while (!quit) {
		SDL_Event event;
		while (SDL_PollEvent(&event)) {
			print_error("SDL_PollEvent()");
			switch (event.type) {
			case SDL_QUIT: {
				quit = true;
				break;
			} case SDL_KEYDOWN: {
				switch (event.key.keysym.sym) {
				case SDLK_ESCAPE:
				case SDLK_q:
					quit = true;
					break;
				case SDLK_c:
					snprintf(text_buf, sizeof(text_buf), "foo%d", rand());
					SDL_SetClipboardText(text_buf);
					print_error("SDL_SetClipboardText()");
					printf("clipboard: set_to=\"%s\"\n", text_buf);
					break;
				case SDLK_v: {
					printf("clipboard: has=%d, ", SDL_HasClipboardText());
					print_error("SDL_HasClipboardText()");
					char *text = SDL_GetClipboardText();
					print_error("SDL_GetClipboardText()");
					printf("text=\"%s\"\n", text);
					SDL_free(text);
					break;
				} case SDLK_d:
					snprintf(text_buf, sizeof(text_buf), "bar%d", rand());
					SDL_SetPrimarySelectionText(text_buf);
					print_error("SDL_SetPrimarySelectionText()");
					printf("primselec: set_to=\"%s\"\n", text_buf);
					break;
				case SDLK_f: {
					printf("primselec: has=%d, ", SDL_HasPrimarySelectionText());
					print_error("SDL_HasPrimarySelectionText()");
					char *text = SDL_GetPrimarySelectionText();
					print_error("SDL_GetPrimarySelectionText()");
					printf("text=\"%s\"\n", text);
					SDL_free(text);
					break;
				} default:
					break;
				}
				break;
			} default: {
				break;
			}}
		}
		// create less noise with WAYLAND_DEBUG=1
		if (do_render == 0) {
			SDL_RenderPresent(renderer);
			print_error("SDL_RenderPresent()");
		}
		do_render += 1;
		usleep(12000);
	}

	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);
	SDL_Quit();
	print_error("quit");
	return 0;
}
```
</details>

* Use c,v,d,f to get and set the clipboard and primary selection.
* Mark text and middle-click also in other applications.
* For wayland under x:
  * `$ mutter --wayland --no-x11 --nested`
  * `$ XDG_SESSION_TYPE=wayland SDL_VIDEODRIVER=wayland ./<path_to_test_appl_binary>`
2022-09-14 09:28:35 -07:00