remove no effective code
[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_ttrace_vblank(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
91 {
92         int enable, output_id = 0;
93         tdm_output *output;
94         char *arg;
95         char *end;
96         tdm_error ret;
97         tdm_output_type type;
98
99         if (argc < 3) {
100                 _tdm_monitor_server_usage(argv[0], reply, len);
101                 return;
102         }
103
104         arg = argv[2];
105         enable = strtol(arg, &end, 10);
106
107         if (*end == '@') {
108                 arg = end + 1;
109                 output_id = strtol(arg, &end, 10);
110         }
111
112         output = tdm_display_get_output(dpy, output_id, NULL);
113         if (!output) {
114                 TDM_SNPRINTF(reply, len, "can't find the output_id(%d)\n", output_id);
115                 return;
116         }
117
118         ret = tdm_output_get_output_type(output, &type);
119         if (ret != TDM_ERROR_NONE) {
120                 TDM_SNPRINTF(reply, len, "can't find the type of output_id(%d)\n", output_id);
121                 return;
122         }
123
124         tdm_display_enable_ttrace_vblank(dpy, output, enable);
125
126         TDM_SNPRINTF(reply, len, "%s ttrace vblank for '%s'\n",
127                                  (enable)?"enable":"disable", tdm_conn_str(type));
128 }
129
130 static void
131 _tdm_monitor_server_debug(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
132 {
133         int level;
134         char *arg;
135         char *end;
136
137         if (argc < 3) {
138                 _tdm_monitor_server_usage(argv[0], reply, len);
139                 return;
140         }
141
142         arg = argv[2];
143         level = strtol(arg, &end, 10);
144
145         tdm_log_set_debug_level(level);
146         TDM_SNPRINTF(reply, len, "debug level: %d\n", level);
147
148         if (*end == '@') {
149                 char *arg = end + 1;
150
151                 tdm_display_enable_debug_module((const char *)arg);
152
153                 TDM_SNPRINTF(reply, len, "debugging... '%s'\n", arg);
154         }
155 }
156
157 static void
158 _tdm_monitor_server_log_path(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
159 {
160         static int old_stdout = -1;
161         char fd_name[TDM_PATH_LEN];
162         char *path;
163
164         if (argc < 3) {
165                 _tdm_monitor_server_usage(argv[0], reply, len);
166                 return;
167         }
168
169         if (old_stdout == -1)
170                 old_stdout = dup(STDOUT_FILENO);
171
172         path = argv[2];
173         TDM_DBG_RETURN_IF_FAIL(path != NULL);
174
175         tdm_log_enable_dlog(0);
176
177         if (!strncmp(path, "dlog", 4)) {
178                 tdm_log_enable_dlog(1);
179                 goto done;
180         } else if (!strncmp(path, "console", 7))
181                 snprintf(fd_name, TDM_PATH_LEN, "/proc/%d/fd/1", pid);
182         else {
183                 if (path[0] == '/')
184                         snprintf(fd_name, TDM_PATH_LEN, "%s", path);
185                 else {
186                         if (cwd)
187                                 snprintf(fd_name, TDM_PATH_LEN, "%s/%s", cwd, path);
188                         else
189                                 snprintf(fd_name, TDM_PATH_LEN, "%s", path);
190                 }
191                 tdm_log_enable_color(0);
192         }
193
194         if (tdm_display_enable_path((const char*)fd_name) != TDM_ERROR_NONE) {
195                 TDM_SNPRINTF(reply, len, "failed: '%s'\n", path);
196                 return;
197         }
198
199 done:
200         TDM_SNPRINTF(reply, len, "log path: '%s'\n", path);
201 }
202
203 static void
204 _tdm_monitor_server_prop(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
205 {
206         tdm_output *output;
207         tdm_output *layer = NULL;
208         int output_idx, layer_idx = -1;
209         int cnt, i, done = 0;
210         tdm_value value;
211         char temp[TDM_PATH_LEN];
212         char *prop_name;
213         char *arg;
214         char *end;
215         tdm_error ret;
216         const tdm_prop *props;
217
218         if (argc < 3) {
219                 _tdm_monitor_server_usage(argv[0], reply, len);
220                 return;
221         }
222
223         snprintf(temp, TDM_PATH_LEN, "%s", argv[2]);
224         arg = temp;
225
226         output_idx = strtol(arg, &end, 10);
227         if (*end == ',') {
228                 arg = end + 1;
229                 layer_idx = strtol(arg, &end, 10);
230         }
231
232         if (*end != ':') {
233                 TDM_SNPRINTF(reply, len, "failed: no prop_name\n");
234                 return;
235         }
236
237         arg = end + 1;
238         prop_name = strtok_r(arg, ",", &end);
239         if (!prop_name) {
240                 TDM_SNPRINTF(reply, len, "failed: get prop_name by strtok_r\n");
241                 return;
242         }
243
244         if (*end == '\0') {
245                 TDM_SNPRINTF(reply, len, "failed: no value\n");
246                 return;
247         }
248
249         arg = strtok_r(NULL, TDM_DELIM, &end);
250         if (!arg) {
251                 TDM_SNPRINTF(reply, len, "failed: get arg by strtok_r\n");
252                 return;
253         }
254         value.u32 = strtol(arg, &end, 10);
255
256         output = tdm_display_get_output(dpy, output_idx, &ret);
257         TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE && output != NULL);
258
259         if (layer_idx != -1) {
260                 layer = tdm_output_get_layer(output, layer_idx, &ret);
261                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE && layer != NULL);
262         }
263
264         if (layer) {
265                 ret = tdm_layer_get_available_properties(layer, &props, &cnt);
266                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
267
268                 for (i = 0; i < cnt; i++) {
269                         if (!strncmp(props[i].name, prop_name, TDM_NAME_LEN)) {
270                                 ret = tdm_layer_set_property(layer, props[i].id, value);
271                                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
272                                 done = 1;
273                                 break;
274                         }
275                 }
276         } else {
277                 ret = tdm_output_get_available_properties(output, &props, &cnt);
278                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
279
280                 for (i = 0; i < cnt; i++) {
281                         if (!strncmp(props[i].name, prop_name, TDM_NAME_LEN)) {
282                                 ret = tdm_output_set_property(output, props[i].id, value);
283                                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
284                                 done = 1;
285                                 break;
286                         }
287                 }
288         }
289
290         if (done)
291                 TDM_SNPRINTF(reply, len, "done: %s:%d \n", prop_name, value.u32);
292         else
293                 TDM_SNPRINTF(reply, len, "no '%s' propperty \n", prop_name);
294 }
295
296 static void
297 _tdm_monitor_server_dump(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
298 {
299         if (argc < 3) {
300                 _tdm_monitor_server_usage(argv[0], reply, len);
301                 return;
302         }
303
304         tdm_display_enable_dump(dpy, (const char*)argv[2], reply, len);
305 }
306
307 static struct {
308         const char *opt;
309         void (*func)(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy);
310         const char *desc;
311         const char *arg;
312         const char *ex;
313 } option_proc[] = {
314         {
315                 "info", _tdm_monitor_server_query,
316                 "show tdm output, layer information", NULL, NULL
317         },
318         {
319                 "dpms", _tdm_monitor_server_dpms,
320                 "set output dpms", "<output_idx>:<dpms>", "0:3 or 0:0"
321         },
322         {
323                 "ttrace_vblank", _tdm_monitor_server_ttrace_vblank,
324                 "enable/disable the vblank for ttrace [0:disable 1:enable]",
325                 "<enable>[@<output_idx>]", "0 or 1"
326         },
327         {
328                 "debug", _tdm_monitor_server_debug,
329                 "set the debug level and modules(module: none, mutex, buffer, thread, vblank)",
330                 "<level>[@<module1>[,<module2>]]",
331                 NULL
332         },
333         {
334                 "log_path", _tdm_monitor_server_log_path,
335                 "set the log path (console,dlog,filepath)",
336                 "<path>",
337                 "console"
338         },
339         {
340                 "prop", _tdm_monitor_server_prop,
341                 "set the property of a output or a layer",
342                 "<output_idx>[,<layer_idx>]:<prop_name>,<value>",
343                 NULL
344         },
345         {
346                 "dump", _tdm_monitor_server_dump,
347                 "dump buffers (type: none, layer, pp, capture, current)\n"
348                 "\t\t  layer, pp, capture - start to dump buffers of layer, pp, capture\n"
349                 "\t\t  none               - stop to dump buffers\n"
350                 "\t\t  current            - dump the current buffer of all layers",
351                 "<object_type1>[,<object_type2>[,...]]@[<directory_path>]",
352                 NULL
353         },
354 };
355
356 static void
357 _tdm_monitor_server_usage(char *app_name, char *reply, int *len)
358 {
359         int opt_size = sizeof(option_proc) / sizeof(option_proc[0]);
360         int i;
361
362         TDM_SNPRINTF(reply, len, "usage: %s \n\n", app_name);
363
364         for (i = 0; i < opt_size; i++) {
365                 TDM_SNPRINTF(reply, len, "\t-%s\t%s\n", option_proc[i].opt, option_proc[i].desc);
366                 if (option_proc[i].arg)
367                         TDM_SNPRINTF(reply, len, "\t\t  %s\n", option_proc[i].arg);
368                 if (option_proc[i].ex)
369                         TDM_SNPRINTF(reply, len, "\t\t  ex) %s\n", option_proc[i].ex);
370                 TDM_SNPRINTF(reply, len, "\n");
371         }
372 }
373
374 static void
375 _tdm_monitor_server_command(unsigned int pid, char *cwd, tdm_display *dpy, int argc, char *argv[], char *reply, int *len)
376 {
377         int opt_size = sizeof(option_proc) / sizeof(option_proc[0]);
378         int i;
379
380         if (argc < 2) {
381                 _tdm_monitor_server_usage(argv[0], reply, len);
382                 return;
383         }
384
385         for (i = 0; i < opt_size; i++) {
386                 if (argv[1][0] == '-' && !strncmp(argv[1] + 1, option_proc[i].opt, 32)) {
387                         if (option_proc[i].func) {
388                                 option_proc[i].func(pid, cwd, argc, argv, reply, len, dpy);
389                                 return;
390                         } else {
391                                 TDM_SNPRINTF(reply, len, "'%s' not implemented.\n", argv[1]);
392                                 return;
393                         }
394                 }
395         }
396
397         _tdm_monitor_server_usage(argv[0], reply, len);
398         return;
399 }
400
401
402 INTERN void
403 tdm_monitor_server_command(tdm_display *dpy, const char *options, char *reply, int *len)
404 {
405         unsigned int pid;
406         char cwd[1024];
407         int argc = 0;
408         char *argv[TDM_DBG_SERVER_ARGS_MAX] = {0,};
409         char temp[1024];
410         char *arg;
411         char *end = NULL, *e;
412
413         snprintf(temp, sizeof(temp), "%s", options);
414
415         arg = strtok_r(temp, " ", &end);
416         if (!arg) {
417                 TDM_SNPRINTF(reply, len, "no pid for tdm-monitor");
418                 return;
419         }
420         pid = strtol(arg, &e, 10);
421
422         arg = strtok_r(NULL, " ", &end);
423         if (!arg) {
424                 TDM_SNPRINTF(reply, len, "no cwd for tdm-monitor");
425                 return;
426         }
427         snprintf(cwd, sizeof(cwd), "%s", arg);
428
429         TDM_DBG("pid(%d) cwd(%s)", pid, cwd);
430
431         argv[argc] = strtok_r(NULL, " ", &end);
432         while (argv[argc]) {
433                 argc++;
434                 if (argc == TDM_DBG_SERVER_ARGS_MAX) {
435                         TDM_SNPRINTF(reply, len, "too many arguments for tdm-monitor");
436                         break;
437                 }
438                 argv[argc] = strtok_r(NULL, " ", &end);
439         }
440
441         _tdm_monitor_server_command(pid, cwd, dpy, argc, argv, reply, len);
442 }