2 * Copyright © 2012 Openismus GmbH
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.
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.
27 #include <linux/input.h>
31 #include "text-client-protocol.h"
32 #include "desktop-shell-client-protocol.h"
34 struct virtual_keyboard {
35 struct input_panel *input_panel;
36 struct input_method *input_method;
37 struct display *display;
41 struct virtual_keyboard *keyboard;
42 struct window *window;
43 struct widget *widget;
49 redraw_handler(struct widget *widget, void *data)
51 struct keyboard *keyboard = data;
52 cairo_surface_t *surface;
53 struct rectangle allocation;
58 surface = window_get_surface(keyboard->window);
59 widget_get_allocation(keyboard->widget, &allocation);
61 cr = cairo_create(surface);
62 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
65 cairo_translate(cr, allocation.x, allocation.y);
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);
75 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
77 for (i = 0; i <= 'Z' - '0'; ++i) {
80 char text[] = { i + '0', '\0' };
81 cairo_text_extents_t extents;
84 cairo_text_extents(cr, text, &extents);
88 cairo_set_source_rgb(cr, 0, 0, 0);
89 cairo_rectangle(cr, x * cx, y * cy, cx, cy);
92 cairo_move_to(cr, x * cx + 0.5 * (cx - dx), y * cy + 0.5 * (cy - dy));
94 cairo_set_source_rgb(cr, 0, 0, 0);
95 cairo_show_text(cr, text);
99 cairo_surface_destroy(surface);
103 resize_handler(struct widget *widget,
104 int32_t width, int32_t height, void *data)
106 /* struct keyboard *keyboard = data; */
110 button_handler(struct widget *widget,
111 struct input *input, uint32_t time,
113 enum wl_pointer_button_state state, void *data)
115 struct keyboard *keyboard = data;
116 struct rectangle allocation;
118 char text[] = { '0', '\0' };
120 if (state != WL_POINTER_BUTTON_STATE_PRESSED || button != BTN_LEFT) {
124 input_get_position(input, &x, &y);
126 widget_get_allocation(keyboard->widget, &allocation);
130 text[0] = y / keyboard->cy * 10 + x / keyboard->cx + '0';
132 input_method_commit_string(keyboard->keyboard->input_method, text, -1);
134 widget_schedule_redraw(widget);
138 global_handler(struct wl_display *display, uint32_t id,
139 const char *interface, uint32_t version, void *data)
141 struct virtual_keyboard *keyboard = data;
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);
151 keyboard_create(struct output *output, struct virtual_keyboard *virtual_keyboard)
153 struct keyboard *keyboard;
155 keyboard = malloc(sizeof *keyboard);
156 memset(keyboard, 0, sizeof *keyboard);
158 keyboard->keyboard = virtual_keyboard;
159 keyboard->window = window_create(virtual_keyboard->display);
160 keyboard->widget = window_add_widget(keyboard->window, keyboard);
162 window_set_title(keyboard->window, "Virtual keyboard");
163 window_set_custom(keyboard->window);
164 window_set_user_data(keyboard->window, keyboard);
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);
173 window_schedule_resize(keyboard->window, keyboard->cx * 10, keyboard->cy * 5);
175 input_panel_set_surface(virtual_keyboard->input_panel,
176 window_get_wl_shell_surface(keyboard->window),
177 output_get_wl_output(output));
181 handle_output_configure(struct output *output, void *data)
183 struct virtual_keyboard *virtual_keyboard = data;
185 /* skip existing outputs */
186 if (output_get_user_data(output))
189 output_set_user_data(output, virtual_keyboard);
191 keyboard_create(output, virtual_keyboard);
195 main(int argc, char *argv[])
197 struct virtual_keyboard virtual_keyboard;
199 virtual_keyboard.display = display_create(argc, argv);
200 if (virtual_keyboard.display == NULL) {
201 fprintf(stderr, "failed to create display: %m\n");
205 wl_display_add_global_listener(display_get_display(virtual_keyboard.display),
206 global_handler, &virtual_keyboard);
208 display_set_user_data(virtual_keyboard.display, &virtual_keyboard);
209 display_set_output_configure_handler(virtual_keyboard.display, handle_output_configure);
211 display_run(virtual_keyboard.display);