2013-11-25 22:01:34 +04:00
|
|
|
/*
|
|
|
|
* Copyright © 2013 Collabora Ltd.
|
|
|
|
*
|
2015-06-11 08:48:59 +03:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
2013-11-25 22:01:34 +04:00
|
|
|
*
|
2015-06-11 08:48:59 +03:00
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
2013-11-25 22:01:34 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
2016-07-19 14:16:27 +03:00
|
|
|
#include <stdint.h>
|
2013-11-25 22:01:34 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
2019-04-27 00:57:31 +03:00
|
|
|
#include <errno.h>
|
2013-11-25 22:01:34 +04:00
|
|
|
|
|
|
|
#include <linux/input.h>
|
|
|
|
#include <cairo.h>
|
2016-09-29 01:18:10 +03:00
|
|
|
#include <wayland-util.h>
|
2013-11-25 22:01:34 +04:00
|
|
|
|
2023-10-26 20:51:31 +03:00
|
|
|
#include "shared/xalloc.h"
|
2015-06-16 01:37:08 +03:00
|
|
|
#include "shared/helpers.h"
|
2013-11-25 22:01:34 +04:00
|
|
|
#include "window.h"
|
|
|
|
|
2023-10-26 20:51:31 +03:00
|
|
|
struct stacking_window {
|
|
|
|
struct stacking *stacking;
|
|
|
|
struct window *window;
|
|
|
|
struct widget *widget;
|
|
|
|
struct wl_list link;
|
|
|
|
};
|
|
|
|
|
2013-11-25 22:01:34 +04:00
|
|
|
struct stacking {
|
|
|
|
struct display *display;
|
2023-10-26 20:51:31 +03:00
|
|
|
struct wl_list windows;
|
2013-11-25 22:01:34 +04:00
|
|
|
};
|
|
|
|
|
2023-10-26 20:51:31 +03:00
|
|
|
static void
|
|
|
|
close_handler(void *data);
|
2013-11-25 22:01:34 +04:00
|
|
|
static void
|
|
|
|
button_handler(struct widget *widget,
|
|
|
|
struct input *input, uint32_t time,
|
|
|
|
uint32_t button,
|
|
|
|
enum wl_pointer_button_state state, void *data);
|
|
|
|
static void
|
|
|
|
key_handler(struct window *window,
|
|
|
|
struct input *input, uint32_t time,
|
|
|
|
uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
|
|
|
|
void *data);
|
|
|
|
static void
|
2013-12-06 03:14:45 +04:00
|
|
|
keyboard_focus_handler(struct window *window,
|
|
|
|
struct input *device, void *data);
|
|
|
|
static void
|
2013-11-25 22:01:34 +04:00
|
|
|
fullscreen_handler(struct window *window, void *data);
|
|
|
|
static void
|
|
|
|
redraw_handler(struct widget *widget, void *data);
|
|
|
|
|
2013-12-10 00:20:16 +04:00
|
|
|
/* Iff parent_window is set, the new window will be transient. */
|
2013-11-25 22:01:34 +04:00
|
|
|
static struct window *
|
2023-10-26 20:51:31 +03:00
|
|
|
create_window(struct stacking *stacking, struct window *parent_window)
|
2013-11-25 22:01:34 +04:00
|
|
|
{
|
2023-10-26 20:51:31 +03:00
|
|
|
struct stacking_window *new_stacking_window;
|
2013-11-25 22:01:34 +04:00
|
|
|
struct window *new_window;
|
|
|
|
struct widget *new_widget;
|
|
|
|
|
2023-10-26 20:51:31 +03:00
|
|
|
new_stacking_window = xzalloc(sizeof *new_stacking_window);
|
|
|
|
new_stacking_window->stacking = stacking;
|
2013-12-09 23:52:33 +04:00
|
|
|
new_window = window_create(stacking->display);
|
2023-10-26 20:51:31 +03:00
|
|
|
new_stacking_window->window = new_window;
|
2014-04-11 05:37:54 +04:00
|
|
|
window_set_parent(new_window, parent_window);
|
2013-12-10 00:20:16 +04:00
|
|
|
|
2013-11-25 22:01:34 +04:00
|
|
|
new_widget = window_frame_create(new_window, new_window);
|
2023-10-26 20:51:31 +03:00
|
|
|
new_stacking_window->widget = new_widget;
|
|
|
|
|
|
|
|
wl_list_insert(stacking->windows.prev, &new_stacking_window->link);
|
2013-11-25 22:01:34 +04:00
|
|
|
|
|
|
|
window_set_title(new_window, "Stacking Test");
|
clients/window: Add functions to set/retrieve app_id
Adds appid for all clients using the toolkit, flower, fullscreen, image,
resizor, scaler, smoke, stacking, subsurfaces, terminal,
touch-calibrator, transformed, etc.
Signed-off-by: Marius Vlad <marius.vlad@collabora.com>
2021-09-09 13:52:18 +03:00
|
|
|
window_set_appid(new_window, "org.freedesktop.weston.stacking-test");
|
2013-11-25 22:01:34 +04:00
|
|
|
window_set_key_handler(new_window, key_handler);
|
2013-12-06 03:14:45 +04:00
|
|
|
window_set_keyboard_focus_handler(new_window, keyboard_focus_handler);
|
2013-11-25 22:01:34 +04:00
|
|
|
window_set_fullscreen_handler(new_window, fullscreen_handler);
|
2023-10-26 20:51:31 +03:00
|
|
|
window_set_close_handler(new_window, close_handler);
|
2013-11-25 22:01:34 +04:00
|
|
|
widget_set_button_handler(new_widget, button_handler);
|
|
|
|
widget_set_redraw_handler(new_widget, redraw_handler);
|
2023-10-26 20:51:31 +03:00
|
|
|
window_set_user_data(new_window, new_stacking_window);
|
2013-11-25 22:01:34 +04:00
|
|
|
|
|
|
|
window_schedule_resize(new_window, 300, 300);
|
|
|
|
|
|
|
|
return new_window;
|
|
|
|
}
|
|
|
|
|
2023-10-26 20:51:31 +03:00
|
|
|
static void
|
|
|
|
destroy_window(struct stacking_window *stacking_window)
|
|
|
|
{
|
|
|
|
struct stacking *stacking = stacking_window->stacking;
|
|
|
|
|
|
|
|
widget_destroy(stacking_window->widget);
|
|
|
|
window_destroy(stacking_window->window);
|
|
|
|
|
|
|
|
wl_list_remove(&stacking_window->link);
|
|
|
|
free(stacking_window);
|
|
|
|
|
|
|
|
if (wl_list_empty(&stacking->windows))
|
|
|
|
display_exit(stacking->display);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-25 22:01:34 +04:00
|
|
|
static void
|
2014-03-13 20:06:00 +04:00
|
|
|
show_popup_cb(void *data, struct input *input, int index)
|
2013-11-25 22:01:34 +04:00
|
|
|
{
|
|
|
|
/* Ignore the selected menu item. */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
show_popup(struct stacking *stacking, struct input *input, uint32_t time,
|
|
|
|
struct window *window)
|
|
|
|
{
|
|
|
|
int32_t x, y;
|
|
|
|
static const char *entries[] = {
|
|
|
|
"Test Entry",
|
|
|
|
"Another Test Entry",
|
|
|
|
};
|
|
|
|
|
|
|
|
input_get_position(input, &x, &y);
|
|
|
|
window_show_menu(stacking->display, input, time, window, x, y,
|
|
|
|
show_popup_cb, entries, ARRAY_LENGTH(entries));
|
|
|
|
}
|
|
|
|
|
2023-10-26 20:51:31 +03:00
|
|
|
static void
|
|
|
|
close_handler(void *data)
|
|
|
|
{
|
|
|
|
struct stacking_window *stacking_window = data;
|
|
|
|
|
|
|
|
destroy_window(stacking_window);
|
|
|
|
}
|
|
|
|
|
2013-11-25 22:01:34 +04:00
|
|
|
static void
|
|
|
|
button_handler(struct widget *widget,
|
|
|
|
struct input *input, uint32_t time,
|
|
|
|
uint32_t button,
|
|
|
|
enum wl_pointer_button_state state, void *data)
|
|
|
|
{
|
2023-10-26 20:51:31 +03:00
|
|
|
struct stacking_window *stacking_window = data;
|
2013-11-25 22:01:34 +04:00
|
|
|
|
|
|
|
switch (button) {
|
|
|
|
case BTN_RIGHT:
|
|
|
|
if (state == WL_POINTER_BUTTON_STATE_PRESSED)
|
2023-10-26 20:51:31 +03:00
|
|
|
show_popup(stacking_window->stacking, input, time,
|
2013-11-25 22:01:34 +04:00
|
|
|
widget_get_user_data(widget));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BTN_LEFT:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
key_handler(struct window *window,
|
|
|
|
struct input *input, uint32_t time,
|
|
|
|
uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
|
|
|
|
void *data)
|
|
|
|
{
|
2023-10-26 20:51:31 +03:00
|
|
|
struct stacking_window *stacking_window = data;
|
|
|
|
struct stacking *stacking = stacking_window->stacking;
|
2013-11-25 22:01:34 +04:00
|
|
|
|
|
|
|
if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (sym) {
|
|
|
|
case XKB_KEY_f:
|
|
|
|
fullscreen_handler(window, data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XKB_KEY_m:
|
|
|
|
window_set_maximized(window, !window_is_maximized(window));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XKB_KEY_n:
|
2013-12-10 00:20:16 +04:00
|
|
|
/* New top-level window. */
|
2023-10-26 20:51:31 +03:00
|
|
|
create_window(stacking, NULL);
|
2013-11-25 22:01:34 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case XKB_KEY_p:
|
|
|
|
show_popup(stacking, input, time, window);
|
|
|
|
break;
|
|
|
|
|
2023-10-26 20:51:31 +03:00
|
|
|
case XKB_KEY_c:
|
|
|
|
destroy_window(stacking_window);
|
|
|
|
break;
|
|
|
|
|
2013-11-25 22:01:34 +04:00
|
|
|
case XKB_KEY_q:
|
2023-10-26 20:51:31 +03:00
|
|
|
display_exit(stacking->display);
|
2013-11-25 22:01:34 +04:00
|
|
|
break;
|
|
|
|
|
2013-12-10 00:20:16 +04:00
|
|
|
case XKB_KEY_t:
|
|
|
|
/* New transient window. */
|
2023-10-26 20:51:31 +03:00
|
|
|
create_window(stacking, window);
|
2013-12-10 00:20:16 +04:00
|
|
|
break;
|
|
|
|
|
2013-11-25 22:01:34 +04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-06 03:14:45 +04:00
|
|
|
static void
|
|
|
|
keyboard_focus_handler(struct window *window,
|
|
|
|
struct input *device, void *data)
|
|
|
|
{
|
|
|
|
window_schedule_redraw(window);
|
|
|
|
}
|
|
|
|
|
2013-11-25 22:01:34 +04:00
|
|
|
static void
|
|
|
|
fullscreen_handler(struct window *window, void *data)
|
|
|
|
{
|
|
|
|
window_set_fullscreen(window, !window_is_fullscreen(window));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
draw_string(cairo_t *cr,
|
2016-09-29 01:18:10 +03:00
|
|
|
const char *fmt, ...) WL_PRINTF(2, 3);
|
2013-11-25 22:01:34 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
draw_string(cairo_t *cr,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
char buffer[4096];
|
|
|
|
char *p, *end;
|
|
|
|
va_list argp;
|
|
|
|
cairo_text_extents_t text_extents;
|
|
|
|
cairo_font_extents_t font_extents;
|
|
|
|
|
|
|
|
cairo_save(cr);
|
|
|
|
|
2021-11-17 20:25:17 +03:00
|
|
|
cairo_select_font_face(cr, "sans-serif",
|
2013-11-25 22:01:34 +04:00
|
|
|
CAIRO_FONT_SLANT_NORMAL,
|
|
|
|
CAIRO_FONT_WEIGHT_NORMAL);
|
|
|
|
cairo_set_font_size(cr, 14);
|
|
|
|
|
|
|
|
cairo_font_extents(cr, &font_extents);
|
|
|
|
|
|
|
|
va_start(argp, fmt);
|
|
|
|
|
|
|
|
vsnprintf(buffer, sizeof(buffer), fmt, argp);
|
|
|
|
|
|
|
|
p = buffer;
|
|
|
|
while (*p) {
|
|
|
|
end = strchr(p, '\n');
|
|
|
|
if (end)
|
|
|
|
*end = 0;
|
|
|
|
|
|
|
|
cairo_show_text(cr, p);
|
|
|
|
cairo_text_extents(cr, p, &text_extents);
|
|
|
|
cairo_rel_move_to(cr, -text_extents.x_advance, font_extents.height);
|
|
|
|
|
|
|
|
if (end)
|
|
|
|
p = end + 1;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(argp);
|
|
|
|
|
|
|
|
cairo_restore(cr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_window_background_colour(cairo_t *cr, struct window *window)
|
|
|
|
{
|
2014-04-11 05:37:54 +04:00
|
|
|
if (window_get_parent(window))
|
2013-12-10 00:20:16 +04:00
|
|
|
cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 0.4);
|
|
|
|
else if (window_is_maximized(window))
|
2013-11-25 22:01:34 +04:00
|
|
|
cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.6);
|
|
|
|
else if (window_is_fullscreen(window))
|
|
|
|
cairo_set_source_rgba(cr, 0.0, 1.0, 1.0, 0.6);
|
|
|
|
else
|
|
|
|
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
redraw_handler(struct widget *widget, void *data)
|
|
|
|
{
|
|
|
|
struct window *window;
|
|
|
|
struct rectangle allocation;
|
|
|
|
cairo_t *cr;
|
|
|
|
|
|
|
|
widget_get_allocation(widget, &allocation);
|
|
|
|
window = widget_get_user_data(widget);
|
|
|
|
|
|
|
|
cr = widget_cairo_create(widget);
|
|
|
|
cairo_translate(cr, allocation.x, allocation.y);
|
|
|
|
|
|
|
|
/* Draw background. */
|
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
|
|
|
|
set_window_background_colour(cr, window);
|
|
|
|
cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
|
|
|
|
cairo_fill(cr);
|
|
|
|
|
|
|
|
/* Print the instructions. */
|
|
|
|
cairo_move_to(cr, 5, 15);
|
|
|
|
cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
|
|
|
|
|
|
|
|
draw_string(cr,
|
|
|
|
"Window: %p\n"
|
|
|
|
"Fullscreen? %u\n"
|
|
|
|
"Maximized? %u\n"
|
2013-12-10 00:20:16 +04:00
|
|
|
"Transient? %u\n"
|
2013-11-25 22:01:34 +04:00
|
|
|
"Keys: (f)ullscreen, (m)aximize,\n"
|
|
|
|
" (n)ew window, (p)opup,\n"
|
2023-10-26 20:51:31 +03:00
|
|
|
" (c)lose, (q)uit,\n"
|
|
|
|
" (t)ransient window\n",
|
2013-11-25 22:01:34 +04:00
|
|
|
window, window_is_fullscreen(window),
|
2014-04-11 05:37:54 +04:00
|
|
|
window_is_maximized(window), window_get_parent(window) ? 1 : 0);
|
2013-11-25 22:01:34 +04:00
|
|
|
|
|
|
|
cairo_destroy(cr);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct stacking stacking;
|
2023-10-26 20:51:31 +03:00
|
|
|
struct stacking_window *stacking_window, *tmp;
|
2013-11-25 22:01:34 +04:00
|
|
|
|
|
|
|
memset(&stacking, 0, sizeof stacking);
|
2023-10-26 20:51:31 +03:00
|
|
|
wl_list_init(&stacking.windows);
|
2013-11-25 22:01:34 +04:00
|
|
|
|
|
|
|
stacking.display = display_create(&argc, argv);
|
|
|
|
if (stacking.display == NULL) {
|
2019-04-27 00:57:31 +03:00
|
|
|
fprintf(stderr, "Failed to create display: %s\n",
|
|
|
|
strerror(errno));
|
2013-11-25 22:01:34 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
display_set_user_data(stacking.display, &stacking);
|
|
|
|
|
2023-10-26 20:51:31 +03:00
|
|
|
create_window(&stacking, NULL);
|
2013-11-25 22:01:34 +04:00
|
|
|
|
|
|
|
display_run(stacking.display);
|
|
|
|
|
2023-10-26 20:51:31 +03:00
|
|
|
wl_list_for_each_safe(stacking_window, tmp, &stacking.windows, link)
|
|
|
|
destroy_window(stacking_window);
|
|
|
|
|
2014-05-15 17:28:16 +04:00
|
|
|
display_destroy(stacking.display);
|
|
|
|
|
2013-11-25 22:01:34 +04:00
|
|
|
return 0;
|
|
|
|
}
|