clients: refactor to prevent access to global variable 70/280970/1
authorduna.oh <duna.oh@samsung.com>
Wed, 7 Sep 2022 05:41:25 +0000 (14:41 +0900)
committerTizen Window System <tizen.windowsystem@gmail.com>
Wed, 7 Sep 2022 08:07:11 +0000 (17:07 +0900)
Change-Id: Ia2283b124fb4b19473d8ca173a017c434153271a

clients/input-generator.c

index cfa1800..c8aee23 100644 (file)
@@ -200,10 +200,10 @@ static const struct layout numeric_layout = {
 struct display data_wl;
 
 static void
-request_key(uint32_t time, const struct key *key, enum wl_keyboard_key_state state);
+request_key(struct display *d, uint32_t time, const struct key *key, enum wl_keyboard_key_state state);
 
 static const struct layout *
-get_current_layout(void);
+get_current_layout(struct display *d);
 
 static void
 usage(void)
@@ -273,77 +273,77 @@ usage(void)
 }
 
 static void
-init_input_generator(enum tizen_input_device_manager_clas clas)
+init_input_generator(struct display *d, enum tizen_input_device_manager_clas clas)
 {
-    if (data_wl.init) {
+    if (d->init) {
         printf("Already init input generator\n");
         return;
     }
 
-    tizen_input_device_manager_init_generator(data_wl.devicemgr, clas);
+    tizen_input_device_manager_init_generator(d->devicemgr, clas);
 
-    while (data_wl.request_notified == -1)
-        wl_display_dispatch_queue(data_wl.display, data_wl.queue);
+    while (d->request_notified == -1)
+        wl_display_dispatch_queue(d->display, d->queue);
 
-    if (data_wl.request_notified == TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE) {
-        data_wl.init = 1;
+    if (d->request_notified == TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE) {
+        d->init = 1;
         printf("Success to init input generator\n");
     } else {
-        printf("Failed to init input generator: %d\n", data_wl.request_notified);
+        printf("Failed to init input generator: %d\n", d->request_notified);
     }
 
-    data_wl.clas = clas;
-    data_wl.request_notified = -1;
+    d->clas = clas;
+    d->request_notified = -1;
 }
 
 static void
-deinit_input_generator(void)
+deinit_input_generator(struct display *d)
 {
-    if (!data_wl.init) {
+    if (!d->init) {
         printf("input generator is not initialized\n");
         return;
     }
 
-    tizen_input_device_manager_deinit_generator(data_wl.devicemgr, data_wl.clas);
+    tizen_input_device_manager_deinit_generator(d->devicemgr, d->clas);
 
-    while (data_wl.request_notified == -1)
-        wl_display_dispatch_queue(data_wl.display, data_wl.queue);
+    while (d->request_notified == -1)
+        wl_display_dispatch_queue(d->display, d->queue);
 
-    if (data_wl.request_notified == TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE) {
-        data_wl.init = 0;
+    if (d->request_notified == TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE) {
+        d->init = 0;
         printf("Success to deinit input generator\n");
     } else {
-        printf("Failed to deinit input generator: %d\n", data_wl.request_notified);
+        printf("Failed to deinit input generator: %d\n", d->request_notified);
     }
 
-    data_wl.request_notified = -1;
+    d->request_notified = -1;
 }
 
 static void
-input_generator_key(char *name, int type)
+input_generator_key(struct display *d, char *name, int type)
 {
-    tizen_input_device_manager_generate_key(data_wl.devicemgr, name, !!type);
+    tizen_input_device_manager_generate_key(d->devicemgr, name, !!type);
 
-    while (data_wl.request_notified == -1)
-        wl_display_dispatch_queue(data_wl.display, data_wl.queue);
+    while (d->request_notified == -1)
+        wl_display_dispatch_queue(d->display, d->queue);
 
-    if (data_wl.request_notified == TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE) {
-        if (data_wl.enable_log) {
+    if (d->request_notified == TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE) {
+        if (d->enable_log) {
             printf("Success to generate key: %s key %s\n", name, type?"down":"up");
         }
     } else {
-        printf("Failed to generate %s key %s: %d\n", name, type?"down":"up", data_wl.request_notified);
+        printf("Failed to generate %s key %s: %d\n", name, type?"down":"up", d->request_notified);
     }
 
-    data_wl.request_notified = -1;
+    d->request_notified = -1;
 }
 
 static void
-key_generate(char *name, int type)
+key_generate(struct display *d, char *name, int type)
 {
     printf("name: %s, type: %d\n", name, type);
 
-    if (!data_wl.init) {
+    if (!d->init) {
         printf("Input generator is not initialized\n");
         return;
     }
@@ -354,10 +354,10 @@ key_generate(char *name, int type)
     }
 
     if (type == KEY_ALL) {
-        input_generator_key(name, 1);
-        input_generator_key(name, 0);
+        input_generator_key(d, name, 1);
+        input_generator_key(d, name, 0);
     } else {
-        input_generator_key(name, !!type);
+        input_generator_key(d, name, !!type);
     }
 }
 
@@ -377,94 +377,94 @@ type_string_get(int type)
 }
 
 static void
-input_generator_touch(int idx, int type, int x, int y)
+input_generator_touch(struct display *d, int idx, int type, int x, int y)
 {
-    tizen_input_device_manager_generate_touch(data_wl.devicemgr, type, x, y, idx);
+    tizen_input_device_manager_generate_touch(d->devicemgr, type, x, y, idx);
 
-    while (data_wl.request_notified == -1)
-        wl_display_dispatch_queue(data_wl.display, data_wl.queue);
+    while (d->request_notified == -1)
+        wl_display_dispatch_queue(d->display, d->queue);
 
-    if (data_wl.request_notified == TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE) {
-        if (data_wl.enable_log) {
+    if (d->request_notified == TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE) {
+        if (d->enable_log) {
             printf("Success to generate touch: %d finger %s on (%d, %d)\n", idx, type_string_get(type), x, y);
         }
     } else {
-        printf("Failed to generate touch(%d finger %s on (%d, %d)): %d\n", idx, type_string_get(type), x, y, data_wl.request_notified);
+        printf("Failed to generate touch(%d finger %s on (%d, %d)): %d\n", idx, type_string_get(type), x, y, d->request_notified);
     }
 
-    data_wl.request_notified = -1;
+    d->request_notified = -1;
 }
 
 static void
-touch_generate(int idx, int type, int x, int y)
+touch_generate(struct display *d, int idx, int type, int x, int y)
 {
-    if (!data_wl.init) {
+    if (!d->init) {
         printf("Input generator is not initialized\n");
         return;
     }
 
     if (type == POINTER_ALL) {
-        input_generator_touch(0, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_BEGIN, 300, 300);
-        input_generator_touch(1, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_BEGIN, 400, 400);
-        input_generator_touch(2, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_BEGIN, 500, 500);
+        input_generator_touch(d, 0, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_BEGIN, 300, 300);
+        input_generator_touch(d, 1, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_BEGIN, 400, 400);
+        input_generator_touch(d, 2, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_BEGIN, 500, 500);
 
-        input_generator_touch(0, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 310, 310);
-        input_generator_touch(1, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 410, 410);
-        input_generator_touch(2, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 510, 510);
+        input_generator_touch(d, 0, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 310, 310);
+        input_generator_touch(d, 1, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 410, 410);
+        input_generator_touch(d, 2, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 510, 510);
 
-        input_generator_touch(0, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 320, 320);
-        input_generator_touch(1, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 420, 420);
-        input_generator_touch(2, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 520, 520);
+        input_generator_touch(d, 0, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 320, 320);
+        input_generator_touch(d, 1, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 420, 420);
+        input_generator_touch(d, 2, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 520, 520);
 
-        input_generator_touch(0, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_END, 320, 320);
-        input_generator_touch(1, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_END, 420, 420);
-        input_generator_touch(2, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_END, 520, 520);
+        input_generator_touch(d, 0, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_END, 320, 320);
+        input_generator_touch(d, 1, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_END, 420, 420);
+        input_generator_touch(d, 2, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_END, 520, 520);
     } else {
-        input_generator_touch(idx, type, x, y);
+        input_generator_touch(d, idx, type, x, y);
     }
 }
 
 static void
-input_generator_mouse(int type, int x, int y, int button)
+input_generator_mouse(struct display *d, int type, int x, int y, int button)
 {
-    tizen_input_device_manager_generate_pointer(data_wl.devicemgr, type, x, y, button);
+    tizen_input_device_manager_generate_pointer(d->devicemgr, type, x, y, button);
 
-    while (data_wl.request_notified == -1)
-        wl_display_dispatch_queue(data_wl.display, data_wl.queue);
+    while (d->request_notified == -1)
+        wl_display_dispatch_queue(d->display, d->queue);
 
-    if (data_wl.request_notified == TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE) {
-        if (data_wl.enable_log) {
+    if (d->request_notified == TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE) {
+        if (d->enable_log) {
             printf("Success to generate mouse: %s on (%d, %d)\n", type_string_get(type), x, y);
         }
     } else {
-        printf("Failed to generate mouse(%s on (%d, %d)): %d\n", type_string_get(type), x, y, data_wl.request_notified);
+        printf("Failed to generate mouse(%s on (%d, %d)): %d\n", type_string_get(type), x, y, d->request_notified);
     }
 
-    data_wl.request_notified = -1;
+    d->request_notified = -1;
 }
 
 static void
-mouse_generate(int btn, int type, int x, int y)
+mouse_generate(struct display *d, int btn, int type, int x, int y)
 {
-    if (!data_wl.init) {
+    if (!d->init) {
         printf("Input generator is not initialized\n");
         return;
     }
 
     if (type == POINTER_ALL) {
-        input_generator_mouse(TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_BEGIN, -1, -1, 1);
-        input_generator_mouse(TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 10, 10, 1);
-        input_generator_mouse(TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 10, 10, 1);
-        input_generator_mouse(TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 10, 10, 1);
-        input_generator_mouse(TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_END, -1, -1, 1);
+        input_generator_mouse(d, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_BEGIN, -1, -1, 1);
+        input_generator_mouse(d, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 10, 10, 1);
+        input_generator_mouse(d, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 10, 10, 1);
+        input_generator_mouse(d, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 10, 10, 1);
+        input_generator_mouse(d, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_END, -1, -1, 1);
 
     } else {
-        input_generator_mouse(type, x, y, btn);
+        input_generator_mouse(d, type, x, y, btn);
     }
 }
 
 static void
-stdin_read(void)
+stdin_read(struct display *d)
 {
     int c;
     char buf[MAX_STR] = {0, }, *tmp, *buf_ptr, key_name[MAX_STR] = {0, };
@@ -492,11 +492,11 @@ stdin_read(void)
                 switch (count) {
                     case 0:
                         if (!strncmp("keyboard", tmp, MAX_STR-1))
-                            init_input_generator(TIZEN_INPUT_DEVICE_MANAGER_CLAS_KEYBOARD);
+                            init_input_generator(d, TIZEN_INPUT_DEVICE_MANAGER_CLAS_KEYBOARD);
                         else if (!strncmp("touch", tmp, MAX_STR-1))
-                            init_input_generator(TIZEN_INPUT_DEVICE_MANAGER_CLAS_TOUCHSCREEN);
+                            init_input_generator(d, TIZEN_INPUT_DEVICE_MANAGER_CLAS_TOUCHSCREEN);
                         else if (!strncmp("mouse", tmp, MAX_STR-1))
-                            init_input_generator(TIZEN_INPUT_DEVICE_MANAGER_CLAS_MOUSE);
+                            init_input_generator(d, TIZEN_INPUT_DEVICE_MANAGER_CLAS_MOUSE);
                         break;
                     default:
                         break;
@@ -505,7 +505,7 @@ stdin_read(void)
             count++;
         }
     } else if (!strncmp(tmp, "deinit", sizeof("deinit"))) {
-        deinit_input_generator();
+        deinit_input_generator(d);
     } else if (!strncmp(tmp, "key", sizeof("key"))) {
         while (tmp) {
             tmp = strtok_r(NULL, " ", &buf_ptr);
@@ -523,7 +523,7 @@ stdin_read(void)
             }
             count++;
         }
-        key_generate(key_name, key_type);
+        key_generate(d, key_name, key_type);
     } else if (!strncmp(tmp, "touch", sizeof("touch"))) {
         while (tmp) {
             tmp = strtok_r(NULL, " ", &buf_ptr);
@@ -547,7 +547,7 @@ stdin_read(void)
             }
             count++;
         }
-        touch_generate(idx, type, x, y);
+        touch_generate(d, idx, type, x, y);
     } else if (!strncmp(tmp, "mouse", sizeof("mouse"))) {
         while (tmp) {
             tmp = strtok_r(NULL, " ", &buf_ptr);
@@ -571,7 +571,7 @@ stdin_read(void)
             }
             count++;
         }
-        mouse_generate(idx, type, x, y);
+        mouse_generate(d, idx, type, x, y);
     }  else if (!strncmp(tmp, "ime", MAX_STR)) {
         while (tmp) {
             tmp = strtok_r(NULL, " ", &buf_ptr);
@@ -589,27 +589,27 @@ stdin_read(void)
             }
             count++;
         }
-        if (data_wl.enable_log)
+        if (d->enable_log)
             printf("request key() key_idx:%u, key_type:%u\n", idx, key_type);
 
-        layout = get_current_layout();
+        layout = get_current_layout(d);
         if (key_type == KEY_ALL) {
-            request_key(time++, &layout->keys[idx], WL_KEYBOARD_KEY_STATE_PRESSED);
-            request_key(time++, &layout->keys[idx], WL_KEYBOARD_KEY_STATE_RELEASED);
+            request_key(d, time++, &layout->keys[idx], WL_KEYBOARD_KEY_STATE_PRESSED);
+            request_key(d, time++, &layout->keys[idx], WL_KEYBOARD_KEY_STATE_RELEASED);
         }
         else
-            request_key(time++, &layout->keys[idx], key_type);
+            request_key(d, time++, &layout->keys[idx], key_type);
     } else if (!strncmp(buf, "q", MAX_STR) || !strncmp(buf, "quit", MAX_STR)) {
-        data_wl.run = 0;
+        d->run = 0;
     } else if (!strncmp(buf, "help", MAX_STR)) {
         usage();
     } else if (!strncmp(buf, "log", MAX_STR)) {
-        if (data_wl.enable_log)
+        if (d->enable_log)
             printf("Disable detailed logs\n");
         else
             printf("Enable detailed logs\n");
 
-        data_wl.enable_log = !data_wl.enable_log;
+        d->enable_log = !d->enable_log;
     } else {
         printf("Invalid arguments\n");
         usage();
@@ -621,9 +621,11 @@ input_device_manager_handle_error(void *data,
         struct tizen_input_device_manager *tizen_input_device_manager,
         uint32_t errorcode)
 {
-    if (data_wl.enable_log)
+    struct display *d = (struct display *)data;
+
+    if (d->enable_log)
         printf("errorcode: %d\n", errorcode);
-    data_wl.request_notified = errorcode;
+    d->request_notified = errorcode;
 }
 
 static const struct tizen_input_device_manager_listener _input_device_manager_listener =
@@ -635,9 +637,9 @@ static const struct tizen_input_device_manager_listener _input_device_manager_li
 };
 
 static const struct layout *
-get_current_layout(void)
+get_current_layout(struct display *d)
 {
-    switch (data_wl.content_purpose) {
+    switch (d->content_purpose) {
         case WL_TEXT_INPUT_CONTENT_PURPOSE_DIGITS:
         case WL_TEXT_INPUT_CONTENT_PURPOSE_NUMBER:
             return &numeric_layout;
@@ -657,36 +659,36 @@ prev_utf8_char(const char *s, const char *p)
 }
 
 static void
-delete_before_cursor(void)
+delete_before_cursor(struct display *d)
 {
     const char *start, *end;
 
-    if (!data_wl.surrounding_text) {
+    if (!d->surrounding_text) {
         printf("delete_before_cursor: No surrounding text available\n");
         return;
     }
 
-    start = prev_utf8_char(data_wl.surrounding_text,
-                   data_wl.surrounding_text + data_wl.surrounding_cursor);
+    start = prev_utf8_char(d->surrounding_text,
+                   d->surrounding_text + d->surrounding_cursor);
     if (!start) {
         printf("delete_before_cursor: No previous character to delete\n");
         return;
     }
 
-    end = data_wl.surrounding_text + data_wl.surrounding_cursor;
+    end = d->surrounding_text + d->surrounding_cursor;
 
-    zwp_input_method_context_v1_delete_surrounding_text(data_wl.context,
-                                (start - data_wl.surrounding_text) - data_wl.surrounding_cursor,
+    zwp_input_method_context_v1_delete_surrounding_text(d->context,
+                                (start - d->surrounding_text) - d->surrounding_cursor,
                                 end - start);
-    zwp_input_method_context_v1_commit_string(data_wl.context,
-                          data_wl.serial,
+    zwp_input_method_context_v1_commit_string(d->context,
+                          d->serial,
                           "");
 
     /* Update surrounding text */
-    data_wl.surrounding_cursor = start - data_wl.surrounding_text;
-    data_wl.surrounding_text[data_wl.surrounding_cursor] = '\0';
+    d->surrounding_cursor = start - d->surrounding_text;
+    d->surrounding_text[d->surrounding_cursor] = '\0';
     if (*end)
-        memmove(data_wl.surrounding_text + data_wl.surrounding_cursor, end, strlen(end));
+        memmove(d->surrounding_text + d->surrounding_cursor, end, strlen(end));
 }
 
 static char *
@@ -709,26 +711,26 @@ append(char *s1, const char *s2)
 }
 
 static void
-request_preedit(int32_t cursor)
+request_preedit(struct display *d, int32_t cursor)
 {
-    printf("request_preedit() preedit_string:%s\n", data_wl.preedit_string);
-    uint32_t index = strlen(data_wl.preedit_string);
+    printf("request_preedit() preedit_string:%s\n", d->preedit_string);
+    uint32_t index = strlen(d->preedit_string);
 
-    if (!data_wl.context) return;
+    if (!d->context) return;
 
-    if (data_wl.preedit_style)
-        zwp_input_method_context_v1_preedit_styling(data_wl.context,
+    if (d->preedit_style)
+        zwp_input_method_context_v1_preedit_styling(d->context,
                                 0,
-                                strlen(data_wl.preedit_string),
-                                data_wl.preedit_style);
+                                strlen(d->preedit_string),
+                                d->preedit_style);
     if (cursor > 0)
         index = cursor;
-    zwp_input_method_context_v1_preedit_cursor(data_wl.context,
+    zwp_input_method_context_v1_preedit_cursor(d->context,
                            index);
-    zwp_input_method_context_v1_preedit_string(data_wl.context,
-                           data_wl.serial,
-                           data_wl.preedit_string,
-                           data_wl.preedit_string);
+    zwp_input_method_context_v1_preedit_string(d->context,
+                           d->serial,
+                           d->preedit_string,
+                           d->preedit_string);
 }
 
 static char *
@@ -752,48 +754,48 @@ insert_text(const char *text, uint32_t offset, const char *insert)
 }
 
 static void
-request_commit_string(void)
+request_commit_string(struct display *d)
 {
     char *surrounding_text;
 
     printf("request_commit_string()\n");
 
-    if (!data_wl.preedit_string ||
-        strlen(data_wl.preedit_string) == 0)
+    if (!d->preedit_string ||
+        strlen(d->preedit_string) == 0)
         return;
-    printf("request_commit_string() preedit_string:%s\n", data_wl.preedit_string);
+    printf("request_commit_string() preedit_string:%s\n", d->preedit_string);
 
-    if (!data_wl.context) return;
+    if (!d->context) return;
 
-    zwp_input_method_context_v1_cursor_position(data_wl.context,
+    zwp_input_method_context_v1_cursor_position(d->context,
                             0, 0);
-    zwp_input_method_context_v1_commit_string(data_wl.context,
-                          data_wl.serial,
-                          data_wl.preedit_string);
-
-    if (data_wl.surrounding_text) {
-        surrounding_text = insert_text(data_wl.surrounding_text,
-                           data_wl.surrounding_cursor,
-                           data_wl.preedit_string);
-        free(data_wl.surrounding_text);
-        data_wl.surrounding_text = surrounding_text;
-        data_wl.surrounding_cursor += strlen(data_wl.preedit_string);
+    zwp_input_method_context_v1_commit_string(d->context,
+                          d->serial,
+                          d->preedit_string);
+
+    if (d->surrounding_text) {
+        surrounding_text = insert_text(d->surrounding_text,
+                           d->surrounding_cursor,
+                           d->preedit_string);
+        free(d->surrounding_text);
+        d->surrounding_text = surrounding_text;
+        d->surrounding_cursor += strlen(d->preedit_string);
     } else {
-        data_wl.surrounding_text = strdup(data_wl.preedit_string);
-        data_wl.surrounding_cursor = strlen(data_wl.preedit_string);
+        d->surrounding_text = strdup(d->preedit_string);
+        d->surrounding_cursor = strlen(d->preedit_string);
     }
 
-    free(data_wl.preedit_string);
-    data_wl.preedit_string = strdup("");
+    free(d->preedit_string);
+    d->preedit_string = strdup("");
 }
 
 static void
-request_key(uint32_t time, const struct key *key, enum wl_keyboard_key_state state)
+request_key(struct display *d, uint32_t time, const struct key *key, enum wl_keyboard_key_state state)
 {
     const char *label = NULL;
     static uint32_t serial = 9999;
 
-    switch(data_wl.state) {
+    switch(d->state) {
     case KEYBOARD_STATE_DEFAULT :
         label = key->label;
         break;
@@ -808,37 +810,37 @@ request_key(uint32_t time, const struct key *key, enum wl_keyboard_key_state sta
         break;
     }
 
-    xkb_mod_mask_t mod_mask = data_wl.state == KEYBOARD_STATE_DEFAULT ? 0 : data_wl.keysym.shift_mask;
+    xkb_mod_mask_t mod_mask = d->state == KEYBOARD_STATE_DEFAULT ? 0 : d->keysym.shift_mask;
     uint32_t key_state = (state == WL_KEYBOARD_KEY_STATE_PRESSED) ? WL_KEYBOARD_KEY_STATE_PRESSED : WL_KEYBOARD_KEY_STATE_RELEASED;
 
-    printf("request_key() data_wl.state:%d, key->key_type:%d, pressed:%u, label:%s\n", data_wl.state, key->key_type, key_state, label);
+    printf("request_key() d->state:%d, key->key_type:%d, pressed:%u, label:%s\n", d->state, key->key_type, key_state, label);
     switch (key->key_type) {
         case keytype_default:
             if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
                 break;
 
-            printf("request_key() keytype_default. append label(%s) to preedit_string(%s)\n", label, data_wl.preedit_string);
-            data_wl.preedit_string =
-                append(data_wl.preedit_string,
+            printf("request_key() keytype_default. append label(%s) to preedit_string(%s)\n", label, d->preedit_string);
+            d->preedit_string =
+                append(d->preedit_string,
                        label);
-            request_preedit(-1);
+            request_preedit(d, -1);
             break;
         case keytype_backspace:
             if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
                 break;
 
-            if (strlen(data_wl.preedit_string) == 0) {
-                delete_before_cursor();
+            if (strlen(d->preedit_string) == 0) {
+                delete_before_cursor(d);
             } else {
-                data_wl.preedit_string[strlen(data_wl.preedit_string) - 1] = '\0';
-                request_preedit(-1);
+                d->preedit_string[strlen(d->preedit_string) - 1] = '\0';
+                request_preedit(d, -1);
             }
             break;
         case keytype_enter:
-            request_commit_string();
+            request_commit_string(d);
 
-            if (!data_wl.context) break;
-            zwp_input_method_context_v1_keysym(data_wl.context,
+            if (!d->context) break;
+            zwp_input_method_context_v1_keysym(d->context,
                                serial++,
                                time,
                                XKB_KEY_Return, key_state, mod_mask);
@@ -846,76 +848,76 @@ request_key(uint32_t time, const struct key *key, enum wl_keyboard_key_state sta
         case keytype_space:
             if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
                 break;
-            data_wl.preedit_string =
-                append(data_wl.preedit_string, " ");
-            request_commit_string();
+            d->preedit_string =
+                append(d->preedit_string, " ");
+            request_commit_string(d);
             break;
         case keytype_switch:
             if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
                 break;
-            switch(data_wl.state) {
+            switch(d->state) {
             case KEYBOARD_STATE_DEFAULT:
-                data_wl.state = KEYBOARD_STATE_UPPERCASE;
+                d->state = KEYBOARD_STATE_UPPERCASE;
                 break;
             case KEYBOARD_STATE_UPPERCASE:
-                data_wl.state = KEYBOARD_STATE_DEFAULT;
+                d->state = KEYBOARD_STATE_DEFAULT;
                 break;
             case KEYBOARD_STATE_SYMBOLS:
-                data_wl.state = KEYBOARD_STATE_UPPERCASE;
+                d->state = KEYBOARD_STATE_UPPERCASE;
                 break;
             }
             break;
         case keytype_symbols:
             if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
                 break;
-            switch(data_wl.state) {
+            switch(d->state) {
             case KEYBOARD_STATE_DEFAULT:
-                data_wl.state = KEYBOARD_STATE_SYMBOLS;
+                d->state = KEYBOARD_STATE_SYMBOLS;
                 break;
             case KEYBOARD_STATE_UPPERCASE:
-                data_wl.state = KEYBOARD_STATE_SYMBOLS;
+                d->state = KEYBOARD_STATE_SYMBOLS;
                 break;
             case KEYBOARD_STATE_SYMBOLS:
-                data_wl.state = KEYBOARD_STATE_DEFAULT;
+                d->state = KEYBOARD_STATE_DEFAULT;
                 break;
             }
             break;
         case keytype_tab:
-            request_commit_string();
-            if (!data_wl.context) break;
-            zwp_input_method_context_v1_keysym(data_wl.context,
+            request_commit_string(d);
+            if (!d->context) break;
+            zwp_input_method_context_v1_keysym(d->context,
                                serial++,
                                time,
                                XKB_KEY_Tab, key_state, mod_mask);
             break;
         case keytype_arrow_up:
-            request_commit_string();
-            if (!data_wl.context) break;
-            zwp_input_method_context_v1_keysym(data_wl.context,
+            request_commit_string(d);
+            if (!d->context) break;
+            zwp_input_method_context_v1_keysym(d->context,
                                serial++,
                                time,
                                XKB_KEY_Up, key_state, mod_mask);
             break;
         case keytype_arrow_left:
-            request_commit_string();
-            if (!data_wl.context) break;
-            zwp_input_method_context_v1_keysym(data_wl.context,
+            request_commit_string(d);
+            if (!d->context) break;
+            zwp_input_method_context_v1_keysym(d->context,
                                serial++,
                                time,
                                XKB_KEY_Left, key_state, mod_mask);
             break;
         case keytype_arrow_right:
-            request_commit_string();
-            if (!data_wl.context) break;
-            zwp_input_method_context_v1_keysym(data_wl.context,
+            request_commit_string(d);
+            if (!d->context) break;
+            zwp_input_method_context_v1_keysym(d->context,
                                serial++,
                                time,
                                XKB_KEY_Right, key_state, mod_mask);
             break;
         case keytype_arrow_down:
-            request_commit_string();
-            if (!data_wl.context) break;
-            zwp_input_method_context_v1_keysym(data_wl.context,
+            request_commit_string(d);
+            if (!d->context) break;
+            zwp_input_method_context_v1_keysym(d->context,
                                serial++,
                                time,
                                XKB_KEY_Down, key_state, mod_mask);
@@ -923,8 +925,8 @@ request_key(uint32_t time, const struct key *key, enum wl_keyboard_key_state sta
         case keytype_style:
             if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
                 break;
-            data_wl.preedit_style = (data_wl.preedit_style + 1) % 8; /* TODO */
-            request_preedit(-1);
+            d->preedit_style = (d->preedit_style + 1) % 8; /* TODO */
+            request_preedit(d, -1);
             break;
     }
 }
@@ -932,79 +934,89 @@ request_key(uint32_t time, const struct key *key, enum wl_keyboard_key_state sta
 static void
 im_context_cb_surrounding_text (void *data, struct zwp_input_method_context_v1 *im_ctx, const char *text, uint32_t cursor, uint32_t anchor)
 {
+    struct display *d = (struct display *)data;
+
     printf("im_context_cb_surrounding_text\n");
-        free(data_wl.surrounding_text);
-    data_wl.surrounding_text = strdup(text);
+    free(d->surrounding_text);
 
-    data_wl.surrounding_cursor = cursor;
+    d->surrounding_text = strdup(text);
+    d->surrounding_cursor = cursor;
 }
 
 static void
 im_context_cb_reset (void *data, struct zwp_input_method_context_v1 *im_ctx)
 {
+    struct display *d = (struct display *)data;
+
     printf("im_context_cb_reset\n");
 
-    if (strlen(data_wl.preedit_string)) {
-        free(data_wl.preedit_string);
-        data_wl.preedit_string = strdup("");
+    if (strlen(d->preedit_string)) {
+        free(d->preedit_string);
+        d->preedit_string = strdup("");
     }
 }
 
 static void
 im_context_cb_content_type (void *data, struct zwp_input_method_context_v1 *im_ctx, uint32_t hint, uint32_t purpose)
 {
+    struct display *d = (struct display *)data;
+
     printf("im_context_cb_content_type\n");
 
-    data_wl.content_hint = hint;
-    data_wl.content_purpose = purpose;
+    d->content_hint = hint;
+    d->content_purpose = purpose;
 }
 
 static void
 im_context_cb_invoke_action (void *data, struct zwp_input_method_context_v1 *im_ctx, uint32_t button, uint32_t index)
 {
+    struct display *d = (struct display *)data;
+
     printf("im_context_cb_invoke_action\n");
 
     //if (button != BTN_LEFT)
     //    return;
 
-    request_preedit(index);
+    request_preedit(d, index);
 }
 
 static void
 im_context_cb_commit_state (void *data, struct zwp_input_method_context_v1 *context, uint32_t serial)
 {
-    printf("im_context_cb_commit_state\n");
-
+    struct display *d = (struct display *)data;
     const struct layout *layout;
 
-    data_wl.serial = serial;
+    printf("im_context_cb_commit_state\n");
+    d->serial = serial;
 
-    layout = get_current_layout();
+    layout = get_current_layout(d);
 
-    if (data_wl.surrounding_text)
-        printf("Surrounding text updated: %s\n", data_wl.surrounding_text);
+    if (d->surrounding_text)
+        printf("Surrounding text updated: %s\n", d->surrounding_text);
 
     if (!context) return;
     zwp_input_method_context_v1_language(context,
-                         data_wl.serial,
+                         d->serial,
                          layout->language);
     zwp_input_method_context_v1_text_direction(context,
-                           data_wl.serial,
+                           d->serial,
                            layout->text_direction);
 }
 
 static void
 im_context_cb_preferred_language (void *data, struct zwp_input_method_context_v1 *context, const char *language)
 {
+    struct display *d = (struct display *)data;
+
     printf("im_context_cb_preferred_language\n");
 
-    if (data_wl.preferred_language)
-        free(data_wl.preferred_language);
+    if (d->preferred_language)
+        free(d->preferred_language);
 
-    data_wl.preferred_language = NULL;
+    d->preferred_language = NULL;
 
     if (language)
-        data_wl.preferred_language = strdup(language);
+        d->preferred_language = strdup(language);
 }
 
 static const struct zwp_input_method_context_v1_listener input_method_context_cb_listener = {
@@ -1078,68 +1090,74 @@ keysym_modifiers_get_mask(struct wl_array *modifiers_map,
 static void
 im_cb_activate (void *data, struct zwp_input_method_v1 *input_method, struct zwp_input_method_context_v1 *context)
 {
+    struct display *d = (struct display *)data;
+
     printf("im_cb_activate\n");
 
     struct wl_array modifiers_map;
     const struct layout *layout;
 
-    if (data_wl.context)
-        zwp_input_method_context_v1_destroy(data_wl.context);
+    if (d->context)
+        zwp_input_method_context_v1_destroy(d->context);
 
-    if (data_wl.preedit_string)
-        free(data_wl.preedit_string);
+    if (d->preedit_string)
+        free(d->preedit_string);
 
-    data_wl.preedit_style = 0;
-    data_wl.preedit_string = strdup("");
-    data_wl.content_hint = 0;
-    data_wl.content_purpose = 0;
-    free(data_wl.preferred_language);
-    data_wl.preferred_language = NULL;
-    free(data_wl.surrounding_text);
-    data_wl.surrounding_text = NULL;
+    d->preedit_style = 0;
+    d->preedit_string = strdup("");
+    d->content_hint = 0;
+    d->content_purpose = 0;
+    free(d->preferred_language);
+    d->preferred_language = NULL;
+    free(d->surrounding_text);
+    d->surrounding_text = NULL;
 
-    data_wl.serial = 0;
+    d->serial = 0;
 
-    data_wl.context = context;
+    d->context = context;
     zwp_input_method_context_v1_add_listener(context,
                          &input_method_context_cb_listener,
-                         NULL);
+                         d);
 
     wl_array_init(&modifiers_map);
     keysym_modifiers_add(&modifiers_map, "Shift");
     keysym_modifiers_add(&modifiers_map, "Control");
     keysym_modifiers_add(&modifiers_map, "Mod1");
     zwp_input_method_context_v1_modifiers_map(context, &modifiers_map);
-    data_wl.keysym.shift_mask = keysym_modifiers_get_mask(&modifiers_map, "Shift");
+    d->keysym.shift_mask = keysym_modifiers_get_mask(&modifiers_map, "Shift");
     wl_array_release(&modifiers_map);
 
-    layout = get_current_layout();
+    layout = get_current_layout(d);
 
     zwp_input_method_context_v1_language(context,
-                         data_wl.serial,
+                         d->serial,
                          layout->language);
     zwp_input_method_context_v1_text_direction(context,
-                           data_wl.serial,
+                           d->serial,
                            layout->text_direction);
 }
 static void
 im_cb_deactivate (void *data, struct zwp_input_method_v1 *input_method, struct zwp_input_method_context_v1 *im_ctx)
 {
+    struct display *d = (struct display *)data;
+
     printf("im_cb_deactivate\n");
 
-    if (!data_wl.context)
+    if (!d->context)
         return;
 
-    zwp_input_method_context_v1_destroy(data_wl.context);
-    data_wl.context = NULL;
+    zwp_input_method_context_v1_destroy(d->context);
+    d->context = NULL;
 }
 
 static void
 im_cb_destroy (void *data, struct zwp_input_method_v1 *input_method, struct zwp_input_method_context_v1 *im_ctx)
 {
+    struct display *d = (struct display *)data;
+
     printf("im_cb_destroy\n");
-    zwp_input_method_v1_destroy(data_wl.input_method);
-    data_wl.input_method = NULL;
+    zwp_input_method_v1_destroy(d->input_method);
+    d->input_method = NULL;
 }
 
 static const struct zwp_input_method_v1_listener input_method_cb_listener = {
@@ -1158,52 +1176,56 @@ static void
 registry_handle_global(void * data, struct wl_registry * registry, uint32_t id,
         const char * interface, uint32_t version)
 {
+    struct display *d = (struct display *)data;
+
     if (strcmp(interface, "wl_compositor") == 0) {
-        data_wl.compositor = wl_registry_bind(registry, id,
+        d->compositor = wl_registry_bind(registry, id,
                 &wl_compositor_interface, version);
-        if (!data_wl.compositor) {
+        if (!d->compositor) {
             printf("Failed to bind compositor.\n");
             return;
         }
-        if (data_wl.enable_log)
+        if (d->enable_log)
             printf("Success to bind compositor.\n");
     } else if (strcmp(interface, "tizen_input_device_manager") == 0) {
-        data_wl.devicemgr = wl_registry_bind(registry, id,
+        d->devicemgr = wl_registry_bind(registry, id,
                 &tizen_input_device_manager_interface, version);
-        if (!data_wl.devicemgr) {
+        if (!d->devicemgr) {
             printf("Failed to bind input device manager");
             return;
         }
-        if (data_wl.enable_log)
+        if (d->enable_log)
             printf("Success to bind tizen input device manager.");
-        tizen_input_device_manager_add_listener(data_wl.devicemgr,
-            &_input_device_manager_listener, data_wl.display);
+        tizen_input_device_manager_add_listener(d->devicemgr,
+            &_input_device_manager_listener, d);
     } else if (!strcmp(interface, "zwp_input_method_manager_v1")) {
-        data_wl.input_method_mgr = wl_registry_bind(registry, id,
+        d->input_method_mgr = wl_registry_bind(registry, id,
                 &zwp_input_method_manager_v1_interface, version);
     } else if (!strcmp(interface, "zwp_input_method_v1")) {
-        data_wl.input_method = wl_registry_bind(registry, id,
+        d->input_method = wl_registry_bind(registry, id,
                 &zwp_input_method_v1_interface, version);
-        zwp_input_method_v1_add_listener(data_wl.input_method,
+        zwp_input_method_v1_add_listener(d->input_method,
                          &input_method_cb_listener,
-                         NULL);
+                         d);
 
         //TODO: delte this code. this job should be done in cb_activate
-        data_wl.preedit_string = strdup("");
-        data_wl.content_hint = 0;
-        data_wl.content_purpose = 0;
-        free(data_wl.preferred_language);
-        data_wl.preferred_language = NULL;
-        free(data_wl.surrounding_text);
-        data_wl.surrounding_text = NULL;
-        data_wl.serial = 0;
+        d->preedit_string = strdup("");
+        d->content_hint = 0;
+        d->content_purpose = 0;
+        free(d->preferred_language);
+        d->preferred_language = NULL;
+        free(d->surrounding_text);
+        d->surrounding_text = NULL;
+        d->serial = 0;
     }
 }
 
 static void
 registry_handle_global_remove(void * data, struct wl_registry * registry, uint32_t id)
 {
-    if (data_wl.enable_log)
+    struct display *d = (struct display *)data;
+
+    if (d->enable_log)
         printf("registry is removed. id: %d !\n", id);
 }
 
@@ -1213,37 +1235,37 @@ static const struct wl_registry_listener _registry_listener = {
 };
 
 static int
-wayland_init(void)
+wayland_init(struct display *d)
 {
-    memset(&data_wl, 0, sizeof(struct display));
-    data_wl.request_notified = -1;
+    memset(d, 0, sizeof(struct display));
+    d->request_notified = -1;
 
-    data_wl.display = wl_display_connect(NULL);
-    if (!data_wl.display) {
+    d->display = wl_display_connect(NULL);
+    if (!d->display) {
         printf("Failed to connect wayland display\n");
         return 0;
     }
 
-    data_wl.queue = wl_display_create_queue(data_wl.display);
-    if (!data_wl.queue) {
+    d->queue = wl_display_create_queue(d->display);
+    if (!d->queue) {
         printf("Failed to create queue\n");
         return 0;
     }
 
-    data_wl.registry = wl_display_get_registry(data_wl.display);
-    if (!data_wl.registry) {
+    d->registry = wl_display_get_registry(d->display);
+    if (!d->registry) {
         printf("Failed to get registry\n");
         return 0;
     }
 
-    wl_proxy_set_queue((struct wl_proxy*)data_wl.registry, data_wl.queue);
-    wl_registry_add_listener(data_wl.registry, &_registry_listener, NULL);
+    wl_proxy_set_queue((struct wl_proxy*)d->registry, d->queue);
+    wl_registry_add_listener(d->registry, &_registry_listener, d);
 
-    if (wl_display_dispatch_queue(data_wl.display, data_wl.queue) == -1) {
+    if (wl_display_dispatch_queue(d->display, d->queue) == -1) {
         printf("Failed to dispatch display\n");
         return 0;
     }
-    if (wl_display_roundtrip_queue(data_wl.display, data_wl.queue) == -1) {
+    if (wl_display_roundtrip_queue(d->display, d->queue) == -1) {
         printf("Failed to roundtrip display\n");
         return 0;
     }
@@ -1252,91 +1274,91 @@ wayland_init(void)
 }
 
 static void
-wayland_deinit(void)
+wayland_deinit(struct display *d)
 {
-    if (data_wl.enable_log)
+    if (d->enable_log)
         printf("Shutdown wayland system\n");
 
-    if (data_wl.init) deinit_input_generator();
-
-    if (data_wl.queue) wl_event_queue_destroy(data_wl.queue);
-    if (data_wl.devicemgr) tizen_input_device_manager_destroy(data_wl.devicemgr);
-    if (data_wl.display) {
-        if (data_wl.input_method)
-            zwp_input_method_v1_destroy(data_wl.input_method);
-        zwp_input_method_manager_v1_destroy(data_wl.input_method_mgr);
-        wl_registry_destroy(data_wl.registry);
-        wl_display_flush(data_wl.display);
-        wl_display_disconnect(data_wl.display);
+    if (d->init) deinit_input_generator(d);
+
+    if (d->queue) wl_event_queue_destroy(d->queue);
+    if (d->devicemgr) tizen_input_device_manager_destroy(d->devicemgr);
+    if (d->display) {
+        if (d->input_method)
+            zwp_input_method_v1_destroy(d->input_method);
+        zwp_input_method_manager_v1_destroy(d->input_method_mgr);
+        wl_registry_destroy(d->registry);
+        wl_display_flush(d->display);
+        wl_display_disconnect(d->display);
     }
 }
 
 static int
-epoll_init(void)
+epoll_init(struct display *d)
 {
     struct epoll_event ep[2];
 
-    data_wl.fd_epoll = epoll_create(SIZE_EPOLL);
-    if (data_wl.fd_epoll <= 0) {
+    d->fd_epoll = epoll_create(SIZE_EPOLL);
+    if (d->fd_epoll <= 0) {
         printf("Failed to epoll create: %d\n", SIZE_EPOLL);
         return 0;
     }
 
-    data_wl.fd_display = wl_display_get_fd(data_wl.display);
+    d->fd_display = wl_display_get_fd(d->display);
 
     memset(ep, 0, sizeof(struct epoll_event)*2);
 
     ep[0].events = EPOLLIN | EPOLLERR | EPOLLHUP;
-    ep[0].data.fd = data_wl.fd_display;
-    epoll_ctl(data_wl.fd_epoll, EPOLL_CTL_ADD, data_wl.fd_display, &ep[0]);
+    ep[0].data.fd = d->fd_display;
+    epoll_ctl(d->fd_epoll, EPOLL_CTL_ADD, d->fd_display, &ep[0]);
     ep[1].events = EPOLLIN | EPOLLERR | EPOLLHUP;
     ep[1].data.fd = STDIN_FILENO;
-    epoll_ctl(data_wl.fd_epoll, EPOLL_CTL_ADD, 0, &ep[1]);
+    epoll_ctl(d->fd_epoll, EPOLL_CTL_ADD, 0, &ep[1]);
 
     return 1;
 }
 
 static void
-mainloop(void)
+mainloop(struct display *d)
 {
     struct epoll_event ep[SIZE_EPOLL];
     int res, count, i;
 
-    res = epoll_init();
+    res = epoll_init(d);
     if (!res) {
         printf("Failed to init epoll\n");
         return;
     }
 
-    data_wl.run = 1;
-    while (data_wl.run) {
-        res = wl_display_dispatch_queue_pending(data_wl.display, data_wl.queue);
+    d->run = 1;
+    while (d->run) {
+        res = wl_display_dispatch_queue_pending(d->display, d->queue);
         if (res < 0) {
             printf("Failed to dispatch pending. result: %d\n", res);
-            data_wl.run = 0;
+            d->run = 0;
             break;
         }
-        res = wl_display_flush(data_wl.display);
+        res = wl_display_flush(d->display);
         if (res < 0) {
             printf("Failed to flush display. result: %d\n", res);
-            data_wl.run = 0;
+            d->run = 0;
             break;
         }
 
-        count = epoll_wait(data_wl.fd_epoll, ep, SIZE_EPOLL, -1);
+        count = epoll_wait(d->fd_epoll, ep, SIZE_EPOLL, -1);
         for (i = 0; i < count; i++) {
             if (ep[i].events & EPOLLIN) {
-                if (ep[i].data.fd == data_wl.fd_display) {
-                    wl_display_dispatch_queue(data_wl.display, data_wl.queue);
+                if (ep[i].data.fd == d->fd_display) {
+                    wl_display_dispatch_queue(d->display, d->queue);
                 } else {
-                    stdin_read();
+                    stdin_read(d);
                 }
             }
             if (ep[i].events & EPOLLERR) {
-                data_wl.run = 0;
+                d->run = 0;
             }
             if (ep[i].events & EPOLLHUP) {
-                data_wl.run = 0;
+                d->run = 0;
             }
         }
     }
@@ -1346,13 +1368,14 @@ int
 main(int argc, char **argv)
 {
     int res;
+    struct display *d = &data_wl;
 
-    res = wayland_init();
+    res = wayland_init(d);
     if (!res) return 0;
 
-    mainloop();
+    mainloop(d);
 
-    wayland_deinit();
+    wayland_deinit(d);
 
     return 0;
 }