add punch option 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_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 void
308 _tdm_monitor_server_punch(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
309 {
310         char *arg, *end;
311         unsigned int output_id, layer_id;
312         tdm_output *output;
313         tdm_layer *layer;
314         tbm_surface_h buffer;
315
316         arg = argv[2];
317
318         output_id = strtol(arg, &end, 10);
319         output = tdm_display_get_output(dpy, output_id, NULL);
320         if (!output) {
321                 TDM_SNPRINTF(reply, len, "not found output\n");
322                 return;
323         }
324         if (*end != ',') {
325                 TDM_SNPRINTF(reply, len, "not found ',<layer_idx>'\n");
326                 return;
327         }
328
329         arg = end + 1;
330         layer_id = strtol(arg, &end, 10);
331         layer = tdm_output_get_layer(output, layer_id, NULL);
332         if (!layer) {
333                 TDM_SNPRINTF(reply, len, "not found layer\n");
334                 return;
335         }
336
337         buffer = tdm_layer_get_displaying_buffer(layer, NULL);
338         if (!buffer) {
339                 TDM_SNPRINTF(reply, len, "not found buffer\n");
340                 return;
341         }
342
343         if (*end == ':') {
344                 tdm_pos pos = {0,};
345
346                 arg = end + 1;
347                 pos.w = strtol(arg, &end, 10);
348                 TDM_EXIT_IF_FAIL(*end == 'x');
349                 arg = end + 1;
350                 pos.h = strtol(arg, &end, 10);
351                 if (*end == '+') {
352                         arg = end + 1;
353                         pos.x = strtol(arg, &end, 10);
354                         TDM_EXIT_IF_FAIL(*end == '+');
355                         arg = end + 1;
356                         pos.y = strtol(arg, &end, 10);
357                 }
358
359                 tdm_helper_clear_buffer_pos(buffer, &pos);
360         }
361         else
362                 tdm_helper_clear_buffer(buffer);
363 }
364
365 static struct {
366         const char *opt;
367         void (*func)(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy);
368         const char *desc;
369         const char *arg;
370         const char *ex;
371 } option_proc[] = {
372         {
373                 "info", _tdm_monitor_server_query,
374                 "show tdm output, layer information", NULL, NULL
375         },
376         {
377                 "dpms", _tdm_monitor_server_dpms,
378                 "set output dpms", "<output_idx>:<dpms>", "0:3 or 0:0"
379         },
380         {
381                 "ttrace_vblank", _tdm_monitor_server_ttrace_vblank,
382                 "enable/disable the vblank for ttrace [0:disable 1:enable]",
383                 "<enable>[@<output_idx>]", "0 or 1"
384         },
385         {
386                 "debug", _tdm_monitor_server_debug,
387                 "set the debug level and modules(module: none, mutex, buffer, thread, vblank)",
388                 "<level>[@<module1>[,<module2>]]",
389                 NULL
390         },
391         {
392                 "log_path", _tdm_monitor_server_log_path,
393                 "set the log path (console,dlog,filepath)",
394                 "<path>",
395                 "console"
396         },
397         {
398                 "prop", _tdm_monitor_server_prop,
399                 "set the property of a output or a layer",
400                 "<output_idx>[,<layer_idx>]:<prop_name>,<value>",
401                 NULL
402         },
403         {
404                 "dump", _tdm_monitor_server_dump,
405                 "dump buffers (type: none, layer, pp, capture, current)\n"
406                 "\t\t  layer, pp, capture - start to dump buffers of layer, pp, capture\n"
407                 "\t\t  none               - stop to dump buffers\n"
408                 "\t\t  current            - dump the current buffer of all layers",
409                 "<object_type1>[,<object_type2>[,...]][@<directory_path>]",
410                 NULL
411         },
412         {
413                 "punch", _tdm_monitor_server_punch,
414                 "punch a layer",
415                 "<output_idx>,<layer_idx>[:<w>x<h>[+<x>+<y>]]",
416                 NULL
417         },
418 };
419
420 static void
421 _tdm_monitor_server_usage(char *app_name, char *reply, int *len)
422 {
423         int opt_size = sizeof(option_proc) / sizeof(option_proc[0]);
424         int i;
425
426         TDM_SNPRINTF(reply, len, "usage: %s \n\n", app_name);
427
428         for (i = 0; i < opt_size; i++) {
429                 TDM_SNPRINTF(reply, len, "\t-%s\t%s\n", option_proc[i].opt, option_proc[i].desc);
430                 if (option_proc[i].arg)
431                         TDM_SNPRINTF(reply, len, "\t\t  %s\n", option_proc[i].arg);
432                 if (option_proc[i].ex)
433                         TDM_SNPRINTF(reply, len, "\t\t  ex) %s\n", option_proc[i].ex);
434                 TDM_SNPRINTF(reply, len, "\n");
435         }
436 }
437
438 static void
439 _tdm_monitor_server_command(unsigned int pid, char *cwd, tdm_display *dpy, int argc, char *argv[], char *reply, int *len)
440 {
441         int opt_size = sizeof(option_proc) / sizeof(option_proc[0]);
442         int i;
443
444         if (argc < 2) {
445                 _tdm_monitor_server_usage(argv[0], reply, len);
446                 return;
447         }
448
449         for (i = 0; i < opt_size; i++) {
450                 if (argv[1][0] == '-' && !strncmp(argv[1] + 1, option_proc[i].opt, 32)) {
451                         if (option_proc[i].func) {
452                                 option_proc[i].func(pid, cwd, argc, argv, reply, len, dpy);
453                                 return;
454                         } else {
455                                 TDM_SNPRINTF(reply, len, "'%s' not implemented.\n", argv[1]);
456                                 return;
457                         }
458                 }
459         }
460
461         _tdm_monitor_server_usage(argv[0], reply, len);
462         return;
463 }
464
465
466 INTERN void
467 tdm_monitor_server_command(tdm_display *dpy, const char *options, char *reply, int *len)
468 {
469         unsigned int pid;
470         char cwd[1024];
471         int argc = 0;
472         char *argv[TDM_DBG_SERVER_ARGS_MAX] = {0,};
473         char temp[1024];
474         char *arg;
475         char *end = NULL, *e;
476
477         snprintf(temp, sizeof(temp), "%s", options);
478
479         arg = strtok_r(temp, " ", &end);
480         if (!arg) {
481                 TDM_SNPRINTF(reply, len, "no pid for tdm-monitor");
482                 return;
483         }
484         pid = strtol(arg, &e, 10);
485
486         arg = strtok_r(NULL, " ", &end);
487         if (!arg) {
488                 TDM_SNPRINTF(reply, len, "no cwd for tdm-monitor");
489                 return;
490         }
491         snprintf(cwd, sizeof(cwd), "%s", arg);
492
493         TDM_DBG("pid(%d) cwd(%s)", pid, cwd);
494
495         argv[argc] = strtok_r(NULL, " ", &end);
496         while (argv[argc]) {
497                 argc++;
498                 if (argc == TDM_DBG_SERVER_ARGS_MAX) {
499                         TDM_SNPRINTF(reply, len, "too many arguments for tdm-monitor");
500                         break;
501                 }
502                 argv[argc] = strtok_r(NULL, " ", &end);
503         }
504
505         _tdm_monitor_server_command(pid, cwd, dpy, argc, argv, reply, len);
506 }