drawlines.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 <toaru/yutani.h>
  24. #include <toaru/graphics.h>
  25. static int left, top, width, height;
  26. static yutani_t * yctx;
  27. static yutani_window_t * wina;
  28. static gfx_context_t * ctx;
  29. static int should_exit = 0;
  30. void * draw_thread(void * garbage) {
  31. (void)garbage;
  32. while (!should_exit) {
  33. draw_line(ctx, rand() % width, rand() % width, rand() % height, rand() % height, rgb(rand() % 255,rand() % 255,rand() % 255));
  34. yutani_flip(yctx, wina);
  35. usleep(16666);
  36. }
  37. pthread_exit(0);
  38. return NULL;
  39. }
  40. int main (int argc, char ** argv) {
  41. left = 100;
  42. top = 100;
  43. width = 500;
  44. height = 500;
  45. srand(time(NULL));
  46. yctx = yutani_init();
  47. if (!yctx) {
  48. fprintf(stderr, "%s: failed to connect to compositor\n", argv[0]);
  49. return 1;
  50. }
  51. wina = yutani_window_create(yctx, width, height);
  52. yutani_window_move(yctx, wina, left, top);
  53. yutani_window_advertise_icon(yctx, wina, "drawlines", "drawlines");
  54. ctx = init_graphics_yutani(wina);
  55. draw_fill(ctx, rgb(0,0,0));
  56. pthread_t thread;
  57. pthread_create(&thread, NULL, draw_thread, NULL);
  58. while (!should_exit) {
  59. yutani_msg_t * m = yutani_poll(yctx);
  60. if (m) {
  61. switch (m->type) {
  62. case YUTANI_MSG_KEY_EVENT:
  63. {
  64. struct yutani_msg_key_event * ke = (void*)m->data;
  65. if (ke->event.action == KEY_ACTION_DOWN && ke->event.keycode == 'q') {
  66. should_exit = 1;
  67. sched_yield();
  68. }
  69. }
  70. break;
  71. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  72. {
  73. struct yutani_msg_window_mouse_event * me = (void*)m->data;
  74. if (me->command == YUTANI_MOUSE_EVENT_DOWN && me->buttons & YUTANI_MOUSE_BUTTON_LEFT) {
  75. yutani_window_drag_start(yctx, wina);
  76. }
  77. }
  78. break;
  79. case YUTANI_MSG_WINDOW_CLOSE:
  80. case YUTANI_MSG_SESSION_END:
  81. should_exit = 1;
  82. break;
  83. default:
  84. break;
  85. }
  86. }
  87. free(m);
  88. }
  89. yutani_close(yctx, wina);
  90. return 0;
  91. }