drawlines.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <pthread.h>
  21. #include <time.h>
  22. #include <sched.h>
  23. #include <math.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. void * draw_thread(void * garbage) {
  33. (void)garbage;
  34. while (!should_exit) {
  35. if (thick) {
  36. draw_line_aa(ctx, rand() % width, rand() % width, rand() % height, rand() % height, rgb(rand() % 255,rand() % 255,rand() % 255), (float)thick);
  37. } else {
  38. draw_line(ctx, rand() % width, rand() % width, rand() % height, rand() % height, rgb(rand() % 255,rand() % 255,rand() % 255));
  39. }
  40. yutani_flip(yctx, wina);
  41. usleep(16666);
  42. }
  43. pthread_exit(0);
  44. return NULL;
  45. }
  46. static void show_usage(char * argv[]) {
  47. printf(
  48. "drawlines - graphical demo, draws lines randomly\n"
  49. "\n"
  50. "usage: %s [-t thickness]\n"
  51. "\n"
  52. " -t \033[3mdraw with anti-aliasing and the specified thickness\033[0m\n"
  53. " -? \033[3mshow this help text\033[0m\n"
  54. "\n", argv[0]);
  55. }
  56. int main (int argc, char ** argv) {
  57. left = 100;
  58. top = 100;
  59. width = 500;
  60. height = 500;
  61. srand(time(NULL));
  62. int c;
  63. while ((c = getopt(argc, argv, "t:?")) != -1) {
  64. switch (c) {
  65. case 't':
  66. thick = atoi(optarg);
  67. break;
  68. case '?':
  69. show_usage(argv);
  70. return 0;
  71. }
  72. }
  73. yctx = yutani_init();
  74. if (!yctx) {
  75. fprintf(stderr, "%s: failed to connect to compositor\n", argv[0]);
  76. return 1;
  77. }
  78. wina = yutani_window_create(yctx, width, height);
  79. yutani_window_move(yctx, wina, left, top);
  80. yutani_window_advertise_icon(yctx, wina, "drawlines", "drawlines");
  81. ctx = init_graphics_yutani(wina);
  82. draw_fill(ctx, rgb(0,0,0));
  83. pthread_t thread;
  84. pthread_create(&thread, NULL, draw_thread, NULL);
  85. while (!should_exit) {
  86. yutani_msg_t * m = yutani_poll(yctx);
  87. if (m) {
  88. switch (m->type) {
  89. case YUTANI_MSG_KEY_EVENT:
  90. {
  91. struct yutani_msg_key_event * ke = (void*)m->data;
  92. if (ke->event.action == KEY_ACTION_DOWN && ke->event.keycode == 'q') {
  93. should_exit = 1;
  94. sched_yield();
  95. }
  96. }
  97. break;
  98. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  99. {
  100. struct yutani_msg_window_mouse_event * me = (void*)m->data;
  101. if (me->command == YUTANI_MOUSE_EVENT_DOWN && me->buttons & YUTANI_MOUSE_BUTTON_LEFT) {
  102. yutani_window_drag_start(yctx, wina);
  103. }
  104. }
  105. break;
  106. case YUTANI_MSG_WINDOW_CLOSE:
  107. case YUTANI_MSG_SESSION_END:
  108. should_exit = 1;
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. free(m);
  115. }
  116. yutani_close(yctx, wina);
  117. return 0;
  118. }