mirror of https://github.com/encounter/SDL.git
ac5b9bc4ee
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>` |
||
---|---|---|
.. | ||
SDL.h | ||
SDL_assert.h | ||
SDL_atomic.h | ||
SDL_audio.h | ||
SDL_bits.h | ||
SDL_blendmode.h | ||
SDL_clipboard.h | ||
SDL_config.h | ||
SDL_config.h.cmake | ||
SDL_config.h.in | ||
SDL_config_android.h | ||
SDL_config_emscripten.h | ||
SDL_config_iphoneos.h | ||
SDL_config_macosx.h | ||
SDL_config_minimal.h | ||
SDL_config_ngage.h | ||
SDL_config_os2.h | ||
SDL_config_pandora.h | ||
SDL_config_windows.h | ||
SDL_config_wingdk.h | ||
SDL_config_winrt.h | ||
SDL_config_xbox.h | ||
SDL_copying.h | ||
SDL_cpuinfo.h | ||
SDL_egl.h | ||
SDL_endian.h | ||
SDL_error.h | ||
SDL_events.h | ||
SDL_filesystem.h | ||
SDL_gamecontroller.h | ||
SDL_gesture.h | ||
SDL_guid.h | ||
SDL_haptic.h | ||
SDL_hidapi.h | ||
SDL_hints.h | ||
SDL_joystick.h | ||
SDL_keyboard.h | ||
SDL_keycode.h | ||
SDL_loadso.h | ||
SDL_locale.h | ||
SDL_log.h | ||
SDL_main.h | ||
SDL_messagebox.h | ||
SDL_metal.h | ||
SDL_misc.h | ||
SDL_mouse.h | ||
SDL_mutex.h | ||
SDL_name.h | ||
SDL_opengl.h | ||
SDL_opengl_glext.h | ||
SDL_opengles.h | ||
SDL_opengles2.h | ||
SDL_opengles2_gl2.h | ||
SDL_opengles2_gl2ext.h | ||
SDL_opengles2_gl2platform.h | ||
SDL_opengles2_khrplatform.h | ||
SDL_pixels.h | ||
SDL_platform.h | ||
SDL_power.h | ||
SDL_quit.h | ||
SDL_rect.h | ||
SDL_render.h | ||
SDL_revision.h | ||
SDL_revision.h.cmake | ||
SDL_rwops.h | ||
SDL_scancode.h | ||
SDL_sensor.h | ||
SDL_shape.h | ||
SDL_stdinc.h | ||
SDL_surface.h | ||
SDL_system.h | ||
SDL_syswm.h | ||
SDL_test.h | ||
SDL_test_assert.h | ||
SDL_test_common.h | ||
SDL_test_compare.h | ||
SDL_test_crc32.h | ||
SDL_test_font.h | ||
SDL_test_fuzzer.h | ||
SDL_test_harness.h | ||
SDL_test_images.h | ||
SDL_test_log.h | ||
SDL_test_md5.h | ||
SDL_test_memory.h | ||
SDL_test_random.h | ||
SDL_thread.h | ||
SDL_timer.h | ||
SDL_touch.h | ||
SDL_types.h | ||
SDL_version.h | ||
SDL_video.h | ||
SDL_vulkan.h | ||
begin_code.h | ||
close_code.h |