vblank: add set_vblank_fps functionality for the given PID
[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_vblank_list(unsigned int pid, char *cwd, int argc, char *argv[],
245                                                                 char *reply, int *len, tdm_display *dpy)
246 {
247         tdm_server_get_vblank_list_information(dpy, reply, len);
248 }
249
250 static void
251 _tdm_monitor_server_vblank_fps(unsigned int pid, char *cwd, int argc, char *argv[],
252                                                            char *reply, int *len, tdm_display *dpy)
253 {
254         unsigned int target_pid, fps;
255         char *arg;
256         char *end;
257         char name[TDM_NAME_LEN];
258         tdm_error ret;
259
260         if (argc < 3) {
261                 _tdm_monitor_server_usage(argv[0], reply, len);
262                 return;
263         }
264
265         arg = argv[2];
266         target_pid = strtol(arg, &end, 10);
267
268         if (*end == ',') {
269                 arg = end + 1;
270                 end = strtostr(name, TDM_NAME_LEN, arg, TDM_DELIM);
271         } else {
272                 strncpy(name, TDM_VBLANK_DEFAULT_NAME, TDM_NAME_LEN - 1);
273                 name[TDM_NAME_LEN - 1] = '\0';
274         }
275
276         if (*end != '@') {
277                 TDM_SNPRINTF(reply, len, "failed: no fps value\n");
278                 return;
279         }
280
281         arg = end + 1;
282         fps = strtol(arg, &end, 10);
283
284         ret = tdm_server_set_client_vblank_fps(target_pid, name, fps);
285         if (ret != TDM_ERROR_NONE) {
286                 TDM_SNPRINTF(reply, len, "can't set '%u' fps to '%s' client vblank(PID:%u)\n", fps, name, target_pid);
287                 return;
288         }
289
290         TDM_SNPRINTF(reply, len, "success: '%u' fps for '%s' client vblank(PID:%u)\n", fps, name, target_pid);
291 }
292
293 static void
294 _tdm_monitor_server_prop(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
295 {
296         tdm_output *output;
297         tdm_output *layer = NULL;
298         int output_idx, layer_idx = -1;
299         int cnt, i, done = 0;
300         tdm_value value;
301         char temp[TDM_PATH_LEN];
302         char *prop_name;
303         char *arg;
304         char *end;
305         tdm_error ret;
306         const tdm_prop *props;
307
308         if (argc < 3) {
309                 _tdm_monitor_server_usage(argv[0], reply, len);
310                 return;
311         }
312
313         snprintf(temp, TDM_PATH_LEN, "%s", argv[2]);
314         arg = temp;
315
316         output_idx = strtol(arg, &end, 10);
317         if (*end == ',') {
318                 arg = end + 1;
319                 layer_idx = strtol(arg, &end, 10);
320         }
321
322         if (*end != ':') {
323                 TDM_SNPRINTF(reply, len, "failed: no prop_name\n");
324                 return;
325         }
326
327         arg = end + 1;
328         prop_name = strtok_r(arg, ",", &end);
329         if (!prop_name) {
330                 TDM_SNPRINTF(reply, len, "failed: get prop_name by strtok_r\n");
331                 return;
332         }
333
334         if (*end == '\0') {
335                 TDM_SNPRINTF(reply, len, "failed: no value\n");
336                 return;
337         }
338
339         arg = strtok_r(NULL, TDM_DELIM, &end);
340         if (!arg) {
341                 TDM_SNPRINTF(reply, len, "failed: get arg by strtok_r\n");
342                 return;
343         }
344         value.u32 = strtol(arg, &end, 10);
345
346         output = tdm_display_get_output(dpy, output_idx, &ret);
347         TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE && output != NULL);
348
349         if (layer_idx != -1) {
350                 layer = tdm_output_get_layer(output, layer_idx, &ret);
351                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE && layer != NULL);
352         }
353
354         if (layer) {
355                 ret = tdm_layer_get_available_properties(layer, &props, &cnt);
356                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
357
358                 for (i = 0; i < cnt; i++) {
359                         if (!strncmp(props[i].name, prop_name, TDM_NAME_LEN)) {
360                                 ret = tdm_layer_set_property(layer, props[i].id, value);
361                                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
362                                 done = 1;
363                                 break;
364                         }
365                 }
366         } else {
367                 ret = tdm_output_get_available_properties(output, &props, &cnt);
368                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
369
370                 for (i = 0; i < cnt; i++) {
371                         if (!strncmp(props[i].name, prop_name, TDM_NAME_LEN)) {
372                                 ret = tdm_output_set_property(output, props[i].id, value);
373                                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
374                                 done = 1;
375                                 break;
376                         }
377                 }
378         }
379
380         if (done)
381                 TDM_SNPRINTF(reply, len, "done: %s:%d \n", prop_name, value.u32);
382         else
383                 TDM_SNPRINTF(reply, len, "no '%s' propperty \n", prop_name);
384 }
385
386 static void
387 _tdm_monitor_server_dump(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
388 {
389         if (argc < 3) {
390                 _tdm_monitor_server_usage(argv[0], reply, len);
391                 return;
392         }
393
394         tdm_display_enable_dump(dpy, (const char*)argv[2], reply, len);
395 }
396
397 static void
398 _tdm_monitor_server_punch(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
399 {
400         char *arg, *end;
401         unsigned int output_id, layer_id;
402         tdm_output *output;
403         tdm_layer *layer;
404         tbm_surface_h buffer;
405
406         arg = argv[2];
407
408         output_id = strtol(arg, &end, 10);
409         output = tdm_display_get_output(dpy, output_id, NULL);
410         if (!output) {
411                 TDM_SNPRINTF(reply, len, "not found output\n");
412                 return;
413         }
414         if (*end != ',') {
415                 TDM_SNPRINTF(reply, len, "not found ',<layer_idx>'\n");
416                 return;
417         }
418
419         arg = end + 1;
420         layer_id = strtol(arg, &end, 10);
421         layer = tdm_output_get_layer(output, layer_id, NULL);
422         if (!layer) {
423                 TDM_SNPRINTF(reply, len, "not found layer\n");
424                 return;
425         }
426
427         buffer = tdm_layer_get_displaying_buffer(layer, NULL);
428         if (!buffer) {
429                 TDM_SNPRINTF(reply, len, "not found buffer\n");
430                 return;
431         }
432
433         if (*end == ':') {
434                 tdm_pos pos = {0,};
435
436                 arg = end + 1;
437                 pos.w = strtol(arg, &end, 10);
438                 TDM_EXIT_IF_FAIL(*end == 'x');
439                 arg = end + 1;
440                 pos.h = strtol(arg, &end, 10);
441                 if (*end == '+') {
442                         arg = end + 1;
443                         pos.x = strtol(arg, &end, 10);
444                         TDM_EXIT_IF_FAIL(*end == '+');
445                         arg = end + 1;
446                         pos.y = strtol(arg, &end, 10);
447                 }
448
449                 tdm_helper_clear_buffer_pos(buffer, &pos);
450         } else
451                 tdm_helper_clear_buffer(buffer);
452 }
453
454 static struct {
455         const char *opt;
456         void (*func)(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy);
457         const char *desc;
458         const char *arg;
459         const char *ex;
460 } option_proc[] = {
461         {
462                 "info", _tdm_monitor_server_query,
463                 "show tdm output, layer information", NULL, NULL
464         },
465         {
466                 "dpms", _tdm_monitor_server_dpms,
467                 "set output dpms", "<output_idx>:<dpms>", "0:3 or 0:0"
468         },
469         {
470                 "ttrace_vblank", _tdm_monitor_server_ttrace_vblank,
471                 "enable/disable the vblank for ttrace [0:disable 1:enable]",
472                 "<enable>[@<output_idx>]", "0 or 1"
473         },
474         {
475                 "debug", _tdm_monitor_server_debug,
476                 "set the debug level and modules(module: none, mutex, buffer, thread, vblank)",
477                 "<level>[@<module1>[,<module2>]]",
478                 NULL
479         },
480         {
481                 "log_path", _tdm_monitor_server_log_path,
482                 "set the log path (console,dlog,filepath)",
483                 "<path>",
484                 "console"
485         },
486         {
487                 "commit_per_vblank", _tdm_monitor_server_commit_per_vblank,
488                 "enable/disable the commit per vblank",
489                 "<enable>",
490                 "0 or 1"
491         },
492         {
493                 "fps", _tdm_monitor_server_fps,
494                 "enable/disable to print fps",
495                 "<enable>",
496                 "0 or 1"
497         },
498         {
499                 "vblank_list", _tdm_monitor_server_vblank_list,
500                 "print the client vblank list",
501                 NULL,
502                 NULL
503         },
504         {
505                 "vblank_fps", _tdm_monitor_server_vblank_fps,
506                 "set the client vblank fps for the given process ID and client vblank name",
507                 "<pid>[,<vblank_name>]@<fps>",
508                 NULL
509         },
510         {
511                 "prop", _tdm_monitor_server_prop,
512                 "set the property of a output or a layer",
513                 "<output_idx>[,<layer_idx>]:<prop_name>,<value>",
514                 NULL
515         },
516         {
517                 "dump", _tdm_monitor_server_dump,
518                 "dump buffers (type: none, layer, pp, capture, current)\n"
519                 "\t\t  layer, pp, capture - start to dump buffers of layer, pp, capture\n"
520                 "\t\t  none               - stop to dump buffers\n"
521                 "\t\t  current            - dump the current buffer of all layers",
522                 "<object_type1>[,<object_type2>[,...]][@<directory_path>]",
523                 NULL
524         },
525         {
526                 "punch", _tdm_monitor_server_punch,
527                 "punch a layer",
528                 "<output_idx>,<layer_idx>[:<w>x<h>[+<x>+<y>]]",
529                 NULL
530         },
531 };
532
533 static void
534 _tdm_monitor_server_usage(char *app_name, char *reply, int *len)
535 {
536         int opt_size = sizeof(option_proc) / sizeof(option_proc[0]);
537         int i;
538
539         TDM_SNPRINTF(reply, len, "usage: %s \n\n", app_name);
540
541         for (i = 0; i < opt_size; i++) {
542                 TDM_SNPRINTF(reply, len, "\t-%s\t%s\n", option_proc[i].opt, option_proc[i].desc);
543                 if (option_proc[i].arg)
544                         TDM_SNPRINTF(reply, len, "\t\t  %s\n", option_proc[i].arg);
545                 if (option_proc[i].ex)
546                         TDM_SNPRINTF(reply, len, "\t\t  ex) %s\n", option_proc[i].ex);
547                 TDM_SNPRINTF(reply, len, "\n");
548         }
549 }
550
551 static void
552 _tdm_monitor_server_command(unsigned int pid, char *cwd, tdm_display *dpy, int argc, char *argv[], char *reply, int *len)
553 {
554         int opt_size = sizeof(option_proc) / sizeof(option_proc[0]);
555         int i;
556
557         if (argc < 2) {
558                 _tdm_monitor_server_usage(argv[0], reply, len);
559                 return;
560         }
561
562         for (i = 0; i < opt_size; i++) {
563                 if (argv[1][0] == '-' && !strncmp(argv[1] + 1, option_proc[i].opt, 32)) {
564                         if (option_proc[i].func) {
565                                 option_proc[i].func(pid, cwd, argc, argv, reply, len, dpy);
566                                 return;
567                         } else {
568                                 TDM_SNPRINTF(reply, len, "'%s' not implemented.\n", argv[1]);
569                                 return;
570                         }
571                 }
572         }
573
574         _tdm_monitor_server_usage(argv[0], reply, len);
575         return;
576 }
577
578
579 INTERN void
580 tdm_monitor_server_command(tdm_display *dpy, const char *options, char *reply, int *len)
581 {
582         unsigned int pid;
583         char cwd[1024];
584         int argc = 0;
585         char *argv[TDM_DBG_SERVER_ARGS_MAX] = {0,};
586         char temp[1024];
587         char *arg;
588         char *end = NULL, *e;
589
590         snprintf(temp, sizeof(temp), "%s", options);
591
592         arg = strtok_r(temp, " ", &end);
593         if (!arg) {
594                 TDM_SNPRINTF(reply, len, "no pid for tdm-monitor");
595                 return;
596         }
597         pid = strtol(arg, &e, 10);
598
599         arg = strtok_r(NULL, " ", &end);
600         if (!arg) {
601                 TDM_SNPRINTF(reply, len, "no cwd for tdm-monitor");
602                 return;
603         }
604         snprintf(cwd, sizeof(cwd), "%s", arg);
605
606         TDM_DBG("pid(%d) cwd(%s)", pid, cwd);
607
608         argv[argc] = strtok_r(NULL, " ", &end);
609         while (argv[argc]) {
610                 argc++;
611                 if (argc == TDM_DBG_SERVER_ARGS_MAX) {
612                         TDM_SNPRINTF(reply, len, "too many arguments for tdm-monitor");
613                         break;
614                 }
615                 argv[argc] = strtok_r(NULL, " ", &end);
616         }
617
618         _tdm_monitor_server_command(pid, cwd, dpy, argc, argv, reply, len);
619 }