2012-01-20 13:09:13 +04:00
|
|
|
/*
|
|
|
|
* Copyright © 2010 Intel Corporation
|
2012-01-20 15:04:56 +04:00
|
|
|
* Copyright © 2012 Collabora, Ltd.
|
2012-05-11 01:26:25 +04:00
|
|
|
* Copyright © 2012 Jonas Ådahl
|
2012-01-20 13:09:13 +04:00
|
|
|
*
|
2015-06-11 08:48:59 +03:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
2012-01-20 13:09:13 +04:00
|
|
|
*
|
2015-06-11 08:48:59 +03:00
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
2012-01-20 13:09:13 +04:00
|
|
|
*/
|
|
|
|
|
2014-04-07 15:40:35 +04:00
|
|
|
#include "config.h"
|
|
|
|
|
2012-01-20 13:09:13 +04:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <cairo.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <assert.h>
|
2014-09-08 21:33:41 +04:00
|
|
|
#include <sys/timerfd.h>
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
#include <unistd.h>
|
2012-01-20 13:09:13 +04:00
|
|
|
|
|
|
|
#include <linux/input.h>
|
|
|
|
#include <wayland-client.h>
|
|
|
|
|
|
|
|
#include "window.h"
|
2015-06-16 01:37:10 +03:00
|
|
|
#include "shared/helpers.h"
|
2012-01-20 13:09:13 +04:00
|
|
|
|
|
|
|
struct clickdot {
|
|
|
|
struct display *display;
|
|
|
|
struct window *window;
|
|
|
|
struct widget *widget;
|
|
|
|
|
2012-05-11 01:26:25 +04:00
|
|
|
cairo_surface_t *buffer;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
int32_t x, y;
|
|
|
|
} dot;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
int32_t x, y;
|
|
|
|
int32_t old_x, old_y;
|
|
|
|
} line;
|
|
|
|
|
|
|
|
int reset;
|
2014-09-08 21:33:41 +04:00
|
|
|
|
|
|
|
struct input *cursor_timeout_input;
|
|
|
|
int cursor_timeout_fd;
|
|
|
|
struct task cursor_timeout_task;
|
2012-01-20 13:09:13 +04:00
|
|
|
};
|
|
|
|
|
2012-05-11 01:26:25 +04:00
|
|
|
static void
|
|
|
|
draw_line(struct clickdot *clickdot, cairo_t *cr,
|
|
|
|
struct rectangle *allocation)
|
|
|
|
{
|
|
|
|
cairo_t *bcr;
|
|
|
|
cairo_surface_t *tmp_buffer = NULL;
|
|
|
|
|
|
|
|
if (clickdot->reset) {
|
|
|
|
tmp_buffer = clickdot->buffer;
|
|
|
|
clickdot->buffer = NULL;
|
|
|
|
clickdot->line.x = -1;
|
|
|
|
clickdot->line.y = -1;
|
|
|
|
clickdot->line.old_x = -1;
|
|
|
|
clickdot->line.old_y = -1;
|
|
|
|
clickdot->reset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clickdot->buffer == NULL) {
|
|
|
|
clickdot->buffer =
|
|
|
|
cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
|
|
|
|
allocation->width,
|
|
|
|
allocation->height);
|
|
|
|
bcr = cairo_create(clickdot->buffer);
|
|
|
|
cairo_set_source_rgba(bcr, 0, 0, 0, 0);
|
|
|
|
cairo_rectangle(bcr,
|
|
|
|
0, 0,
|
|
|
|
allocation->width, allocation->height);
|
|
|
|
cairo_fill(bcr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
bcr = cairo_create(clickdot->buffer);
|
|
|
|
|
|
|
|
if (tmp_buffer) {
|
|
|
|
cairo_set_source_surface(bcr, tmp_buffer, 0, 0);
|
|
|
|
cairo_rectangle(bcr, 0, 0,
|
|
|
|
allocation->width, allocation->height);
|
|
|
|
cairo_clip(bcr);
|
|
|
|
cairo_paint(bcr);
|
|
|
|
|
|
|
|
cairo_surface_destroy(tmp_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clickdot->line.x != -1 && clickdot->line.y != -1) {
|
|
|
|
if (clickdot->line.old_x != -1 &&
|
|
|
|
clickdot->line.old_y != -1) {
|
|
|
|
cairo_set_line_width(bcr, 2.0);
|
|
|
|
cairo_set_source_rgb(bcr, 1, 1, 1);
|
|
|
|
cairo_translate(bcr,
|
|
|
|
-allocation->x, -allocation->y);
|
|
|
|
|
|
|
|
cairo_move_to(bcr,
|
|
|
|
clickdot->line.old_x,
|
|
|
|
clickdot->line.old_y);
|
|
|
|
cairo_line_to(bcr,
|
|
|
|
clickdot->line.x,
|
|
|
|
clickdot->line.y);
|
|
|
|
|
|
|
|
cairo_stroke(bcr);
|
|
|
|
}
|
|
|
|
|
|
|
|
clickdot->line.old_x = clickdot->line.x;
|
|
|
|
clickdot->line.old_y = clickdot->line.y;
|
|
|
|
}
|
|
|
|
cairo_destroy(bcr);
|
|
|
|
|
|
|
|
cairo_set_source_surface(cr, clickdot->buffer,
|
|
|
|
allocation->x, allocation->y);
|
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_ADD);
|
|
|
|
cairo_rectangle(cr,
|
|
|
|
allocation->x, allocation->y,
|
|
|
|
allocation->width, allocation->height);
|
|
|
|
cairo_clip(cr);
|
|
|
|
cairo_paint(cr);
|
|
|
|
}
|
|
|
|
|
2012-01-20 13:09:13 +04:00
|
|
|
static void
|
|
|
|
redraw_handler(struct widget *widget, void *data)
|
|
|
|
{
|
2012-01-20 15:04:56 +04:00
|
|
|
static const double r = 10.0;
|
2012-01-20 13:09:13 +04:00
|
|
|
struct clickdot *clickdot = data;
|
|
|
|
cairo_surface_t *surface;
|
|
|
|
cairo_t *cr;
|
|
|
|
struct rectangle allocation;
|
|
|
|
|
|
|
|
widget_get_allocation(clickdot->widget, &allocation);
|
|
|
|
|
|
|
|
surface = window_get_surface(clickdot->window);
|
|
|
|
|
|
|
|
cr = cairo_create(surface);
|
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
|
|
|
|
cairo_rectangle(cr,
|
|
|
|
allocation.x,
|
|
|
|
allocation.y,
|
|
|
|
allocation.width,
|
|
|
|
allocation.height);
|
|
|
|
cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
|
|
|
|
cairo_fill(cr);
|
|
|
|
|
2012-05-11 01:26:25 +04:00
|
|
|
draw_line(clickdot, cr, &allocation);
|
|
|
|
|
|
|
|
cairo_translate(cr, clickdot->dot.x + 0.5, clickdot->dot.y + 0.5);
|
2012-01-20 15:04:56 +04:00
|
|
|
cairo_set_line_width(cr, 1.0);
|
|
|
|
cairo_set_source_rgb(cr, 0.1, 0.9, 0.9);
|
|
|
|
cairo_move_to(cr, 0.0, -r);
|
|
|
|
cairo_line_to(cr, 0.0, r);
|
|
|
|
cairo_move_to(cr, -r, 0.0);
|
|
|
|
cairo_line_to(cr, r, 0.0);
|
|
|
|
cairo_arc(cr, 0.0, 0.0, r, 0.0, 2.0 * M_PI);
|
|
|
|
cairo_stroke(cr);
|
2012-01-20 13:09:13 +04:00
|
|
|
|
2012-01-20 15:04:56 +04:00
|
|
|
cairo_destroy(cr);
|
2012-01-20 13:09:13 +04:00
|
|
|
|
2012-01-20 15:04:56 +04:00
|
|
|
cairo_surface_destroy(surface);
|
2012-01-20 13:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
keyboard_focus_handler(struct window *window,
|
|
|
|
struct input *device, void *data)
|
|
|
|
{
|
|
|
|
struct clickdot *clickdot = data;
|
|
|
|
|
|
|
|
window_schedule_redraw(clickdot->window);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
key_handler(struct window *window, struct input *input, uint32_t time,
|
2012-05-30 19:31:52 +04:00
|
|
|
uint32_t key, uint32_t sym,
|
|
|
|
enum wl_keyboard_key_state state, void *data)
|
2012-01-20 13:09:13 +04:00
|
|
|
{
|
|
|
|
struct clickdot *clickdot = data;
|
|
|
|
|
2012-05-30 19:31:52 +04:00
|
|
|
if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
|
2012-01-20 13:09:13 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
switch (sym) {
|
2012-05-11 19:24:29 +04:00
|
|
|
case XKB_KEY_Escape:
|
2012-01-20 13:09:13 +04:00
|
|
|
display_exit(clickdot->display);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
button_handler(struct widget *widget,
|
|
|
|
struct input *input, uint32_t time,
|
2012-05-30 19:31:51 +04:00
|
|
|
uint32_t button,
|
|
|
|
enum wl_pointer_button_state state, void *data)
|
2012-01-20 13:09:13 +04:00
|
|
|
{
|
|
|
|
struct clickdot *clickdot = data;
|
|
|
|
|
2012-05-30 19:31:51 +04:00
|
|
|
if (state == WL_POINTER_BUTTON_STATE_PRESSED && button == BTN_LEFT)
|
2012-05-11 01:26:25 +04:00
|
|
|
input_get_position(input, &clickdot->dot.x, &clickdot->dot.y);
|
2012-01-20 15:04:56 +04:00
|
|
|
|
|
|
|
widget_schedule_redraw(widget);
|
2012-01-20 13:09:13 +04:00
|
|
|
}
|
|
|
|
|
2014-09-08 21:33:41 +04:00
|
|
|
static void
|
|
|
|
cursor_timeout_reset(struct clickdot *clickdot)
|
|
|
|
{
|
|
|
|
const long cursor_timeout = 500;
|
|
|
|
struct itimerspec its;
|
|
|
|
|
|
|
|
its.it_interval.tv_sec = 0;
|
|
|
|
its.it_interval.tv_nsec = 0;
|
|
|
|
its.it_value.tv_sec = cursor_timeout / 1000;
|
|
|
|
its.it_value.tv_nsec = (cursor_timeout % 1000) * 1000 * 1000;
|
|
|
|
timerfd_settime(clickdot->cursor_timeout_fd, 0, &its, NULL);
|
|
|
|
}
|
|
|
|
|
2012-05-11 01:26:25 +04:00
|
|
|
static int
|
|
|
|
motion_handler(struct widget *widget,
|
|
|
|
struct input *input, uint32_t time,
|
|
|
|
float x, float y, void *data)
|
|
|
|
{
|
|
|
|
struct clickdot *clickdot = data;
|
|
|
|
clickdot->line.x = x;
|
|
|
|
clickdot->line.y = y;
|
|
|
|
|
|
|
|
window_schedule_redraw(clickdot->window);
|
|
|
|
|
2014-09-08 21:33:41 +04:00
|
|
|
cursor_timeout_reset(clickdot);
|
|
|
|
clickdot->cursor_timeout_input = input;
|
|
|
|
|
|
|
|
return CURSOR_BLANK;
|
2012-05-11 01:26:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
resize_handler(struct widget *widget,
|
|
|
|
int32_t width, int32_t height,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
struct clickdot *clickdot = data;
|
|
|
|
|
|
|
|
clickdot->reset = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
leave_handler(struct widget *widget,
|
|
|
|
struct input *input, void *data)
|
|
|
|
{
|
|
|
|
struct clickdot *clickdot = data;
|
|
|
|
|
|
|
|
clickdot->reset = 1;
|
|
|
|
}
|
|
|
|
|
2014-09-08 21:33:41 +04:00
|
|
|
static void
|
|
|
|
cursor_timeout_func(struct task *task, uint32_t events)
|
|
|
|
{
|
|
|
|
struct clickdot *clickdot =
|
|
|
|
container_of(task, struct clickdot, cursor_timeout_task);
|
|
|
|
uint64_t exp;
|
|
|
|
|
|
|
|
if (read(clickdot->cursor_timeout_fd, &exp, sizeof (uint64_t)) !=
|
|
|
|
sizeof(uint64_t))
|
|
|
|
abort();
|
|
|
|
|
|
|
|
input_set_pointer_image(clickdot->cursor_timeout_input,
|
|
|
|
CURSOR_LEFT_PTR);
|
|
|
|
}
|
|
|
|
|
2012-01-20 13:09:13 +04:00
|
|
|
static struct clickdot *
|
|
|
|
clickdot_create(struct display *display)
|
|
|
|
{
|
|
|
|
struct clickdot *clickdot;
|
|
|
|
|
2013-08-08 05:57:05 +04:00
|
|
|
clickdot = xzalloc(sizeof *clickdot);
|
2012-02-01 00:24:48 +04:00
|
|
|
clickdot->window = window_create(display);
|
2013-10-14 04:08:38 +04:00
|
|
|
clickdot->widget = window_frame_create(clickdot->window, clickdot);
|
2012-01-20 15:04:56 +04:00
|
|
|
window_set_title(clickdot->window, "Wayland ClickDot");
|
2012-01-20 13:09:13 +04:00
|
|
|
clickdot->display = display;
|
2012-05-11 01:26:25 +04:00
|
|
|
clickdot->buffer = NULL;
|
2012-01-20 13:09:13 +04:00
|
|
|
|
|
|
|
window_set_key_handler(clickdot->window, key_handler);
|
|
|
|
window_set_user_data(clickdot->window, clickdot);
|
|
|
|
window_set_keyboard_focus_handler(clickdot->window,
|
|
|
|
keyboard_focus_handler);
|
|
|
|
|
2012-01-20 15:04:56 +04:00
|
|
|
widget_set_redraw_handler(clickdot->widget, redraw_handler);
|
2012-01-20 13:09:13 +04:00
|
|
|
widget_set_button_handler(clickdot->widget, button_handler);
|
2012-05-11 01:26:25 +04:00
|
|
|
widget_set_motion_handler(clickdot->widget, motion_handler);
|
|
|
|
widget_set_resize_handler(clickdot->widget, resize_handler);
|
|
|
|
widget_set_leave_handler(clickdot->widget, leave_handler);
|
2012-01-20 13:09:13 +04:00
|
|
|
|
2012-01-20 15:04:56 +04:00
|
|
|
widget_schedule_resize(clickdot->widget, 500, 400);
|
2012-05-11 01:26:25 +04:00
|
|
|
clickdot->dot.x = 250;
|
|
|
|
clickdot->dot.y = 200;
|
|
|
|
clickdot->line.x = -1;
|
|
|
|
clickdot->line.y = -1;
|
|
|
|
clickdot->line.old_x = -1;
|
|
|
|
clickdot->line.old_y = -1;
|
|
|
|
clickdot->reset = 0;
|
2012-01-20 13:09:13 +04:00
|
|
|
|
2014-09-08 21:33:41 +04:00
|
|
|
clickdot->cursor_timeout_fd =
|
|
|
|
timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
|
|
|
|
clickdot->cursor_timeout_task.run = cursor_timeout_func;
|
|
|
|
display_watch_fd(window_get_display(clickdot->window),
|
|
|
|
clickdot->cursor_timeout_fd,
|
|
|
|
EPOLLIN, &clickdot->cursor_timeout_task);
|
|
|
|
|
2012-01-20 13:09:13 +04:00
|
|
|
return clickdot;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clickdot_destroy(struct clickdot *clickdot)
|
|
|
|
{
|
2014-09-08 21:33:41 +04:00
|
|
|
display_unwatch_fd(window_get_display(clickdot->window),
|
|
|
|
clickdot->cursor_timeout_fd);
|
|
|
|
close(clickdot->cursor_timeout_fd);
|
2012-05-11 01:26:25 +04:00
|
|
|
if (clickdot->buffer)
|
|
|
|
cairo_surface_destroy(clickdot->buffer);
|
2012-01-20 13:09:13 +04:00
|
|
|
widget_destroy(clickdot->widget);
|
|
|
|
window_destroy(clickdot->window);
|
|
|
|
free(clickdot);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct display *display;
|
|
|
|
struct clickdot *clickdot;
|
|
|
|
|
2013-02-21 00:27:49 +04:00
|
|
|
display = display_create(&argc, argv);
|
2012-01-20 13:09:13 +04:00
|
|
|
if (display == NULL) {
|
|
|
|
fprintf(stderr, "failed to create display: %m\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
clickdot = clickdot_create(display);
|
|
|
|
|
|
|
|
display_run(display);
|
|
|
|
|
|
|
|
clickdot_destroy(clickdot);
|
|
|
|
display_destroy(display);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|