blob: 15315b238d9c98a3eaeb5961c5fe3c8df27f20c7 [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
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010023#include "config.h"
24
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040025#include <stdint.h>
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040026#include <stdio.h>
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040027#include <stdlib.h>
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040028#include <string.h>
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -050029#include <time.h>
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040030#include <math.h>
31#include <cairo.h>
Kristian Høgsberg82da52b2010-12-17 09:53:12 -050032#include <sys/time.h>
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040033
Kristian Høgsbergc25a1d72012-01-31 09:54:04 -050034#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020035#include <wayland-client.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040036#include "window.h"
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040037
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050038struct flower {
39 struct display *display;
40 struct window *window;
41 struct widget *widget;
42 int width, height;
43};
44
Kristian Høgsberga234e702008-10-11 22:13:51 -040045static void
46set_random_color(cairo_t *cr)
47{
48 cairo_set_source_rgba(cr,
Kristian Høgsbergd311e8a2008-10-12 22:58:40 -040049 0.5 + (random() % 50) / 49.0,
50 0.5 + (random() % 50) / 49.0,
51 0.5 + (random() % 50) / 49.0,
52 0.5 + (random() % 100) / 99.0);
Kristian Høgsberga234e702008-10-11 22:13:51 -040053}
54
55
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050056static void
57draw_stuff(cairo_surface_t *surface, int width, int height)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040058{
Kristian Høgsberga234e702008-10-11 22:13:51 -040059 const int petal_count = 3 + random() % 5;
60 const double r1 = 60 + random() % 35;
61 const double r2 = 20 + random() % 40;
62 const double u = (10 + random() % 90) / 100.0;
63 const double v = (random() % 90) / 100.0;
64
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040065 cairo_t *cr;
Kristian Høgsberga234e702008-10-11 22:13:51 -040066 int i;
67 double t, dt = 2 * M_PI / (petal_count * 2);
68 double x1, y1, x2, y2, x3, y3;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040069
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040070 cr = cairo_create(surface);
Kristian Høgsberg8ca1cc22010-08-30 08:21:44 -040071 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
72 cairo_set_source_rgba(cr, 0, 0, 0, 0);
73 cairo_paint(cr);
74
75 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberga234e702008-10-11 22:13:51 -040076 cairo_translate(cr, width / 2, height / 2);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050077 cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
Kristian Høgsberga234e702008-10-11 22:13:51 -040078 for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
79 x1 = cos(t) * r1;
80 y1 = sin(t) * r1;
81 x2 = cos(t + dt) * r2;
82 y2 = sin(t + dt) * r2;
83 x3 = cos(t + 2 * dt) * r1;
84 y3 = sin(t + 2 * dt) * r1;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040085
Kristian Høgsberga234e702008-10-11 22:13:51 -040086 cairo_curve_to(cr,
87 x1 - y1 * u, y1 + x1 * u,
88 x2 + y2 * v, y2 - x2 * v,
Michael Vetter2a18a522015-05-15 17:17:47 +020089 x2, y2);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040090
Kristian Høgsberga234e702008-10-11 22:13:51 -040091 cairo_curve_to(cr,
92 x2 - y2 * v, y2 + x2 * v,
93 x3 + y3 * u, y3 - x3 * u,
94 x3, y3);
95 }
96
97 cairo_close_path(cr);
98 set_random_color(cr);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040099 cairo_fill_preserve(cr);
Kristian Høgsberga234e702008-10-11 22:13:51 -0400100 set_random_color(cr);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400101 cairo_stroke(cr);
102
103 cairo_destroy(cr);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400104}
105
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500106static void
Kristian Høgsberg8e054f72012-01-31 11:53:20 -0500107resize_handler(struct widget *widget,
108 int32_t width, int32_t height, void *data)
109{
110 struct flower *flower = data;
111
112 /* Dont resize me */
113 widget_set_size(flower->widget, flower->width, flower->height);
114}
115
116static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500117redraw_handler(struct widget *widget, void *data)
118{
119 struct flower *flower = data;
120 cairo_surface_t *surface;
121
122 surface = window_get_surface(flower->window);
123 if (surface == NULL ||
124 cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
125 fprintf(stderr, "failed to create cairo egl surface\n");
126 return;
127 }
128
129 draw_stuff(surface, flower->width, flower->height);
130 cairo_surface_destroy(surface);
131}
132
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500133static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500134button_handler(struct widget *widget,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500135 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100136 uint32_t button, enum wl_pointer_button_state state, void *data)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500137{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500138 struct flower *flower = data;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500139
Kristian Høgsbergc25a1d72012-01-31 09:54:04 -0500140 switch (button) {
141 case BTN_LEFT:
Daniel Stone4dbadb12012-05-30 16:31:51 +0100142 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400143 window_move(flower->window, input,
144 display_get_serial(flower->display));
Kristian Høgsbergc25a1d72012-01-31 09:54:04 -0500145 break;
146 case BTN_MIDDLE:
Daniel Stone4dbadb12012-05-30 16:31:51 +0100147 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergc25a1d72012-01-31 09:54:04 -0500148 widget_schedule_redraw(widget);
149 break;
150 case BTN_RIGHT:
Daniel Stone4dbadb12012-05-30 16:31:51 +0100151 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergc25a1d72012-01-31 09:54:04 -0500152 window_show_frame_menu(flower->window, input, time);
153 break;
154 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500155}
156
Rusty Lynch1084da52013-08-15 09:10:08 -0700157static void
Michael Vetter2a18a522015-05-15 17:17:47 +0200158touch_down_handler(struct widget *widget, struct input *input,
159 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch1084da52013-08-15 09:10:08 -0700160 float x, float y, void *data)
161{
162 struct flower *flower = data;
Jasper St. Pierre01eaaac2013-11-12 20:19:57 -0500163 window_move(flower->window, input, display_get_serial(flower->display));
Rusty Lynch1084da52013-08-15 09:10:08 -0700164}
165
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400166int main(int argc, char *argv[])
167{
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500168 struct flower flower;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400169 struct display *d;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500170 struct timeval tv;
Kristian Høgsberga234e702008-10-11 22:13:51 -0400171
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500172 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200173 if (d == NULL) {
174 fprintf(stderr, "failed to create display: %m\n");
175 return -1;
176 }
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400177
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500178 gettimeofday(&tv, NULL);
179 srandom(tv.tv_usec);
180
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500181 flower.width = 200;
182 flower.height = 200;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400183 flower.display = d;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500184 flower.window = window_create(d);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500185 flower.widget = window_add_widget(flower.window, &flower);
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600186 window_set_title(flower.window, "Flower");
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500187
Kristian Høgsberg8e054f72012-01-31 11:53:20 -0500188 widget_set_resize_handler(flower.widget, resize_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500189 widget_set_redraw_handler(flower.widget, redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500190 widget_set_button_handler(flower.widget, button_handler);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500191 widget_set_default_cursor(flower.widget, CURSOR_HAND1);
Rusty Lynch1084da52013-08-15 09:10:08 -0700192 widget_set_touch_down_handler(flower.widget, touch_down_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500193
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500194 window_schedule_resize(flower.window, flower.width, flower.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500195
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400196 display_run(d);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400197
vivek31732f72014-05-15 18:58:16 +0530198 widget_destroy(flower.widget);
199 window_destroy(flower.window);
200 display_destroy(d);
201
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400202 return 0;
203}