drawlines.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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) 2013-2018 K. Lange
  5. *
  6. * drawlines - Draw random lines into a GUI window
  7. *
  8. * The original compositor demo application, this dates all the
  9. * way back to the original pre-Yutani compositor. Opens a very
  10. * basic window (no decorations) and randomly fills it with
  11. * colorful lines in a separate thread from the listener.
  12. *
  13. * There's no good reason for this to use threads - it should use
  14. * `fswait2` to apply timeouts - but it demonstrates threading
  15. * so we'll leave it that way for now.
  16. */
  17. #include <stdlib.h>
  18. #include <assert.h>
  19. #include <unistd.h>
  20. #include <time.h>
  21. #include <sched.h>
  22. #include <math.h>
  23. #include <sys/fswait.h>
  24. #include <toaru/yutani.h>
  25. #include <toaru/graphics.h>
  26. static int left, top, width, height;
  27. static yutani_t * yctx;
  28. static yutani_window_t * wina;
  29. static gfx_context_t * ctx;
  30. static int should_exit = 0;
  31. static int thick = 0;
  32. static void draw(void) {
  33. if (thick) {
  34. draw_line_aa(ctx, rand() % width, rand() % width, rand() % height, rand() % height, rgb(rand() % 255,rand() % 255,rand() % 255), (float)thick);
  35. } else {
  36. draw_line(ctx, rand() % width, rand() % width, rand() % height, rand() % height, rgb(rand() % 255,rand() % 255,rand() % 255));
  37. }
  38. yutani_flip(yctx, wina);
  39. }
  40. static void show_usage(char * argv[]) {
  41. printf(
  42. "drawlines - graphical demo, draws lines randomly\n"
  43. "\n"
  44. "usage: %s [-t thickness]\n"
  45. "\n"
  46. " -t \033[3mdraw with anti-aliasing and the specified thickness\033[0m\n"
  47. " -? \033[3mshow this help text\033[0m\n"
  48. "\n", argv[0]);
  49. }
  50. int main (int argc, char ** argv) {
  51. left = 100;
  52. top = 100;
  53. width = 500;
  54. height = 500;
  55. srand(time(NULL));
  56. int c;
  57. while ((c = getopt(argc, argv, "t:?")) != -1) {
  58. switch (c) {
  59. case 't':
  60. thick = atoi(optarg);
  61. break;
  62. case '?':
  63. show_usage(argv);
  64. return 0;
  65. }
  66. }
  67. yctx = yutani_init();
  68. if (!yctx) {
  69. fprintf(stderr, "%s: failed to connect to compositor\n", argv[0]);
  70. return 1;
  71. }
  72. wina = yutani_window_create(yctx, width, height);
  73. yutani_window_move(yctx, wina, left, top);
  74. yutani_window_advertise_icon(yctx, wina, "drawlines", "drawlines");
  75. ctx = init_graphics_yutani(wina);
  76. draw_fill(ctx, rgb(0,0,0));
  77. while (!should_exit) {
  78. int fds[1] = {fileno(yctx->sock)};
  79. int index = fswait2(1,fds,20);
  80. if (index == 0) {
  81. yutani_msg_t * m = yutani_poll(yctx);
  82. while (m) {
  83. switch (m->type) {
  84. case YUTANI_MSG_KEY_EVENT:
  85. {
  86. struct yutani_msg_key_event * ke = (void*)m->data;
  87. if (ke->event.action == KEY_ACTION_DOWN && ke->event.keycode == 'q') {
  88. should_exit = 1;
  89. sched_yield();
  90. }
  91. }
  92. break;
  93. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  94. {
  95. struct yutani_msg_window_mouse_event * me = (void*)m->data;
  96. if (me->command == YUTANI_MOUSE_EVENT_DOWN && me->buttons & YUTANI_MOUSE_BUTTON_LEFT) {
  97. yutani_window_drag_start(yctx, wina);
  98. }
  99. }
  100. break;
  101. case YUTANI_MSG_WINDOW_CLOSE:
  102. case YUTANI_MSG_SESSION_END:
  103. should_exit = 1;
  104. break;
  105. default:
  106. break;
  107. }
  108. free(m);
  109. m = yutani_poll_async(yctx);
  110. }
  111. }
  112. draw();
  113. }
  114. yutani_close(yctx, wina);
  115. return 0;
  116. }