summaryrefslogtreecommitdiff
path: root/src/bin/e_comp_wl_shell.c
blob: 323e7191ed0ebba0c6eb23f95cbee376e5790e3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#include "e.h"
#ifdef HAVE_WAYLAND_CLIENTS
# include "e_comp_wl.h"
# include "e_comp_wl_comp.h"
# include "e_comp_wl_output.h"
# include "e_comp_wl_input.h"
# include "e_comp_wl_shell.h"
# include "e_comp_wl_surface.h"
#endif

/* local function prototypes */
static void                   _e_comp_wl_shell_bind(struct wl_client *client, void *data, uint32_t version __UNUSED__, uint32_t id);
static void                   _e_comp_wl_shell_lock(Wayland_Shell *base);
static void                   _e_comp_wl_shell_unlock(Wayland_Shell *base);
static void                   _e_comp_wl_shell_map(Wayland_Shell *base, Wayland_Surface *surface, int32_t width, int32_t height);
static void                   _e_comp_wl_shell_configure(Wayland_Shell *base, Wayland_Surface *surface, int32_t x, int32_t y, int32_t width, int32_t height);
static void                   _e_comp_wl_shell_destroy(Wayland_Shell *base);
static void                   _e_comp_wl_shell_activate(Wayland_Shell *base, Wayland_Surface *surface, uint32_t timestamp);

static void                   _e_comp_wl_shell_shell_surface_get(struct wl_client *client, struct wl_resource *resource, uint32_t id, struct wl_resource *surface_resource);
static void                   _e_comp_wl_shell_surface_pong(struct wl_client *client __UNUSED__, struct wl_resource *resource, unsigned int serial);
static void                   _e_comp_wl_shell_surface_move(struct wl_client *client __UNUSED__, struct wl_resource *resource, struct wl_resource *input_resource, uint32_t timestamp);
static void                   _e_comp_wl_shell_surface_resize(struct wl_client *client __UNUSED__, struct wl_resource *resource, struct wl_resource *input_resource, uint32_t timestamp, uint32_t edges);
static void                   _e_comp_wl_shell_surface_set_toplevel(struct wl_client *client __UNUSED__, struct wl_resource *resource);
static void                   _e_comp_wl_shell_surface_set_transient(struct wl_client *client __UNUSED__, struct wl_resource *resource, struct wl_resource *parent_resource, int32_t x, int32_t y, uint32_t flags __UNUSED__);
static void                   _e_comp_wl_shell_surface_set_fullscreen(struct wl_client *client __UNUSED__, struct wl_resource *resource, uint32_t method __UNUSED__, uint32_t framerate __UNUSED__, struct wl_resource *output_resource __UNUSED__);
static void                   _e_comp_wl_shell_surface_set_popup(struct wl_client *client __UNUSED__, struct wl_resource *resource, struct wl_resource *input_resource  __UNUSED__, uint32_t timestamp  __UNUSED__, struct wl_resource *parent_resource, int32_t x, int32_t y, uint32_t flags  __UNUSED__);
static void                   _e_comp_wl_shell_surface_set_maximized(struct wl_client *client, struct wl_resource *resource, struct wl_resource *output_resource __UNUSED__);
static void                   _e_comp_wl_shell_surface_set_title(struct wl_client *client, struct wl_resource *resource, const char *title);
static void                   _e_comp_wl_shell_surface_set_class(struct wl_client *client, struct wl_resource *resource, const char *clas);

static void                   _e_comp_wl_shell_surface_destroy_handle(struct wl_listener *listener, void *data __UNUSED__);
static Wayland_Shell_Surface *_e_comp_wl_shell_get_shell_surface(Wayland_Surface *ws);
static void                   _e_comp_wl_shell_surface_destroy(struct wl_resource *resource);

