window-title: implement window-title command line argument

When -t or --window-title is supplied, its required argument is treated as a
format string where `%o` is replaced by the `wlr-output`'s `name` when we set
the window title.

This uses glib to split and join the format string because our own
`string-helpers` library only has `str_join`. Otherwise using `string-helpers`
would have been preferred.
This commit is contained in:
massi 2026-05-11 16:39:22 -07:00 committed by Johan Malm
parent 7a53f294a8
commit df6dde131f
3 changed files with 19 additions and 8 deletions

View file

@ -315,6 +315,8 @@ struct server {
struct sfdo *sfdo; struct sfdo *sfdo;
pid_t primary_client_pid; pid_t primary_client_pid;
char *window_title_fmt;
}; };
/* defined in main.c */ /* defined in main.c */

View file

@ -9,6 +9,7 @@
#include "common/font.h" #include "common/font.h"
#include "common/macros.h" #include "common/macros.h"
#include "common/spawn.h" #include "common/spawn.h"
#include "common/string-helpers.h"
#include "config/rcxml.h" #include "config/rcxml.h"
#include "config/session.h" #include "config/session.h"
#include "labwc.h" #include "labwc.h"
@ -37,6 +38,7 @@ static const struct option long_options[] = {
{"reconfigure", no_argument, NULL, 'r'}, {"reconfigure", no_argument, NULL, 'r'},
{"startup", required_argument, NULL, 's'}, {"startup", required_argument, NULL, 's'},
{"session", required_argument, NULL, 'S'}, {"session", required_argument, NULL, 'S'},
{"window-title", required_argument, NULL, 't'},
{"version", no_argument, NULL, 'v'}, {"version", no_argument, NULL, 'v'},
{"verbose", no_argument, NULL, 'V'}, {"verbose", no_argument, NULL, 'V'},
{0, 0, 0, 0} {0, 0, 0, 0}
@ -178,7 +180,7 @@ main(int argc, char *argv[])
int c; int c;
while (1) { while (1) {
int index = 0; int index = 0;
c = getopt_long(argc, argv, "c:C:dehmrs:S:vV", long_options, &index); c = getopt_long(argc, argv, "c:C:dehmrs:S:t:vV", long_options, &index);
if (c == -1) { if (c == -1) {
break; break;
} }
@ -207,6 +209,9 @@ main(int argc, char *argv[])
case 'S': case 'S':
primary_client = optarg; primary_client = optarg;
break; break;
case 't':
server.window_title_fmt = optarg;
break;
case 'v': case 'v':
print_version(); print_version();
exit(0); exit(0);
@ -265,6 +270,10 @@ main(int argc, char *argv[])
increase_nofile_limit(); increase_nofile_limit();
if (string_null_or_empty(server.window_title_fmt)) {
server.window_title_fmt = "labwc - %o";
}
server_init(); server_init();
server_start(); server_start();

View file

@ -10,6 +10,7 @@
#include "output.h" #include "output.h"
#include <assert.h> #include <assert.h>
#include <drm_fourcc.h> #include <drm_fourcc.h>
#include <glib.h>
#include <strings.h> #include <strings.h>
#include <wlr/backend/wayland.h> #include <wlr/backend/wayland.h>
#include <wlr/config.h> #include <wlr/config.h>
@ -26,6 +27,7 @@
#include "common/macros.h" #include "common/macros.h"
#include "common/mem.h" #include "common/mem.h"
#include "common/scene-helpers.h" #include "common/scene-helpers.h"
#include "common/string-helpers.h"
#include "config/rcxml.h" #include "config/rcxml.h"
#include "labwc.h" #include "labwc.h"
#include "layers.h" #include "layers.h"
@ -605,13 +607,11 @@ handle_new_output(struct wl_listener *listener, void *data)
} }
if (wlr_output_is_wl(wlr_output)) { if (wlr_output_is_wl(wlr_output)) {
char title[64]; gchar** parts = g_strsplit(server.window_title_fmt, "%o", -1);
if (getenv("LABWC_TITLE")) { gchar* formatted_title = g_strjoinv(wlr_output->name, parts);
snprintf(title, sizeof(title), "%s", getenv("LABWC_TITLE")); g_strfreev(parts);
} else { wlr_wl_output_set_title(wlr_output, formatted_title);
snprintf(title, sizeof(title), "%s - %s", "labwc", wlr_output->name); g_free(formatted_title);
}
wlr_wl_output_set_title(wlr_output, title);
wlr_wl_output_set_app_id(wlr_output, "labwc"); wlr_wl_output_set_app_id(wlr_output, "labwc");
} }