2010-12-16 21:35:23 +03:00
|
|
|
/*
|
|
|
|
* Copyright © 2010 Intel Corporation
|
|
|
|
*
|
|
|
|
* 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 <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <cairo.h>
|
2010-12-16 22:56:41 +03:00
|
|
|
#include <math.h>
|
2011-12-15 17:26:29 +04:00
|
|
|
#include <assert.h>
|
2010-12-16 21:35:23 +03:00
|
|
|
|
2011-12-20 00:36:10 +04:00
|
|
|
#include <linux/input.h>
|
2011-11-22 16:18:50 +04:00
|
|
|
#include <wayland-client.h>
|
2010-12-16 21:35:23 +03:00
|
|
|
|
|
|
|
#include "window.h"
|
|
|
|
|
|
|
|
#include <X11/keysym.h>
|
|
|
|
|
|
|
|
struct resizor {
|
|
|
|
struct display *display;
|
|
|
|
struct window *window;
|
2012-01-10 18:43:58 +04:00
|
|
|
struct widget *widget;
|
2011-01-22 02:03:15 +03:00
|
|
|
struct window *menu;
|
2011-01-11 18:00:52 +03:00
|
|
|
int32_t width;
|
2010-12-16 21:35:23 +03:00
|
|
|
|
|
|
|
struct {
|
|
|
|
double current;
|
|
|
|
double target;
|
|
|
|
double previous;
|
|
|
|
} height;
|
2011-12-15 17:26:29 +04:00
|
|
|
struct wl_callback *frame_callback;
|
2010-12-16 21:35:23 +03:00
|
|
|
};
|
|
|
|
|
2010-12-17 05:11:10 +03:00
|
|
|
static void
|
2011-08-24 02:14:06 +04:00
|
|
|
frame_callback(void *data, struct wl_callback *callback, uint32_t time)
|
2010-12-17 05:11:10 +03:00
|
|
|
{
|
|
|
|
struct resizor *resizor = data;
|
|
|
|
double force, height;
|
|
|
|
|
2011-12-15 17:26:29 +04:00
|
|
|
assert(!callback || callback == resizor->frame_callback);
|
|
|
|
|
2010-12-17 05:11:10 +03:00
|
|
|
height = resizor->height.current;
|
|
|
|
force = (resizor->height.target - height) / 10.0 +
|
|
|
|
(resizor->height.previous - height);
|
|
|
|
|
|
|
|
resizor->height.current =
|
|
|
|
height + (height - resizor->height.previous) + force;
|
|
|
|
resizor->height.previous = height;
|
|
|
|
|
|
|
|
if (resizor->height.current >= 400) {
|
|
|
|
resizor->height.current = 400;
|
|
|
|
resizor->height.previous = 400;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resizor->height.current <= 200) {
|
|
|
|
resizor->height.current = 200;
|
|
|
|
resizor->height.previous = 200;
|
|
|
|
}
|
|
|
|
|
2012-01-11 04:11:42 +04:00
|
|
|
widget_schedule_resize(resizor->widget, resizor->width, height + 0.5);
|
2011-08-24 02:14:06 +04:00
|
|
|
|
2011-12-15 17:26:29 +04:00
|
|
|
if (resizor->frame_callback) {
|
|
|
|
wl_callback_destroy(resizor->frame_callback);
|
|
|
|
resizor->frame_callback = NULL;
|
|
|
|
}
|
2010-12-17 05:11:10 +03:00
|
|
|
}
|
|
|
|
|
2012-01-11 04:11:42 +04:00
|
|
|
static const struct wl_callback_listener listener = {
|
|
|
|
frame_callback
|
|
|
|
};
|
|
|
|
|
2010-12-16 21:35:23 +03:00
|
|
|
static void
|
2012-01-10 21:23:19 +04:00
|
|
|
redraw_handler(struct widget *widget, void *data)
|
2010-12-16 21:35:23 +03:00
|
|
|
{
|
2012-01-10 21:23:19 +04:00
|
|
|
struct resizor *resizor = data;
|
2010-12-16 21:35:23 +03:00
|
|
|
cairo_surface_t *surface;
|
|
|
|
cairo_t *cr;
|
2011-01-11 18:00:52 +03:00
|
|
|
struct rectangle allocation;
|
2010-12-16 21:35:23 +03:00
|
|
|
|
2012-01-11 04:11:42 +04:00
|
|
|
widget_get_allocation(resizor->widget, &allocation);
|
2010-12-16 21:35:23 +03:00
|
|
|
|
|
|
|
surface = window_get_surface(resizor->window);
|
|
|
|
|
|
|
|
cr = cairo_create(surface);
|
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
|
|
|
|
cairo_rectangle(cr,
|
2011-01-11 18:00:52 +03:00
|
|
|
allocation.x,
|
|
|
|
allocation.y,
|
|
|
|
allocation.width,
|
|
|
|
allocation.height);
|
2010-12-16 21:35:23 +03:00
|
|
|
cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
|
|
|
|
cairo_fill(cr);
|
|
|
|
cairo_destroy(cr);
|
|
|
|
|
|
|
|
cairo_surface_destroy(surface);
|
2012-01-11 04:11:42 +04:00
|
|
|
|
|
|
|
if (fabs(resizor->height.previous - resizor->height.target) > 0.1) {
|
|
|
|
resizor->frame_callback =
|
|
|
|
wl_surface_frame(
|
|
|
|
window_get_wl_surface(resizor->window));
|
|
|
|
wl_callback_add_listener(resizor->frame_callback, &listener,
|
|
|
|
resizor);
|
|
|
|
}
|
|
|
|
|
2010-12-16 21:35:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
keyboard_focus_handler(struct window *window,
|
|
|
|
struct input *device, void *data)
|
|
|
|
{
|
|
|
|
struct resizor *resizor = data;
|
|
|
|
|
|
|
|
window_schedule_redraw(resizor->window);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-01-19 22:20:33 +03:00
|
|
|
key_handler(struct window *window, struct input *input, uint32_t time,
|
|
|
|
uint32_t key, uint32_t sym, uint32_t state, void *data)
|
2010-12-16 21:35:23 +03:00
|
|
|
{
|
|
|
|
struct resizor *resizor = data;
|
|
|
|
|
|
|
|
if (state == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (sym) {
|
2010-12-17 05:11:10 +03:00
|
|
|
case XK_Down:
|
2010-12-16 21:35:23 +03:00
|
|
|
resizor->height.target = 400;
|
2011-08-24 02:14:06 +04:00
|
|
|
frame_callback(resizor, NULL, 0);
|
2010-12-16 21:35:23 +03:00
|
|
|
break;
|
2010-12-17 05:11:10 +03:00
|
|
|
case XK_Up:
|
2010-12-16 21:35:23 +03:00
|
|
|
resizor->height.target = 200;
|
2011-08-24 02:14:06 +04:00
|
|
|
frame_callback(resizor, NULL, 0);
|
2010-12-16 21:35:23 +03:00
|
|
|
break;
|
2011-12-15 17:26:29 +04:00
|
|
|
case XK_Escape:
|
|
|
|
display_exit(resizor->display);
|
|
|
|
break;
|
2010-12-16 21:35:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-07 06:59:05 +04:00
|
|
|
static void
|
|
|
|
menu_func(struct window *window, int index, void *user_data)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "picked entry %d\n", index);
|
|
|
|
}
|
|
|
|
|
2011-01-22 02:03:15 +03:00
|
|
|
static void
|
2012-01-05 07:19:14 +04:00
|
|
|
show_menu(struct resizor *resizor, struct input *input, uint32_t time)
|
2011-01-22 02:03:15 +03:00
|
|
|
{
|
2011-12-20 00:40:10 +04:00
|
|
|
int32_t x, y;
|
|
|
|
static const char *entries[] = {
|
|
|
|
"Roy", "Pris", "Leon", "Zhora"
|
|
|
|
};
|
2011-01-22 02:03:15 +03:00
|
|
|
|
|
|
|
input_get_position(input, &x, &y);
|
2012-01-19 17:17:59 +04:00
|
|
|
window_show_menu(resizor->display, input, time, resizor->window,
|
|
|
|
x - 10, y - 10, menu_func, entries, 4);
|
2011-01-22 02:03:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-01-09 20:12:05 +04:00
|
|
|
button_handler(struct widget *widget,
|
2011-01-22 02:03:15 +03:00
|
|
|
struct input *input, uint32_t time,
|
|
|
|
int button, int state, void *data)
|
|
|
|
{
|
2012-01-10 18:43:58 +04:00
|
|
|
struct resizor *resizor = data;
|
2011-01-22 02:03:15 +03:00
|
|
|
|
|
|
|
switch (button) {
|
2011-12-20 00:36:10 +04:00
|
|
|
case BTN_RIGHT:
|
2011-01-22 02:03:15 +03:00
|
|
|
if (state)
|
2012-01-05 07:19:14 +04:00
|
|
|
show_menu(resizor, input, time);
|
2011-01-22 02:03:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-16 21:35:23 +03:00
|
|
|
static struct resizor *
|
|
|
|
resizor_create(struct display *display)
|
|
|
|
{
|
|
|
|
struct resizor *resizor;
|
2011-01-11 18:00:52 +03:00
|
|
|
int32_t height;
|
2010-12-16 21:35:23 +03:00
|
|
|
|
|
|
|
resizor = malloc(sizeof *resizor);
|
|
|
|
if (resizor == NULL)
|
|
|
|
return resizor;
|
|
|
|
memset(resizor, 0, sizeof *resizor);
|
|
|
|
|
2012-02-01 00:24:48 +04:00
|
|
|
resizor->window = window_create(display);
|
2012-01-11 07:41:05 +04:00
|
|
|
resizor->widget = frame_create(resizor->window, resizor);
|
2011-01-22 02:03:15 +03:00
|
|
|
window_set_title(resizor->window, "Wayland Resizor");
|
2010-12-16 21:35:23 +03:00
|
|
|
resizor->display = display;
|
|
|
|
|
|
|
|
window_set_key_handler(resizor->window, key_handler);
|
|
|
|
window_set_user_data(resizor->window, resizor);
|
2012-01-10 21:23:19 +04:00
|
|
|
widget_set_redraw_handler(resizor->widget, redraw_handler);
|
2010-12-16 21:35:23 +03:00
|
|
|
window_set_keyboard_focus_handler(resizor->window,
|
|
|
|
keyboard_focus_handler);
|
|
|
|
|
2011-01-11 18:00:52 +03:00
|
|
|
resizor->width = 300;
|
2010-12-16 21:35:23 +03:00
|
|
|
resizor->height.current = 400;
|
2011-01-11 18:00:52 +03:00
|
|
|
resizor->height.previous = resizor->height.current;
|
|
|
|
resizor->height.target = resizor->height.current;
|
|
|
|
height = resizor->height.current + 0.5;
|
2010-12-16 21:35:23 +03:00
|
|
|
|
2012-01-10 18:43:58 +04:00
|
|
|
widget_set_button_handler(resizor->widget, button_handler);
|
2010-12-16 21:35:23 +03:00
|
|
|
|
2012-01-11 04:11:42 +04:00
|
|
|
widget_schedule_resize(resizor->widget, resizor->width, height);
|
2010-12-16 21:35:23 +03:00
|
|
|
|
|
|
|
return resizor;
|
|
|
|
}
|
|
|
|
|
2011-12-15 17:26:29 +04:00
|
|
|
static void
|
|
|
|
resizor_destroy(struct resizor *resizor)
|
|
|
|
{
|
|
|
|
if (resizor->frame_callback)
|
|
|
|
wl_callback_destroy(resizor->frame_callback);
|
|
|
|
|
2012-01-19 16:21:35 +04:00
|
|
|
widget_destroy(resizor->widget);
|
2011-12-15 17:26:29 +04:00
|
|
|
window_destroy(resizor->window);
|
|
|
|
free(resizor);
|
|
|
|
}
|
|
|
|
|
2010-12-16 21:35:23 +03:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2011-12-15 17:26:29 +04:00
|
|
|
struct display *display;
|
|
|
|
struct resizor *resizor;
|
2010-12-16 21:35:23 +03:00
|
|
|
|
2011-12-15 17:26:29 +04:00
|
|
|
display = display_create(&argc, &argv, NULL);
|
|
|
|
if (display == NULL) {
|
2010-12-16 21:35:23 +03:00
|
|
|
fprintf(stderr, "failed to create display: %m\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-12-15 17:26:29 +04:00
|
|
|
resizor = resizor_create(display);
|
|
|
|
|
|
|
|
display_run(display);
|
2010-12-16 21:35:23 +03:00
|
|
|
|
2011-12-15 17:26:29 +04:00
|
|
|
resizor_destroy(resizor);
|
|
|
|
display_destroy(display);
|
2010-12-16 21:35:23 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|