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 LIST_DEL(&vblank_info->link);
247 if (vblank_info->vblank)
248 tdm_vblank_destroy(vblank_info->vblank);
250 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
251 if (w->vblank_info == vblank_info)
259 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
261 wl_resource_destroy(resource);
265 _tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
267 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
269 tdm_vblank_set_name(vblank_info->vblank, name);
273 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
275 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
277 tdm_vblank_set_fps(vblank_info->vblank, fps);
281 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
283 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
285 tdm_vblank_set_offset(vblank_info->vblank, offset);
289 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
291 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
293 tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
297 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
298 uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
300 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
301 tdm_server_output_info *output_info = vblank_info->output_info;
302 tdm_private_server *private_server = output_info->private_server;
303 tdm_server_wait_info *wait_info;
304 unsigned int enable_fake = 0;
307 TDM_TRACE_COUNT(ServerWaitVBlank, req_id);
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;
320 if (tdm_debug_module & TDM_DEBUG_VBLANK)
321 TDM_DBG("req_id(%d) wait", req_id);
323 ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
325 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
326 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
329 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
333 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
335 destroy_wait(wait_info);
339 _tdm_server_vblank_cb_wait_vblank_seq(struct wl_client *client, struct wl_resource *resource,
340 uint32_t sequence, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
342 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
343 tdm_server_output_info *output_info = vblank_info->output_info;
344 tdm_private_server *private_server = output_info->private_server;
345 tdm_server_wait_info *wait_info;
346 unsigned int enable_fake = 0;
349 TDM_TRACE_COUNT(ServerWaitVBlank, req_id);
351 wait_info = calloc(1, sizeof * wait_info);
353 TDM_ERR("alloc failed");
354 ret = TDM_ERROR_OUT_OF_MEMORY;
358 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
359 wait_info->vblank_info = vblank_info;
360 wait_info->req_id = req_id;
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;
437 tdm_vblank_set_resource(vblank, vblank_resource);
439 wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
440 vblank_info, destroy_vblank_callback);
446 _tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
448 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
450 TDM_RETURN_IF_FAIL(output_info != NULL);
452 output_info->watch_output_changes = enable;
456 _tdm_server_output_cb_get_connection(struct wl_client *client, struct wl_resource *resource)
458 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
459 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
462 TDM_RETURN_IF_FAIL(output_info != NULL);
464 ret = tdm_output_get_conn_status(output_info->output, &status);
465 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
467 wl_tdm_output_send_connection(output_info->resource, status, ret);
472 wl_tdm_output_send_connection(output_info->resource, TDM_OUTPUT_CONN_STATUS_DISCONNECTED, ret);
476 _tdm_server_output_cb_get_mode(struct wl_client *client, struct wl_resource *resource)
478 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
479 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
482 TDM_RETURN_IF_FAIL(output_info != NULL);
484 ret = tdm_output_get_conn_status(output_info->output, &status);
485 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
487 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
488 const tdm_output_mode *mode = NULL;
489 unsigned int hdisplay, vdisplay, vrefresh;
491 ret = tdm_output_get_mode(output_info->output, &mode);
492 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
494 hdisplay = (mode) ? mode->hdisplay : 0;
495 vdisplay = (mode) ? mode->vdisplay : 0;
496 vrefresh = (mode) ? mode->vrefresh : 0;
498 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
500 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
505 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
509 _tdm_server_output_cb_get_dpms(struct wl_client *client, struct wl_resource *resource)
511 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
512 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
515 TDM_RETURN_IF_FAIL(output_info != NULL);
517 ret = tdm_output_get_conn_status(output_info->output, &status);
518 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
520 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
521 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
523 ret = tdm_output_get_dpms(output_info->output, &dpms_value);
524 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
526 wl_tdm_output_send_dpms(output_info->resource, dpms_value, ret);
528 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
533 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, ret);
536 static const struct wl_tdm_output_interface tdm_output_implementation = {
537 _tdm_server_output_cb_destroy,
538 _tdm_server_output_cb_create_vblank,
539 _tdm_server_output_cb_watch_output_changes,
540 _tdm_server_output_cb_get_connection,
541 _tdm_server_output_cb_get_mode,
542 _tdm_server_output_cb_get_dpms,
546 destroy_output_callback(struct wl_resource *resource)
548 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
549 tdm_server_vblank_info *v = NULL, *vv = NULL;
551 TDM_RETURN_IF_FAIL(output_info != NULL);
553 LIST_DEL(&output_info->link);
555 tdm_output_remove_change_handler(output_info->output,
556 _tdm_server_cb_output_change, output_info);
558 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
559 wl_resource_destroy(v->resource);
566 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
567 const char *name, uint32_t id)
569 tdm_private_server *private_server = wl_resource_get_user_data(resource);
570 tdm_server_output_info *output_info;
571 struct wl_resource *output_resource = NULL;
573 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
576 output = _tdm_server_find_output(private_server, name);
578 TDM_ERR("There is no '%s' output", name);
579 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
580 "There is no '%s' output", name);
585 wl_resource_create(client, &wl_tdm_output_interface,
586 wl_resource_get_version(resource), id);
587 if (!output_resource) {
588 wl_resource_post_no_memory(resource);
589 TDM_ERR("wl_resource_create failed");
593 output_info = calloc(1, sizeof * output_info);
595 wl_resource_post_no_memory(resource);
596 wl_resource_destroy(output_resource);
597 TDM_ERR("alloc failed");
601 LIST_ADDTAIL(&output_info->link, &private_server->output_list);
602 output_info->private_server = private_server;
603 output_info->resource = output_resource;
604 output_info->output = output;
605 LIST_INITHEAD(&output_info->vblank_list);
607 tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
609 wl_resource_set_implementation(output_resource, &tdm_output_implementation,
610 output_info, destroy_output_callback);
612 ret = tdm_output_get_conn_status(output, &status);
613 wl_tdm_output_send_connection(output_resource, status, ret);
615 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
616 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
617 const tdm_output_mode *mode = NULL;
618 unsigned int hdisplay, vdisplay, vrefresh;
620 ret = tdm_output_get_mode(output, &mode);
621 hdisplay = (mode) ? mode->hdisplay : 0;
622 vdisplay = (mode) ? mode->vdisplay : 0;
623 vrefresh = (mode) ? mode->vrefresh : 0;
624 wl_tdm_output_send_mode(output_resource, hdisplay, vdisplay, vrefresh, ret);
626 ret = tdm_output_get_dpms(output, &dpms_value);
627 wl_tdm_output_send_dpms(output_resource, dpms_value, ret);
629 wl_tdm_output_send_mode(output_resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
630 wl_tdm_output_send_dpms(output_resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
635 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
637 tdm_private_server *private_server = wl_resource_get_user_data(resource);
638 tdm_private_loop *private_loop = private_server->private_loop;
639 char message[TDM_SERVER_REPLY_MSG_LEN];
641 int len = sizeof(message), size;
644 wl_client_get_credentials(client, NULL, &uid, NULL);
647 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
648 TDM_ERR("%s", message);
650 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
653 size = sizeof(message) - len;
656 wl_client_flush(client);
659 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
660 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
662 strncpy(buffer, m, copylen);
666 buffer[copylen] = '\0';
668 wl_tdm_send_debug_message(resource, buffer);
671 wl_tdm_send_debug_done(resource);
675 _tdm_server_cb_set_client_vblank_fps(struct wl_client *client, struct wl_resource *resource,
676 unsigned int pid, const char *name, unsigned int fps)
678 tdm_error ret = tdm_vblank_set_client_vblank_fps(pid, name, fps);
679 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
681 TDM_INFO("'%s' vblank fps(PID: '%u'): %u", name, pid, fps);
684 static const struct wl_tdm_interface tdm_implementation = {
685 _tdm_server_cb_debug,
686 _tdm_server_cb_create_output,
687 _tdm_server_cb_set_client_vblank_fps,
691 destroy_client(struct wl_resource *resource)
693 tdm_server_client_info *c = NULL, *cc = NULL;
694 struct wl_client *client;
697 client = wl_resource_get_client(resource);
698 TDM_RETURN_IF_FAIL(client != NULL);
700 wl_client_get_credentials(client, &pid, NULL, NULL);
702 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
712 _tdm_server_bind(struct wl_client *client, void *data,
713 uint32_t version, uint32_t id)
715 struct wl_resource *resource;
716 tdm_server_client_info *cinfo;
718 resource = wl_resource_create(client, &wl_tdm_interface, version, id);
720 wl_client_post_no_memory(client);
724 cinfo = calloc(1, sizeof(tdm_server_client_info));
726 wl_client_post_no_memory(client);
727 wl_resource_destroy(resource);
731 LIST_ADDTAIL(&cinfo->link, &client_list);
732 wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
733 _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
735 wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
739 _tdm_getgrnam_r(const char *name)
741 struct group *grp = NULL;
742 struct group *grp_res = NULL;
748 buf_len = sysconf(_SC_GETGR_R_SIZE_MAX);
752 buf = calloc(1, buf_len * sizeof(char));
754 TDM_ERR("creating buffer failed");
758 grp = calloc(1, sizeof(struct group));
760 TDM_ERR("creating group failed");
764 ret = getgrnam_r(name, grp, buf, buf_len, &grp_res);
766 TDM_ERR("getgrnam_r failed errno:%d(%m)", ret);
770 if (grp_res == NULL) {
771 TDM_ERR("finding name:%s group failed", name);
791 _tdm_socket_init(tdm_private_loop *private_loop)
793 const char *dir = NULL;
794 char socket_path[TDM_NAME_LEN * 2];
799 dir = getenv("XDG_RUNTIME_DIR");
801 TDM_WRN("getting XDG_RUNTIME_DIR failed");
805 strncpy(socket_path, dir, TDM_NAME_LEN - 1);
806 socket_path[TDM_NAME_LEN - 1] = '\0';
808 strncat(socket_path, "/tdm-socket", 11);
809 socket_path[TDM_NAME_LEN + 10] = '\0';
811 ret = chmod(socket_path, 509);
813 TDM_WRN("changing modes of socket file failed:%s (%m)", socket_path);
817 ret = _tdm_getgrnam_r("root");
819 TDM_WRN("getting uid failed");
824 ret = _tdm_getgrnam_r("display");
826 TDM_WRN("getting gid failed");
831 ret = chown(socket_path, uid, gid);
833 TDM_WRN("changing owner of socket file failed:%s (%m)", socket_path);
839 tdm_server_init(tdm_private_loop *private_loop)
841 tdm_private_server *private_server;
843 TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
844 TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
846 if (private_loop->private_server)
847 return TDM_ERROR_NONE;
849 if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
850 TDM_ERR("createing a tdm-socket failed");
851 return TDM_ERROR_OPERATION_FAILED;
854 _tdm_socket_init(private_loop);
856 private_server = calloc(1, sizeof * private_server);
857 if (!private_server) {
858 TDM_ERR("alloc failed");
859 return TDM_ERROR_OUT_OF_MEMORY;
862 LIST_INITHEAD(&private_server->output_list);
863 LIST_INITHEAD(&private_server->wait_list);
865 if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
866 private_server, _tdm_server_bind)) {
867 TDM_ERR("creating a global resource failed");
868 free(private_server);
869 return TDM_ERROR_OUT_OF_MEMORY;
872 private_server->private_loop = private_loop;
873 private_loop->private_server = private_server;
874 keep_private_server = private_server;
876 LIST_INITHEAD(&client_list);
878 return TDM_ERROR_NONE;
882 tdm_server_deinit(tdm_private_loop *private_loop)
884 tdm_server_output_info *o = NULL, *oo = NULL;
885 tdm_server_wait_info *w = NULL, *ww = NULL;
886 tdm_server_client_info *c = NULL, *cc = NULL;
887 tdm_private_server *private_server;
889 if (!private_loop->private_server)
892 private_server = private_loop->private_server;
894 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
898 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
899 wl_resource_destroy(o->resource);
902 free(private_server);
903 private_loop->private_server = NULL;
904 keep_private_server = NULL;
906 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
913 tdm_server_get_client_name(pid_t pid)
915 tdm_server_client_info *c = NULL;
917 LIST_FOR_EACH_ENTRY(c, &client_list, link) {
919 return (const char*)c->name;