yutani-test.c 4.9 KB

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