2015-11-19 23:58:57 +11:00
|
|
|
#include <wayland-client.h>
|
|
|
|
#include <wayland-cursor.h>
|
|
|
|
#include "wayland-xdg-shell-client-protocol.h"
|
|
|
|
#include "wayland-desktop-shell-client-protocol.h"
|
|
|
|
#include <cairo/cairo.h>
|
|
|
|
#include <pango/pangocairo.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include "client/window.h"
|
|
|
|
#include "client/buffer.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
static void pointer_handle_enter(void *data, struct wl_pointer *pointer,
|
|
|
|
uint32_t serial, struct wl_surface *surface, wl_fixed_t sx_w, wl_fixed_t sy_w) {
|
|
|
|
struct window *window = data;
|
2016-01-27 10:38:05 +11:00
|
|
|
if (window->registry->pointer) {
|
|
|
|
struct wl_cursor_image *image = window->cursor.cursor->images[0];
|
|
|
|
wl_pointer_set_cursor(pointer, serial, window->cursor.surface, image->hotspot_x, image->hotspot_y);
|
|
|
|
}
|
2015-11-19 23:58:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pointer_handle_leave(void *data, struct wl_pointer *pointer,
|
|
|
|
uint32_t serial, struct wl_surface *surface) {
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pointer_handle_motion(void *data, struct wl_pointer *pointer,
|
|
|
|
uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w) {
|
2016-07-05 16:21:56 +10:00
|
|
|
struct window *window = data;
|
|
|
|
|
2016-07-11 15:03:15 +10:00
|
|
|
window->pointer_input.last_x = wl_fixed_to_int(sx_w);
|
|
|
|
window->pointer_input.last_y = wl_fixed_to_int(sy_w);
|
2015-11-19 23:58:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
|
|
|
|
uint32_t time, uint32_t button, uint32_t state_w) {
|
2016-07-05 16:21:56 +10:00
|
|
|
struct window *window = data;
|
|
|
|
struct pointer_input *input = &window->pointer_input;
|
|
|
|
|
2016-07-12 13:51:50 +10:00
|
|
|
if (window->pointer_input.notify_button) {
|
2016-07-16 23:09:22 +10:00
|
|
|
window->pointer_input.notify_button(window, input->last_x, input->last_y, button, state_w);
|
2016-07-05 16:21:56 +10:00
|
|
|
}
|
2015-11-19 23:58:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pointer_handle_axis(void *data, struct wl_pointer *pointer,
|
2016-07-12 14:15:23 +10:00
|
|
|
uint32_t time, uint32_t axis, wl_fixed_t value) {
|
|
|
|
struct window *window = data;
|
|
|
|
enum scroll_direction direction;
|
|
|
|
|
|
|
|
switch (axis) {
|
|
|
|
case 0:
|
|
|
|
direction = wl_fixed_to_double(value) < 0 ? SCROLL_UP : SCROLL_DOWN;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
direction = wl_fixed_to_double(value) < 0 ? SCROLL_LEFT : SCROLL_RIGHT;
|
|
|
|
break;
|
|
|
|
default:
|
2016-07-16 23:13:38 +10:00
|
|
|
sway_log(L_DEBUG, "Unexpected axis value on mouse scroll");
|
|
|
|
return;
|
2016-07-12 14:15:23 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
if (window->pointer_input.notify_scroll) {
|
|
|
|
window->pointer_input.notify_scroll(window, direction);
|
|
|
|
}
|
2015-11-19 23:58:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_pointer_listener pointer_listener = {
|
|
|
|
.enter = pointer_handle_enter,
|
|
|
|
.leave = pointer_handle_leave,
|
|
|
|
.motion = pointer_handle_motion,
|
|
|
|
.button = pointer_handle_button,
|
|
|
|
.axis = pointer_handle_axis
|
|
|
|
};
|
|
|
|
|
|
|
|
void shell_surface_configure(void *data, struct wl_shell_surface *wl_shell_surface,
|
|
|
|
uint32_t edges, int32_t width, int32_t height) {
|
|
|
|
struct window *window = data;
|
|
|
|
window->width = width;
|
|
|
|
window->height = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_shell_surface_listener surface_listener = {
|
|
|
|
.configure = shell_surface_configure
|
|
|
|
};
|
|
|
|
|
2016-07-05 08:01:37 +10:00
|
|
|
void window_make_shell(struct window *window) {
|
|
|
|
window->shell_surface = wl_shell_get_shell_surface(window->registry->shell, window->surface);
|
|
|
|
wl_shell_surface_add_listener(window->shell_surface, &surface_listener, window);
|
|
|
|
wl_shell_surface_set_toplevel(window->shell_surface);
|
|
|
|
}
|
|
|
|
|
2016-09-06 01:36:48 +10:00
|
|
|
struct window *window_setup(struct registry *registry, uint32_t width, uint32_t height,
|
|
|
|
int32_t scale, bool shell_surface) {
|
2015-11-19 23:58:57 +11:00
|
|
|
struct window *window = malloc(sizeof(struct window));
|
|
|
|
memset(window, 0, sizeof(struct window));
|
|
|
|
window->width = width;
|
|
|
|
window->height = height;
|
2016-09-06 01:36:48 +10:00
|
|
|
window->scale = scale;
|
2015-11-19 23:58:57 +11:00
|
|
|
window->registry = registry;
|
2015-11-30 07:27:28 +11:00
|
|
|
window->font = "monospace 10";
|
2015-11-19 23:58:57 +11:00
|
|
|
|
|
|
|
window->surface = wl_compositor_create_surface(registry->compositor);
|
|
|
|
if (shell_surface) {
|
2016-07-05 08:01:37 +10:00
|
|
|
window_make_shell(window);
|
2015-11-19 23:58:57 +11:00
|
|
|
}
|
|
|
|
if (registry->pointer) {
|
|
|
|
wl_pointer_add_listener(registry->pointer, &pointer_listener, window);
|
|
|
|
}
|
|
|
|
|
2015-11-30 07:27:28 +11:00
|
|
|
get_next_buffer(window);
|
|
|
|
|
2016-01-27 10:38:05 +11:00
|
|
|
if (registry->pointer) {
|
2016-07-18 00:59:03 +10:00
|
|
|
char *cursor_theme = getenv("SWAY_CURSOR_THEME");
|
|
|
|
if (!cursor_theme) {
|
|
|
|
cursor_theme = "default";
|
|
|
|
}
|
|
|
|
char *cursor_size = getenv("SWAY_CURSOR_SIZE");
|
|
|
|
if (!cursor_size) {
|
|
|
|
cursor_size = "16";
|
|
|
|
}
|
|
|
|
|
2016-09-06 01:36:48 +10:00
|
|
|
sway_log(L_DEBUG, "Cursor scale: %d", scale);
|
2016-07-18 00:59:03 +10:00
|
|
|
window->cursor.cursor_theme = wl_cursor_theme_load(cursor_theme,
|
2016-09-06 01:36:48 +10:00
|
|
|
atoi(cursor_size) * scale, registry->shm);
|
2016-01-27 10:38:05 +11:00
|
|
|
window->cursor.cursor = wl_cursor_theme_get_cursor(window->cursor.cursor_theme, "left_ptr");
|
|
|
|
window->cursor.surface = wl_compositor_create_surface(registry->compositor);
|
|
|
|
|
|
|
|
struct wl_cursor_image *image = window->cursor.cursor->images[0];
|
|
|
|
struct wl_buffer *cursor_buf = wl_cursor_image_get_buffer(image);
|
|
|
|
wl_surface_attach(window->cursor.surface, cursor_buf, 0, 0);
|
2016-09-06 01:36:48 +10:00
|
|
|
wl_surface_set_buffer_scale(window->cursor.surface, scale);
|
|
|
|
wl_surface_damage(window->cursor.surface, 0, 0,
|
|
|
|
image->width, image->height);
|
2016-01-27 10:38:05 +11:00
|
|
|
wl_surface_commit(window->cursor.surface);
|
|
|
|
}
|
2015-11-19 23:58:57 +11:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void frame_callback(void *data, struct wl_callback *callback, uint32_t time) {
|
|
|
|
struct window *window = data;
|
|
|
|
wl_callback_destroy(callback);
|
|
|
|
window->frame_cb = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_callback_listener listener = {
|
|
|
|
frame_callback
|
|
|
|
};
|
|
|
|
|
|
|
|
int window_prerender(struct window *window) {
|
|
|
|
if (window->frame_cb) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
get_next_buffer(window);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int window_render(struct window *window) {
|
|
|
|
window->frame_cb = wl_surface_frame(window->surface);
|
|
|
|
wl_callback_add_listener(window->frame_cb, &listener, window);
|
|
|
|
|
|
|
|
wl_surface_attach(window->surface, window->buffer->buffer, 0, 0);
|
2016-09-06 01:36:48 +10:00
|
|
|
wl_surface_set_buffer_scale(window->surface, window->scale);
|
|
|
|
wl_surface_damage(window->surface, 0, 0, window->width, window->height);
|
2015-11-19 23:58:57 +11:00
|
|
|
wl_surface_commit(window->surface);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void window_teardown(struct window *window) {
|
2015-11-30 07:27:28 +11:00
|
|
|
// TODO
|
2015-11-19 23:58:57 +11:00
|
|
|
}
|