/* wayland interfaces */
static const struct wl_shell_interface _wl_shell_interface =
{
   _e_comp_wl_shell_shell_surface_get
};
static const struct wl_shell_surface_interface _wl_shell_surface_interface =
{
   _e_comp_wl_shell_surface_pong,
   _e_comp_wl_shell_surface_move,
   _e_comp_wl_shell_surface_resize,
   _e_comp_wl_shell_surface_set_toplevel,
   _e_comp_wl_shell_surface_set_transient,
   _e_comp_wl_shell_surface_set_fullscreen,
   _e_comp_wl_shell_surface_set_popup,
   _e_comp_wl_shell_surface_set_maximized,
   _e_comp_wl_shell_surface_set_title,
   _e_comp_wl_shell_surface_set_class
};

/* private variables */
struct wl_shell *_wl_shell;

Eina_Bool
e_comp_wl_shell_init(void)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   if (!(_wl_shell = malloc(sizeof(*_wl_shell))))
     {
        EINA_LOG_ERR("Could not allocate space for shell\n");
        return EINA_FALSE;
     }

   memset(_wl_shell, 0, sizeof(*_wl_shell));

   _wl_shell->shell.lock = _e_comp_wl_shell_lock;
   _wl_shell->shell.unlock = _e_comp_wl_shell_unlock;
   _wl_shell->shell.map = _e_comp_wl_shell_map;
   _wl_shell->shell.configure = _e_comp_wl_shell_configure;
   _wl_shell->shell.destroy = _e_comp_wl_shell_destroy;

   if (!wl_display_add_global(_wl_disp, &wl_shell_interface, _wl_shell,
                              _e_comp_wl_shell_bind))
     {
        EINA_LOG_ERR("Could not create shell\n");
        free(_wl_shell);
        return EINA_FALSE;
     }

   return EINA_TRUE;
}

void
e_comp_wl_shell_shutdown(void)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   if (_wl_shell)
     _wl_shell->shell.destroy(&_wl_shell->shell);
}

struct wl_shell *
e_comp_wl_shell_get(void)
{
   return _wl_shell;
}

/* local functions */
static void
_e_comp_wl_shell_bind(struct wl_client *client, void *data, uint32_t version __UNUSED__, uint32_t id)
{
   struct wl_shell *shell;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   shell = data;
   wl_client_add_object(client, &wl_shell_interface, &_wl_shell_interface,
                        id, shell);
}

static void
_e_comp_wl_shell_lock(Wayland_Shell *base)
{
   struct wl_shell *shell;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   shell = container_of(base, struct wl_shell, shell);
}

static void
_e_comp_wl_shell_unlock(Wayland_Shell *base)
{
   struct wl_shell *shell;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   shell = container_of(base, struct wl_shell, shell);
}

static void
_e_comp_wl_shell_map(Wayland_Shell *base, Wayland_Surface *surface, int32_t width, int32_t height)
{
   Wayland_Compositor *comp;
   Wayland_Shell_Surface *wss;
   struct wl_shell *shell;
   struct wl_list *list;
   uint32_t type;
   Eina_Bool do_configure = EINA_FALSE;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   comp = e_comp_wl_comp_get();
   type = SHELL_SURFACE_NONE;
   shell = container_of(base, struct wl_shell, shell);

   if ((wss = _e_comp_wl_shell_get_shell_surface(surface)))
     type = wss->type;

   if (shell->locked)
     {
        list = &shell->hidden_surfaces;
        do_configure = EINA_FALSE;
     }
   else
     {
        list = &comp->surfaces;
        do_configure = EINA_TRUE;
     }

   surface->w = width;
   surface->h = height;

   switch (type)
     {
      case SHELL_SURFACE_TOPLEVEL:
        surface->x = 10 + random() % 400;
        surface->y = 10 + random() % 400;
        break;

      default:
        wl_list_insert(list, &surface->link);
        break;
     }

   if (do_configure)
     e_comp_wl_surface_configure(surface, surface->x, surface->y,
                                 surface->w, surface->h);

   switch (type)
     {
      case SHELL_SURFACE_TOPLEVEL:
      case SHELL_SURFACE_TRANSIENT:
      case SHELL_SURFACE_FULLSCREEN:
        if (!shell->locked)
          _e_comp_wl_shell_activate(base, surface, e_comp_wl_time_get());
        break;
     }
}

