Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 1 | /* |
| 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 <stdlib.h> |
| 24 | |
| 25 | #include "compositor.h" |
| 26 | #include "text-server-protocol.h" |
| 27 | |
| 28 | struct input_method; |
| 29 | |
| 30 | struct text_model { |
| 31 | struct wl_resource resource; |
| 32 | |
| 33 | struct wl_list link; |
| 34 | |
| 35 | struct input_method *input_method; |
| 36 | }; |
| 37 | |
| 38 | struct input_method { |
| 39 | struct wl_object base; |
| 40 | struct weston_compositor *ec; |
| 41 | struct wl_global *global; |
| 42 | struct wl_listener destroy_listener; |
| 43 | struct wl_list models; |
| 44 | |
| 45 | struct text_model *active_model; |
| 46 | }; |
| 47 | |
| 48 | static void |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 49 | deactivate_text_model(struct text_model *text_model) |
| 50 | { |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame^] | 51 | struct weston_compositor *ec = text_model->input_method->ec; |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 52 | |
| 53 | if (text_model->input_method->active_model == text_model) { |
| 54 | text_model->input_method->active_model = NULL; |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame^] | 55 | wl_signal_emit(&ec->hide_input_panel_signal, ec); |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | static void |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 60 | destroy_text_model(struct wl_resource *resource) |
| 61 | { |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame^] | 62 | struct text_model *text_model = |
| 63 | container_of(resource, struct text_model, resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 64 | |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 65 | deactivate_text_model(text_model); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 66 | |
| 67 | wl_list_remove(&text_model->link); |
| 68 | free(text_model); |
| 69 | } |
| 70 | |
| 71 | static void |
| 72 | text_model_set_surrounding_text(struct wl_client *client, |
| 73 | struct wl_resource *resource, |
| 74 | const char *text) |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | static void |
| 79 | text_model_set_cursor_index(struct wl_client *client, |
| 80 | struct wl_resource *resource, |
| 81 | uint32_t index) |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | static void |
| 86 | text_model_activate(struct wl_client *client, |
| 87 | struct wl_resource *resource) |
| 88 | { |
| 89 | struct text_model *text_model = resource->data; |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame^] | 90 | struct weston_compositor *ec = text_model->input_method->ec; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 91 | |
| 92 | text_model->input_method->active_model = text_model; |
| 93 | |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame^] | 94 | wl_signal_emit(&ec->show_input_panel_signal, ec); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static void |
| 98 | text_model_deactivate(struct wl_client *client, |
| 99 | struct wl_resource *resource) |
| 100 | { |
| 101 | struct text_model *text_model = resource->data; |
| 102 | |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 103 | deactivate_text_model(text_model); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static void |
| 107 | text_model_set_selected_text(struct wl_client *client, |
| 108 | struct wl_resource *resource, |
| 109 | const char *text, |
| 110 | int32_t index) |
| 111 | { |
| 112 | } |
| 113 | |
| 114 | static void |
| 115 | text_model_set_micro_focus(struct wl_client *client, |
| 116 | struct wl_resource *resource, |
| 117 | int32_t x, |
| 118 | int32_t y, |
| 119 | int32_t width, |
| 120 | int32_t height) |
| 121 | { |
| 122 | } |
| 123 | |
| 124 | static void |
| 125 | text_model_set_preedit(struct wl_client *client, |
| 126 | struct wl_resource *resource) |
| 127 | { |
| 128 | } |
| 129 | |
| 130 | static void |
| 131 | text_model_set_content_type(struct wl_client *client, |
| 132 | struct wl_resource *resource) |
| 133 | { |
| 134 | } |
| 135 | |
| 136 | struct text_model_interface text_model_implementation = { |
| 137 | text_model_set_surrounding_text, |
| 138 | text_model_set_cursor_index, |
| 139 | text_model_activate, |
| 140 | text_model_deactivate, |
| 141 | text_model_set_selected_text, |
| 142 | text_model_set_micro_focus, |
| 143 | text_model_set_preedit, |
| 144 | text_model_set_content_type |
| 145 | }; |
| 146 | |
| 147 | static void input_method_create_text_model(struct wl_client *client, |
| 148 | struct wl_resource *resource, |
| 149 | uint32_t id, |
| 150 | struct wl_resource *surface) |
| 151 | { |
| 152 | struct input_method *input_method = resource->data; |
| 153 | struct text_model *text_model; |
| 154 | |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 155 | text_model = calloc(1, sizeof *text_model); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 156 | |
| 157 | text_model->resource.destroy = destroy_text_model; |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 158 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 159 | text_model->resource.object.id = id; |
| 160 | text_model->resource.object.interface = &text_model_interface; |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 161 | text_model->resource.object.implementation = |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 162 | (void (**)(void)) &text_model_implementation; |
| 163 | text_model->resource.data = text_model; |
| 164 | |
| 165 | text_model->input_method = input_method; |
| 166 | |
| 167 | wl_client_add_resource(client, &text_model->resource); |
| 168 | |
| 169 | wl_list_insert(&input_method->models, &text_model->link); |
| 170 | }; |
| 171 | |
| 172 | static void |
| 173 | input_method_commit_string(struct wl_client *client, |
| 174 | struct wl_resource *resource, |
| 175 | const char *text, |
| 176 | uint32_t index) |
| 177 | { |
| 178 | struct input_method *input_method = resource->data; |
| 179 | |
| 180 | if (input_method->active_model) { |
| 181 | text_model_send_commit_string(&input_method->active_model->resource, text, index); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | struct input_method_interface input_method_implementation = { |
| 186 | input_method_create_text_model, |
| 187 | input_method_commit_string |
| 188 | }; |
| 189 | |
| 190 | static void |
| 191 | bind_input_method(struct wl_client *client, |
| 192 | void *data, |
| 193 | uint32_t version, |
| 194 | uint32_t id) |
| 195 | { |
| 196 | wl_client_add_object(client, &input_method_interface, |
| 197 | &input_method_implementation, id, data); |
| 198 | } |
| 199 | |
| 200 | static void |
| 201 | input_method_notifier_destroy(struct wl_listener *listener, void *data) |
| 202 | { |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame^] | 203 | struct input_method *input_method = |
| 204 | container_of(listener, struct input_method, destroy_listener); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 205 | |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame^] | 206 | wl_display_remove_global(input_method->ec->wl_display, |
| 207 | input_method->global); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 208 | free(input_method); |
| 209 | } |
| 210 | |
| 211 | void |
| 212 | input_method_create(struct weston_compositor *ec) |
| 213 | { |
| 214 | struct input_method *input_method; |
| 215 | |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 216 | input_method = calloc(1, sizeof *input_method); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 217 | |
| 218 | input_method->base.interface = &input_method_interface; |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame^] | 219 | input_method->base.implementation = |
| 220 | (void(**)(void)) &input_method_implementation; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 221 | input_method->ec = ec; |
| 222 | input_method->active_model = NULL; |
| 223 | |
| 224 | wl_list_init(&input_method->models); |
| 225 | |
| 226 | input_method->global = wl_display_add_global(ec->wl_display, |
| 227 | &input_method_interface, |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame^] | 228 | input_method, |
| 229 | bind_input_method); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 230 | |
| 231 | input_method->destroy_listener.notify = input_method_notifier_destroy; |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 232 | wl_signal_add(&ec->destroy_signal, &input_method->destroy_listener); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 233 | } |