2017-06-15 19:31:13 +00:00
|
|
|
#define _POSIX_C_SOURCE 199309L
|
|
|
|
#define _XOPEN_SOURCE 500
|
2018-03-19 22:16:29 +00:00
|
|
|
#include <GLES2/gl2.h>
|
|
|
|
#include <math.h>
|
2017-06-15 19:31:13 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2018-03-19 22:16:29 +00:00
|
|
|
#include <time.h>
|
2017-06-15 19:31:13 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <wayland-server-protocol.h>
|
2018-03-19 22:16:29 +00:00
|
|
|
#include <wayland-server.h>
|
2017-06-15 19:31:13 +00:00
|
|
|
#include <wlr/backend.h>
|
2017-07-11 07:18:34 +00:00
|
|
|
#include <wlr/backend/session.h>
|
2018-03-19 22:16:29 +00:00
|
|
|
#include <wlr/render/wlr_renderer.h>
|
2018-03-08 01:57:55 +00:00
|
|
|
#include <wlr/types/wlr_box.h>
|
2018-03-19 22:16:29 +00:00
|
|
|
#include <wlr/types/wlr_matrix.h>
|
2017-06-21 14:27:45 +00:00
|
|
|
#include <wlr/types/wlr_output.h>
|
|
|
|
#include <wlr/types/wlr_tablet_pad.h>
|
2018-03-19 22:16:29 +00:00
|
|
|
#include <wlr/types/wlr_tablet_tool.h>
|
2017-08-15 05:56:47 +00:00
|
|
|
#include <wlr/util/log.h>
|
2018-03-19 22:16:29 +00:00
|
|
|
#include <xkbcommon/xkbcommon.h>
|
|
|
|
#include "support/shared.h"
|
2017-06-15 19:31:13 +00:00
|
|
|
|
|
|
|
struct sample_state {
|
|
|
|
struct wlr_renderer *renderer;
|
2017-06-19 18:49:07 +00:00
|
|
|
bool proximity, tap, button;
|
2017-06-15 20:15:12 +00:00
|
|
|
double distance;
|
|
|
|
double pressure;
|
2018-03-28 15:40:35 +00:00
|
|
|
double x, y;
|
2018-03-08 01:57:55 +00:00
|
|
|
double x_tilt, y_tilt;
|
2017-06-15 20:15:12 +00:00
|
|
|
double width_mm, height_mm;
|
2018-03-08 01:57:55 +00:00
|
|
|
double ring;
|
2017-06-15 20:15:12 +00:00
|
|
|
struct wl_list link;
|
|
|
|
float tool_color[4];
|
2017-06-19 18:49:07 +00:00
|
|
|
float pad_color[4];
|
2017-06-15 19:31:13 +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-15 20:15:12 +00:00
|
|
|
int32_t width, height;
|
|
|
|
wlr_output_effective_resolution(wlr_output, &width, &height);
|
|
|
|
|
2018-01-20 23:06:35 +00:00
|
|
|
wlr_output_make_current(wlr_output, NULL);
|
2018-03-20 22:10:42 +00:00
|
|
|
wlr_renderer_begin(sample->renderer, wlr_output->width, wlr_output->height);
|
2018-03-15 10:10:56 +00:00
|
|
|
wlr_renderer_clear(sample->renderer, (float[]){0.25f, 0.25f, 0.25f, 1});
|
2017-06-15 19:31:13 +00:00
|
|
|
|
2017-06-15 20:15:12 +00:00
|
|
|
float distance = 0.8f * (1 - sample->distance);
|
|
|
|
float tool_color[4] = { distance, distance, distance, 1 };
|
2017-06-19 18:49:07 +00:00
|
|
|
for (size_t i = 0; sample->button && i < 4; ++i) {
|
|
|
|
tool_color[i] = sample->tool_color[i];
|
2017-06-15 20:15:12 +00:00
|
|
|
}
|
|
|
|
float scale = 4;
|
|
|
|
|
|
|
|
float pad_width = sample->width_mm * scale;
|
|
|
|
float pad_height = sample->height_mm * scale;
|
|
|
|
float left = width / 2.0f - pad_width / 2.0f;
|
|
|
|
float top = height / 2.0f - pad_height / 2.0f;
|
2018-03-26 16:41:51 +00:00
|
|
|
const struct wlr_box box = {
|
2018-03-08 01:57:55 +00:00
|
|
|
.x = left, .y = top,
|
|
|
|
.width = pad_width, .height = pad_height,
|
|
|
|
};
|
2018-03-26 16:41:51 +00:00
|
|
|
wlr_render_rect(sample->renderer, &box, sample->pad_color,
|
|
|
|
wlr_output->transform_matrix);
|
2017-06-15 20:15:12 +00:00
|
|
|
|
|
|
|
if (sample->proximity) {
|
2018-03-08 01:57:55 +00:00
|
|
|
struct wlr_box box = {
|
2018-03-28 15:40:35 +00:00
|
|
|
.x = (sample->x * pad_width) - 8 * (sample->pressure + 1) + left,
|
|
|
|
.y = (sample->y * pad_height) - 8 * (sample->pressure + 1) + top,
|
2018-03-08 01:57:55 +00:00
|
|
|
.width = 16 * (sample->pressure + 1),
|
|
|
|
.height = 16 * (sample->pressure + 1),
|
|
|
|
};
|
2018-03-26 16:41:51 +00:00
|
|
|
float matrix[9];
|
|
|
|
wlr_matrix_project_box(matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL,
|
|
|
|
sample->ring, wlr_output->transform_matrix);
|
|
|
|
wlr_render_quad_with_matrix(sample->renderer, tool_color, matrix);
|
|
|
|
|
2018-03-08 01:57:55 +00:00
|
|
|
box.x += sample->x_tilt;
|
|
|
|
box.y += sample->y_tilt;
|
|
|
|
box.width /= 2;
|
|
|
|
box.height /= 2;
|
2018-03-26 16:41:51 +00:00
|
|
|
wlr_render_rect(sample->renderer, &box, tool_color,
|
2018-03-15 10:10:56 +00:00
|
|
|
wlr_output->transform_matrix);
|
2017-06-15 20:15:12 +00:00
|
|
|
}
|
2017-06-15 19:31:13 +00:00
|
|
|
|
|
|
|
wlr_renderer_end(sample->renderer);
|
2018-01-19 13:08:47 +00:00
|
|
|
wlr_output_swap_buffers(wlr_output, NULL, NULL);
|
2017-06-15 19:31:13 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 20:15:12 +00:00
|
|
|
static void handle_tool_axis(struct tablet_tool_state *tstate,
|
2017-06-21 18:07:09 +00:00
|
|
|
struct wlr_event_tablet_tool_axis *event) {
|
2017-06-15 20:15:12 +00:00
|
|
|
struct sample_state *sample = tstate->compositor->data;
|
|
|
|
if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_X)) {
|
2018-03-28 15:40:35 +00:00
|
|
|
sample->x = event->x;
|
2017-06-15 20:15:12 +00:00
|
|
|
}
|
|
|
|
if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_Y)) {
|
2018-03-28 15:40:35 +00:00
|
|
|
sample->y = event->y;
|
2017-06-15 20:15:12 +00:00
|
|
|
}
|
|
|
|
if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_DISTANCE)) {
|
|
|
|
sample->distance = event->distance;
|
|
|
|
}
|
|
|
|
if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_PRESSURE)) {
|
|
|
|
sample->pressure = event->pressure;
|
|
|
|
}
|
2018-03-08 01:57:55 +00:00
|
|
|
if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_TILT_X)) {
|
|
|
|
sample->x_tilt = event->tilt_x;
|
|
|
|
}
|
|
|
|
if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_TILT_Y)) {
|
|
|
|
sample->y_tilt = event->tilt_y;
|
|
|
|
}
|
2017-06-15 20:15:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_tool_proximity(struct tablet_tool_state *tstate,
|
|
|
|
enum wlr_tablet_tool_proximity_state state) {
|
|
|
|
struct sample_state *sample = tstate->compositor->data;
|
|
|
|
sample->proximity = state == WLR_TABLET_TOOL_PROXIMITY_IN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_tool_button(struct tablet_tool_state *tstate,
|
|
|
|
uint32_t button, enum wlr_button_state state) {
|
|
|
|
struct sample_state *sample = tstate->compositor->data;
|
|
|
|
if (state == WLR_BUTTON_RELEASED) {
|
2017-06-19 18:49:07 +00:00
|
|
|
sample->button = false;
|
2017-06-15 20:15:12 +00:00
|
|
|
} else {
|
2017-06-19 18:49:07 +00:00
|
|
|
sample->button = true;
|
2017-06-15 20:15:12 +00:00
|
|
|
for (size_t i = 0; i < 3; ++i) {
|
2017-06-19 18:49:07 +00:00
|
|
|
if (button % 3 == i) {
|
|
|
|
sample->tool_color[i] = 0;
|
|
|
|
} else {
|
|
|
|
sample->tool_color[i] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_pad_button(struct tablet_pad_state *pstate,
|
|
|
|
uint32_t button, enum wlr_button_state state) {
|
|
|
|
struct sample_state *sample = pstate->compositor->data;
|
2018-03-08 01:57:55 +00:00
|
|
|
float default_color[4] = { 0.5, 0.5, 0.5, 1.0 };
|
2017-06-19 18:49:07 +00:00
|
|
|
if (state == WLR_BUTTON_RELEASED) {
|
|
|
|
memcpy(sample->pad_color, default_color, sizeof(default_color));
|
|
|
|
} else {
|
|
|
|
for (size_t i = 0; i < 3; ++i) {
|
|
|
|
if (button % 3 == i) {
|
|
|
|
sample->pad_color[i] = 0;
|
|
|
|
} else {
|
|
|
|
sample->pad_color[i] = 1;
|
2017-06-15 20:15:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 01:57:55 +00:00
|
|
|
static void handle_pad_ring(struct tablet_pad_state *pstate,
|
|
|
|
uint32_t ring, double position) {
|
|
|
|
struct sample_state *sample = pstate->compositor->data;
|
|
|
|
if (position != -1) {
|
|
|
|
sample->ring = -(position * (M_PI / 180.0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-28 15:40:35 +00:00
|
|
|
static void handle_input_add(struct compositor_state *cstate,
|
|
|
|
struct wlr_input_device *inputdev) {
|
|
|
|
struct sample_state *sample = cstate->data;
|
|
|
|
if (inputdev->type == WLR_INPUT_DEVICE_TABLET_TOOL) {
|
|
|
|
sample->width_mm = inputdev->width_mm == 0 ?
|
|
|
|
20 : inputdev->width_mm;
|
|
|
|
sample->height_mm = inputdev->height_mm == 0 ?
|
|
|
|
10 : inputdev->height_mm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-15 19:31:13 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
2018-01-16 03:51:00 +00:00
|
|
|
wlr_log_init(L_DEBUG, NULL);
|
2017-06-15 20:15:12 +00:00
|
|
|
struct sample_state state = {
|
2017-06-19 18:49:07 +00:00
|
|
|
.tool_color = { 1, 1, 1, 1 },
|
2018-03-08 01:57:55 +00:00
|
|
|
.pad_color = { 0.5, 0.5, 0.5, 1.0 }
|
2017-06-15 20:15:12 +00:00
|
|
|
};
|
2018-03-28 15:40:35 +00:00
|
|
|
struct compositor_state compositor = {
|
2017-06-20 19:29:27 +00:00
|
|
|
.data = &state,
|
|
|
|
.output_frame_cb = handle_output_frame,
|
|
|
|
.tool_axis_cb = handle_tool_axis,
|
|
|
|
.tool_proximity_cb = handle_tool_proximity,
|
|
|
|
.tool_button_cb = handle_tool_button,
|
|
|
|
.pad_button_cb = handle_pad_button,
|
2018-03-08 01:57:55 +00:00
|
|
|
.pad_ring_cb = handle_pad_ring,
|
2018-03-28 15:40:35 +00:00
|
|
|
.input_add_cb = handle_input_add,
|
|
|
|
0
|
2017-06-20 19:29:27 +00:00
|
|
|
};
|
2017-06-15 19:31:13 +00:00
|
|
|
compositor_init(&compositor);
|
|
|
|
|
2018-04-01 20:57:29 +00:00
|
|
|
state.renderer = wlr_backend_get_renderer(compositor.backend);
|
2017-08-15 05:56:47 +00:00
|
|
|
if (!state.renderer) {
|
|
|
|
wlr_log(L_ERROR, "Could not start compositor, OOM");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2017-09-24 18:12:56 +00:00
|
|
|
if (!wlr_backend_start(compositor.backend)) {
|
|
|
|
wlr_log(L_ERROR, "Failed to start backend");
|
|
|
|
wlr_backend_destroy(compositor.backend);
|
|
|
|
exit(1);
|
|
|
|
}
|
2017-08-19 07:26:25 +00:00
|
|
|
wl_display_run(compositor.display);
|
2017-06-15 19:31:13 +00:00
|
|
|
|
|
|
|
wlr_renderer_destroy(state.renderer);
|
2017-08-19 07:26:25 +00:00
|
|
|
compositor_fini(&compositor);
|
2017-06-15 19:31:13 +00:00
|
|
|
}
|