clickdot: implement the purpose
authorPekka Paalanen <ppaalanen@gmail.com>
Fri, 20 Jan 2012 11:04:56 +0000 (13:04 +0200)
committerPekka Paalanen <ppaalanen@gmail.com>
Fri, 27 Jan 2012 08:44:22 +0000 (10:44 +0200)
Remove all unneeded resizor features, and add the feature why clickdot
exists: put a visible marker to exactly where mouse was clicked.

This app can be used to check input coordinate transformations in a
compositor.

Signed-off-by: Pekka Paalanen <ppaalanen@gmail.com>
clients/clickdot.c

index a707037..71bc091 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright © 2010 Intel Corporation
+ * Copyright © 2012 Collabora, Ltd.
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
@@ -39,58 +40,14 @@ struct clickdot {
        struct display *display;
        struct window *window;
        struct widget *widget;
-       struct window *menu;
-       int32_t width;
-
-       struct {
-               double current;
-               double target;
-               double previous;
-       } height;
-       struct wl_callback *frame_callback;
-};
-
-static void
-frame_callback(void *data, struct wl_callback *callback, uint32_t time)
-{
-       struct clickdot *clickdot = data;
-       double force, height;
-
-       assert(!callback || callback == clickdot->frame_callback);
 
-       height = clickdot->height.current;
-       force = (clickdot->height.target - height) / 10.0 +
-               (clickdot->height.previous - height);
-
-       clickdot->height.current =
-               height + (height - clickdot->height.previous) + force;
-       clickdot->height.previous = height;
-
-       if (clickdot->height.current >= 400) {
-               clickdot->height.current = 400;
-               clickdot->height.previous = 400;
-       }
-
-       if (clickdot->height.current <= 200) {
-               clickdot->height.current = 200;
-               clickdot->height.previous = 200;
-       }
-
-       widget_schedule_resize(clickdot->widget, clickdot->width, height + 0.5);
-
-       if (clickdot->frame_callback) {
-               wl_callback_destroy(clickdot->frame_callback);
-               clickdot->frame_callback = NULL;
-       }
-}
-
-static const struct wl_callback_listener listener = {
-       frame_callback
+       int32_t x, y;
 };
 
 static void
 redraw_handler(struct widget *widget, void *data)
 {
+       static const double r = 10.0;
        struct clickdot *clickdot = data;
        cairo_surface_t *surface;
        cairo_t *cr;
@@ -109,18 +66,20 @@ redraw_handler(struct widget *widget, void *data)
                        allocation.height);
        cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
        cairo_fill(cr);
-       cairo_destroy(cr);
 
-       cairo_surface_destroy(surface);
+       cairo_translate(cr, clickdot->x + 0.5, clickdot->y + 0.5);
+       cairo_set_line_width(cr, 1.0);
+       cairo_set_source_rgb(cr, 0.1, 0.9, 0.9);
+       cairo_move_to(cr, 0.0, -r);
+       cairo_line_to(cr, 0.0, r);
+       cairo_move_to(cr, -r, 0.0);
+       cairo_line_to(cr, r, 0.0);
+       cairo_arc(cr, 0.0, 0.0, r, 0.0, 2.0 * M_PI);
+       cairo_stroke(cr);
 
-       if (fabs(clickdot->height.previous - clickdot->height.target) > 0.1) {
-               clickdot->frame_callback =
-                       wl_surface_frame(
-                               window_get_wl_surface(clickdot->window));
-               wl_callback_add_listener(clickdot->frame_callback, &listener,
-                                        clickdot);
-       }
+       cairo_destroy(cr);
 
+       cairo_surface_destroy(surface);
 }
 
 static void
@@ -142,14 +101,6 @@ key_handler(struct window *window, struct input *input, uint32_t time,
                return;
 
        switch (sym) {
-       case XK_Down:
-               clickdot->height.target = 400;
-               frame_callback(clickdot, NULL, 0);
-               break;
-       case XK_Up:
-               clickdot->height.target = 200;
-               frame_callback(clickdot, NULL, 0);
-               break;
        case XK_Escape:
                display_exit(clickdot->display);
                break;
@@ -157,44 +108,22 @@ key_handler(struct window *window, struct input *input, uint32_t time,
 }
 
 static void
-menu_func(struct window *window, int index, void *user_data)
-{
-       fprintf(stderr, "picked entry %d\n", index);
-}
-
-static void
-show_menu(struct clickdot *clickdot, struct input *input, uint32_t time)
-{
-       int32_t x, y;
-       static const char *entries[] = {
-               "Roy", "Pris", "Leon", "Zhora"
-       };
-
-       input_get_position(input, &x, &y);
-       window_show_menu(clickdot->display, input, time, clickdot->window,
-                        x - 10, y - 10, menu_func, entries, 4);
-}
-
-static void
 button_handler(struct widget *widget,
               struct input *input, uint32_t time,
               int button, int state, void *data)
 {
        struct clickdot *clickdot = data;
 
-       switch (button) {
-       case BTN_RIGHT:
-               if (state)
-                       show_menu(clickdot, input, time);
-               break;
-       }
+       if (state && button == BTN_LEFT)
+               input_get_position(input, &clickdot->x, &clickdot->y);
+
+       widget_schedule_redraw(widget);
 }
 
 static struct clickdot *
 clickdot_create(struct display *display)
 {
        struct clickdot *clickdot;
-       int32_t height;
 
        clickdot = malloc(sizeof *clickdot);
        if (clickdot == NULL)
@@ -203,24 +132,20 @@ clickdot_create(struct display *display)
 
        clickdot->window = window_create(display, 500, 400);
        clickdot->widget = frame_create(clickdot->window, clickdot);
-       window_set_title(clickdot->window, "Wayland Resizor");
+       window_set_title(clickdot->window, "Wayland ClickDot");
        clickdot->display = display;
 
        window_set_key_handler(clickdot->window, key_handler);
        window_set_user_data(clickdot->window, clickdot);
-       widget_set_redraw_handler(clickdot->widget, redraw_handler);
        window_set_keyboard_focus_handler(clickdot->window,
                                          keyboard_focus_handler);
 
-       clickdot->width = 300;
-       clickdot->height.current = 400;
-       clickdot->height.previous = clickdot->height.current;
-       clickdot->height.target = clickdot->height.current;
-       height = clickdot->height.current + 0.5;
-
+       widget_set_redraw_handler(clickdot->widget, redraw_handler);
        widget_set_button_handler(clickdot->widget, button_handler);
 
-       widget_schedule_resize(clickdot->widget, clickdot->width, height);
+       widget_schedule_resize(clickdot->widget, 500, 400);
+       clickdot->x = 250;
+       clickdot->y = 200;
 
        return clickdot;
 }
@@ -228,9 +153,6 @@ clickdot_create(struct display *display)
 static void
 clickdot_destroy(struct clickdot *clickdot)
 {
-       if (clickdot->frame_callback)
-               wl_callback_destroy(clickdot->frame_callback);
-
        widget_destroy(clickdot->widget);
        window_destroy(clickdot->window);
        free(clickdot);