correct ttrace debug information for vblank
[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         double req_time;
84 } tdm_server_wait_info;
85
86 typedef struct _tdm_server_client_info {
87         struct list_head link;
88         pid_t pid;
89         char name[TDM_NAME_LEN];
90 } tdm_server_client_info;
91
92 static tdm_private_server *keep_private_server;
93 static struct list_head client_list;
94
95 static void destroy_wait(tdm_server_wait_info *wait_info);
96
97 static void
98 _tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
99 {
100         char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
101         FILE *h;
102         size_t len;
103
104         snprintf(name, size, "Unknown");
105
106         snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
107         h = fopen(proc, "r");
108         if (!h)
109                 return;
110
111         len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
112         if (len == 0) {
113                 char *p = strncpy(pname, "NO NAME", sizeof(pname) - 1);
114                 len = p - pname;
115         }
116         pname[len - 1] = '\0';
117
118         strncpy(name, pname, size - 1);
119         name[size - 1] = '\0';
120
121         fclose(h);
122 }
123
124 static tdm_output*
125 _tdm_server_find_output(tdm_private_server *private_server, const char *name)
126 {
127         tdm_private_loop *private_loop = private_server->private_loop;
128         tdm_output *found = NULL;
129
130         if (!strncasecmp(name, "primary", 7) || !strncasecmp(name, "default", 7))
131                 found = tdm_display_get_output(private_loop->dpy, 0, NULL);
132
133         if (!found) {
134                 int count = 0, i;
135
136                 tdm_display_get_output_count(private_loop->dpy, &count);
137
138                 for (i = 0; i < count; i++) {
139                         tdm_output *output = tdm_display_get_output(private_loop->dpy, i, NULL);
140                         tdm_output_conn_status status;
141                         const char *model = NULL;
142                         tdm_error ret;
143
144                         ret = tdm_output_get_conn_status(output, &status);
145                         if (ret || status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
146                                 continue;
147
148                         ret = tdm_output_get_model_info(output, NULL, &model, NULL);
149                         if (ret || !model)
150                                 continue;
151
152                         if (strncmp(model, name, TDM_NAME_LEN))
153                                 continue;
154
155                         found = output;
156                         break;
157                 }
158         }
159
160         return found;
161 }
162
163 static void
164 _tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error,
165                                           unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec)
166 {
167         tdm_server_wait_info *found;
168         tdm_server_vblank_info *vblank_info;
169
170         if (!keep_private_server)
171                 return;
172
173         LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list,
174                                    tdm_server_wait_info, link, found);
175         if (!found) {
176                 TDM_DBG("wait_info(%p) is destroyed", wait_info);
177                 return;
178         }
179
180         if (tdm_debug_module & TDM_DEBUG_VBLANK)
181                 TDM_DBG("req_id(%d) done", wait_info->req_id);
182
183         vblank_info = wait_info->vblank_info;
184         wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id,
185                                                         sequence, tv_sec, tv_usec, error);
186
187         TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
188
189         destroy_wait(wait_info);
190 }
191
192 static void
193 _tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
194                                           unsigned int tv_sec, unsigned int tv_usec, void *user_data)
195 {
196         _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
197 }
198
199 static void
200 _tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
201                                                          tdm_value value, void *user_data)
202 {
203         tdm_server_output_info *output_info = user_data;
204         struct wl_client *client;
205         pid_t pid = 0;
206
207         TDM_RETURN_IF_FAIL(output_info != NULL);
208
209         client = wl_resource_get_client(output_info->resource);
210         TDM_RETURN_IF_FAIL(client != NULL);
211
212         wl_client_get_credentials(client, &pid, NULL, NULL);
213
214         if (!output_info->watch_output_changes) {
215                 TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid);
216                 return;
217         }
218
219         TDM_DBG("send the output changes: %d", (unsigned int)pid);
220
221         switch (type) {
222         case TDM_OUTPUT_CHANGE_DPMS:
223                 wl_tdm_output_send_dpms(output_info->resource, value.u32, TDM_ERROR_NONE);
224                 break;
225         case TDM_OUTPUT_CHANGE_CONNECTION:
226                 wl_tdm_output_send_connection(output_info->resource, value.u32, TDM_ERROR_NONE);
227                 break;
228         default:
229                 break;
230         }
231 }
232
233 static void
234 destroy_wait(tdm_server_wait_info *wait_info)
235 {
236         LIST_DEL(&wait_info->link);
237         free(wait_info);
238 }
239
240 static void
241 destroy_vblank_callback(struct wl_resource *resource)
242 {
243         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
244         tdm_server_wait_info *w = NULL, *ww = NULL;
245
246         TDM_RETURN_IF_FAIL(vblank_info != NULL);
247
248         LIST_DEL(&vblank_info->link);
249
250         if (vblank_info->vblank)
251                 tdm_vblank_destroy(vblank_info->vblank);
252
253         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
254                 if (w->vblank_info == vblank_info)
255                         destroy_wait(w);
256         }
257
258         free(vblank_info);
259 }
260
261 static void
262 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
263 {
264         wl_resource_destroy(resource);
265 }
266
267 static void
268 _tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
269 {
270         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
271
272         tdm_vblank_set_name(vblank_info->vblank, name);
273 }
274
275 static void
276 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
277 {
278         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
279
280         tdm_vblank_set_fps(vblank_info->vblank, fps);
281 }
282
283 static void
284 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
285 {
286         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
287
288         tdm_vblank_set_offset(vblank_info->vblank, offset);
289 }
290
291 static void
292 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
293 {
294         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
295
296         tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
297 }
298
299 static void
300 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
301                                                                   uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
302 {
303         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
304         tdm_server_output_info *output_info = vblank_info->output_info;
305         tdm_private_server *private_server = output_info->private_server;
306         tdm_server_wait_info *wait_info;
307         unsigned int enable_fake = 0;
308         tdm_error ret;
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         wait_info->req_time = TDM_TIME(req_sec, req_usec);
321
322         if (tdm_debug_module & TDM_DEBUG_VBLANK)
323                 TDM_DBG("req_id(%d) wait", req_id);
324
325         TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
326
327         ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
328
329         tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
330         if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
331                 goto wait_failed;
332
333         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
334
335         return;
336 wait_failed:
337         wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
338         if (wait_info)
339                 destroy_wait(wait_info);
340 }
341
342 static void
343 _tdm_server_vblank_cb_wait_vblank_seq(struct wl_client *client, struct wl_resource *resource,
344                                                                           uint32_t sequence, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
345 {
346         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
347         tdm_server_output_info *output_info = vblank_info->output_info;
348         tdm_private_server *private_server = output_info->private_server;
349         tdm_server_wait_info *wait_info;
350         unsigned int enable_fake = 0;
351         tdm_error ret;
352
353         wait_info = calloc(1, sizeof * wait_info);
354         if (!wait_info) {
355                 TDM_ERR("alloc failed");
356                 ret = TDM_ERROR_OUT_OF_MEMORY;
357                 goto wait_failed;
358         }
359
360         LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
361         wait_info->vblank_info = vblank_info;
362         wait_info->req_id = req_id;
363         wait_info->req_time = TDM_TIME(req_sec, req_usec);
364
365         if (tdm_debug_module & TDM_DEBUG_VBLANK)
366                 TDM_DBG("req_id(%d) wait", req_id);
367
368         TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
369
370         ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, sequence, _tdm_server_cb_vblank, wait_info);
371
372         tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
373         if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
374                 goto wait_failed;
375
376         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
377
378         return;
379 wait_failed:
380         wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
381         if (wait_info)
382                 destroy_wait(wait_info);
383 }
384
385 static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
386         _tdm_server_vblank_cb_destroy,
387         _tdm_server_vblank_cb_set_name,
388         _tdm_server_vblank_cb_set_fps,
389         _tdm_server_vblank_cb_set_offset,
390         _tdm_server_vblank_cb_set_enable_fake,
391         _tdm_server_vblank_cb_wait_vblank,
392         _tdm_server_vblank_cb_wait_vblank_seq,
393 };
394
395 static void
396 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
397 {
398         wl_resource_destroy(resource);
399 }
400
401 static void
402 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
403 {
404         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
405         tdm_private_server *private_server = output_info->private_server;
406         tdm_private_loop *private_loop = private_server->private_loop;
407         struct wl_resource *vblank_resource;
408         tdm_vblank *vblank;
409         tdm_server_vblank_info *vblank_info;
410
411         vblank_resource =
412                 wl_resource_create(client, &wl_tdm_vblank_interface,
413                                                    wl_resource_get_version(resource), id);
414         if (!vblank_resource) {
415                 wl_resource_post_no_memory(resource);
416                 TDM_ERR("wl_resource_create failed");
417                 return;
418         }
419
420         vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
421         if (!vblank) {
422                 wl_resource_post_no_memory(resource);
423                 wl_resource_destroy(vblank_resource);
424                 TDM_ERR("tdm_vblank_create failed");
425                 return;
426         }
427
428         vblank_info = calloc(1, sizeof * vblank_info);
429         if (!vblank_info) {
430                 wl_resource_post_no_memory(resource);
431                 wl_resource_destroy(vblank_resource);
432                 tdm_vblank_destroy(vblank);
433                 TDM_ERR("alloc failed");
434                 return;
435         }
436
437         LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
438         vblank_info->output_info = output_info;
439         vblank_info->resource = vblank_resource;
440         vblank_info->vblank = vblank;
441         vblank_info->stamp = (unsigned int)tdm_vblank_get_stamp(vblank);
442
443         tdm_vblank_set_resource(vblank, vblank_resource);
444
445         wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
446                                                                    vblank_info, destroy_vblank_callback);
447
448         wl_tdm_vblank_send_stamp(vblank_info->resource, vblank_info->stamp);
449
450         return;
451 }
452
453 static void
454 _tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
455 {
456         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
457
458         TDM_RETURN_IF_FAIL(output_info != NULL);
459
460         output_info->watch_output_changes = enable;
461 }
462
463 static void
464 _tdm_server_output_cb_get_connection(struct wl_client *client, struct wl_resource *resource)
465 {
466         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
467         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
468         tdm_error ret;
469
470         TDM_RETURN_IF_FAIL(output_info != NULL);
471
472         ret = tdm_output_get_conn_status(output_info->output, &status);
473         TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
474
475         wl_tdm_output_send_connection(output_info->resource, status, ret);
476
477         return;
478
479 failed:
480         wl_tdm_output_send_connection(output_info->resource, TDM_OUTPUT_CONN_STATUS_DISCONNECTED, ret);
481 }
482
483 static void
484 _tdm_server_output_cb_get_mode(struct wl_client *client, struct wl_resource *resource)
485 {
486         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
487         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
488         tdm_error ret;
489
490         TDM_RETURN_IF_FAIL(output_info != NULL);
491
492         ret = tdm_output_get_conn_status(output_info->output, &status);
493         TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
494
495         if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
496                 const tdm_output_mode *mode = NULL;
497                 unsigned int hdisplay, vdisplay, vrefresh;
498
499                 ret = tdm_output_get_mode(output_info->output, &mode);
500                 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
501
502                 hdisplay = (mode) ? mode->hdisplay : 0;
503                 vdisplay = (mode) ? mode->vdisplay : 0;
504                 vrefresh = (mode) ? mode->vrefresh : 0;
505
506                 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
507         } else {
508                 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
509         }
510
511         return;
512 failed:
513         wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
514 }
515
516 static void
517 _tdm_server_output_cb_get_dpms(struct wl_client *client, struct wl_resource *resource)
518 {
519         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
520         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
521         tdm_error ret;
522
523         TDM_RETURN_IF_FAIL(output_info != NULL);
524
525         ret = tdm_output_get_conn_status(output_info->output, &status);
526         TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
527
528         if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
529                 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
530
531                 ret = tdm_output_get_dpms(output_info->output, &dpms_value);
532                 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
533
534                 wl_tdm_output_send_dpms(output_info->resource, dpms_value, ret);
535         } else {
536                 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
537         }
538
539         return;
540 failed:
541         wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, ret);
542 }
543
544 static const struct wl_tdm_output_interface tdm_output_implementation = {
545         _tdm_server_output_cb_destroy,
546         _tdm_server_output_cb_create_vblank,
547         _tdm_server_output_cb_watch_output_changes,
548         _tdm_server_output_cb_get_connection,
549         _tdm_server_output_cb_get_mode,
550         _tdm_server_output_cb_get_dpms,
551 };
552
553 static void
554 destroy_output_callback(struct wl_resource *resource)
555 {
556         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
557         tdm_server_vblank_info *v = NULL, *vv = NULL;
558
559         TDM_RETURN_IF_FAIL(output_info != NULL);
560
561         LIST_DEL(&output_info->link);
562
563         tdm_output_remove_change_handler(output_info->output,
564                                                                          _tdm_server_cb_output_change, output_info);
565
566         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
567                 wl_resource_destroy(v->resource);
568         }
569
570         free(output_info);
571 }
572
573 static void
574 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
575                                                          const char *name, uint32_t id)
576 {
577         tdm_private_server *private_server = wl_resource_get_user_data(resource);
578         tdm_server_output_info *output_info;
579         struct wl_resource *output_resource = NULL;
580         tdm_output *output;
581         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
582         tdm_error ret;
583
584         output = _tdm_server_find_output(private_server, name);
585         if (!output) {
586                 TDM_ERR("There is no '%s' output", name);
587                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
588                                                            "There is no '%s' output", name);
589                 return;
590         }
591
592         output_resource =
593                 wl_resource_create(client, &wl_tdm_output_interface,
594                                                    wl_resource_get_version(resource), id);
595         if (!output_resource) {
596                 wl_resource_post_no_memory(resource);
597                 TDM_ERR("wl_resource_create failed");
598                 return;
599         }
600
601         output_info = calloc(1, sizeof * output_info);
602         if (!output_info) {
603                 wl_resource_post_no_memory(resource);
604                 wl_resource_destroy(output_resource);
605                 TDM_ERR("alloc failed");
606                 return;
607         }
608
609         LIST_ADDTAIL(&output_info->link, &private_server->output_list);
610         output_info->private_server = private_server;
611         output_info->resource = output_resource;
612         output_info->output = output;
613         LIST_INITHEAD(&output_info->vblank_list);
614
615         tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
616
617         wl_resource_set_implementation(output_resource, &tdm_output_implementation,
618                                                                    output_info, destroy_output_callback);
619
620         ret = tdm_output_get_conn_status(output, &status);
621         wl_tdm_output_send_connection(output_resource, status, ret);
622
623         if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
624                 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
625                 const tdm_output_mode *mode = NULL;
626                 unsigned int hdisplay, vdisplay, vrefresh;
627
628                 ret = tdm_output_get_mode(output, &mode);
629                 hdisplay = (mode) ? mode->hdisplay : 0;
630                 vdisplay = (mode) ? mode->vdisplay : 0;
631                 vrefresh = (mode) ? mode->vrefresh : 0;
632                 wl_tdm_output_send_mode(output_resource, hdisplay, vdisplay, vrefresh, ret);
633
634                 ret = tdm_output_get_dpms(output, &dpms_value);
635                 wl_tdm_output_send_dpms(output_resource, dpms_value, ret);
636         } else {
637                 wl_tdm_output_send_mode(output_resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
638                 wl_tdm_output_send_dpms(output_resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
639         }
640 }
641
642 static void
643 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
644 {
645         tdm_private_server *private_server = wl_resource_get_user_data(resource);
646         tdm_private_loop *private_loop = private_server->private_loop;
647         char message[TDM_SERVER_REPLY_MSG_LEN];
648         char *m;
649         int len = sizeof(message), size;
650         uid_t uid;
651
652         wl_client_get_credentials(client, NULL, &uid, NULL);
653
654         if (uid != 0) {
655                 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
656                 TDM_ERR("%s", message);
657         } else {
658                 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
659         }
660
661         size = sizeof(message) - len;
662         m = message;
663
664         wl_client_flush(client);
665
666         while (size > 0) {
667                 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
668                 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
669
670                 strncpy(buffer, m, copylen);
671                 m += copylen;
672                 size -= copylen;
673
674                 buffer[copylen] = '\0';
675
676                 wl_tdm_send_debug_message(resource, buffer);
677         }
678
679         wl_tdm_send_debug_done(resource);
680 }
681
682 static void
683 _tdm_server_cb_set_client_vblank_fps(struct wl_client *client, struct wl_resource *resource,
684                                                                          unsigned int pid, const char *name, unsigned int fps)
685 {
686         tdm_error ret = tdm_vblank_set_client_vblank_fps(pid, name, fps);
687         TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
688
689         TDM_INFO("'%s' vblank fps(PID: '%u'): %u", name, pid, fps);
690 }
691
692 static const struct wl_tdm_interface tdm_implementation = {
693         _tdm_server_cb_debug,
694         _tdm_server_cb_create_output,
695         _tdm_server_cb_set_client_vblank_fps,
696 };
697
698 static void
699 destroy_client(struct wl_resource *resource)
700 {
701         tdm_server_client_info *c = NULL, *cc = NULL;
702         struct wl_client *client;
703         pid_t pid = -1;
704
705         client = wl_resource_get_client(resource);
706         TDM_RETURN_IF_FAIL(client != NULL);
707
708         wl_client_get_credentials(client, &pid, NULL, NULL);
709
710         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
711                 if (c->pid == pid) {
712                         LIST_DEL(&c->link);
713                         free(c);
714                         return;
715                 }
716         }
717 }
718
719 static void
720 _tdm_server_bind(struct wl_client *client, void *data,
721                                  uint32_t version, uint32_t id)
722 {
723         struct wl_resource *resource;
724         tdm_server_client_info *cinfo;
725
726         resource = wl_resource_create(client, &wl_tdm_interface, version, id);
727         if (!resource) {
728                 wl_client_post_no_memory(client);
729                 return;
730         }
731
732         cinfo = calloc(1, sizeof(tdm_server_client_info));
733         if (!cinfo) {
734                 wl_client_post_no_memory(client);
735                 wl_resource_destroy(resource);
736                 return;
737         }
738
739         LIST_ADDTAIL(&cinfo->link, &client_list);
740         wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
741         _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
742
743         wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
744 }
745
746 static int
747 _tdm_getgrnam_r(const char *name)
748 {
749         struct group *grp = NULL;
750         struct group *grp_res = NULL;
751         char* buf = NULL;
752         size_t buf_len;
753         int ret;
754         int id;
755
756         buf_len = sysconf(_SC_GETGR_R_SIZE_MAX);
757         if (buf_len == -1)
758                 buf_len = 2048;
759
760         buf = calloc(1, buf_len * sizeof(char));
761         if (!buf) {
762                 TDM_ERR("creating buffer failed");
763                 goto failed;
764         }
765
766         grp = calloc(1, sizeof(struct group));
767         if (!grp) {
768                 TDM_ERR("creating group failed");
769                 goto failed;
770         }
771
772         ret = getgrnam_r(name, grp, buf, buf_len, &grp_res);
773         if (ret < 0) {
774                 TDM_ERR("getgrnam_r failed errno:%d(%m)", ret);
775                 goto failed;
776         }
777
778         if (grp_res == NULL) {
779                 TDM_ERR("finding name:%s group failed", name);
780                 goto failed;
781         }
782
783         id = grp->gr_gid;
784         free(buf);
785         free(grp);
786
787         return id;
788
789 failed:
790         if (buf)
791                 free(buf);
792         if (grp)
793                 free(grp);
794
795         return -1;
796 }
797
798 static void
799 _tdm_socket_init(tdm_private_loop *private_loop)
800 {
801         const char *dir = NULL;
802         char socket_path[TDM_NAME_LEN * 2];
803         int ret = -1;
804         uid_t uid;
805         gid_t gid;
806
807         dir = getenv("XDG_RUNTIME_DIR");
808         if (!dir) {
809                 TDM_WRN("getting XDG_RUNTIME_DIR failed");
810                 return;
811         }
812
813         strncpy(socket_path, dir, TDM_NAME_LEN - 1);
814         socket_path[TDM_NAME_LEN - 1] = '\0';
815
816         strncat(socket_path, "/tdm-socket", 11);
817         socket_path[TDM_NAME_LEN + 10] = '\0';
818
819         ret = chmod(socket_path, 509);
820         if (ret < 0) {
821                 TDM_WRN("changing modes of socket file failed:%s (%m)", socket_path);
822                 return;
823         }
824
825         ret = _tdm_getgrnam_r("root");
826         if (ret < 0) {
827                 TDM_WRN("getting uid failed");
828                 return;
829         }
830         uid = ret;
831
832         ret = _tdm_getgrnam_r("display");
833         if (ret < 0) {
834                 TDM_WRN("getting gid failed");
835                 return;
836         }
837         gid = ret;
838
839         ret = chown(socket_path, uid, gid);
840         if (ret < 0) {
841                 TDM_WRN("changing owner of socket file failed:%s (%m)", socket_path);
842                 return;
843         }
844 }
845
846 INTERN tdm_error
847 tdm_server_init(tdm_private_loop *private_loop)
848 {
849         tdm_private_server *private_server;
850
851         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
852         TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
853
854         if (private_loop->private_server)
855                 return TDM_ERROR_NONE;
856
857         if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
858                 TDM_ERR("createing a tdm-socket failed");
859                 return TDM_ERROR_OPERATION_FAILED;
860         }
861
862         _tdm_socket_init(private_loop);
863
864         private_server = calloc(1, sizeof * private_server);
865         if (!private_server) {
866                 TDM_ERR("alloc failed");
867                 return TDM_ERROR_OUT_OF_MEMORY;
868         }
869
870         LIST_INITHEAD(&private_server->output_list);
871         LIST_INITHEAD(&private_server->wait_list);
872
873         if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
874                                                   private_server, _tdm_server_bind)) {
875                 TDM_ERR("creating a global resource failed");
876                 free(private_server);
877                 return TDM_ERROR_OUT_OF_MEMORY;
878         }
879
880         private_server->private_loop = private_loop;
881         private_loop->private_server = private_server;
882         keep_private_server = private_server;
883
884         LIST_INITHEAD(&client_list);
885
886         return TDM_ERROR_NONE;
887 }
888
889 INTERN void
890 tdm_server_deinit(tdm_private_loop *private_loop)
891 {
892         tdm_server_output_info *o = NULL, *oo = NULL;
893         tdm_server_wait_info *w = NULL, *ww = NULL;
894         tdm_server_client_info *c = NULL, *cc = NULL;
895         tdm_private_server *private_server;
896
897         if (!private_loop->private_server)
898                 return;
899
900         private_server = private_loop->private_server;
901
902         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
903                 destroy_wait(w);
904         }
905
906         LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
907                 wl_resource_destroy(o->resource);
908         }
909
910         free(private_server);
911         private_loop->private_server = NULL;
912         keep_private_server = NULL;
913
914         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
915                 LIST_DEL(&c->link);
916                 free(c);
917         }
918 }
919
920 INTERN const char*
921 tdm_server_get_client_name(pid_t pid)
922 {
923         tdm_server_client_info *c = NULL;
924
925         LIST_FOR_EACH_ENTRY(c, &client_list, link) {
926                 if (c->pid == pid)
927                         return (const char*)c->name;
928         }
929
930         return NULL;
931 }