blob: 87a1a00726ec4a623da7fbc797eda23c62361f2c [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040023#include <stdint.h>
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040024#include <stdio.h>
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040025#include <stdlib.h>
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040026#include <string.h>
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -050027#include <time.h>
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040028#include <math.h>
Kristian Høgsberga234e702008-10-11 22:13:51 -040029#include <time.h>
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040030#include <cairo.h>
Kristian Høgsberg82da52b2010-12-17 09:53:12 -050031#include <sys/time.h>
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040032
Kristian Høgsbergc25a1d72012-01-31 09:54:04 -050033#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020034#include <wayland-client.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040035#include "window.h"
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040036
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050037struct flower {
38 struct display *display;
39 struct window *window;
40 struct widget *widget;
41 int width, height;
42};
43
Kristian Høgsberga234e702008-10-11 22:13:51 -040044static void
45set_random_color(cairo_t *cr)
46{
47 cairo_set_source_rgba(cr,
Kristian Høgsbergd311e8a2008-10-12 22:58:40 -040048 0.5 + (random() % 50) / 49.0,
49 0.5 + (random() % 50) / 49.0,
50 0.5 + (random() % 50) / 49.0,
51 0.5 + (random() % 100) / 99.0);
Kristian Høgsberga234e702008-10-11 22:13:51 -040052}
53
54
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050055static void
56draw_stuff(cairo_surface_t *surface, int width, int height)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040057{
Kristian Høgsberga234e702008-10-11 22:13:51 -040058 const int petal_count = 3 + random() % 5;
59 const double r1 = 60 + random() % 35;
60 const double r2 = 20 + random() % 40;
61 const double u = (10 + random() % 90) / 100.0;
62 const double v = (random() % 90) / 100.0;
63
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040064 cairo_t *cr;
Kristian Høgsberga234e702008-10-11 22:13:51 -040065 int i;
66 double t, dt = 2 * M_PI / (petal_count * 2);
67 double x1, y1, x2, y2, x3, y3;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040068
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040069 cr = cairo_create(surface);
Kristian Høgsberg8ca1cc22010-08-30 08:21:44 -040070 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
71 cairo_set_source_rgba(cr, 0, 0, 0, 0);
72 cairo_paint(cr);
73
74 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberga234e702008-10-11 22:13:51 -040075 cairo_translate(cr, width / 2, height / 2);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050076 cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
Kristian Høgsberga234e702008-10-11 22:13:51 -040077 for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
78 x1 = cos(t) * r1;
79 y1 = sin(t) * r1;
80 x2 = cos(t + dt) * r2;
81 y2 = sin(t + dt) * r2;
82 x3 = cos(t + 2 * dt) * r1;
83 y3 = sin(t + 2 * dt) * r1;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040084
Kristian Høgsberga234e702008-10-11 22:13:51 -040085 cairo_curve_to(cr,
86 x1 - y1 * u, y1 + x1 * u,
87 x2 + y2 * v, y2 - x2 * v,
88 x2, y2);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040089
Kristian Høgsberga234e702008-10-11 22:13:51 -040090 cairo_curve_to(cr,
91 x2 - y2 * v, y2 + x2 * v,
92 x3 + y3 * u, y3 - x3 * u,
93 x3, y3);
94 }
95
96 cairo_close_path(cr);
97 set_random_color(cr);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040098 cairo_fill_preserve(cr);
Kristian Høgsberga234e702008-10-11 22:13:51 -040099 set_random_color(cr);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400100 cairo_stroke(cr);
101
102 cairo_destroy(cr);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400103}
104
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500105static void
Kristian Høgsberg8e054f72012-01-31 11:53:20 -0500106resize_handler(struct widget *widget,
107 int32_t width, int32_t height, void *data)
108{
109 struct flower *flower = data;
110
111 /* Dont resize me */
112 widget_set_size(flower->widget, flower->width, flower->height);
113}
114
115static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500116redraw_handler(struct widget *widget, void *data)
117{
118 struct flower *flower = data;
119 cairo_surface_t *surface;
120
121 surface = window_get_surface(flower->window);
122 if (surface == NULL ||
123 cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
124 fprintf(stderr, "failed to create cairo egl surface\n");
125 return;
126 }
127
128 draw_stuff(surface, flower->width, flower->height);
129 cairo_surface_destroy(surface);
130}
131
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500132static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500133button_handler(struct widget *widget,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500134 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100135 uint32_t button, enum wl_pointer_button_state state, void *data)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500136{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500137 struct flower *flower = data;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500138
Kristian Høgsbergc25a1d72012-01-31 09:54:04 -0500139 switch (button) {
140 case BTN_LEFT:
Daniel Stone4dbadb12012-05-30 16:31:51 +0100141 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400142 window_move(flower->window, input,
143 display_get_serial(flower->display));
Kristian Høgsbergc25a1d72012-01-31 09:54:04 -0500144 break;
145 case BTN_MIDDLE:
Daniel Stone4dbadb12012-05-30 16:31:51 +0100146 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergc25a1d72012-01-31 09:54:04 -0500147 widget_schedule_redraw(widget);
148 break;
149 case BTN_RIGHT:
Daniel Stone4dbadb12012-05-30 16:31:51 +0100150 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergc25a1d72012-01-31 09:54:04 -0500151 window_show_frame_menu(flower->window, input, time);
152 break;
153 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500154}
155
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400156int main(int argc, char *argv[])
157{
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500158 struct flower flower;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400159 struct display *d;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500160 struct timeval tv;
Kristian Høgsberga234e702008-10-11 22:13:51 -0400161
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500162 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200163 if (d == NULL) {
164 fprintf(stderr, "failed to create display: %m\n");
165 return -1;
166 }
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400167
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500168 gettimeofday(&tv, NULL);
169 srandom(tv.tv_usec);
170
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500171 flower.width = 200;
172 flower.height = 200;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400173 flower.display = d;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500174 flower.window = window_create(d);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500175 flower.widget = window_add_widget(flower.window, &flower);
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600176 window_set_title(flower.window, "Flower");
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500177
Kristian Høgsberg8e054f72012-01-31 11:53:20 -0500178 widget_set_resize_handler(flower.widget, resize_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500179 widget_set_redraw_handler(flower.widget, redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500180 widget_set_button_handler(flower.widget, button_handler);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500181 widget_set_default_cursor(flower.widget, CURSOR_HAND1);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500182
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500183 window_schedule_resize(flower.window, flower.width, flower.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500184
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400185 display_run(d);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400186
187 return 0;
188}