static void
_e_comp_wl_shell_configure(Wayland_Shell *base, Wayland_Surface *surface, int32_t x, int32_t y, int32_t width, int32_t height)
{
   struct wl_shell *shell;
   Wayland_Shell_Surface *wss;
   Wayland_Shell_Surface_Type type;
   Eina_Bool do_configure = EINA_FALSE;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   type = SHELL_SURFACE_NONE;
   shell = container_of(base, struct wl_shell, shell);
   do_configure = !shell->locked;
   if ((wss = _e_comp_wl_shell_get_shell_surface(surface)))
     type = wss->type;

   surface->w = width;
   surface->h = height;

   switch (type)
     {
      case SHELL_SURFACE_SCREENSAVER:
        do_configure = !do_configure;

      case SHELL_SURFACE_FULLSCREEN:
        break;

      default:
        break;
     }

   if (do_configure)
     e_comp_wl_surface_configure(surface, x, y, width, height);
}

static void
_e_comp_wl_shell_destroy(Wayland_Shell *base)
{
   struct wl_shell *shell;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   shell = container_of(base, struct wl_shell, shell);
   if (shell->child.client) wl_client_destroy(shell->child.client);
   free(shell);
}

static void
_e_comp_wl_shell_activate(Wayland_Shell *base, Wayland_Surface *surface, uint32_t timestamp)
{
   struct wl_shell *shell;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   shell = container_of(base, struct wl_shell, shell);
   e_comp_wl_surface_activate(surface, e_comp_wl_input_get(), timestamp);
}

static void
_e_comp_wl_shell_shell_surface_get(struct wl_client *client, struct wl_resource *resource, uint32_t id, struct wl_resource *surface_resource)
{
   Wayland_Surface *ws;
   Wayland_Shell_Surface *wss;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   ws = surface_resource->data;
   if (_e_comp_wl_shell_get_shell_surface(ws))
     {
        wl_resource_post_error(surface_resource,
                               WL_DISPLAY_ERROR_INVALID_OBJECT,
                               "get_shell_surface already requested");
        return;
     }

   if (!(wss = calloc(1, sizeof(*wss))))
     {
        wl_resource_post_no_memory(resource);
        return;
     }

   wss->resource.destroy = _e_comp_wl_shell_surface_destroy;
   wss->resource.object.id = id;
   wss->resource.object.interface = &wl_shell_surface_interface;
   wss->resource.object.implementation =
     (void (* *)(void)) & _wl_shell_surface_interface;
   wss->resource.data = wss;

   wss->surface = ws;
   wss->surface_destroy_listener.notify =
     _e_comp_wl_shell_surface_destroy_handle;
   wl_signal_add(&ws->surface.resource.destroy_signal,
                 &wss->surface_destroy_listener);

   wl_list_init(&wss->link);

   wss->type = SHELL_SURFACE_NONE;
   wl_client_add_resource(client, &wss->resource);
}

static void
_e_comp_wl_shell_surface_pong(struct wl_client *client __UNUSED__, struct wl_resource *resource, unsigned int serial)
{
   Wayland_Shell_Surface *wss;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   wss = resource->data;
   /* TODO: handle ping timer */
}

static void
_e_comp_wl_shell_surface_move(struct wl_client *client __UNUSED__, struct wl_resource *resource, struct wl_resource *input_resource, uint32_t timestamp)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);
}

static void
_e_comp_wl_shell_surface_resize(struct wl_client *client __UNUSED__, struct wl_resource *resource, struct wl_resource *input_resource, uint32_t timestamp, uint32_t edges)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);
}

static void
_e_comp_wl_shell_surface_set_toplevel(struct wl_client *client __UNUSED__, struct wl_resource *resource)
{
   Wayland_Shell_Surface *wss;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   wss = resource->data;

   /* TODO: Surface type reset */

   e_comp_wl_surface_damage_surface(wss->surface);
   wss->type = SHELL_SURFACE_TOPLEVEL;
}

