• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2008 Kristian Høgsberg
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #ifndef _WINDOW_H_
25 #define _WINDOW_H_
26 
27 #include "config.h"
28 
29 #include <stdint.h>
30 #include <time.h>
31 #include <xkbcommon/xkbcommon.h>
32 #include <wayland-client.h>
33 #include <cairo.h>
34 #include <libweston/config-parser.h>
35 #include <libweston/zalloc.h>
36 #include "shared/platform.h"
37 
38 struct window;
39 struct widget;
40 struct display;
41 struct input;
42 struct output;
43 
44 struct task {
45 	void (*run)(struct task *task, uint32_t events);
46 	struct wl_list link;
47 };
48 
49 struct rectangle {
50 	int32_t x;
51 	int32_t y;
52 	int32_t width;
53 	int32_t height;
54 };
55 
56 struct display *
57 display_create(int *argc, char *argv[]);
58 
59 void
60 display_destroy(struct display *display);
61 
62 void
63 display_set_user_data(struct display *display, void *data);
64 
65 void *
66 display_get_user_data(struct display *display);
67 
68 struct wl_display *
69 display_get_display(struct display *display);
70 
71 int
72 display_has_subcompositor(struct display *display);
73 
74 cairo_device_t *
75 display_get_cairo_device(struct display *display);
76 
77 struct wl_compositor *
78 display_get_compositor(struct display *display);
79 
80 struct output *
81 display_get_output(struct display *display);
82 
83 uint32_t
84 display_get_serial(struct display *display);
85 
86 typedef void (*display_global_handler_t)(struct display *display,
87 					 uint32_t name,
88 					 const char *interface,
89 					 uint32_t version, void *data);
90 
91 void
92 display_set_global_handler(struct display *display,
93 			   display_global_handler_t handler);
94 void
95 display_set_global_handler_remove(struct display *display,
96 			   display_global_handler_t remove_handler);
97 void *
98 display_bind(struct display *display, uint32_t name,
99 	     const struct wl_interface *interface, uint32_t version);
100 
101 typedef void (*display_output_handler_t)(struct output *output, void *data);
102 
103 /*
104  * The output configure handler is called, when a new output is connected
105  * and we know its current mode, or when the current mode changes.
106  * Test and set the output user data in your handler to know, if the
107  * output is new. Note: 'data' in the configure handler is the display
108  * user data.
109  */
110 void
111 display_set_output_configure_handler(struct display *display,
112 				     display_output_handler_t handler);
113 
114 struct wl_data_source *
115 display_create_data_source(struct display *display);
116 
117 #ifdef EGL_NO_DISPLAY
118 EGLDisplay
119 display_get_egl_display(struct display *d);
120 
121 EGLConfig
122 display_get_argb_egl_config(struct display *d);
123 
124 int
125 display_acquire_window_surface(struct display *display,
126 			       struct window *window,
127 			       EGLContext ctx);
128 void
129 display_release_window_surface(struct display *display,
130 			       struct window *window);
131 #endif
132 
133 #define SURFACE_OPAQUE 0x01
134 #define SURFACE_SHM    0x02
135 
136 #define SURFACE_HINT_RESIZE 0x10
137 
138 cairo_surface_t *
139 display_create_surface(struct display *display,
140 		       struct wl_surface *surface,
141 		       struct rectangle *rectangle,
142 		       uint32_t flags);
143 
144 struct wl_buffer *
145 display_get_buffer_for_surface(struct display *display,
146 			       cairo_surface_t *surface);
147 
148 struct wl_cursor_image *
149 display_get_pointer_image(struct display *display, int pointer);
150 
151 void
152 display_defer(struct display *display, struct task *task);
153 
154 void
155 display_watch_fd(struct display *display,
156 		 int fd, uint32_t events, struct task *task);
157 
158 void
159 display_unwatch_fd(struct display *display, int fd);
160 
161 void
162 display_run(struct display *d);
163 
164 void
165 display_exit(struct display *d);
166 
167 int
168 display_get_data_device_manager_version(struct display *d);
169 
170 enum cursor_type {
171 	CURSOR_BOTTOM_LEFT,
172 	CURSOR_BOTTOM_RIGHT,
173 	CURSOR_BOTTOM,
174 	CURSOR_DRAGGING,
175 	CURSOR_LEFT_PTR,
176 	CURSOR_LEFT,
177 	CURSOR_RIGHT,
178 	CURSOR_TOP_LEFT,
179 	CURSOR_TOP_RIGHT,
180 	CURSOR_TOP,
181 	CURSOR_IBEAM,
182 	CURSOR_HAND1,
183 	CURSOR_WATCH,
184 	CURSOR_DND_MOVE,
185 	CURSOR_DND_COPY,
186 	CURSOR_DND_FORBIDDEN,
187 
188 	CURSOR_BLANK
189 };
190 
191 typedef void (*window_key_handler_t)(struct window *window, struct input *input,
192 				     uint32_t time, uint32_t key, uint32_t unicode,
193 				     enum wl_keyboard_key_state state, void *data);
194 
195 typedef void (*window_keyboard_focus_handler_t)(struct window *window,
196 						struct input *device, void *data);
197 
198 typedef void (*window_data_handler_t)(struct window *window,
199 				      struct input *input,
200 				      float x, float y,
201 				      const char **types,
202 				      void *data);
203 
204 typedef void (*window_drop_handler_t)(struct window *window,
205 				      struct input *input,
206 				      int32_t x, int32_t y, void *data);
207 
208 typedef void (*window_close_handler_t)(void *data);
209 typedef void (*window_fullscreen_handler_t)(struct window *window, void *data);
210 
211 typedef void (*window_output_handler_t)(struct window *window, struct output *output,
212 					int enter, void *data);
213 typedef void (*window_state_changed_handler_t)(struct window *window,
214 					       void *data);
215 
216 
217 typedef void (*window_locked_pointer_motion_handler_t)(struct window *window,
218 						       struct input *input,
219 						       uint32_t time,
220 						       float x, float y,
221 						       void *data);
222 
223 typedef void (*locked_pointer_locked_handler_t)(struct window *window,
224 						struct input *input,
225 						void *data);
226 
227 typedef void (*locked_pointer_unlocked_handler_t)(struct window *window,
228 						  struct input *input,
229 						  void *data);
230 
231 typedef void (*confined_pointer_confined_handler_t)(struct window *window,
232 						    struct input *input,
233 						    void *data);
234 
235 typedef void (*confined_pointer_unconfined_handler_t)(struct window *window,
236 						      struct input *input,
237 						      void *data);
238 
239 typedef void (*widget_resize_handler_t)(struct widget *widget,
240 					int32_t width, int32_t height,
241 					void *data);
242 typedef void (*widget_redraw_handler_t)(struct widget *widget, void *data);
243 
244 typedef int (*widget_enter_handler_t)(struct widget *widget,
245 				      struct input *input,
246 				      float x, float y, void *data);
247 typedef void (*widget_leave_handler_t)(struct widget *widget,
248 				       struct input *input, void *data);
249 typedef int (*widget_motion_handler_t)(struct widget *widget,
250 				       struct input *input, uint32_t time,
251 				       float x, float y, void *data);
252 typedef void (*widget_button_handler_t)(struct widget *widget,
253 					struct input *input, uint32_t time,
254 					uint32_t button,
255 					enum wl_pointer_button_state state,
256 					void *data);
257 typedef void (*widget_touch_down_handler_t)(struct widget *widget,
258 					    struct input *input,
259 					    uint32_t serial,
260 					    uint32_t time,
261 					    int32_t id,
262 					    float x,
263 					    float y,
264 					    void *data);
265 typedef void (*widget_touch_up_handler_t)(struct widget *widget,
266 					  struct input *input,
267 					  uint32_t serial,
268 					  uint32_t time,
269 					  int32_t id,
270 					  void *data);
271 typedef void (*widget_touch_motion_handler_t)(struct widget *widget,
272 					      struct input *input,
273 					      uint32_t time,
274 					      int32_t id,
275 					      float x,
276 					      float y,
277 					      void *data);
278 typedef void (*widget_touch_frame_handler_t)(struct widget *widget,
279 					     struct input *input, void *data);
280 typedef void (*widget_touch_cancel_handler_t)(struct widget *widget,
281 					      struct input *input, void *data);
282 typedef void (*widget_axis_handler_t)(struct widget *widget,
283 				      struct input *input, uint32_t time,
284 				      uint32_t axis,
285 				      wl_fixed_t value,
286 				      void *data);
287 
288 typedef void (*widget_pointer_frame_handler_t)(struct widget *widget,
289 					       struct input *input,
290 					       void *data);
291 
292 typedef void (*widget_axis_source_handler_t)(struct widget *widget,
293 					     struct input *input,
294 					     uint32_t source,
295 					     void *data);
296 
297 typedef void (*widget_axis_stop_handler_t)(struct widget *widget,
298 					   struct input *input,
299 					   uint32_t time,
300 					   uint32_t axis,
301 					   void *data);
302 
303 typedef void (*widget_axis_discrete_handler_t)(struct widget *widget,
304 					       struct input *input,
305 					       uint32_t axis,
306 					       int32_t discrete,
307 					       void *data);
308 
309 struct window *
310 window_create(struct display *display);
311 struct window *
312 window_create_custom(struct display *display);
313 
314 void
315 window_set_parent(struct window *window, struct window *parent_window);
316 struct window *
317 window_get_parent(struct window *window);
318 
319 int
320 window_has_focus(struct window *window);
321 
322 typedef void (*menu_func_t)(void *data, struct input *input, int index);
323 
324 void
325 window_show_menu(struct display *display,
326 		 struct input *input, uint32_t time, struct window *parent,
327 		 int32_t x, int32_t y,
328 		 menu_func_t func, const char **entries, int count);
329 
330 void
331 window_show_frame_menu(struct window *window,
332 		       struct input *input, uint32_t time);
333 
334 int
335 window_get_buffer_transform(struct window *window);
336 
337 void
338 window_set_buffer_transform(struct window *window,
339 			    enum wl_output_transform transform);
340 
341 uint32_t
342 window_get_buffer_scale(struct window *window);
343 
344 void
345 window_set_buffer_scale(struct window *window,
346                         int32_t scale);
347 
348 uint32_t
349 window_get_output_scale(struct window *window);
350 
351 void
352 window_destroy(struct window *window);
353 
354 struct widget *
355 window_add_widget(struct window *window, void *data);
356 
357 enum subsurface_mode {
358 	SUBSURFACE_SYNCHRONIZED,
359 	SUBSURFACE_DESYNCHRONIZED
360 };
361 
362 struct widget *
363 window_add_subsurface(struct window *window, void *data,
364 		      enum subsurface_mode default_mode);
365 
366 typedef void (*data_func_t)(void *data, size_t len,
367 			    int32_t x, int32_t y, void *user_data);
368 
369 struct display *
370 window_get_display(struct window *window);
371 void
372 window_move(struct window *window, struct input *input, uint32_t time);
373 void
374 window_get_allocation(struct window *window, struct rectangle *allocation);
375 void
376 window_schedule_redraw(struct window *window);
377 void
378 window_schedule_resize(struct window *window, int width, int height);
379 
380 int
381 window_lock_pointer(struct window *window, struct input *input);
382 
383 void
384 window_unlock_pointer(struct window *window);
385 
386 void
387 widget_set_locked_pointer_cursor_hint(struct widget *widget,
388 				      float x, float y);
389 
390 int
391 window_confine_pointer_to_rectangles(struct window *window,
392 				     struct input *input,
393 				     struct rectangle *rectangles,
394 				     int num_rectangles);
395 
396 void
397 window_update_confine_rectangles(struct window *window,
398 				 struct rectangle *rectangles,
399 				 int num_rectangles);
400 
401 int
402 window_confine_pointer_to_widget(struct window *window,
403 				 struct widget *widget,
404 				 struct input *input);
405 
406 void
407 window_unconfine_pointer(struct window *window);
408 
409 cairo_surface_t *
410 window_get_surface(struct window *window);
411 
412 struct wl_surface *
413 window_get_wl_surface(struct window *window);
414 
415 struct wl_subsurface *
416 widget_get_wl_subsurface(struct widget *widget);
417 
418 enum window_buffer_type {
419 	WINDOW_BUFFER_TYPE_EGL_WINDOW,
420 	WINDOW_BUFFER_TYPE_SHM,
421 };
422 
423 void
424 display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
425 		       int32_t x, int32_t y, int32_t width, int32_t height);
426 
427 void
428 window_set_buffer_type(struct window *window, enum window_buffer_type type);
429 
430 enum window_buffer_type
431 window_get_buffer_type(struct window *window);
432 
433 int
434 window_is_fullscreen(struct window *window);
435 
436 void
437 window_set_fullscreen(struct window *window, int fullscreen);
438 
439 int
440 window_is_maximized(struct window *window);
441 
442 void
443 window_set_maximized(struct window *window, int maximized);
444 
445 int
446 window_is_resizing(struct window *window);
447 
448 void
449 window_set_minimized(struct window *window);
450 
451 void
452 window_set_user_data(struct window *window, void *data);
453 
454 void *
455 window_get_user_data(struct window *window);
456 
457 void
458 window_set_key_handler(struct window *window,
459 		       window_key_handler_t handler);
460 
461 void
462 window_set_keyboard_focus_handler(struct window *window,
463 				  window_keyboard_focus_handler_t handler);
464 
465 void
466 window_set_data_handler(struct window *window,
467 			window_data_handler_t handler);
468 
469 void
470 window_set_drop_handler(struct window *window,
471 			window_drop_handler_t handler);
472 
473 void
474 window_set_close_handler(struct window *window,
475 			 window_close_handler_t handler);
476 void
477 window_set_fullscreen_handler(struct window *window,
478 			      window_fullscreen_handler_t handler);
479 void
480 window_set_output_handler(struct window *window,
481 			  window_output_handler_t handler);
482 void
483 window_set_state_changed_handler(struct window *window,
484 				 window_state_changed_handler_t handler);
485 
486 void
487 window_set_pointer_locked_handler(struct window *window,
488 				  locked_pointer_locked_handler_t locked,
489 				  locked_pointer_unlocked_handler_t unlocked);
490 
491 void
492 window_set_pointer_confined_handler(struct window *window,
493 				    confined_pointer_confined_handler_t confined,
494 				    confined_pointer_unconfined_handler_t unconfined);
495 
496 void
497 window_set_locked_pointer_motion_handler(
498 	struct window *window, window_locked_pointer_motion_handler_t handler);
499 
500 void
501 window_set_title(struct window *window, const char *title);
502 
503 const char *
504 window_get_title(struct window *window);
505 
506 void
507 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y);
508 
509 int
510 widget_set_tooltip(struct widget *parent, char *entry, float x, float y);
511 
512 void
513 widget_destroy_tooltip(struct widget *parent);
514 
515 struct widget *
516 widget_add_widget(struct widget *parent, void *data);
517 
518 void
519 widget_destroy(struct widget *widget);
520 void
521 widget_set_default_cursor(struct widget *widget, int cursor);
522 void
523 widget_get_allocation(struct widget *widget, struct rectangle *allocation);
524 
525 void
526 widget_set_allocation(struct widget *widget,
527 		      int32_t x, int32_t y, int32_t width, int32_t height);
528 void
529 widget_set_size(struct widget *widget, int32_t width, int32_t height);
530 void
531 widget_set_transparent(struct widget *widget, int transparent);
532 void
533 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height);
534 
535 void *
536 widget_get_user_data(struct widget *widget);
537 
538 cairo_t *
539 widget_cairo_create(struct widget *widget);
540 
541 struct wl_surface *
542 widget_get_wl_surface(struct widget *widget);
543 
544 uint32_t
545 widget_get_last_time(struct widget *widget);
546 
547 void
548 widget_input_region_add(struct widget *widget, const struct rectangle *rect);
549 
550 void
551 widget_set_redraw_handler(struct widget *widget,
552 			  widget_redraw_handler_t handler);
553 void
554 widget_set_resize_handler(struct widget *widget,
555 			  widget_resize_handler_t handler);
556 void
557 widget_set_enter_handler(struct widget *widget,
558 			 widget_enter_handler_t handler);
559 void
560 widget_set_leave_handler(struct widget *widget,
561 			 widget_leave_handler_t handler);
562 void
563 widget_set_motion_handler(struct widget *widget,
564 			  widget_motion_handler_t handler);
565 void
566 widget_set_button_handler(struct widget *widget,
567 			  widget_button_handler_t handler);
568 void
569 widget_set_touch_down_handler(struct widget *widget,
570 			      widget_touch_down_handler_t handler);
571 void
572 widget_set_touch_up_handler(struct widget *widget,
573 			    widget_touch_up_handler_t handler);
574 void
575 widget_set_touch_motion_handler(struct widget *widget,
576 				widget_touch_motion_handler_t handler);
577 void
578 widget_set_touch_frame_handler(struct widget *widget,
579 			       widget_touch_frame_handler_t handler);
580 void
581 widget_set_touch_cancel_handler(struct widget *widget,
582 				widget_touch_cancel_handler_t handler);
583 void
584 widget_set_axis_handler(struct widget *widget,
585 			widget_axis_handler_t handler);
586 void
587 widget_set_pointer_frame_handler(struct widget *widget,
588 				 widget_pointer_frame_handler_t handler);
589 void
590 widget_set_axis_handlers(struct widget *widget,
591 			widget_axis_handler_t axis_handler,
592 			widget_axis_source_handler_t axis_source_handler,
593 			widget_axis_stop_handler_t axis_stop_handler,
594 			widget_axis_discrete_handler_t axis_discrete_handler);
595 
596 void
597 window_inhibit_redraw(struct window *window);
598 void
599 window_uninhibit_redraw(struct window *window);
600 void
601 widget_schedule_redraw(struct widget *widget);
602 void
603 widget_set_use_cairo(struct widget *widget, int use_cairo);
604 
605 /*
606  * Sets the viewport destination for the widget's surface
607  * return 0 on success and -1 on failure. Set width and height to
608  * -1 to reset the viewport.
609  */
610 int
611 widget_set_viewport_destination(struct widget *widget, int width, int height);
612 
613 struct widget *
614 window_frame_create(struct window *window, void *data);
615 
616 void
617 window_frame_set_child_size(struct widget *widget, int child_width,
618 			    int child_height);
619 
620 void
621 input_set_pointer_image(struct input *input, int pointer);
622 
623 void
624 input_get_position(struct input *input, int32_t *x, int32_t *y);
625 
626 int
627 input_get_touch(struct input *input, int32_t id, float *x, float *y);
628 
629 #define MOD_SHIFT_MASK		0x01
630 #define MOD_ALT_MASK		0x02
631 #define MOD_CONTROL_MASK	0x04
632 
633 uint32_t
634 input_get_modifiers(struct input *input);
635 
636 void
637 touch_grab(struct input *input, int32_t touch_id);
638 
639 void
640 touch_ungrab(struct input *input);
641 
642 void
643 input_grab(struct input *input, struct widget *widget, uint32_t button);
644 
645 void
646 input_ungrab(struct input *input);
647 
648 struct widget *
649 input_get_focus_widget(struct input *input);
650 
651 struct display *
652 input_get_display(struct input *input);
653 
654 struct wl_seat *
655 input_get_seat(struct input *input);
656 
657 struct wl_data_device *
658 input_get_data_device(struct input *input);
659 
660 void
661 input_set_selection(struct input *input,
662 		    struct wl_data_source *source, uint32_t time);
663 
664 void
665 input_accept(struct input *input, const char *type);
666 
667 
668 void
669 input_receive_drag_data(struct input *input, const char *mime_type,
670 			data_func_t func, void *user_data);
671 int
672 input_receive_drag_data_to_fd(struct input *input,
673 			      const char *mime_type, int fd);
674 
675 int
676 input_receive_selection_data(struct input *input, const char *mime_type,
677 			     data_func_t func, void *data);
678 int
679 input_receive_selection_data_to_fd(struct input *input,
680 				   const char *mime_type, int fd);
681 
682 void
683 output_set_user_data(struct output *output, void *data);
684 
685 void *
686 output_get_user_data(struct output *output);
687 
688 void
689 output_set_destroy_handler(struct output *output,
690 			   display_output_handler_t handler);
691 
692 void
693 output_get_allocation(struct output *output, struct rectangle *allocation);
694 
695 struct wl_output *
696 output_get_wl_output(struct output *output);
697 
698 enum wl_output_transform
699 output_get_transform(struct output *output);
700 
701 uint32_t
702 output_get_scale(struct output *output);
703 
704 const char *
705 output_get_make(struct output *output);
706 
707 const char *
708 output_get_model(struct output *output);
709 
710 void
711 keysym_modifiers_add(struct wl_array *modifiers_map,
712 		     const char *name);
713 
714 xkb_mod_mask_t
715 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
716 			  const char *name);
717 
718 struct toytimer;
719 typedef void (*toytimer_cb)(struct toytimer *);
720 
721 struct toytimer {
722 	struct display *display;
723 	struct task tsk;
724 	int fd;
725 	toytimer_cb callback;
726 };
727 
728 void
729 toytimer_init(struct toytimer *tt, clockid_t clock, struct display *display,
730 	      toytimer_cb callback);
731 
732 void
733 toytimer_fini(struct toytimer *tt);
734 
735 void
736 toytimer_arm(struct toytimer *tt, const struct itimerspec *its);
737 
738 void
739 toytimer_arm_once_usec(struct toytimer *tt, uint32_t usec);
740 
741 void
742 toytimer_disarm(struct toytimer *tt);
743 
744 #endif
745