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