1 /**************************************************************************
5 * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
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>
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:
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
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.
34 **************************************************************************/
41 #include "tdm_private.h"
43 #include "tdm-server-protocol.h"
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.
54 struct _tdm_private_server {
55 tdm_private_loop *private_loop;
56 struct list_head output_list;
57 struct list_head wait_list;
60 typedef struct _tdm_server_output_info {
61 struct list_head link;
62 tdm_private_server *private_server;
63 struct wl_resource *resource;
65 struct list_head vblank_list;
66 unsigned int watch_output_changes;
67 } tdm_server_output_info;
69 typedef struct _tdm_server_vblank_info {
70 struct list_head link;
71 tdm_server_output_info *output_info;
72 struct wl_resource *resource;
76 } tdm_server_vblank_info;
78 typedef struct _tdm_server_wait_info {
79 struct list_head link;
80 tdm_server_vblank_info *vblank_info;
84 } tdm_server_wait_info;
86 typedef struct _tdm_server_client_info {
87 struct list_head link;
89 char name[TDM_NAME_LEN];
90 struct wl_resource *resource;
91 } tdm_server_client_info;
93 static tdm_private_server *keep_private_server;
94 static struct list_head client_list;
96 static void destroy_wait(tdm_server_wait_info *wait_info);
99 _tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
101 char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
105 snprintf(name, size, "Unknown");
107 snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
108 h = fopen(proc, "r");
112 len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
114 char *p = strncpy(pname, "NO NAME", sizeof(pname) - 1);
117 pname[len - 1] = '\0';
119 strncpy(name, pname, size - 1);
120 name[size - 1] = '\0';
126 _tdm_server_find_output(tdm_private_server *private_server, const char *name)
128 tdm_private_loop *private_loop = private_server->private_loop;
129 tdm_output *found = NULL;
131 if (!strncasecmp(name, "primary", 7) || !strncasecmp(name, "default", 7))
132 found = tdm_display_get_output(private_loop->dpy, 0, NULL);
137 tdm_display_get_output_count(private_loop->dpy, &count);
139 for (i = 0; i < count; i++) {
140 tdm_output *output = tdm_display_get_output(private_loop->dpy, i, NULL);
141 tdm_output_conn_status status;
142 const char *model = NULL;
145 ret = tdm_output_get_conn_status(output, &status);
146 if (ret || status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
149 ret = tdm_output_get_model_info(output, NULL, &model, NULL);
153 if (strncmp(model, name, TDM_NAME_LEN))
165 _tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error,
166 unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec)
168 tdm_server_wait_info *found;
169 tdm_server_vblank_info *vblank_info;
171 if (!keep_private_server)
174 LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list,
175 tdm_server_wait_info, link, found);
177 TDM_DBG("wait_info(%p) is destroyed", wait_info);
181 if (tdm_debug_module & TDM_DEBUG_VBLANK)
182 TDM_DBG("req_id(%d) done", wait_info->req_id);
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);
188 destroy_wait(wait_info);
192 _tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
193 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
195 _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
199 _tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
200 tdm_value value, void *user_data)
202 tdm_server_output_info *output_info = user_data;
203 struct wl_client *client;
206 TDM_RETURN_IF_FAIL(output_info != NULL);
208 client = wl_resource_get_client(output_info->resource);
209 TDM_RETURN_IF_FAIL(client != NULL);
211 wl_client_get_credentials(client, &pid, NULL, NULL);
213 if (!output_info->watch_output_changes) {
214 TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid);
218 TDM_DBG("send the output changes: %d", (unsigned int)pid);
221 case TDM_OUTPUT_CHANGE_DPMS:
222 wl_tdm_output_send_dpms(output_info->resource, value.u32, TDM_ERROR_NONE);
224 case TDM_OUTPUT_CHANGE_CONNECTION:
225 wl_tdm_output_send_connection(output_info->resource, value.u32, TDM_ERROR_NONE);
233 destroy_wait(tdm_server_wait_info *wait_info)
235 LIST_DEL(&wait_info->link);
240 destroy_vblank_callback(struct wl_resource *resource)
242 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
243 tdm_server_wait_info *w = NULL, *ww = NULL;
245 TDM_RETURN_IF_FAIL(vblank_info != NULL);
247 LIST_DEL(&vblank_info->link);
249 if (vblank_info->vblank)
250 tdm_vblank_destroy(vblank_info->vblank);
252 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
253 if (w->vblank_info == vblank_info)
261 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
263 wl_resource_destroy(resource);
267 _tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
269 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
271 tdm_vblank_set_name(vblank_info->vblank, name);
275 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
277 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
279 tdm_vblank_set_fps(vblank_info->vblank, fps);
283 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
285 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
287 tdm_vblank_set_offset(vblank_info->vblank, offset);
291 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
293 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
295 tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
299 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
300 uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
302 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
303 tdm_server_output_info *output_info = vblank_info->output_info;
304 tdm_private_server *private_server = output_info->private_server;
305 tdm_server_wait_info *wait_info;
306 unsigned int enable_fake = 0;
309 wait_info = calloc(1, sizeof * wait_info);
311 TDM_ERR("alloc failed");
312 ret = TDM_ERROR_OUT_OF_MEMORY;
316 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
317 wait_info->vblank_info = vblank_info;
318 wait_info->req_id = req_id;
319 wait_info->req_time = TDM_TIME(req_sec, req_usec);
321 if (tdm_debug_module & TDM_DEBUG_VBLANK)
322 TDM_DBG("req_id(%d) wait", req_id);
324 ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
326 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
327 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
330 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
334 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
336 destroy_wait(wait_info);
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)
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;
350 wait_info = calloc(1, sizeof * wait_info);
352 TDM_ERR("alloc failed");
353 ret = TDM_ERROR_OUT_OF_MEMORY;
357 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
358 wait_info->vblank_info = vblank_info;
359 wait_info->req_id = req_id;
360 wait_info->req_time = TDM_TIME(req_sec, req_usec);
362 if (tdm_debug_module & TDM_DEBUG_VBLANK)
363 TDM_DBG("req_id(%d) wait", req_id);
365 ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, sequence, _tdm_server_cb_vblank, wait_info);
367 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
368 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
371 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
375 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
377 destroy_wait(wait_info);
380 static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
381 _tdm_server_vblank_cb_destroy,
382 _tdm_server_vblank_cb_set_name,
383 _tdm_server_vblank_cb_set_fps,
384 _tdm_server_vblank_cb_set_offset,
385 _tdm_server_vblank_cb_set_enable_fake,
386 _tdm_server_vblank_cb_wait_vblank,
387 _tdm_server_vblank_cb_wait_vblank_seq,
391 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
393 wl_resource_destroy(resource);
397 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
399 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
400 tdm_private_server *private_server = output_info->private_server;
401 tdm_private_loop *private_loop = private_server->private_loop;
402 struct wl_resource *vblank_resource;
404 tdm_server_vblank_info *vblank_info;
407 wl_resource_create(client, &wl_tdm_vblank_interface,
408 wl_resource_get_version(resource), id);
409 if (!vblank_resource) {
410 wl_resource_post_no_memory(resource);
411 TDM_ERR("wl_resource_create failed");
415 vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
417 wl_resource_post_no_memory(resource);
418 wl_resource_destroy(vblank_resource);
419 TDM_ERR("tdm_vblank_create failed");
423 vblank_info = calloc(1, sizeof * vblank_info);
425 wl_resource_post_no_memory(resource);
426 wl_resource_destroy(vblank_resource);
427 tdm_vblank_destroy(vblank);
428 TDM_ERR("alloc failed");
432 LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
433 vblank_info->output_info = output_info;
434 vblank_info->resource = vblank_resource;
435 vblank_info->vblank = vblank;
436 vblank_info->stamp = (unsigned int)tdm_vblank_get_stamp(vblank);
438 tdm_vblank_set_resource(vblank, vblank_resource);
440 wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
441 vblank_info, destroy_vblank_callback);
443 wl_tdm_vblank_send_stamp(vblank_info->resource, vblank_info->stamp);
445 if (tdm_ttrace_module & TDM_TTRACE_CLIENT) {
446 tdm_output *output = tdm_display_get_output(private_loop->dpy, tdm_ttrace_output, NULL);
447 if (output == output_info->output)
448 wl_tdm_vblank_send_ttrace(vblank_info->resource, 1);
455 _tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
457 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
459 TDM_RETURN_IF_FAIL(output_info != NULL);
461 output_info->watch_output_changes = enable;
465 _tdm_server_output_cb_get_connection(struct wl_client *client, struct wl_resource *resource)
467 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
468 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
471 TDM_RETURN_IF_FAIL(output_info != NULL);
473 ret = tdm_output_get_conn_status(output_info->output, &status);
474 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
476 wl_tdm_output_send_connection(output_info->resource, status, ret);
481 wl_tdm_output_send_connection(output_info->resource, TDM_OUTPUT_CONN_STATUS_DISCONNECTED, ret);
485 _tdm_server_output_cb_get_mode(struct wl_client *client, struct wl_resource *resource)
487 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
488 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
491 TDM_RETURN_IF_FAIL(output_info != NULL);
493 ret = tdm_output_get_conn_status(output_info->output, &status);
494 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
496 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
497 const tdm_output_mode *mode = NULL;
498 unsigned int hdisplay, vdisplay, vrefresh;
500 ret = tdm_output_get_mode(output_info->output, &mode);
501 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
503 hdisplay = (mode) ? mode->hdisplay : 0;
504 vdisplay = (mode) ? mode->vdisplay : 0;
505 vrefresh = (mode) ? mode->vrefresh : 0;
507 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
509 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
514 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
518 _tdm_server_output_cb_get_dpms(struct wl_client *client, struct wl_resource *resource)
520 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
521 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
524 TDM_RETURN_IF_FAIL(output_info != NULL);
526 ret = tdm_output_get_conn_status(output_info->output, &status);
527 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
529 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
530 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
532 ret = tdm_output_get_dpms(output_info->output, &dpms_value);
533 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
535 wl_tdm_output_send_dpms(output_info->resource, dpms_value, ret);
537 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
542 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, ret);
545 static const struct wl_tdm_output_interface tdm_output_implementation = {
546 _tdm_server_output_cb_destroy,
547 _tdm_server_output_cb_create_vblank,
548 _tdm_server_output_cb_watch_output_changes,
549 _tdm_server_output_cb_get_connection,
550 _tdm_server_output_cb_get_mode,
551 _tdm_server_output_cb_get_dpms,
555 destroy_output_callback(struct wl_resource *resource)
557 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
558 tdm_server_vblank_info *v = NULL, *vv = NULL;
560 TDM_RETURN_IF_FAIL(output_info != NULL);
562 LIST_DEL(&output_info->link);
564 tdm_output_remove_change_handler(output_info->output,
565 _tdm_server_cb_output_change, output_info);
567 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
568 wl_resource_destroy(v->resource);
575 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
576 const char *name, uint32_t id)
578 tdm_private_server *private_server = wl_resource_get_user_data(resource);
579 tdm_server_output_info *output_info;
580 struct wl_resource *output_resource = NULL;
582 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
585 output = _tdm_server_find_output(private_server, name);
587 TDM_ERR("There is no '%s' output", name);
588 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
589 "There is no '%s' output", name);
594 wl_resource_create(client, &wl_tdm_output_interface,
595 wl_resource_get_version(resource), id);
596 if (!output_resource) {
597 wl_resource_post_no_memory(resource);
598 TDM_ERR("wl_resource_create failed");
602 output_info = calloc(1, sizeof * output_info);
604 wl_resource_post_no_memory(resource);
605 wl_resource_destroy(output_resource);
606 TDM_ERR("alloc failed");
610 LIST_ADDTAIL(&output_info->link, &private_server->output_list);
611 output_info->private_server = private_server;
612 output_info->resource = output_resource;
613 output_info->output = output;
614 LIST_INITHEAD(&output_info->vblank_list);
616 tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
618 wl_resource_set_implementation(output_resource, &tdm_output_implementation,
619 output_info, destroy_output_callback);
621 ret = tdm_output_get_conn_status(output, &status);
622 wl_tdm_output_send_connection(output_resource, status, ret);
624 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
625 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
626 const tdm_output_mode *mode = NULL;
627 unsigned int hdisplay, vdisplay, vrefresh;
629 ret = tdm_output_get_mode(output, &mode);
630 hdisplay = (mode) ? mode->hdisplay : 0;
631 vdisplay = (mode) ? mode->vdisplay : 0;
632 vrefresh = (mode) ? mode->vrefresh : 0;
633 wl_tdm_output_send_mode(output_resource, hdisplay, vdisplay, vrefresh, ret);
635 ret = tdm_output_get_dpms(output, &dpms_value);
636 wl_tdm_output_send_dpms(output_resource, dpms_value, ret);
638 wl_tdm_output_send_mode(output_resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
639 wl_tdm_output_send_dpms(output_resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
644 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
646 tdm_private_server *private_server = wl_resource_get_user_data(resource);
647 tdm_private_loop *private_loop = private_server->private_loop;
648 char message[TDM_SERVER_REPLY_MSG_LEN];
650 int len = sizeof(message), size;
653 wl_client_get_credentials(client, NULL, &uid, NULL);
656 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
657 TDM_ERR("%s", message);
659 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
662 size = sizeof(message) - len;
665 wl_client_flush(client);
668 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
669 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
671 strncpy(buffer, m, copylen);
675 buffer[copylen] = '\0';
677 wl_tdm_send_debug_message(resource, buffer);
680 wl_tdm_send_debug_done(resource);
683 static const struct wl_tdm_interface tdm_implementation = {
684 _tdm_server_cb_debug,
685 _tdm_server_cb_create_output,
689 destroy_client(struct wl_resource *resource)
691 tdm_server_client_info *c = NULL, *cc = NULL;
692 struct wl_client *client;
694 client = wl_resource_get_client(resource);
695 TDM_RETURN_IF_FAIL(client != NULL);
697 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
698 if (c->resource == resource) {
707 _tdm_server_bind(struct wl_client *client, void *data,
708 uint32_t version, uint32_t id)
710 struct wl_resource *resource;
711 tdm_server_client_info *cinfo;
713 resource = wl_resource_create(client, &wl_tdm_interface, version, id);
715 wl_client_post_no_memory(client);
719 cinfo = calloc(1, sizeof(tdm_server_client_info));
721 wl_client_post_no_memory(client);
722 wl_resource_destroy(resource);
726 cinfo->resource = resource;
728 LIST_ADDTAIL(&cinfo->link, &client_list);
729 wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
730 _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
732 wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
736 _tdm_getgrnam_r(const char *name)
738 struct group *grp = NULL;
739 struct group *grp_res = NULL;
745 buf_len = sysconf(_SC_GETGR_R_SIZE_MAX);
749 buf = calloc(1, buf_len * sizeof(char));
751 TDM_ERR("creating buffer failed");
755 grp = calloc(1, sizeof(struct group));
757 TDM_ERR("creating group failed");
761 ret = getgrnam_r(name, grp, buf, buf_len, &grp_res);
763 TDM_ERR("getgrnam_r failed errno:%d(%m)", ret);
767 if (grp_res == NULL) {
768 TDM_ERR("finding name:%s group failed", name);
788 _tdm_socket_init(tdm_private_loop *private_loop)
790 const char *dir = NULL;
791 char socket_path[TDM_NAME_LEN * 2];
796 dir = getenv("XDG_RUNTIME_DIR");
798 TDM_WRN("getting XDG_RUNTIME_DIR failed");
803 if (len > TDM_NAME_LEN - 1) {
804 TDM_ERR("XDG_RUNTIME_DIR is too long\n");
808 strncpy(socket_path, dir, TDM_NAME_LEN - 1);
809 socket_path[TDM_NAME_LEN - 1] = '\0';
811 strncat(socket_path, "/tdm-socket", 11);
812 socket_path[TDM_NAME_LEN + 10] = '\0';
814 ret = chmod(socket_path, 509);
816 TDM_WRN("changing modes of socket file failed:%s (%m)", socket_path);
820 ret = _tdm_getgrnam_r("root");
822 TDM_WRN("getting uid failed");
827 ret = _tdm_getgrnam_r("display");
829 TDM_WRN("getting gid failed");
834 ret = chown(socket_path, uid, gid);
836 TDM_WRN("changing owner of socket file failed:%s (%m)", socket_path);
842 tdm_server_init(tdm_private_loop *private_loop)
844 tdm_private_server *private_server;
846 TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
847 TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
849 if (private_loop->private_server)
850 return TDM_ERROR_NONE;
852 if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
853 TDM_ERR("createing a tdm-socket failed");
854 return TDM_ERROR_OPERATION_FAILED;
857 _tdm_socket_init(private_loop);
859 private_server = calloc(1, sizeof * private_server);
860 if (!private_server) {
861 TDM_ERR("alloc failed");
862 return TDM_ERROR_OUT_OF_MEMORY;
865 LIST_INITHEAD(&private_server->output_list);
866 LIST_INITHEAD(&private_server->wait_list);
868 if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
869 private_server, _tdm_server_bind)) {
870 TDM_ERR("creating a global resource failed");
871 free(private_server);
872 return TDM_ERROR_OUT_OF_MEMORY;
875 private_server->private_loop = private_loop;
876 private_loop->private_server = private_server;
877 keep_private_server = private_server;
879 LIST_INITHEAD(&client_list);
881 return TDM_ERROR_NONE;
885 tdm_server_deinit(tdm_private_loop *private_loop)
887 tdm_server_output_info *o = NULL, *oo = NULL;
888 tdm_server_wait_info *w = NULL, *ww = NULL;
889 tdm_server_client_info *c = NULL, *cc = NULL;
890 tdm_private_server *private_server;
892 if (!private_loop->private_server)
895 private_server = private_loop->private_server;
897 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
901 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
902 wl_resource_destroy(o->resource);
905 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
906 wl_resource_destroy(c->resource);
909 free(private_server);
910 private_loop->private_server = NULL;
911 keep_private_server = NULL;
915 tdm_server_get_client_name(pid_t pid)
917 tdm_server_client_info *c = NULL;
919 LIST_FOR_EACH_ENTRY(c, &client_list, link) {
921 return (const char*)c->name;
928 tdm_server_enable_ttrace_client_vblank(tdm_display *dpy, tdm_output *output, int enable)
930 tdm_private_server *private_server = keep_private_server;
931 tdm_server_output_info *output_info = NULL;
933 if (!keep_private_server)
934 return TDM_ERROR_NONE;
936 LIST_FOR_EACH_ENTRY(output_info, &private_server->output_list, link) {
937 tdm_server_vblank_info *vblank_info = NULL;
939 if (output && output_info->output != output)
942 LIST_FOR_EACH_ENTRY(vblank_info, &output_info->vblank_list, link) {
943 wl_tdm_vblank_send_ttrace(vblank_info->resource, enable);
947 return TDM_ERROR_NONE;