impl for using dump path
[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(dpy, (const char*)argv[2], reply, len);
256 }
257
258 static struct {
259         const char *opt;
260         void (*func)(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy);
261         const char *desc;
262         const char *arg;
263         const char *ex;
264 } option_proc[] = {
265         {
266                 "info", _tdm_monitor_server_query,
267                 "show tdm output, layer information", NULL, NULL
268         },
269         {
270                 "dpms", _tdm_monitor_server_dpms,
271                 "set output dpms", "<output_idx>:<dpms>", "0:3 or 0:0"
272         },
273         {
274                 "debug", _tdm_monitor_server_debug,
275                 "set the debug level and modules(module: none, mutex, buffer, thread, vblank)",
276                 "<level>[@<module1>[,<module2>]]",
277                 NULL
278         },
279         {
280                 "log_path", _tdm_monitor_server_log_path,
281                 "set the log path (console,dlog,filepath)",
282                 "<path>",
283                 "console"
284         },
285         {
286                 "prop", _tdm_monitor_server_prop,
287                 "set the property of a output or a layer",
288                 "<output_idx>[,<layer_idx>]:<prop_name>,<value>",
289                 NULL
290         },
291         {
292                 "dump", _tdm_monitor_server_dump,
293                 "dump buffers (type: none, layer, pp, capture, current)\n"
294                 "\t\t  layer, pp, capture - start to dump buffers of layer, pp, capture\n"
295                 "\t\t  none               - stop to dump buffers\n"
296                 "\t\t  current            - dump the current buffer of all layers",
297                 "<object_type1>[,<object_type2>[,...]]@[<directory_path>]",
298                 NULL
299         },
300 };
301
302 static void
303 _tdm_monitor_server_usage(char *app_name, char *reply, int *len)
304 {
305         int opt_size = sizeof(option_proc) / sizeof(option_proc[0]);
306         int i;
307
308         TDM_SNPRINTF(reply, len, "usage: %s \n\n", app_name);
309
310         for (i = 0; i < opt_size; i++) {
311                 TDM_SNPRINTF(reply, len, "\t-%s\t%s\n", option_proc[i].opt, option_proc[i].desc);
312                 if (option_proc[i].arg)
313                         TDM_SNPRINTF(reply, len, "\t\t  %s\n", option_proc[i].arg);
314                 if (option_proc[i].ex)
315                         TDM_SNPRINTF(reply, len, "\t\t  ex) %s\n", option_proc[i].ex);
316                 TDM_SNPRINTF(reply, len, "\n");
317         }
318 }
319
320 static void
321 _tdm_monitor_server_command(unsigned int pid, char *cwd, tdm_display *dpy, int argc, char *argv[], char *reply, int *len)
322 {
323         int opt_size = sizeof(option_proc) / sizeof(option_proc[0]);
324         int i;
325
326         if (argc < 2) {
327                 _tdm_monitor_server_usage(argv[0], reply, len);
328                 return;
329         }
330
331         for (i = 0; i < opt_size; i++) {
332                 if (argv[1][0] == '-' && !strncmp(argv[1] + 1, option_proc[i].opt, 32)) {
333                         if (option_proc[i].func) {
334                                 option_proc[i].func(pid, cwd, argc, argv, reply, len, dpy);
335                                 return;
336                         } else {
337                                 TDM_SNPRINTF(reply, len, "'%s' not implemented.\n", argv[1]);
338                                 return;
339                         }
340                 }
341         }
342
343         _tdm_monitor_server_usage(argv[0], reply, len);
344         return;
345 }
346
347
348 INTERN void
349 tdm_monitor_server_command(tdm_display *dpy, const char *options, char *reply, int *len)
350 {
351         unsigned int pid;
352         char cwd[1024];
353         int argc = 0;
354         char *argv[TDM_DBG_SERVER_ARGS_MAX] = {0,};
355         char temp[1024];
356         char *arg;
357         char *end = NULL, *e;
358
359         snprintf(temp, sizeof(temp), "%s", options);
360
361         arg = strtok_r(temp, " ", &end);
362         if (!arg) {
363                 TDM_SNPRINTF(reply, len, "no pid for tdm-monitor");
364                 return;
365         }
366         pid = strtol(arg, &e, 10);
367
368         arg = strtok_r(NULL, " ", &end);
369         if (!arg) {
370                 TDM_SNPRINTF(reply, len, "no cwd for tdm-monitor");
371                 return;
372         }
373         snprintf(cwd, sizeof(cwd), "%s", arg);
374
375         TDM_DBG("pid(%d) cwd(%s)", pid, cwd);
376
377         argv[argc] = strtok_r(NULL, " ", &end);
378         while (argv[argc]) {
379                 argc++;
380                 if (argc == TDM_DBG_SERVER_ARGS_MAX) {
381                         TDM_SNPRINTF(reply, len, "too many arguments for tdm-monitor");
382                         break;
383                 }
384                 argv[argc] = strtok_r(NULL, " ", &end);
385         }
386
387         _tdm_monitor_server_command(pid, cwd, dpy, argc, argv, reply, len);
388 }