Skip to content

Commit

Permalink
Cleanup error and help messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtsyMacaw committed Apr 22, 2024
1 parent 611970d commit fc9685c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 53 deletions.
115 changes: 65 additions & 50 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@
#include <getopt.h>
#include <gtk/gtk.h>
#include "jsmn.h"
#include "config.h" /* Generated by meson */
#ifdef LAYERSHELL
#include <gtk-layer-shell/gtk-layer-shell.h>
#endif

#ifdef LAYERSHELL
static const int exclusive_level = -1;
#endif
static const int default_size = 100;
static const char *version = "1.2.1";

#ifdef LAYERSHELL
static gboolean protocol = TRUE;
#else
static gboolean protocol = FALSE;
#endif

typedef struct
{
Expand All @@ -27,6 +32,7 @@ typedef struct
gboolean circular;
} button;

static const int default_size = 100;
static char *command = NULL;
static char *layout_path = NULL;
static char *css_path = NULL;
Expand All @@ -40,54 +46,53 @@ static int buttons_per_row = 3;
static int primary_monitor = -1;
static int margin[] = {230, 230, 230, 230};
static int space[] = {0, 0};
static gboolean protocol = TRUE;
static gboolean show_bind = FALSE;
static gboolean no_span = FALSE;
static gboolean layershell = FALSE;

static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"layout", required_argument, NULL, 'l'},
{"version", no_argument, NULL, 'v'},
{"css", required_argument, NULL, 'C'},
{"margin", required_argument, NULL, 'm'},
{"margin-top", required_argument, NULL, 'T'},
{"margin-bottom", required_argument, NULL, 'B'},
{"margin-left", required_argument, NULL, 'L'},
{"margin-right", required_argument, NULL, 'R'},
{"buttons-per-row", required_argument, NULL, 'b'},
{"column-spacing", required_argument, NULL, 'c'},
{"row-spacing", required_argument, NULL, 'r'},
{"protocol", required_argument, NULL, 'p'},
{"show-binds", no_argument, NULL, 's'},
{"no-span", no_argument, NULL, 'n'},
{"primary-monitor", required_argument, NULL, 'P'},
{0, 0, 0, 0}};

static const char *help =
"Usage: wlogout [options...]\n"
"\n"
" -h, --help Show help message and stop\n"
" -l, --layout </path/to/layout> Specify a layout file\n"
" -v, --version Show version number and stop\n"
" -C, --css </path/to/css> Specify a css file\n"
" -b, --buttons-per-row <0-x> Set the number of buttons per row\n"
" -c --column-spacing <0-x> Set space between buttons columns\n"
" -r --row-spacing <0-x> Set space between buttons rows\n"
" -m, --margin <0-x> Set margin around buttons\n"
" -L, --margin-left <0-x> Set margin for left of buttons\n"
" -R, --margin-right <0-x> Set margin for right of buttons\n"
" -T, --margin-top <0-x> Set margin for top of buttons\n"
" -B, --margin-bottom <0-x> Set margin for bottom of buttons\n"
" -p, --protocol <protocol> Use layer-shell or xdg protocol\n"
" -s, --show-binds Show the keybinds on their "
"corresponding button\n"
" -n, --no-span Stops from spanning across "
"multiple monitors\n"
" -P, --primary-monitor <0-x> Set the primary monitor\n";

static gboolean process_args(int argc, char *argv[])
{
static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"layout", required_argument, NULL, 'l'},
{"version", no_argument, NULL, 'v'},
{"css", required_argument, NULL, 'C'},
{"margin", required_argument, NULL, 'm'},
{"margin-top", required_argument, NULL, 'T'},
{"margin-bottom", required_argument, NULL, 'B'},
{"margin-left", required_argument, NULL, 'L'},
{"margin-right", required_argument, NULL, 'R'},
{"buttons-per-row", required_argument, NULL, 'b'},
{"column-spacing", required_argument, NULL, 'c'},
{"row-spacing", required_argument, NULL, 'r'},
{"protocol", required_argument, NULL, 'p'},
{"show-binds", no_argument, NULL, 's'},
{"no-span", no_argument, NULL, 'n'},
{"primary-monitor", required_argument, NULL, 'P'},
{0, 0, 0, 0}};

