#include <xkbcommon/xkbcommon.h>
#include "eloop.h"
#include "log.h"
+#include "shl_array.h"
#include "shl_dlist.h"
#include "shl_hook.h"
+#include "shl_misc.h"
#include "tsm_vte.h"
#include "wlt_toolkit.h"
STATE_HUP,
};
+struct data_offer {
+ unsigned long ref;
+ struct wl_data_offer *w_offer;
+ struct wlt_display *disp;
+ struct shl_array *types;
+};
+
struct wlt_display {
unsigned long ref;
struct ev_eloop *eloop;
struct wlt_window *keyboard_focus;
struct ev_timer *repeat_timer;
uint32_t repeat_sym;
+
+ struct wl_data_device_manager *w_manager;
+ struct wl_data_device *w_data_dev;
+ struct data_offer *drag_offer;
+ struct data_offer *selection_offer;
};
struct wlt_window {
unsigned int saved_height;
unsigned int resize_edges;
bool maximized;
+ bool fullscreen;
struct wlt_shm_buffer buffer;
struct wl_callback *w_frame;
ev_timer_update(disp->repeat_timer, NULL);
}
-static unsigned int get_effective_modmask(struct xkb_state *state)
-{
- unsigned int mods = 0;
-
- if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_SHIFT,
- XKB_STATE_EFFECTIVE))
- mods |= TSM_SHIFT_MASK;
- if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
- XKB_STATE_EFFECTIVE))
- mods |= TSM_LOCK_MASK;
- if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
- XKB_STATE_EFFECTIVE))
- mods |= TSM_CONTROL_MASK;
- if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
- XKB_STATE_EFFECTIVE))
- mods |= TSM_MOD1_MASK;
- if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_LOGO,
- XKB_STATE_EFFECTIVE))
- mods |= TSM_MOD4_MASK;
-
- return mods;
-}
-
static void keyboard_key(void *data, struct wl_keyboard *keyboard,
uint32_t serial, uint32_t time, uint32_t key,
uint32_t state_w)
struct shl_dlist *iter;
struct wlt_widget *widget;
struct itimerspec spec;
+ bool handled;
disp->last_serial = serial;
if (!disp->xkb_state)
if (!wnd)
return;
- mask = get_effective_modmask(disp->xkb_state);
+ mask = shl_get_xkb_mods(disp->xkb_state);
num_syms = xkb_key_get_syms(disp->xkb_state, code, &syms);
sym = XKB_KEY_NoSymbol;
if (num_syms == 1)
sym = syms[0];
+ handled = false;
shl_dlist_for_each(iter, &wnd->widget_list) {
widget = shl_dlist_entry(iter, struct wlt_widget, list);
- if (widget->keyboard_cb)
- widget->keyboard_cb(widget, mask, sym, state,
- widget->data);
+ if (widget->keyboard_cb) {
+ if (widget->keyboard_cb(widget, mask, sym, state,
+ handled, widget->data))
+ handled = true;
+ }
}
if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
struct wlt_widget *widget;
struct shl_dlist *iter;
unsigned int mask;
+ bool handled;
if (!wnd)
return;
- mask = get_effective_modmask(disp->xkb_state);
+ mask = shl_get_xkb_mods(disp->xkb_state);
+ handled = false;
shl_dlist_for_each(iter, &wnd->widget_list) {
widget = shl_dlist_entry(iter, struct wlt_widget, list);
- if (widget->keyboard_cb)
- widget->keyboard_cb(widget, mask, disp->repeat_sym,
- WL_KEYBOARD_KEY_STATE_PRESSED,
- widget->data);
+ if (widget->keyboard_cb) {
+ if (widget->keyboard_cb(widget, mask, disp->repeat_sym,
+ WL_KEYBOARD_KEY_STATE_PRESSED,
+ handled, widget->data))
+ handled = true;
+ }
}
}
.modifiers = keyboard_modifiers,
};
+static void data_offer_unref(struct data_offer *offer)
+{
+ unsigned int i, len;
+
+ if (!offer || !offer->ref || --offer->ref)
+ return;
+
+ len = shl_array_get_length(offer->types);
+ for (i = 0; i < len; ++i)
+ free(*SHL_ARRAY_AT(offer->types, char*, i));
+ shl_array_free(offer->types);
+ wl_data_offer_destroy(offer->w_offer);
+ free(offer);
+}
+
+static void data_offer_offer(void *data, struct wl_data_offer *w_offer,
+ const char *type)
+{
+ char *tmp;
+ int ret;
+ struct data_offer *offer = wl_data_offer_get_user_data(w_offer);
+
+ tmp = strdup(type);
+ if (!tmp)
+ return;
+
+ ret = shl_array_push(offer->types, &tmp);
+ if (ret) {
+ free(tmp);
+ return;
+ }
+}
+
+static const struct wl_data_offer_listener data_offer_listener = {
+ .offer = data_offer_offer,
+};
+
+static void data_dev_data_offer(void *data, struct wl_data_device *w_dev,
+ struct wl_data_offer *w_offer)
+{
+ struct data_offer *offer;
+ struct wlt_display *disp = data;
+ int ret;
+
+ offer = malloc(sizeof(*offer));
+ if (!offer) {
+ wl_data_offer_destroy(w_offer);
+ return;
+ }
+ memset(offer, 0, sizeof(*offer));
+ offer->ref = 1;
+ offer->w_offer = w_offer;
+ offer->disp = disp;
+
+ ret = shl_array_new(&offer->types, sizeof(char*), 4);
+ if (ret) {
+ wl_data_offer_destroy(w_offer);
+ free(offer);
+ return;
+ }
+
+ wl_data_offer_add_listener(w_offer, &data_offer_listener, offer);
+ wl_data_offer_set_user_data(w_offer, offer);
+}
+
+static void data_dev_enter(void *data, struct wl_data_device *w_dev,
+ uint32_t serial, struct wl_surface *w_surface,
+ wl_fixed_t x, wl_fixed_t y,
+ struct wl_data_offer *w_offer)
+{
+ struct wlt_display *disp = data;
+
+ if (disp->drag_offer) {
+ data_offer_unref(disp->drag_offer);
+ disp->drag_offer = NULL;
+ }
+
+ if (!w_offer)
+ return;
+
+ disp->drag_offer = wl_data_offer_get_user_data(w_offer);
+}
+
+static void data_dev_leave(void *data, struct wl_data_device *w_dev)
+{
+ struct wlt_display *disp = data;
+
+ if (disp->drag_offer) {
+ data_offer_unref(disp->drag_offer);
+ disp->drag_offer = NULL;
+ }
+}
+
+static void data_dev_motion(void *data, struct wl_data_device *w_dev,
+ uint32_t time, wl_fixed_t x, wl_fixed_t y)
+{
+}
+
+static void data_dev_drop(void *data, struct wl_data_device *w_dev)
+{
+}
+
+static void data_dev_selection(void *data, struct wl_data_device *w_dev,
+ struct wl_data_offer *w_offer)
+{
+ struct wlt_display *disp = data;
+
+ if (disp->selection_offer) {
+ data_offer_unref(disp->selection_offer);
+ disp->selection_offer = NULL;
+ }
+
+ if (!w_offer)
+ return;
+
+ disp->selection_offer = wl_data_offer_get_user_data(w_offer);
+}
+
+int wlt_display_get_selection_to_fd(struct wlt_display *disp, const char *mime,
+ int output_fd)
+{
+ unsigned int i, num;
+ struct data_offer *offer;
+
+ if (!disp || !mime)
+ return -EINVAL;
+ if (!disp->selection_offer)
+ return -ENOENT;
+
+ offer = disp->selection_offer;
+ num = shl_array_get_length(offer->types);
+ for (i = 0; i < num; ++i) {
+ if (!strcmp(mime, *SHL_ARRAY_AT(offer->types, char*, i)))
+ break;
+ }
+
+ if (i == num)
+ return -EAGAIN;
+
+ wl_data_offer_receive(offer->w_offer, mime, output_fd);
+ return 0;
+}
+
+int wlt_display_get_selection_fd(struct wlt_display *disp, const char *mime)
+{
+ int p[2], ret;
+
+ if (pipe2(p, O_CLOEXEC | O_NONBLOCK))
+ return -EFAULT;
+
+ ret = wlt_display_get_selection_to_fd(disp, mime, p[1]);
+ close(p[1]);
+
+ if (ret) {
+ close(p[0]);
+ return ret;
+ }
+
+ return p[0];
+}
+
+int wlt_display_new_data_source(struct wlt_display *disp,
+ struct wl_data_source **out)
+{
+ struct wl_data_source *src;
+
+ if (!disp)
+ return -EINVAL;
+
+ src = wl_data_device_manager_create_data_source(disp->w_manager);
+ if (!src)
+ return -EFAULT;
+
+ *out = src;
+ return 0;
+}
+
+void wlt_display_set_selection(struct wlt_display *disp,
+ struct wl_data_source *selection)
+{
+ if (!disp)
+ return;
+
+ wl_data_device_set_selection(disp->w_data_dev, selection,
+ disp->last_serial);
+}
+
+static const struct wl_data_device_listener data_dev_listener = {
+ .data_offer = data_dev_data_offer,
+ .enter = data_dev_enter,
+ .leave = data_dev_leave,
+ .motion = data_dev_motion,
+ .drop = data_dev_drop,
+ .selection = data_dev_selection,
+};
+
static void check_ready(struct wlt_display *disp)
{
if (disp->state > STATE_INIT)
disp->w_shell &&
disp->w_shm &&
disp->w_pointer &&
- disp->w_keyboard) {
+ disp->w_keyboard &&
+ disp->w_manager) {
log_debug("wayland display initialized");
load_cursors(disp);
+ disp->w_data_dev = wl_data_device_manager_get_data_device(
+ disp->w_manager, disp->w_seat);
+ wl_data_device_add_listener(disp->w_data_dev,
+ &data_dev_listener,
+ disp);
+
disp->state = STATE_RUNNING;
shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_READY);
}
log_error("cannot bind wl_shm object");
return;
}
+ } else if (!strcmp(iface, "wl_data_device_manager")) {
+ if (disp->w_manager) {
+ log_error("global wl_data_device_manager advertised twice");
+ return;
+ }
+ disp->w_manager = wl_display_bind(disp->dp, id,
+ &wl_data_device_manager_interface);
+ if (!disp->w_manager) {
+ log_error("cannot bind wl_data_device_manager_object");
+ return;
+ }
} else {
log_debug("ignoring new unknown global %s", iface);
return;
{
struct shl_dlist *iter;
struct wlt_widget *widget;
- unsigned int x, y;
+ unsigned int x, y, flags;
struct wlt_rect alloc;
+ flags = 0;
+ if (wnd->maximized)
+ flags |= WLT_WINDOW_MAXIMIZED;
+ if (wnd->fullscreen)
+ flags |= WLT_WINDOW_FULLSCREEN;
+
alloc.x = 0;
alloc.y = 0;
alloc.width = wnd->buffer.width;
shl_dlist_for_each(iter, &wnd->widget_list) {
widget = shl_dlist_entry(iter, struct wlt_widget, list);
if (widget->resize_cb)
- widget->resize_cb(widget, &alloc, widget->data);
+ widget->resize_cb(widget, flags, &alloc, widget->data);
}
memset(wnd->buffer.data, 0,
shl_dlist_for_each(iter, &wnd->widget_list) {
widget = shl_dlist_entry(iter, struct wlt_widget, list);
if (widget->redraw_cb)
- widget->redraw_cb(widget, widget->data);
+ widget->redraw_cb(widget, flags, widget->data);
}
wnd->skip_damage = false;
}
static int resize_window(struct wlt_window *wnd, unsigned int width,
- unsigned int height)
+ unsigned int height, bool force_redraw)
{
struct shl_dlist *iter;
struct wlt_widget *widget;
struct wlt_pool *old_pool = NULL;
size_t nsize;
int ret;
- unsigned int oldw, oldh;
+ unsigned int oldw, oldh, neww, newh, minw, minh, flags;
+
+ if (!width)
+ width = wnd->buffer.width;
+ if (!height)
+ height = wnd->buffer.height;
- if (!wnd || !width || !height)
+ if (!width || !height)
return -EINVAL;
+ flags = 0;
+ if (wnd->maximized)
+ flags |= WLT_WINDOW_MAXIMIZED;
+ if (wnd->fullscreen)
+ flags |= WLT_WINDOW_FULLSCREEN;
+
+ neww = 0;
+ newh = 0;
+ minw = 0;
+ minh = 0;
shl_dlist_for_each(iter, &wnd->widget_list) {
widget = shl_dlist_entry(iter, struct wlt_widget, list);
if (widget->prepare_resize_cb)
- widget->prepare_resize_cb(widget, &width, &height,
+ widget->prepare_resize_cb(widget,
+ flags,
+ width, height,
+ &minw, &minh,
+ &neww, &newh,
widget->data);
}
+ if (neww)
+ width = neww;
+ if (newh)
+ height = newh;
+
if (width == wnd->buffer.width &&
- height == wnd->buffer.height)
+ height == wnd->buffer.height) {
+ if (force_redraw)
+ wlt_window_do_redraw(wnd, width, height);
return 0;
+ }
oldw = wnd->buffer.width;
oldh = wnd->buffer.height;
static void do_frame(struct wlt_window *wnd)
{
+ bool force;
+
wnd->idle_pending = false;
ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
if (wnd->need_resize) {
+ force = wnd->need_redraw;
wnd->need_frame = true;
wnd->need_resize = false;
wnd->need_redraw = false;
- resize_window(wnd, wnd->new_width, wnd->new_height);
+ resize_window(wnd, wnd->new_width, wnd->new_height, force);
}
if (wnd->need_redraw) {
&shell_surface_listener, wnd);
wl_shell_surface_set_toplevel(wnd->w_shell_surface);
- ret = resize_window(wnd, width, height);
+ ret = resize_window(wnd, width, height, true);
if (ret)
goto err_shell_surface;
widget->wnd = wnd;
widget->data = data;
- wlt_window_schedule_redraw(wnd);
+ wlt_window_set_size(wnd, wnd->buffer.width, wnd->buffer.height);
shl_dlist_link_tail(&wnd->widget_list, &widget->list);
*out = widget;
return 0;
int wlt_window_set_size(struct wlt_window *wnd,
unsigned int width, unsigned int height)
{
- if (!wnd || !width || !height)
+ if (!wnd)
return -EINVAL;
wnd->new_width = width;
return;
if (wnd->maximized) {
- wl_shell_surface_set_toplevel(wnd->w_shell_surface);
- wlt_window_set_size(wnd, wnd->saved_width, wnd->saved_height);
+ if (!wnd->fullscreen) {
+ wl_shell_surface_set_toplevel(wnd->w_shell_surface);
+ wlt_window_set_size(wnd, wnd->saved_width,
+ wnd->saved_height);
+ }
} else {
- wnd->saved_width = wnd->buffer.width;
- wnd->saved_height = wnd->buffer.height;
- wl_shell_surface_set_maximized(wnd->w_shell_surface, NULL);
+ if (!wnd->fullscreen) {
+ wnd->saved_width = wnd->buffer.width;
+ wnd->saved_height = wnd->buffer.height;
+ wl_shell_surface_set_maximized(wnd->w_shell_surface,
+ NULL);
+ }
}
wnd->maximized = !wnd->maximized;
}
+void wlt_window_toggle_fullscreen(struct wlt_window *wnd)
+{
+ if (!wnd)
+ return;
+
+ if (wnd->fullscreen) {
+ if (wnd->maximized) {
+ wl_shell_surface_set_maximized(wnd->w_shell_surface,
+ NULL);
+ } else {
+ wl_shell_surface_set_toplevel(wnd->w_shell_surface);
+ wlt_window_set_size(wnd, wnd->saved_width,
+ wnd->saved_height);
+ }
+ } else {
+ if (!wnd->maximized) {
+ wnd->saved_width = wnd->buffer.width;
+ wnd->saved_height = wnd->buffer.height;
+ }
+ wl_shell_surface_set_fullscreen(wnd->w_shell_surface,
+ WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
+ 0, NULL);
+ }
+
+ wnd->fullscreen = !wnd->fullscreen;
+}
+
struct ev_eloop *wlt_window_get_eloop(struct wlt_window *wnd)
{
if (!wnd)
return wnd->disp->eloop;
}
+struct wlt_display *wlt_window_get_display(struct wlt_window *wnd)
+{
+ if (!wnd)
+ return NULL;
+
+ return wnd->disp;
+}
+
void wlt_widget_destroy(struct wlt_widget *widget)
{
if (!widget)