plasma.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. * plasma - Draw animated plasma in a window
  7. *
  8. */
  9. #include <stdlib.h>
  10. #include <assert.h>
  11. #include <math.h>
  12. #include <wait.h>
  13. #include <pthread.h>
  14. #include <sched.h>
  15. #include <toaru/yutani.h>
  16. #include <toaru/graphics.h>
  17. #include <toaru/decorations.h>
  18. #include <toaru/spinlock.h>
  19. #include <toaru/menu.h>
  20. #define dist(a,b,c,d) sqrt((double)(((a) - (c)) * ((a) - (c)) + ((b) - (d)) * ((b) - (d))))
  21. static yutani_t * yctx;
  22. static yutani_window_t * wina;
  23. static int should_exit = 0;
  24. uint16_t win_width;
  25. uint16_t win_height;
  26. uint16_t off_x;
  27. uint16_t off_y;
  28. static int volatile draw_lock = 0;
  29. gfx_context_t * ctx;
  30. void redraw_borders() {
  31. render_decorations(wina, ctx, "Plasma");
  32. }
  33. uint32_t hsv_to_rgb(int h, float s, float v) {
  34. float c = v * s;
  35. float hp = (float)h / 42.6666666f;
  36. float x = c * (1.0 - fabs(fmod(hp, 2) - 1.0));
  37. float m = v - c;
  38. float rp, gp, bp;
  39. if (hp < 1.0) { rp = c; gp = x; bp = 0; }
  40. else if (hp < 2.0) { rp = x; gp = c; bp = 0; }
  41. else if (hp < 3.0) { rp = 0; gp = c; bp = x; }
  42. else if (hp < 4.0) { rp = 0; gp = x; bp = c; }
  43. else if (hp < 5.0) { rp = x; gp = 0; bp = c; }
  44. else if (hp < 6.0) { rp = c; gp = 0; bp = x; }
  45. else { rp = 0; gp = 0; bp = 0; }
  46. return rgb((rp + m) * 255, (gp + m) * 255, (bp + m) * 255);
  47. }
  48. void * draw_thread(void * garbage) {
  49. (void)garbage;
  50. double time = 0;
  51. /* Generate a palette */
  52. uint32_t palette[256];
  53. for (int x = 0; x < 256; ++x) {
  54. palette[x] = hsv_to_rgb(x,1.0,1.0);
  55. }
  56. while (!should_exit) {
  57. time += 1.0;
  58. spin_lock(&draw_lock);
  59. for (int x = 0; x < win_width; ++x) {
  60. for (int y = 0; y < win_height; ++y) {
  61. double value = sin(dist(x + time, y, 128.0, 128.0) / 8.0)
  62. + sin(dist(x, y, 64.0, 64.0) / 8.0)
  63. + sin(dist(x, y + time / 7, 192.0, 64) / 7.0)
  64. + sin(dist(x, y, 192.0, 100.0) / 8.0);
  65. GFX(ctx, x + off_x, y + off_y) = palette[(int)((value + 4) * 32)];
  66. }
  67. }
  68. redraw_borders();
  69. flip(ctx);
  70. yutani_flip(yctx, wina);
  71. spin_unlock(&draw_lock);
  72. sched_yield();
  73. }
  74. return NULL;
  75. }
  76. void resize_finish(int w, int h) {
  77. yutani_window_resize_accept(yctx, wina, w, h);
  78. reinit_graphics_yutani(ctx, wina);
  79. struct decor_bounds bounds;
  80. decor_get_bounds(wina, &bounds);
  81. win_width = w - bounds.width;
  82. win_height = h - bounds.height;
  83. off_x = bounds.left_width;
  84. off_y = bounds.top_height;
  85. yutani_window_resize_done(yctx, wina);
  86. }
  87. int main (int argc, char ** argv) {
  88. yctx = yutani_init();
  89. if (!yctx) {
  90. fprintf(stderr, "%s: failed to connect to compositor\n", argv[0]);
  91. return 1;
  92. }
  93. win_width = 300;
  94. win_height = 300;
  95. init_decorations();
  96. struct decor_bounds bounds;
  97. decor_get_bounds(NULL, &bounds);
  98. /* Do something with a window */
  99. wina = yutani_window_create(yctx, win_width + bounds.width, win_height + bounds.height);
  100. yutani_window_move(yctx, wina, 300, 300);
  101. decor_get_bounds(wina, &bounds);
  102. off_x = bounds.left_width;
  103. off_y = bounds.top_height;
  104. win_width = wina->width - bounds.width;
  105. win_height = wina->height - bounds.height;
  106. ctx = init_graphics_yutani_double_buffer(wina);
  107. draw_fill(ctx, rgb(0,0,0));
  108. redraw_borders();
  109. flip(ctx);
  110. yutani_flip(yctx, wina);
  111. yutani_window_advertise_icon(yctx, wina, "Plasma", "plasma");
  112. pthread_t thread;
  113. pthread_create(&thread, NULL, draw_thread, NULL);
  114. while (!should_exit) {
  115. yutani_msg_t * m = yutani_poll(yctx);
  116. while (m) {
  117. menu_process_event(yctx, m);
  118. switch (m->type) {
  119. case YUTANI_MSG_KEY_EVENT:
  120. {
  121. struct yutani_msg_key_event * ke = (void*)m->data;
  122. if (ke->event.action == KEY_ACTION_DOWN && ke->event.keycode == 'q') {
  123. should_exit = 1;
  124. }
  125. }
  126. break;
  127. case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
  128. {
  129. struct yutani_msg_window_focus_change * wf = (void*)m->data;
  130. yutani_window_t * win = hashmap_get(yctx->windows, (void*)wf->wid);
  131. if (win && win == wina) {
  132. win->focused = wf->focused;
  133. }
  134. }
  135. break;
  136. case YUTANI_MSG_WINDOW_CLOSE:
  137. case YUTANI_MSG_SESSION_END:
  138. should_exit = 1;
  139. break;
  140. case YUTANI_MSG_RESIZE_OFFER:
  141. {
  142. struct yutani_msg_window_resize * wr = (void*)m->data;
  143. if (wr->wid == wina->wid) {
  144. spin_lock(&draw_lock);
  145. resize_finish(wr->width, wr->height);
  146. spin_unlock(&draw_lock);
  147. }
  148. }
  149. break;
  150. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  151. {
  152. struct yutani_msg_window_mouse_event * me = (void*)m->data;
  153. switch (decor_handle_event(yctx, m)) {
  154. case DECOR_CLOSE:
  155. should_exit = 1;
  156. break;
  157. case DECOR_RIGHT:
  158. decor_show_default_menu(wina, wina->x + me->new_x, wina->y + me->new_y);
  159. break;
  160. }
  161. }
  162. break;
  163. default:
  164. break;
  165. }
  166. free(m);
  167. m = yutani_poll_async(yctx);
  168. }
  169. }
  170. wait(NULL);
  171. yutani_close(yctx, wina);
  172. return 0;
  173. }