2012-06-21 23:52:19 +04:00
|
|
|
/*
|
|
|
|
* Copyright © 2012 Openismus GmbH
|
2012-09-10 01:08:32 +04:00
|
|
|
* Copyright © 2012 Intel Corporation
|
2012-06-21 23:52:19 +04:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, distribute, and sell this software and
|
|
|
|
* its documentation for any purpose is hereby granted without fee, provided
|
|
|
|
* that the above copyright notice appear in all copies and that both that
|
|
|
|
* copyright notice and this permission notice appear in supporting
|
|
|
|
* documentation, and that the name of the copyright holders not be used in
|
|
|
|
* advertising or publicity pertaining to distribution of the software
|
|
|
|
* without specific, written prior permission. The copyright holders make
|
|
|
|
* no representations about the suitability of this software for any
|
|
|
|
* purpose. It is provided "as is" without express or implied warranty.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
|
|
|
|
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
|
|
|
|
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
|
|
|
|
* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <linux/input.h>
|
|
|
|
#include <cairo.h>
|
|
|
|
|
|
|
|
#include "window.h"
|
2012-09-10 01:08:41 +04:00
|
|
|
#include "input-method-client-protocol.h"
|
2012-06-21 23:52:19 +04:00
|
|
|
#include "desktop-shell-client-protocol.h"
|
|
|
|
|
|
|
|
struct virtual_keyboard {
|
|
|
|
struct input_panel *input_panel;
|
|
|
|
struct input_method *input_method;
|
2012-09-10 01:08:32 +04:00
|
|
|
struct input_method_context *context;
|
2012-06-21 23:52:19 +04:00
|
|
|
struct display *display;
|
2012-09-10 01:08:43 +04:00
|
|
|
char *preedit_string;
|
2013-01-17 00:26:41 +04:00
|
|
|
uint32_t preedit_style;
|
2012-11-18 22:06:44 +04:00
|
|
|
struct {
|
|
|
|
xkb_mod_mask_t shift_mask;
|
|
|
|
} keysym;
|
2013-01-17 00:26:39 +04:00
|
|
|
uint32_t serial;
|
2012-06-21 23:52:19 +04:00
|
|
|
};
|
|
|
|
|
2012-09-10 01:08:42 +04:00
|
|
|
enum key_type {
|
|
|
|
keytype_default,
|
|
|
|
keytype_backspace,
|
|
|
|
keytype_enter,
|
|
|
|
keytype_space,
|
|
|
|
keytype_switch,
|
|
|
|
keytype_symbols,
|
2012-09-17 17:28:07 +04:00
|
|
|
keytype_tab,
|
|
|
|
keytype_arrow_up,
|
|
|
|
keytype_arrow_left,
|
|
|
|
keytype_arrow_right,
|
2013-01-17 00:26:41 +04:00
|
|
|
keytype_arrow_down,
|
|
|
|
keytype_style
|
2012-09-10 01:08:42 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct key {
|
|
|
|
enum key_type key_type;
|
|
|
|
|
|
|
|
char *label;
|
|
|
|
char *alt;
|
|
|
|
|
|
|
|
unsigned int width;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct key keys[] = {
|
|
|
|
{ keytype_default, "q", "Q", 1},
|
|
|
|
{ keytype_default, "w", "W", 1},
|
|
|
|
{ keytype_default, "e", "E", 1},
|
|
|
|
{ keytype_default, "r", "R", 1},
|
|
|
|
{ keytype_default, "t", "T", 1},
|
|
|
|
{ keytype_default, "y", "Y", 1},
|
|
|
|
{ keytype_default, "u", "U", 1},
|
|
|
|
{ keytype_default, "i", "I", 1},
|
|
|
|
{ keytype_default, "o", "O", 1},
|
|
|
|
{ keytype_default, "p", "P", 1},
|
|
|
|
{ keytype_backspace, "<--", "<--", 2},
|
|
|
|
|
|
|
|
{ keytype_tab, "->|", "->|", 1},
|
|
|
|
{ keytype_default, "a", "A", 1},
|
|
|
|
{ keytype_default, "s", "S", 1},
|
|
|
|
{ keytype_default, "d", "D", 1},
|
|
|
|
{ keytype_default, "f", "F", 1},
|
|
|
|
{ keytype_default, "g", "G", 1},
|
|
|
|
{ keytype_default, "h", "H", 1},
|
|
|
|
{ keytype_default, "j", "J", 1},
|
|
|
|
{ keytype_default, "k", "K", 1},
|
|
|
|
{ keytype_default, "l", "L", 1},
|
|
|
|
{ keytype_enter, "Enter", "Enter", 2},
|
|
|
|
|
|
|
|
{ keytype_switch, "ABC", "abc", 2},
|
|
|
|
{ keytype_default, "z", "Z", 1},
|
|
|
|
{ keytype_default, "x", "X", 1},
|
|
|
|
{ keytype_default, "c", "C", 1},
|
|
|
|
{ keytype_default, "v", "V", 1},
|
|
|
|
{ keytype_default, "b", "B", 1},
|
|
|
|
{ keytype_default, "n", "N", 1},
|
|
|
|
{ keytype_default, "m", "M", 1},
|
|
|
|
{ keytype_default, ",", ",", 1},
|
|
|
|
{ keytype_default, ".", ".", 1},
|
|
|
|
{ keytype_switch, "ABC", "abc", 1},
|
|
|
|
|
2012-09-17 17:28:07 +04:00
|
|
|
{ keytype_symbols, "?123", "?123", 1},
|
2013-01-17 00:26:41 +04:00
|
|
|
{ keytype_space, "", "", 5},
|
2012-09-17 17:28:07 +04:00
|
|
|
{ keytype_arrow_up, "/\\", "/\\", 1},
|
|
|
|
{ keytype_arrow_left, "<", "<", 1},
|
|
|
|
{ keytype_arrow_right, ">", ">", 1},
|
|
|
|
{ keytype_arrow_down, "\\/", "\\/", 1},
|
2013-01-17 00:26:41 +04:00
|
|
|
{ keytype_style, "", "", 2}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *style_labels[] = {
|
|
|
|
"none",
|
|
|
|
"default",
|
|
|
|
"active",
|
|
|
|
"inactive",
|
|
|
|
"highlight",
|
|
|
|
"underline",
|
|
|
|
"selection",
|
|
|
|
"incorrect"
|
2012-09-10 01:08:42 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned int columns = 12;
|
|
|
|
static const unsigned int rows = 4;
|
|
|
|
|
|
|
|
static const double key_width = 60;
|
|
|
|
static const double key_height = 50;
|
|
|
|
|
|
|
|
enum keyboard_state {
|
|
|
|
keyboardstate_default,
|
|
|
|
keyboardstate_uppercase
|
|
|
|
};
|
|
|
|
|
2012-06-21 23:52:19 +04:00
|
|
|
struct keyboard {
|
|
|
|
struct virtual_keyboard *keyboard;
|
|
|
|
struct window *window;
|
|
|
|
struct widget *widget;
|
2012-09-10 01:08:42 +04:00
|
|
|
|
|
|
|
enum keyboard_state state;
|
2012-06-21 23:52:19 +04:00
|
|
|
};
|
|
|
|
|
2013-01-17 00:26:41 +04:00
|
|
|
static const char *
|
|
|
|
label_from_key(struct keyboard *keyboard,
|
|
|
|
const struct key *key)
|
|
|
|
{
|
|
|
|
if (key->key_type == keytype_style)
|
|
|
|
return style_labels[keyboard->keyboard->preedit_style];
|
|
|
|
|
|
|
|
if (keyboard->state == keyboardstate_default)
|
|
|
|
return key->label;
|
|
|
|
else
|
|
|
|
return key->alt;
|
|
|
|
}
|
|
|
|
|
2012-09-10 01:08:42 +04:00
|
|
|
static void
|
2013-01-17 00:26:41 +04:00
|
|
|
draw_key(struct keyboard *keyboard,
|
|
|
|
const struct key *key,
|
2012-09-10 01:08:42 +04:00
|
|
|
cairo_t *cr,
|
|
|
|
unsigned int row,
|
|
|
|
unsigned int col)
|
|
|
|
{
|
|
|
|
const char *label;
|
|
|
|
cairo_text_extents_t extents;
|
|
|
|
|
|
|
|
cairo_save(cr);
|
|
|
|
cairo_rectangle(cr,
|
|
|
|
col * key_width, row * key_height,
|
|
|
|
key->width * key_width, key_height);
|
|
|
|
cairo_clip(cr);
|
|
|
|
|
|
|
|
/* Paint frame */
|
|
|
|
cairo_rectangle(cr,
|
|
|
|
col * key_width, row * key_height,
|
|
|
|
key->width * key_width, key_height);
|
|
|
|
cairo_set_line_width(cr, 3);
|
|
|
|
cairo_stroke(cr);
|
|
|
|
|
|
|
|
/* Paint text */
|
2013-01-17 00:26:41 +04:00
|
|
|
label = label_from_key(keyboard, key);
|
2012-09-10 01:08:42 +04:00
|
|
|
cairo_text_extents(cr, label, &extents);
|
|
|
|
|
|
|
|
cairo_translate(cr,
|
|
|
|
col * key_width,
|
|
|
|
row * key_height);
|
|
|
|
cairo_translate(cr,
|
|
|
|
(key->width * key_width - extents.width) / 2,
|
|
|
|
(key_height - extents.y_bearing) / 2);
|
|
|
|
cairo_show_text(cr, label);
|
|
|
|
|
|
|
|
cairo_restore(cr);
|
|
|
|
}
|
|
|
|
|
2012-06-21 23:52:19 +04:00
|
|
|
static void
|
|
|
|
redraw_handler(struct widget *widget, void *data)
|
|
|
|
{
|
|
|
|
struct keyboard *keyboard = data;
|
|
|
|
cairo_surface_t *surface;
|
|
|
|
struct rectangle allocation;
|
|
|
|
cairo_t *cr;
|
2012-09-10 01:08:42 +04:00
|
|
|
unsigned int i;
|
|
|
|
unsigned int row = 0, col = 0;
|
2012-06-21 23:52:19 +04:00
|
|
|
|
|
|
|
surface = window_get_surface(keyboard->window);
|
|
|
|
widget_get_allocation(keyboard->widget, &allocation);
|
|
|
|
|
|
|
|
cr = cairo_create(surface);
|
|
|
|
cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
|
|
|
|
cairo_clip(cr);
|
|
|
|
|
2012-09-10 01:08:42 +04:00
|
|
|
cairo_select_font_face(cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
|
|
|
|
cairo_set_font_size(cr, 16);
|
2012-06-21 23:52:19 +04:00
|
|
|
|
2012-09-10 01:08:42 +04:00
|
|
|
cairo_translate(cr, allocation.x, allocation.y);
|
2012-06-21 23:52:19 +04:00
|
|
|
|
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
|
2012-09-10 01:08:42 +04:00
|
|
|
cairo_set_source_rgba(cr, 1, 1, 1, 0.75);
|
|
|
|
cairo_rectangle(cr, 0, 0, columns * key_width, rows * key_height);
|
2012-06-21 23:52:19 +04:00
|
|
|
cairo_paint(cr);
|
|
|
|
|
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
|
|
|
|
|
2012-09-10 01:08:42 +04:00
|
|
|
for (i = 0; i < sizeof(keys) / sizeof(*keys); ++i) {
|
2012-06-21 23:52:19 +04:00
|
|
|
cairo_set_source_rgb(cr, 0, 0, 0);
|
2013-01-17 00:26:41 +04:00
|
|
|
draw_key(keyboard, &keys[i], cr, row, col);
|
2012-09-10 01:08:42 +04:00
|
|
|
col += keys[i].width;
|
|
|
|
if (col >= columns) {
|
|
|
|
row += 1;
|
|
|
|
col = 0;
|
|
|
|
}
|
2012-06-21 23:52:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
cairo_destroy(cr);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
resize_handler(struct widget *widget,
|
|
|
|
int32_t width, int32_t height, void *data)
|
|
|
|
{
|
|
|
|
/* struct keyboard *keyboard = data; */
|
|
|
|
}
|
|
|
|
|
2012-11-05 06:26:42 +04:00
|
|
|
static void
|
|
|
|
virtual_keyboard_commit_preedit(struct virtual_keyboard *keyboard)
|
|
|
|
{
|
|
|
|
if (!keyboard->preedit_string ||
|
|
|
|
strlen(keyboard->preedit_string) == 0)
|
|
|
|
return;
|
|
|
|
|
2013-01-17 00:26:38 +04:00
|
|
|
input_method_context_preedit_cursor(keyboard->context,
|
2013-01-17 00:26:39 +04:00
|
|
|
keyboard->serial,
|
2013-01-17 00:26:38 +04:00
|
|
|
0);
|
2012-11-05 06:26:42 +04:00
|
|
|
input_method_context_preedit_string(keyboard->context,
|
2013-01-17 00:26:39 +04:00
|
|
|
keyboard->serial,
|
2012-11-05 06:26:42 +04:00
|
|
|
"",
|
2013-01-17 00:26:38 +04:00
|
|
|
"");
|
2012-11-05 06:26:42 +04:00
|
|
|
input_method_context_commit_string(keyboard->context,
|
2013-01-17 00:26:39 +04:00
|
|
|
keyboard->serial,
|
2012-11-05 06:26:42 +04:00
|
|
|
keyboard->preedit_string,
|
|
|
|
strlen(keyboard->preedit_string));
|
|
|
|
free(keyboard->preedit_string);
|
|
|
|
keyboard->preedit_string = strdup("");
|
|
|
|
}
|
|
|
|
|
2013-01-17 00:26:41 +04:00
|
|
|
static void
|
|
|
|
virtual_keyboard_send_preedit(struct virtual_keyboard *keyboard)
|
|
|
|
{
|
|
|
|
if (keyboard->preedit_style)
|
|
|
|
input_method_context_preedit_styling(keyboard->context,
|
|
|
|
keyboard->serial,
|
|
|
|
0,
|
|
|
|
strlen(keyboard->preedit_string),
|
|
|
|
keyboard->preedit_style);
|
|
|
|
input_method_context_preedit_cursor(keyboard->context,
|
|
|
|
keyboard->serial,
|
|
|
|
strlen(keyboard->preedit_string));
|
|
|
|
input_method_context_preedit_string(keyboard->context,
|
|
|
|
keyboard->serial,
|
|
|
|
keyboard->preedit_string,
|
|
|
|
keyboard->preedit_string);
|
|
|
|
}
|
|
|
|
|
2012-09-10 01:08:42 +04:00
|
|
|
static void
|
2012-11-18 22:06:44 +04:00
|
|
|
keyboard_handle_key(struct keyboard *keyboard, uint32_t time, const struct key *key, struct input *input, enum wl_pointer_button_state state)
|
2012-09-10 01:08:42 +04:00
|
|
|
{
|
|
|
|
const char *label = keyboard->state == keyboardstate_default ? key->label : key->alt;
|
2012-11-18 22:06:44 +04:00
|
|
|
xkb_mod_mask_t mod_mask = keyboard->state == keyboardstate_default ? 0 : keyboard->keyboard->keysym.shift_mask;
|
|
|
|
uint32_t key_state = (state == WL_POINTER_BUTTON_STATE_PRESSED) ? WL_KEYBOARD_KEY_STATE_PRESSED : WL_KEYBOARD_KEY_STATE_RELEASED;
|
2012-09-10 01:08:42 +04:00
|
|
|
|
|
|
|
switch (key->key_type) {
|
|
|
|
case keytype_default:
|
2012-11-18 22:06:44 +04:00
|
|
|
if (state != WL_POINTER_BUTTON_STATE_PRESSED)
|
|
|
|
break;
|
|
|
|
|
2012-09-10 01:08:43 +04:00
|
|
|
keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
|
|
|
|
label);
|
2013-01-17 00:26:41 +04:00
|
|
|
virtual_keyboard_send_preedit(keyboard->keyboard);
|
2012-09-10 01:08:42 +04:00
|
|
|
break;
|
|
|
|
case keytype_backspace:
|
2012-11-18 22:06:44 +04:00
|
|
|
if (state != WL_POINTER_BUTTON_STATE_PRESSED)
|
|
|
|
break;
|
|
|
|
|
2012-09-10 01:08:44 +04:00
|
|
|
if (strlen(keyboard->keyboard->preedit_string) == 0) {
|
|
|
|
input_method_context_delete_surrounding_text(keyboard->keyboard->context,
|
2013-01-17 00:26:39 +04:00
|
|
|
keyboard->keyboard->serial,
|
2012-09-10 01:08:44 +04:00
|
|
|
-1, 1);
|
2012-09-17 17:28:08 +04:00
|
|
|
} else {
|
|
|
|
keyboard->keyboard->preedit_string[strlen(keyboard->keyboard->preedit_string) - 1] = '\0';
|
2013-01-17 00:26:41 +04:00
|
|
|
virtual_keyboard_send_preedit(keyboard->keyboard);
|
2012-09-10 01:08:44 +04:00
|
|
|
}
|
2012-09-10 01:08:42 +04:00
|
|
|
break;
|
|
|
|
case keytype_enter:
|
2012-11-05 06:26:42 +04:00
|
|
|
virtual_keyboard_commit_preedit(keyboard->keyboard);
|
2012-11-18 22:06:43 +04:00
|
|
|
input_method_context_keysym(keyboard->keyboard->context,
|
|
|
|
display_get_serial(keyboard->keyboard->display),
|
|
|
|
time,
|
2012-11-18 22:06:44 +04:00
|
|
|
XKB_KEY_Return, key_state, mod_mask);
|
2012-09-10 01:08:42 +04:00
|
|
|
break;
|
|
|
|
case keytype_space:
|
2012-11-18 22:06:44 +04:00
|
|
|
if (state != WL_POINTER_BUTTON_STATE_PRESSED)
|
|
|
|
break;
|
2012-09-10 01:08:43 +04:00
|
|
|
keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
|
|
|
|
" ");
|
2012-11-05 06:26:42 +04:00
|
|
|
virtual_keyboard_commit_preedit(keyboard->keyboard);
|
2012-09-10 01:08:42 +04:00
|
|
|
break;
|
|
|
|
case keytype_switch:
|
2012-11-18 22:06:44 +04:00
|
|
|
if (state != WL_POINTER_BUTTON_STATE_PRESSED)
|
|
|
|
break;
|
2012-09-10 01:08:42 +04:00
|
|
|
if (keyboard->state == keyboardstate_default)
|
|
|
|
keyboard->state = keyboardstate_uppercase;
|
|
|
|
else
|
|
|
|
keyboard->state = keyboardstate_default;
|
|
|
|
break;
|
|
|
|
case keytype_symbols:
|
2012-11-18 22:06:44 +04:00
|
|
|
if (state != WL_POINTER_BUTTON_STATE_PRESSED)
|
|
|
|
break;
|
2012-09-10 01:08:42 +04:00
|
|
|
break;
|
|
|
|
case keytype_tab:
|
2012-11-05 06:26:42 +04:00
|
|
|
virtual_keyboard_commit_preedit(keyboard->keyboard);
|
2012-11-18 22:06:43 +04:00
|
|
|
input_method_context_keysym(keyboard->keyboard->context,
|
|
|
|
display_get_serial(keyboard->keyboard->display),
|
|
|
|
time,
|
2012-11-18 22:06:44 +04:00
|
|
|
XKB_KEY_Tab, key_state, mod_mask);
|
2012-09-10 01:08:42 +04:00
|
|
|
break;
|
2012-09-17 17:28:07 +04:00
|
|
|
case keytype_arrow_up:
|
2012-11-05 06:26:42 +04:00
|
|
|
virtual_keyboard_commit_preedit(keyboard->keyboard);
|
2012-11-18 22:06:43 +04:00
|
|
|
input_method_context_keysym(keyboard->keyboard->context,
|
|
|
|
display_get_serial(keyboard->keyboard->display),
|
|
|
|
time,
|
2012-11-18 22:06:44 +04:00
|
|
|
XKB_KEY_Up, key_state, mod_mask);
|
2012-09-17 17:28:07 +04:00
|
|
|
break;
|
|
|
|
case keytype_arrow_left:
|
2012-11-05 06:26:42 +04:00
|
|
|
virtual_keyboard_commit_preedit(keyboard->keyboard);
|
2012-11-18 22:06:43 +04:00
|
|
|
input_method_context_keysym(keyboard->keyboard->context,
|
|
|
|
display_get_serial(keyboard->keyboard->display),
|
|
|
|
time,
|
2012-11-18 22:06:44 +04:00
|
|
|
XKB_KEY_Left, key_state, mod_mask);
|
2012-09-17 17:28:07 +04:00
|
|
|
break;
|
|
|
|
case keytype_arrow_right:
|
2012-11-05 06:26:42 +04:00
|
|
|
virtual_keyboard_commit_preedit(keyboard->keyboard);
|
2012-11-18 22:06:43 +04:00
|
|
|
input_method_context_keysym(keyboard->keyboard->context,
|
|
|
|
display_get_serial(keyboard->keyboard->display),
|
|
|
|
time,
|
2012-11-18 22:06:44 +04:00
|
|
|
XKB_KEY_Right, key_state, mod_mask);
|
2012-09-17 17:28:07 +04:00
|
|
|
break;
|
|
|
|
case keytype_arrow_down:
|
2012-11-05 06:26:42 +04:00
|
|
|
virtual_keyboard_commit_preedit(keyboard->keyboard);
|
2012-11-18 22:06:43 +04:00
|
|
|
input_method_context_keysym(keyboard->keyboard->context,
|
|
|
|
display_get_serial(keyboard->keyboard->display),
|
|
|
|
time,
|
2012-11-18 22:06:44 +04:00
|
|
|
XKB_KEY_Down, key_state, mod_mask);
|
2012-09-17 17:28:07 +04:00
|
|
|
break;
|
2013-01-17 00:26:41 +04:00
|
|
|
case keytype_style:
|
|
|
|
if (state != WL_POINTER_BUTTON_STATE_PRESSED)
|
|
|
|
break;
|
|
|
|
keyboard->keyboard->preedit_style = (keyboard->keyboard->preedit_style + 1) % 8; /* TODO */
|
|
|
|
virtual_keyboard_send_preedit(keyboard->keyboard);
|
|
|
|
break;
|
2012-09-10 01:08:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-21 23:52:19 +04:00
|
|
|
static void
|
|
|
|
button_handler(struct widget *widget,
|
|
|
|
struct input *input, uint32_t time,
|
|
|
|
uint32_t button,
|
|
|
|
enum wl_pointer_button_state state, void *data)
|
|
|
|
{
|
|
|
|
struct keyboard *keyboard = data;
|
|
|
|
struct rectangle allocation;
|
2012-09-10 01:08:42 +04:00
|
|
|
int32_t x, y;
|
|
|
|
int row, col;
|
|
|
|
unsigned int i;
|
2012-06-21 23:52:19 +04:00
|
|
|
|
2012-11-18 22:06:44 +04:00
|
|
|
if (button != BTN_LEFT) {
|
2012-06-21 23:52:19 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
input_get_position(input, &x, &y);
|
|
|
|
|
|
|
|
widget_get_allocation(keyboard->widget, &allocation);
|
|
|
|
x -= allocation.x;
|
|
|
|
y -= allocation.y;
|
|
|
|
|
2012-09-10 01:08:42 +04:00
|
|
|
row = y / key_height;
|
|
|
|
col = x / key_width + row * columns;
|
|
|
|
for (i = 0; i < sizeof(keys) / sizeof(*keys); ++i) {
|
|
|
|
col -= keys[i].width;
|
2012-10-09 21:44:34 +04:00
|
|
|
if (col < 0) {
|
2012-11-18 22:06:44 +04:00
|
|
|
keyboard_handle_key(keyboard, time, &keys[i], input, state);
|
2012-09-10 01:08:42 +04:00
|
|
|
break;
|
2012-10-09 21:44:34 +04:00
|
|
|
}
|
2012-09-10 01:08:42 +04:00
|
|
|
}
|
2012-06-21 23:52:19 +04:00
|
|
|
|
|
|
|
widget_schedule_redraw(widget);
|
|
|
|
}
|
|
|
|
|
2012-09-10 01:08:40 +04:00
|
|
|
static void
|
|
|
|
input_method_context_surrounding_text(void *data,
|
|
|
|
struct input_method_context *context,
|
|
|
|
const char *text,
|
|
|
|
uint32_t cursor,
|
|
|
|
uint32_t anchor)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Surrounding text updated: %s\n", text);
|
|
|
|
}
|
|
|
|
|
2012-09-10 01:08:46 +04:00
|
|
|
static void
|
|
|
|
input_method_context_reset(void *data,
|
2013-01-17 00:26:39 +04:00
|
|
|
struct input_method_context *context,
|
|
|
|
uint32_t serial)
|
2012-09-10 01:08:46 +04:00
|
|
|
{
|
|
|
|
struct virtual_keyboard *keyboard = data;
|
|
|
|
|
|
|
|
fprintf(stderr, "Reset pre-edit buffer\n");
|
|
|
|
|
|
|
|
if (strlen(keyboard->preedit_string)) {
|
2013-01-17 00:26:38 +04:00
|
|
|
input_method_context_preedit_cursor(context,
|
2013-01-17 00:26:39 +04:00
|
|
|
serial,
|
2013-01-17 00:26:38 +04:00
|
|
|
0);
|
2012-09-10 01:08:46 +04:00
|
|
|
input_method_context_preedit_string(context,
|
2013-01-17 00:26:39 +04:00
|
|
|
serial,
|
2012-09-10 01:08:46 +04:00
|
|
|
"",
|
2013-01-17 00:26:38 +04:00
|
|
|
"");
|
2012-09-10 01:08:46 +04:00
|
|
|
free(keyboard->preedit_string);
|
|
|
|
keyboard->preedit_string = strdup("");
|
|
|
|
}
|
2013-01-17 00:26:39 +04:00
|
|
|
|
|
|
|
keyboard->serial = serial;
|
2012-09-10 01:08:46 +04:00
|
|
|
}
|
|
|
|
|
2012-09-10 01:08:40 +04:00
|
|
|
static const struct input_method_context_listener input_method_context_listener = {
|
|
|
|
input_method_context_surrounding_text,
|
2012-09-10 01:08:46 +04:00
|
|
|
input_method_context_reset
|
2012-09-10 01:08:40 +04:00
|
|
|
};
|
|
|
|
|
2012-09-10 01:08:32 +04:00
|
|
|
static void
|
|
|
|
input_method_activate(void *data,
|
|
|
|
struct input_method *input_method,
|
2013-01-17 00:26:39 +04:00
|
|
|
struct input_method_context *context,
|
|
|
|
uint32_t serial)
|
2012-09-10 01:08:32 +04:00
|
|
|
{
|
|
|
|
struct virtual_keyboard *keyboard = data;
|
2012-11-18 22:06:44 +04:00
|
|
|
struct wl_array modifiers_map;
|
2012-09-10 01:08:32 +04:00
|
|
|
|
|
|
|
if (keyboard->context)
|
|
|
|
input_method_context_destroy(keyboard->context);
|
|
|
|
|
2012-09-10 01:08:43 +04:00
|
|
|
if (keyboard->preedit_string)
|
|
|
|
free(keyboard->preedit_string);
|
|
|
|
|
|
|
|
keyboard->preedit_string = strdup("");
|
2013-01-17 00:26:39 +04:00
|
|
|
keyboard->serial = serial;
|
2012-09-10 01:08:43 +04:00
|
|
|
|
2012-09-10 01:08:32 +04:00
|
|
|
keyboard->context = context;
|
2012-09-10 01:08:40 +04:00
|
|
|
input_method_context_add_listener(context,
|
|
|
|
&input_method_context_listener,
|
|
|
|
keyboard);
|
2012-11-18 22:06:44 +04:00
|
|
|
|
|
|
|
wl_array_init(&modifiers_map);
|
|
|
|
keysym_modifiers_add(&modifiers_map, "Shift");
|
|
|
|
keysym_modifiers_add(&modifiers_map, "Control");
|
|
|
|
keysym_modifiers_add(&modifiers_map, "Mod1");
|
|
|
|
input_method_context_modifiers_map(context, &modifiers_map);
|
|
|
|
keyboard->keysym.shift_mask = keysym_modifiers_get_mask(&modifiers_map, "Shift");
|
|
|
|
wl_array_release(&modifiers_map);
|
2012-09-10 01:08:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
input_method_deactivate(void *data,
|
|
|
|
struct input_method *input_method,
|
|
|
|
struct input_method_context *context)
|
|
|
|
{
|
|
|
|
struct virtual_keyboard *keyboard = data;
|
|
|
|
|
|
|
|
if (!keyboard->context)
|
|
|
|
return;
|
|
|
|
|
|
|
|
input_method_context_destroy(keyboard->context);
|
|
|
|
keyboard->context = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct input_method_listener input_method_listener = {
|
|
|
|
input_method_activate,
|
|
|
|
input_method_deactivate
|
|
|
|
};
|
|
|
|
|
2012-06-21 23:52:19 +04:00
|
|
|
static void
|
2012-10-11 05:34:26 +04:00
|
|
|
global_handler(struct display *display, uint32_t name,
|
2012-06-21 23:52:19 +04:00
|
|
|
const char *interface, uint32_t version, void *data)
|
|
|
|
{
|
|
|
|
struct virtual_keyboard *keyboard = data;
|
|
|
|
|
|
|
|
if (!strcmp(interface, "input_panel")) {
|
2012-10-11 05:34:26 +04:00
|
|
|
keyboard->input_panel =
|
|
|
|
display_bind(display, name, &input_panel_interface, 1);
|
2012-06-21 23:52:19 +04:00
|
|
|
} else if (!strcmp(interface, "input_method")) {
|
2012-10-11 05:34:26 +04:00
|
|
|
keyboard->input_method =
|
|
|
|
display_bind(display, name,
|
|
|
|
&input_method_interface, 1);
|
2012-09-10 01:08:32 +04:00
|
|
|
input_method_add_listener(keyboard->input_method, &input_method_listener, keyboard);
|
2012-06-21 23:52:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
keyboard_create(struct output *output, struct virtual_keyboard *virtual_keyboard)
|
|
|
|
{
|
|
|
|
struct keyboard *keyboard;
|
|
|
|
|
|
|
|
keyboard = malloc(sizeof *keyboard);
|
|
|
|
memset(keyboard, 0, sizeof *keyboard);
|
|
|
|
|
|
|
|
keyboard->keyboard = virtual_keyboard;
|
2012-06-27 18:22:15 +04:00
|
|
|
keyboard->window = window_create_custom(virtual_keyboard->display);
|
2012-06-21 23:52:19 +04:00
|
|
|
keyboard->widget = window_add_widget(keyboard->window, keyboard);
|
|
|
|
|
|
|
|
window_set_title(keyboard->window, "Virtual keyboard");
|
|
|
|
window_set_user_data(keyboard->window, keyboard);
|
|
|
|
|
|
|
|
widget_set_redraw_handler(keyboard->widget, redraw_handler);
|
|
|
|
widget_set_resize_handler(keyboard->widget, resize_handler);
|
|
|
|
widget_set_button_handler(keyboard->widget, button_handler);
|
|
|
|
|
2012-09-10 01:08:42 +04:00
|
|
|
window_schedule_resize(keyboard->window,
|
|
|
|
columns * key_width,
|
|
|
|
rows * key_height);
|
2012-06-21 23:52:19 +04:00
|
|
|
|
|
|
|
input_panel_set_surface(virtual_keyboard->input_panel,
|
2012-06-27 18:22:15 +04:00
|
|
|
window_get_wl_surface(keyboard->window),
|
2012-06-21 23:52:19 +04:00
|
|
|
output_get_wl_output(output));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_output_configure(struct output *output, void *data)
|
|
|
|
{
|
|
|
|
struct virtual_keyboard *virtual_keyboard = data;
|
|
|
|
|
|
|
|
/* skip existing outputs */
|
|
|
|
if (output_get_user_data(output))
|
|
|
|
return;
|
|
|
|
|
|
|
|
output_set_user_data(output, virtual_keyboard);
|
|
|
|
|
|
|
|
keyboard_create(output, virtual_keyboard);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct virtual_keyboard virtual_keyboard;
|
|
|
|
|
2013-01-17 00:26:41 +04:00
|
|
|
memset(&virtual_keyboard, 0, sizeof virtual_keyboard);
|
|
|
|
|
2012-06-21 23:52:19 +04:00
|
|
|
virtual_keyboard.display = display_create(argc, argv);
|
|
|
|
if (virtual_keyboard.display == NULL) {
|
|
|
|
fprintf(stderr, "failed to create display: %m\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
display_set_user_data(virtual_keyboard.display, &virtual_keyboard);
|
2012-10-11 05:34:26 +04:00
|
|
|
display_set_global_handler(virtual_keyboard.display, global_handler);
|
2012-06-21 23:52:19 +04:00
|
|
|
display_set_output_configure_handler(virtual_keyboard.display, handle_output_configure);
|
|
|
|
|
|
|
|
display_run(virtual_keyboard.display);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|