Merge pull request #975 from acrisci/fix-pointer-example

pointer example fixes
master
Drew DeVault 7 years ago committed by GitHub
commit 383ce3d5b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,7 +1,6 @@
#define _POSIX_C_SOURCE 200112L
#define _XOPEN_SOURCE 500
#include <assert.h>
#include <GLES2/gl2.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
@ -19,14 +18,14 @@
#include <wlr/types/wlr_list.h>
#include <wlr/types/wlr_matrix.h>
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_xcursor_manager.h>
#include <wlr/util/log.h>
#include <wlr/xcursor.h>
#include <xkbcommon/xkbcommon.h>
struct sample_state {
struct wl_display *display;
struct compositor_state *compositor;
struct wlr_xcursor *xcursor;
struct wlr_xcursor_manager *xcursor_manager;
struct wlr_cursor *cursor;
double cur_x, cur_y;
float default_color[4];
@ -61,50 +60,50 @@ struct touch_point {
};
struct sample_output {
struct sample_state *sample;
struct sample_state *state;
struct wlr_output *output;
struct wl_listener frame;
struct wl_listener destroy;
};
struct sample_keyboard {
struct sample_state *sample;
struct sample_state *state;
struct wlr_input_device *device;
struct wl_listener key;
struct wl_listener destroy;
};
static void warp_to_touch(struct sample_state *sample,
static void warp_to_touch(struct sample_state *state,
struct wlr_input_device *dev) {
if (wl_list_empty(&sample->touch_points)) {
if (wl_list_empty(&state->touch_points)) {
return;
}
double x = 0, y = 0;
size_t n = 0;
struct touch_point *point;
wl_list_for_each(point, &sample->touch_points, link) {
wl_list_for_each(point, &state->touch_points, link) {
x += point->x;
y += point->y;
n++;
}
x /= n;
y /= n;
wlr_cursor_warp_absolute(sample->cursor, dev, x, y);
wlr_cursor_warp_absolute(state->cursor, dev, x, y);
}
void output_frame_notify(struct wl_listener *listener, void *data) {
struct sample_output *sample_output = wl_container_of(listener, sample_output, frame);
struct sample_state *sample = sample_output->sample;
struct sample_state *state = sample_output->state;
struct wlr_output *wlr_output = sample_output->output;
struct wlr_renderer *renderer = wlr_backend_get_renderer(wlr_output->backend);
assert(renderer);
wlr_output_make_current(wlr_output, NULL);
glClearColor(sample->clear_color[0], sample->clear_color[1],
sample->clear_color[2], sample->clear_color[3]);
glClear(GL_COLOR_BUFFER_BIT);
wlr_renderer_begin(renderer, wlr_output->width, wlr_output->height);
wlr_renderer_clear(renderer, state->clear_color);
wlr_output_swap_buffers(wlr_output, NULL, NULL);
wlr_renderer_end(renderer);
}
static void handle_cursor_motion(struct wl_listener *listener, void *data) {
@ -225,7 +224,7 @@ static void handle_tablet_tool_axis(struct wl_listener *listener, void *data) {
void keyboard_key_notify(struct wl_listener *listener, void *data) {
struct sample_keyboard *keyboard = wl_container_of(listener, keyboard, key);
struct sample_state *sample = keyboard->sample;
struct sample_state *sample = keyboard->state;
struct wlr_event_keyboard_key *event = data;
uint32_t keycode = event->keycode + 8;
const xkb_keysym_t *syms;
@ -241,7 +240,7 @@ void keyboard_key_notify(struct wl_listener *listener, void *data) {
void output_remove_notify(struct wl_listener *listener, void *data) {
struct sample_output *sample_output = wl_container_of(listener, sample_output, destroy);
struct sample_state *sample = sample_output->sample;
struct sample_state *sample = sample_output->state;
wlr_output_layout_remove(sample->layout, sample_output->output);
wl_list_remove(&sample_output->frame.link);
wl_list_remove(&sample_output->destroy.link);
@ -257,17 +256,16 @@ void new_output_notify(struct wl_listener *listener, void *data) {
wlr_output_set_mode(output, mode);
}
sample_output->output = output;
sample_output->sample = sample;
sample_output->state = sample;
wl_signal_add(&output->events.frame, &sample_output->frame);
sample_output->frame.notify = output_frame_notify;
wl_signal_add(&output->events.destroy, &sample_output->destroy);
sample_output->destroy.notify = output_remove_notify;
wlr_output_layout_add_auto(sample->layout, sample_output->output);
struct wlr_xcursor_image *image = sample->xcursor->images[0];
wlr_cursor_set_image(sample->cursor, image->buffer, image->width * 4,
image->width, image->height, image->hotspot_x, image->hotspot_y, 0);
wlr_xcursor_manager_load(sample->xcursor_manager, output->scale);
wlr_xcursor_manager_set_cursor_image(sample->xcursor_manager, "left_ptr",
sample->cursor);
}
@ -280,18 +278,18 @@ void keyboard_destroy_notify(struct wl_listener *listener, void *data) {
void new_input_notify(struct wl_listener *listener, void *data) {
struct wlr_input_device *device = data;
struct sample_state *sample = wl_container_of(listener, sample, new_input);
struct sample_state *state = wl_container_of(listener, state, new_input);
switch (device->type) {
case WLR_INPUT_DEVICE_POINTER:
case WLR_INPUT_DEVICE_TOUCH:
case WLR_INPUT_DEVICE_TABLET_TOOL:
wlr_cursor_attach_input_device(sample->cursor, device);
wlr_cursor_attach_input_device(state->cursor, device);
break;
case WLR_INPUT_DEVICE_KEYBOARD:;
struct sample_keyboard *keyboard = calloc(1, sizeof(struct sample_keyboard));
keyboard->device = device;
keyboard->sample = sample;
keyboard->state = state;
wl_signal_add(&device->events.destroy, &keyboard->destroy);
keyboard->destroy.notify = keyboard_destroy_notify;
wl_signal_add(&device->keyboard->events.key, &keyboard->key);
@ -375,20 +373,14 @@ int main(int argc, char *argv[]) {
&state.tablet_tool_axis);
state.tablet_tool_axis.notify = handle_tablet_tool_axis;
struct wlr_xcursor_theme *theme = wlr_xcursor_theme_load("default", 16);
if (!theme) {
wlr_log(L_ERROR, "Failed to load cursor theme");
return 1;
}
state.xcursor = wlr_xcursor_theme_get_cursor(theme, "left_ptr");
if (!state.xcursor) {
state.xcursor_manager = wlr_xcursor_manager_create("default", 24);
if (!state.xcursor_manager) {
wlr_log(L_ERROR, "Failed to load left_ptr cursor");
return 1;
}
struct wlr_xcursor_image *image = state.xcursor->images[0];
wlr_cursor_set_image(state.cursor, image->buffer, image->width * 4,
image->width, image->height, image->hotspot_x, image->hotspot_y, 0);
wlr_xcursor_manager_set_cursor_image(state.xcursor_manager, "left_ptr",
state.cursor);
clock_gettime(CLOCK_MONOTONIC, &state.last_frame);
@ -400,7 +392,7 @@ int main(int argc, char *argv[]) {
wl_display_run(display);
wl_display_destroy(display);
wlr_xcursor_theme_destroy(theme);
wlr_xcursor_manager_destroy(state.xcursor_manager);
wlr_cursor_destroy(state.cursor);
wlr_output_layout_destroy(state.layout);
}

Loading…
Cancel
Save