resizor: add exit key and cleanups

Make resizor quit when you press Esc key.

Call the toytoolkit cleanup functions, properly destroy window, display
and frame callback.

Signed-off-by: Pekka Paalanen <ppaalanen@gmail.com>
This commit is contained in:
Pekka Paalanen 2011-12-15 15:26:29 +02:00
parent fe6079ac09
commit e59d74a9b9

View File

@ -26,6 +26,7 @@
#include <string.h> #include <string.h>
#include <cairo.h> #include <cairo.h>
#include <math.h> #include <math.h>
#include <assert.h>
#include <linux/input.h> #include <linux/input.h>
#include <wayland-client.h> #include <wayland-client.h>
@ -45,6 +46,7 @@ struct resizor {
double target; double target;
double previous; double previous;
} height; } height;
struct wl_callback *frame_callback;
}; };
static void static void
@ -56,6 +58,8 @@ frame_callback(void *data, struct wl_callback *callback, uint32_t time)
struct resizor *resizor = data; struct resizor *resizor = data;
double force, height; double force, height;
assert(!callback || callback == resizor->frame_callback);
height = resizor->height.current; height = resizor->height.current;
force = (resizor->height.target - height) / 10.0 + force = (resizor->height.target - height) / 10.0 +
(resizor->height.previous - height); (resizor->height.previous - height);
@ -78,12 +82,17 @@ frame_callback(void *data, struct wl_callback *callback, uint32_t time)
window_schedule_redraw(resizor->window); window_schedule_redraw(resizor->window);
if (callback) if (resizor->frame_callback) {
wl_callback_destroy(callback); wl_callback_destroy(resizor->frame_callback);
resizor->frame_callback = NULL;
}
if (fabs(resizor->height.previous - resizor->height.target) > 0.1) { if (fabs(resizor->height.previous - resizor->height.target) > 0.1) {
callback = wl_surface_frame(window_get_wl_surface(resizor->window)); resizor->frame_callback =
wl_callback_add_listener(callback, &listener, resizor); wl_surface_frame(
window_get_wl_surface(resizor->window));
wl_callback_add_listener(resizor->frame_callback, &listener,
resizor);
} }
} }
@ -151,6 +160,9 @@ key_handler(struct window *window, struct input *input, uint32_t time,
resizor->height.target = 200; resizor->height.target = 200;
frame_callback(resizor, NULL, 0); frame_callback(resizor, NULL, 0);
break; break;
case XK_Escape:
display_exit(resizor->display);
break;
} }
} }
@ -220,20 +232,34 @@ resizor_create(struct display *display)
return resizor; return resizor;
} }
static void
resizor_destroy(struct resizor *resizor)
{
if (resizor->frame_callback)
wl_callback_destroy(resizor->frame_callback);
window_destroy(resizor->window);
free(resizor);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct display *d; struct display *display;
struct resizor *resizor;
d = display_create(&argc, &argv, NULL); display = display_create(&argc, &argv, NULL);
if (d == NULL) { if (display == NULL) {
fprintf(stderr, "failed to create display: %m\n"); fprintf(stderr, "failed to create display: %m\n");
return -1; return -1;
} }
resizor_create (d); resizor = resizor_create(display);
display_run(d); display_run(display);
resizor_destroy(resizor);
display_destroy(display);
return 0; return 0;
} }