commit db267cf450441b238689a113f1fdf3ed50ab1465
parent 86806a43fbe42ba86d540d22bbdca84f18ba711b
Author: Janne Veteläinen <janne.vetelainen@elisanet.fi>
Date: Sat, 6 Jul 2024 17:32:41 +0300
Use getopt for arg parsing
Diffstat:
| M | dwlb.c | | | 109 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------- |
| M | systray/dwlbtray.c | | | 35 | +++++++++++++++-------------------- |
2 files changed, 95 insertions(+), 49 deletions(-)
diff --git a/dwlb.c b/dwlb.c
@@ -1689,47 +1689,98 @@ sig_handler(int sig)
run_display = false;
}
+const char tray_bin_name[] = "dwlbtray";
+
static void
-start_systray(const char *parent_progname, const char *traymon, bool bottom)
+construct_tray_path(char *path_buf, const char *parent_progname, size_t size)
{
- char tray_exe_path[PATH_MAX];
- char traypath_maybe[PATH_MAX];
char progname_buf[PATH_MAX];
- char traybg_arg[64];
- char height_arg[64];
- char traymon_arg[64];
- char position_arg[64];
+ char traypath_maybe[PATH_MAX];
+ snprintf(progname_buf, sizeof(progname_buf), "%s", parent_progname);
+
+ char *lastslash = strrchr(progname_buf, '/');
+ if (lastslash) {
+ *lastslash = '\0';
+ snprintf(traypath_maybe,
+ sizeof(traypath_maybe),
+ "%s/systray/%s",
+ progname_buf,
+ tray_bin_name);
+ } else {
+ *traypath_maybe = '\0';
+ }
+
+ if (access(traypath_maybe, X_OK) == 0) {
+ if ((strlen(traypath_maybe) + 1) > size) {
+ DIE("Path too long");
+ }
+ snprintf(path_buf, size, "%s", traypath_maybe);
+ } else {
+ snprintf(path_buf, size, "%s", tray_bin_name);
+ }
+}
+
+static void
+construct_traybg_arg(char *traybg_arg, size_t size)
+{
pixman_color_t *traybg_clr = &inactive_bg_color;
snprintf(traybg_arg,
- sizeof(traybg_arg),
- "--bg-color=#%02x%02x%02x",
+ size,
+ "#%02x%02x%02x",
(traybg_clr->red / 0x101),
(traybg_clr->green / 0x101),
(traybg_clr->blue) / 0x101);
+}
- strncpy(progname_buf, parent_progname, sizeof(progname_buf));
- char *lastslash = strrchr(progname_buf, '/');
- if (lastslash) {
- *(lastslash) = '\0';
- snprintf(traypath_maybe, sizeof(traypath_maybe), "%s/systray/dwlbtray", progname_buf);
- } else {
- *(traypath_maybe) = '\0';
+static void
+construct_trayheight_arg(char *height_arg, size_t size)
+{
+ snprintf(height_arg, size, "%u", height);
+}
+
+static void
+construct_traymon_arg(char *traymon_arg, const char *traymon, size_t size)
+{
+ snprintf(traymon_arg, size, "%s", traymon);
+}
+
+static void
+start_systray(const char *parent_progname, const char *traymon, bool bottom)
+{
+ char *args[16];
+
+ char argv0[PATH_MAX];
+ char traybg_opt[] = "-c";
+ char trayheight_opt[] = "-s";
+ char bottom_opt[] = "-b";
+ char traymon_opt[] = "-t";
+ char traybg_arg[16];
+ char trayheight_arg[16];
+ char traymon_arg[64];
+
+ construct_tray_path(argv0, parent_progname, sizeof(argv0));
+ construct_traybg_arg(traybg_arg, sizeof(traybg_arg));
+ construct_trayheight_arg(trayheight_arg, sizeof(trayheight_arg));
+
+ int curarg = 0;
+ args[curarg++] = argv0;
+ args[curarg++] = traybg_opt;
+ args[curarg++] = traybg_arg;
+ args[curarg++] = trayheight_opt;
+ args[curarg++] = trayheight_arg;
+ if (bottom) {
+ args[curarg++] = bottom_opt;
}
- if (access(traypath_maybe, X_OK) == 0)
- strcpy(tray_exe_path, traypath_maybe);
- else
- strcpy(tray_exe_path, "dwlbtray");
+ if (traymon) {
+ construct_traymon_arg(traymon_arg, traymon, sizeof(traymon_arg));
+ args[curarg++] = traymon_opt;
+ args[curarg++] = traymon_arg;
+ }
+ args[curarg] = NULL;
- snprintf(height_arg, sizeof(height_arg), "--height=%u", height);
- snprintf(traymon_arg, sizeof(traymon_arg), "--traymon=%s", traymon);
- if (!bottom)
- snprintf(position_arg, sizeof(position_arg), "--position=%s", "top");
- else
- snprintf(position_arg, sizeof(position_arg), "--position=%s", "bottom");
- char *args[] = { tray_exe_path, position_arg, height_arg, traybg_arg, traymon_arg, NULL };
- if (!traymon)
- args[4] = NULL;
+ // Example result:
+ // char *args[16] = { "/home/user/git/dwlb/systray/dwlbtray", "-c", "#FFFFFF", "-s", "99", "-b", "-t", "DP-1", NULL, *garbage*, ... };
int child_pid = fork();
if (child_pid == 0) {
diff --git a/systray/dwlbtray.c b/systray/dwlbtray.c
@@ -1,4 +1,5 @@
#include <stdlib.h>
+#include <unistd.h>
#include <string.h>
#include <glib.h>
@@ -11,13 +12,14 @@
#include "snhost.h"
typedef struct args_parsed {
- int barheight;
char cssdata[64];
+ int barheight;
int position;
} args_parsed;
static const int margin = 4;
static const int spacing = 4;
+static const char cssskele[] = "window{background-color:%s;}";
enum {
DWLB_POSITION_TOP,
@@ -119,28 +121,21 @@ main(int argc, char *argv[])
args_parsed args;
args.barheight = 22;
args.position = DWLB_POSITION_TOP;
+ snprintf(args.cssdata, sizeof(args.cssdata), cssskele, "#222222");
- char *bgcolor = NULL;
-
- int i = 1;
- for (; i < argc; i++) {
- char **strings = g_strsplit(argv[i], "=", 0);
- if (strcmp(strings[0], "--height") == 0) {
- args.barheight = strtol(strings[1], NULL, 10);
- } else if (strcmp(strings[0], "--bg-color") == 0) {
- bgcolor = strdup(strings[1]);
- } else if (strcmp(strings[0], "--position") == 0) {
- if (strcmp(strings[1], "bottom") == 0)
+ int option;
+ while ((option = getopt(argc, argv, "bc:s:")) != -1) {
+ switch (option) {
+ case 'b': // "bottom"
args.position = DWLB_POSITION_BOTTOM;
+ break;
+ case 'c': // "color"
+ snprintf(args.cssdata, sizeof(args.cssdata), cssskele, optarg);
+ break;
+ case 's': // "size"
+ args.barheight = strtol(optarg, NULL, 10);
+ break;
}
- g_strfreev(strings);
- }
-
- if (bgcolor) {
- snprintf(args.cssdata, sizeof(args.cssdata), "window{background-color:%s;}", bgcolor);
- g_free(bgcolor);
- } else {
- snprintf(args.cssdata, sizeof(args.cssdata), "window{background-color:%s;}", "#222222");
}
GtkApplication *app = gtk_application_new("org.dwlb.dwlbtray",