blob: aafc63c035570c16ea1f542fd1c0b55017d7f1f8 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07004 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050010 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070011 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050022 */
23
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010024#include "config.h"
25
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040026#include <stdint.h>
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040027#include <stdio.h>
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040028#include <stdlib.h>
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040029#include <string.h>
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -050030#include <time.h>
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040031#include <math.h>
32#include <cairo.h>
Kristian Høgsberg82da52b2010-12-17 09:53:12 -050033#include <sys/time.h>
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040034
Kristian Høgsbergc25a1d72012-01-31 09:54:04 -050035#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020036#include <wayland-client.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040037#include "window.h"
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040038
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050039struct flower {
40 struct display *display;
41 struct window *window;
42 struct widget *widget;
43 int width, height;
44};
45
Kristian Høgsberga234e702008-10-11 22:13:51 -040046static void
47set_random_color(cairo_t *cr)
48{
49 cairo_set_source_rgba(cr,
Kristian Høgsbergd311e8a2008-10-12 22:58:40 -040050 0.5 + (random() % 50) / 49.0,
51 0.5 + (random() % 50) / 49.0,
52 0.5 + (random() % 50) / 49.0,
53 0.5 + (random() % 100) / 99.0);
Kristian Høgsberga234e702008-10-11 22:13:51 -040054}
55
56
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050057static void
58draw_stuff(cairo_surface_t *surface, int width, int height)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040059{
Kristian Høgsberga234e702008-10-11 22:13:51 -040060 const int petal_count = 3 + random() % 5;
61 const double r1 = 60 + random() % 35;
62 const double r2 = 20 + random() % 40;
63 const double u = (10 + random() % 90) / 100.0;
64 const double v = (random() % 90) / 100.0;
65
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040066 cairo_t *cr;
Kristian Høgsberga234e702008-10-11 22:13:51 -040067 int i;
68 double t, dt = 2 * M_PI / (petal_count * 2);
69 double x1, y1, x2, y2, x3, y3;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040070
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040071 cr = cairo_create(surface);
Kristian Høgsberg8ca1cc22010-08-30 08:21:44 -040072 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
73 cairo_set_source_rgba(cr, 0, 0, 0, 0);
74 cairo_paint(cr);
75
76 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberga234e702008-10-11 22:13:51 -040077 cairo_translate(cr, width / 2, height / 2);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050078 cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
Kristian Høgsberga234e702008-10-11 22:13:51 -040079 for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
80 x1 = cos(t) * r1;
81 y1 = sin(t) * r1;
82 x2 = cos(t + dt) * r2;
83 y2 = sin(t + dt) * r2;
84 x3 = cos(t + 2 * dt) * r1;
85 y3 = sin(t + 2 * dt) * r1;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040086
Kristian Høgsberga234e702008-10-11 22:13:51 -040087 cairo_curve_to(cr,
88 x1 - y1 * u, y1 + x1 * u,
89 x2 + y2 * v, y2 - x2 * v,
Michael Vetter2a18a522015-05-15 17:17:47 +020090 x2, y2);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040091
Kristian Høgsberga234e702008-10-11 22:13:51 -040092 cairo_curve_to(cr,
93 x2 - y2 * v, y2 + x2 * v,
94 x3 + y3 * u, y3 - x3 * u,
95 x3, y3);
96 }
97
98 cairo_close_path(cr);
99 set_random_color(cr);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400100 cairo_fill_preserve(cr);
Kristian Høgsberga234e702008-10-11 22:13:51 -0400101 set_random_color(cr);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400102 cairo_stroke(cr);
103
104 cairo_destroy(cr);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400105}
106
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500107static void
Kristian Høgsberg8e054f72012-01-31 11:53:20 -0500108resize_handler(struct widget *widget,
109 int32_t width, int32_t height, void *data)
110{
111 struct flower *flower = data;
112
113 /* Dont resize me */
114 widget_set_size(flower->widget, flower->width, flower->height);
115}
116
117static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500118redraw_handler(struct widget *widget, void *data)
119{
120 struct flower *flower = data;
121 cairo_surface_t *surface;
122
123 surface = window_get_surface(flower->window);
124 if (surface == NULL ||
125 cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
126 fprintf(stderr, "failed to create cairo egl surface\n");
127 return;
128 }
129
130 draw_stuff(surface, flower->width, flower->height);
131 cairo_surface_destroy(surface);
132}
133
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500134static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500135button_handler(struct widget *widget,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500136 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100137 uint32_t button, enum wl_pointer_button_state state, void *data)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500138{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500139 struct flower *flower = data;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500140
Kristian Høgsbergc25a1d72012-01-31 09:54:04 -0500141 switch (button) {
142 case BTN_LEFT:
Daniel Stone4dbadb12012-05-30 16:31:51 +0100143 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400144 window_move(flower->window, input,
145 display_get_serial(flower->display));
Kristian Høgsbergc25a1d72012-01-31 09:54:04 -0500146 break;
147 case BTN_MIDDLE:
Daniel Stone4dbadb12012-05-30 16:31:51 +0100148 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergc25a1d72012-01-31 09:54:04 -0500149 widget_schedule_redraw(widget);
150 break;
151 case BTN_RIGHT:
Daniel Stone4dbadb12012-05-30 16:31:51 +0100152 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergc25a1d72012-01-31 09:54:04 -0500153 window_show_frame_menu(flower->window, input, time);
154 break;
155 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500156}
157
Rusty Lynch1084da52013-08-15 09:10:08 -0700158static void
Michael Vetter2a18a522015-05-15 17:17:47 +0200159touch_down_handler(struct widget *widget, struct input *input,
160 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch1084da52013-08-15 09:10:08 -0700161 float x, float y, void *data)
162{
163 struct flower *flower = data;
Jasper St. Pierre01eaaac2013-11-12 20:19:57 -0500164 window_move(flower->window, input, display_get_serial(flower->display));
Rusty Lynch1084da52013-08-15 09:10:08 -0700165}
166
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400167int main(int argc, char *argv[])
168{
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500169 struct flower flower;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400170 struct display *d;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500171 struct timeval tv;
Kristian Høgsberga234e702008-10-11 22:13:51 -0400172
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500173 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200174 if (d == NULL) {
175 fprintf(stderr, "failed to create display: %m\n");
176 return -1;
177 }
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400178
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500179 gettimeofday(&tv, NULL);
180 srandom(tv.tv_usec);
181
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500182 flower.width = 200;
183 flower.height = 200;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400184 flower.display = d;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500185 flower.window = window_create(d);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500186 flower.widget = window_add_widget(flower.window, &flower);
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600187 window_set_title(flower.window, "Flower");
Kristian Høgsbergd3fa34c2008-11-02 15:28:22 -0500188
Kristian Høgsberg8e054f72012-01-31 11:53:20 -0500189 widget_set_resize_handler(flower.widget, resize_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500190 widget_set_redraw_handler(flower.widget, redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500191 widget_set_button_handler(flower.widget, button_handler);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500192 widget_set_default_cursor(flower.widget, CURSOR_HAND1);
Rusty Lynch1084da52013-08-15 09:10:08 -0700193 widget_set_touch_down_handler(flower.widget, touch_down_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500194
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500195 window_schedule_resize(flower.window, flower.width, flower.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500196
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400197 display_run(d);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400198
vivek31732f72014-05-15 18:58:16 +0530199 widget_destroy(flower.widget);
200 window_destroy(flower.window);
201 display_destroy(d);
202
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400203 return 0;
204}