Commit Graph

104 Commits

Author SHA1 Message Date
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
Sam Lantinga d166f5ef76 Fixed uninitialized variable warning 2022-08-22 14:10:54 -07:00
Frank Praznik 057086e389 wayland: Add high resolution scroll wheel support
Update the Wayland core protocol spec file and add support for the new axis_value120 event to handle high resolution scroll wheels.

The axis_value120 replaces the axis_discrete event, which is no longer sent as of version 8 of the protocol.  Note that unlike the axis_discrete event, no mention in the spec is made regarding how many axis_value120 events may occur per-axis per-frame, so the values are accumulated and committed when the pointer frame event occurs.
2022-08-21 08:54:58 -07:00
Ryan C. Gordon f600364b8a
wayland: Mark window as MOUSE_CAPTURE while a mouse button is down.
Wayland works like SDL's "auto capture" feature already, tracking the mouse
globally only while a drag is occuring, and this is the only way to get mouse
input outside the window.

Setting this flag ourselves lets SDL_CaptureMouse() work in the most common
use case without actually implementing CaptureMouse for the backend, including
SDL's auto capture feature.

Fixes #6010.
2022-08-06 09:19:52 -04:00
Cameron Gutman 8b438f7b51 keyboard: Only send SDL_KEYMAPCHANGED when the keymap actually changes 2022-07-31 14:02:28 -07:00
Cameron Cawley 78089e6598 Remove unused internal header SDL_sysevents.h 2022-07-01 07:39:48 -07:00
Sam Lantinga 5f6d0abebe SDL_SendEditingText() has int parameters, so use that type for parameter calculation
We might want to use ssize_t as @Guldoman suggested, but that's a larger internal API change, and still requires casting of the SDL_utf8strnlen() result.

Fixes https://github.com/libsdl-org/SDL/pull/5821
2022-06-18 12:59:28 -07:00
takase1121 f8ae3ef1eb wayland: use libdecor resize edge enums for libdecor 2022-06-15 10:21:18 -04:00
Guldoman d11702ce29 ime: wayland: Make use of `SDL_TEXTEDITING_EXT`
Because we were sending multiple chunks of preedit strings,
`SDL_SendEditingText` was using the old `SDL_TEXTEDITING` event only.

Now if `SDL_HINT_IME_SUPPORT_EXTENDED_TEXT` is enabled, we send the full
string and correctly set the cursor position and selection size.
2022-06-09 15:18:50 -07:00
David Gow 847539afeb wayland: Only call libdecor_dispatch() if we've loaded libdecor
As of #5703, we call libdecor_dispatch() in Wayland_WaitEventTimeout(),
but this will crash if we don't load libdecor, as
SDL_VideoData::shell.libdecor will be NULL.

Since we don't load libdecor if we don't intend to use it (i.e., if
should_use_libdecor returns false), this results in a crash under KDE in
almost all circumstances.
2022-06-01 08:32:13 -07:00
Christian Rauch e59cba95a0 add libdecor_dispatch 2022-05-21 09:40:26 -07:00
Ethan Lee 6f88cbe4c9 wayland: Support xdg_decoration requesting client-side decorations.
Don't be fooled by the diff size - this ended up being a big refactor of the
shell surface management, masked only by some helper macros I wrote for the
popup support.

This change makes it so when xdg_decoration is supported, but CSD is requested,
the system bails on xdg support entirely and resets all the windows to use
libdecor instead. This transition isn't pretty, but once it's done it will be
smooth if decorations are an OS toggle since libdecor will take things from
there.

