SDL/src/video/x11
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
..
SDL_x11clipboard.c Add support for X11 primary selection (#6132) 2022-09-14 09:28:35 -07:00
SDL_x11clipboard.h Add support for X11 primary selection (#6132) 2022-09-14 09:28:35 -07:00
SDL_x11dyn.c Silence unused variable warning 2022-08-30 12:52:01 -07:00
SDL_x11dyn.h x11: Remove XVidMode and Xinerama support. 2022-04-26 23:17:13 -04:00
SDL_x11events.c Add support for X11 primary selection (#6132) 2022-09-14 09:28:35 -07:00
SDL_x11events.h Fixed bug #1650: X11 doesn't set KMOD_NUM and KMOD_CAPS to system state 2022-03-24 18:09:45 +01:00
SDL_x11framebuffer.c Updated copyright for 2022 2022-01-03 09:40:21 -08:00
SDL_x11framebuffer.h Updated copyright for 2022 2022-01-03 09:40:21 -08:00
SDL_x11keyboard.c keyboard: Only send SDL_KEYMAPCHANGED when the keymap actually changes 2022-07-31 14:02:28 -07:00
SDL_x11keyboard.h keyboard: Only send SDL_KEYMAPCHANGED when the keymap actually changes 2022-07-31 14:02:28 -07:00
SDL_x11messagebox.c x11: Minor cleanups and corrections in X11_ShowMessageBox. 2022-03-24 14:25:34 -04:00
SDL_x11messagebox.h Updated copyright for 2022 2022-01-03 09:40:21 -08:00
SDL_x11modes.c x11: Don't send diplay-add events for displays connected at init time. 2022-06-21 14:49:21 -04:00
SDL_x11modes.h x11: Hook up display hotplug notifications. 2022-06-06 02:13:37 -04:00
SDL_x11mouse.c Fixed interactions between mouse capture and grab on X11 2022-08-17 14:26:34 -07:00
SDL_x11mouse.h x11: Attempt to deal with XInput2 devices with absolute coordinates. 2022-08-04 02:12:46 -04:00
SDL_x11opengl.c SDL_video: Added SDL_GL_FLOATBUFFERS to allow Cocoa GL contexts to use EDR 2022-08-02 15:45:30 -07:00
SDL_x11opengl.h Updated copyright for 2022 2022-01-03 09:40:21 -08:00
SDL_x11opengles.c Updated copyright for 2022 2022-01-03 09:40:21 -08:00
SDL_x11opengles.h Updated copyright for 2022 2022-01-03 09:40:21 -08:00
SDL_x11shape.c Updated copyright for 2022 2022-01-03 09:40:21 -08:00
SDL_x11shape.h Updated copyright for 2022 2022-01-03 09:40:21 -08:00
SDL_x11sym.h x11: Hook up display hotplug notifications. 2022-06-06 02:13:37 -04:00
SDL_x11touch.c x11: Don't try to use XInput2 multitouch if not supported. 2022-07-04 12:48:32 -04:00
SDL_x11touch.h Updated copyright for 2022 2022-01-03 09:40:21 -08:00
SDL_x11video.c Add support for X11 primary selection (#6132) 2022-09-14 09:28:35 -07:00
SDL_x11video.h x11: Attempt to deal with XInput2 devices with absolute coordinates. 2022-08-04 02:12:46 -04:00
SDL_x11vulkan.c Updated copyright for 2022 2022-01-03 09:40:21 -08:00
SDL_x11vulkan.h Updated copyright for 2022 2022-01-03 09:40:21 -08:00
SDL_x11window.c x11: Specify windowed dimensions when creating fullscreen windows. 2022-08-21 11:35:14 -04:00
SDL_x11window.h Fixed interactions between mouse capture and grab on X11 2022-08-17 14:26:34 -07:00
SDL_x11xfixes.c Updated copyright for 2022 2022-01-03 09:40:21 -08:00
SDL_x11xfixes.h Updated copyright for 2022 2022-01-03 09:40:21 -08:00
SDL_x11xinput2.c x11: Don't look up xinput2 devices unless we're in relative mode. 2022-08-09 09:50:55 -04:00
SDL_x11xinput2.h Updated copyright for 2022 2022-01-03 09:40:21 -08:00
edid-parse.c Fixed bug #3232 - Integer overflow generates Illegal instruction under sanitizers + see bug #4995 2021-11-23 09:30:42 +01:00
edid.h Fixed edid parsing code for older gcc compilers 2016-12-12 09:18:42 -08:00