support the commit-per-vblank
[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_commit_per_vblank(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
205 {
206         int enable;
207         char *arg;
208         char *end;
209
210         if (argc < 3) {
211                 _tdm_monitor_server_usage(argv[0], reply, len);
212                 return;
213         }
214
215         arg = argv[2];
216         enable = strtol(arg, &end, 10);
217
218         tdm_display_enable_commit_per_vblank(dpy, enable);
219
220         TDM_SNPRINTF(reply, len, "%s the commit-per-vblank\n", (enable) ? "enable" : "disable");
221 }
222
223 static void
224 _tdm_monitor_server_fps(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
225 {
226         int enable;
227         char *arg;
228         char *end;
229
230         if (argc < 3) {
231                 _tdm_monitor_server_usage(argv[0], reply, len);
232                 return;
233         }
234
235         arg = argv[2];
236         enable = strtol(arg, &end, 10);
237
238         tdm_display_enable_fps(dpy, enable);
239
240         TDM_SNPRINTF(reply, len, "%s to print fps\n", (enable) ? "enable" : "disable");
241 }
242
243 static void
244 _tdm_monitor_server_prop(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
245 {
246         tdm_output *output;
247         tdm_output *layer = NULL;
248         int output_idx, layer_idx = -1;
249         int cnt, i, done = 0;
250         tdm_value value;
251         char temp[TDM_PATH_LEN];
252         char *prop_name;
253         char *arg;
254         char *end;
255         tdm_error ret;
256         const tdm_prop *props;
257
258         if (argc < 3) {
259                 _tdm_monitor_server_usage(argv[0], reply, len);
260                 return;
261         }
262
263         snprintf(temp, TDM_PATH_LEN, "%s", argv[2]);
264         arg = temp;
265
266         output_idx = strtol(arg, &end, 10);
267         if (*end == ',') {
268                 arg = end + 1;
269                 layer_idx = strtol(arg, &end, 10);
270         }
271
272         if (*end != ':') {
273                 TDM_SNPRINTF(reply, len, "failed: no prop_name\n");
274                 return;
275         }
276
277         arg = end + 1;
278         prop_name = strtok_r(arg, ",", &end);
279         if (!prop_name) {
280                 TDM_SNPRINTF(reply, len, "failed: get prop_name by strtok_r\n");
281                 return;
282         }
283
284         if (*end == '\0') {
285                 TDM_SNPRINTF(reply, len, "failed: no value\n");
286                 return;
287         }
288
289         arg = strtok_r(NULL, TDM_DELIM, &end);
290         if (!arg) {
291                 TDM_SNPRINTF(reply, len, "failed: get arg by strtok_r\n");
292                 return;
293         }
294         value.u32 = strtol(arg, &end, 10);
295
296         output = tdm_display_get_output(dpy, output_idx, &ret);
297         TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE && output != NULL);
298
299         if (layer_idx != -1) {
300                 layer = tdm_output_get_layer(output, layer_idx, &ret);
301                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE && layer != NULL);
302         }
303
304         if (layer) {
305                 ret = tdm_layer_get_available_properties(layer, &props, &cnt);
306                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
307
308                 for (i = 0; i < cnt; i++) {
309                         if (!strncmp(props[i].name, prop_name, TDM_NAME_LEN)) {
310                                 ret = tdm_layer_set_property(layer, props[i].id, value);
311                                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
312                                 done = 1;
313                                 break;
314                         }
315                 }
316         } else {
317                 ret = tdm_output_get_available_properties(output, &props, &cnt);
318                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
319
320                 for (i = 0; i < cnt; i++) {
321                         if (!strncmp(props[i].name, prop_name, TDM_NAME_LEN)) {
322                                 ret = tdm_output_set_property(output, props[i].id, value);
323                                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
324                                 done = 1;
325                                 break;
326                         }
327                 }
328         }
329
330         if (done)
331                 TDM_SNPRINTF(reply, len, "done: %s:%d \n", prop_name, value.u32);
332         else
333                 TDM_SNPRINTF(reply, len, "no '%s' propperty \n", prop_name);
334 }
335
336 static void
337 _tdm_monitor_server_dump(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
338 {
339         if (argc < 3) {
340                 _tdm_monitor_server_usage(argv[0], reply, len);
341                 return;
342         }
343
344         tdm_display_enable_dump(dpy, (const char*)argv[2], reply, len);
345 }
346
347 static void
348 _tdm_monitor_server_punch(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
349 {
350         char *arg, *end;
351         unsigned int output_id, layer_id;
352         tdm_output *output;
353         tdm_layer *layer;
354         tbm_surface_h buffer;
355
356         arg = argv[2];
357
358         output_id = strtol(arg, &end, 10);
359         output = tdm_display_get_output(dpy, output_id, NULL);
360         if (!output) {
361                 TDM_SNPRINTF(reply, len, "not found output\n");
362                 return;
363         }
364         if (*end != ',') {
365                 TDM_SNPRINTF(reply, len, "not found ',<layer_idx>'\n");
366                 return;
367         }
368
369         arg = end + 1;
370         layer_id = strtol(arg, &end, 10);
371         layer = tdm_output_get_layer(output, layer_id, NULL);
372         if (!layer) {
373                 TDM_SNPRINTF(reply, len, "not found layer\n");
374                 return;
375         }
376
377         buffer = tdm_layer_get_displaying_buffer(layer, NULL);
378         if (!buffer) {
379                 TDM_SNPRINTF(reply, len, "not found buffer\n");
380                 return;
381         }
382
383         if (*end == ':') {
384                 tdm_pos pos = {0,};
385
386                 arg = end + 1;
387                 pos.w = strtol(arg, &end, 10);
388                 TDM_EXIT_IF_FAIL(*end == 'x');
389                 arg = end + 1;
390                 pos.h = strtol(arg, &end, 10);
391                 if (*end == '+') {
392                         arg = end + 1;
393                         pos.x = strtol(arg, &end, 10);
394                         TDM_EXIT_IF_FAIL(*end == '+');
395                         arg = end + 1;
396                         pos.y = strtol(arg, &end, 10);
397                 }
398
399                 tdm_helper_clear_buffer_pos(buffer, &pos);
400         } else
401                 tdm_helper_clear_buffer(buffer);
402 }
403
404 static struct {
405         const char *opt;
406         void (*func)(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy);
407         const char *desc;
408         const char *arg;
409         const char *ex;
410 } option_proc[] = {
411         {
412                 "info", _tdm_monitor_server_query,
413                 "show tdm output, layer information", NULL, NULL
414         },
415         {
416                 "dpms", _tdm_monitor_server_dpms,
417                 "set output dpms", "<output_idx>:<dpms>", "0:3 or 0:0"
418         },
419         {
420                 "ttrace_vblank", _tdm_monitor_server_ttrace_vblank,
421                 "enable/disable the vblank for ttrace [0:disable 1:enable]",
422                 "<enable>[@<output_idx>]", "0 or 1"
423         },
424         {
425                 "debug", _tdm_monitor_server_debug,
426                 "set the debug level and modules(module: none, mutex, buffer, thread, vblank)",
427                 "<level>[@<module1>[,<module2>]]",
428                 NULL
429         },
430         {
431                 "log_path", _tdm_monitor_server_log_path,
432                 "set the log path (console,dlog,filepath)",
433                 "<path>",
434                 "console"
435         },
436         {
437                 "commit_per_vblank", _tdm_monitor_server_commit_per_vblank,
438                 "enable/disable the commit per vblank",
439                 "<enable>",
440                 "0 or 1"
441         },
442         {
443                 "fps", _tdm_monitor_server_fps,
444                 "enable/disable to print fps",
445                 "<enable>",
446                 "0 or 1"
447         },
448         {
449                 "prop", _tdm_monitor_server_prop,
450                 "set the property of a output or a layer",
451                 "<output_idx>[,<layer_idx>]:<prop_name>,<value>",
452                 NULL
453         },
454         {
455                 "dump", _tdm_monitor_server_dump,
456                 "dump buffers (type: none, layer, pp, capture, current)\n"
457                 "\t\t  layer, pp, capture - start to dump buffers of layer, pp, capture\n"
458                 "\t\t  none               - stop to dump buffers\n"
459                 "\t\t  current            - dump the current buffer of all layers",
460                 "<object_type1>[,<object_type2>[,...]][@<directory_path>]",
461                 NULL
462         },
463         {
464                 "punch", _tdm_monitor_server_punch,
465                 "punch a layer",
466                 "<output_idx>,<layer_idx>[:<w>x<h>[+<x>+<y>]]",
467                 NULL
468         },
469 };
470
471 static void
472 _tdm_monitor_server_usage(char *app_name, char *reply, int *len)
473 {
474         int opt_size = sizeof(option_proc) / sizeof(option_proc[0]);
475         int i;
476
477         TDM_SNPRINTF(reply, len, "usage: %s \n\n", app_name);
478
479         for (i = 0; i < opt_size; i++) {
480                 TDM_SNPRINTF(reply, len, "\t-%s\t%s\n", option_proc[i].opt, option_proc[i].desc);
481                 if (option_proc[i].arg)
482                         TDM_SNPRINTF(reply, len, "\t\t  %s\n", option_proc[i].arg);
483                 if (option_proc[i].ex)
484                         TDM_SNPRINTF(reply, len, "\t\t  ex) %s\n", option_proc[i].ex);
485                 TDM_SNPRINTF(reply, len, "\n");
486         }
487 }
488
489 static void
490 _tdm_monitor_server_command(unsigned int pid, char *cwd, tdm_display *dpy, int argc, char *argv[], char *reply, int *len)
491 {
492         int opt_size = sizeof(option_proc) / sizeof(option_proc[0]);
493         int i;
494
495         if (argc < 2) {
496                 _tdm_monitor_server_usage(argv[0], reply, len);
497                 return;
498         }
499
500         for (i = 0; i < opt_size; i++) {
501                 if (argv[1][0] == '-' && !strncmp(argv[1] + 1, option_proc[i].opt, 32)) {
502                         if (option_proc[i].func) {
503                                 option_proc[i].func(pid, cwd, argc, argv, reply, len, dpy);
504                                 return;
505                         } else {
506                                 TDM_SNPRINTF(reply, len, "'%s' not implemented.\n", argv[1]);
507                                 return;
508                         }
509                 }
510         }
511
512         _tdm_monitor_server_usage(argv[0], reply, len);
513         return;
514 }
515
516
517 INTERN void
518 tdm_monitor_server_command(tdm_display *dpy, const char *options, char *reply, int *len)
519 {
520         unsigned int pid;
521         char cwd[1024];
522         int argc = 0;
523         char *argv[TDM_DBG_SERVER_ARGS_MAX] = {0,};
524         char temp[1024];
525         char *arg;
526         char *end = NULL, *e;
527
528         snprintf(temp, sizeof(temp), "%s", options);
529
530         arg = strtok_r(temp, " ", &end);
531         if (!arg) {
532                 TDM_SNPRINTF(reply, len, "no pid for tdm-monitor");
533                 return;
534         }
535         pid = strtol(arg, &e, 10);
536
537         arg = strtok_r(NULL, " ", &end);
538         if (!arg) {
539                 TDM_SNPRINTF(reply, len, "no cwd for tdm-monitor");
540                 return;
541         }
542         snprintf(cwd, sizeof(cwd), "%s", arg);
543
544         TDM_DBG("pid(%d) cwd(%s)", pid, cwd);
545
546         argv[argc] = strtok_r(NULL, " ", &end);
547         while (argv[argc]) {
548                 argc++;
549                 if (argc == TDM_DBG_SERVER_ARGS_MAX) {
550                         TDM_SNPRINTF(reply, len, "too many arguments for tdm-monitor");
551                         break;
552                 }
553                 argv[argc] = strtok_r(NULL, " ", &end);
554         }
555
556         _tdm_monitor_server_command(pid, cwd, dpy, argc, argv, reply, len);
557 }