2021-11-26 06:41:56 +03:00
|
|
|
/**
|
|
|
|
* @brief Threaded graphical demo that draws animated plasma.
|
|
|
|
*
|
|
|
|
* Good for burning CPU.
|
|
|
|
*
|
|
|
|
* @copyright
|
2018-08-01 03:21:13 +03:00
|
|
|
* This file is part of ToaruOS and is released under the terms
|
2018-04-17 10:48:47 +03:00
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
2018-05-01 11:12:56 +03:00
|
|
|
* Copyright (C) 2013-2018 K. Lange
|
2018-04-17 10:48:47 +03:00
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <math.h>
|
2018-04-18 08:53:04 +03:00
|
|
|
#include <wait.h>
|
2018-05-09 15:26:45 +03:00
|
|
|
#include <pthread.h>
|
2018-08-02 07:13:27 +03:00
|
|
|
#include <sched.h>
|
2019-07-05 10:46:58 +03:00
|
|
|
#include <signal.h>
|
2018-04-17 10:48:47 +03:00
|
|
|
|
|
|
|
#include <toaru/yutani.h>
|
|
|
|
#include <toaru/graphics.h>
|
|
|
|
#include <toaru/decorations.h>
|
|
|
|
#include <toaru/spinlock.h>
|
2018-04-29 17:50:23 +03:00
|
|
|
#include <toaru/menu.h>
|
2018-04-17 10:48:47 +03:00
|
|
|
|
|
|
|
#define dist(a,b,c,d) sqrt((double)(((a) - (c)) * ((a) - (c)) + ((b) - (d)) * ((b) - (d))))
|
|
|
|
|
|
|
|
static yutani_t * yctx;
|
|
|
|
static yutani_window_t * wina;
|
|
|
|
static int should_exit = 0;
|
|
|
|
|
|
|
|
uint16_t win_width;
|
|
|
|
uint16_t win_height;
|
|
|
|
|
|
|
|
uint16_t off_x;
|
|
|
|
uint16_t off_y;
|
|
|
|
|
|
|
|
static int volatile draw_lock = 0;
|
|
|
|
|
|
|
|
gfx_context_t * ctx;
|
|
|
|
|
2019-07-05 10:46:58 +03:00
|
|
|
void sigint_handler() {
|
|
|
|
should_exit = 1;
|
|
|
|
}
|
|
|
|
|
2018-04-17 10:48:47 +03:00
|
|
|
void redraw_borders() {
|
|
|
|
render_decorations(wina, ctx, "Plasma");
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t hsv_to_rgb(int h, float s, float v) {
|
|
|
|
float c = v * s;
|
|
|
|
float hp = (float)h / 42.6666666f;
|
|
|
|
float x = c * (1.0 - fabs(fmod(hp, 2) - 1.0));
|
|
|
|
float m = v - c;
|
|
|
|
float rp, gp, bp;
|
|
|
|
if (hp < 1.0) { rp = c; gp = x; bp = 0; }
|
|
|
|
else if (hp < 2.0) { rp = x; gp = c; bp = 0; }
|
|
|
|
else if (hp < 3.0) { rp = 0; gp = c; bp = x; }
|
|
|
|
else if (hp < 4.0) { rp = 0; gp = x; bp = c; }
|
|
|
|
else if (hp < 5.0) { rp = x; gp = 0; bp = c; }
|
|
|
|
else if (hp < 6.0) { rp = c; gp = 0; bp = x; }
|
|
|
|
else { rp = 0; gp = 0; bp = 0; }
|
|
|
|
return rgb((rp + m) * 255, (gp + m) * 255, (bp + m) * 255);
|
|
|
|
}
|
|
|
|
|
|
|
|
void * draw_thread(void * garbage) {
|
|
|
|
(void)garbage;
|
|
|
|
|
|
|
|
double time = 0;
|
|
|
|
|
|
|
|
/* Generate a palette */
|
|
|
|
uint32_t palette[256];
|
|
|
|
for (int x = 0; x < 256; ++x) {
|
|
|
|
palette[x] = hsv_to_rgb(x,1.0,1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!should_exit) {
|
|
|
|
|
|
|
|
time += 1.0;
|
|
|
|
|
|
|
|
spin_lock(&draw_lock);
|
|
|
|
for (int x = 0; x < win_width; ++x) {
|
|
|
|
for (int y = 0; y < win_height; ++y) {
|
|
|
|
double value = sin(dist(x + time, y, 128.0, 128.0) / 8.0)
|
|
|
|
+ sin(dist(x, y, 64.0, 64.0) / 8.0)
|
|
|
|
+ sin(dist(x, y + time / 7, 192.0, 64) / 7.0)
|
|
|
|
+ sin(dist(x, y, 192.0, 100.0) / 8.0);
|
2021-06-02 12:34:18 +03:00
|
|
|
GFX(ctx, x + off_x, y + off_y) = palette[(unsigned int)((value + 4) * 32) & 0xFF];
|
2018-04-17 10:48:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
redraw_borders();
|
|
|
|
flip(ctx);
|
|
|
|
yutani_flip(yctx, wina);
|
|
|
|
spin_unlock(&draw_lock);
|
2018-08-02 07:13:27 +03:00
|
|
|
sched_yield();
|
2018-04-17 10:48:47 +03:00
|
|
|
}
|
2018-04-25 08:03:29 +03:00
|
|
|
return NULL;
|
2018-04-17 10:48:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void resize_finish(int w, int h) {
|
|
|
|
yutani_window_resize_accept(yctx, wina, w, h);
|
|
|
|
reinit_graphics_yutani(ctx, wina);
|
|
|
|
|
2018-09-12 06:53:08 +03:00
|
|
|
struct decor_bounds bounds;
|
|
|
|
decor_get_bounds(wina, &bounds);
|
|
|
|
|
|
|
|
win_width = w - bounds.width;
|
|
|
|
win_height = h - bounds.height;
|
|
|
|
off_x = bounds.left_width;
|
|
|
|
off_y = bounds.top_height;
|
2018-04-17 10:48:47 +03:00
|
|
|
|
|
|
|
yutani_window_resize_done(yctx, wina);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc, char ** argv) {
|
|
|
|
yctx = yutani_init();
|
2018-08-01 03:21:13 +03:00
|
|
|
if (!yctx) {
|
|
|
|
fprintf(stderr, "%s: failed to connect to compositor\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
2018-04-17 10:48:47 +03:00
|
|
|
|
2018-04-17 14:49:49 +03:00
|
|
|
win_width = 300;
|
|
|
|
win_height = 300;
|
2018-04-17 10:48:47 +03:00
|
|
|
|
|
|
|
init_decorations();
|
|
|
|
|
2018-09-12 06:53:08 +03:00
|
|
|
struct decor_bounds bounds;
|
|
|
|
decor_get_bounds(NULL, &bounds);
|
2018-04-17 10:48:47 +03:00
|
|
|
|
|
|
|
/* Do something with a window */
|
2018-09-12 06:53:08 +03:00
|
|
|
wina = yutani_window_create(yctx, win_width + bounds.width, win_height + bounds.height);
|
2018-04-17 10:48:47 +03:00
|
|
|
yutani_window_move(yctx, wina, 300, 300);
|
|
|
|
|
2018-09-12 06:53:08 +03:00
|
|
|
decor_get_bounds(wina, &bounds);
|
|
|
|
off_x = bounds.left_width;
|
|
|
|
off_y = bounds.top_height;
|
|
|
|
win_width = wina->width - bounds.width;
|
|
|
|
win_height = wina->height - bounds.height;
|
|
|
|
|
2018-04-17 10:48:47 +03:00
|
|
|
ctx = init_graphics_yutani_double_buffer(wina);
|
|
|
|
|
|
|
|
draw_fill(ctx, rgb(0,0,0));
|
|
|
|
redraw_borders();
|
|
|
|
flip(ctx);
|
|
|
|
yutani_flip(yctx, wina);
|
|
|
|
|
|
|
|
yutani_window_advertise_icon(yctx, wina, "Plasma", "plasma");
|
|
|
|
|
|
|
|
pthread_t thread;
|
|
|
|
pthread_create(&thread, NULL, draw_thread, NULL);
|
|
|
|
|
2019-07-05 10:46:58 +03:00
|
|
|
signal(SIGINT, sigint_handler);
|
2018-04-17 10:48:47 +03:00
|
|
|
while (!should_exit) {
|
|
|
|
yutani_msg_t * m = yutani_poll(yctx);
|
2018-04-29 17:50:23 +03:00
|
|
|
while (m) {
|
|
|
|
menu_process_event(yctx, m);
|
2018-04-17 10:48:47 +03:00
|
|
|
switch (m->type) {
|
|
|
|
case YUTANI_MSG_KEY_EVENT:
|
|
|
|
{
|
|
|
|
struct yutani_msg_key_event * ke = (void*)m->data;
|
|
|
|
if (ke->event.action == KEY_ACTION_DOWN && ke->event.keycode == 'q') {
|
|
|
|
should_exit = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
|
|
|
|
{
|
|
|
|
struct yutani_msg_window_focus_change * wf = (void*)m->data;
|
2021-05-31 04:47:02 +03:00
|
|
|
yutani_window_t * win = hashmap_get(yctx->windows, (void*)(uintptr_t)wf->wid);
|
2018-04-29 17:50:23 +03:00
|
|
|
if (win && win == wina) {
|
2018-04-17 10:48:47 +03:00
|
|
|
win->focused = wf->focused;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2018-04-27 16:38:36 +03:00
|
|
|
case YUTANI_MSG_WINDOW_CLOSE:
|
2018-04-17 10:48:47 +03:00
|
|
|
case YUTANI_MSG_SESSION_END:
|
|
|
|
should_exit = 1;
|
|
|
|
break;
|
|
|
|
case YUTANI_MSG_RESIZE_OFFER:
|
|
|
|
{
|
|
|
|
struct yutani_msg_window_resize * wr = (void*)m->data;
|
2018-04-29 17:50:23 +03:00
|
|
|
if (wr->wid == wina->wid) {
|
|
|
|
spin_lock(&draw_lock);
|
|
|
|
resize_finish(wr->width, wr->height);
|
|
|
|
spin_unlock(&draw_lock);
|
|
|
|
}
|
2018-04-17 10:48:47 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case YUTANI_MSG_WINDOW_MOUSE_EVENT:
|
2018-04-29 17:50:23 +03:00
|
|
|
{
|
|
|
|
struct yutani_msg_window_mouse_event * me = (void*)m->data;
|
|
|
|
switch (decor_handle_event(yctx, m)) {
|
|
|
|
case DECOR_CLOSE:
|
|
|
|
should_exit = 1;
|
|
|
|
break;
|
|
|
|
case DECOR_RIGHT:
|
|
|
|
decor_show_default_menu(wina, wina->x + me->new_x, wina->y + me->new_y);
|
|
|
|
break;
|
|
|
|
}
|
2018-04-17 10:48:47 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(m);
|
2018-04-29 17:50:23 +03:00
|
|
|
m = yutani_poll_async(yctx);
|
2018-04-17 10:48:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 08:53:04 +03:00
|
|
|
wait(NULL);
|
|
|
|
|
2018-04-17 10:48:47 +03:00
|
|
|
yutani_close(yctx, wina);
|
|
|
|
return 0;
|
|
|
|
}
|