yutani-test.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* This file is part of ToaruOS and is released under the terms
  2. * of the NCSA / University of Illinois License - see LICENSE.md
  3. * Copyright (C) 2015 Kevin Lange
  4. *
  5. * Yutani Test Tool
  6. *
  7. * Kinda like xev: Pops up a window and displays events in a
  8. * human-readable format.
  9. *
  10. */
  11. #include <stdlib.h>
  12. #include <assert.h>
  13. #include <syscall.h>
  14. #include <unistd.h>
  15. #include <toaru/yutani.h>
  16. #include <toaru/graphics.h>
  17. #include <toaru/pthread.h>
  18. static int left, top, width, height;
  19. static yutani_t * yctx;
  20. static yutani_window_t * wina;
  21. static gfx_context_t * ctx;
  22. static int should_exit = 0;
  23. const char * action_name(unsigned int action) {
  24. switch (action) {
  25. case KEY_ACTION_UP:
  26. return "up";
  27. case KEY_ACTION_DOWN:
  28. return "down";
  29. default:
  30. return "?";
  31. }
  32. }
  33. char * modifiers(unsigned int m) {
  34. static char out[] = "........";
  35. if (m & KEY_MOD_LEFT_CTRL) out[0] = 'c'; else out[0] = '.';
  36. if (m & KEY_MOD_LEFT_SHIFT) out[1] = 's'; else out[1] = '.';
  37. if (m & KEY_MOD_LEFT_ALT) out[2] = 'a'; else out[2] = '.';
  38. if (m & KEY_MOD_LEFT_SUPER) out[3] = 'x'; else out[3] = '.';
  39. if (m & KEY_MOD_RIGHT_CTRL) out[4] = 'c'; else out[4] = '.';
  40. if (m & KEY_MOD_RIGHT_SHIFT) out[5] = 's'; else out[5] = '.';
  41. if (m & KEY_MOD_RIGHT_ALT) out[6] = 'a'; else out[6] = '.';
  42. if (m & KEY_MOD_RIGHT_SUPER) out[7] = 'x'; else out[7] = '.';
  43. return out;
  44. }
  45. char * mouse_buttons(unsigned char button) {
  46. static char out[] = "....";
  47. if (button & YUTANI_MOUSE_BUTTON_LEFT) out[0] = 'l'; else out[0] = '.';
  48. if (button & YUTANI_MOUSE_BUTTON_MIDDLE) out[1] = 'm'; else out[1] = '.';
  49. if (button & YUTANI_MOUSE_BUTTON_RIGHT) out[2] = 'r'; else out[2] = '.';
  50. if (button & YUTANI_MOUSE_SCROLL_UP) out[3] = 'u'; else \
  51. if (button & YUTANI_MOUSE_SCROLL_DOWN) out[3] = 'd'; else out[3] = '.';
  52. return out;
  53. }
  54. const char * mouse_command(unsigned char type) {
  55. switch (type) {
  56. case (YUTANI_MOUSE_EVENT_CLICK):
  57. return "click";
  58. case (YUTANI_MOUSE_EVENT_DRAG ):
  59. return "drag";
  60. case (YUTANI_MOUSE_EVENT_RAISE):
  61. return "raise";
  62. case (YUTANI_MOUSE_EVENT_DOWN ):
  63. return "down";
  64. case (YUTANI_MOUSE_EVENT_MOVE ):
  65. return "move";
  66. case (YUTANI_MOUSE_EVENT_LEAVE):
  67. return "leave";
  68. case (YUTANI_MOUSE_EVENT_ENTER):
  69. return "enter";
  70. default:
  71. return "unknown";
  72. }
  73. }
  74. void redraw(void) {
  75. draw_fill(ctx, rgb(0,0,0));
  76. int w = width - 1, h = height - 1;
  77. draw_line(ctx, 0, w, 0, 0, rgb(255,255,255));
  78. draw_line(ctx, 0, w, h, h, rgb(255,255,255));
  79. draw_line(ctx, 0, 0, 0, h, rgb(255,255,255));
  80. draw_line(ctx, w, w, 0, h, rgb(255,255,255));
  81. }
  82. int main (int argc, char ** argv) {
  83. int show_cursor = 1;
  84. left = 100;
  85. top = 100;
  86. width = 500;
  87. height = 500;
  88. yctx = yutani_init();
  89. wina = yutani_window_create(yctx, width, height);
  90. yutani_window_move(yctx, wina, left, top);
  91. ctx = init_graphics_yutani(wina);
  92. redraw();
  93. while (!should_exit) {
  94. yutani_msg_t * m = yutani_poll(yctx);
  95. if (m) {
  96. switch (m->type) {
  97. case YUTANI_MSG_KEY_EVENT:
  98. {
  99. struct yutani_msg_key_event * ke = (void*)m->data;
  100. fprintf(stderr, "Key Press (wid=%d) %s\n"
  101. "\tevent.action = %d\n"
  102. "\tevent.keycode = %d\n"
  103. "\tevent.modifiers = %s\n"
  104. "\tevent.key = %d (%c)\n",
  105. ke->wid,
  106. action_name(ke->event.action),
  107. ke->event.action,
  108. ke->event.keycode,
  109. modifiers(ke->event.modifiers),
  110. ke->event.key, ke->event.key);
  111. if (ke->event.key == 'm' && ke->event.action == KEY_ACTION_DOWN) {
  112. show_cursor = !show_cursor;
  113. yutani_window_show_mouse(yctx, wina, show_cursor);
  114. }
  115. }
  116. break;
  117. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  118. {
  119. struct yutani_msg_window_mouse_event * me = (void*)m->data;
  120. fprintf(stderr, "Mouse Event (wid=%d) %s\n"
  121. "\tnew = %d, %d\n"
  122. "\told = %d, %d\n"
  123. "\tbuttons = %s\n"
  124. "\tcommand = %d\n",
  125. me->wid,
  126. mouse_command(me->command),
  127. me->new_x, me->new_y,
  128. me->old_x, me->old_y,
  129. mouse_buttons(me->buttons),
  130. me->command);
  131. }
  132. break;
  133. case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
  134. {
  135. struct yutani_msg_window_focus_change * fc = (void*)m->data;
  136. fprintf(stderr, "Focus Change (wid=%d) %s\n", fc->wid, fc->focused ? "on" : "off");
  137. }
  138. break;
  139. case YUTANI_MSG_WINDOW_MOVE:
  140. {
  141. struct yutani_msg_window_move * wm = (void*)m->data;
  142. fprintf(stderr, "Window Moved (wid=%d) %d, %d\n", wm->wid, wm->x, wm->y);
  143. }
  144. break;
  145. case YUTANI_MSG_RESIZE_OFFER:
  146. {
  147. struct yutani_msg_window_resize * wr = (void*)m->data;
  148. fprintf(stderr, "Resize Offer (wid=%d) %d x %d\n"
  149. "\tbufid = %d\n",
  150. wr->wid,
  151. wr->width, wr->height,
  152. wr->bufid);
  153. }
  154. break;
  155. case YUTANI_MSG_SESSION_END:
  156. should_exit = 1;
  157. break;
  158. default:
  159. break;
  160. }
  161. }
  162. free(m);
  163. }
  164. yutani_close(yctx, wina);
  165. return 0;
  166. }