2021-09-24 21:45:48 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2020-12-30 10:29:21 +00:00
|
|
|
#include "config.h"
|
2021-08-16 07:16:56 +01:00
|
|
|
#include <cairo.h>
|
|
|
|
|
#include <drm_fourcc.h>
|
|
|
|
|
#include <pango/pangocairo.h>
|
2021-07-23 21:15:55 +01:00
|
|
|
#include <wlr/util/log.h>
|
2022-02-11 23:12:45 +00:00
|
|
|
#include "buffer.h"
|
2021-08-16 07:16:56 +01:00
|
|
|
#include "common/buf.h"
|
|
|
|
|
#include "common/font.h"
|
2020-09-28 20:41:41 +01:00
|
|
|
#include "config/rcxml.h"
|
|
|
|
|
#include "labwc.h"
|
2021-10-13 21:29:32 +01:00
|
|
|
#include "theme.h"
|
2020-08-20 21:13:04 +01:00
|
|
|
|
2021-08-16 07:16:56 +01:00
|
|
|
#define OSD_ITEM_HEIGHT (20)
|
|
|
|
|
#define OSD_ITEM_WIDTH (600)
|
|
|
|
|
#define OSD_ITEM_PADDING (10)
|
|
|
|
|
#define OSD_BORDER_WIDTH (6)
|
|
|
|
|
#define OSD_TAB1 (120)
|
|
|
|
|
#define OSD_TAB2 (300)
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
set_source(cairo_t *cairo, float *c)
|
2020-08-20 21:13:04 +01:00
|
|
|
{
|
2021-08-16 07:16:56 +01:00
|
|
|
cairo_set_source_rgba(cairo, c[0], c[1], c[2], c[3]);
|
|
|
|
|
}
|
2020-08-20 21:13:04 +01:00
|
|
|
|
2021-08-16 07:16:56 +01:00
|
|
|
/* is title different from app_id/class? */
|
|
|
|
|
static int
|
|
|
|
|
is_title_different(struct view *view)
|
|
|
|
|
{
|
|
|
|
|
switch (view->type) {
|
|
|
|
|
case LAB_XDG_SHELL_VIEW:
|
2021-12-16 16:24:13 +00:00
|
|
|
return g_strcmp0(view_get_string_prop(view, "title"),
|
2021-10-18 20:01:10 +01:00
|
|
|
view_get_string_prop(view, "app_id"));
|
2021-08-16 07:16:56 +01:00
|
|
|
#if HAVE_XWAYLAND
|
|
|
|
|
case LAB_XWAYLAND_VIEW:
|
2021-12-16 16:24:13 +00:00
|
|
|
return g_strcmp0(view_get_string_prop(view, "title"),
|
2021-08-16 07:16:56 +01:00
|
|
|
view->xwayland_surface->class);
|
|
|
|
|
#endif
|
2020-08-20 21:13:04 +01:00
|
|
|
}
|
2021-08-16 07:16:56 +01:00
|
|
|
return 1;
|
2020-08-20 21:13:04 +01:00
|
|
|
}
|
2019-12-27 21:45:00 +00:00
|
|
|
|
2021-08-16 07:16:56 +01:00
|
|
|
static const char *
|
|
|
|
|
get_formatted_app_id(struct view *view)
|
2019-12-27 21:45:00 +00:00
|
|
|
{
|
2021-10-18 20:01:10 +01:00
|
|
|
char *s = (char *)view_get_string_prop(view, "app_id");
|
2021-12-03 16:16:59 -05:00
|
|
|
if (s == NULL) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2021-08-16 07:16:56 +01:00
|
|
|
/* remove the first two nodes of 'org.' strings */
|
|
|
|
|
if (!strncmp(s, "org.", 4)) {
|
|
|
|
|
char *p = s + 4;
|
|
|
|
|
p = strchr(p, '.');
|
|
|
|
|
if (p) {
|
|
|
|
|
return ++p;
|
|
|
|
|
}
|
2019-12-27 21:45:00 +00:00
|
|
|
}
|
2021-08-16 07:16:56 +01:00
|
|
|
return s;
|
2019-12-27 21:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
2021-08-16 07:16:56 +01:00
|
|
|
static int
|
|
|
|
|
get_osd_height(struct wl_list *views)
|
2019-12-27 21:45:00 +00:00
|
|
|
{
|
2021-08-16 07:16:56 +01:00
|
|
|
int height = 0;
|
|
|
|
|
struct view *view;
|
|
|
|
|
wl_list_for_each(view, views, link) {
|
|
|
|
|
if (!isfocusable(view)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
height += OSD_ITEM_HEIGHT;
|
2019-12-27 21:45:00 +00:00
|
|
|
}
|
2021-08-16 07:16:56 +01:00
|
|
|
height += 2 * OSD_BORDER_WIDTH;
|
|
|
|
|
return height;
|
2019-12-27 21:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-12 22:04:27 +00:00
|
|
|
static void
|
|
|
|
|
destroy_osd_nodes(struct server *server)
|
|
|
|
|
{
|
|
|
|
|
struct wlr_scene_node *child, *next;
|
|
|
|
|
struct wl_list *children = &server->osd_tree->node.state.children;
|
|
|
|
|
wl_list_for_each_safe(child, next, children, state.link) {
|
|
|
|
|
wlr_scene_node_destroy(child);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
osd_finish(struct server *server)
|
|
|
|
|
{
|
|
|
|
|
destroy_osd_nodes(server);
|
|
|
|
|
wlr_scene_node_set_enabled(&server->osd_tree->node, false);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
void
|
2021-08-16 07:16:56 +01:00
|
|
|
osd_update(struct server *server)
|
2019-12-27 21:45:00 +00:00
|
|
|
{
|
2021-10-13 21:29:32 +01:00
|
|
|
struct theme *theme = server->theme;
|
|
|
|
|
|
2022-02-12 22:04:27 +00:00
|
|
|
destroy_osd_nodes(server);
|
2022-02-09 16:38:07 -05:00
|
|
|
struct output *output;
|
|
|
|
|
wl_list_for_each(output, &server->outputs, link) {
|
|
|
|
|
float scale = output->wlr_output->scale;
|
|
|
|
|
int w = (OSD_ITEM_WIDTH + (2 * OSD_BORDER_WIDTH)) * scale;
|
|
|
|
|
int h = get_osd_height(&server->views) * scale;
|
|
|
|
|
|
2022-02-13 13:00:26 +00:00
|
|
|
if (output->osd_buffer) {
|
|
|
|
|
wlr_buffer_drop(&output->osd_buffer->base);
|
|
|
|
|
}
|
|
|
|
|
output->osd_buffer = buffer_create(w, h, scale);
|
|
|
|
|
|
|
|
|
|
cairo_t *cairo = output->osd_buffer->cairo;
|
|
|
|
|
cairo_surface_t *surf = cairo_get_target(cairo);
|
2022-02-09 16:38:07 -05:00
|
|
|
|
|
|
|
|
/* background */
|
|
|
|
|
set_source(cairo, theme->osd_bg_color);
|
|
|
|
|
cairo_rectangle(cairo, 0, 0, w, h);
|
|
|
|
|
cairo_fill(cairo);
|
|
|
|
|
|
|
|
|
|
/* border */
|
|
|
|
|
set_source(cairo, theme->osd_label_text_color);
|
|
|
|
|
cairo_rectangle(cairo, 0, 0, w, h);
|
|
|
|
|
cairo_stroke(cairo);
|
|
|
|
|
|
|
|
|
|
/* highlight current window */
|
|
|
|
|
int y = OSD_BORDER_WIDTH;
|
|
|
|
|
struct view *view;
|
|
|
|
|
wl_list_for_each(view, &server->views, link) {
|
|
|
|
|
if (!isfocusable(view)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (view == server->cycle_view) {
|
|
|
|
|
set_source(cairo, theme->osd_label_text_color);
|
|
|
|
|
cairo_rectangle(cairo, OSD_BORDER_WIDTH, y,
|
|
|
|
|
OSD_ITEM_WIDTH, OSD_ITEM_HEIGHT);
|
|
|
|
|
cairo_stroke(cairo);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
y += OSD_ITEM_HEIGHT;
|
2021-08-16 07:16:56 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-09 16:38:07 -05:00
|
|
|
/* text */
|
|
|
|
|
set_source(cairo, theme->osd_label_text_color);
|
|
|
|
|
PangoLayout *layout = pango_cairo_create_layout(cairo);
|
|
|
|
|
pango_layout_set_width(layout, w * PANGO_SCALE);
|
|
|
|
|
pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
|
|
|
|
|
|
|
|
|
|
struct font font = {
|
|
|
|
|
.name = rc.font_name_osd,
|
|
|
|
|
.size = rc.font_size_osd,
|
|
|
|
|
};
|
|
|
|
|
PangoFontDescription *desc = pango_font_description_new();
|
|
|
|
|
pango_font_description_set_family(desc, font.name);
|
|
|
|
|
pango_font_description_set_size(desc, font.size * PANGO_SCALE);
|
|
|
|
|
pango_layout_set_font_description(layout, desc);
|
|
|
|
|
pango_font_description_free(desc);
|
|
|
|
|
|
|
|
|
|
PangoTabArray *tabs = pango_tab_array_new_with_positions(2, TRUE,
|
|
|
|
|
PANGO_TAB_LEFT, OSD_TAB1, PANGO_TAB_LEFT, OSD_TAB2);
|
|
|
|
|
pango_layout_set_tabs(layout, tabs);
|
|
|
|
|
pango_tab_array_free(tabs);
|
|
|
|
|
|
|
|
|
|
pango_cairo_update_layout(cairo, layout);
|
|
|
|
|
|
|
|
|
|
struct buf buf;
|
|
|
|
|
buf_init(&buf);
|
|
|
|
|
y = OSD_BORDER_WIDTH;
|
|
|
|
|
|
|
|
|
|
y += (OSD_ITEM_HEIGHT - font_height(&font)) / 2;
|
|
|
|
|
|
|
|
|
|
wl_list_for_each(view, &server->views, link) {
|
|
|
|
|
if (!isfocusable(view)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
buf.len = 0;
|
|
|
|
|
cairo_move_to(cairo, OSD_BORDER_WIDTH + OSD_ITEM_PADDING, y);
|
|
|
|
|
|
|
|
|
|
switch (view->type) {
|
|
|
|
|
case LAB_XDG_SHELL_VIEW:
|
|
|
|
|
buf_add(&buf, "[xdg-shell]\t");
|
|
|
|
|
buf_add(&buf, get_formatted_app_id(view));
|
|
|
|
|
buf_add(&buf, "\t");
|
|
|
|
|
break;
|
2020-12-30 10:29:21 +00:00
|
|
|
#if HAVE_XWAYLAND
|
2022-02-09 16:38:07 -05:00
|
|
|
case LAB_XWAYLAND_VIEW:
|
|
|
|
|
buf_add(&buf, "[xwayland]\t");
|
|
|
|
|
buf_add(&buf, view_get_string_prop(view, "class"));
|
|
|
|
|
buf_add(&buf, "\t");
|
|
|
|
|
break;
|
2020-12-30 10:29:21 +00:00
|
|
|
#endif
|
2022-02-09 16:38:07 -05:00
|
|
|
}
|
2019-12-27 21:45:00 +00:00
|
|
|
|
2022-02-09 16:38:07 -05:00
|
|
|
if (is_title_different(view)) {
|
|
|
|
|
buf_add(&buf, view_get_string_prop(view, "title"));
|
|
|
|
|
}
|
2019-12-27 21:45:00 +00:00
|
|
|
|
2022-02-09 16:38:07 -05:00
|
|
|
pango_layout_set_text(layout, buf.buf, -1);
|
|
|
|
|
pango_cairo_show_layout(cairo, layout);
|
|
|
|
|
y += OSD_ITEM_HEIGHT;
|
|
|
|
|
}
|
|
|
|
|
g_object_unref(layout);
|
|
|
|
|
cairo_surface_flush(surf);
|
2022-02-11 23:12:45 +00:00
|
|
|
|
|
|
|
|
struct wlr_scene_buffer *scene_buffer = wlr_scene_buffer_create(
|
|
|
|
|
&server->osd_tree->node, &output->osd_buffer->base);
|
|
|
|
|
|
|
|
|
|
/* TODO: set position properly */
|
|
|
|
|
wlr_scene_node_set_position(&scene_buffer->node, 10, 10);
|
|
|
|
|
wlr_scene_node_set_enabled(&server->osd_tree->node, true);
|
2020-06-18 20:39:55 +01:00
|
|
|
}
|
|
|
|
|
}
|