Compare commits

...

2 Commits

Author SHA1 Message Date
Přemysl Eric Janouch 94bc8c251c
wmstatus: improve Sway forwards of xkb-lock-group
Alpine 3.20 Success Details
OpenBSD 7.5 Success Details
2024-08-08 14:58:02 +02:00
Přemysl Eric Janouch e83cfa3c15
Fix calloc argument order, add some consts 2024-08-08 14:39:28 +02:00
4 changed files with 53 additions and 33 deletions

View File

@ -124,7 +124,7 @@ config_validate_nonnegative (const struct config_item *item, struct error **e)
return error_set (e, "must be non-negative"); return error_set (e, "must be non-negative");
} }
static struct config_schema g_config_device[] = static const struct config_schema g_config_device[] =
{ {
{ .name = "name", { .name = "name",
.comment = "Device identifier", .comment = "Device identifier",
@ -137,7 +137,7 @@ static struct config_schema g_config_device[] =
{} {}
}; };
static struct config_schema g_config_pwm[] = static const struct config_schema g_config_pwm[] =
{ {
{ .name = "temp", { .name = "temp",
.comment = "Path to temperature sensor output", .comment = "Path to temperature sensor output",
@ -415,7 +415,7 @@ device_create (struct app_context *ctx, const char *path,
// There is no room for errors in the configuration, everything must be valid. // There is no room for errors in the configuration, everything must be valid.
// Thus the reset to defaults on invalid values is effectively disabled here. // Thus the reset to defaults on invalid values is effectively disabled here.
static bool static bool
apply_schema (struct config_schema *schema, struct config_item *object, apply_schema (const struct config_schema *schema, struct config_item *object,
struct error **e) struct error **e)
{ {
struct error *warning = NULL, *error = NULL; struct error *warning = NULL, *error = NULL;
@ -445,7 +445,7 @@ static bool
check_device_configuration (struct config_item *subtree, struct error **e) check_device_configuration (struct config_item *subtree, struct error **e)
{ {
// Check regular fields in the device object // Check regular fields in the device object
for (struct config_schema *s = g_config_device; s->name; s++) for (const struct config_schema *s = g_config_device; s->name; s++)
if (!apply_schema (s, subtree, e)) if (!apply_schema (s, subtree, e))
return false; return false;
@ -465,7 +465,7 @@ check_device_configuration (struct config_item *subtree, struct error **e)
while ((pwm = str_map_iter_next (&iter))) while ((pwm = str_map_iter_next (&iter)))
{ {
const char *subpath = iter.link->key; const char *subpath = iter.link->key;
for (struct config_schema *s = g_config_pwm; s->name; s++) for (const struct config_schema *s = g_config_pwm; s->name; s++)
if (!apply_schema (s, pwm, &error)) if (!apply_schema (s, pwm, &error))
{ {
error_set (e, "PWM `%s': %s", subpath, error->message); error_set (e, "PWM `%s': %s", subpath, error->message);

View File

@ -290,7 +290,7 @@ on_sink_info (pa_context *context, const pa_sink_info *info, int eol,
sink->ports_len++; sink->ports_len++;
struct port *port = sink->ports = struct port *port = sink->ports =
xcalloc (sizeof *sink->ports, sink->ports_len); xcalloc (sink->ports_len, sizeof *sink->ports);
for (struct pa_sink_port_info **iter = info->ports; *iter; iter++) for (struct pa_sink_port_info **iter = info->ports; *iter; iter++)
{ {
port->name = xstrdup ((*iter)->name); port->name = xstrdup ((*iter)->name);

View File

@ -2581,7 +2581,7 @@ action_xkb_lock_group (struct app_context *ctx, const struct strv *args)
return; return;
} }
long group = strtol (args->vector[0], NULL, 10) - 1; long group = strtol (args->vector[0], NULL, 10);
if (group < XkbGroup1Index || group > XkbGroup4Index) if (group < XkbGroup1Index || group > XkbGroup4Index)
print_warning ("invalid XKB group index: %s", args->vector[0]); print_warning ("invalid XKB group index: %s", args->vector[0]);
else else
@ -3039,6 +3039,30 @@ sway_append_command_argument (struct str *out, const char *word)
str_append_c (out, '\''); str_append_c (out, '\'');
} }
static char *
sway_shell_command_for_action (const struct strv *args)
{
// The i3/Sway quoting is properly fucked up,
// and its exec command forwards to `sh -c`.
struct str shell_command = str_make ();
if (strcmp (args->vector[0], "exec"))
{
// argv[0] would need realpath() applied on it.
shell_quote (PROGRAM_NAME, &shell_command);
str_append (&shell_command, " -- ");
shell_quote (args->vector[0], &shell_command);
str_append_c (&shell_command, ' ');
}
for (size_t i = 1; i < args->len; i++)
{
shell_quote (args->vector[i], &shell_command);
str_append_c (&shell_command, ' ');
}
if (shell_command.len)
shell_command.str[--shell_command.len] = 0;
return str_steal (&shell_command);
}
static const char * static const char *
sway_bindsym (const char *combination, const char *action) sway_bindsym (const char *combination, const char *action)
{ {
@ -3063,25 +3087,6 @@ sway_bindsym (const char *combination, const char *action)
goto out; goto out;
} }
// The i3/Sway quoting is properly fucked up,
// and its exec command forwards to `sh -c`.
struct str shell_command = str_make ();
if (strcmp (handler.name, "exec"))
{
// argv[0] would need realpath() applied on it.
shell_quote (PROGRAM_NAME, &shell_command);
str_append (&shell_command, " -- ");
shell_quote (handler.name, &shell_command);
str_append_c (&shell_command, ' ');
}
for (size_t i = 1; i < args.len; i++)
{
shell_quote (args.vector[i], &shell_command);
str_append_c (&shell_command, ' ');
}
if (shell_command.len)
shell_command.str[--shell_command.len] = 0;
// This command name may not be quoted. // This command name may not be quoted.
// Note that i3-msg doesn't accept bindsym at all, only swaymsg does. // Note that i3-msg doesn't accept bindsym at all, only swaymsg does.
struct str sway_command = str_make (); struct str sway_command = str_make ();
@ -3089,9 +3094,24 @@ sway_bindsym (const char *combination, const char *action)
char *recombined = strv_join (&keys, "+"); char *recombined = strv_join (&keys, "+");
sway_append_command_argument (&sway_command, recombined); sway_append_command_argument (&sway_command, recombined);
free (recombined); free (recombined);
sway_append_command_argument (&sway_command, "exec");
sway_append_command_argument (&sway_command, shell_command.str); if (handler.handler == action_xkb_lock_group)
str_free (&shell_command); {
// This should also switch the XWayland layout,
// though it has been observed to not take effect immediately.
sway_append_command_argument (&sway_command, "input");
sway_append_command_argument (&sway_command, "type:keyboard");
sway_append_command_argument (&sway_command, "xkb_switch_layout");
for (size_t i = 1; i < args.len; i++)
sway_append_command_argument (&sway_command, args.vector[i]);
}
else
{
sway_append_command_argument (&sway_command, "exec");
char *shell_command = sway_shell_command_for_action (&args);
sway_append_command_argument (&sway_command, shell_command);
free (shell_command);
}
struct strv argv = strv_make (); struct strv argv = strv_make ();
strv_append (&argv, "swaymsg"); strv_append (&argv, "swaymsg");

View File

@ -16,10 +16,10 @@ keys = {
"XF86AudioPrev" = "mpd previous" "XF86AudioPrev" = "mpd previous"
"XF86AudioNext" = "mpd next" "XF86AudioNext" = "mpd next"
"Mod4 F1" = "xkb-lock-group 1" "Mod4 F1" = "xkb-lock-group 0"
"Mod4 F2" = "xkb-lock-group 2" "Mod4 F2" = "xkb-lock-group 1"
"Mod4 F3" = "xkb-lock-group 3" "Mod4 F3" = "xkb-lock-group 2"
"Mod4 F4" = "xkb-lock-group 4" "Mod4 F4" = "xkb-lock-group 3"
"Mod4 Control F1" = "exec input-switch vga 1" "Mod4 Control F1" = "exec input-switch vga 1"
"Mod4 Control Shift F1" = "exec input-switch vga 2" "Mod4 Control Shift F1" = "exec input-switch vga 2"