Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Intel Corporation |
Pekka Paalanen | b13e84f | 2012-01-20 13:04:56 +0200 | [diff] [blame] | 3 | * Copyright © 2012 Collabora, Ltd. |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 4 | * |
| 5 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 6 | * documentation for any purpose is hereby granted without fee, provided that |
| 7 | * the above copyright notice appear in all copies and that both that copyright |
| 8 | * notice and this permission notice appear in supporting documentation, and |
| 9 | * that the name of the copyright holders not be used in advertising or |
| 10 | * publicity pertaining to distribution of the software without specific, |
| 11 | * written prior permission. The copyright holders make no representations |
| 12 | * about the suitability of this software for any purpose. It is provided "as |
| 13 | * is" without express or implied warranty. |
| 14 | * |
| 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 21 | * OF THIS SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include <stdint.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <cairo.h> |
| 29 | #include <math.h> |
| 30 | #include <assert.h> |
| 31 | |
| 32 | #include <linux/input.h> |
| 33 | #include <wayland-client.h> |
| 34 | |
| 35 | #include "window.h" |
| 36 | |
| 37 | #include <X11/keysym.h> |
| 38 | |
| 39 | struct clickdot { |
| 40 | struct display *display; |
| 41 | struct window *window; |
| 42 | struct widget *widget; |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 43 | |
Pekka Paalanen | b13e84f | 2012-01-20 13:04:56 +0200 | [diff] [blame] | 44 | int32_t x, y; |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | static void |
| 48 | redraw_handler(struct widget *widget, void *data) |
| 49 | { |
Pekka Paalanen | b13e84f | 2012-01-20 13:04:56 +0200 | [diff] [blame] | 50 | static const double r = 10.0; |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 51 | struct clickdot *clickdot = data; |
| 52 | cairo_surface_t *surface; |
| 53 | cairo_t *cr; |
| 54 | struct rectangle allocation; |
| 55 | |
| 56 | widget_get_allocation(clickdot->widget, &allocation); |
| 57 | |
| 58 | surface = window_get_surface(clickdot->window); |
| 59 | |
| 60 | cr = cairo_create(surface); |
| 61 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 62 | cairo_rectangle(cr, |
| 63 | allocation.x, |
| 64 | allocation.y, |
| 65 | allocation.width, |
| 66 | allocation.height); |
| 67 | cairo_set_source_rgba(cr, 0, 0, 0, 0.8); |
| 68 | cairo_fill(cr); |
Pekka Paalanen | b13e84f | 2012-01-20 13:04:56 +0200 | [diff] [blame] | 69 | |
| 70 | cairo_translate(cr, clickdot->x + 0.5, clickdot->y + 0.5); |
| 71 | cairo_set_line_width(cr, 1.0); |
| 72 | cairo_set_source_rgb(cr, 0.1, 0.9, 0.9); |
| 73 | cairo_move_to(cr, 0.0, -r); |
| 74 | cairo_line_to(cr, 0.0, r); |
| 75 | cairo_move_to(cr, -r, 0.0); |
| 76 | cairo_line_to(cr, r, 0.0); |
| 77 | cairo_arc(cr, 0.0, 0.0, r, 0.0, 2.0 * M_PI); |
| 78 | cairo_stroke(cr); |
| 79 | |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 80 | cairo_destroy(cr); |
| 81 | |
| 82 | cairo_surface_destroy(surface); |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static void |
| 86 | keyboard_focus_handler(struct window *window, |
| 87 | struct input *device, void *data) |
| 88 | { |
| 89 | struct clickdot *clickdot = data; |
| 90 | |
| 91 | window_schedule_redraw(clickdot->window); |
| 92 | } |
| 93 | |
| 94 | static void |
| 95 | key_handler(struct window *window, struct input *input, uint32_t time, |
| 96 | uint32_t key, uint32_t sym, uint32_t state, void *data) |
| 97 | { |
| 98 | struct clickdot *clickdot = data; |
| 99 | |
| 100 | if (state == 0) |
| 101 | return; |
| 102 | |
| 103 | switch (sym) { |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 104 | case XK_Escape: |
| 105 | display_exit(clickdot->display); |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | static void |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 111 | button_handler(struct widget *widget, |
| 112 | struct input *input, uint32_t time, |
| 113 | int button, int state, void *data) |
| 114 | { |
| 115 | struct clickdot *clickdot = data; |
| 116 | |
Pekka Paalanen | b13e84f | 2012-01-20 13:04:56 +0200 | [diff] [blame] | 117 | if (state && button == BTN_LEFT) |
| 118 | input_get_position(input, &clickdot->x, &clickdot->y); |
| 119 | |
| 120 | widget_schedule_redraw(widget); |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static struct clickdot * |
| 124 | clickdot_create(struct display *display) |
| 125 | { |
| 126 | struct clickdot *clickdot; |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 127 | |
| 128 | clickdot = malloc(sizeof *clickdot); |
| 129 | if (clickdot == NULL) |
| 130 | return clickdot; |
| 131 | memset(clickdot, 0, sizeof *clickdot); |
| 132 | |
Kristian Høgsberg | 009ac0a | 2012-01-31 15:24:48 -0500 | [diff] [blame^] | 133 | clickdot->window = window_create(display); |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 134 | clickdot->widget = frame_create(clickdot->window, clickdot); |
Pekka Paalanen | b13e84f | 2012-01-20 13:04:56 +0200 | [diff] [blame] | 135 | window_set_title(clickdot->window, "Wayland ClickDot"); |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 136 | clickdot->display = display; |
| 137 | |
| 138 | window_set_key_handler(clickdot->window, key_handler); |
| 139 | window_set_user_data(clickdot->window, clickdot); |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 140 | window_set_keyboard_focus_handler(clickdot->window, |
| 141 | keyboard_focus_handler); |
| 142 | |
Pekka Paalanen | b13e84f | 2012-01-20 13:04:56 +0200 | [diff] [blame] | 143 | widget_set_redraw_handler(clickdot->widget, redraw_handler); |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 144 | widget_set_button_handler(clickdot->widget, button_handler); |
| 145 | |
Pekka Paalanen | b13e84f | 2012-01-20 13:04:56 +0200 | [diff] [blame] | 146 | widget_schedule_resize(clickdot->widget, 500, 400); |
| 147 | clickdot->x = 250; |
| 148 | clickdot->y = 200; |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 149 | |
| 150 | return clickdot; |
| 151 | } |
| 152 | |
| 153 | static void |
| 154 | clickdot_destroy(struct clickdot *clickdot) |
| 155 | { |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 156 | widget_destroy(clickdot->widget); |
| 157 | window_destroy(clickdot->window); |
| 158 | free(clickdot); |
| 159 | } |
| 160 | |
| 161 | int |
| 162 | main(int argc, char *argv[]) |
| 163 | { |
| 164 | struct display *display; |
| 165 | struct clickdot *clickdot; |
| 166 | |
| 167 | display = display_create(&argc, &argv, NULL); |
| 168 | if (display == NULL) { |
| 169 | fprintf(stderr, "failed to create display: %m\n"); |
| 170 | return -1; |
| 171 | } |
| 172 | |
| 173 | clickdot = clickdot_create(display); |
| 174 | |
| 175 | display_run(display); |
| 176 | |
| 177 | clickdot_destroy(clickdot); |
| 178 | display_destroy(display); |
| 179 | |
| 180 | return 0; |
| 181 | } |