Signed-off-by: Jonas Ådahl <jad...@gmail.com>
---
 clients/Makefile.am |    4 ++
 clients/draw.c      |  197 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 201 insertions(+)
 create mode 100644 clients/draw.c

diff --git a/clients/Makefile.am b/clients/Makefile.am
index 2377189..bb3954b 100644
--- a/clients/Makefile.am
+++ b/clients/Makefile.am
@@ -47,6 +47,7 @@ clients_programs =                            \
        resizor                                 \
        eventdemo                               \
        clickdot                                \
+       draw                                    \
        $(full_gl_client_programs)
 
 desktop_shell = weston-desktop-shell
@@ -93,6 +94,9 @@ eventdemo_LDADD = $(toolkit_libs)
 clickdot_SOURCES = clickdot.c
 clickdot_LDADD = $(toolkit_libs)
 
+draw_SOURCES = draw.c
+draw_LDADD = $(toolkit_libs)
+
 weston_desktop_shell_SOURCES =                 \
        desktop-shell.c                         \
        desktop-shell-client-protocol.h         \
diff --git a/clients/draw.c b/clients/draw.c
new file mode 100644
index 0000000..544b604
--- /dev/null
+++ b/clients/draw.c
@@ -0,0 +1,197 @@
+/*
+ * Copyright © 2012 Jonas Ådahl
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation, and that the name of the copyright holders not be used in
+ * advertising or publicity pertaining to distribution of the software
+ * without specific, written prior permission.  The copyright holders make
+ * no representations about the suitability of this software for any
+ * purpose.  It is provided "as is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+ * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "window.h"
+#include "cairo-util.h"
+
+struct draw {
+       struct window *window;
+       struct widget *widget;
+       struct display *display;
+
+       cairo_surface_t *buffer;
+
+       int width, height;
+       int x, y, old_x, old_y;
+};
+
+static void
+redraw_handler(struct widget *widget, void *data)
+{
+       struct draw *draw = data;
+       struct rectangle allocation;
+       cairo_surface_t *surface;
+       cairo_t *cairo;
+       cairo_t *buffer_context;
+       
+       widget_get_allocation(draw->widget, &allocation);
+
+       surface = window_get_surface(draw->window);
+       cairo = cairo_create(surface);
+       cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
+
+       if (draw->buffer == NULL) {
+               /* Initiate buffer */
+               draw->buffer =
+                       cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
+                                                  allocation.width,
+                                                  allocation.height);
+               buffer_context = cairo_create(draw->buffer);
+
+               cairo_set_source_rgb(buffer_context, 1, 1, 1);
+               cairo_rectangle(buffer_context,
+                               0, 0,
+                               allocation.width, allocation.height);
+               cairo_fill(buffer_context);
+       }
+       else
+               buffer_context = cairo_create(draw->buffer);
+
+       /* Paint buffer */
+       cairo_set_source_surface(cairo, draw->buffer,
+                                allocation.x, allocation.y);
+
+       cairo_rectangle(cairo,
+                       allocation.x, allocation.y,
+                       allocation.width, allocation.height);
+       cairo_clip(cairo);
+       cairo_paint(cairo);
+       cairo_reset_clip(cairo);
+
+       /* Draw line between current pointer position and last */
+       if (draw->x != -1 && draw->y != -1) {
+               if (draw->old_x != -1 && draw->old_y != -1) {
+                       cairo_set_line_width(cairo, 2.0);
+                       cairo_set_source_rgb(cairo, 0, 0, 0);
+
+                       cairo_move_to(cairo, draw->old_x, draw->old_y);
+                       cairo_line_to(cairo, draw->x, draw->y);
+                       cairo_stroke(cairo);
+               }
+
+               draw->old_x = draw->x;
+               draw->old_y = draw->y;
+       }
+
+       /* Copy new content to buffer */
+       buffer_context = cairo_create(draw->buffer);
+       cairo_set_source_surface(buffer_context, surface,
+                                -allocation.x, -allocation.y);
+       cairo_paint(buffer_context);
+       cairo_destroy(buffer_context);
+
+       cairo_destroy(cairo);
+       cairo_surface_destroy(surface);
+}
+
+static int
+motion_handler(struct widget *widget,
+              struct input *input, uint32_t time,
+              GLfloat x, GLfloat y, void *data)
+{
+       struct draw *draw = data;
+       draw->x = x;
+       draw->y = y;
+
+       window_schedule_redraw(draw->window);
+
+       return POINTER_LEFT_PTR;
+}
+
+static void
+keyboard_focus_handler(struct window *window,
+                      struct input *device, void *data)
+{
+       struct draw *draw = data;
+
+       window_schedule_redraw(draw->window);
+}
+
+static struct draw *
+create_draw(struct display *display)
+{
+       struct draw *draw;
+
+       draw = malloc(sizeof *draw);
+       if (draw == NULL)
+               return NULL;
+
+       draw->width = 1000;
+       draw->height = 600;
+       draw->x = -1;
+       draw->y = -1;
+       draw->old_x = -1;
+       draw->old_y = -1;
+
+       draw->display = display;
+       draw->window = window_create(display);
+       draw->widget = frame_create(draw->window, draw);
+       draw->buffer = NULL;
+
+       widget_set_redraw_handler(draw->widget, redraw_handler);
+
+       window_set_title(draw->window, "Wayland Draw");
+       window_set_user_data(draw->window, draw);
+       window_set_keyboard_focus_handler(draw->window,
+                                         keyboard_focus_handler);
+
+       widget_set_motion_handler(draw->widget, motion_handler);
+       widget_schedule_resize(draw->widget, draw->width, draw->height);
+
+       return draw;
+}
+
+static void destroy_draw(struct draw *draw)
+{
+       if (draw->buffer)
+               cairo_surface_destroy(draw->buffer);
+       widget_destroy(draw->widget);
+       window_destroy(draw->window);
+       free(draw);
+}
+
+int main(int argc, char *argv[])
+{
+       struct display *display;
+       struct draw *draw;
+
+       display = display_create(argc, argv);
+       if (display == NULL) {
+               fprintf(stderr, "failed to create display; %m\n");
+               return EXIT_FAILURE;
+       }
+
+       draw = create_draw(display);
+       if (draw == NULL) {
+               fprintf(stderr, "failed to create window\n");
+               return EXIT_FAILURE;
+       }
+
+       display_run(display);
+
+       destroy_draw(draw);
+                
+       return EXIT_SUCCESS;
+}
-- 
1.7.9.5

_______________________________________________
wayland-devel mailing list
wayland-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/wayland-devel

Reply via email to