2008-12-02 23:15:01 +03:00
|
|
|
/*
|
|
|
|
* Copyright © 2008 Kristian Høgsberg
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, distribute, and sell this software and its
|
|
|
|
* documentation for any purpose is hereby granted without fee, provided that
|
|
|
|
* the above copyright notice appear in all copies and that both that copyright
|
|
|
|
* notice and this permission notice appear in supporting documentation, and
|
|
|
|
* that the name of the copyright holders not be used in advertising or
|
|
|
|
* publicity pertaining to distribution of the software without specific,
|
|
|
|
* written prior permission. The copyright holders make no representations
|
|
|
|
* about the suitability of this software for any purpose. It is provided "as
|
|
|
|
* is" without express or implied warranty.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
|
|
|
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
|
|
|
|
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
|
|
|
|
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
|
|
|
|
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
|
|
|
* OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2008-09-30 17:46:10 +04:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2008-10-07 18:10:36 +04:00
|
|
|
#include <stdlib.h>
|
2008-09-30 17:46:10 +04:00
|
|
|
#include <string.h>
|
2008-11-02 23:28:22 +03:00
|
|
|
#include <time.h>
|
2008-10-07 18:10:36 +04:00
|
|
|
#include <fcntl.h>
|
2008-09-30 17:46:10 +04:00
|
|
|
#include <unistd.h>
|
2008-10-07 18:10:36 +04:00
|
|
|
#include <math.h>
|
2008-10-12 06:13:51 +04:00
|
|
|
#include <time.h>
|
2008-10-07 18:10:36 +04:00
|
|
|
#include <cairo.h>
|
2008-11-07 22:27:23 +03:00
|
|
|
#include <glib.h>
|
2008-10-07 18:10:36 +04:00
|
|
|
|
|
|
|
#include "wayland-client.h"
|
2008-11-07 22:27:23 +03:00
|
|
|
#include "wayland-glib.h"
|
2010-06-08 22:08:30 +04:00
|
|
|
#include "window.h"
|
2008-09-30 17:46:10 +04:00
|
|
|
|
2008-10-12 06:13:51 +04:00
|
|
|
static void
|
|
|
|
set_random_color(cairo_t *cr)
|
|
|
|
{
|
|
|
|
cairo_set_source_rgba(cr,
|
2008-10-13 06:58:40 +04:00
|
|
|
0.5 + (random() % 50) / 49.0,
|
|
|
|
0.5 + (random() % 50) / 49.0,
|
|
|
|
0.5 + (random() % 50) / 49.0,
|
|
|
|
0.5 + (random() % 100) / 99.0);
|
2008-10-12 06:13:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-15 19:37:43 +03:00
|
|
|
static void
|
|
|
|
draw_stuff(cairo_surface_t *surface, int width, int height)
|
2008-10-07 18:10:36 +04:00
|
|
|
{
|
2008-10-12 06:13:51 +04:00
|
|
|
const int petal_count = 3 + random() % 5;
|
|
|
|
const double r1 = 60 + random() % 35;
|
|
|
|
const double r2 = 20 + random() % 40;
|
|
|
|
const double u = (10 + random() % 90) / 100.0;
|
|
|
|
const double v = (random() % 90) / 100.0;
|
|
|
|
|
2008-10-07 18:10:36 +04:00
|
|
|
cairo_t *cr;
|
2008-10-12 06:13:51 +04:00
|
|
|
int i;
|
|
|
|
double t, dt = 2 * M_PI / (petal_count * 2);
|
|
|
|
double x1, y1, x2, y2, x3, y3;
|
2008-09-30 17:46:10 +04:00
|
|
|
|
2008-10-07 18:10:36 +04:00
|
|
|
cr = cairo_create(surface);
|
2008-10-12 06:13:51 +04:00
|
|
|
cairo_translate(cr, width / 2, height / 2);
|
2009-01-15 19:37:43 +03:00
|
|
|
cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
|
2008-10-12 06:13:51 +04:00
|
|
|
for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
|
|
|
|
x1 = cos(t) * r1;
|
|
|
|
y1 = sin(t) * r1;
|
|
|
|
x2 = cos(t + dt) * r2;
|
|
|
|
y2 = sin(t + dt) * r2;
|
|
|
|
x3 = cos(t + 2 * dt) * r1;
|
|
|
|
y3 = sin(t + 2 * dt) * r1;
|
|
|
|
|
|
|
|
cairo_curve_to(cr,
|
|
|
|
x1 - y1 * u, y1 + x1 * u,
|
|
|
|
x2 + y2 * v, y2 - x2 * v,
|
|
|
|
x2, y2);
|
|
|
|
|
|
|
|
cairo_curve_to(cr,
|
|
|
|
x2 - y2 * v, y2 + x2 * v,
|
|
|
|
x3 + y3 * u, y3 - x3 * u,
|
|
|
|
x3, y3);
|
|
|
|
}
|
2008-10-07 18:10:36 +04:00
|
|
|
|
2008-10-12 06:13:51 +04:00
|
|
|
cairo_close_path(cr);
|
|
|
|
set_random_color(cr);
|
2008-10-07 18:10:36 +04:00
|
|
|
cairo_fill_preserve(cr);
|
2008-10-12 06:13:51 +04:00
|
|
|
set_random_color(cr);
|
2008-10-07 18:10:36 +04:00
|
|
|
cairo_stroke(cr);
|
|
|
|
|
|
|
|
cairo_destroy(cr);
|
2008-09-30 17:46:10 +04:00
|
|
|
}
|
|
|
|
|
2008-11-07 22:27:23 +03:00
|
|
|
struct flower {
|
2010-06-08 22:08:30 +04:00
|
|
|
struct window *window;
|
2008-11-07 22:27:23 +03:00
|
|
|
int x, y, width, height;
|
2009-01-15 20:45:49 +03:00
|
|
|
int offset;
|
2008-11-07 22:27:23 +03:00
|
|
|
};
|
|
|
|
|
2008-11-26 20:57:31 +03:00
|
|
|
static void
|
2010-06-08 22:08:30 +04:00
|
|
|
handle_frame(struct window *window,
|
|
|
|
uint32_t frame, uint32_t timestamp, void *data)
|
2008-11-26 20:57:31 +03:00
|
|
|
{
|
2008-12-30 19:03:33 +03:00
|
|
|
struct flower *flower = data;
|
|
|
|
|
2010-06-08 22:08:30 +04:00
|
|
|
window_move(flower->window,
|
|
|
|
flower->x + cos((flower->offset + timestamp) / 400.0) * 400 - flower->width / 2,
|
|
|
|
flower->y + sin((flower->offset + timestamp) / 320.0) * 300 - flower->height / 2);
|
|
|
|
window_commit(flower->window, 0);
|
2008-10-08 21:32:07 +04:00
|
|
|
}
|
|
|
|
|
2008-09-30 17:46:10 +04:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2008-10-07 18:10:36 +04:00
|
|
|
cairo_surface_t *s;
|
2008-11-02 23:28:22 +03:00
|
|
|
struct timespec ts;
|
2008-11-07 22:27:23 +03:00
|
|
|
struct flower flower;
|
2010-06-08 22:08:30 +04:00
|
|
|
struct display *d;
|
2008-10-12 06:13:51 +04:00
|
|
|
|
2010-06-08 22:59:44 +04:00
|
|
|
d = display_create(&argc, &argv, NULL);
|
2008-09-30 17:46:10 +04:00
|
|
|
|
2008-11-07 22:27:23 +03:00
|
|
|
flower.x = 512;
|
|
|
|
flower.y = 384;
|
|
|
|
flower.width = 200;
|
|
|
|
flower.height = 200;
|
2010-06-08 22:08:30 +04:00
|
|
|
flower.window = window_create(d, "flower", flower.x, flower.y,
|
|
|
|
flower.width, flower.height);
|
2008-11-02 23:28:22 +03:00
|
|
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
srandom(ts.tv_nsec);
|
2009-01-15 20:45:49 +03:00
|
|
|
flower.offset = random();
|
2008-11-02 23:28:22 +03:00
|
|
|
|
2010-06-08 22:59:44 +04:00
|
|
|
window_set_decoration(flower.window, 0);
|
2010-06-08 22:08:30 +04:00
|
|
|
window_draw(flower.window);
|
|
|
|
s = window_get_surface(flower.window);
|
|
|
|
if (s == NULL || cairo_surface_status (s) != CAIRO_STATUS_SUCCESS) {
|
|
|
|
fprintf(stderr, "failed to create cairo drm surface\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2008-12-30 19:03:33 +03:00
|
|
|
|
2010-06-08 22:08:30 +04:00
|
|
|
draw_stuff(s, flower.width, flower.height);
|
|
|
|
cairo_surface_flush(s);
|
2008-12-30 19:03:33 +03:00
|
|
|
|
2010-06-25 19:19:22 +04:00
|
|
|
window_set_user_data(flower.window, &flower);
|
|
|
|
window_set_frame_handler(flower.window, handle_frame);
|
2010-06-08 22:08:30 +04:00
|
|
|
window_commit(flower.window, 0);
|
2008-11-07 22:27:23 +03:00
|
|
|
|
2010-06-08 22:59:44 +04:00
|
|
|
display_run(d);
|
2008-09-30 17:46:10 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|