wlroots/examples/pointer.c

153 lines
4.4 KiB
C
Raw Normal View History

2017-06-13 16:21:36 +00:00
#define _POSIX_C_SOURCE 199309L
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
2017-06-21 18:15:06 +00:00
#include <math.h>
2017-06-13 16:21:36 +00:00
#include <wayland-server.h>
#include <wayland-server-protocol.h>
#include <xkbcommon/xkbcommon.h>
2017-06-23 15:38:45 +00:00
#include <GLES2/gl2.h>
2017-06-13 16:21:36 +00:00
#include <wlr/render/matrix.h>
2017-06-23 15:38:45 +00:00
#include <wlr/render/gles2.h>
2017-06-13 16:21:36 +00:00
#include <wlr/render.h>
#include <wlr/backend.h>
2017-07-11 07:18:34 +00:00
#include <wlr/backend/session.h>
2017-06-21 14:27:45 +00:00
#include <wlr/types/wlr_keyboard.h>
2017-08-08 01:13:04 +00:00
#include <wlr/xcursor.h>
2017-06-21 18:15:06 +00:00
#include <wlr/util/log.h>
2017-06-13 16:21:36 +00:00
#include "shared.h"
#include "cat.h"
struct sample_state {
struct wlr_xcursor *cursor;
2017-06-26 05:34:15 +00:00
double cur_x, cur_y;
2017-06-13 16:38:57 +00:00
float default_color[4];
float clear_color[4];
2017-06-13 16:21:36 +00:00
};
static void handle_output_frame(struct output_state *output, struct timespec *ts) {
struct compositor_state *state = output->compositor;
struct sample_state *sample = state->data;
struct wlr_output *wlr_output = output->output;
2017-06-26 05:34:15 +00:00
wlr_output_make_current(wlr_output);
glClearColor(sample->clear_color[0], sample->clear_color[1],
sample->clear_color[2], sample->clear_color[3]);
glClear(GL_COLOR_BUFFER_BIT);
2017-06-13 16:21:36 +00:00
2017-06-26 05:34:15 +00:00
wlr_output_swap_buffers(wlr_output);
2017-06-13 16:21:36 +00:00
}
static void handle_pointer_motion(struct pointer_state *pstate,
double d_x, double d_y) {
struct sample_state *state = pstate->compositor->data;
state->cur_x += d_x;
state->cur_y += d_y;
2017-06-26 05:34:15 +00:00
struct wlr_xcursor_image *image = state->cursor->images[0];
2017-06-26 05:34:15 +00:00
struct output_state *output;
wl_list_for_each(output, &pstate->compositor->outputs, link) {
wlr_output_move_cursor(output->output,
state->cur_x - image->hotspot_x,
state->cur_y - image->hotspot_y);
2017-06-26 05:34:15 +00:00
}
2017-06-13 16:21:36 +00:00
}
static void handle_pointer_motion_absolute(struct pointer_state *pstate,
double x, double y) {
struct sample_state *state = pstate->compositor->data;
state->cur_x = x;
state->cur_y = y;
2017-06-26 05:34:15 +00:00
struct wlr_xcursor_image *image = state->cursor->images[0];
2017-06-26 05:34:15 +00:00
struct output_state *output;
wl_list_for_each(output, &pstate->compositor->outputs, link) {
wlr_output_move_cursor(output->output,
state->cur_x - image->hotspot_x,
state->cur_y - image->hotspot_y);
2017-06-26 05:34:15 +00:00
}
}
static void handle_pointer_button(struct pointer_state *pstate,
uint32_t button, enum wlr_button_state state) {
struct sample_state *sample = pstate->compositor->data;
float (*color)[4];
if (state == WLR_BUTTON_RELEASED) {
2017-06-13 16:38:57 +00:00
color = &sample->default_color;
} else {
float red[4] = { 0.25f, 0.25f, 0.25f, 1 };
red[button % 3] = 1;
color = &red;
}
memcpy(&sample->clear_color, color, sizeof(*color));
}
2017-06-13 16:38:57 +00:00
static void handle_pointer_axis(struct pointer_state *pstate,
enum wlr_axis_source source,
enum wlr_axis_orientation orientation,
double delta) {
struct sample_state *sample = pstate->compositor->data;
for (size_t i = 0; i < 3; ++i) {
sample->default_color[i] += delta > 0 ? -0.05f : 0.05f;
if (sample->default_color[i] > 1.0f) {
sample->default_color[i] = 1.0f;
}
if (sample->default_color[i] < 0.0f) {
sample->default_color[i] = 0.0f;
}
}
memcpy(&sample->clear_color, &sample->default_color,
sizeof(sample->clear_color));
}
2017-06-16 19:38:34 +00:00
static void handle_output_add(struct output_state *ostate) {
2017-08-08 01:13:04 +00:00
struct sample_state *state = ostate->compositor->data;
2017-06-16 19:38:34 +00:00
struct wlr_output *wlr_output = ostate->output;
struct wlr_xcursor_image *image = state->cursor->images[0];
2017-08-08 01:13:04 +00:00
if (!wlr_output_set_cursor(wlr_output, image->buffer,
image->width, image->width, image->height)) {
2017-06-21 18:15:06 +00:00
wlr_log(L_DEBUG, "Failed to set hardware cursor");
2017-06-16 19:38:34 +00:00
return;
}
if (!wlr_output_move_cursor(wlr_output, 0, 0)) {
2017-06-21 18:15:06 +00:00
wlr_log(L_DEBUG, "Failed to move hardware cursor");
2017-06-16 19:38:34 +00:00
}
}
2017-06-13 16:21:36 +00:00
int main(int argc, char *argv[]) {
struct sample_state state = {
2017-06-13 16:38:57 +00:00
.default_color = { 0.25f, 0.25f, 0.25f, 1 },
.clear_color = { 0.25f, 0.25f, 0.25f, 1 }
};
struct compositor_state compositor = { 0 };
compositor.data = &state;
compositor.output_add_cb = handle_output_add;
compositor.output_frame_cb = handle_output_frame;
compositor.pointer_motion_cb = handle_pointer_motion;
compositor.pointer_motion_absolute_cb = handle_pointer_motion_absolute;
compositor.pointer_button_cb = handle_pointer_button;
compositor.pointer_axis_cb = handle_pointer_axis;
2017-06-13 16:21:36 +00:00
struct wlr_xcursor_theme *theme = wlr_xcursor_theme_load("default", 16);
2017-08-08 01:13:04 +00:00
if (!theme) {
wlr_log(L_ERROR, "Failed to load cursor theme");
return 1;
}
state.cursor = wlr_xcursor_theme_get_cursor(theme, "left_ptr");
2017-08-08 01:13:04 +00:00
if (!state.cursor) {
wlr_log(L_ERROR, "Failed to load left_ptr cursor");
return 1;
}
compositor_init(&compositor);
2017-06-13 16:21:36 +00:00
compositor_run(&compositor);
2017-08-08 01:13:04 +00:00
wlr_xcursor_theme_destroy(theme);
2017-06-13 16:21:36 +00:00
}