Tizen 2.1 release
[platform/core/uifw/e17.git] / src / modules / comp / e_mod_comp_wl.c
1 #include "e.h"
2 #include "e_mod_main.h"
3 #ifdef HAVE_WAYLAND_CLIENTS
4 # include <xcb/xcb_image.h>
5 # include "e_mod_comp_wl.h"
6 # include "e_mod_comp_wl_comp.h"
7 # include "e_mod_comp_wl_output.h"
8 # include "e_mod_comp_wl_input.h"
9 # include "e_mod_comp_wl_shell.h"
10 #endif
11
12 /* local function prototypes */
13 static Eina_Bool _e_mod_comp_wl_fd_handle(void *data, Ecore_Fd_Handler *hdl __UNUSED__);
14
15 /* private variables */
16 static Ecore_Fd_Handler *_wl_fd_handler = NULL;
17
18 /* extern variables */
19 struct wl_display *_wl_disp;
20
21 Eina_Bool
22 e_mod_comp_wl_init(void)
23 {
24    struct wl_event_loop *loop;
25    int fd = 0;
26
27    LOGFN(__FILE__, __LINE__, __FUNCTION__);
28
29    /* init wayland display */
30    if (!(_wl_disp = wl_display_create()))
31      {
32         EINA_LOG_ERR("Failed to create wayland display\n");
33         return EINA_FALSE;
34      }
35
36    if (wl_display_add_socket(_wl_disp, NULL))
37      {
38         wl_display_terminate(_wl_disp);
39         EINA_LOG_ERR("Failed to add socket to wayland display\n");
40         return EINA_FALSE;
41      }
42
43    /* init a wayland compositor ?? */
44    if (!e_mod_comp_wl_comp_init())
45      {
46         wl_display_terminate(_wl_disp);
47         EINA_LOG_ERR("Failed to create wayland compositor\n");
48         return EINA_FALSE;
49      }
50
51    /* init output */
52    if (!e_mod_comp_wl_output_init())
53      {
54         e_mod_comp_wl_comp_shutdown();
55         wl_display_terminate(_wl_disp);
56         EINA_LOG_ERR("Failed to create wayland output\n");
57         return EINA_FALSE;
58      }
59
60    /* init input */
61    if (!e_mod_comp_wl_input_init())
62      {
63         e_mod_comp_wl_output_shutdown();
64         e_mod_comp_wl_comp_shutdown();
65         wl_display_terminate(_wl_disp);
66         EINA_LOG_ERR("Failed to create wayland input\n");
67         return EINA_FALSE;
68      }
69
70    /* init a wayland shell */
71    if (!e_mod_comp_wl_shell_init())
72      {
73         e_mod_comp_wl_input_shutdown();
74         e_mod_comp_wl_output_shutdown();
75         e_mod_comp_wl_comp_shutdown();
76         wl_display_terminate(_wl_disp);
77         EINA_LOG_ERR("Failed to create wayland shell\n");
78         return EINA_FALSE;
79      }
80
81    loop = wl_display_get_event_loop(_wl_disp);
82    fd = wl_event_loop_get_fd(loop);
83
84    _wl_fd_handler =
85      ecore_main_fd_handler_add(fd, ECORE_FD_READ,
86                                _e_mod_comp_wl_fd_handle, NULL, NULL, NULL);
87
88    wl_event_loop_dispatch(loop, 0);
89
90    return EINA_TRUE;
91 }
92
93 void
94 e_mod_comp_wl_shutdown(void)
95 {
96    LOGFN(__FILE__, __LINE__, __FUNCTION__);
97
98    if (_wl_fd_handler)
99      ecore_main_fd_handler_del(_wl_fd_handler);
100    _wl_fd_handler = NULL;
101
102    e_mod_comp_wl_shell_shutdown();
103    e_mod_comp_wl_input_shutdown();
104    e_mod_comp_wl_output_shutdown();
105    e_mod_comp_wl_comp_shutdown();
106
107    if (_wl_disp) wl_display_terminate(_wl_disp);
108    _wl_disp = NULL;
109 }
110
111 uint32_t
112 e_mod_comp_wl_time_get(void)
113 {
114    struct timeval tv;
115
116    gettimeofday(&tv, NULL);
117    return tv.tv_sec * 1000 + tv.tv_usec / 1000;
118 }
119
120 Ecore_X_Pixmap
121 e_mod_comp_wl_pixmap_get(Ecore_X_Window win)
122 {
123    Wayland_Compositor *comp;
124    Wayland_Surface *ws;
125 //   struct wl_list *list;
126    Ecore_X_Pixmap pmap = 0;
127
128    LOGFN(__FILE__, __LINE__, __FUNCTION__);
129
130    comp = e_mod_comp_wl_comp_get();
131    if (wl_list_empty(&comp->surfaces)) return 0;
132
133 //   list = &comp->surfaces;
134    wl_list_for_each(ws, &comp->surfaces, link)
135    {
136       if (!ws->buffer) continue;
137       if (((ws->win) && (ws->win->border))
138           && (ws->win->border->win == win))
139         {
140            Ecore_X_Connection *conn;
141            Ecore_X_GC gc;
142            uint8_t *pix = 0;
143            int depth;
144
145            if (ws->buffer)
146              {
147                 if (wl_buffer_is_shm(ws->buffer))
148                   pix = (uint8_t *)wl_shm_buffer_get_data(ws->buffer);
149                 else
150                   {
151                      if (ws->texture) pix = (uint8_t *)ws->texture;
152                      else if (ws->saved_texture)
153                        pix = (uint8_t *)ws->saved_texture;
154                   }
155              }
156            else if (ws->image)
157              {
158                 if (ws->texture) pix = (uint8_t *)ws->texture;
159                 else if (ws->saved_texture)
160                   pix = (uint8_t *)ws->saved_texture;
161              }
162
163            if (!pix) return 0;
164
165            depth = ecore_x_window_depth_get(win);
166            conn = ecore_x_connection_get();
167
168            pmap = xcb_generate_id(conn);
169            xcb_create_pixmap(conn, depth, pmap, win, ws->w, ws->h);
170
171            gc = ecore_x_gc_new(pmap, 0, NULL);
172            xcb_put_image(conn, 2, pmap, gc, ws->w, ws->h,
173                          0, 0, 0, depth,
174                          (ws->w * ws->h * sizeof(int)), pix);
175            ecore_x_gc_free(gc);
176         }
177    }
178
179    return pmap;
180 }
181
182 /* local functions */
183 static Eina_Bool
184 _e_mod_comp_wl_fd_handle(void *data __UNUSED__, Ecore_Fd_Handler *hdl __UNUSED__)
185 {
186    struct wl_event_loop *loop;
187
188    loop = wl_display_get_event_loop(_wl_disp);
189    wl_event_loop_dispatch(loop, 0);
190    return ECORE_CALLBACK_RENEW;
191 }
192