button-test: Use wl_display_roundtrip instead of yield()
[profile/ivi/weston.git] / clients / flower.c
index 5f3a85e..dac52d5 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
-#include <fcntl.h>
-#include <unistd.h>
 #include <math.h>
 #include <time.h>
 #include <cairo.h>
-#include <glib.h>
+#include <sys/time.h>
 
-#include "wayland-client.h"
-#include "wayland-glib.h"
+#include <linux/input.h>
+#include <wayland-client.h>
 #include "window.h"
 
-static const char gem_device[] = "/dev/dri/card0";
-static const char socket_name[] = "\0wayland";
+struct flower {
+       struct display *display;
+       struct window *window;
+       struct widget *widget;
+       int width, height;
+};
 
 static void
 set_random_color(cairo_t *cr)
@@ -65,6 +67,11 @@ draw_stuff(cairo_surface_t *surface, int width, int height)
        double x1, y1, x2, y2, x3, y3;
 
        cr = cairo_create(surface);
+       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);
        cairo_translate(cr, width / 2, height / 2);
        cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
        for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
@@ -95,80 +102,94 @@ draw_stuff(cairo_surface_t *surface, int width, int height)
        cairo_destroy(cr);
 }
 
-struct flower {
-       struct window *window;
-       int x, y, width, height;
-       int offset;
-};
+static void
+resize_handler(struct widget *widget,
+              int32_t width, int32_t height, void *data)
+{
+       struct flower *flower = data;
+
+       /* Dont resize me */
+       widget_set_size(flower->widget, flower->width, flower->height);
+}
 
 static void
-handle_frame(struct window *window,
-            uint32_t frame, uint32_t timestamp, void *data)
+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;
+       }
 
-       window_move(flower->window, 
-                   flower->x + cos((flower->offset + timestamp) / 400.0) * 400 - flower->width / 2,
-                   flower->y + sin((flower->offset + timestamp) / 320.0) * 300 - flower->height / 2);
-       window_commit(flower->window, 0);
+       draw_stuff(surface, flower->width, flower->height);
+       cairo_surface_destroy(surface);
 }
 
-int main(int argc, char *argv[])
+static int
+motion_handler(struct widget *widget, struct input *input,
+              uint32_t time, float x, float y, void *data)
 {
-       struct wl_display *display;
-       struct wl_visual *visual;
-       int fd;
-       cairo_surface_t *s;
-       struct timespec ts;
-       GMainLoop *loop;
-       GSource *source;
-       struct flower flower;
-       struct display *d;
+       return CURSOR_HAND1;
+}
 
-       fd = open(gem_device, O_RDWR);
-       if (fd < 0) {
-               fprintf(stderr, "drm open failed: %m\n");
-               return -1;
+static void
+button_handler(struct widget *widget,
+              struct input *input, uint32_t time,
+              uint32_t button, enum wl_pointer_button_state state, void *data)
+{
+       struct flower *flower = data;
+
+       switch (button) {
+       case BTN_LEFT:
+               if (state == WL_POINTER_BUTTON_STATE_PRESSED)
+                       window_move(flower->window, input,
+                                   display_get_serial(flower->display));
+               break;
+       case BTN_MIDDLE:
+               if (state == WL_POINTER_BUTTON_STATE_PRESSED)
+                       widget_schedule_redraw(widget);
+               break;
+       case BTN_RIGHT:
+               if (state == WL_POINTER_BUTTON_STATE_PRESSED)
+                       window_show_frame_menu(flower->window, input, time);
+               break;
        }
+}
 
-       loop = g_main_loop_new(NULL, FALSE);
+int main(int argc, char *argv[])
+{
+       struct flower flower;
+       struct display *d;
+       struct timeval tv;
 
-       display = wl_display_create(socket_name, sizeof socket_name);
-       if (display == NULL) {
+       d = display_create(argc, argv);
+       if (d == NULL) {
                fprintf(stderr, "failed to create display: %m\n");
                return -1;
        }
 
-       d = display_create(display, fd);
-
-       source = wl_glib_source_new(display);
-       g_source_attach(source, NULL);
+       gettimeofday(&tv, NULL);
+       srandom(tv.tv_usec);
 
-       flower.x = 512;
-       flower.y = 384;
        flower.width = 200;
        flower.height = 200;
-       flower.window = window_create(d, "flower", flower.x, flower.y,
-                                     flower.width, flower.height);
-
-       clock_gettime(CLOCK_MONOTONIC, &ts);
-       srandom(ts.tv_nsec);
-       flower.offset = random();
-
-       window_draw(flower.window);
-       s = window_get_surface(flower.window);
-       if (s == NULL || cairo_surface_status (s) != CAIRO_STATUS_SUCCESS) {
-               fprintf(stderr, "failed to create cairo drm surface\n");
-               return -1;
-       }
+       flower.display = d;
+       flower.window = window_create(d);
+       flower.widget = window_add_widget(flower.window, &flower);
+       window_set_title(flower.window, "Flower");
 
-       draw_stuff(s, flower.width, flower.height);
-       cairo_surface_flush(s);
+       widget_set_resize_handler(flower.widget, resize_handler);
+       widget_set_redraw_handler(flower.widget, redraw_handler);
+       widget_set_motion_handler(flower.widget, motion_handler);
+       widget_set_button_handler(flower.widget, button_handler);
 
-       window_set_frame_handler(flower.window, handle_frame, &flower);
-       window_commit(flower.window, 0);
+       window_schedule_resize(flower.window, flower.width, flower.height);
 
-       g_main_loop_run(loop);
+       display_run(d);
 
        return 0;
 }