+++ /dev/null
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/epoll.h>
-
-#include <wayland-client.h>
-#include <tizen-extension-client-protocol.h>
-
-#define MAX_STR 1024
-#define SIZE_EPOLL 16
-
-enum enum_key_type
-{
- KEY_UP = 0,
- KEY_DOWN,
- KEY_ALL
-};
-
-enum enum_touch_type
-{
- TOUCH_BEGIN = 0,
- TOUCH_UPDATE,
- TOUCH_END,
- TOUCH_ALL
-};
-
-struct display
-{
- struct wl_display *display;
- struct wl_registry *registry;
- struct wl_compositor *compositor;
-
- struct tizen_input_device_manager *devicemgr;
- enum tizen_input_device_manager_clas clas;
- struct wl_event_queue *queue;
-
- int run;
- int fd_epoll;
- int fd_display;
-
- int request_notified;
- int init;
-
- int enable_log;
-};
-
-struct display data_wl;
-
-static void
-usage(void)
-{
- printf(" Supported commands: init (Initialize input generator)\n");
- printf(" : deinit (Deinitialize input generator)\n");
- printf(" : key (Generate key events)\n");
- printf(" : touch (Generate touch events)\n");
- printf(" : help (Print this help text)\n");
- printf(" : q/quit (Quit program)\n");
- printf(" : log (Print detailed logs)\n");
- printf("init {device type}\n");
- printf(" : device type:\n");
- printf(" - default: all\n");
- printf(" - key/keyboard: keyboard\n");
- printf(" - touch: touch screen\n");
- printf(" - all: all of devices\n");
- printf(" : ex> init keyboard / init\n");
- printf("\n");
- printf("deinit\n");
- printf(" : ex> deinit\n");
- printf("\n");
- printf("key [keyname] {pressed}\n");
- printf(" : pressed:\n");
- printf(" - default: down&up pair\n");
- printf(" - key down: 1\n");
- printf(" - key up: 0\n");
- printf(" : ex> key XF86Back 1\n");
- printf("\n");
- printf("touch {index} {type} {x} {y}\n");
- printf(" : index:\n");
- printf(" - default: first finger(0)\n");
- printf(" - first finger is 0\n");
- printf(" : type:\n");
- printf(" - default: generate sample touch events\n");
- printf(" - touch begin: 1\n");
- printf(" - touch update: 2\n");
- printf(" - touch end: 3\n");
- printf(" : x/y:\n");
- printf(" - default: 0\n");
- printf(" : ex> touch / touch 0 1 100 100\n");
- printf("\n");
-}
-
-static void
-init_input_generator(enum tizen_input_device_manager_clas clas)
-{
- if (data_wl.init) {
- printf("Already init input generator\n");
- return;
- }
-
- tizen_input_device_manager_init_generator(data_wl.devicemgr, clas);
-
- while (data_wl.request_notified == -1)
- wl_display_dispatch_queue(data_wl.display, data_wl.queue);
-
- if (data_wl.request_notified == TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE) {
- data_wl.init = 1;
- printf("Success to init input generator\n");
- } else {
- printf("Failed to init input generator: %d\n", data_wl.request_notified);
- }
-
- data_wl.clas = clas;
- data_wl.request_notified = -1;
-}
-
-static void
-deinit_input_generator(void)
-{
- if (!data_wl.init) {
- printf("input generator is not initialized\n");
- return;
- }
-
- tizen_input_device_manager_deinit_generator(data_wl.devicemgr, data_wl.clas);
-
- while (data_wl.request_notified == -1)
- wl_display_dispatch_queue(data_wl.display, data_wl.queue);
-
- if (data_wl.request_notified == TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE) {
- data_wl.init = 0;
- printf("Success to deinit input generator\n");
- } else {
- printf("Failed to deinit input generator: %d\n", data_wl.request_notified);
- }
-
- data_wl.request_notified = -1;
-}
-
-static void
-input_generator_key(char *name, int type)
-{
- tizen_input_device_manager_generate_key(data_wl.devicemgr, name, !!type);
-
- while (data_wl.request_notified == -1)
- wl_display_dispatch_queue(data_wl.display, data_wl.queue);
-
- if (data_wl.request_notified == TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE) {
- if (data_wl.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);
- }
-
- data_wl.request_notified = -1;
-}
-
-static void
-key_generate(char *name, int type)
-{
- printf("name: %s, type: %d\n", name, type);
-
- if (!data_wl.init) {
- printf("Input genrator is not initialized\n");
- return;
- }
-
- if (!name) {
- printf("Type which key is generated\n");
- return;
- }
-
- if (type == KEY_ALL) {
- input_generator_key(name, 1);
- input_generator_key(name, 0);
- } else {
- input_generator_key(name, !!type);
- }
-}
-
-static char *
-touch_type_string_get(int type)
-{
- switch (type) {
- case TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_BEGIN:
- return "begin";
- case TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE:
- return "update";
- case TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_END:
- return "end";
- default:
- return "Unknown";
- }
-}
-
-static void
-input_generator_touch(int idx, int type, int x, int y)
-{
- tizen_input_device_manager_generate_touch(data_wl.devicemgr, type, x, y, idx);
-
- while (data_wl.request_notified == -1)
- wl_display_dispatch_queue(data_wl.display, data_wl.queue);
-
- if (data_wl.request_notified == TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE) {
- if (data_wl.enable_log) {
- printf("Success to generate touch: %d finger %s on (%d, %d)\n", idx, touch_type_string_get(type), x, y);
- }
- } else {
- printf("Failed to generate touch(%d finger %s on (%d, %d)): %d\n", idx, touch_type_string_get(type), x, y, data_wl.request_notified);
- }
-
- data_wl.request_notified = -1;
-}
-
-static void
-touch_generate(int idx, int type, int x, int y)
-{
- if (!data_wl.init) {
- printf("Input genrator is not initialized\n");
- return;
- }
-
- if (type == TOUCH_ALL) {
- input_generator_touch(0, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_BEGIN, 100, 100);
- input_generator_touch(1, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_BEGIN, 200, 200);
- input_generator_touch(2, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_BEGIN, 300, 300);
-
- input_generator_touch(0, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 110, 110);
- input_generator_touch(1, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 210, 210);
- input_generator_touch(2, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 310, 310);
-
- input_generator_touch(0, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 120, 120);
- input_generator_touch(1, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 220, 220);
- input_generator_touch(2, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE, 320, 320);
-
- input_generator_touch(0, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_END, 120, 120);
- input_generator_touch(1, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_END, 220, 220);
- input_generator_touch(2, TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_END, 320, 320);
- } else {
- input_generator_touch(idx, type, x, y);
- }
-}
-
-static void
-stdin_read(void)
-{
- int c;
- char buf[MAX_STR] = {0, }, *tmp, *buf_ptr, key_name[MAX_STR] = {0, };
- int count = 0;
- int key_type = KEY_ALL, touch_idx = 0, touch_type = TOUCH_ALL, touch_x = 0, touch_y = 0;
-
- while ((c = getchar()) != EOF) {
- if (c == '\n') break;
- if (count >= MAX_STR) break;
-
- buf[count] = c;
- count++;
- }
-
- count = 0;
- tmp = strtok_r(buf, " ", &buf_ptr);
- if (!tmp) return;
-
- if (!strncmp(tmp, "init", sizeof("init"))) {
- while (tmp) {
- tmp = strtok_r(NULL, " ", &buf_ptr);
- if (tmp) {
- switch (count) {
- case 0:
- if (!strncmp("keyboard", tmp, MAX_STR-1))
- init_input_generator(TIZEN_INPUT_DEVICE_MANAGER_CLAS_KEYBOARD);
- else if (!strncmp("touch", tmp, MAX_STR-1))
- init_input_generator(TIZEN_INPUT_DEVICE_MANAGER_CLAS_TOUCHSCREEN);
- break;
- default:
- break;
- }
- }
- count++;
- }
- } else if (!strncmp(tmp, "deinit", sizeof("deinit"))) {
- deinit_input_generator();
- } else if (!strncmp(tmp, "key", sizeof("key"))) {
- while (tmp) {
- tmp = strtok_r(NULL, " ", &buf_ptr);
- if (tmp) {
- switch (count) {
- case 0:
- strncpy(key_name, tmp, MAX_STR-1);
- break;
- case 1:
- key_type = atoi(tmp);
- break;
- default:
- break;
- }
- }
- count++;
- }
- key_generate(key_name, key_type);
- } else if (!strncmp(tmp, "touch", sizeof("touch"))) {
- while (tmp) {
- tmp = strtok_r(NULL, " ", &buf_ptr);
- if (tmp) {
- switch (count) {
- case 0:
- touch_idx = atoi(tmp);
- break;
- case 1:
- touch_type = atoi(tmp);
- break;
- case 2:
- touch_x = atoi(tmp);
- break;
- case 3:
- touch_y = atoi(tmp);
- break;
- default:
- break;
- }
- }
- count++;
- }
- touch_generate(touch_idx, touch_type, touch_x, touch_y);
- } else if (!strncmp(buf, "q", MAX_STR) || !strncmp(buf, "quit", MAX_STR)) {
- data_wl.run = 0;
- } else if (!strncmp(buf, "help", MAX_STR)) {
- usage();
- } else if (!strncmp(buf, "log", MAX_STR)) {
- if (data_wl.enable_log)
- printf("Disable detailed logs\n");
- else
- printf("Enable detailed logs\n");
-
- data_wl.enable_log = !data_wl.enable_log;
- } else {
- printf("Invalid arguments\n");
- usage();
- }
-}
-
-static void
-input_device_manager_handle_error(void *data,
- struct tizen_input_device_manager *tizen_input_device_manager,
- uint32_t errorcode)
-{
- if (data_wl.enable_log)
- printf("errorcode: %d\n", errorcode);
- data_wl.request_notified = errorcode;
-}
-
-static const struct tizen_input_device_manager_listener _input_device_manager_listener =
-{
- .device_add = NULL,
- .device_remove = NULL,
- .error = input_device_manager_handle_error,
- .block_expired = NULL,
-};
-
-static void
-registry_handle_global(void * data, struct wl_registry * registry, uint32_t id,
- const char * interface, uint32_t version)
-{
- if (strcmp(interface, "wl_compositor") == 0) {
- data_wl.compositor = wl_registry_bind(registry, id,
- &wl_compositor_interface, version);
- if (!data_wl.compositor) {
- printf("Failed to bind compositor.");
- return;
- }
- if (data_wl.enable_log)
- printf("Success to bind compositor.");
- } else if (strcmp(interface, "tizen_input_device_manager") == 0) {
- data_wl.devicemgr = wl_registry_bind(registry, id,
- &tizen_input_device_manager_interface, version);
- if (!data_wl.devicemgr) {
- printf("Failed to bind input device manager");
- return;
- }
- if (data_wl.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);
- }
-}
-
-static void
-registry_handle_global_remove(void * data, struct wl_registry * registry, uint32_t id)
-{
- if (data_wl.enable_log)
- printf("registry is removed. id: %d !\n", id);
-}
-
-static const struct wl_registry_listener _registry_listener = {
- registry_handle_global,
- registry_handle_global_remove
-};
-
-static int
-wayland_init(void)
-{
- memset(&data_wl, 0, sizeof(struct display));
- data_wl.request_notified = -1;
-
- data_wl.display = wl_display_connect(NULL);
- if (!data_wl.display) {
- printf("Failed to connect wayland display\n");
- return 0;
- }
-
- data_wl.queue = wl_display_create_queue(data_wl.display);
- if (!data_wl.queue) {
- printf("Failed to create queue\n");
- return 0;
- }
-
- data_wl.registry = wl_display_get_registry(data_wl.display);
- if (!data_wl.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);
-
- if (wl_display_dispatch_queue(data_wl.display, data_wl.queue) == -1) {
- printf("Failed to dispatch display\n");
- return 0;
- }
- if (wl_display_roundtrip_queue(data_wl.display, data_wl.queue) == -1) {
- printf("Failed to roundtrip display\n");
- return 0;
- }
-
- return 1;
-}
-
-static void
-wayland_deinit(void)
-{
- if (data_wl.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) {
- wl_registry_destroy(data_wl.registry);
- wl_display_flush(data_wl.display);
- wl_display_disconnect(data_wl.display);
- }
-}
-
-static int
-epoll_init(void)
-{
- struct epoll_event ep[2];
-
- data_wl.fd_epoll = epoll_create(SIZE_EPOLL);
- if (data_wl.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);
-
- 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[1].events = EPOLLIN | EPOLLERR | EPOLLHUP;
- ep[1].data.fd = STDIN_FILENO;
- epoll_ctl(data_wl.fd_epoll, EPOLL_CTL_ADD, 0, &ep[1]);
-
- return 1;
-}
-
-static void
-mainloop(void)
-{
- struct epoll_event ep[SIZE_EPOLL];
- int res, count, i;
-
- res = epoll_init();
- 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);
- if (res < 0) {
- printf("Failed to dispatch pending. result: %d\n", res);
- data_wl.run = 0;
- break;
- }
- res = wl_display_flush(data_wl.display);
- if (res < 0) {
- printf("Failed to flush display. result: %d\n", res);
- data_wl.run = 0;
- break;
- }
-
- count = epoll_wait(data_wl.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);
- } else {
- stdin_read();
- }
- }
- if (ep[i].events & EPOLLERR) {
- data_wl.run = 0;
- }
- if (ep[i].events & EPOLLHUP) {
- data_wl.run = 0;
- }
- }
- }
-}
-
-int
-main(int argc, char **argv)
-{
- int res;
-
- res = wayland_init();
- if (!res) return 0;
-
- mainloop();
-
- wayland_deinit();
-
- return 0;
-}