editor, keyboard: Add support for arrow keys
[profile/ivi/weston.git] / clients / editor.c
1 /*
2  * Copyright © 2012 Openismus GmbH
3  * Copyright © 2012 Intel Corporation
4  *
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
24 #include <assert.h>
25 #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
35 static const char *font_name = "sans-serif";
36 static int font_size = 14;
37
38 struct 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
47 struct text_entry {
48         struct widget *widget;
49         struct window *window;
50         char *text;
51         int active;
52         uint32_t cursor;
53         uint32_t anchor;
54         char *preedit_text;
55         uint32_t preedit_cursor;
56         struct text_model *model;
57         struct text_layout *layout;
58 };
59
60 struct editor {
61         struct text_model_factory *text_model_factory;
62         struct display *display;
63         struct window *window;
64         struct widget *widget;
65         struct text_entry *entry;
66         struct text_entry *editor;
67 };
68
69 static struct text_layout *
70 text_layout_create(void)
71 {
72         struct text_layout *layout;
73         cairo_surface_t *surface;
74         cairo_t *cr;
75
76         layout = malloc(sizeof *layout);
77         if (!layout)
78                 return NULL;
79
80         layout->glyphs = NULL;
81         layout->num_glyphs = 0;
82
83         layout->clusters = NULL;
84         layout->num_clusters = 0;
85
86         surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
87         cr = cairo_create(surface);
88         cairo_set_font_size(cr, font_size);
89         cairo_select_font_face(cr, font_name, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
90         layout->font = cairo_get_scaled_font(cr);
91         cairo_scaled_font_reference(layout->font);
92
93         cairo_destroy(cr);
94         cairo_surface_destroy(surface);
95
96         return layout;
97 }
98
99 static void
100 text_layout_destroy(struct text_layout *layout)
101 {
102         if (layout->glyphs)
103                 cairo_glyph_free(layout->glyphs);
104
105         if (layout->clusters)
106                 cairo_text_cluster_free(layout->clusters);
107
108         cairo_scaled_font_destroy(layout->font);
109
110         free(layout);
111 }
112
113 static void
114 text_layout_set_text(struct text_layout *layout,
115                      const char *text)
116 {
117         if (layout->glyphs)
118                 cairo_glyph_free(layout->glyphs);
119
120         if (layout->clusters)
121                 cairo_text_cluster_free(layout->clusters);
122
123         layout->glyphs = NULL;
124         layout->num_glyphs = 0;
125         layout->clusters = NULL;
126         layout->num_clusters = 0;
127
128         cairo_scaled_font_text_to_glyphs(layout->font, 0, 0, text, -1,
129                                          &layout->glyphs, &layout->num_glyphs,
130                                          &layout->clusters, &layout->num_clusters,
131                                          &layout->cluster_flags);
132 }
133
134 static void
135 text_layout_draw(struct text_layout *layout, cairo_t *cr)
136 {
137         cairo_save(cr);
138         cairo_set_scaled_font(cr, layout->font);
139         cairo_show_glyphs(cr, layout->glyphs, layout->num_glyphs);
140         cairo_restore(cr);
141 }
142
143 static void
144 text_layout_extents(struct text_layout *layout, cairo_text_extents_t *extents)
145 {
146         cairo_scaled_font_glyph_extents(layout->font,
147                                         layout->glyphs, layout->num_glyphs,
148                                         extents);
149 }
150
151 static int
152 text_layout_xy_to_index(struct text_layout *layout, double x, double y)
153 {
154         cairo_text_extents_t extents;
155         int i;
156
157         cairo_scaled_font_glyph_extents(layout->font,
158                                         layout->glyphs, layout->num_glyphs,
159                                         &extents);
160
161         for (i = 1; i < layout->num_glyphs; i++) {
162                 if (layout->glyphs[i].x >= x) {
163                         return i - 1;
164                 }
165         }
166
167         if (x >= layout->glyphs[layout->num_glyphs - 1].x && x < extents.width)
168                 return layout->num_glyphs - 1;
169
170         return layout->num_glyphs;
171 }
172
173 static void
174 text_layout_index_to_pos(struct text_layout *layout, uint32_t index, cairo_rectangle_t *pos)
175 {
176         cairo_text_extents_t extents;
177
178         if (!pos)
179                 return;
180
181         cairo_scaled_font_glyph_extents(layout->font,
182                                         layout->glyphs, layout->num_glyphs,
183                                         &extents);
184
185         if ((int)index >= layout->num_glyphs) {
186                 pos->x = extents.x_advance;
187                 pos->y = layout->num_glyphs ? layout->glyphs[layout->num_glyphs - 1].y : 0;
188                 pos->width = 1;
189                 pos->height = extents.height;
190                 return;
191         }
192
193         pos->x = layout->glyphs[index].x;
194         pos->y = layout->glyphs[index].y;
195         pos->width = (int)index < layout->num_glyphs - 1 ? layout->glyphs[index + 1].x : extents.x_advance - pos->x;
196         pos->height = extents.height;
197 }
198
199 static void
200 text_layout_get_cursor_pos(struct text_layout *layout, int index, cairo_rectangle_t *pos)
201 {
202         text_layout_index_to_pos(layout, index, pos);
203         pos->width = 1;
204 }
205
206 static void text_entry_redraw_handler(struct widget *widget, void *data);
207 static void text_entry_button_handler(struct widget *widget,
208                                       struct input *input, uint32_t time,
209                                       uint32_t button,
210                                       enum wl_pointer_button_state state, void *data);
211 static void text_entry_insert_at_cursor(struct text_entry *entry, const char *text);
212 static void text_entry_set_preedit(struct text_entry *entry,
213                                    const char *preedit_text,
214                                    int preedit_cursor);
215 static void text_entry_delete_text(struct text_entry *entry,
216                                    uint32_t index, uint32_t length);
217
218 static void
219 text_model_commit_string(void *data,
220                          struct text_model *text_model,
221                          const char *text,
222                          uint32_t index)
223 {
224         struct text_entry *entry = data;
225
226         if (index > strlen(text)) {
227                 fprintf(stderr, "Invalid cursor index %d\n", index);
228                 index = strlen(text);
229         }
230
231         text_entry_insert_at_cursor(entry, text);
232
233         widget_schedule_redraw(entry->widget);
234 }
235
236 static void
237 text_model_preedit_string(void *data,
238                           struct text_model *text_model,
239                           const char *text,
240                           uint32_t index)
241 {
242         struct text_entry *entry = data;
243
244         if (index > strlen(text)) {
245                 fprintf(stderr, "Invalid cursor index %d\n", index);
246                 index = strlen(text);
247         }
248
249         text_entry_set_preedit(entry, text, index);
250
251         widget_schedule_redraw(entry->widget);
252 }
253
254 static void
255 text_model_delete_surrounding_text(void *data,
256                                    struct text_model *text_model,
257                                    int32_t index,
258                                    uint32_t length)
259 {
260         struct text_entry *entry = data;
261         uint32_t cursor_index = index + entry->cursor;
262
263         if (cursor_index > strlen(entry->text)) {
264                 fprintf(stderr, "Invalid cursor index %d\n", index);
265                 return;
266         }
267
268         if (cursor_index + length > strlen(entry->text)) {
269                 fprintf(stderr, "Invalid length %d\n", length);
270                 return;
271         }
272
273         if (length == 0)
274                 return;
275
276         text_entry_delete_text(entry, cursor_index, length);
277 }
278
279 static void
280 text_model_preedit_styling(void *data,
281                            struct text_model *text_model)
282 {
283 }
284
285 static void
286 text_model_key(void *data,
287                struct text_model *text_model,
288                uint32_t key,
289                uint32_t state)
290 {
291         struct text_entry *entry = data;
292         const char *state_label;
293         const char *key_label = "released";
294
295         if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
296                 state_label = "pressed";
297         }
298
299         switch (key) {
300                 case XKB_KEY_Tab:
301                         key_label = "Tab";
302                         break;
303                 case XKB_KEY_KP_Enter:
304                         key_label = "Enter";
305                         break;
306                 case XKB_KEY_Left:
307                         if (entry->cursor > 0) {
308                                 entry->cursor--;
309                                 entry->anchor = entry->cursor;
310                                 widget_schedule_redraw(entry->widget);
311                         }
312                         break;
313                 case XKB_KEY_Right:
314                         if (entry->cursor < strlen(entry->text)) {
315                                 entry->cursor++;
316                                 entry->anchor = entry->cursor;
317                                 widget_schedule_redraw(entry->widget);
318                         }
319                         break;
320                 default:
321                         key_label = "Unknown";
322         }
323
324         fprintf(stderr, "%s key was %s.\n", key_label, state_label);
325 }
326
327 static void
328 text_model_selection_replacement(void *data,
329                                  struct text_model *text_model)
330 {
331 }
332
333 static void
334 text_model_direction(void *data,
335                      struct text_model *text_model)
336 {
337 }
338
339 static void
340 text_model_locale(void *data,
341                   struct text_model *text_model)
342 {
343 }
344
345 static void
346 text_model_activated(void *data,
347                      struct text_model *text_model)
348 {
349         struct text_entry *entry = data;
350
351         entry->active = 1;
352
353         widget_schedule_redraw(entry->widget);
354 }
355
356 static void
357 text_model_deactivated(void *data,
358                        struct text_model *text_model)
359 {
360         struct text_entry *entry = data;
361
362         entry->active = 0;
363
364         widget_schedule_redraw(entry->widget);
365 }
366
367 static const struct text_model_listener text_model_listener = {
368         text_model_commit_string,
369         text_model_preedit_string,
370         text_model_delete_surrounding_text,
371         text_model_preedit_styling,
372         text_model_key,
373         text_model_selection_replacement,
374         text_model_direction,
375         text_model_locale,
376         text_model_activated,
377         text_model_deactivated
378 };
379
380 static struct text_entry*
381 text_entry_create(struct editor *editor, const char *text)
382 {
383         struct text_entry *entry;
384
385         entry = malloc(sizeof *entry);
386
387         entry->widget = widget_add_widget(editor->widget, entry);
388         entry->window = editor->window;
389         entry->text = strdup(text);
390         entry->active = 0;
391         entry->cursor = strlen(text);
392         entry->anchor = entry->cursor;
393         entry->preedit_text = NULL;
394         entry->preedit_cursor = 0;
395         entry->model = text_model_factory_create_text_model(editor->text_model_factory);
396         text_model_add_listener(entry->model, &text_model_listener, entry);
397
398         entry->layout = text_layout_create();
399         text_layout_set_text(entry->layout, entry->text);
400
401         widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
402         widget_set_button_handler(entry->widget, text_entry_button_handler);
403
404         return entry;
405 }
406
407 static void
408 text_entry_destroy(struct text_entry *entry)
409 {
410         widget_destroy(entry->widget);
411         text_model_destroy(entry->model);
412         text_layout_destroy(entry->layout);
413         free(entry->text);
414         free(entry);
415 }
416
417 static void
418 redraw_handler(struct widget *widget, void *data)
419 {
420         struct editor *editor = data;
421         cairo_surface_t *surface;
422         struct rectangle allocation;
423         cairo_t *cr;
424
425         surface = window_get_surface(editor->window);
426         widget_get_allocation(editor->widget, &allocation);
427
428         cr = cairo_create(surface);
429         cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
430         cairo_clip(cr);
431
432         cairo_translate(cr, allocation.x, allocation.y);
433
434         /* Draw background */
435         cairo_push_group(cr);
436         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
437         cairo_set_source_rgba(cr, 1, 1, 1, 1);
438         cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
439         cairo_fill(cr);
440
441         cairo_pop_group_to_source(cr);
442         cairo_paint(cr);
443
444         cairo_destroy(cr);
445         cairo_surface_destroy(surface);
446 }
447
448 static void
449 text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
450                     int32_t width, int32_t height)
451 {
452         widget_set_allocation(entry->widget, x, y, width, height);
453 }
454
455 static void
456 resize_handler(struct widget *widget,
457                int32_t width, int32_t height, void *data)
458 {
459         struct editor *editor = data;
460         struct rectangle allocation;
461
462         widget_get_allocation(editor->widget, &allocation);
463
464         text_entry_allocate(editor->entry,
465                             allocation.x + 20, allocation.y + 20,
466                             width - 40, height / 2 - 40);
467         text_entry_allocate(editor->editor,
468                             allocation.x + 20, allocation.y + height / 2 + 20,
469                             width - 40, height / 2 - 40);
470 }
471
472 static void
473 text_entry_activate(struct text_entry *entry,
474                     struct wl_seat *seat)
475 {
476         struct wl_surface *surface = window_get_wl_surface(entry->window);
477
478         text_model_activate(entry->model,
479                             seat,
480                             surface);
481 }
482
483 static void
484 text_entry_deactivate(struct text_entry *entry,
485                       struct wl_seat *seat)
486 {
487         text_model_deactivate(entry->model,
488                               seat);
489 }
490
491 static void
492 text_entry_update_layout(struct text_entry *entry)
493 {
494         char *text;
495
496         assert(((unsigned int)entry->cursor) <= strlen(entry->text));
497
498         if (!entry->preedit_text) {
499                 text_layout_set_text(entry->layout, entry->text);
500                 return;
501         }
502
503         text = malloc(strlen(entry->text) + strlen(entry->preedit_text) + 1);
504         strncpy(text, entry->text, entry->cursor);
505         strcpy(text + entry->cursor, entry->preedit_text);
506         strcpy(text + entry->cursor + strlen(entry->preedit_text),
507                entry->text + entry->cursor);
508
509         text_layout_set_text(entry->layout, text);
510         free(text);
511
512         widget_schedule_redraw(entry->widget);
513
514         text_model_set_surrounding_text(entry->model,
515                                         entry->text,
516                                         entry->cursor,
517                                         entry->anchor);
518 }
519
520 static void
521 text_entry_insert_at_cursor(struct text_entry *entry, const char *text)
522 {
523         char *new_text = malloc(strlen(entry->text) + strlen(text) + 1);
524
525         strncpy(new_text, entry->text, entry->cursor);
526         strcpy(new_text + entry->cursor, text);
527         strcpy(new_text + entry->cursor + strlen(text),
528                entry->text + entry->cursor);
529
530         free(entry->text);
531         entry->text = new_text;
532         entry->cursor += strlen(text);
533         entry->anchor += strlen(text);
534
535         text_entry_update_layout(entry);
536 }
537
538 static void
539 text_entry_set_preedit(struct text_entry *entry,
540                        const char *preedit_text,
541                        int preedit_cursor)
542 {
543         if (entry->preedit_text) {
544                 free(entry->preedit_text);
545                 entry->preedit_text = NULL;
546                 entry->preedit_cursor = 0;
547         }
548
549         if (!preedit_text)
550                 return;
551
552         entry->preedit_text = strdup(preedit_text);
553         entry->preedit_cursor = preedit_cursor;
554
555         text_entry_update_layout(entry);
556 }
557
558 static void
559 text_entry_set_cursor_position(struct text_entry *entry,
560                                int32_t x, int32_t y)
561 {
562         entry->cursor = text_layout_xy_to_index(entry->layout, x, y);
563
564         text_model_reset(entry->model);
565
566         if (entry->cursor >= entry->preedit_cursor) {
567                 entry->cursor -= entry->preedit_cursor;
568         }
569
570         text_entry_update_layout(entry);
571
572         widget_schedule_redraw(entry->widget);
573 }
574
575 static void
576 text_entry_set_anchor_position(struct text_entry *entry,
577                                int32_t x, int32_t y)
578 {
579         entry->anchor = text_layout_xy_to_index(entry->layout, x, y);
580
581         widget_schedule_redraw(entry->widget);
582 }
583
584 static void
585 text_entry_delete_text(struct text_entry *entry,
586                        uint32_t index, uint32_t length)
587 {
588         if (entry->cursor > index)
589                 entry->cursor -= length;
590
591         entry->text[index] = '\0';
592         strcat(entry->text, entry->text + index + length);
593
594         text_entry_update_layout(entry);
595
596         widget_schedule_redraw(entry->widget);
597 }
598
599 static void
600 text_entry_draw_selection(struct text_entry *entry, cairo_t *cr)
601 {
602         cairo_text_extents_t extents;
603         uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
604         uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
605         cairo_rectangle_t start;
606         cairo_rectangle_t end;
607
608         if (entry->anchor == entry->cursor)
609                 return;
610
611         text_layout_extents(entry->layout, &extents);
612
613         text_layout_index_to_pos(entry->layout, start_index, &start);
614         text_layout_index_to_pos(entry->layout, end_index, &end);
615
616         cairo_save (cr);
617
618         cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
619         cairo_rectangle(cr,
620                         start.x, extents.y_bearing + extents.height + 2,
621                         end.x - start.x, -extents.height - 4);
622         cairo_fill(cr);
623
624         cairo_rectangle(cr,
625                         start.x, extents.y_bearing + extents.height,
626                         end.x - start.x, -extents.height);
627         cairo_clip(cr);
628         cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
629         text_layout_draw(entry->layout, cr);
630
631         cairo_restore (cr);
632 }
633
634 static void
635 text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
636 {
637         cairo_text_extents_t extents;
638         cairo_rectangle_t cursor_pos;
639
640         text_layout_extents(entry->layout, &extents);
641         text_layout_get_cursor_pos(entry->layout,
642                                    entry->cursor + entry->preedit_cursor,
643                                    &cursor_pos);
644
645         cairo_set_line_width(cr, 1.0);
646         cairo_move_to(cr, cursor_pos.x, extents.y_bearing + extents.height + 2);
647         cairo_line_to(cr, cursor_pos.x, extents.y_bearing - 2);
648         cairo_stroke(cr);
649 }
650
651 static void
652 text_entry_draw_preedit(struct text_entry *entry, cairo_t *cr)
653 {
654         cairo_text_extents_t extents;
655         cairo_rectangle_t start;
656         cairo_rectangle_t end;
657
658         if (!entry->preedit_text)
659                 return;
660
661         text_layout_extents(entry->layout, &extents);
662
663         text_layout_index_to_pos(entry->layout, entry->cursor, &start);
664         text_layout_index_to_pos(entry->layout,
665                                  entry->cursor + strlen(entry->preedit_text),
666                                  &end);
667
668         cairo_save (cr);
669
670         cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
671         cairo_rectangle(cr,
672                         start.x, 0,
673                         end.x - start.x, 1);
674         cairo_fill(cr);
675
676         cairo_restore (cr);
677 }
678
679 static void
680 text_entry_redraw_handler(struct widget *widget, void *data)
681 {
682         struct text_entry *entry = data;
683         cairo_surface_t *surface;
684         struct rectangle allocation;
685         cairo_t *cr;
686
687         surface = window_get_surface(entry->window);
688         widget_get_allocation(entry->widget, &allocation);
689
690         cr = cairo_create(surface);
691         cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
692         cairo_clip(cr);
693
694         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
695
696         cairo_push_group(cr);
697         cairo_translate(cr, allocation.x, allocation.y);
698
699         cairo_set_source_rgba(cr, 1, 1, 1, 1);
700         cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
701         cairo_fill(cr);
702
703         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
704
705         if (entry->active) {
706                 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
707                 cairo_set_line_width (cr, 3);
708                 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
709                 cairo_stroke(cr);
710         }
711
712         cairo_set_source_rgba(cr, 0, 0, 0, 1);
713
714         cairo_translate(cr, 10, allocation.height / 2);
715         text_layout_draw(entry->layout, cr);
716
717         text_entry_draw_selection(entry, cr);
718
719         text_entry_draw_cursor(entry, cr);
720
721         text_entry_draw_preedit(entry, cr);
722
723         cairo_pop_group_to_source(cr);
724         cairo_paint(cr);
725
726         cairo_destroy(cr);
727         cairo_surface_destroy(surface);
728 }
729
730 static int
731 text_entry_motion_handler(struct widget *widget,
732                           struct input *input, uint32_t time,
733                           float x, float y, void *data)
734 {
735         struct text_entry *entry = data;
736         struct rectangle allocation;
737
738         widget_get_allocation(entry->widget, &allocation);
739
740         text_entry_set_cursor_position(entry,
741                                        x - allocation.x,
742                                        y - allocation.y);
743
744         return CURSOR_IBEAM;
745 }
746
747 static void
748 text_entry_button_handler(struct widget *widget,
749                           struct input *input, uint32_t time,
750                           uint32_t button,
751                           enum wl_pointer_button_state state, void *data)
752 {
753         struct text_entry *entry = data;
754         struct rectangle allocation;
755         int32_t x, y;
756
757         widget_get_allocation(entry->widget, &allocation);
758         input_get_position(input, &x, &y);
759
760         if (button != BTN_LEFT) {
761                 return;
762         }
763
764         text_entry_set_cursor_position(entry,
765                                        x - allocation.x,
766                                        y - allocation.y);
767
768         if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
769                 struct wl_seat *seat = input_get_seat(input);
770
771                 text_entry_activate(entry, seat);
772
773                 text_entry_set_anchor_position(entry,
774                                                x - allocation.x,
775                                                y - allocation.y);
776
777                 widget_set_motion_handler(entry->widget, text_entry_motion_handler);
778         } else {
779                 widget_set_motion_handler(entry->widget, NULL);
780         }
781 }
782
783 static void
784 editor_button_handler(struct widget *widget,
785                       struct input *input, uint32_t time,
786                       uint32_t button,
787                       enum wl_pointer_button_state state, void *data)
788 {
789         struct editor *editor = data;
790
791         if (button != BTN_LEFT) {
792                 return;
793         }
794
795         if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
796                 struct wl_seat *seat = input_get_seat(input);
797
798                 text_entry_deactivate(editor->entry, seat);
799                 text_entry_deactivate(editor->editor, seat);
800         }
801 }
802
803 static void
804 global_handler(struct wl_display *display, uint32_t id,
805                const char *interface, uint32_t version, void *data)
806 {
807         struct editor *editor = data;
808
809         if (!strcmp(interface, "text_model_factory")) {
810                 editor->text_model_factory = wl_display_bind(display, id,
811                                                              &text_model_factory_interface);
812         }
813 }
814
815 int
816 main(int argc, char *argv[])
817 {
818         struct editor editor;
819
820         editor.display = display_create(argc, argv);
821         if (editor.display == NULL) {
822                 fprintf(stderr, "failed to create display: %m\n");
823                 return -1;
824         }
825         wl_display_add_global_listener(display_get_display(editor.display),
826                                        global_handler, &editor);
827
828
829         editor.window = window_create(editor.display);
830         editor.widget = frame_create(editor.window, &editor);
831
832         editor.entry = text_entry_create(&editor, "Entry");
833         editor.editor = text_entry_create(&editor, "Editor");
834         text_entry_set_preedit(editor.editor, "preedit", strlen("preedit"));
835
836         window_set_title(editor.window, "Text Editor");
837
838         widget_set_redraw_handler(editor.widget, redraw_handler);
839         widget_set_resize_handler(editor.widget, resize_handler);
840         widget_set_button_handler(editor.widget, editor_button_handler);
841
842         window_schedule_resize(editor.window, 500, 400);
843
844         display_run(editor.display);
845
846         text_entry_destroy(editor.entry);
847         text_entry_destroy(editor.editor);
848
849         return 0;
850 }