blob: 389f8bee39db72b7e94cf30d32f0b5a754751249 [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>
29#include <cairo.h>
Kristian Høgsberg82da52b2010-12-17 09:53:12 -050030#include <sys/time.h>
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040031
Kristian Høgsbergc25a1d72012-01-31 09:54:04 -050032#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020033#include <wayland-client.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040034#include "window.h"
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040035
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050036struct flower {
37 struct display *display;
38 struct window *window;
39 struct widget *widget;
40 int width, height;
41};
42
Kristian Høgsberga234e702008-10-11 22:13:51 -040043static void
44set_random_color(cairo_t *cr)
45{
46 cairo_set_source_rgba(cr,
Kristian Høgsbergd311e8a2008-10-12 22:58:40 -040047 0.5 + (random() % 50) / 49.0,
48 0.5 + (random() % 50) / 49.0,
49 0.5 + (random() % 50) / 49.0,
50 0.5 + (random() % 100) / 99.0);
Kristian Høgsberga234e702008-10-11 22:13:51 -040051}
52
53
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050054static void
55draw_stuff(cairo_surface_t *surface, int width, int height)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040056{
Kristian Høgsberga234e702008-10-11 22:13:51 -040057 const int petal_count = 3 + random() % 5;
58 const double r1 = 60 + random() % 35;
59 const double r2 = 20 + random() % 40;
60 const double u = (10 + random() % 90) / 100.0;
61 const double v = (random() % 90) / 100.0;
62
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040063 cairo_t *cr;
Kristian Høgsberga234e702008-10-11 22:13:51 -040064 int i;
65 double t, dt = 2 * M_PI / (petal_count * 2);
66 double x1, y1, x2, y2, x3, y3;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040067
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040068 cr = cairo_create(surface);
Kristian Høgsberg8ca1cc22010-08-30 08:21:44 -040069 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
70 cairo_set_source_rgba(cr, 0, 0, 0, 0);
71 cairo_paint(cr);
72
73 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberga234e702008-10-11 22:13:51 -040074 cairo_translate(cr, width / 2, height / 2);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050075 cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
Kristian Høgsberga234e702008-10-11 22:13:51 -040076 for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
77 x1 = cos(t) * r1;
78 y1 = sin(t) * r1;
79 x2 = cos(t + dt) * r2;
80 y2 = sin(t + dt) * r2;
81 x3 = cos(t + 2 * dt) * r1;
82 y3 = sin(t + 2 * dt) * r1;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040083
Kristian Høgsberga234e702008-10-11 22:13:51 -040084 cairo_curve_to(cr,
85 x1 - y1 * u, y1 + x1 * u,
86 x2 + y2 * v, y2 - x2 * v,
87 x2, y2);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040088
Kristian Høgsberga234e702008-10-11 22:13:51 -040089 cairo_curve_to(cr,
90 x2 - y2 * v, y2 + x2 * v,
91 x3 + y3 * u, y3 - x3 * u,
92 x3, y3);
93 }
94
95 cairo_close_path(cr);
96 set_random_color(cr);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040097 cairo_fill_preserve(cr);
Kristian Høgsberga234e702008-10-11 22:13:51 -040098 set_random_color(cr);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040099 cairo_stroke(cr);
100
101 cairo_destroy(cr);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400102}
103
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500104static void
Kristian Høgsberg8e054f72012-01-31 11:53:20 -0500105resize_handler(struct widget *widget,
106 int32_t width, int32_t height, void *data)
107{
108 struct flower *flower = data;
109
110 /* Dont resize me */
111 widget_set_size(flower->widget, flower->width, flower->height);
112}
113
114static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500115redraw_handler(struct widget *widget, void *data)
116{
117 struct flower *flower = data;
118 cairo_surface_t *surface;
119
120 surface = window_get_surface(flower->window);
121 if (surface == NULL ||
122 cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
123 fprintf(stderr, "failed to create cairo egl surface\n");
124 return;
125 }
126
127 draw_stuff(surface, flower->width, flower->height);
128 cairo_surface_destroy(surface);
129}
130
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500131static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500132button_handler(struct widget *widget,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500133 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100134 uint32_t button, enum wl_pointer_button_state state, void *data)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500135{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500136 struct flower *flower = data;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500137
Kristian Høgsbergc25a1d72012-01-31 09:54:04 -0500138 switch (button) {
139 case BTN_LEFT:
Daniel Stone4dbadb12012-05-30 16:31:51 +0100140 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400141 window_move(flower->window, input,
142 display_get_serial(flower->display));
Kristian Høgsbergc25a1d72012-01-31 09:54:04 -0500143 break;
144 case BTN_MIDDLE:
Daniel Stone4dbadb12012-05-30 16:31:51 +0100145 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergc25a1d72012-01-31 09:54:04 -0500146 widget_schedule_redraw(widget);
147 break;
148 case BTN_RIGHT:
Daniel Stone4dbadb12012-05-30 16:31:51 +0100149 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergc25a1d72012-01-31 09:54:04 -0500150 window_show_frame_menu(flower->window, input, time);
151 break;
152 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500153}
154
Rusty Lynch1084da52013-08-15 09:10:08 -0700155static void
156touch_down_handler(struct widget *widget, struct input *input,
157 uint32_t serial, uint32_t time, int32_t id,
158 float x, float y, void *data)
159{
160 struct flower *flower = data;
Jasper St. Pierre01eaaac2013-11-12 20:19:57 -0500161 window_move(flower->window, input, display_get_serial(flower->display));
Rusty Lynch1084da52013-08-15 09:10:08 -0700162}
163
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400164int main(int argc, char *argv[])
165{
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500166 struct flower flower;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400167 struct display *d;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500168 struct timeval tv;
Kristian Høgsberga234e702008-10-11 22:13:51 -0400169
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500170 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200171 if (d == NULL) {
172 fprintf(stderr, "failed to create display: %m\n");
173 return -1;
174 }
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400175
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500176 gettimeofday(&tv, NULL);
177 srandom(tv.tv_usec);
178
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500179 flower.width = 200;
180 flower.height = 200;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400181 flower.display = d;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500182 flower.window = window_create(d);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500183 flower.widget = window_add_widget(flower.window, &flower);
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600184 window_set_title(flower.window, "Flower");
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500185
Kristian Høgsberg8e054f72012-01-31 11:53:20 -0500186 widget_set_resize_handler(flower.widget, resize_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500187 widget_set_redraw_handler(flower.widget, redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500188 widget_set_button_handler(flower.widget, button_handler);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500189 widget_set_default_cursor(flower.widget, CURSOR_HAND1);
Rusty Lynch1084da52013-08-15 09:10:08 -0700190 widget_set_touch_down_handler(flower.widget, touch_down_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500191
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500192 window_schedule_resize(flower.window, flower.width, flower.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500193
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400194 display_run(d);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400195
196 return 0;
197}