Merge branch 'tizen' into sandbox/cyeon/devel
[platform/core/uifw/libtdm.git] / tools / 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 <boram1288.park@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
58         struct {
59                 int   done;
60                 char  message[TDM_SERVER_REPLY_MSG_LEN];
61                 char *m;
62                 int   len;
63                 int  *l;
64         } debug;
65 } tdm_monitor_info;
66
67 static tdm_monitor_info td_info;
68
69 static void
70 _tdm_monitor_cb_debug_message(void *data, struct wl_tdm *wl_tdm, const char *message)
71 {
72         tdm_monitor_info *info = data;
73         TDM_SNPRINTF(info->debug.m, info->debug.l, "%s", message);
74 }
75
76 static void
77 _tdm_monitor_cb_debug_done(void *data, struct wl_tdm *wl_tdm)
78 {
79         tdm_monitor_info *info = data;
80         info->debug.done = 1;
81         *(info->debug.m) = '\0';
82         printf("%s", info->debug.message);
83 }
84
85 static const struct wl_tdm_listener  tdm_monitor_listener = {
86         _tdm_monitor_cb_debug_message,
87         _tdm_monitor_cb_debug_done,
88 };
89
90 static void
91 _tdm_monitor_cb_global(void *data, struct wl_registry *registry,
92                                            uint32_t name, const char *interface,
93                                            uint32_t version)
94 {
95         tdm_monitor_info *info = data;
96
97         if (strncmp(interface, "wl_tdm", 6) == 0) {
98                 info->tdm = wl_registry_bind(registry, name, &wl_tdm_interface, version);
99                 exit_if_fail(info->tdm != NULL);
100                 wl_tdm_add_listener(info->tdm, &tdm_monitor_listener, info);
101                 wl_display_flush(info->display);
102         }
103 }
104
105 static void
106 _tdm_monitor_cb_global_remove(void *data, struct wl_registry *registry, uint32_t name)
107 {
108 }
109
110 static const struct wl_registry_listener tdm_monitor_registry_listener = {
111         _tdm_monitor_cb_global,
112         _tdm_monitor_cb_global_remove
113 };
114
115 int
116 main(int argc, char ** argv)
117 {
118         tdm_monitor_info *info = &td_info;
119         int i, ret = 0;
120         char cwd[1024];
121         char options[1024];
122         int bufsize = sizeof(options);
123         char *str_buf = options;
124         int *len_buf = &bufsize;
125         const char *xdg;
126
127         xdg = (const char*)getenv("XDG_RUNTIME_DIR");
128         if (!xdg) {
129                 char buf[32];
130                 snprintf(buf, sizeof(buf), "/run");
131
132                 ret = setenv("XDG_RUNTIME_DIR", (const char*)buf, 1);
133                 exit_if_fail(ret == 0);
134         }
135
136         info->display = wl_display_connect("tdm-socket");
137         exit_if_fail(info->display != NULL);
138
139         info->registry = wl_display_get_registry(info->display);
140         exit_if_fail(info->registry != NULL);
141
142         wl_registry_add_listener(info->registry,
143                                                          &tdm_monitor_registry_listener, info);
144         wl_display_roundtrip(info->display);
145         exit_if_fail(info->tdm != NULL);
146
147         TDM_SNPRINTF(str_buf, len_buf, "%d ", getpid());
148
149         if (!getcwd(cwd, sizeof(cwd)))
150                 snprintf(cwd, sizeof(cwd), "/tmp");
151         TDM_SNPRINTF(str_buf, len_buf, "%s ", cwd);
152
153         for (i = 0; i < argc; i++)
154                 TDM_SNPRINTF(str_buf, len_buf, "%s ", argv[i]);
155
156         info->debug.done = 0;
157         info->debug.message[0] = '\0';
158         info->debug.len = sizeof(info->debug.message);
159         info->debug.m = info->debug.message;
160         info->debug.l = &info->debug.len;
161
162         wl_tdm_debug(info->tdm, options);
163
164         while (!info->debug.done && ret >= 0)
165                 ret = wl_display_dispatch(info->display);
166
167         wl_tdm_destroy(info->tdm);
168         wl_registry_destroy(info->registry);
169         wl_display_disconnect(info->display);
170
171         return 0;
172 }