rename tdm-dbg to tdm-monitor
[platform/core/uifw/libtdm.git] / src / tdm_monitor_server.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 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <stdint.h>
40
41 #include "tdm.h"
42 #include "tdm_private.h"
43 #include "tdm_helper.h"
44 #include "tdm_log.h"
45
46 #define TDM_DBG_SERVER_ARGS_MAX         32
47
48 static void _tdm_monitor_server_usage(char *app_name, char *reply, int *len);
49
50 static void
51 _tdm_monitor_server_query(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
52 {
53         tdm_helper_get_display_information(dpy, reply, len);
54 }
55
56 static void
57 _tdm_monitor_server_dpms(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
58 {
59         tdm_output *output;
60         int output_idx, dpms_value;
61         char *arg;
62         char *end;
63         tdm_error ret;
64
65         if (argc < 3) {
66                 _tdm_monitor_server_usage(argv[0], reply, len);
67                 return;
68         }
69
70         arg = argv[2];
71         output_idx = strtol(arg, &end, 10);
72         if (*end != ':') {
73                 TDM_SNPRINTF(reply, len, "failed: no onoff value\n");
74                 return;
75         }
76
77         arg = end + 1;
78         dpms_value = strtol(arg, &end, 10);
79
80         output = tdm_display_get_output(dpy, output_idx, &ret);
81         TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE && output != NULL);
82
83         ret = tdm_output_set_dpms(output, dpms_value);
84         TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
85
86         TDM_SNPRINTF(reply, len, "done: DPMS %s\n", tdm_dpms_str(dpms_value));
87 }
88
89 static void
90 _tdm_monitor_server_debug(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
91 {
92         int level;
93         char *arg;
94         char *end;
95
96         if (argc < 3) {
97                 _tdm_monitor_server_usage(argv[0], reply, len);
98                 return;
99         }
100
101         arg = argv[2];
102         level = strtol(arg, &end, 10);
103
104         tdm_log_set_debug_level(level);
105         TDM_SNPRINTF(reply, len, "debug level: %d\n", level);
106
107         if (*end == '@') {
108                 char *arg = end + 1;
109
110                 tdm_display_enable_debug_module((const char *)arg);
111
112                 TDM_SNPRINTF(reply, len, "debugging... '%s'\n", arg);
113         }
114 }
115
116 static void
117 _tdm_monitor_server_log_path(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
118 {
119         static int old_stdout = -1;
120         char fd_name[TDM_PATH_LEN];
121         char *path;
122
123         if (argc < 3) {
124                 _tdm_monitor_server_usage(argv[0], reply, len);
125                 return;
126         }
127
128         if (old_stdout == -1)
129                 old_stdout = dup(STDOUT_FILENO);
130
131         path = argv[2];
132         TDM_DBG_RETURN_IF_FAIL(path != NULL);
133
134         tdm_log_enable_dlog(0);
135
136         if (!strncmp(path, "dlog", 4)) {
137                 tdm_log_enable_dlog(1);
138                 goto done;
139         } else if (!strncmp(path, "console", 7))
140                 snprintf(fd_name, TDM_PATH_LEN, "/proc/%d/fd/1", pid);
141         else {
142                 if (path[0] == '/')
143                         snprintf(fd_name, TDM_PATH_LEN, "%s", path);
144                 else {
145                         if (cwd)
146                                 snprintf(fd_name, TDM_PATH_LEN, "%s/%s", cwd, path);
147                         else
148                                 snprintf(fd_name, TDM_PATH_LEN, "%s", path);
149                 }
150                 tdm_log_enable_color(0);
151         }
152
153         if (tdm_display_enable_path((const char*)fd_name) != TDM_ERROR_NONE) {
154                 TDM_SNPRINTF(reply, len, "failed: '%s'\n", path);
155                 return;
156         }
157
158 done:
159         TDM_SNPRINTF(reply, len, "log path: '%s'\n", path);
160 }
161
162 static void
163 _tdm_monitor_server_prop(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
164 {
165         tdm_output *output;
166         tdm_output *layer = NULL;
167         int output_idx, layer_idx = -1;
168         int cnt, i, done = 0;
169         tdm_value value;
170         char temp[TDM_PATH_LEN];
171         char *prop_name;
172         char *arg;
173         char *end;
174         tdm_error ret;
175         const tdm_prop *props;
176
177         if (argc < 3) {
178                 _tdm_monitor_server_usage(argv[0], reply, len);
179                 return;
180         }
181
182         snprintf(temp, TDM_PATH_LEN, "%s", argv[2]);
183         arg = temp;
184
185         output_idx = strtol(arg, &end, 10);
186         if (*end == ',') {
187                 arg = end + 1;
188                 layer_idx = strtol(arg, &end, 10);
189         }
190
191         if (*end != ':') {
192                 TDM_SNPRINTF(reply, len, "failed: no prop_name\n");
193                 return;
194         }
195
196         arg = end + 1;
197         prop_name = strtok_r(arg, ",", &end);
198
199         if (*end == '\0') {
200                 TDM_SNPRINTF(reply, len, "failed: no value\n");
201                 return;
202         }
203
204         arg = strtok_r(NULL, TDM_DELIM, &end);
205         value.u32 = strtol(arg, &end, 10);
206
207         output = tdm_display_get_output(dpy, output_idx, &ret);
208         TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE && output != NULL);
209
210         if (layer_idx != -1) {
211                 layer = tdm_output_get_layer(output, layer_idx, &ret);
212                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE && layer != NULL);
213         }
214
215         if (layer) {
216                 ret = tdm_layer_get_available_properties(layer, &props, &cnt);
217                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
218
219                 for (i = 0; i < cnt; i++) {
220                         if (!strncmp(props[i].name, prop_name, TDM_NAME_LEN)) {
221                                 ret = tdm_layer_set_property(layer, props[i].id, value);
222                                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
223                                 done = 1;
224                                 break;
225                         }
226                 }
227         } else {
228                 ret = tdm_output_get_available_properties(output, &props, &cnt);
229                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
230
231                 for (i = 0; i < cnt; i++) {
232                         if (!strncmp(props[i].name, prop_name, TDM_NAME_LEN)) {
233                                 ret = tdm_output_set_property(output, props[i].id, value);
234                                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
235                                 done = 1;
236                                 break;
237                         }
238                 }
239         }
240
241         if (done)
242                 TDM_SNPRINTF(reply, len, "done: %s:%d \n", prop_name, value.u32);
243         else
244                 TDM_SNPRINTF(reply, len, "no '%s' propperty \n", prop_name);
245 }
246
247 static void
248 _tdm_monitor_server_dump(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
249 {
250         if (argc < 3) {
251                 _tdm_monitor_server_usage(argv[0], reply, len);
252                 return;
253         }
254
255         tdm_display_enable_dump((const char*)argv[2]);
256
257         TDM_SNPRINTF(reply, len, "%s done\n", argv[2]);
258 }
259
260 static struct {
261         const char *opt;
262         void (*func)(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy);
263         const char *desc;
264         const char *arg;
265         const char *ex;
266 } option_proc[] = {
267         {
268                 "info", _tdm_monitor_server_query,
269                 "show tdm output, layer information", NULL, NULL
270         },
271         {
272                 "dpms", _tdm_monitor_server_dpms,
273                 "set output dpms", "<output_idx>:<dpms>", "0:3 or 0:0"
274         },
275         {
276                 "debug", _tdm_monitor_server_debug,
277                 "set the debug level and modules(none,mutex,buffer,thread,vblank)",
278                 "<level>[@<module1>[,<module2>]]",
279                 NULL
280         },
281         {
282                 "log_path", _tdm_monitor_server_log_path,
283                 "set the log path (console,dlog,filepath)",
284                 "<path>",
285                 "console"
286         },
287         {
288                 "prop", _tdm_monitor_server_prop,
289                 "set the property of a output or a layer",
290                 "<output_idx>[,<layer_idx>]:<prop_name>,<value>",
291                 NULL
292         },
293         {
294                 "dump", _tdm_monitor_server_dump,
295                 "dump buffers (type: layer, pp, capture, none)",
296                 "<object_type1>[,<object_type2>[,...]]",
297                 NULL
298         },
299 };
300
301 static void
302 _tdm_monitor_server_usage(char *app_name, char *reply, int *len)
303 {
304         int opt_size = sizeof(option_proc) / sizeof(option_proc[0]);
305         int i;
306
307         TDM_SNPRINTF(reply, len, "usage: %s \n\n", app_name);
308
309         for (i = 0; i < opt_size; i++) {
310                 TDM_SNPRINTF(reply, len, "\t-%s\t%s\n", option_proc[i].opt, option_proc[i].desc);
311                 if (option_proc[i].arg)
312                         TDM_SNPRINTF(reply, len, "\t\t  %s\n", option_proc[i].arg);
313                 if (option_proc[i].ex)
314                         TDM_SNPRINTF(reply, len, "\t\t  ex) %s\n", option_proc[i].ex);
315                 TDM_SNPRINTF(reply, len, "\n");
316         }
317 }
318
319 static void
320 _tdm_monitor_server_command(unsigned int pid, char *cwd, tdm_display *dpy, int argc, char *argv[], char *reply, int *len)
321 {
322         int opt_size = sizeof(option_proc) / sizeof(option_proc[0]);
323         int i;
324
325         if (argc < 2) {
326                 _tdm_monitor_server_usage(argv[0], reply, len);
327                 return;
328         }
329
330         for (i = 0; i < opt_size; i++) {
331                 if (argv[1][0] == '-' && !strncmp(argv[1] + 1, option_proc[i].opt, 32)) {
332                         if (option_proc[i].func) {
333                                 option_proc[i].func(pid, cwd, argc, argv, reply, len, dpy);
334                                 return;
335                         } else {
336                                 TDM_SNPRINTF(reply, len, "'%s' not implemented.\n", argv[1]);
337                                 return;
338                         }
339                 }
340         }
341
342         _tdm_monitor_server_usage(argv[0], reply, len);
343         return;
344 }
345
346
347 INTERN void
348 tdm_monitor_server_command(tdm_display *dpy, const char *options, char *reply, int *len)
349 {
350         unsigned int pid;
351         char cwd[1024];
352         int argc = 0;
353         char *argv[TDM_DBG_SERVER_ARGS_MAX] = {0,};
354         char temp[1024];
355         char *arg;
356         char *end = NULL, *e;
357
358         snprintf(temp, sizeof(temp), "%s", options);
359
360         arg = strtok_r(temp, " ", &end);
361         if (!arg) {
362                 TDM_SNPRINTF(reply, len, "no pid for tdm-monitor");
363                 return;
364         }
365         pid = strtol(arg, &e, 10);
366
367         arg = strtok_r(NULL, " ", &end);
368         if (!arg) {
369                 TDM_SNPRINTF(reply, len, "no cwd for tdm-monitor");
370                 return;
371         }
372         snprintf(cwd, sizeof(cwd), "%s", arg);
373
374         TDM_DBG("pid(%d) cwd(%s)", pid, cwd);
375
376         argv[argc] = strtok_r(NULL, " ", &end);
377         while (argv[argc]) {
378                 argc++;
379                 if (argc == TDM_DBG_SERVER_ARGS_MAX) {
380                         TDM_SNPRINTF(reply, len, "too many arguments for tdm-monitor");
381                         break;
382                 }
383                 argv[argc] = strtok_r(NULL, " ", &end);
384         }
385
386         _tdm_monitor_server_command(pid, cwd, dpy, argc, argv, reply, len);
387 }