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;
75 } tdm_server_vblank_info;
77 typedef struct _tdm_server_wait_info {
78 struct list_head link;
79 tdm_server_vblank_info *vblank_info;
82 } tdm_server_wait_info;
84 typedef struct _tdm_server_client_info {
85 struct list_head link;
87 char name[TDM_NAME_LEN];
88 } tdm_server_client_info;
90 static tdm_private_server *keep_private_server;
91 static struct list_head client_list;
93 static void destroy_wait(tdm_server_wait_info *wait_info);
96 _tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
98 char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
102 snprintf(name, size, "Unknown");
104 snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
105 h = fopen(proc, "r");
109 len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
111 char *p = strncpy(pname, "NO NAME", sizeof(pname) - 1);
114 pname[len - 1] = '\0';
116 strncpy(name, pname, size - 1);
117 name[size - 1] = '\0';
123 _tdm_server_find_output(tdm_private_server *private_server, const char *name)
125 tdm_private_loop *private_loop = private_server->private_loop;
126 tdm_output *found = NULL;
128 if (!strncasecmp(name, "primary", 7) || !strncasecmp(name, "default", 7))
129 found = tdm_display_get_output(private_loop->dpy, 0, NULL);
134 tdm_display_get_output_count(private_loop->dpy, &count);
136 for (i = 0; i < count; i++) {
137 tdm_output *output = tdm_display_get_output(private_loop->dpy, i, NULL);
138 tdm_output_conn_status status;
139 const char *model = NULL;
142 ret = tdm_output_get_conn_status(output, &status);
143 if (ret || status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
146 ret = tdm_output_get_model_info(output, NULL, &model, NULL);
150 if (strncmp(model, name, TDM_NAME_LEN))
162 _tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error,
163 unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec)
165 tdm_server_wait_info *found;
166 tdm_server_vblank_info *vblank_info;
168 if (!keep_private_server)
171 LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list,
172 tdm_server_wait_info, link, found);
174 TDM_DBG("wait_info(%p) is destroyed", wait_info);
178 if (tdm_debug_module & TDM_DEBUG_VBLANK)
179 TDM_DBG("req_id(%d) done", wait_info->req_id);
181 TDM_TRACE_COUNT(ServerDoneVBlank, wait_info->req_id);
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 destroy_wait(wait_info);
190 _tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
191 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
193 _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
197 _tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
198 tdm_value value, void *user_data)
200 tdm_server_output_info *output_info = user_data;
201 struct wl_client *client;
204 TDM_RETURN_IF_FAIL(output_info != NULL);
206 client = wl_resource_get_client(output_info->resource);
207 TDM_RETURN_IF_FAIL(client != NULL);
209 wl_client_get_credentials(client, &pid, NULL, NULL);
211 if (!output_info->watch_output_changes) {
212 TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid);
216 TDM_DBG("send the output changes: %d", (unsigned int)pid);
219 case TDM_OUTPUT_CHANGE_DPMS:
220 wl_tdm_output_send_dpms(output_info->resource, value.u32, TDM_ERROR_NONE);
222 case TDM_OUTPUT_CHANGE_CONNECTION:
223 wl_tdm_output_send_connection(output_info->resource, value.u32, TDM_ERROR_NONE);
231 destroy_wait(tdm_server_wait_info *wait_info)
233 LIST_DEL(&wait_info->link);
238 destroy_vblank_callback(struct wl_resource *resource)
240 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
241 tdm_server_wait_info *w = NULL, *ww = NULL;
243 TDM_RETURN_IF_FAIL(vblank_info != NULL);
245 if (vblank_info->vblank)
246 tdm_vblank_destroy(vblank_info->vblank);
248 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
249 if (w->vblank_info == vblank_info)
253 LIST_DEL(&vblank_info->link);
258 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
260 wl_resource_destroy(resource);
264 _tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
266 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
268 tdm_vblank_set_name(vblank_info->vblank, name);
272 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
274 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
276 tdm_vblank_set_fps(vblank_info->vblank, fps);
280 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
282 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
284 tdm_vblank_set_offset(vblank_info->vblank, offset);
288 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
290 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
292 tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
296 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
297 uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
299 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
300 tdm_server_output_info *output_info = vblank_info->output_info;
301 tdm_private_server *private_server = output_info->private_server;
302 tdm_server_wait_info *wait_info;
303 unsigned int enable_fake = 0;
306 TDM_TRACE_COUNT(ServerWaitVBlank, req_id);
308 wait_info = calloc(1, sizeof * wait_info);
310 TDM_ERR("alloc failed");
311 ret = TDM_ERROR_OUT_OF_MEMORY;
315 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
316 wait_info->vblank_info = vblank_info;
317 wait_info->req_id = req_id;
319 if (tdm_debug_module & TDM_DEBUG_VBLANK)
320 TDM_DBG("req_id(%d) wait", req_id);
322 ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
324 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
325 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
328 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
332 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
334 destroy_wait(wait_info);
338 _tdm_server_vblank_cb_wait_vblank_seq(struct wl_client *client, struct wl_resource *resource,
339 uint32_t sequence, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
341 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
342 tdm_server_output_info *output_info = vblank_info->output_info;
343 tdm_private_server *private_server = output_info->private_server;
344 tdm_server_wait_info *wait_info;
345 unsigned int enable_fake = 0;
348 TDM_TRACE_COUNT(ServerWaitVBlank, req_id);
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;
361 if (tdm_debug_module & TDM_DEBUG_VBLANK)
362 TDM_DBG("req_id(%d) wait", req_id);
364 ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, sequence, _tdm_server_cb_vblank, wait_info);
366 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
367 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
370 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
374 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
376 destroy_wait(wait_info);
379 static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
380 _tdm_server_vblank_cb_destroy,
381 _tdm_server_vblank_cb_set_name,
382 _tdm_server_vblank_cb_set_fps,
383 _tdm_server_vblank_cb_set_offset,
384 _tdm_server_vblank_cb_set_enable_fake,
385 _tdm_server_vblank_cb_wait_vblank,
386 _tdm_server_vblank_cb_wait_vblank_seq,
390 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
392 wl_resource_destroy(resource);
396 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
398 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
399 tdm_private_server *private_server = output_info->private_server;
400 tdm_private_loop *private_loop = private_server->private_loop;
401 struct wl_resource *vblank_resource;
403 tdm_server_vblank_info *vblank_info;
406 wl_resource_create(client, &wl_tdm_vblank_interface,
407 wl_resource_get_version(resource), id);
408 if (!vblank_resource) {
409 wl_resource_post_no_memory(resource);
410 TDM_ERR("wl_resource_create failed");
414 vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
416 wl_resource_post_no_memory(resource);
417 wl_resource_destroy(vblank_resource);
418 TDM_ERR("tdm_vblank_create failed");
422 vblank_info = calloc(1, sizeof * vblank_info);
424 wl_resource_post_no_memory(resource);
425 wl_resource_destroy(vblank_resource);
426 tdm_vblank_destroy(vblank);
427 TDM_ERR("alloc failed");
431 LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
432 vblank_info->output_info = output_info;
433 vblank_info->resource = vblank_resource;
434 vblank_info->vblank = vblank;
436 tdm_vblank_set_resource(vblank, vblank_resource);
438 wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
439 vblank_info, destroy_vblank_callback);
445 _tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
447 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
449 TDM_RETURN_IF_FAIL(output_info != NULL);
451 output_info->watch_output_changes = enable;
455 _tdm_server_output_cb_get_connection(struct wl_client *client, struct wl_resource *resource)
457 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
458 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
461 TDM_RETURN_IF_FAIL(output_info != NULL);
463 ret = tdm_output_get_conn_status(output_info->output, &status);
464 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
466 wl_tdm_output_send_connection(output_info->resource, status, ret);
471 wl_tdm_output_send_connection(output_info->resource, TDM_OUTPUT_CONN_STATUS_DISCONNECTED, ret);
475 _tdm_server_output_cb_get_mode(struct wl_client *client, struct wl_resource *resource)
477 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
478 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
481 TDM_RETURN_IF_FAIL(output_info != NULL);
483 ret = tdm_output_get_conn_status(output_info->output, &status);
484 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
486 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
487 const tdm_output_mode *mode = NULL;
488 unsigned int hdisplay, vdisplay, vrefresh;
490 ret = tdm_output_get_mode(output_info->output, &mode);
491 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
493 hdisplay = (mode) ? mode->hdisplay : 0;
494 vdisplay = (mode) ? mode->vdisplay : 0;
495 vrefresh = (mode) ? mode->vrefresh : 0;
497 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
499 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
504 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
508 _tdm_server_output_cb_get_dpms(struct wl_client *client, struct wl_resource *resource)
510 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
511 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
514 TDM_RETURN_IF_FAIL(output_info != NULL);
516 ret = tdm_output_get_conn_status(output_info->output, &status);
517 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
519 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
520 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
522 ret = tdm_output_get_dpms(output_info->output, &dpms_value);
523 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
525 wl_tdm_output_send_dpms(output_info->resource, dpms_value, ret);
527 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
532 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, ret);
535 static const struct wl_tdm_output_interface tdm_output_implementation = {
536 _tdm_server_output_cb_destroy,
537 _tdm_server_output_cb_create_vblank,
538 _tdm_server_output_cb_watch_output_changes,
539 _tdm_server_output_cb_get_connection,
540 _tdm_server_output_cb_get_mode,
541 _tdm_server_output_cb_get_dpms,
545 destroy_output_callback(struct wl_resource *resource)
547 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
548 tdm_server_vblank_info *v = NULL, *vv = NULL;
550 TDM_RETURN_IF_FAIL(output_info != NULL);
552 tdm_output_remove_change_handler(output_info->output,
553 _tdm_server_cb_output_change, output_info);
555 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
556 wl_resource_destroy(v->resource);
559 LIST_DEL(&output_info->link);
564 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
565 const char *name, uint32_t id)
567 tdm_private_server *private_server = wl_resource_get_user_data(resource);
568 tdm_server_output_info *output_info;
569 struct wl_resource *output_resource = NULL;
571 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
574 output = _tdm_server_find_output(private_server, name);
576 TDM_ERR("There is no '%s' output", name);
577 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
578 "There is no '%s' output", name);
583 wl_resource_create(client, &wl_tdm_output_interface,
584 wl_resource_get_version(resource), id);
585 if (!output_resource) {
586 wl_resource_post_no_memory(resource);
587 TDM_ERR("wl_resource_create failed");
591 output_info = calloc(1, sizeof * output_info);
593 wl_resource_post_no_memory(resource);
594 wl_resource_destroy(output_resource);
595 TDM_ERR("alloc failed");
599 LIST_ADDTAIL(&output_info->link, &private_server->output_list);
600 output_info->private_server = private_server;
601 output_info->resource = output_resource;
602 output_info->output = output;
603 LIST_INITHEAD(&output_info->vblank_list);
605 tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
607 wl_resource_set_implementation(output_resource, &tdm_output_implementation,
608 output_info, destroy_output_callback);
610 ret = tdm_output_get_conn_status(output, &status);
611 wl_tdm_output_send_connection(output_resource, status, ret);
613 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
614 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
615 const tdm_output_mode *mode = NULL;
616 unsigned int hdisplay, vdisplay, vrefresh;
618 ret = tdm_output_get_mode(output, &mode);
619 hdisplay = (mode) ? mode->hdisplay : 0;
620 vdisplay = (mode) ? mode->vdisplay : 0;
621 vrefresh = (mode) ? mode->vrefresh : 0;
622 wl_tdm_output_send_mode(output_resource, hdisplay, vdisplay, vrefresh, ret);
624 ret = tdm_output_get_dpms(output, &dpms_value);
625 wl_tdm_output_send_dpms(output_resource, dpms_value, ret);
627 wl_tdm_output_send_mode(output_resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
628 wl_tdm_output_send_dpms(output_resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
633 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
635 tdm_private_server *private_server = wl_resource_get_user_data(resource);
636 tdm_private_loop *private_loop = private_server->private_loop;
637 char message[TDM_SERVER_REPLY_MSG_LEN];
639 int len = sizeof(message), size;
642 wl_client_get_credentials(client, NULL, &uid, NULL);
645 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
646 TDM_ERR("%s", message);
648 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
651 size = sizeof(message) - len;
654 wl_client_flush(client);
657 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
658 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
660 strncpy(buffer, m, copylen);
664 buffer[copylen] = '\0';
666 wl_tdm_send_debug_message(resource, buffer);
669 wl_tdm_send_debug_done(resource);
673 _tdm_server_cb_set_client_vblank_fps(struct wl_client *client, struct wl_resource *resource,
674 unsigned int pid, const char *name, unsigned int fps)
676 tdm_error ret = tdm_vblank_set_client_vblank_fps(pid, name, fps);
677 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
679 TDM_INFO("'%s' vblank fps(PID: '%u'): %u", name, pid, fps);
682 static const struct wl_tdm_interface tdm_implementation = {
683 _tdm_server_cb_debug,
684 _tdm_server_cb_create_output,
685 _tdm_server_cb_set_client_vblank_fps,
689 destroy_client(struct wl_resource *resource)
691 tdm_server_client_info *c = NULL, *cc = NULL;
692 struct wl_client *client;
695 client = wl_resource_get_client(resource);
696 TDM_RETURN_IF_FAIL(client != NULL);
698 wl_client_get_credentials(client, &pid, NULL, NULL);
700 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
710 _tdm_server_bind(struct wl_client *client, void *data,
711 uint32_t version, uint32_t id)
713 struct wl_resource *resource;
714 tdm_server_client_info *cinfo;
716 resource = wl_resource_create(client, &wl_tdm_interface, version, id);
718 wl_client_post_no_memory(client);
722 cinfo = calloc(1, sizeof(tdm_server_client_info));
724 wl_client_post_no_memory(client);
725 wl_resource_destroy(resource);
729 LIST_ADDTAIL(&cinfo->link, &client_list);
730 wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
731 _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
733 wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
737 _tdm_getgrnam_r(const char *name)
739 struct group *grp = NULL;
740 struct group *grp_res = NULL;
746 buf_len = sysconf(_SC_GETGR_R_SIZE_MAX);
750 buf = calloc(1, buf_len * sizeof(char));
752 TDM_ERR("creating buffer failed");
756 grp = calloc(1, sizeof(struct group));
758 TDM_ERR("creating group failed");
762 ret = getgrnam_r(name, grp, buf, buf_len, &grp_res);
764 TDM_ERR("getgrnam_r failed errno:%d(%m)", ret);
768 if (grp_res == NULL) {
769 TDM_ERR("finding name:%s group failed", name);
789 _tdm_socket_init(tdm_private_loop *private_loop)
791 const char *dir = NULL;
792 char socket_path[128];
798 dir = getenv("XDG_RUNTIME_DIR");
800 TDM_WRN("getting XDG_RUNTIME_DIR failed");
804 size = snprintf(socket_path, sizeof(socket_path), "%s/%s", dir, "tdm-socket");
805 if (size >= (int)sizeof(socket_path)) {
806 TDM_WRN("too long: %s/tdm-socket", dir);
810 ret = chmod(socket_path, 509);
812 TDM_WRN("changing modes of socket file failed:%s (%m)", socket_path);
816 ret = _tdm_getgrnam_r("root");
818 TDM_WRN("getting uid failed");
823 ret = _tdm_getgrnam_r("display");
825 TDM_WRN("getting gid failed");
830 ret = chown(socket_path, uid, gid);
832 TDM_WRN("changing owner of socket file failed:%s (%m)", socket_path);
838 tdm_server_init(tdm_private_loop *private_loop)
840 tdm_private_server *private_server;
842 TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
843 TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
845 if (private_loop->private_server)
846 return TDM_ERROR_NONE;
848 if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
849 TDM_ERR("createing a tdm-socket failed");
850 return TDM_ERROR_OPERATION_FAILED;
853 _tdm_socket_init(private_loop);
855 private_server = calloc(1, sizeof * private_server);
856 if (!private_server) {
857 TDM_ERR("alloc failed");
858 return TDM_ERROR_OUT_OF_MEMORY;
861 LIST_INITHEAD(&private_server->output_list);
862 LIST_INITHEAD(&private_server->wait_list);
864 if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
865 private_server, _tdm_server_bind)) {
866 TDM_ERR("creating a global resource failed");
867 free(private_server);
868 return TDM_ERROR_OUT_OF_MEMORY;
871 private_server->private_loop = private_loop;
872 private_loop->private_server = private_server;
873 keep_private_server = private_server;
875 LIST_INITHEAD(&client_list);
877 return TDM_ERROR_NONE;
881 tdm_server_deinit(tdm_private_loop *private_loop)
883 tdm_server_output_info *o = NULL, *oo = NULL;
884 tdm_server_wait_info *w = NULL, *ww = NULL;
885 tdm_server_client_info *c = NULL, *cc = NULL;
886 tdm_private_server *private_server;
888 if (!private_loop->private_server)
891 private_server = private_loop->private_server;
893 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
897 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
898 wl_resource_destroy(o->resource);
901 free(private_server);
902 private_loop->private_server = NULL;
903 keep_private_server = NULL;
905 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
912 tdm_server_get_client_name(pid_t pid)
914 tdm_server_client_info *c = NULL;
916 LIST_FOR_EACH_ENTRY(c, &client_list, link) {
918 return (const char*)c->name;