const char *help =
"Usage: wlogout [options] [command]\n"
"\n"
" -h, --help Show help message and stop\n"
" -l, --layout <layout> Specify a layout file\n"
" -v, --version Show version number and stop\n"
" -C, --css <css> Specify a css file\n"
" -b, --buttons-per-row <num> Set the number of buttons per row\n"
" -c --column-spacing <space> Set space between buttons columns\n"
" -r --row-spacing <space> Set space between buttons rows\n"
" -m, --margin <padding> Set margin around buttons\n"
" -L, --margin-left <padding> Set margin for left of buttons\n"
" -R, --margin-right <padding> Set margin for right of buttons\n"
" -T, --margin-top <padding> Set margin for top of buttons\n"
" -B, --margin-bottom <padding> Set margin for bottom of buttons\n"
" -p, --protocol <protocol> Use layer-shell or xdg protocol\n"
" -s, --show-binds Show the keybinds on their "
"corresponding button\n"
" -n, --no-span Stops from spanning across "
"multiple monitors\n"
" -P, --primary-monitor <num> Set the monitor that buttons "
"appear on\n";

int c;
while (TRUE)
Expand Down Expand Up @@ -125,14 +130,11 @@ static gboolean process_args(int argc, char *argv[])
case 'r':
space[0] = atoi(optarg);
break;
case 'h':
g_print("%s\n", help);
return TRUE;
case 'l':
layout_path = g_strdup(optarg);
break;
case 'v':
g_print("%s\n", version);
g_print("wlogout %s\n", PROJECT_VERSION);
return TRUE;
case 'C':
css_path = g_strdup(optarg);
Expand Down Expand Up @@ -164,6 +166,11 @@ static gboolean process_args(int argc, char *argv[])
case 'n':
no_span = TRUE;
break;
case '?':
case 'h':
default:
g_print("%s\n", help);
return TRUE;
}
}
return FALSE;
Expand Down Expand Up @@ -523,6 +530,16 @@ static gboolean check_key(GtkWidget *widget, GdkEventKey *event, gpointer data)

static void set_fullscreen(GtkWindow *win, int monitor, gboolean keyboard)
{
if (!layershell && protocol)
{
#ifdef LAYERSHELL
g_warning("Falling back to xdg protocol");
#else
g_warning("wlogout was compiled without layer-shell support\n"
"Falling back to xdg protocol");
#endif
}

if (protocol && layershell)
{
#ifdef LAYERSHELL
Expand Down Expand Up @@ -716,8 +733,6 @@ int main(int argc, char *argv[])

#ifdef LAYERSHELL
layershell = gtk_layer_is_supported();
#else
printf("wlogout was not compiled with layer shell support\n");
#endif

GtkWindow *active_window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
Expand Down
2 changes: 1 addition & 1 deletion man/wlogout.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ wlogout - A Wayland logout menu

# SYNOPSIS

*wlogout* [options] [command]
*wlogout* [options...]

# OPTIONS

Expand Down
10 changes: 8 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
project(
'wlogout',
'c',
version: '1.1.1',
version: '1.2.1',
license: 'MIT',
default_options:
[
Expand All @@ -19,9 +19,15 @@ add_project_arguments(
language: 'c'
)

# Set version in config.h
conf_data = configuration_data()
conf_data.set('PROJECT_VERSION', '"@0@"'.format(meson.project_version()))
configure_file(output: 'config.h', configuration: conf_data)

datadir = get_option('datadir')
sysconfdir = get_option('sysconfdir')

# Build man pages
scdoc = dependency('scdoc', native: true, required: get_option('man-pages'))
if scdoc.found()
scdoc_prog = find_program(scdoc.get_pkgconfig_variable('scdoc'), native: true)
Expand Down Expand Up @@ -68,7 +74,7 @@ gtk = dependency('gtk+-wayland-3.0')
layershell = dependency('gtk-layer-shell-0', required : false)

if layershell.found()
add_project_arguments('-DLAYERSHELL=1', language : 'c')
add_project_arguments('-DLAYERSHELL=1', language : 'c')
endif

install_subdir('assets', install_dir : datadir / 'wlogout')
Expand Down

0 comments on commit fc9685c

Please sign in to comment.