In hindsight, we really should have designed libdecor to be passed a toplevel,
having it manage that for us keeps causing major refactors for _every_ change.
2022-05-11 13:13:59 -07:00
Ethan Lee c37090f9a4 wayland: Add support for TOOLTIP/POPUP_MENU 2022-04-18 12:31:02 -04:00
Frank Praznik edb473cf46 video: Wayland: Always round scaled pointer coordinates down
Rounding up can cause the pointer coordinates to exceed the window boundaries at the right and bottom edges.
2022-04-15 16:24:20 -04:00
Weng Xuetian 138d96c8a6
Send key release event to input method. (#5281)
Co-authored-by: Ethan Lee <flibitijibibo@gmail.com>
2022-04-05 22:30:25 -04:00
Frank Praznik fa4c51989d video: wayland: Expose more resolutions for mode emulation
Expose as many emulated display modes as possible.  They will currently display stretched to the display's native desktop aspect, but if an application requires a hardcoded resolution, it will work at minimum.

Aside from the change in the emulated display mode list, the Wayland event handling code had to be updated to support separate scaling for the x and y axes, as square pixels are no longer guaranteed.
2022-03-28 22:19:34 -04:00
Frank Praznik 4d76c9cb46 video: wayland: Use wp-viewporter for fullscreen with non-native resolutions
Wayland doesn't support mode switching, however Wayland compositors can support the wp_viewporter protocol, which allows for the mapping of arbitrarily sized buffer regions to output surfaces.  Use this functionality, when available, for fullscreen output when using non-native display modes and/or when dealing with scaled desktops, which can incur significant overdraw without this extension.

This also allows for the exposure of arbitrarily sized, emulated display modes, which can be useful for legacy compatability.
2022-03-28 13:18:26 -04:00
Ethan Lee 7a1c45bd1c wayland: Optimize keyboard_handle_modifiers.
1. Mod index values are (mostly) constant, so can be done with xkb_state_new
2. Mods can change without the group changing, avoid remap events if possible

Lastly, as a bonus, I added braces to the locale check, because I was nearby.
2022-03-25 12:54:05 -04:00
Ethan Lee 2891f0821b wayland: Use xkb_keymap_mod to set mod state 2022-03-25 02:49:49 -04:00
Ethan Lee 6d9ca92626 wayland: Enforce text capitalization manually, for remapped keymods 2022-03-25 01:36:39 -04:00
Ethan Lee a75c6150e0 wayland: Add an xkb_keysym_t->SDL_Keycode mapping for backspace 2022-03-25 01:33:40 -04:00
Florian "sp1rit"​ 9125b244e7 wayland: Basic support for zwp_tablet_*v2 protocol 2022-03-23 13:47:46 -04:00
Weng Xuetian b11dfd7611 Only generate key repetition for keys that should repeat on wayland.
This fix repetition on modifier keys, e.g. Control.
2022-02-01 17:56:04 -05:00
Weng Xuetian a90a2e7582 Fix text_input_v3 preedit string
For every batch of text_input_v3 updates, if there is no preedit in this
batch, preedit should be cleared.
2022-02-01 15:51:26 -05:00
Ethan Lee 9a2bbd8acb wayland: Convert URI to local path for DropFile 2022-01-12 13:01:05 -05:00
Ethan Lee 3e1b3bc344
wayland: Horizontal wheel values do not need to be inverted 2022-01-10 10:07:44 -05:00
Joan Bruguera 9e6249fa54 wayland: Avoid spurious key repeats when not pumping events
Previous to this commit, key repeats events were typically generated when
pumping events, based on the time of when the events are pumped. However,
if an application doesn't call `SDL_PumpEvents` for some seconds, this time
can be multiple seconds in the future compared to the actual key up event time,
and generates key repeats even if a key was pressed only for an instant.

In practice, this can happen when the user presses a key which causes the
application to do something without pumping events (e.g. load a level).
In Crispy Doom & PrBoom+, when the user presses the key bound to "Restart
level/demo", the game doesn't pump events during the "screen melt" effect,
and the level is restarted multiple times due to spurious repeats.

To fix this, if the key up event is among the events to be pumped, we generate
the key repeats there, since in the Wayland callback we receive the time when
the key up event happened. Otherwise, we know no key up event happened and we
can generate as many repeats as necessary after pumping.

Signed-off-by: Joan Bruguera <joanbrugueram@gmail.com>
2022-01-08 14:10:22 -08:00
Joan Bruguera 461724d287 wayland: Refactor time fields in SDL_WaylandKeyboardRepeat
Refactorization with no functional changes.

Instead of `next_repeat_ms` containing a timestamp based on SDL ticks, we make
it zero-based relative to the key press time, and we store the key press time in
SDL ticks in a new field.

This refactorization is groundwork for future commits which need to use the
key press and release timestamps provided by the Wayland API, which are also
expressed in milliseconds, but whose base does not match the one for SDL ticks.

Signed-off-by: Joan Bruguera <joanbrugueram@gmail.com>
2022-01-08 14:10:22 -08:00
Joan Bruguera fb0c3040c0 wayland: Avoid infinite loop in keyboard_repeat_handle
If `repeat_info->next_repeat_ms` overflows, many key presses will be generated.
In the worst case, `now = 0xFFFFFFFFU` and the loop will never terminate.

Rearrange the comparison in order to gracefully handle the overflow case.

Signed-off-by: Joan Bruguera <joanbrugueram@gmail.com>
2022-01-08 14:10:22 -08:00
Sam Lantinga 120c76c84b Updated copyright for 2022 2022-01-03 09:40:21 -08:00
Valentin Hăloiu cb8fa5f9c3 wayland: fix keycodes of swapped xkb modifier keys 2021-12-09 09:30:58 -08:00
Sylvain cb9f85e8d0 Don't use "round", so that it's doesn't show up while searching for the function 2021-11-22 08:38:46 -08:00
Ozkan Sezer 781caec2b2 SDL_waylandevents.c (keyboard_handle_keymap): silenced -Wwrite-strings . 2021-11-15 00:55:24 +03:00
Ethan Lee ae67c7d2da Implemented SDL_SetWindowMouseRect() on Wayland 2021-11-09 01:34:02 -05:00
Cameron Gutman a559864968 x11/wayland: Fix signal handling while blocking in WaitEventTimeout()
Add a new flag to avoid suppressing EINTR in SDL_IOReady(). Pass the
flag in WaitEventTimeout() to ensure that a SIGINT will wake up
SDL_WaitEvent() without another event coming in.
2021-10-30 21:23:45 -07:00
Cameron Gutman c97c46877f core: Convert SDL_IOReady()'s 2nd parameter to flags 2021-10-30 21:23:45 -07:00
Cameron Gutman 2bf36bfac4 wayland: Implement WaitEventTimeout() and SendWakeupEvent()
We can have spurious wakeups in WaitEventTimeout() due to Wayland events
that don't end up causing us to generate an SDL event. Fortunately for us,
SDL_WaitEventTimeout_Device() handles this situation properly by calling
WaitEventTimeout() again with an adjusted timeout.
2021-10-27 19:16:53 -05:00
Cameron Gutman 408a93a1ec wayland: Use multi-thread event reading APIs
Wayland provides the prepare_read()/read_events() family of APIs for
reading from the display fd in a deadlock-free manner across multiple
threads in a multi-threaded application. Let's use those instead of
trying to roll our own solution using a mutex.

This fixes an issue where a call to SDL_GL_SwapWindow() doesn't swap
buffers if it happens to collide with SDL_PumpEvents() in the main
thread. It also allows coexistence with other code or toolkits in
our process that may want read and dispatch events themselves.
2021-10-25 12:00:35 -04:00
David Gow eadc8f9355 wayland: Cleanup some SDL_TryLockMutex() calls.
Check the result of these against 0 explicitly, so that it's obvious
we're bailing out on failure, not success.
2021-10-02 11:14:52 -04:00
David Gow 25f9e32b0e wayland: Don't let multiple threads dispatch wayland events at once
wl_display_dispatch() will block if there are no events available, and
while we try to avoid this by using SDL_IOReady() to verify there are
events before calling it, there is a race condition between
SDL_IOReady() and wl_display_dispatch() if multiple threads are
involved.

This is made more likely by the fact that SDL_GL_SwapWindow() calls
wl_display_dispatch() if vsync is enabled, in order to wait for frame
events. Therefore any program which pumps events on a different thread
from SDL_GL_SwapWindow() could end up blocking in one or other of them
until another event arrives.

This change fixes this by wrapping wl_display_dispatch() in a new mutex,
which ensures only one thread can compete for wayland events at a time,
and hence the SDL_IOReady() check should successfully prevent either
from blocking.
2021-10-02 11:03:20 -04:00
Ethan Lee 7ed415d2ed wayland: Reuse KeySymToUcs4 to replicate X11 keymap behavior 2021-09-23 11:50:47 -07:00
Ethan Lee 1a4e2e5ef7 wayland: For text, ignore key events when Ctrl is held
Fixes #4695
2021-09-23 14:34:39 -04:00
Aleksey Rybalkin 402b86f2a8 waylandevents: prevent segfault if xkb compose table is not found
this can happen e.g. on pure wayland system where there is no X11
locales for xkbcommon to find.
2021-08-15 10:11:19 -04:00
David Gow fbc364908a Use the new SDL_clamp() macro where sensible
There were a few places throughout the SDL code where values were
clamped using SDL_min() and SDL_max(). Now that we have an SDL_clamp()
macro, use this instead.
2021-08-14 09:01:14 -07:00
Ethan Lee 32f909f7e3 wayland: Remove redundant waylanddyn.h includes.
All files including waylanddyn.h already include waylandvideo.h first.
2021-08-03 14:57:22 -04:00
Sebastian Krzyszkowiak 54aea2446e wayland: Disable key repeat when repeat rate equals 0
This fixes a crash on pressing keyboard button when compositor sends
zero as repeat rate, indicating that key repeat should be disabled.

From Wayland protocol spec:

> Negative values for either rate or delay are illegal. A rate of zero
> will disable any repeating (regardless of the value of delay).
2021-07-31 21:40:27 -07:00
Sam Lantinga 9984891ba8 Use the wl_touch object as a touch ID on Wayland (thanks @russelltg!)
This fixes https://github.com/libsdl-org/SDL/issues/4517
2021-07-29 14:46:24 -07:00
Ethan Lee 74162b7401 wayland: Add support for text-input-unstable-v3 2021-07-29 14:43:46 -07:00
David Gow 1fb4429bc0 wayland: Avoid a pointer→TouchID cast warning
As of [1], SDL now compiles with a warning in SDL_waylandevents.c on
32-bit systems under gcc 10.3.0:

/tmp/SDL/src/video/wayland/SDL_waylandevents.c: In function 'seat_handle_capabilities':
/tmp/SDL/src/video/wayland/SDL_waylandevents.c:958:22: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  958 |         SDL_AddTouch((SDL_TouchID)seat, SDL_TOUCH_DEVICE_DIRECT, "wayland_touch");
      |                      ^
/tmp/SDL/src/video/wayland/SDL_waylandevents.c:964:22: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  964 |         SDL_DelTouch((SDL_TouchID)seat);
      |                      ^

This is due to SDL_TouchID always being 32-bit, but seat being a pointer
which is (obviously) only 32-bit on 32-bit systems. The conversion is
therefore harmless, so silence it with an extra cast via intptr_t.

This is what the cocoa backend does (and is similar to what the Win32
backend does, except with size_t).

Fixes: 03c19efbd1 ("Added support for multiple seats with touch input on Wayland")

[1]: 03c19efbd1
2021-07-28 09:05:23 -07:00
Simon Zeni 6aae5b44f8
Remove wl-shell and xdg-shell-unstable-v6 support (#4323)
* wayland-protocol: update wayland.xml to 1.19.0

* wayland: remove shell_surface field from SDL_SysWMinfo

* wayland: remove wl_shell support

* waypand-protocols: update xdg-shell.xml to 1.20

* wayland: remove xdg-shell-unstable-v6 support

* wayland: deprecate wl shell surface syswm info, add xdg surface
2021-07-27 14:12:26 -07:00