text: Add support for control keys to the protocol
[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         const char *state_label;
292         const char *key_label;
293
294         if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
295                 state_label = "pressed";
296         } else {
297                 state_label = "released";
298         }
299
300         switch (key) {
301                 case XKB_KEY_Tab:
302                         key_label = "Tab";
303                         break;
304                 case XKB_KEY_KP_Enter:
305                         key_label = "Enter";
306                         break;
307                 default:
308                         key_label = "Unknown";
309         }
310
311         fprintf(stderr, "%s key was %s.\n", key_label, state_label);
312 }
313
314 static void
315 text_model_selection_replacement(void *data,
316                                  struct text_model *text_model)
317 {
318 }
319
320 static void
321 text_model_direction(void *data,
322                      struct text_model *text_model)
323 {
324 }
325
326 static void
327 text_model_locale(void *data,
328                   struct text_model *text_model)
329 {
330 }
331
332 static void
333 text_model_activated(void *data,
334                      struct text_model *text_model)
335 {
336         struct text_entry *entry = data;
337
338         entry->active = 1;
339
340         widget_schedule_redraw(entry->widget);
341 }
342
343 static void
344 text_model_deactivated(void *data,
345                        struct text_model *text_model)
346 {
347         struct text_entry *entry = data;
348
349         entry->active = 0;
350
351         widget_schedule_redraw(entry->widget);
352 }
353
354 static const struct text_model_listener text_model_listener = {
355         text_model_commit_string,
356         text_model_preedit_string,
357         text_model_delete_surrounding_text,
358         text_model_preedit_styling,
359         text_model_key,
360         text_model_selection_replacement,
361         text_model_direction,
362         text_model_locale,
363         text_model_activated,
364         text_model_deactivated
365 };
366
367 static struct text_entry*
368 text_entry_create(struct editor *editor, const char *text)
369 {
370         struct text_entry *entry;
371
372         entry = malloc(sizeof *entry);
373
374         entry->widget = widget_add_widget(editor->widget, entry);
375         entry->window = editor->window;
376         entry->text = strdup(text);
377         entry->active = 0;
378         entry->cursor = strlen(text);
379         entry->anchor = entry->cursor;
380         entry->preedit_text = NULL;
381         entry->preedit_cursor = 0;
382         entry->model = text_model_factory_create_text_model(editor->text_model_factory);
383         text_model_add_listener(entry->model, &text_model_listener, entry);
384
385         entry->layout = text_layout_create();
386         text_layout_set_text(entry->layout, entry->text);
387
388         widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
389         widget_set_button_handler(entry->widget, text_entry_button_handler);
390
391         return entry;
392 }
393
394 static void
395 text_entry_destroy(struct text_entry *entry)
396 {
397         widget_destroy(entry->widget);
398         text_model_destroy(entry->model);
399         text_layout_destroy(entry->layout);
400         free(entry->text);
401         free(entry);
402 }
403
404 static void
405 redraw_handler(struct widget *widget, void *data)
406 {
407         struct editor *editor = data;
408         cairo_surface_t *surface;
409         struct rectangle allocation;
410         cairo_t *cr;
411
412         surface = window_get_surface(editor->window);
413         widget_get_allocation(editor->widget, &allocation);
414
415         cr = cairo_create(surface);
416         cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
417         cairo_clip(cr);
418
419         cairo_translate(cr, allocation.x, allocation.y);
420
421         /* Draw background */
422         cairo_push_group(cr);
423         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
424         cairo_set_source_rgba(cr, 1, 1, 1, 1);
425         cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
426         cairo_fill(cr);
427
428         cairo_pop_group_to_source(cr);
429         cairo_paint(cr);
430
431         cairo_destroy(cr);
432         cairo_surface_destroy(surface);
433 }
434
435 static void
436 text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
437                     int32_t width, int32_t height)
438 {
439         widget_set_allocation(entry->widget, x, y, width, height);
440 }
441
442 static void
443 resize_handler(struct widget *widget,
444                int32_t width, int32_t height, void *data)
445 {
446         struct editor *editor = data;
447         struct rectangle allocation;
448
449         widget_get_allocation(editor->widget, &allocation);
450
451         text_entry_allocate(editor->entry,
452                             allocation.x + 20, allocation.y + 20,
453                             width - 40, height / 2 - 40);
454         text_entry_allocate(editor->editor,
455                             allocation.x + 20, allocation.y + height / 2 + 20,
456                             width - 40, height / 2 - 40);
457 }
458
459 static void
460 text_entry_activate(struct text_entry *entry,
461                     struct wl_seat *seat)
462 {
463         struct wl_surface *surface = window_get_wl_surface(entry->window);
464
465         text_model_activate(entry->model,
466                             seat,
467                             surface);
468 }
469
470 static void
471 text_entry_deactivate(struct text_entry *entry,
472                       struct wl_seat *seat)
473 {
474         text_model_deactivate(entry->model,
475                               seat);
476 }
477
478 static void
479 text_entry_update_layout(struct text_entry *entry)
480 {
481         char *text;
482
483         assert(((unsigned int)entry->cursor) <= strlen(entry->text));
484
485         if (!entry->preedit_text) {
486                 text_layout_set_text(entry->layout, entry->text);
487                 return;
488         }
489
490         text = malloc(strlen(entry->text) + strlen(entry->preedit_text) + 1);
491         strncpy(text, entry->text, entry->cursor);
492         strcpy(text + entry->cursor, entry->preedit_text);
493         strcpy(text + entry->cursor + strlen(entry->preedit_text),
494                entry->text + entry->cursor);
495
496         text_layout_set_text(entry->layout, text);
497         free(text);
498
499         widget_schedule_redraw(entry->widget);
500
501         text_model_set_surrounding_text(entry->model,
502                                         entry->text,
503                                         entry->cursor,
504                                         entry->anchor);
505 }
506
507 static void
508 text_entry_insert_at_cursor(struct text_entry *entry, const char *text)
509 {
510         char *new_text = malloc(strlen(entry->text) + strlen(text) + 1);
511
512         strncpy(new_text, entry->text, entry->cursor);
513         strcpy(new_text + entry->cursor, text);
514         strcpy(new_text + entry->cursor + strlen(text),
515                entry->text + entry->cursor);
516
517         free(entry->text);
518         entry->text = new_text;
519         entry->cursor += strlen(text);
520         entry->anchor += strlen(text);
521
522         text_entry_update_layout(entry);
523 }
524
525 static void
526 text_entry_set_preedit(struct text_entry *entry,
527                        const char *preedit_text,
528                        int preedit_cursor)
529 {
530         if (entry->preedit_text) {
531                 free(entry->preedit_text);
532                 entry->preedit_text = NULL;
533                 entry->preedit_cursor = 0;
534         }
535
536         if (!preedit_text)
537                 return;
538
539         entry->preedit_text = strdup(preedit_text);
540         entry->preedit_cursor = preedit_cursor;
541
542         text_entry_update_layout(entry);
543 }
544
545 static void
546 text_entry_set_cursor_position(struct text_entry *entry,
547                                int32_t x, int32_t y)
548 {
549         entry->cursor = text_layout_xy_to_index(entry->layout, x, y);
550
551         if (entry->cursor >= entry->preedit_cursor) {
552                 entry->cursor -= entry->preedit_cursor;
553         }
554
555         text_entry_update_layout(entry);
556
557         widget_schedule_redraw(entry->widget);
558 }
559
560 static void
561 text_entry_set_anchor_position(struct text_entry *entry,
562                                int32_t x, int32_t y)
563 {
564         entry->anchor = text_layout_xy_to_index(entry->layout, x, y);
565
566         widget_schedule_redraw(entry->widget);
567 }
568
569 static void
570 text_entry_delete_text(struct text_entry *entry,
571                        uint32_t index, uint32_t length)
572 {
573         if (entry->cursor > index)
574                 entry->cursor -= length;
575
576         entry->text[index] = '\0';
577         strcat(entry->text, entry->text + index + length);
578
579         text_entry_update_layout(entry);
580
581         widget_schedule_redraw(entry->widget);
582 }
583
584 static void
585 text_entry_draw_selection(struct text_entry *entry, cairo_t *cr)
586 {
587         cairo_text_extents_t extents;
588         uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
589         uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
590         cairo_rectangle_t start;
591         cairo_rectangle_t end;
592
593         if (entry->anchor == entry->cursor)
594                 return;
595
596         text_layout_extents(entry->layout, &extents);
597
598         text_layout_index_to_pos(entry->layout, start_index, &start);
599         text_layout_index_to_pos(entry->layout, end_index, &end);
600
601         cairo_save (cr);
602
603         cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
604         cairo_rectangle(cr,
605                         start.x, extents.y_bearing + extents.height + 2,
606                         end.x - start.x, -extents.height - 4);
607         cairo_fill(cr);
608
609         cairo_rectangle(cr,
610                         start.x, extents.y_bearing + extents.height,
611                         end.x - start.x, -extents.height);
612         cairo_clip(cr);
613         cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
614         text_layout_draw(entry->layout, cr);
615
616         cairo_restore (cr);
617 }
618
619 static void
620 text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
621 {
622         cairo_text_extents_t extents;
623         cairo_rectangle_t cursor_pos;
624
625         text_layout_extents(entry->layout, &extents);
626         text_layout_get_cursor_pos(entry->layout,
627                                    entry->cursor + entry->preedit_cursor,
628                                    &cursor_pos);
629
630         cairo_set_line_width(cr, 1.0);
631         cairo_move_to(cr, cursor_pos.x, extents.y_bearing + extents.height + 2);
632         cairo_line_to(cr, cursor_pos.x, extents.y_bearing - 2);
633         cairo_stroke(cr);
634 }
635
636 static void
637 text_entry_draw_preedit(struct text_entry *entry, cairo_t *cr)
638 {
639         cairo_text_extents_t extents;
640         cairo_rectangle_t start;
641         cairo_rectangle_t end;
642
643         if (!entry->preedit_text)
644                 return;
645
646         text_layout_extents(entry->layout, &extents);
647
648         text_layout_index_to_pos(entry->layout, entry->cursor, &start);
649         text_layout_index_to_pos(entry->layout,
650                                  entry->cursor + strlen(entry->preedit_text),
651                                  &end);
652
653         cairo_save (cr);
654
655         cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
656         cairo_rectangle(cr,
657                         start.x, 0,
658                         end.x - start.x, 1);
659         cairo_fill(cr);
660
661         cairo_restore (cr);
662 }
663
664 static void
665 text_entry_redraw_handler(struct widget *widget, void *data)
666 {
667         struct text_entry *entry = data;
668         cairo_surface_t *surface;
669         struct rectangle allocation;
670         cairo_t *cr;
671
672         surface = window_get_surface(entry->window);
673         widget_get_allocation(entry->widget, &allocation);
674
675         cr = cairo_create(surface);
676         cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
677         cairo_clip(cr);
678
679         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
680
681         cairo_push_group(cr);
682         cairo_translate(cr, allocation.x, allocation.y);
683
684         cairo_set_source_rgba(cr, 1, 1, 1, 1);
685         cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
686         cairo_fill(cr);
687
688         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
689
690         if (entry->active) {
691                 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
692                 cairo_set_line_width (cr, 3);
693                 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
694                 cairo_stroke(cr);
695         }
696
697         cairo_set_source_rgba(cr, 0, 0, 0, 1);
698
699         cairo_translate(cr, 10, allocation.height / 2);
700         text_layout_draw(entry->layout, cr);
701
702         text_entry_draw_selection(entry, cr);
703
704         text_entry_draw_cursor(entry, cr);
705
706         text_entry_draw_preedit(entry, cr);
707
708         cairo_pop_group_to_source(cr);
709         cairo_paint(cr);
710
711         cairo_destroy(cr);
712         cairo_surface_destroy(surface);
713 }
714
715 static int
716 text_entry_motion_handler(struct widget *widget,
717                           struct input *input, uint32_t time,
718                           float x, float y, void *data)
719 {
720         struct text_entry *entry = data;
721         struct rectangle allocation;
722
723         widget_get_allocation(entry->widget, &allocation);
724
725         text_entry_set_cursor_position(entry,
726                                        x - allocation.x,
727                                        y - allocation.y);
728
729         return CURSOR_IBEAM;
730 }
731
732 static void
733 text_entry_button_handler(struct widget *widget,
734                           struct input *input, uint32_t time,
735                           uint32_t button,
736                           enum wl_pointer_button_state state, void *data)
737 {
738         struct text_entry *entry = data;
739         struct rectangle allocation;
740         int32_t x, y;
741
742         widget_get_allocation(entry->widget, &allocation);
743         input_get_position(input, &x, &y);
744
745         if (button != BTN_LEFT) {
746                 return;
747         }
748
749         text_entry_set_cursor_position(entry,
750                                        x - allocation.x,
751                                        y - allocation.y);
752
753         if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
754                 struct wl_seat *seat = input_get_seat(input);
755
756                 text_entry_activate(entry, seat);
757
758                 text_entry_set_anchor_position(entry,
759                                                x - allocation.x,
760                                                y - allocation.y);
761
762                 widget_set_motion_handler(entry->widget, text_entry_motion_handler);
763         } else {
764                 widget_set_motion_handler(entry->widget, NULL);
765         }
766 }
767
768 static void
769 editor_button_handler(struct widget *widget,
770                       struct input *input, uint32_t time,
771                       uint32_t button,
772                       enum wl_pointer_button_state state, void *data)
773 {
774         struct editor *editor = data;
775
776         if (button != BTN_LEFT) {
777                 return;
778         }
779
780         if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
781                 struct wl_seat *seat = input_get_seat(input);
782
783                 text_entry_deactivate(editor->entry, seat);
784                 text_entry_deactivate(editor->editor, seat);
785         }
786 }
787
788 static void
789 global_handler(struct wl_display *display, uint32_t id,
790                const char *interface, uint32_t version, void *data)
791 {
792         struct editor *editor = data;
793
794         if (!strcmp(interface, "text_model_factory")) {
795                 editor->text_model_factory = wl_display_bind(display, id,
796                                                              &text_model_factory_interface);
797         }
798 }
799
800 int
801 main(int argc, char *argv[])
802 {
803         struct editor editor;
804
805         editor.display = display_create(argc, argv);
806         if (editor.display == NULL) {
807                 fprintf(stderr, "failed to create display: %m\n");
808                 return -1;
809         }
810         wl_display_add_global_listener(display_get_display(editor.display),
811                                        global_handler, &editor);
812
813
814         editor.window = window_create(editor.display);
815         editor.widget = frame_create(editor.window, &editor);
816
817         editor.entry = text_entry_create(&editor, "Entry");
818         editor.editor = text_entry_create(&editor, "Editor");
819         text_entry_set_preedit(editor.editor, "preedit", strlen("preedit"));
820
821         window_set_title(editor.window, "Text Editor");
822
823         widget_set_redraw_handler(editor.widget, redraw_handler);
824         widget_set_resize_handler(editor.widget, resize_handler);
825         widget_set_button_handler(editor.widget, editor_button_handler);
826
827         window_schedule_resize(editor.window, 500, 400);
828
829         display_run(editor.display);
830
831         text_entry_destroy(editor.entry);
832         text_entry_destroy(editor.editor);
833
834         return 0;
835 }