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 } tdm_server_client_info;
92 static tdm_private_server *keep_private_server;
93 static struct list_head client_list;
95 static void destroy_wait(tdm_server_wait_info *wait_info);
98 _tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
100 char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
104 snprintf(name, size, "Unknown");
106 snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
107 h = fopen(proc, "r");
111 len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
113 char *p = strncpy(pname, "NO NAME", sizeof(pname) - 1);
116 pname[len - 1] = '\0';
118 strncpy(name, pname, size - 1);
119 name[size - 1] = '\0';
125 _tdm_server_find_output(tdm_private_server *private_server, const char *name)
127 tdm_private_loop *private_loop = private_server->private_loop;
128 tdm_output *found = NULL;
130 if (!strncasecmp(name, "primary", 7) || !strncasecmp(name, "default", 7))
131 found = tdm_display_get_output(private_loop->dpy, 0, NULL);
136 tdm_display_get_output_count(private_loop->dpy, &count);
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;
144 ret = tdm_output_get_conn_status(output, &status);
145 if (ret || status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
148 ret = tdm_output_get_model_info(output, NULL, &model, NULL);
152 if (strncmp(model, name, TDM_NAME_LEN))
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)
167 tdm_server_wait_info *found;
168 tdm_server_vblank_info *vblank_info;
170 if (!keep_private_server)
173 LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list,
174 tdm_server_wait_info, link, found);
176 TDM_DBG("wait_info(%p) is destroyed", wait_info);
180 if (tdm_debug_module & TDM_DEBUG_VBLANK)
181 TDM_DBG("req_id(%d) done", 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);
187 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
189 destroy_wait(wait_info);
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)
196 _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
200 _tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
201 tdm_value value, void *user_data)
203 tdm_server_output_info *output_info = user_data;
204 struct wl_client *client;
207 TDM_RETURN_IF_FAIL(output_info != NULL);
209 client = wl_resource_get_client(output_info->resource);
210 TDM_RETURN_IF_FAIL(client != NULL);
212 wl_client_get_credentials(client, &pid, NULL, NULL);
214 if (!output_info->watch_output_changes) {
215 TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid);
219 TDM_DBG("send the output changes: %d", (unsigned int)pid);
222 case TDM_OUTPUT_CHANGE_DPMS:
223 wl_tdm_output_send_dpms(output_info->resource, value.u32, TDM_ERROR_NONE);
225 case TDM_OUTPUT_CHANGE_CONNECTION:
226 wl_tdm_output_send_connection(output_info->resource, value.u32, TDM_ERROR_NONE);
234 destroy_wait(tdm_server_wait_info *wait_info)
236 LIST_DEL(&wait_info->link);
241 destroy_vblank_callback(struct wl_resource *resource)
243 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
244 tdm_server_wait_info *w = NULL, *ww = NULL;
246 TDM_RETURN_IF_FAIL(vblank_info != NULL);
248 LIST_DEL(&vblank_info->link);
250 if (vblank_info->vblank)
251 tdm_vblank_destroy(vblank_info->vblank);
253 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
254 if (w->vblank_info == vblank_info)
262 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
264 wl_resource_destroy(resource);
268 _tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
270 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
272 tdm_vblank_set_name(vblank_info->vblank, name);
276 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
278 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
280 tdm_vblank_set_fps(vblank_info->vblank, fps);
284 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
286 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
288 tdm_vblank_set_offset(vblank_info->vblank, offset);
292 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
294 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
296 tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
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)
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;
310 wait_info = calloc(1, sizeof * wait_info);
312 TDM_ERR("alloc failed");
313 ret = TDM_ERROR_OUT_OF_MEMORY;
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);
322 if (tdm_debug_module & TDM_DEBUG_VBLANK)
323 TDM_DBG("req_id(%d) wait", req_id);
325 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
327 ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
329 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
330 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
333 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
337 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
339 destroy_wait(wait_info);
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)
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;
353 wait_info = calloc(1, sizeof * wait_info);
355 TDM_ERR("alloc failed");
356 ret = TDM_ERROR_OUT_OF_MEMORY;
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);
365 if (tdm_debug_module & TDM_DEBUG_VBLANK)
366 TDM_DBG("req_id(%d) wait", req_id);
368 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
370 ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, sequence, _tdm_server_cb_vblank, wait_info);
372 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
373 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
376 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
380 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
382 destroy_wait(wait_info);
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,
396 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
398 wl_resource_destroy(resource);
402 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
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;
409 tdm_server_vblank_info *vblank_info;
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");
420 vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
422 wl_resource_post_no_memory(resource);
423 wl_resource_destroy(vblank_resource);
424 TDM_ERR("tdm_vblank_create failed");
428 vblank_info = calloc(1, sizeof * 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");
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);
443 tdm_vblank_set_resource(vblank, vblank_resource);
445 wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
446 vblank_info, destroy_vblank_callback);
448 wl_tdm_vblank_send_stamp(vblank_info->resource, vblank_info->stamp);
454 _tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
456 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
458 TDM_RETURN_IF_FAIL(output_info != NULL);
460 output_info->watch_output_changes = enable;
464 _tdm_server_output_cb_get_connection(struct wl_client *client, struct wl_resource *resource)
466 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
467 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
470 TDM_RETURN_IF_FAIL(output_info != NULL);
472 ret = tdm_output_get_conn_status(output_info->output, &status);
473 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
475 wl_tdm_output_send_connection(output_info->resource, status, ret);
480 wl_tdm_output_send_connection(output_info->resource, TDM_OUTPUT_CONN_STATUS_DISCONNECTED, ret);
484 _tdm_server_output_cb_get_mode(struct wl_client *client, struct wl_resource *resource)
486 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
487 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
490 TDM_RETURN_IF_FAIL(output_info != NULL);
492 ret = tdm_output_get_conn_status(output_info->output, &status);
493 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
495 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
496 const tdm_output_mode *mode = NULL;
497 unsigned int hdisplay, vdisplay, vrefresh;
499 ret = tdm_output_get_mode(output_info->output, &mode);
500 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
502 hdisplay = (mode) ? mode->hdisplay : 0;
503 vdisplay = (mode) ? mode->vdisplay : 0;
504 vrefresh = (mode) ? mode->vrefresh : 0;
506 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
508 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
513 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
517 _tdm_server_output_cb_get_dpms(struct wl_client *client, struct wl_resource *resource)
519 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
520 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
523 TDM_RETURN_IF_FAIL(output_info != NULL);
525 ret = tdm_output_get_conn_status(output_info->output, &status);
526 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
528 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
529 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
531 ret = tdm_output_get_dpms(output_info->output, &dpms_value);
532 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
534 wl_tdm_output_send_dpms(output_info->resource, dpms_value, ret);
536 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
541 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, ret);
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,
554 destroy_output_callback(struct wl_resource *resource)
556 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
557 tdm_server_vblank_info *v = NULL, *vv = NULL;
559 TDM_RETURN_IF_FAIL(output_info != NULL);
561 LIST_DEL(&output_info->link);
563 tdm_output_remove_change_handler(output_info->output,
564 _tdm_server_cb_output_change, output_info);
566 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
567 wl_resource_destroy(v->resource);
574 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
575 const char *name, uint32_t id)
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;
581 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
584 output = _tdm_server_find_output(private_server, name);
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);
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");
601 output_info = calloc(1, sizeof * output_info);
603 wl_resource_post_no_memory(resource);
604 wl_resource_destroy(output_resource);
605 TDM_ERR("alloc failed");
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);
615 tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
617 wl_resource_set_implementation(output_resource, &tdm_output_implementation,
618 output_info, destroy_output_callback);
620 ret = tdm_output_get_conn_status(output, &status);
621 wl_tdm_output_send_connection(output_resource, status, ret);
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;
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);
634 ret = tdm_output_get_dpms(output, &dpms_value);
635 wl_tdm_output_send_dpms(output_resource, dpms_value, ret);
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);
643 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
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];
649 int len = sizeof(message), size;
652 wl_client_get_credentials(client, NULL, &uid, NULL);
655 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
656 TDM_ERR("%s", message);
658 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
661 size = sizeof(message) - len;
664 wl_client_flush(client);
667 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
668 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
670 strncpy(buffer, m, copylen);
674 buffer[copylen] = '\0';
676 wl_tdm_send_debug_message(resource, buffer);
679 wl_tdm_send_debug_done(resource);
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)
686 tdm_error ret = tdm_vblank_set_client_vblank_fps(pid, name, fps);
687 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
689 TDM_INFO("'%s' vblank fps(PID: '%u'): %u", name, pid, fps);
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,
699 destroy_client(struct wl_resource *resource)
701 tdm_server_client_info *c = NULL, *cc = NULL;
702 struct wl_client *client;
705 client = wl_resource_get_client(resource);
706 TDM_RETURN_IF_FAIL(client != NULL);
708 wl_client_get_credentials(client, &pid, NULL, NULL);
710 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
720 _tdm_server_bind(struct wl_client *client, void *data,
721 uint32_t version, uint32_t id)
723 struct wl_resource *resource;
724 tdm_server_client_info *cinfo;
726 resource = wl_resource_create(client, &wl_tdm_interface, version, id);
728 wl_client_post_no_memory(client);
732 cinfo = calloc(1, sizeof(tdm_server_client_info));
734 wl_client_post_no_memory(client);
735 wl_resource_destroy(resource);
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);
743 wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
747 _tdm_getgrnam_r(const char *name)
749 struct group *grp = NULL;
750 struct group *grp_res = NULL;
756 buf_len = sysconf(_SC_GETGR_R_SIZE_MAX);
760 buf = calloc(1, buf_len * sizeof(char));
762 TDM_ERR("creating buffer failed");
766 grp = calloc(1, sizeof(struct group));
768 TDM_ERR("creating group failed");
772 ret = getgrnam_r(name, grp, buf, buf_len, &grp_res);
774 TDM_ERR("getgrnam_r failed errno:%d(%m)", ret);
778 if (grp_res == NULL) {
779 TDM_ERR("finding name:%s group failed", name);
799 _tdm_socket_init(tdm_private_loop *private_loop)
801 const char *dir = NULL;
802 char socket_path[TDM_NAME_LEN * 2];
807 dir = getenv("XDG_RUNTIME_DIR");
809 TDM_WRN("getting XDG_RUNTIME_DIR failed");
813 strncpy(socket_path, dir, TDM_NAME_LEN - 1);
814 socket_path[TDM_NAME_LEN - 1] = '\0';
816 strncat(socket_path, "/tdm-socket", 11);
817 socket_path[TDM_NAME_LEN + 10] = '\0';
819 ret = chmod(socket_path, 509);
821 TDM_WRN("changing modes of socket file failed:%s (%m)", socket_path);
825 ret = _tdm_getgrnam_r("root");
827 TDM_WRN("getting uid failed");
832 ret = _tdm_getgrnam_r("display");
834 TDM_WRN("getting gid failed");
839 ret = chown(socket_path, uid, gid);
841 TDM_WRN("changing owner of socket file failed:%s (%m)", socket_path);
847 tdm_server_init(tdm_private_loop *private_loop)
849 tdm_private_server *private_server;
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);
854 if (private_loop->private_server)
855 return TDM_ERROR_NONE;
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;
862 _tdm_socket_init(private_loop);
864 private_server = calloc(1, sizeof * private_server);
865 if (!private_server) {
866 TDM_ERR("alloc failed");
867 return TDM_ERROR_OUT_OF_MEMORY;
870 LIST_INITHEAD(&private_server->output_list);
871 LIST_INITHEAD(&private_server->wait_list);
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;
880 private_server->private_loop = private_loop;
881 private_loop->private_server = private_server;
882 keep_private_server = private_server;
884 LIST_INITHEAD(&client_list);
886 return TDM_ERROR_NONE;
890 tdm_server_deinit(tdm_private_loop *private_loop)
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;
897 if (!private_loop->private_server)
900 private_server = private_loop->private_server;
902 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
906 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
907 wl_resource_destroy(o->resource);
910 free(private_server);
911 private_loop->private_server = NULL;
912 keep_private_server = NULL;
914 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
921 tdm_server_get_client_name(pid_t pid)
923 tdm_server_client_info *c = NULL;
925 LIST_FOR_EACH_ENTRY(c, &client_list, link) {
927 return (const char*)c->name;