blob: 295018a3a3046259afacd58144a300a2e9438eaf [file] [log] [blame]
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001/*
2 * Copyright © 2012 Openismus GmbH
Jan Arne Petersen4c265182012-09-09 23:08:30 +02003 * Copyright © 2012 Intel Corporation
Jan Arne Petersencba9e472012-06-21 21:52:19 +02004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
Philipp Brüschweiler591cfca2012-07-11 22:25:29 +020024#include <assert.h>
Jan Arne Petersencba9e472012-06-21 21:52:19 +020025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
29#include <linux/input.h>
30#include <cairo.h>
31
32#include "window.h"
33#include "text-client-protocol.h"
34
Jan Arne Petersenb9eb02c2012-09-09 23:08:35 +020035static const char *font_name = "sans-serif";
36static int font_size = 14;
37
38struct text_layout {
39 cairo_glyph_t *glyphs;
40 int num_glyphs;
41 cairo_text_cluster_t *clusters;
42 int num_clusters;
43 cairo_text_cluster_flags_t cluster_flags;
44 cairo_scaled_font_t *font;
45};
46
Jan Arne Petersencba9e472012-06-21 21:52:19 +020047struct text_entry {
48 struct widget *widget;
Jan Arne Petersene829adc2012-08-10 16:47:22 +020049 struct window *window;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020050 char *text;
51 int active;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +020052 uint32_t cursor;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020053 struct text_model *model;
Jan Arne Petersenb9eb02c2012-09-09 23:08:35 +020054 struct text_layout *layout;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020055};
56
57struct editor {
Jan Arne Petersen51963742012-08-10 16:47:20 +020058 struct text_model_factory *text_model_factory;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020059 struct display *display;
60 struct window *window;
61 struct widget *widget;
62 struct text_entry *entry;
63 struct text_entry *editor;
64};
65
Jan Arne Petersenb9eb02c2012-09-09 23:08:35 +020066static struct text_layout *
67text_layout_create(void)
68{
69 struct text_layout *layout;
70 cairo_surface_t *surface;
71 cairo_t *cr;
72
73 layout = malloc(sizeof *layout);
74 if (!layout)
75 return NULL;
76
77 layout->glyphs = NULL;
78 layout->num_glyphs = 0;
79
80 layout->clusters = NULL;
81 layout->num_clusters = 0;
82
83 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
84 cr = cairo_create(surface);
85 cairo_set_font_size(cr, font_size);
86 cairo_select_font_face(cr, font_name, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
87 layout->font = cairo_get_scaled_font(cr);
88 cairo_scaled_font_reference(layout->font);
89
90 cairo_destroy(cr);
91 cairo_surface_destroy(surface);
92
93 return layout;
94}
95
96static void
97text_layout_destroy(struct text_layout *layout)
98{
99 if (layout->glyphs)
100 cairo_glyph_free(layout->glyphs);
101
102 if (layout->clusters)
103 cairo_text_cluster_free(layout->clusters);
104
105 cairo_scaled_font_destroy(layout->font);
106
107 free(layout);
108}
109
110static void
111text_layout_set_text(struct text_layout *layout,
112 const char *text)
113{
114 if (layout->glyphs)
115 cairo_glyph_free(layout->glyphs);
116
117 if (layout->clusters)
118 cairo_text_cluster_free(layout->clusters);
119
120 layout->glyphs = NULL;
121 layout->num_glyphs = 0;
122 layout->clusters = NULL;
123 layout->num_clusters = 0;
124
125 cairo_scaled_font_text_to_glyphs(layout->font, 0, 0, text, -1,
126 &layout->glyphs, &layout->num_glyphs,
127 &layout->clusters, &layout->num_clusters,
128 &layout->cluster_flags);
129}
130
131static void
132text_layout_draw(struct text_layout *layout, cairo_t *cr)
133{
134 cairo_save(cr);
135 cairo_set_scaled_font(cr, layout->font);
136 cairo_show_glyphs(cr, layout->glyphs, layout->num_glyphs);
137 cairo_restore(cr);
138}
139
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200140static void
141text_layout_extents(struct text_layout *layout, cairo_text_extents_t *extents)
142{
143 cairo_scaled_font_glyph_extents(layout->font,
144 layout->glyphs, layout->num_glyphs,
145 extents);
146}
147
148static int
149text_layout_xy_to_index(struct text_layout *layout, double x, double y)
150{
151 cairo_text_extents_t extents;
152 int i;
153
154 cairo_scaled_font_glyph_extents(layout->font,
155 layout->glyphs, layout->num_glyphs,
156 &extents);
157
158 for (i = 1; i < layout->num_glyphs; i++) {
159 if (layout->glyphs[i].x >= x) {
160 return i - 1;
161 }
162 }
163
164 if (x >= layout->glyphs[layout->num_glyphs - 1].x && x < extents.width)
165 return layout->num_glyphs - 1;
166
167 return layout->num_glyphs;
168}
169
170static void
171text_layout_index_to_pos(struct text_layout *layout, uint32_t index, cairo_rectangle_t *pos)
172{
173 cairo_text_extents_t extents;
174
175 if (!pos)
176 return;
177
178 cairo_scaled_font_glyph_extents(layout->font,
179 layout->glyphs, layout->num_glyphs,
180 &extents);
181
182 if ((int)index >= layout->num_glyphs) {
183 pos->x = extents.x_advance;
184 pos->y = layout->num_glyphs ? layout->glyphs[layout->num_glyphs - 1].y : 0;
185 pos->width = 1;
186 pos->height = extents.height;
187 return;
188 }
189
190 pos->x = layout->glyphs[index].x;
191 pos->y = layout->glyphs[index].y;
192 pos->width = (int)index < layout->num_glyphs - 1 ? layout->glyphs[index + 1].x : extents.x_advance - pos->x;
193 pos->height = extents.height;
194}
195
196static void
197text_layout_get_cursor_pos(struct text_layout *layout, int index, cairo_rectangle_t *pos)
198{
199 text_layout_index_to_pos(layout, index, pos);
200 pos->width = 1;
201}
Jan Arne Petersenb9eb02c2012-09-09 23:08:35 +0200202
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200203static void text_entry_redraw_handler(struct widget *widget, void *data);
204static void text_entry_button_handler(struct widget *widget,
205 struct input *input, uint32_t time,
206 uint32_t button,
207 enum wl_pointer_button_state state, void *data);
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200208static void text_entry_insert_at_cursor(struct text_entry *entry, const char *text);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200209
210static void
211text_model_commit_string(void *data,
212 struct text_model *text_model,
213 const char *text,
214 uint32_t index)
215{
216 struct text_entry *entry = data;
217
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200218 if (index > strlen(text)) {
219 fprintf(stderr, "Invalid cursor index %d\n", index);
220 index = strlen(text);
221 }
222
223 text_entry_insert_at_cursor(entry, text);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200224
225 widget_schedule_redraw(entry->widget);
226}
227
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200228static void
229text_model_preedit_string(void *data,
230 struct text_model *text_model,
231 const char *text,
232 uint32_t index)
233{
234}
235
236static void
237text_model_preedit_styling(void *data,
238 struct text_model *text_model)
239{
240}
241
242static void
243text_model_key(void *data,
244 struct text_model *text_model)
245{
246}
247
248static void
249text_model_selection_replacement(void *data,
250 struct text_model *text_model)
251{
252}
253
254static void
255text_model_direction(void *data,
256 struct text_model *text_model)
257{
258}
259
260static void
261text_model_locale(void *data,
262 struct text_model *text_model)
263{
264}
265
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200266static void
267text_model_activated(void *data,
268 struct text_model *text_model)
269{
270 struct text_entry *entry = data;
271
272 entry->active = 1;
273
274 widget_schedule_redraw(entry->widget);
275}
276
277static void
278text_model_deactivated(void *data,
279 struct text_model *text_model)
280{
281 struct text_entry *entry = data;
282
283 entry->active = 0;
284
285 widget_schedule_redraw(entry->widget);
286}
287
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200288static const struct text_model_listener text_model_listener = {
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200289 text_model_commit_string,
290 text_model_preedit_string,
291 text_model_preedit_styling,
292 text_model_key,
293 text_model_selection_replacement,
294 text_model_direction,
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200295 text_model_locale,
296 text_model_activated,
297 text_model_deactivated
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200298};
299
300static struct text_entry*
301text_entry_create(struct editor *editor, const char *text)
302{
303 struct text_entry *entry;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200304
305 entry = malloc(sizeof *entry);
306
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200307 entry->widget = widget_add_widget(editor->widget, entry);
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200308 entry->window = editor->window;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200309 entry->text = strdup(text);
310 entry->active = 0;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200311 entry->cursor = strlen(text);
Jan Arne Petersen4c265182012-09-09 23:08:30 +0200312 entry->model = text_model_factory_create_text_model(editor->text_model_factory);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200313 text_model_add_listener(entry->model, &text_model_listener, entry);
314
Jan Arne Petersenb9eb02c2012-09-09 23:08:35 +0200315 entry->layout = text_layout_create();
316 text_layout_set_text(entry->layout, entry->text);
317
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200318 widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
319 widget_set_button_handler(entry->widget, text_entry_button_handler);
320
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200321 return entry;
322}
323
324static void
325text_entry_destroy(struct text_entry *entry)
326{
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200327 widget_destroy(entry->widget);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200328 text_model_destroy(entry->model);
Jan Arne Petersenb9eb02c2012-09-09 23:08:35 +0200329 text_layout_destroy(entry->layout);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200330 free(entry->text);
331 free(entry);
332}
333
334static void
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200335redraw_handler(struct widget *widget, void *data)
336{
337 struct editor *editor = data;
338 cairo_surface_t *surface;
339 struct rectangle allocation;
340 cairo_t *cr;
341
342 surface = window_get_surface(editor->window);
343 widget_get_allocation(editor->widget, &allocation);
344
345 cr = cairo_create(surface);
346 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
347 cairo_clip(cr);
348
349 cairo_translate(cr, allocation.x, allocation.y);
350
351 /* Draw background */
352 cairo_push_group(cr);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200353 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200354 cairo_set_source_rgba(cr, 1, 1, 1, 1);
355 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
356 cairo_fill(cr);
357
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200358 cairo_pop_group_to_source(cr);
359 cairo_paint(cr);
360
361 cairo_destroy(cr);
362 cairo_surface_destroy(surface);
363}
364
365static void
366text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
367 int32_t width, int32_t height)
368{
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200369 widget_set_allocation(entry->widget, x, y, width, height);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200370}
371
372static void
373resize_handler(struct widget *widget,
374 int32_t width, int32_t height, void *data)
375{
376 struct editor *editor = data;
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200377 struct rectangle allocation;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200378
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200379 widget_get_allocation(editor->widget, &allocation);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200380
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200381 text_entry_allocate(editor->entry,
382 allocation.x + 20, allocation.y + 20,
383 width - 40, height / 2 - 40);
384 text_entry_allocate(editor->editor,
385 allocation.x + 20, allocation.y + height / 2 + 20,
386 width - 40, height / 2 - 40);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200387}
388
389static void
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200390text_entry_activate(struct text_entry *entry,
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200391 struct wl_seat *seat)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200392{
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200393 struct wl_surface *surface = window_get_wl_surface(entry->window);
394
395 text_model_activate(entry->model,
396 seat,
397 surface);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200398}
399
400static void
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200401text_entry_deactivate(struct text_entry *entry,
402 struct wl_seat *seat)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200403{
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200404 text_model_deactivate(entry->model,
405 seat);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200406}
407
408static void
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200409text_entry_insert_at_cursor(struct text_entry *entry, const char *text)
410{
411 char *new_text = malloc(strlen(entry->text) + strlen(text) + 1);
412
413 strncpy(new_text, entry->text, entry->cursor);
414 strcpy(new_text + entry->cursor, text);
415 strcpy(new_text + entry->cursor + strlen(text),
416 entry->text + entry->cursor);
417
418 free(entry->text);
419 entry->text = new_text;
420 entry->cursor += strlen(text);
421
422 text_layout_set_text(entry->layout, entry->text);
423}
424
425static void
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200426text_entry_set_cursor_position(struct text_entry *entry,
427 int32_t x, int32_t y)
428{
429 entry->cursor = text_layout_xy_to_index(entry->layout, x, y);
430
431 widget_schedule_redraw(entry->widget);
432}
433
434static void
435text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
436{
437 cairo_text_extents_t extents;
438 cairo_rectangle_t cursor_pos;
439
440 text_layout_extents(entry->layout, &extents);
441 text_layout_get_cursor_pos(entry->layout, entry->cursor, &cursor_pos);
442
443 cairo_set_line_width(cr, 1.0);
444 cairo_move_to(cr, cursor_pos.x, extents.y_bearing + extents.height + 2);
445 cairo_line_to(cr, cursor_pos.x, extents.y_bearing - 2);
446 cairo_stroke(cr);
447}
448
449static void
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200450text_entry_redraw_handler(struct widget *widget, void *data)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200451{
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200452 struct text_entry *entry = data;
453 cairo_surface_t *surface;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200454 struct rectangle allocation;
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200455 cairo_t *cr;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200456
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200457 surface = window_get_surface(entry->window);
458 widget_get_allocation(entry->widget, &allocation);
459
460 cr = cairo_create(surface);
461 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
462 cairo_clip(cr);
463
464 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
465
466 cairo_push_group(cr);
467 cairo_translate(cr, allocation.x, allocation.y);
468
469 cairo_set_source_rgba(cr, 1, 1, 1, 1);
470 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
471 cairo_fill(cr);
472
473 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
474
475 if (entry->active) {
476 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
477 cairo_set_line_width (cr, 3);
478 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
479 cairo_stroke(cr);
480 }
481
482 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200483
484 cairo_translate(cr, 10, allocation.height / 2);
Jan Arne Petersenb9eb02c2012-09-09 23:08:35 +0200485 text_layout_draw(entry->layout, cr);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200486
487 text_entry_draw_cursor(entry, cr);
488
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200489 cairo_pop_group_to_source(cr);
490 cairo_paint(cr);
491
492 cairo_destroy(cr);
493 cairo_surface_destroy(surface);
494}
495
496static void
497text_entry_button_handler(struct widget *widget,
498 struct input *input, uint32_t time,
499 uint32_t button,
500 enum wl_pointer_button_state state, void *data)
501{
502 struct text_entry *entry = data;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200503 struct rectangle allocation;
504 int32_t x, y;
505
506 widget_get_allocation(entry->widget, &allocation);
507 input_get_position(input, &x, &y);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200508
509 if (button != BTN_LEFT) {
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200510 return;
511 }
512
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200513 text_entry_set_cursor_position(entry,
514 x - allocation.x,
515 y - allocation.y);
516
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200517 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
518 struct wl_seat *seat = input_get_seat(input);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200519
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200520 text_entry_activate(entry, seat);
521 }
522}
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200523
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200524static void
525editor_button_handler(struct widget *widget,
526 struct input *input, uint32_t time,
527 uint32_t button,
528 enum wl_pointer_button_state state, void *data)
529{
530 struct editor *editor = data;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200531
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200532 if (button != BTN_LEFT) {
533 return;
534 }
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200535
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200536 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
537 struct wl_seat *seat = input_get_seat(input);
538
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200539 text_entry_deactivate(editor->entry, seat);
540 text_entry_deactivate(editor->editor, seat);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200541 }
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200542}
543
544static void
545global_handler(struct wl_display *display, uint32_t id,
546 const char *interface, uint32_t version, void *data)
547{
548 struct editor *editor = data;
549
Jan Arne Petersen51963742012-08-10 16:47:20 +0200550 if (!strcmp(interface, "text_model_factory")) {
551 editor->text_model_factory = wl_display_bind(display, id,
552 &text_model_factory_interface);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200553 }
554}
555
556int
557main(int argc, char *argv[])
558{
559 struct editor editor;
560
561 editor.display = display_create(argc, argv);
562 if (editor.display == NULL) {
563 fprintf(stderr, "failed to create display: %m\n");
564 return -1;
565 }
566 wl_display_add_global_listener(display_get_display(editor.display),
567 global_handler, &editor);
568
569
570 editor.window = window_create(editor.display);
571 editor.widget = frame_create(editor.window, &editor);
572
573 editor.entry = text_entry_create(&editor, "Entry");
574 editor.editor = text_entry_create(&editor, "Editor");
575
576 window_set_title(editor.window, "Text Editor");
577
578 widget_set_redraw_handler(editor.widget, redraw_handler);
579 widget_set_resize_handler(editor.widget, resize_handler);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200580 widget_set_button_handler(editor.widget, editor_button_handler);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200581
582 window_schedule_resize(editor.window, 500, 400);
583
584 display_run(editor.display);
585
586 text_entry_destroy(editor.entry);
587 text_entry_destroy(editor.editor);
588
589 return 0;
590}