static void
_e_comp_wl_shell_surface_set_transient(struct wl_client *client __UNUSED__, struct wl_resource *resource, struct wl_resource *parent_resource, int32_t x, int32_t y, uint32_t flags __UNUSED__)
{
   Wayland_Shell_Surface *wss, *pss;
   Wayland_Surface *ws, *pws;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   wss = resource->data;
   pss = parent_resource->data;
   ws = wss->surface;
   pws = pss->surface;

   /* TODO: Surface type reset */

   ws->x = pws->x + x;
   ws->y = pws->y + y;

   e_comp_wl_surface_damage_surface(ws);

   wss->type = SHELL_SURFACE_TRANSIENT;
}

static void
_e_comp_wl_shell_surface_set_fullscreen(struct wl_client *client __UNUSED__, struct wl_resource *resource, uint32_t method __UNUSED__, uint32_t framerate __UNUSED__, struct wl_resource *output_resource __UNUSED__)
{
   Wayland_Shell_Surface *wss;
   Wayland_Surface *ws;
   Wayland_Output *output;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   wss = resource->data;
   ws = wss->surface;
   output = e_comp_wl_output_get();

   wss->saved_x = ws->x;
   wss->saved_y = ws->y;
   ws->x = (output->w - ws->w) / 2;
   ws->y = (output->h - ws->h) / 2;
   e_comp_wl_surface_damage_surface(ws);
   wss->type = SHELL_SURFACE_FULLSCREEN;
}

static void
_e_comp_wl_shell_surface_set_popup(struct wl_client *client __UNUSED__, struct wl_resource *resource, struct wl_resource *input_resource  __UNUSED__, uint32_t timestamp  __UNUSED__, struct wl_resource *parent_resource, int32_t x, int32_t y, uint32_t flags  __UNUSED__)
{
   Wayland_Shell_Surface *wss;
   Wayland_Surface *ws;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   wss = resource->data;
   ws = wss->surface;
   e_comp_wl_surface_damage_surface(ws);
   wss->type = SHELL_SURFACE_POPUP;
   wss->parent = parent_resource->data;
   wss->popup.x = x;
   wss->popup.y = y;
}

static void
_e_comp_wl_shell_surface_set_maximized(struct wl_client *client __UNUSED__, struct wl_resource *resource, struct wl_resource *output_resource __UNUSED__)
{
   Wayland_Shell_Surface *wss;
   Wayland_Surface *ws;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   wss = resource->data;
   ws = wss->surface;
   /* FIXME: Implement */
}

static void
_e_comp_wl_shell_surface_set_title(struct wl_client *client, struct wl_resource *resource, const char *title)
{
   Wayland_Shell_Surface *wss;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   wss = resource->data;
   free(wss->title);
   wss->title = strdup(title);
}

static void
_e_comp_wl_shell_surface_set_class(struct wl_client *client, struct wl_resource *resource, const char *clas)
{
   Wayland_Shell_Surface *wss;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   wss = resource->data;
   free(wss->clas);
   wss->clas = strdup(clas);
}

static void
_e_comp_wl_shell_surface_destroy_handle(struct wl_listener *listener, void *data __UNUSED__)
{
   Wayland_Shell_Surface *wss;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   wss = container_of(listener, Wayland_Shell_Surface, surface_destroy_listener);
   wss->surface = NULL;
   wl_resource_destroy(&wss->resource);
}

static Wayland_Shell_Surface *
_e_comp_wl_shell_get_shell_surface(Wayland_Surface *ws)
{
   struct wl_listener *listener;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   listener =
     wl_signal_get(&ws->surface.resource.destroy_signal,
                   _e_comp_wl_shell_surface_destroy_handle);
   if (listener)
     return container_of(listener, Wayland_Shell_Surface,
                         surface_destroy_listener);
   return NULL;
}

static void
_e_comp_wl_shell_surface_destroy(struct wl_resource *resource)
{
   Wayland_Shell_Surface *wss;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   wss = resource->data;

   /* TODO: popup grab input */

   if (wss->surface) wl_list_remove(&wss->surface_destroy_listener.link);
   wl_list_remove(&wss->link);
   free(wss);
}