Fix invalid assignment with unsigned values
[platform/core/uifw/libtdm.git] / tools / tdm_test_client.c
index 2b61c03..a3f9cb2 100644 (file)
@@ -9,7 +9,7 @@
  *          Taeheon Kim <th908.kim@samsung.com>,
  *          YoungJun Cho <yj44.cho@samsung.com>,
  *          SooChan Lim <sc1.lim@samsung.com>,
- *          Boram Park <sc1.lim@samsung.com>
+ *          Boram Park <boram1288.park@samsung.com>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the
 #include <time.h>
 #include <stdint.h>
 
+#include "tdm_client.h"
 #include "tdm_macro.h"
+#include "buffers.h"
 
-#include <tdm_client.h>
-#include <tdm_helper.h>
-
-int tdm_debug;
+#define CHECK_V_STEP 0
 
 typedef struct _tdm_test_client_arg {
-       char output_name[512];
+       char *output_name;
        int fps;
        int sync;
        int interval;
        int offset;
        int enable_fake;
+       int pid;
+       char *vblank_name;
 } tdm_test_client_arg;
 
 typedef struct _tdm_test_client {
@@ -62,22 +63,25 @@ typedef struct _tdm_test_client {
 
        int do_query;
        int do_vblank;
+       int do_voutput;
        int waiting;
 
        tdm_client *client;
+       tdm_client_voutput *voutput;
+       tdm_client_output *output;
 } tdm_test_client;
 
 struct typestrings {
        int type;
-       char string[512];
+       const char *string;
 };
 
 struct optstrings {
        int  type;
-       char opt[512];
-       char desc[512];
-       char arg[512];
-       char ex[512];
+       const char *opt;
+       const char *desc;
+       const char *arg;
+       const char *ex;
 };
 
 enum {
@@ -94,26 +98,10 @@ static struct typestrings typestrs[] = {
 
 static struct optstrings optstrs[] = {
        {OPT_QRY, "qo", "output objects info", "<output_name>", "primary"},
-       {OPT_TST, "v", "vblank test", "<output_name>[,<sync>][@<fps>][#<interval>][+<offset>][*fake]", "primary,0@60#1+0*1"},
+       {OPT_TST, "v", "vblank test", "<output_name>[,<sync>][@<fps>][~<interval>][+<offset>][*fake][^vblank_name]", "primary,0@60~1+0*1^test"},
+       {OPT_TST, "V", "virtual output test", NULL, NULL},
 };
 
-#define DELIM "!@#^&*+-|,"
-
-static char*
-strtostr(char *buf, int len, char *str, char *delim)
-{
-       char *end;
-       end = strpbrk(str, delim);
-       if (end)
-               len = ((end - str + 1) < len) ? (end - str + 1) : len;
-       else {
-               int l = strlen(str);
-               len = ((l + 1) < len) ? (l + 1) : len;
-       }
-       snprintf(buf, len, "%s", str);
-       return str + len - 1;
-}
-
 static void
 usage(char *app_name)
 {
@@ -131,10 +119,13 @@ usage(char *app_name)
                                if (f == 1)
                                        printf(" %s options:\n\n", typestrs[t].string);
                                printf("\t-%s\t%s\n", optstrs[o].opt, optstrs[o].desc);
-                               printf("\t\t%s\n", optstrs[o].arg);
-                               printf("\t\tex) %s\n", optstrs[o].ex);
+                               if (optstrs[o].arg)
+                                       printf("\t\t  %s\n", optstrs[o].arg);
+                               if (optstrs[o].ex)
+                                       printf("\t\t  ex) %s\n", optstrs[o].ex);
                                f = 0;
                        }
+               printf("\n");
        }
 
        exit(0);
@@ -142,84 +133,92 @@ usage(char *app_name)
 
 //"<output_name>"
 static void
-parse_arg_qo(tdm_test_client *data, char *p)
+parse_arg_qo(tdm_test_client *data, char *arg)
 {
-       strtostr(data->args.output_name, 512, p, DELIM);
+       char name[TDM_NAME_LEN];
+       strtostr(name, TDM_NAME_LEN, arg, TDM_DELIM);
+       data->args.output_name = strndup(name, TDM_NAME_LEN);
 }
 
-//"<output_name>[,<sync>][@<fps>][#<interval>][+<offset>][*fake]"
+//"<output_name>[,<sync>][@<fps>][~<interval>][+<offset>][*fake]"
 static void
-parse_arg_v(tdm_test_client *data, char *p)
+parse_arg_v(tdm_test_client *data, char *arg)
 {
-       char *end = p;
+       char *end = arg;
+       char name[TDM_NAME_LEN];
 
-       end = strtostr(data->args.output_name, 512, p, DELIM);
+       end = strtostr(name, TDM_NAME_LEN, arg, TDM_DELIM);
+       data->args.output_name = strndup(name, TDM_NAME_LEN);
 
        if (*end == ',') {
-               p = end + 1;
-               data->args.sync = strtol(p, &end, 10);
+               arg = end + 1;
+               data->args.sync = strtol(arg, &end, 10);
        }
 
        if (*end == '@') {
-               p = end + 1;
-               data->args.fps = strtol(p, &end, 10);
+               arg = end + 1;
+               data->args.fps = strtol(arg, &end, 10);
        }
 
-       if (*end == '#') {
-               p = end + 1;
-               data->args.interval = strtol(p, &end, 10);
+       if (*end == '~') {
+               arg = end + 1;
+               data->args.interval = strtol(arg, &end, 10);
        }
 
        if (*end == '+' || *end == '-') {
-               p = end;
-               data->args.offset = strtol(p, &end, 10);
+               arg = end;
+               data->args.offset = strtol(arg, &end, 10);
        }
 
        if (*end == '*') {
-               p = end + 1;
-               data->args.enable_fake= strtol(p, &end, 10);
+               arg = end + 1;
+               data->args.enable_fake = strtol(arg, &end, 10);
+       }
+
+       if (*end == '^') {
+               char name[TDM_NAME_LEN];
+               arg = end + 1;
+               end = strtostr(name, TDM_NAME_LEN, arg, TDM_DELIM);
+               data->args.vblank_name = strndup(name, TDM_NAME_LEN);
        }
 }
 
 static void
 parse_args(tdm_test_client *data, int argc, char *argv[])
 {
-       int size = sizeof(optstrs) / sizeof(struct optstrings);
-       int i, j = 0;
+       int i;
 
        if (argc < 2) {
                usage(argv[0]);
-               exit(1);
+               exit(0);
        }
 
        memset(data, 0, sizeof *data);
        data->args.interval = 1;
 
        for (i = 1; i < argc; i++) {
-               for (j = 0; j < size; j++) {
-                       if (!strncmp(argv[i]+1, "qo", 512)) {
-                               data->do_query = 1;
-                               parse_arg_qo(data, argv[++i]);
-                               break;
-                       } else if (!strncmp(argv[i]+1, "v", 512)) {
-                               data->do_vblank = 1;
-                               parse_arg_v(data, argv[++i]);
-                               break;
-                       } else {
-                               usage(argv[0]);
-                               exit(1);
-                       }
+               if (!strncmp(argv[i] + 1, "qo", 2)) {
+                       data->do_query = 1;
+                       parse_arg_qo(data, argv[++i]);
+               } else if (!strncmp(argv[i] + 1, "v", 1)) {
+                       data->do_vblank = 1;
+                       parse_arg_v(data, argv[++i]);
+               } else if (!strncmp(argv[i] + 1, "V", 1)) {
+                       data->do_voutput = 1;
+               } else {
+                       usage(argv[0]);
+                       exit(0);
                }
        }
 }
 
-static unsigned long
-get_time_in_micros(void)
+static double
+get_time(void)
 {
        struct timespec tp;
 
        if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
-               return (unsigned long)(tp.tv_sec * 1000000) + (tp.tv_nsec / 1000L);
+               return (double)tp.tv_sec + ((double)tp.tv_nsec) / 1000000000.0;
 
        return 0;
 }
@@ -229,35 +228,28 @@ _client_vblank_handler(tdm_client_vblank *vblank, tdm_error error, unsigned int
                                           unsigned int tv_sec, unsigned int tv_usec, void *user_data)
 {
        tdm_test_client *data = user_data;
-       unsigned long cur, vbl;
-       static unsigned long p_vbl = 0;
+       double cur, vbl;
+       static double p_vbl = 0;
 
        data->waiting = 0;
 
        if (error == TDM_ERROR_DPMS_OFF) {
                printf("exit: dpms off\n");
-               exit(1);
+               exit(0);
        }
 
        if (error != TDM_ERROR_NONE) {
                printf("exit: error(%d)\n", error);
-               exit(1);
+               exit(0);
        }
 
-       cur = get_time_in_micros();
-       vbl = (unsigned long)tv_sec * (unsigned long)1000000 + (unsigned long)tv_usec;
+       cur = get_time();
+       vbl = (double)tv_sec + ((double)tv_usec) / 1000000.0;
 
-       printf("vblank              : %ld us vbl(%lu)\n", vbl - p_vbl, vbl);
+       printf("vblank              : %.6f us vbl(%.6f)\n", vbl - p_vbl, vbl);
 
-       if (cur - vbl > 2000) /* 2ms */
-               printf("kernel -> tdm-client: %ld us\n", cur - vbl);
-
-       if (tdm_debug) {
-               static unsigned long p_cur = 0;
-               printf("vblank event interval: %ld %ld\n",
-                          vbl - p_vbl, cur - p_cur);
-               p_cur = cur;
-       }
+       if (cur - vbl > 0.002) /* 2ms */
+               printf("kernel -> tdm-client: %.0f us\n", (cur - vbl) * 1000000.0);
 
        p_vbl = vbl;
 }
@@ -290,9 +282,12 @@ do_query(tdm_test_client *data)
                return;
        }
 
-       tdm_client_output_get_conn_status(output, &status);
-       tdm_client_output_get_dpms(output, &dpms);
-       tdm_client_output_get_refresh_rate(output, &refresh);
+       error = tdm_client_output_get_conn_status(output, &status);
+       TDM_WARNING_IF_FAIL(error == TDM_ERROR_NONE);
+       error = tdm_client_output_get_dpms(output, &dpms);
+       TDM_WARNING_IF_FAIL(error == TDM_ERROR_NONE);
+       error = tdm_client_output_get_refresh_rate(output, &refresh);
+       TDM_WARNING_IF_FAIL(error == TDM_ERROR_NONE);
 
        printf("tdm_output \"%s\"\n", data->args.output_name);
        printf("\tstatus : %s\n", conn_str[status]);
@@ -309,13 +304,14 @@ do_vblank(tdm_test_client *data)
        int fd = -1;
        struct pollfd fds;
 
-       output = tdm_client_get_output(data->client, NULL, &error);
+       output = tdm_client_get_output(data->client, data->args.output_name, &error);
        if (error != TDM_ERROR_NONE) {
                printf("tdm_client_get_output failed\n");
                return;
        }
 
-       tdm_client_output_add_change_handler(output, _client_output_handler, NULL);
+       error = tdm_client_output_add_change_handler(output, _client_output_handler, NULL);
+       TDM_WARNING_IF_FAIL(error == TDM_ERROR_NONE);
 
        vblank = tdm_client_output_create_vblank(output, &error);
        if (error != TDM_ERROR_NONE) {
@@ -323,11 +319,18 @@ do_vblank(tdm_test_client *data)
                return;
        }
 
-       tdm_client_vblank_set_enable_fake(vblank, data->args.enable_fake);
-       tdm_client_vblank_set_sync(vblank, data->args.sync);
-       if (data->args.fps > 0)
-               tdm_client_vblank_set_fps(vblank, data->args.fps);
-       tdm_client_vblank_set_offset(vblank, data->args.offset);
+       error = tdm_client_vblank_set_name(vblank, data->args.vblank_name);
+       TDM_WARNING_IF_FAIL(error == TDM_ERROR_NONE);
+       error = tdm_client_vblank_set_enable_fake(vblank, data->args.enable_fake);
+       TDM_WARNING_IF_FAIL(error == TDM_ERROR_NONE);
+       error = tdm_client_vblank_set_sync(vblank, data->args.sync);
+       TDM_WARNING_IF_FAIL(error == TDM_ERROR_NONE);
+       if (data->args.fps > 0) {
+               error = tdm_client_vblank_set_fps(vblank, data->args.fps);
+               TDM_WARNING_IF_FAIL(error == TDM_ERROR_NONE);
+       }
+       error = tdm_client_vblank_set_offset(vblank, data->args.offset);
+       TDM_WARNING_IF_FAIL(error == TDM_ERROR_NONE);
 
        error = tdm_client_get_fd(data->client, &fd);
        if (error != TDM_ERROR_NONE || fd < 0) {
@@ -380,6 +383,165 @@ done:
                tdm_client_vblank_destroy(vblank);
 }
 
+static void
+_dump_buffer(tbm_surface_h buffer, int count)
+{
+       char temp[TDM_PATH_LEN] = {0,};
+       tbm_format tformat;
+       const char *ext, *file_exts[2] = {"png", "yuv"};
+
+       tformat = tbm_surface_get_format(buffer);
+
+       if (IS_RGB(tformat))
+               ext = file_exts[0];
+       else
+               ext = file_exts[1];
+
+       snprintf(temp, TDM_PATH_LEN, "%c%c%c%c_%dx%d_%d",
+               FOURCC_STR(tbm_surface_get_format(buffer)),
+               tbm_surface_get_width(buffer),
+               tbm_surface_get_height(buffer),
+               count);
+       tbm_surface_internal_capture_buffer(buffer, "/tmp", temp, ext);
+}
+
+static void
+_voutput_commit(tdm_client_voutput *voutput, tbm_surface_h buffer, void *user_data)
+{
+       tdm_test_client *data = (tdm_test_client *)user_data;
+       static int count = 0;
+
+       TDM_EXIT_IF_FAIL(data != NULL);
+       TDM_EXIT_IF_FAIL(buffer != NULL);
+
+       if ((count < 10) || (count >= 31 && count <= 40))
+               _dump_buffer(buffer, count);
+       count++;
+
+       if (count == 30) {
+               printf("client: %d commited(%p), mode change request to index 1\n", count, buffer);
+               tdm_client_voutput_set_mode(data->voutput, 1);
+       } else if (count == 50) {
+               printf("client: %d commited(%p), disconnect\n", count, buffer);
+               tdm_client_voutput_disconnect(data->voutput);
+       } else {
+               printf("client: %d commited(%p)\n", count, buffer);
+       }
+
+       tdm_client_voutput_commit_done(voutput);
+}
+
+static void
+_voutput_output_handler(tdm_client_output *output, tdm_output_change_type type,
+                                          tdm_value value, void *user_data)
+{
+       tdm_client_voutput *voutput = NULL;
+       tdm_output_conn_status status;
+       tdm_test_client *data;
+       unsigned int width, height;
+
+       data = (tdm_test_client *) user_data;
+       TDM_RETURN_IF_FAIL(data != NULL);
+       voutput = data->voutput;
+       TDM_RETURN_IF_FAIL(voutput != NULL);
+
+       if (type == TDM_OUTPUT_CHANGE_CONNECTION) {
+               status = (tdm_output_conn_status)value.u32;
+               printf("output %s.\n", conn_str[value.u32]);
+
+               if (status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
+                       printf("client: disconnected, destroy voutput\n");
+                       tdm_client_output_remove_change_handler(output, _voutput_output_handler, data);
+#if CHECK_V_STEP
+                       printf("press enter to continuet\n");
+                       getchar();
+#endif
+                       tdm_client_voutput_destroy(voutput);
+               } else if (status == TDM_OUTPUT_CONN_STATUS_CONNECTED) {
+                       printf("client: connected\n");
+               } else if (status == TDM_OUTPUT_CONN_STATUS_MODE_SETTED) {
+                       tdm_client_output_get_mode(output, &width, &height);
+                       printf("client: mode setted(%dx%d)\n", width, height);
+#if CHECK_V_STEP
+                       printf("press enter to continuet\n");
+                       getchar();
+#endif
+               }
+       } else if (type == TDM_OUTPUT_CHANGE_DPMS) {
+               printf("output %s.\n", dpms_str[value.u32]);
+       }
+}
+
+static void
+_voutput_make_available_mode(tdm_client_output_mode *modes, int count)
+{
+       int i;
+       for (i = 0 ; i < count; i++) {
+               modes[i].clock = 25200;
+               modes[i].hdisplay = 640 * (count - i);
+               modes[i].hsync_start = 656;
+               modes[i].hsync_end = 752;
+               modes[i].htotal = 800;
+               modes[i].hskew = 0;
+               modes[i].vdisplay = 480 * (count - i);
+               modes[i].vsync_start = 490;
+               modes[i].vsync_end = 492;
+               modes[i].vtotal = 525;
+               modes[i].vscan = 0;
+               modes[i].vrefresh = 30;
+               modes[i].flags = 0;
+               modes[i].type = 0;
+               snprintf(modes[i].name, TDM_NAME_LEN, "%dx%d_%d", modes[i].hdisplay, modes[i].vdisplay, i);
+       }
+}
+
+static void
+do_voutput(tdm_test_client *data)
+{
+       tdm_client_voutput *voutput = NULL;
+       tdm_client_output *output = NULL;
+       tdm_client_output_mode modes[2];
+       tdm_error ret = TDM_ERROR_NONE;
+
+       printf("virtual output test - client\n");
+
+       voutput = tdm_client_create_voutput(data->client, "virtual-test", &ret);
+       TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
+
+       ret = tdm_client_voutput_add_commit_handler(voutput, _voutput_commit, data);
+       TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, done);
+
+       output = tdm_client_voutput_get_client_output(voutput, &ret);
+       TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, done);
+
+       ret = tdm_client_output_add_change_handler(output, _voutput_output_handler, data);
+       TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, done);
+
+       ret = tdm_client_voutput_set_physical_size(voutput, 300, 200);
+       TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, done);
+
+       _voutput_make_available_mode(modes, 2);
+       ret = tdm_client_voutput_set_available_modes(voutput, modes, 2);
+       TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, done);
+#if CHECK_V_STEP
+       printf("virtual output test - press enter to connect\n");
+       getchar();
+#endif
+       ret = tdm_client_voutput_connect(voutput);
+       TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, done);
+
+       data->voutput = voutput;
+       data->output = output;
+
+       while (1) {
+               tdm_client_handle_events_timeout(data->client, 1000);
+       }
+
+done:
+       if (voutput)
+               tdm_client_voutput_destroy(voutput);
+}
+
 static tdm_test_client ttc_data;
 
 int
