[client] clipboard/wayland: make use of boilerplate from wm.c

This commit is contained in:
Tudor Brindus 2021-01-11 20:55:42 -05:00 committed by Geoffrey McRae
parent 2aa2ec31ef
commit 96c10c2c2d
4 changed files with 133 additions and 169 deletions

View File

@ -20,6 +20,8 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include "interface/clipboard.h" #include "interface/clipboard.h"
#include "common/debug.h" #include "common/debug.h"
#include "../../client/src/wm.h"
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
@ -35,21 +37,11 @@ struct WCBTransfer
struct WCBState struct WCBState
{ {
struct wl_display * display;
struct wl_registry * registry;
struct wl_data_device_manager * dataDeviceManager;
struct wl_seat * seat;
struct wl_data_device * dataDevice;
enum LG_ClipboardData stashedType; enum LG_ClipboardData stashedType;
char * stashedMimetype; char * stashedMimetype;
uint8_t * stashedContents; uint8_t * stashedContents;
ssize_t stashedSize; ssize_t stashedSize;
struct wl_keyboard * keyboard;
uint32_t keyboardEnterSerial;
uint32_t capabilities;
LG_ClipboardReleaseFn releaseFn; LG_ClipboardReleaseFn releaseFn;
LG_ClipboardRequestFn requestFn; LG_ClipboardRequestFn requestFn;
LG_ClipboardNotifyFn notifyFn; LG_ClipboardNotifyFn notifyFn;
@ -182,104 +174,6 @@ static const char * wayland_cb_getName()
return "Wayland"; return "Wayland";
} }
// Keyboard-handling listeners.
static void keyboardKeymapHandler(void * data, struct wl_keyboard * keyboard,
uint32_t format, int fd, uint32_t size)
{
close(fd);
}
static void keyboardEnterHandler(void * data, struct wl_keyboard * keyboard,
uint32_t serial, struct wl_surface * surface, struct wl_array * keys)
{
this->keyboardEnterSerial = serial;
}
static void keyboardLeaveHandler(void * data, struct wl_keyboard * keyboard,
uint32_t serial, struct wl_surface * surface)
{
// Do nothing.
}
static void keyboardKeyHandler(void * data, struct wl_keyboard * keyboard,
uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
{
// Do nothing.
}
static void keyboardModifiersHandler(void * data,
struct wl_keyboard * keyboard, uint32_t serial, uint32_t modsDepressed,
uint32_t modsLatched, uint32_t modsLocked, uint32_t group)
{
// Do nothing.
}
static const struct wl_keyboard_listener keyboardListener = {
.keymap = keyboardKeymapHandler,
.enter = keyboardEnterHandler,
.leave = keyboardLeaveHandler,
.key = keyboardKeyHandler,
.modifiers = keyboardModifiersHandler,
};
// Seat-handling listeners.
static void seatCapabilitiesHandler(void * data, struct wl_seat * seat,
uint32_t capabilities)
{
this->capabilities = capabilities;
bool hasKeyboard = capabilities & WL_SEAT_CAPABILITY_KEYBOARD;
if (!hasKeyboard && this->keyboard)
{
wl_keyboard_destroy(this->keyboard);
this->keyboard = NULL;
}
else if (hasKeyboard && !this->keyboard)
{
this->keyboard = wl_seat_get_keyboard(this->seat);
wl_keyboard_add_listener(this->keyboard, &keyboardListener, NULL);
}
}
static void seatNameHandler(void * data, struct wl_seat * seat,
const char * name)
{
// Do nothing.
}
static const struct wl_seat_listener seatListener = {
.capabilities = seatCapabilitiesHandler,
.name = seatNameHandler
};
// Registry-handling listeners.
static void registryGlobalHandler(void * data,
struct wl_registry * registry, uint32_t name, const char * interface,
uint32_t version)
{
if (!strcmp(interface, wl_data_device_manager_interface.name))
this->dataDeviceManager = wl_registry_bind(this->registry, name,
&wl_data_device_manager_interface, 3);
else if (!strcmp(interface, wl_seat_interface.name) && !this->seat)
// TODO: multi-seat support.
this->seat = wl_registry_bind(this->registry, name,
&wl_seat_interface, 1);
}
static void registryGlobalRemoveHandler(void * data,
struct wl_registry * registry, uint32_t name)
{
// Do nothing.
}
static const struct wl_registry_listener registryListener = {
.global = registryGlobalHandler,
.global_remove = registryGlobalRemoveHandler,
};
// Destination client handlers. // Destination client handlers.
static void dataHandleOffer(void * data, struct wl_data_offer * offer, static void dataHandleOffer(void * data, struct wl_data_offer * offer,
@ -327,7 +221,8 @@ static void dataDeviceHandleSelection(void * data,
free(this->stashedMimetype); free(this->stashedMimetype);
this->stashedMimetype = NULL; this->stashedMimetype = NULL;
wl_display_roundtrip(this->display); struct WMDataWayland * wm = g_wmState.opaque;
wl_display_roundtrip(wm->display);
if (this->stashedContents) if (this->stashedContents)
{ {
@ -404,21 +299,10 @@ static bool wayland_cb_init(
this->releaseFn = releaseFn; this->releaseFn = releaseFn;
this->notifyFn = notifyFn; this->notifyFn = notifyFn;
this->dataFn = dataFn; this->dataFn = dataFn;
this->display = wminfo->info.wl.display;
this->registry = wl_display_get_registry(this->display);
this->stashedType = LG_CLIPBOARD_DATA_NONE; this->stashedType = LG_CLIPBOARD_DATA_NONE;
// Wait for the initial set of globals to appear. struct WMDataWayland * wm = g_wmState.opaque;
wl_registry_add_listener(this->registry, &registryListener, NULL); wl_data_device_add_listener(wm->dataDevice, &dataDeviceListener, NULL);
wl_display_roundtrip(this->display);
this->dataDevice = wl_data_device_manager_get_data_device(
this->dataDeviceManager, this->seat);
wl_data_device_add_listener(this->dataDevice, &dataDeviceListener, NULL);
// Wait for the compositor to let us know of capabilities.
wl_seat_add_listener(this->seat, &seatListener, NULL);
wl_display_roundtrip(this->display);
return true; return true;
} }
@ -479,6 +363,8 @@ static const struct wl_data_source_listener dataSourceListener = {
static void wayland_cb_reply_fn(void * opaque, LG_ClipboardData type, static void wayland_cb_reply_fn(void * opaque, LG_ClipboardData type,
uint8_t * data, uint32_t size) uint8_t * data, uint32_t size)
{ {
struct WMDataWayland * wm = g_wmState.opaque;
struct WCBTransfer * transfer = malloc(sizeof(struct WCBTransfer)); struct WCBTransfer * transfer = malloc(sizeof(struct WCBTransfer));
void * dataCopy = malloc(size); void * dataCopy = malloc(size);
memcpy(dataCopy, data, size); memcpy(dataCopy, data, size);
@ -489,13 +375,13 @@ static void wayland_cb_reply_fn(void * opaque, LG_ClipboardData type,
}; };
struct wl_data_source * source = struct wl_data_source * source =
wl_data_device_manager_create_data_source(this->dataDeviceManager); wl_data_device_manager_create_data_source(wm->dataDeviceManager);
wl_data_source_add_listener(source, &dataSourceListener, transfer); wl_data_source_add_listener(source, &dataSourceListener, transfer);
for (const char ** mimetype = transfer->mimetypes; *mimetype; mimetype++) for (const char ** mimetype = transfer->mimetypes; *mimetype; mimetype++)
wl_data_source_offer(source, *mimetype); wl_data_source_offer(source, *mimetype);
wl_data_device_set_selection(this->dataDevice, source, wl_data_device_set_selection(wm->dataDevice, source,
this->keyboardEnterSerial); wm->keyboardEnterSerial);
} }
static void wayland_cb_notice(LG_ClipboardRequestFn requestFn, static void wayland_cb_notice(LG_ClipboardRequestFn requestFn,
@ -509,7 +395,8 @@ static void wayland_cb_notice(LG_ClipboardRequestFn requestFn,
// Won't have a keyboard enter serial if we don't have the keyboard // Won't have a keyboard enter serial if we don't have the keyboard
// capability. // capability.
if (!this->keyboard) struct WMDataWayland * wm = g_wmState.opaque;
if (!wm->keyboard)
return; return;
this->requestFn(wayland_cb_reply_fn, NULL); this->requestFn(wayland_cb_reply_fn, NULL);

View File

@ -2031,21 +2031,6 @@ static int lg_run()
return -1; return -1;
} }
for (LG_Clipboard ** clipboard = LG_Clipboards; *clipboard; clipboard++)
if ((*clipboard)->init(&g_state.wminfo, clipboardRelease, clipboardNotify, clipboardData))
{
g_state.lgc = *clipboard;
break;
}
if (g_state.lgc)
{
DEBUG_INFO("Using Clipboard: %s", g_state.lgc->getName());
g_state.cbRequestList = ll_new();
}
else
DEBUG_WARN("Failed to initialize the clipboard interface, continuing anyway");
initSDLCursor(); initSDLCursor();
if (params.hideMouse) if (params.hideMouse)
SDL_ShowCursor(SDL_DISABLE); SDL_ShowCursor(SDL_DISABLE);
@ -2081,6 +2066,21 @@ static int lg_run()
wmInit(); wmInit();
for (LG_Clipboard ** clipboard = LG_Clipboards; *clipboard; clipboard++)
if ((*clipboard)->init(&g_state.wminfo, clipboardRelease, clipboardNotify, clipboardData))
{
g_state.lgc = *clipboard;
break;
}
if (g_state.lgc)
{
DEBUG_INFO("Using Clipboard: %s", g_state.lgc->getName());
g_state.cbRequestList = ll_new();
}
else
DEBUG_WARN("Failed to initialize the clipboard interface, continuing anyway");
LGMP_STATUS status; LGMP_STATUS status;
while(g_state.state == APP_STATE_RUNNING) while(g_state.state == APP_STATE_RUNNING)

View File

@ -21,8 +21,9 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include "main.h" #include "main.h"
#include <stdbool.h> #include <stdbool.h>
#include <SDL2/SDL.h> #include <unistd.h>
#include <SDL2/SDL.h>
#include <wayland-client.h> #include <wayland-client.h>
#include "common/debug.h" #include "common/debug.h"
@ -31,15 +32,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include "wayland-pointer-constraints-unstable-v1-client-protocol.h" #include "wayland-pointer-constraints-unstable-v1-client-protocol.h"
#include "wayland-relative-pointer-unstable-v1-client-protocol.h" #include "wayland-relative-pointer-unstable-v1-client-protocol.h"
struct WMState struct WMState g_wmState;
{
bool pointerGrabbed;
bool keyboardGrabbed;
void * opaque;
};
static struct WMState g_wmState;
static void wmWaylandInit(); static void wmWaylandInit();
static void wmWaylandFree(); static void wmWaylandFree();
@ -215,24 +208,6 @@ void wmWarpMouse(int x, int y)
// Wayland support. // Wayland support.
struct WMDataWayland
{
struct wl_display * display;
struct wl_surface * surface;
struct wl_registry * registry;
struct wl_seat * seat;
uint32_t capabilities;
struct wl_pointer * pointer;
struct zwp_relative_pointer_manager_v1 * relativePointerManager;
struct zwp_pointer_constraints_v1 * pointerConstraints;
struct zwp_relative_pointer_v1 * relativePointer;
struct zwp_confined_pointer_v1 * confinedPointer;
struct zwp_keyboard_shortcuts_inhibit_manager_v1 * keyboardInhibitManager;
struct zwp_keyboard_shortcuts_inhibitor_v1 * keyboardInhibitor;
};
// Registry-handling listeners. // Registry-handling listeners.
static void registryGlobalHandler(void * data, struct wl_registry * registry, static void registryGlobalHandler(void * data, struct wl_registry * registry,
@ -251,6 +226,9 @@ static void registryGlobalHandler(void * data, struct wl_registry * registry,
else if (!strcmp(interface, zwp_keyboard_shortcuts_inhibit_manager_v1_interface.name)) else if (!strcmp(interface, zwp_keyboard_shortcuts_inhibit_manager_v1_interface.name))
wm->keyboardInhibitManager = wl_registry_bind(wm->registry, name, wm->keyboardInhibitManager = wl_registry_bind(wm->registry, name,
&zwp_keyboard_shortcuts_inhibit_manager_v1_interface, 1); &zwp_keyboard_shortcuts_inhibit_manager_v1_interface, 1);
else if (!strcmp(interface, wl_data_device_manager_interface.name))
wm->dataDeviceManager = wl_registry_bind(wm->registry, name,
&wl_data_device_manager_interface, 3);
} }
static void registryGlobalRemoveHandler(void * data, static void registryGlobalRemoveHandler(void * data,
@ -264,6 +242,48 @@ static const struct wl_registry_listener registryListener = {
.global_remove = registryGlobalRemoveHandler, .global_remove = registryGlobalRemoveHandler,
}; };
// Keyboard-handling listeners.
static void keyboardKeymapHandler(void * data, struct wl_keyboard * keyboard,
uint32_t format, int fd, uint32_t size)
{
close(fd);
}
static void keyboardEnterHandler(void * data, struct wl_keyboard * keyboard,
uint32_t serial, struct wl_surface * surface, struct wl_array * keys)
{
struct WMDataWayland * wm = data;
wm->keyboardEnterSerial = serial;
}
static void keyboardLeaveHandler(void * data, struct wl_keyboard * keyboard,
uint32_t serial, struct wl_surface * surface)
{
// Do nothing.
}
static void keyboardKeyHandler(void * data, struct wl_keyboard * keyboard,
uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
{
// Do nothing.
}
static void keyboardModifiersHandler(void * data,
struct wl_keyboard * keyboard, uint32_t serial, uint32_t modsDepressed,
uint32_t modsLatched, uint32_t modsLocked, uint32_t group)
{
// Do nothing.
}
static const struct wl_keyboard_listener keyboardListener = {
.keymap = keyboardKeymapHandler,
.enter = keyboardEnterHandler,
.leave = keyboardLeaveHandler,
.key = keyboardKeyHandler,
.modifiers = keyboardModifiersHandler,
};
// Seat-handling listeners. // Seat-handling listeners.
static void handlePointerCapability(struct WMDataWayland * wm, static void handlePointerCapability(struct WMDataWayland * wm,
@ -279,12 +299,29 @@ static void handlePointerCapability(struct WMDataWayland * wm,
wm->pointer = wl_seat_get_pointer(wm->seat); wm->pointer = wl_seat_get_pointer(wm->seat);
} }
static void handleKeyboardCapability(struct WMDataWayland * wm,
uint32_t capabilities)
{
bool hasKeyboard = capabilities & WL_SEAT_CAPABILITY_KEYBOARD;
if (!hasKeyboard && wm->keyboard)
{
wl_keyboard_destroy(wm->keyboard);
wm->keyboard = NULL;
}
else if (hasKeyboard && !wm->keyboard)
{
wm->keyboard = wl_seat_get_keyboard(wm->seat);
wl_keyboard_add_listener(wm->keyboard, &keyboardListener, wm);
}
}
static void seatCapabilitiesHandler(void * data, struct wl_seat * seat, static void seatCapabilitiesHandler(void * data, struct wl_seat * seat,
uint32_t capabilities) uint32_t capabilities)
{ {
struct WMDataWayland * wm = data; struct WMDataWayland * wm = data;
wm->capabilities = capabilities; wm->capabilities = capabilities;
handlePointerCapability(wm, capabilities); handlePointerCapability(wm, capabilities);
handleKeyboardCapability(wm, capabilities);
} }
static void seatNameHandler(void * data, struct wl_seat * seat, static void seatNameHandler(void * data, struct wl_seat * seat,
@ -313,6 +350,9 @@ void wmWaylandInit()
wl_seat_add_listener(wm->seat, &seatListener, wm); wl_seat_add_listener(wm->seat, &seatListener, wm);
wl_display_roundtrip(wm->display); wl_display_roundtrip(wm->display);
wm->dataDevice = wl_data_device_manager_get_data_device(
wm->dataDeviceManager, wm->seat);
g_wmState.opaque = wm; g_wmState.opaque = wm;
} }

View File

@ -17,6 +17,43 @@ this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#include <stdbool.h>
#include <stdint.h>
struct WMState
{
bool pointerGrabbed;
bool keyboardGrabbed;
void * opaque;
};
struct WMDataWayland
{
struct wl_display * display;
struct wl_surface * surface;
struct wl_registry * registry;
struct wl_seat * seat;
struct wl_data_device_manager * dataDeviceManager;
struct wl_data_device * dataDevice;
uint32_t capabilities;
struct wl_keyboard * keyboard;
struct zwp_keyboard_shortcuts_inhibit_manager_v1 * keyboardInhibitManager;
struct zwp_keyboard_shortcuts_inhibitor_v1 * keyboardInhibitor;
uint32_t keyboardEnterSerial;
struct wl_pointer * pointer;
struct zwp_relative_pointer_manager_v1 * relativePointerManager;
struct zwp_pointer_constraints_v1 * pointerConstraints;
struct zwp_relative_pointer_v1 * relativePointer;
struct zwp_confined_pointer_v1 * confinedPointer;
};
struct WMState g_wmState;
void wmInit(); void wmInit();
void wmFree(); void wmFree();
void wmGrabPointer(); void wmGrabPointer();