not create the reduntant directory when done
[platform/core/uifw/libtdm.git] / client / tdm_monitor.c
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
7  * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8  *          JinYoung Jeon <jy0.jeon@samsung.com>,
9  *          Taeheon Kim <th908.kim@samsung.com>,
10  *          YoungJun Cho <yj44.cho@samsung.com>,
11  *          SooChan Lim <sc1.lim@samsung.com>,
12  *          Boram Park <sc1.lim@samsung.com>
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a
15  * copy of this software and associated documentation files (the
16  * "Software"), to deal in the Software without restriction, including
17  * without limitation the rights to use, copy, modify, merge, publish,
18  * distribute, sub license, and/or sell copies of the Software, and to
19  * permit persons to whom the Software is furnished to do so, subject to
20  * the following conditions:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33  *
34 **************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <tdm_log.h>
41 #include <tdm_macro.h>
42 #include <tdm_list.h>
43 #include <tdm-client-protocol.h>
44
45 #undef exit_if_fail
46 #define exit_if_fail(cond) { \
47         if (!(cond)) { \
48                 printf("'%s' failed. (line:%d)\n", #cond, __LINE__); \
49                 exit(0); \
50         } \
51 }
52
53 typedef struct _tdm_monitor_info {
54         struct wl_display *display;
55         struct wl_registry *registry;
56         struct wl_tdm *tdm;
57 } tdm_monitor_info;
58
59 static tdm_monitor_info td_info;
60 static int done;
61
62 static void
63 _tdm_monitor_cb_debug_done(void *data, struct wl_tdm *wl_tdm, const char *message)
64 {
65         printf("%s", message);
66
67         done = 1;
68 }
69
70 static const struct wl_tdm_listener  tdm_monitor_listener = {
71         _tdm_monitor_cb_debug_done,
72 };
73
74 static void
75 _tdm_monitor_cb_global(void *data, struct wl_registry *registry,
76                                            uint32_t name, const char *interface,
77                                            uint32_t version)
78 {
79         tdm_monitor_info *info = data;
80
81         if (strncmp(interface, "wl_tdm", 6) == 0) {
82                 info->tdm = wl_registry_bind(registry, name, &wl_tdm_interface, version);
83                 exit_if_fail(info->tdm != NULL);
84                 wl_tdm_add_listener(info->tdm, &tdm_monitor_listener, info);
85                 wl_display_flush(info->display);
86         }
87 }
88
89 static void
90 _tdm_monitor_cb_global_remove(void *data, struct wl_registry *registry, uint32_t name)
91 {
92 }
93
94 static const struct wl_registry_listener tdm_monitor_registry_listener = {
95         _tdm_monitor_cb_global,
96         _tdm_monitor_cb_global_remove
97 };
98
99 int
100 main(int argc, char ** argv)
101 {
102         tdm_monitor_info *info = &td_info;
103         int i, ret = 0;
104         char cwd[1024];
105         char options[1024];
106         int bufsize = sizeof(options);
107         char *str_buf = options;
108         int *len_buf = &bufsize;
109         const char *xdg;
110
111         xdg = (const char*)getenv("XDG_RUNTIME_DIR");
112         if (!xdg) {
113                 char buf[32];
114                 snprintf(buf, sizeof(buf), "/run");
115
116                 ret = setenv("XDG_RUNTIME_DIR", (const char*)buf, 1);
117                 exit_if_fail(ret == 0);
118         }
119
120         info->display = wl_display_connect("tdm-socket");
121         exit_if_fail(info->display != NULL);
122
123         info->registry = wl_display_get_registry(info->display);
124         exit_if_fail(info->registry != NULL);
125
126         wl_registry_add_listener(info->registry,
127                                                          &tdm_monitor_registry_listener, info);
128         wl_display_roundtrip(info->display);
129         exit_if_fail(info->tdm != NULL);
130
131         TDM_SNPRINTF(str_buf, len_buf, "%d ", getpid());
132
133         if (!getcwd(cwd, sizeof(cwd)))
134                 snprintf(cwd, sizeof(cwd), "/tmp");
135         TDM_SNPRINTF(str_buf, len_buf, "%s ", cwd);
136
137         for (i = 0; i < argc; i++)
138                 TDM_SNPRINTF(str_buf, len_buf, "%s ", argv[i]);
139
140         done = 0;
141
142         wl_tdm_debug(info->tdm, options);
143
144         while (!done && ret >= 0)
145                 ret = wl_display_dispatch(info->display);
146
147         if (info->tdm)
148                 wl_tdm_destroy(info->tdm);
149         if (info->registry)
150                 wl_registry_destroy(info->registry);
151         if (info->display)
152                 wl_display_disconnect(info->display);
153
154         return 0;
155 }