add stamp to vblank objects
[platform/core/uifw/libtdm.git] / src / tdm_server.c
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
7  * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8  *          JinYoung Jeon <jy0.jeon@samsung.com>,
9  *          Taeheon Kim <th908.kim@samsung.com>,
10  *          YoungJun Cho <yj44.cho@samsung.com>,
11  *          SooChan Lim <sc1.lim@samsung.com>,
12  *          Boram Park <sc1.lim@samsung.com>
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a
15  * copy of this software and associated documentation files (the
16  * "Software"), to deal in the Software without restriction, including
17  * without limitation the rights to use, copy, modify, merge, publish,
18  * distribute, sub license, and/or sell copies of the Software, and to
19  * permit persons to whom the Software is furnished to do so, subject to
20  * the following conditions:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33  *
34 **************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include "tdm.h"
41 #include "tdm_private.h"
42 #include "tdm_list.h"
43 #include "tdm-server-protocol.h"
44
45 /* CAUTION:
46  * - tdm server doesn't care about thread things.
47  * - DO NOT use the TDM internal functions here.
48  *     However, the internal function which does lock/unlock the mutex of
49  *     private_display in itself can be called.
50  * - DO NOT use the tdm_private_display structure here.
51  * - The callback function things can be called in main thread.
52  */
53
54 struct _tdm_private_server {
55         tdm_private_loop *private_loop;
56         struct list_head output_list;
57         struct list_head wait_list;
58 };
59
60 typedef struct _tdm_server_output_info {
61         struct list_head link;
62         tdm_private_server *private_server;
63         struct wl_resource *resource;
64         tdm_output *output;
65         struct list_head vblank_list;
66         unsigned int watch_output_changes;
67 } tdm_server_output_info;
68
69 typedef struct _tdm_server_vblank_info {
70         struct list_head link;
71         tdm_server_output_info *output_info;
72         struct wl_resource *resource;
73
74         tdm_vblank *vblank;
75         unsigned int stamp;
76 } tdm_server_vblank_info;
77
78 typedef struct _tdm_server_wait_info {
79         struct list_head link;
80         tdm_server_vblank_info *vblank_info;
81
82         unsigned int req_id;
83 } tdm_server_wait_info;
84
85 typedef struct _tdm_server_client_info {
86         struct list_head link;
87         pid_t pid;
88         char name[TDM_NAME_LEN];
89 } tdm_server_client_info;
90
91 static tdm_private_server *keep_private_server;
92 static struct list_head client_list;
93
94 static void destroy_wait(tdm_server_wait_info *wait_info);
95
96 static void
97 _tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
98 {
99         char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
100         FILE *h;
101         size_t len;
102
103         snprintf(name, size, "Unknown");
104
105         snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
106         h = fopen(proc, "r");
107         if (!h)
108                 return;
109
110         len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
111         if (len == 0) {
112                 char *p = strncpy(pname, "NO NAME", sizeof(pname) - 1);
113                 len = p - pname;
114         }
115         pname[len - 1] = '\0';
116
117         strncpy(name, pname, size - 1);
118         name[size - 1] = '\0';
119
120         fclose(h);
121 }
122
123 static tdm_output*
124 _tdm_server_find_output(tdm_private_server *private_server, const char *name)
125 {
126         tdm_private_loop *private_loop = private_server->private_loop;
127         tdm_output *found = NULL;
128
129         if (!strncasecmp(name, "primary", 7) || !strncasecmp(name, "default", 7))
130                 found = tdm_display_get_output(private_loop->dpy, 0, NULL);
131
132         if (!found) {
133                 int count = 0, i;
134
135                 tdm_display_get_output_count(private_loop->dpy, &count);
136
137                 for (i = 0; i < count; i++) {
138                         tdm_output *output = tdm_display_get_output(private_loop->dpy, i, NULL);
139                         tdm_output_conn_status status;
140                         const char *model = NULL;
141                         tdm_error ret;
142
143                         ret = tdm_output_get_conn_status(output, &status);
144                         if (ret || status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
145                                 continue;
146
147                         ret = tdm_output_get_model_info(output, NULL, &model, NULL);
148                         if (ret || !model)
149                                 continue;
150
151                         if (strncmp(model, name, TDM_NAME_LEN))
152                                 continue;
153
154                         found = output;
155                         break;
156                 }
157         }
158
159         return found;
160 }
161
162 static void
163 _tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error,
164                                           unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec)
165 {
166         tdm_server_wait_info *found;
167         tdm_server_vblank_info *vblank_info;
168
169         if (!keep_private_server)
170                 return;
171
172         LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list,
173                                    tdm_server_wait_info, link, found);
174         if (!found) {
175                 TDM_DBG("wait_info(%p) is destroyed", wait_info);
176                 return;
177         }
178
179         if (tdm_debug_module & TDM_DEBUG_VBLANK)
180                 TDM_DBG("req_id(%d) done", wait_info->req_id);
181
182         TDM_TRACE_COUNT(ServerDoneVBlank, wait_info->req_id);
183
184         vblank_info = wait_info->vblank_info;
185         wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id,
186                                                         sequence, tv_sec, tv_usec, error);
187         destroy_wait(wait_info);
188 }
189
190 static void
191 _tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
192                                           unsigned int tv_sec, unsigned int tv_usec, void *user_data)
193 {
194         _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
195 }
196
197 static void
198 _tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
199                                                          tdm_value value, void *user_data)
200 {
201         tdm_server_output_info *output_info = user_data;
202         struct wl_client *client;
203         pid_t pid = 0;
204
205         TDM_RETURN_IF_FAIL(output_info != NULL);
206
207         client = wl_resource_get_client(output_info->resource);
208         TDM_RETURN_IF_FAIL(client != NULL);
209
210         wl_client_get_credentials(client, &pid, NULL, NULL);
211
212         if (!output_info->watch_output_changes) {
213                 TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid);
214                 return;
215         }
216
217         TDM_DBG("send the output changes: %d", (unsigned int)pid);
218
219         switch (type) {
220         case TDM_OUTPUT_CHANGE_DPMS:
221                 wl_tdm_output_send_dpms(output_info->resource, value.u32, TDM_ERROR_NONE);
222                 break;
223         case TDM_OUTPUT_CHANGE_CONNECTION:
224                 wl_tdm_output_send_connection(output_info->resource, value.u32, TDM_ERROR_NONE);
225                 break;
226         default:
227                 break;
228         }
229 }
230
231 static void
232 destroy_wait(tdm_server_wait_info *wait_info)
233 {
234         LIST_DEL(&wait_info->link);
235         free(wait_info);
236 }
237
238 static void
239 destroy_vblank_callback(struct wl_resource *resource)
240 {
241         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
242         tdm_server_wait_info *w = NULL, *ww = NULL;
243
244         TDM_RETURN_IF_FAIL(vblank_info != NULL);
245
246         LIST_DEL(&vblank_info->link);
247
248         if (vblank_info->vblank)
249                 tdm_vblank_destroy(vblank_info->vblank);
250
251         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
252                 if (w->vblank_info == vblank_info)
253                         destroy_wait(w);
254         }
255
256         free(vblank_info);
257 }
258
259 static void
260 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
261 {
262         wl_resource_destroy(resource);
263 }
264
265 static void
266 _tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
267 {
268         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
269
270         tdm_vblank_set_name(vblank_info->vblank, name);
271 }
272
273 static void
274 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
275 {
276         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
277
278         tdm_vblank_set_fps(vblank_info->vblank, fps);
279 }
280
281 static void
282 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
283 {
284         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
285
286         tdm_vblank_set_offset(vblank_info->vblank, offset);
287 }
288
289 static void
290 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
291 {
292         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
293
294         tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
295 }
296
297 static void
298 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
299                                                                   uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
300 {
301         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
302         tdm_server_output_info *output_info = vblank_info->output_info;
303         tdm_private_server *private_server = output_info->private_server;
304         tdm_server_wait_info *wait_info;
305         unsigned int enable_fake = 0;
306         tdm_error ret;
307
308         TDM_TRACE_COUNT(ServerWaitVBlank, req_id);
309
310         wait_info = calloc(1, sizeof * wait_info);
311         if (!wait_info) {
312                 TDM_ERR("alloc failed");
313                 ret = TDM_ERROR_OUT_OF_MEMORY;
314                 goto wait_failed;
315         }
316
317         LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
318         wait_info->vblank_info = vblank_info;
319         wait_info->req_id = req_id;
320
321         if (tdm_debug_module & TDM_DEBUG_VBLANK)
322                 TDM_DBG("req_id(%d) wait", req_id);
323
324         ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
325
326         tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
327         if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
328                 goto wait_failed;
329
330         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
331
332         return;
333 wait_failed:
334         wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
335         if (wait_info)
336                 destroy_wait(wait_info);
337 }
338
339 static void
340 _tdm_server_vblank_cb_wait_vblank_seq(struct wl_client *client, struct wl_resource *resource,
341                                                                           uint32_t sequence, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
342 {
343         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
344         tdm_server_output_info *output_info = vblank_info->output_info;
345         tdm_private_server *private_server = output_info->private_server;
346         tdm_server_wait_info *wait_info;
347         unsigned int enable_fake = 0;
348         tdm_error ret;
349
350         TDM_TRACE_COUNT(ServerWaitVBlank, req_id);
351
352         wait_info = calloc(1, sizeof * wait_info);
353         if (!wait_info) {
354                 TDM_ERR("alloc failed");
355                 ret = TDM_ERROR_OUT_OF_MEMORY;
356                 goto wait_failed;
357         }
358
359         LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
360         wait_info->vblank_info = vblank_info;
361         wait_info->req_id = req_id;
362
363         if (tdm_debug_module & TDM_DEBUG_VBLANK)
364                 TDM_DBG("req_id(%d) wait", req_id);
365
366         ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, sequence, _tdm_server_cb_vblank, wait_info);
367
368         tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
369         if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
370                 goto wait_failed;
371
372         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
373
374         return;
375 wait_failed:
376         wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
377         if (wait_info)
378                 destroy_wait(wait_info);
379 }
380
381 static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
382         _tdm_server_vblank_cb_destroy,
383         _tdm_server_vblank_cb_set_name,
384         _tdm_server_vblank_cb_set_fps,
385         _tdm_server_vblank_cb_set_offset,
386         _tdm_server_vblank_cb_set_enable_fake,
387         _tdm_server_vblank_cb_wait_vblank,
388         _tdm_server_vblank_cb_wait_vblank_seq,
389 };
390
391 static void
392 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
393 {
394         wl_resource_destroy(resource);
395 }
396
397 static void
398 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
399 {
400         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
401         tdm_private_server *private_server = output_info->private_server;
402         tdm_private_loop *private_loop = private_server->private_loop;
403         struct wl_resource *vblank_resource;
404         tdm_vblank *vblank;
405         tdm_server_vblank_info *vblank_info;
406
407         vblank_resource =
408                 wl_resource_create(client, &wl_tdm_vblank_interface,
409                                                    wl_resource_get_version(resource), id);
410         if (!vblank_resource) {
411                 wl_resource_post_no_memory(resource);
412                 TDM_ERR("wl_resource_create failed");
413                 return;
414         }
415
416         vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
417         if (!vblank) {
418                 wl_resource_post_no_memory(resource);
419                 wl_resource_destroy(vblank_resource);
420                 TDM_ERR("tdm_vblank_create failed");
421                 return;
422         }
423
424         vblank_info = calloc(1, sizeof * vblank_info);
425         if (!vblank_info) {
426                 wl_resource_post_no_memory(resource);
427                 wl_resource_destroy(vblank_resource);
428                 tdm_vblank_destroy(vblank);
429                 TDM_ERR("alloc failed");
430                 return;
431         }
432
433         LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
434         vblank_info->output_info = output_info;
435         vblank_info->resource = vblank_resource;
436         vblank_info->vblank = vblank;
437         vblank_info->stamp = (unsigned int)tdm_vblank_get_stamp(vblank);
438
439         tdm_vblank_set_resource(vblank, vblank_resource);
440
441         wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
442                                                                    vblank_info, destroy_vblank_callback);
443
444         wl_tdm_vblank_send_stamp(vblank_info->resource, vblank_info->stamp);
445
446         return;
447 }
448
449 static void
450 _tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
451 {
452         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
453
454         TDM_RETURN_IF_FAIL(output_info != NULL);
455
456         output_info->watch_output_changes = enable;
457 }
458
459 static void
460 _tdm_server_output_cb_get_connection(struct wl_client *client, struct wl_resource *resource)
461 {
462         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
463         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
464         tdm_error ret;
465
466         TDM_RETURN_IF_FAIL(output_info != NULL);
467
468         ret = tdm_output_get_conn_status(output_info->output, &status);
469         TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
470
471         wl_tdm_output_send_connection(output_info->resource, status, ret);
472
473         return;
474
475 failed:
476         wl_tdm_output_send_connection(output_info->resource, TDM_OUTPUT_CONN_STATUS_DISCONNECTED, ret);
477 }
478
479 static void
480 _tdm_server_output_cb_get_mode(struct wl_client *client, struct wl_resource *resource)
481 {
482         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
483         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
484         tdm_error ret;
485
486         TDM_RETURN_IF_FAIL(output_info != NULL);
487
488         ret = tdm_output_get_conn_status(output_info->output, &status);
489         TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
490
491         if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
492                 const tdm_output_mode *mode = NULL;
493                 unsigned int hdisplay, vdisplay, vrefresh;
494
495                 ret = tdm_output_get_mode(output_info->output, &mode);
496                 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
497
498                 hdisplay = (mode) ? mode->hdisplay : 0;
499                 vdisplay = (mode) ? mode->vdisplay : 0;
500                 vrefresh = (mode) ? mode->vrefresh : 0;
501
502                 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
503         } else {
504                 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
505         }
506
507         return;
508 failed:
509         wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
510 }
511
512 static void
513 _tdm_server_output_cb_get_dpms(struct wl_client *client, struct wl_resource *resource)
514 {
515         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
516         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
517         tdm_error ret;
518
519         TDM_RETURN_IF_FAIL(output_info != NULL);
520
521         ret = tdm_output_get_conn_status(output_info->output, &status);
522         TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
523
524         if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
525                 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
526
527                 ret = tdm_output_get_dpms(output_info->output, &dpms_value);
528                 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
529
530                 wl_tdm_output_send_dpms(output_info->resource, dpms_value, ret);
531         } else {
532                 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
533         }
534
535         return;
536 failed:
537         wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, ret);
538 }
539
540 static const struct wl_tdm_output_interface tdm_output_implementation = {
541         _tdm_server_output_cb_destroy,
542         _tdm_server_output_cb_create_vblank,
543         _tdm_server_output_cb_watch_output_changes,
544         _tdm_server_output_cb_get_connection,
545         _tdm_server_output_cb_get_mode,
546         _tdm_server_output_cb_get_dpms,
547 };
548
549 static void
550 destroy_output_callback(struct wl_resource *resource)
551 {
552         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
553         tdm_server_vblank_info *v = NULL, *vv = NULL;
554
555         TDM_RETURN_IF_FAIL(output_info != NULL);
556
557         LIST_DEL(&output_info->link);
558
559         tdm_output_remove_change_handler(output_info->output,
560                                                                          _tdm_server_cb_output_change, output_info);
561
562         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
563                 wl_resource_destroy(v->resource);
564         }
565
566         free(output_info);
567 }
568
569 static void
570 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
571                                                          const char *name, uint32_t id)
572 {
573         tdm_private_server *private_server = wl_resource_get_user_data(resource);
574         tdm_server_output_info *output_info;
575         struct wl_resource *output_resource = NULL;
576         tdm_output *output;
577         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
578         tdm_error ret;
579
580         output = _tdm_server_find_output(private_server, name);
581         if (!output) {
582                 TDM_ERR("There is no '%s' output", name);
583                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
584                                                            "There is no '%s' output", name);
585                 return;
586         }
587
588         output_resource =
589                 wl_resource_create(client, &wl_tdm_output_interface,
590                                                    wl_resource_get_version(resource), id);
591         if (!output_resource) {
592                 wl_resource_post_no_memory(resource);
593                 TDM_ERR("wl_resource_create failed");
594                 return;
595         }
596
597         output_info = calloc(1, sizeof * output_info);
598         if (!output_info) {
599                 wl_resource_post_no_memory(resource);
600                 wl_resource_destroy(output_resource);
601                 TDM_ERR("alloc failed");
602                 return;
603         }
604
605         LIST_ADDTAIL(&output_info->link, &private_server->output_list);
606         output_info->private_server = private_server;
607         output_info->resource = output_resource;
608         output_info->output = output;
609         LIST_INITHEAD(&output_info->vblank_list);
610
611         tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
612
613         wl_resource_set_implementation(output_resource, &tdm_output_implementation,
614                                                                    output_info, destroy_output_callback);
615
616         ret = tdm_output_get_conn_status(output, &status);
617         wl_tdm_output_send_connection(output_resource, status, ret);
618
619         if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
620                 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
621                 const tdm_output_mode *mode = NULL;
622                 unsigned int hdisplay, vdisplay, vrefresh;
623
624                 ret = tdm_output_get_mode(output, &mode);
625                 hdisplay = (mode) ? mode->hdisplay : 0;
626                 vdisplay = (mode) ? mode->vdisplay : 0;
627                 vrefresh = (mode) ? mode->vrefresh : 0;
628                 wl_tdm_output_send_mode(output_resource, hdisplay, vdisplay, vrefresh, ret);
629
630                 ret = tdm_output_get_dpms(output, &dpms_value);
631                 wl_tdm_output_send_dpms(output_resource, dpms_value, ret);
632         } else {
633                 wl_tdm_output_send_mode(output_resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
634                 wl_tdm_output_send_dpms(output_resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
635         }
636 }
637
638 static void
639 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
640 {
641         tdm_private_server *private_server = wl_resource_get_user_data(resource);
642         tdm_private_loop *private_loop = private_server->private_loop;
643         char message[TDM_SERVER_REPLY_MSG_LEN];
644         char *m;
645         int len = sizeof(message), size;
646         uid_t uid;
647
648         wl_client_get_credentials(client, NULL, &uid, NULL);
649
650         if (uid != 0) {
651                 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
652                 TDM_ERR("%s", message);
653         } else {
654                 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
655         }
656
657         size = sizeof(message) - len;
658         m = message;
659
660         wl_client_flush(client);
661
662         while (size > 0) {
663                 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
664                 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
665
666                 strncpy(buffer, m, copylen);
667                 m += copylen;
668                 size -= copylen;
669
670                 buffer[copylen] = '\0';
671
672                 wl_tdm_send_debug_message(resource, buffer);
673         }
674
675         wl_tdm_send_debug_done(resource);
676 }
677
678 static void
679 _tdm_server_cb_set_client_vblank_fps(struct wl_client *client, struct wl_resource *resource,
680                                                                          unsigned int pid, const char *name, unsigned int fps)
681 {
682         tdm_error ret = tdm_vblank_set_client_vblank_fps(pid, name, fps);
683         TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
684
685         TDM_INFO("'%s' vblank fps(PID: '%u'): %u", name, pid, fps);
686 }
687
688 static const struct wl_tdm_interface tdm_implementation = {
689         _tdm_server_cb_debug,
690         _tdm_server_cb_create_output,
691         _tdm_server_cb_set_client_vblank_fps,
692 };
693
694 static void
695 destroy_client(struct wl_resource *resource)
696 {
697         tdm_server_client_info *c = NULL, *cc = NULL;
698         struct wl_client *client;
699         pid_t pid = -1;
700
701         client = wl_resource_get_client(resource);
702         TDM_RETURN_IF_FAIL(client != NULL);
703
704         wl_client_get_credentials(client, &pid, NULL, NULL);
705
706         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
707                 if (c->pid == pid) {
708                         LIST_DEL(&c->link);
709                         free(c);
710                         return;
711                 }
712         }
713 }
714
715 static void
716 _tdm_server_bind(struct wl_client *client, void *data,
717                                  uint32_t version, uint32_t id)
718 {
719         struct wl_resource *resource;
720         tdm_server_client_info *cinfo;
721
722         resource = wl_resource_create(client, &wl_tdm_interface, version, id);
723         if (!resource) {
724                 wl_client_post_no_memory(client);
725                 return;
726         }
727
728         cinfo = calloc(1, sizeof(tdm_server_client_info));
729         if (!cinfo) {
730                 wl_client_post_no_memory(client);
731                 wl_resource_destroy(resource);
732                 return;
733         }
734
735         LIST_ADDTAIL(&cinfo->link, &client_list);
736         wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
737         _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
738
739         wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
740 }
741
742 static int
743 _tdm_getgrnam_r(const char *name)
744 {
745         struct group *grp = NULL;
746         struct group *grp_res = NULL;
747         char* buf = NULL;
748         size_t buf_len;
749         int ret;
750         int id;
751
752         buf_len = sysconf(_SC_GETGR_R_SIZE_MAX);
753         if (buf_len == -1)
754                 buf_len = 2048;
755
756         buf = calloc(1, buf_len * sizeof(char));
757         if (!buf) {
758                 TDM_ERR("creating buffer failed");
759                 goto failed;
760         }
761
762         grp = calloc(1, sizeof(struct group));
763         if (!grp) {
764                 TDM_ERR("creating group failed");
765                 goto failed;
766         }
767
768         ret = getgrnam_r(name, grp, buf, buf_len, &grp_res);
769         if (ret < 0) {
770                 TDM_ERR("getgrnam_r failed errno:%d(%m)", ret);
771                 goto failed;
772         }
773
774         if (grp_res == NULL) {
775                 TDM_ERR("finding name:%s group failed", name);
776                 goto failed;
777         }
778
779         id = grp->gr_gid;
780         free(buf);
781         free(grp);
782
783         return id;
784
785 failed:
786         if (buf)
787                 free(buf);
788         if (grp)
789                 free(grp);
790
791         return -1;
792 }
793
794 static void
795 _tdm_socket_init(tdm_private_loop *private_loop)
796 {
797         const char *dir = NULL;
798         char socket_path[TDM_NAME_LEN * 2];
799         int ret = -1;
800         uid_t uid;
801         gid_t gid;
802
803         dir = getenv("XDG_RUNTIME_DIR");
804         if (!dir) {
805                 TDM_WRN("getting XDG_RUNTIME_DIR failed");
806                 return;
807         }
808
809         strncpy(socket_path, dir, TDM_NAME_LEN - 1);
810         socket_path[TDM_NAME_LEN - 1] = '\0';
811
812         strncat(socket_path, "/tdm-socket", 11);
813         socket_path[TDM_NAME_LEN + 10] = '\0';
814
815         ret = chmod(socket_path, 509);
816         if (ret < 0) {
817                 TDM_WRN("changing modes of socket file failed:%s (%m)", socket_path);
818                 return;
819         }
820
821         ret = _tdm_getgrnam_r("root");
822         if (ret < 0) {
823                 TDM_WRN("getting uid failed");
824                 return;
825         }
826         uid = ret;
827
828         ret = _tdm_getgrnam_r("display");
829         if (ret < 0) {
830                 TDM_WRN("getting gid failed");
831                 return;
832         }
833         gid = ret;
834
835         ret = chown(socket_path, uid, gid);
836         if (ret < 0) {
837                 TDM_WRN("changing owner of socket file failed:%s (%m)", socket_path);
838                 return;
839         }
840 }
841
842 INTERN tdm_error
843 tdm_server_init(tdm_private_loop *private_loop)
844 {
845         tdm_private_server *private_server;
846
847         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
848         TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
849
850         if (private_loop->private_server)
851                 return TDM_ERROR_NONE;
852
853         if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
854                 TDM_ERR("createing a tdm-socket failed");
855                 return TDM_ERROR_OPERATION_FAILED;
856         }
857
858         _tdm_socket_init(private_loop);
859
860         private_server = calloc(1, sizeof * private_server);
861         if (!private_server) {
862                 TDM_ERR("alloc failed");
863                 return TDM_ERROR_OUT_OF_MEMORY;
864         }
865
866         LIST_INITHEAD(&private_server->output_list);
867         LIST_INITHEAD(&private_server->wait_list);
868
869         if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
870                                                   private_server, _tdm_server_bind)) {
871                 TDM_ERR("creating a global resource failed");
872                 free(private_server);
873                 return TDM_ERROR_OUT_OF_MEMORY;
874         }
875
876         private_server->private_loop = private_loop;
877         private_loop->private_server = private_server;
878         keep_private_server = private_server;
879
880         LIST_INITHEAD(&client_list);
881
882         return TDM_ERROR_NONE;
883 }
884
885 INTERN void
886 tdm_server_deinit(tdm_private_loop *private_loop)
887 {
888         tdm_server_output_info *o = NULL, *oo = NULL;
889         tdm_server_wait_info *w = NULL, *ww = NULL;
890         tdm_server_client_info *c = NULL, *cc = NULL;
891         tdm_private_server *private_server;
892
893         if (!private_loop->private_server)
894                 return;
895
896         private_server = private_loop->private_server;
897
898         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
899                 destroy_wait(w);
900         }
901
902         LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
903                 wl_resource_destroy(o->resource);
904         }
905
906         free(private_server);
907         private_loop->private_server = NULL;
908         keep_private_server = NULL;
909
910         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
911                 LIST_DEL(&c->link);
912                 free(c);
913         }
914 }
915
916 INTERN const char*
917 tdm_server_get_client_name(pid_t pid)
918 {
919         tdm_server_client_info *c = NULL;
920
921         LIST_FOR_EACH_ENTRY(c, &client_list, link) {
922                 if (c->pid == pid)
923                         return (const char*)c->name;
924         }
925
926         return NULL;
927 }