blob: f7bddeb95e4eda7713bd66e271844f5f4bfd9215 [file] [log] [blame]
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001/*
2 * Copyright © 2012 Openismus GmbH
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include <linux/input.h>
28#include <cairo.h>
29
30#include "window.h"
31#include "text-client-protocol.h"
32#include "desktop-shell-client-protocol.h"
33
34struct virtual_keyboard {
35 struct input_panel *input_panel;
36 struct input_method *input_method;
37 struct display *display;
38};
39
40struct keyboard {
41 struct virtual_keyboard *keyboard;
42 struct window *window;
43 struct widget *widget;
44 int cx;
45 int cy;
46};
47
48static void
49redraw_handler(struct widget *widget, void *data)
50{
51 struct keyboard *keyboard = data;
52 cairo_surface_t *surface;
53 struct rectangle allocation;
54 cairo_t *cr;
55 int cx, cy;
56 int i;
57
58 surface = window_get_surface(keyboard->window);
59 widget_get_allocation(keyboard->widget, &allocation);
60
61 cr = cairo_create(surface);
62 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
63 cairo_clip(cr);
64
65 cairo_translate(cr, allocation.x, allocation.y);
66
67 cx = keyboard->cx;
68 cy = keyboard->cy;
69
70 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
71 cairo_set_source_rgba(cr, 1, 1, 1, 0.5);
72 cairo_rectangle(cr, 0, 0, 10 * cx, 5 * cy);
73 cairo_paint(cr);
74
75 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
76
77 for (i = 0; i <= 'Z' - '0'; ++i) {
78 const int x = i % 10;
79 const int y = i / 10;
80 char text[] = { i + '0', '\0' };
81 cairo_text_extents_t extents;
82 int dx, dy;
83
84 cairo_text_extents(cr, text, &extents);
85 dx = extents.width;
86 dy = extents.height;
87
88 cairo_set_source_rgb(cr, 0, 0, 0);
89 cairo_rectangle(cr, x * cx, y * cy, cx, cy);
90 cairo_stroke(cr);
91
92 cairo_move_to(cr, x * cx + 0.5 * (cx - dx), y * cy + 0.5 * (cy - dy));
93
94 cairo_set_source_rgb(cr, 0, 0, 0);
95 cairo_show_text(cr, text);
96 }
97
98 cairo_destroy(cr);
99 cairo_surface_destroy(surface);
100}
101
102static void
103resize_handler(struct widget *widget,
104 int32_t width, int32_t height, void *data)
105{
106 /* struct keyboard *keyboard = data; */
107}
108
109static void
110button_handler(struct widget *widget,
111 struct input *input, uint32_t time,
112 uint32_t button,
113 enum wl_pointer_button_state state, void *data)
114{
115 struct keyboard *keyboard = data;
116 struct rectangle allocation;
117 int32_t x, y;
118 char text[] = { '0', '\0' };
119
120 if (state != WL_POINTER_BUTTON_STATE_PRESSED || button != BTN_LEFT) {
121 return;
122 }
123
124 input_get_position(input, &x, &y);
125
126 widget_get_allocation(keyboard->widget, &allocation);
127 x -= allocation.x;
128 y -= allocation.y;
129
130 text[0] = y / keyboard->cy * 10 + x / keyboard->cx + '0';
131
132 input_method_commit_string(keyboard->keyboard->input_method, text, -1);
133
134 widget_schedule_redraw(widget);
135}
136
137static void
138global_handler(struct wl_display *display, uint32_t id,
139 const char *interface, uint32_t version, void *data)
140{
141 struct virtual_keyboard *keyboard = data;
142
143 if (!strcmp(interface, "input_panel")) {
144 keyboard->input_panel = wl_display_bind(display, id, &input_panel_interface);
145 } else if (!strcmp(interface, "input_method")) {
146 keyboard->input_method = wl_display_bind(display, id, &input_method_interface);
147 }
148}
149
150static void
151keyboard_create(struct output *output, struct virtual_keyboard *virtual_keyboard)
152{
153 struct keyboard *keyboard;
154
155 keyboard = malloc(sizeof *keyboard);
156 memset(keyboard, 0, sizeof *keyboard);
157
158 keyboard->keyboard = virtual_keyboard;
159 keyboard->window = window_create(virtual_keyboard->display);
160 keyboard->widget = window_add_widget(keyboard->window, keyboard);
161
162 window_set_title(keyboard->window, "Virtual keyboard");
163 window_set_custom(keyboard->window);
164 window_set_user_data(keyboard->window, keyboard);
165
166 keyboard->cx = 40;
167 keyboard->cy = 40;
168
169 widget_set_redraw_handler(keyboard->widget, redraw_handler);
170 widget_set_resize_handler(keyboard->widget, resize_handler);
171 widget_set_button_handler(keyboard->widget, button_handler);
172
173 window_schedule_resize(keyboard->window, keyboard->cx * 10, keyboard->cy * 5);
174
175 input_panel_set_surface(virtual_keyboard->input_panel,
176 window_get_wl_shell_surface(keyboard->window),
177 output_get_wl_output(output));
178}
179
180static void
181handle_output_configure(struct output *output, void *data)
182{
183 struct virtual_keyboard *virtual_keyboard = data;
184
185 /* skip existing outputs */
186 if (output_get_user_data(output))
187 return;
188
189 output_set_user_data(output, virtual_keyboard);
190
191 keyboard_create(output, virtual_keyboard);
192}
193
194int
195main(int argc, char *argv[])
196{
197 struct virtual_keyboard virtual_keyboard;
198
199 virtual_keyboard.display = display_create(argc, argv);
200 if (virtual_keyboard.display == NULL) {
201 fprintf(stderr, "failed to create display: %m\n");
202 return -1;
203 }
204
205 wl_display_add_global_listener(display_get_display(virtual_keyboard.display),
206 global_handler, &virtual_keyboard);
207
208 display_set_user_data(virtual_keyboard.display, &virtual_keyboard);
209 display_set_output_configure_handler(virtual_keyboard.display, handle_output_configure);
210
211 display_run(virtual_keyboard.display);
212
213 return 0;
214}