2018-08-15 04:07:33 +03:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
2018-08-01 03:21:13 +03:00
|
|
|
* This file is part of ToaruOS and is released under the terms
|
2018-02-25 11:14:43 +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-02-25 11:14:43 +03:00
|
|
|
*
|
2018-08-15 04:07:33 +03:00
|
|
|
* drawlines - Draw random lines into a GUI window
|
2018-08-01 03:21:13 +03:00
|
|
|
*
|
2018-08-15 04:07:33 +03:00
|
|
|
* The original compositor demo application, this dates all the
|
|
|
|
* way back to the original pre-Yutani compositor. Opens a very
|
|
|
|
* basic window (no decorations) and randomly fills it with
|
|
|
|
* colorful lines in a separate thread from the listener.
|
|
|
|
*
|
|
|
|
* There's no good reason for this to use threads - it should use
|
|
|
|
* `fswait2` to apply timeouts - but it demonstrates threading
|
|
|
|
* so we'll leave it that way for now.
|
2018-02-25 11:14:43 +03:00
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
2018-05-09 15:26:45 +03:00
|
|
|
#include <pthread.h>
|
2018-08-01 03:21:13 +03:00
|
|
|
#include <time.h>
|
2018-08-02 07:13:27 +03:00
|
|
|
#include <sched.h>
|
2018-02-25 11:14:43 +03:00
|
|
|
|
2018-03-19 05:38:11 +03:00
|
|
|
#include <toaru/yutani.h>
|
|
|
|
#include <toaru/graphics.h>
|
2018-02-25 11:14:43 +03:00
|
|
|
|
|
|
|
static int left, top, width, height;
|
|
|
|
|
|
|
|
static yutani_t * yctx;
|
|
|
|
static yutani_window_t * wina;
|
|
|
|
static gfx_context_t * ctx;
|
|
|
|
static int should_exit = 0;
|
|
|
|
|
|
|
|
void * draw_thread(void * garbage) {
|
|
|
|
(void)garbage;
|
|
|
|
while (!should_exit) {
|
2018-05-09 13:43:11 +03:00
|
|
|
draw_line(ctx, rand() % width, rand() % width, rand() % height, rand() % height, rgb(rand() % 255,rand() % 255,rand() % 255));
|
2018-02-25 11:14:43 +03:00
|
|
|
yutani_flip(yctx, wina);
|
|
|
|
usleep(16666);
|
|
|
|
}
|
|
|
|
pthread_exit(0);
|
2018-04-25 08:03:29 +03:00
|
|
|
return NULL;
|
2018-02-25 11:14:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc, char ** argv) {
|
|
|
|
left = 100;
|
|
|
|
top = 100;
|
|
|
|
width = 500;
|
|
|
|
height = 500;
|
|
|
|
|
2018-05-09 13:43:11 +03:00
|
|
|
srand(time(NULL));
|
|
|
|
|
2018-02-25 11:14:43 +03:00
|
|
|
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-02-25 11:14:43 +03:00
|
|
|
wina = yutani_window_create(yctx, width, height);
|
|
|
|
yutani_window_move(yctx, wina, left, top);
|
|
|
|
yutani_window_advertise_icon(yctx, wina, "drawlines", "drawlines");
|
|
|
|
|
|
|
|
ctx = init_graphics_yutani(wina);
|
|
|
|
draw_fill(ctx, rgb(0,0,0));
|
|
|
|
|
|
|
|
pthread_t thread;
|
|
|
|
pthread_create(&thread, NULL, draw_thread, NULL);
|
|
|
|
|
|
|
|
while (!should_exit) {
|
|
|
|
yutani_msg_t * m = yutani_poll(yctx);
|
|
|
|
if (m) {
|
|
|
|
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;
|
2018-08-02 07:13:27 +03:00
|
|
|
sched_yield();
|
2018-02-25 11:14:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case YUTANI_MSG_WINDOW_MOUSE_EVENT:
|
|
|
|
{
|
|
|
|
struct yutani_msg_window_mouse_event * me = (void*)m->data;
|
|
|
|
if (me->command == YUTANI_MOUSE_EVENT_DOWN && me->buttons & YUTANI_MOUSE_BUTTON_LEFT) {
|
|
|
|
yutani_window_drag_start(yctx, wina);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2018-04-27 16:38:36 +03:00
|
|
|
case YUTANI_MSG_WINDOW_CLOSE:
|
2018-02-25 11:14:43 +03:00
|
|
|
case YUTANI_MSG_SESSION_END:
|
|
|
|
should_exit = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
yutani_close(yctx, wina);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|