2008-12-02 23:15:01 +03:00
|
|
|
/*
|
|
|
|
* Copyright © 2008 Kristian Høgsberg
|
|
|
|
*
|
2015-06-11 08:48:59 +03:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
2008-12-02 23:15:01 +03:00
|
|
|
*
|
2015-06-11 08:48:59 +03:00
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
2008-12-02 23:15:01 +03:00
|
|
|
*/
|
|
|
|
|
2014-04-07 15:40:35 +04:00
|
|
|
#include "config.h"
|
|
|
|
|
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 <math.h>
|
|
|
|
#include <cairo.h>
|
2019-04-27 00:57:31 +03:00
|
|
|
#include <errno.h>
|
2010-12-17 17:53:12 +03:00
|
|
|
#include <sys/time.h>
|
2008-10-07 18:10:36 +04:00
|
|
|
|
2012-01-31 18:54:04 +04:00
|
|
|
#include <linux/input.h>
|
2011-11-22 16:18:50 +04:00
|
|
|
#include <wayland-client.h>
|
2010-06-08 22:08:30 +04:00
|
|
|
#include "window.h"
|
2008-09-30 17:46:10 +04:00
|
|
|
|
2012-01-10 18:43:58 +04:00
|
|
|
struct flower {
|
|
|
|
struct display *display;
|
|
|
|
struct window *window;
|
|
|
|
struct widget *widget;
|
|
|
|
int width, height;
|
|
|
|
};
|
|
|
|
|
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);
|
2010-08-30 16:21:44 +04:00
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
|
|
|
|
cairo_set_source_rgba(cr, 0, 0, 0, 0);
|
|
|
|
cairo_paint(cr);
|
|
|
|
|
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
|
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,
|
2015-05-15 18:17:47 +03:00
|
|
|
x2, y2);
|
2008-10-12 06:13:51 +04:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-01-31 20:53:20 +04:00
|
|
|
static void
|
|
|
|
resize_handler(struct widget *widget,
|
|
|
|
int32_t width, int32_t height, void *data)
|
|
|
|
{
|
|
|
|
struct flower *flower = data;
|
|
|
|
|
2016-04-02 19:03:15 +03:00
|
|
|
/* Don't resize me */
|
2012-01-31 20:53:20 +04:00
|
|
|
widget_set_size(flower->widget, flower->width, flower->height);
|
|
|
|
}
|
|
|
|
|
2012-01-10 21:23:19 +04:00
|
|
|
static void
|
|
|
|
redraw_handler(struct widget *widget, void *data)
|
|
|
|
{
|
|
|
|
struct flower *flower = data;
|
|
|
|
cairo_surface_t *surface;
|
|
|
|
|
|
|
|
surface = window_get_surface(flower->window);
|
|
|
|
if (surface == NULL ||
|
|
|
|
cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
|
|
|
|
fprintf(stderr, "failed to create cairo egl surface\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
draw_stuff(surface, flower->width, flower->height);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
}
|
|
|
|
|
2008-11-26 20:57:31 +03:00
|
|
|
static void
|
2012-01-09 20:12:05 +04:00
|
|
|
button_handler(struct widget *widget,
|
2010-12-17 17:53:12 +03:00
|
|
|
struct input *input, uint32_t time,
|
2012-05-30 19:31:51 +04:00
|
|
|
uint32_t button, enum wl_pointer_button_state state, void *data)
|
2008-11-26 20:57:31 +03:00
|
|
|
{
|
2012-01-10 18:43:58 +04:00
|
|
|
struct flower *flower = data;
|
2012-01-09 20:12:05 +04:00
|
|
|
|
2012-01-31 18:54:04 +04:00
|
|
|
switch (button) {
|
|
|
|
case BTN_LEFT:
|
2012-05-30 19:31:51 +04:00
|
|
|
if (state == WL_POINTER_BUTTON_STATE_PRESSED)
|
2012-04-12 06:42:15 +04:00
|
|
|
window_move(flower->window, input,
|
|
|
|
display_get_serial(flower->display));
|
2012-01-31 18:54:04 +04:00
|
|
|
break;
|
|
|
|
case BTN_MIDDLE:
|
2012-05-30 19:31:51 +04:00
|
|
|
if (state == WL_POINTER_BUTTON_STATE_PRESSED)
|
2012-01-31 18:54:04 +04:00
|
|
|
widget_schedule_redraw(widget);
|
|
|
|
break;
|
|
|
|
case BTN_RIGHT:
|
2012-05-30 19:31:51 +04:00
|
|
|
if (state == WL_POINTER_BUTTON_STATE_PRESSED)
|
2012-01-31 18:54:04 +04:00
|
|
|
window_show_frame_menu(flower->window, input, time);
|
|
|
|
break;
|
|
|
|
}
|
2008-10-08 21:32:07 +04:00
|
|
|
}
|
|
|
|
|
2013-08-15 20:10:08 +04:00
|
|
|
static void
|
2015-05-15 18:17:47 +03:00
|
|
|
touch_down_handler(struct widget *widget, struct input *input,
|
|
|
|
uint32_t serial, uint32_t time, int32_t id,
|
2013-08-15 20:10:08 +04:00
|
|
|
float x, float y, void *data)
|
|
|
|
{
|
|
|
|
struct flower *flower = data;
|
2013-11-13 05:19:57 +04:00
|
|
|
window_move(flower->window, input, display_get_serial(flower->display));
|
2013-08-15 20:10:08 +04:00
|
|
|
}
|
|
|
|
|
2008-09-30 17:46:10 +04:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2008-11-07 22:27:23 +03:00
|
|
|
struct flower flower;
|
2010-06-08 22:08:30 +04:00
|
|
|
struct display *d;
|
2010-12-17 17:53:12 +03:00
|
|
|
struct timeval tv;
|
2008-10-12 06:13:51 +04:00
|
|
|
|
2013-02-21 00:27:49 +04:00
|
|
|
d = display_create(&argc, argv);
|
2010-11-22 22:34:19 +03:00
|
|
|
if (d == NULL) {
|
2019-04-27 00:57:31 +03:00
|
|
|
fprintf(stderr, "failed to create display: %s\n",
|
|
|
|
strerror(errno));
|
2010-11-22 22:34:19 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2008-09-30 17:46:10 +04:00
|
|
|
|
2010-12-17 17:53:12 +03:00
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
srandom(tv.tv_usec);
|
|
|
|
|
2008-11-07 22:27:23 +03:00
|
|
|
flower.width = 200;
|
|
|
|
flower.height = 200;
|
2010-09-03 22:46:38 +04:00
|
|
|
flower.display = d;
|
2012-02-01 00:24:48 +04:00
|
|
|
flower.window = window_create(d);
|
2012-01-10 18:43:58 +04:00
|
|
|
flower.widget = window_add_widget(flower.window, &flower);
|
2012-10-07 18:56:30 +04:00
|
|
|
window_set_title(flower.window, "Flower");
|
2008-11-02 23:28:22 +03:00
|
|
|
|
2012-01-31 20:53:20 +04:00
|
|
|
widget_set_resize_handler(flower.widget, resize_handler);
|
2012-01-10 21:23:19 +04:00
|
|
|
widget_set_redraw_handler(flower.widget, redraw_handler);
|
2012-01-10 18:43:58 +04:00
|
|
|
widget_set_button_handler(flower.widget, button_handler);
|
2012-11-30 23:54:35 +04:00
|
|
|
widget_set_default_cursor(flower.widget, CURSOR_HAND1);
|
2013-08-15 20:10:08 +04:00
|
|
|
widget_set_touch_down_handler(flower.widget, touch_down_handler);
|
2012-01-10 21:23:19 +04:00
|
|
|
|
2012-02-01 00:24:48 +04:00
|
|
|
window_schedule_resize(flower.window, flower.width, flower.height);
|
2012-01-10 21:23:19 +04:00
|
|
|
|
2010-06-08 22:59:44 +04:00
|
|
|
display_run(d);
|
2008-09-30 17:46:10 +04:00
|
|
|
|
2014-05-15 17:28:16 +04:00
|
|
|
widget_destroy(flower.widget);
|
|
|
|
window_destroy(flower.window);
|
|
|
|
display_destroy(d);
|
|
|
|
|
2008-09-30 17:46:10 +04:00
|
|
|
return 0;
|
|
|
|
}
|