aboutsummaryrefslogtreecommitdiff
path: root/examples/pointer.c
blob: c75188416cafc65745cdb94175f13de347d3ac9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#define _POSIX_C_SOURCE 199309L
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <wayland-server.h>
#include <wayland-server-protocol.h>
#include <xkbcommon/xkbcommon.h>
#include <GLES2/gl2.h>
#include <wlr/render/matrix.h>
#include <wlr/render/gles2.h>
#include <wlr/render.h>
#include <wlr/backend.h>
#include <wlr/session.h>
#include <wlr/types/wlr_keyboard.h>
#include <wlr/util/log.h>
#include "shared.h"
#include "cat.h"

struct sample_state {
	double cur_x, cur_y;
	float default_color[4];
	float clear_color[4];
};

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;

	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);

	wlr_output_swap_buffers(wlr_output);
}

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;

	struct output_state *output;
	wl_list_for_each(output, &pstate->compositor->outputs, link) {
		wlr_output_move_cursor(output->output, state->cur_x, state->cur_y);
	}
}

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;

	struct output_state *output;
	wl_list_for_each(output, &pstate->compositor->outputs, link) {
		wlr_output_move_cursor(output->output, state->cur_x, state->cur_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) {
		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));
}

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));
}

static void handle_output_add(struct output_state *ostate) {
	struct wlr_output *wlr_output = ostate->output;
	if (!wlr_output_set_cursor(wlr_output, cat_tex.pixel_data,
			cat_tex.width, cat_tex.width, cat_tex.height)) {
		wlr_log(L_DEBUG, "Failed to set hardware cursor");
		return;
	}
	if (!wlr_output_move_cursor(wlr_output, 0, 0)) {
		wlr_log(L_DEBUG, "Failed to move hardware cursor");
	}
}

int main(int argc, char *argv[]) {
	struct sample_state state = {
		.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;
	compositor_init(&compositor);

	compositor_run(&compositor);
}