Demonstrate pointer button event in example

This commit is contained in:
Drew DeVault 2017-06-13 12:31:24 -04:00
parent 2f9fd72fdc
commit 8f284ec0b2
3 changed files with 41 additions and 4 deletions

View file

@ -23,6 +23,7 @@ struct sample_state {
struct wlr_renderer *renderer; struct wlr_renderer *renderer;
struct wlr_surface *cat_texture; struct wlr_surface *cat_texture;
int cur_x, cur_y; int cur_x, cur_y;
float clear_color[4];
}; };
static void handle_output_frame(struct output_state *output, struct timespec *ts) { static void handle_output_frame(struct output_state *output, struct timespec *ts) {
@ -31,6 +32,9 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts
struct wlr_output *wlr_output = output->output; struct wlr_output *wlr_output = output->output;
wlr_renderer_begin(sample->renderer, wlr_output); wlr_renderer_begin(sample->renderer, wlr_output);
glClearColor(sample->clear_color[0], sample->clear_color[1],
sample->clear_color[2], sample->clear_color[3]);
glClear(GL_COLOR_BUFFER_BIT);
float matrix[16]; float matrix[16];
wlr_surface_get_matrix(sample->cat_texture, &matrix, wlr_surface_get_matrix(sample->cat_texture, &matrix,
@ -55,14 +59,32 @@ static void handle_pointer_motion(struct pointer_state *pstate,
state->cur_y += d_y; state->cur_y += d_y;
} }
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) {
float _default[4] = { 0.25f, 0.25f, 0.25f, 1 };
color = &_default;
} else {
float red[4] = { 0.25f, 0.25f, 0.25f, 1 };
red[button % 3] = 1;
color = &red;
}
memcpy(&sample->clear_color, color, sizeof(*color));
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
struct sample_state state = { 0 }; struct sample_state state = {
.clear_color = { 0.25f, 0.25f, 0.25f, 1 }
};
struct compositor_state compositor; struct compositor_state compositor;
compositor_init(&compositor); compositor_init(&compositor);
compositor.output_frame_cb = handle_output_frame; compositor.output_frame_cb = handle_output_frame;
compositor.pointer_motion_cb = handle_pointer_motion;
compositor.keyboard_key_cb = handle_keyboard_key; compositor.keyboard_key_cb = handle_keyboard_key;
compositor.pointer_motion_cb = handle_pointer_motion;
compositor.pointer_button_cb = handle_pointer_button;
state.renderer = wlr_gles3_renderer_init(); state.renderer = wlr_gles3_renderer_init();
state.cat_texture = wlr_render_surface_init(state.renderer); state.cat_texture = wlr_render_surface_init(state.renderer);

View file

@ -74,7 +74,17 @@ static void pointer_motion_notify(struct wl_listener *listener, void *data) {
struct wlr_pointer_motion *event = data; struct wlr_pointer_motion *event = data;
struct pointer_state *pstate = wl_container_of(listener, pstate, motion); struct pointer_state *pstate = wl_container_of(listener, pstate, motion);
if (pstate->compositor->pointer_motion_cb) { if (pstate->compositor->pointer_motion_cb) {
pstate->compositor->pointer_motion_cb(pstate, event->delta_x, event->delta_y); pstate->compositor->pointer_motion_cb(pstate,
event->delta_x, event->delta_y);
}
}
static void pointer_button_notify(struct wl_listener *listener, void *data) {
struct wlr_pointer_button *event = data;
struct pointer_state *pstate = wl_container_of(listener, pstate, button);
if (pstate->compositor->pointer_button_cb) {
pstate->compositor->pointer_button_cb(pstate,
event->button, event->state);
} }
} }
@ -87,7 +97,9 @@ static void pointer_add(struct wlr_input_device *device, struct compositor_state
wl_list_init(&pstate->button.link); wl_list_init(&pstate->button.link);
wl_list_init(&pstate->axis.link); wl_list_init(&pstate->axis.link);
pstate->motion.notify = pointer_motion_notify; pstate->motion.notify = pointer_motion_notify;
pstate->button.notify = pointer_button_notify;
wl_signal_add(&device->pointer->events.motion, &pstate->motion); wl_signal_add(&device->pointer->events.motion, &pstate->motion);
wl_signal_add(&device->pointer->events.button, &pstate->button);
wl_list_insert(&state->pointers, &pstate->link); wl_list_insert(&state->pointers, &pstate->link);
} }

View file

@ -47,7 +47,10 @@ struct compositor_state {
void (*keyboard_remove_cb)(struct keyboard_state *s); void (*keyboard_remove_cb)(struct keyboard_state *s);
void (*keyboard_key_cb)(struct keyboard_state *s, xkb_keysym_t sym, void (*keyboard_key_cb)(struct keyboard_state *s, xkb_keysym_t sym,
enum wlr_key_state key_state); enum wlr_key_state key_state);
void (*pointer_motion_cb)(struct pointer_state *s, double d_x, double d_y); void (*pointer_motion_cb)(struct pointer_state *s,
double d_x, double d_y);
void (*pointer_button_cb)(struct pointer_state *s,
uint32_t button, enum wlr_button_state state);
struct wl_display *display; struct wl_display *display;
struct wl_event_loop *event_loop; struct wl_event_loop *event_loop;