@@ -387,17 +549,32 @@ main(int argc, char *argv[])
 {
        tdm_test_client *data = &ttc_data;
        tdm_error error;
-       const char *debug;
 
-       debug = getenv("TDM_DEBUG");
-       if (debug && (strstr(debug, "1")))
-               tdm_debug = 1;
+       /* for testing */
+       const char *xdg = (const char*)getenv("XDG_RUNTIME_DIR");
+       if (!xdg) {
+               char buf[32];
+               snprintf(buf, sizeof(buf), "/run");
+               int ret = setenv("XDG_RUNTIME_DIR", (const char*)buf, 1);
+               if (ret != 0)
+                       exit(0);
+       }
+
+       /* for tbm_bufmgr_init */
+       const char *s  = (const char*)getenv("TBM_DISPLAY_SERVER");
+       if (!s) {
+               char buf[32];
+               snprintf(buf, sizeof(buf), "1");
+               int ret = setenv("TBM_DISPLAY_SERVER", (const char*)buf, 1);
+               if (ret != 0)
+                       exit(0);
+       }
 
        parse_args(data, argc, argv);
 
-       printf("sync(%d) fps(%d) interval(%d) offset(%d) enable_fake(%d)\n",
+       printf("sync(%d) fps(%d) interval(%d) offset(%d) enable_fake(%d) pid(%d)\n",
                   data->args.sync, data->args.fps, data->args.interval,
-                  data->args.offset, data->args.enable_fake);
+                  data->args.offset, data->args.enable_fake, data->args.pid);
 
        data->client = tdm_client_create(&error);
        if (error != TDM_ERROR_NONE) {
@@ -409,8 +586,14 @@ main(int argc, char *argv[])
                do_query(data);
        if (data->do_vblank)
                do_vblank(data);
+       if (data->do_voutput)
+               do_voutput(data);
 
 done:
+       if (data->args.output_name)
+               free(data->args.output_name);
+       if (data->args.vblank_name)
+               free(data->args.vblank_name);
        if (data->client)
                tdm_client_destroy(data->client);