wl-overlay/wl-overlay.c

158 lines
4.4 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <wayland-client.h>
#include "common.h"
#include "wayland.h"
#define STR_EQUAL 0
#define DEFAULT_BORDER_RADIUS 15
#define DEFAULT_PADDING 10
#define DEFAULT_WIDTH 256
#define DEFAULT_HEIGHT 256
#define DEFAULT_FONT "Fira Sans 17"
#define DEFAULT_BACKDROP_COLOR \
((struct color_argb){0.85, 0x11 / 255.0, 0x11 / 255.0, 0x11 / 255.0})
static bool
color_argb_invalid(struct color_argb *a)
{
return !(
(0.0 <= a->a) && (a->a <= 1.0) &&
(0.0 <= a->r) && (a->r <= 1.0) &&
(0.0 <= a->g) && (a->g <= 1.0) &&
(0.0 <= a->b) && (a->b <= 1.0)
);
}
static void
parse_hex(char *hex, struct color_argb *result_color) {
if (strlen(hex) != 4 * 2) {
// Invalid
result_color->a = -1.0;
return;
}
int a, r, g, b;
sscanf(hex, "%02x%02x%02x%02x", &a, &r, &g, &b);
result_color->a = a / 255.0;
result_color->r = r / 255.0;
result_color->g = g / 255.0;
result_color->b = b / 255.0;
}
static void
print_usage()
{
fprintf(stderr, "Usage: wl-overlay [options...] <image.png> [text]\n\n"
"image.png must be the path to a valid PNG image or may be the empty string for "
"no image. text is the text to show.\n\n"
"Options:\n"
" -h --help Show this help text and exit\n"
" --backdrop=<aarrggbb> Set backdrop colour (default d9111111, set to 00000000 to disable backdrop)\n"
" --color=<aarrggbb> Set text colour (default ffffffff)\n"
" --border-radius=<int> Give the backdrop rounded corners with specified radius (default %d)\n"
" --font=<str> Set font of text (default " DEFAULT_FONT ")\n"
" --width=<int> \n"
" --height=<int> Set width and height (default %d×%d)\n"
" --padding=<int> Set padding around image and text (default %d)\n",
DEFAULT_BORDER_RADIUS,
DEFAULT_WIDTH, DEFAULT_HEIGHT,
DEFAULT_PADDING);
}
int
main(int argc, char *argv[])
{
struct client_state state = { 0 };
state.user_request.text_color = (struct color_argb){1.0, 1.0, 1.0, 1.0};
state.user_request.border_radius = DEFAULT_BORDER_RADIUS;
state.user_request.backdrop = DEFAULT_BACKDROP_COLOR;
state.user_request.width = DEFAULT_WIDTH;
state.user_request.height = DEFAULT_HEIGHT;
state.user_request.padding = DEFAULT_PADDING;
int c;
while (1) {
int option_index = 0;
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"backdrop", required_argument, 0, 0 },
{"color", required_argument, 0, 0 },
{"colour", required_argument, 0, 0 },
{"border-radius", required_argument, 0, 0 },
{"font", required_argument, 0, 0 },
{"width", required_argument, 0, 0 },
{"height", required_argument, 0, 0 },
{"padding", required_argument, 0, 0 },
{0, 0, 0, 0 }
};
c = getopt_long(argc, argv, "h", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
switch (option_index) {
case 1: parse_hex(optarg, &state.user_request.backdrop); break;
case 2: case 3: parse_hex(optarg, &state.user_request.text_color); break;
case 4: state.user_request.border_radius = atoi(optarg); break;
case 5: state.user_request.font = strdup(optarg); break;
case 6: state.user_request.width = atoi(optarg); break;
case 7: state.user_request.height = atoi(optarg); break;
case 8: state.user_request.padding = atoi(optarg); break;
default: break;
}
break;
case 'h':
print_usage();
exit(0);
break;
default: break;
}
}
if (optind < argc) {
state.user_request.graphics_filename = strdup(argv[optind]);
} else {
print_usage();
exit(1);
}
optind++;
if (optind < argc) {
state.user_request.text = strdup(argv[optind]);
} else {
state.user_request.text = NULL;
}
if (state.user_request.font == NULL) {
state.user_request.font = strdup(DEFAULT_FONT);
}
if (color_argb_invalid(&state.user_request.backdrop)) {
fprintf(stderr, "Invalid --backdrop color, must be hexadecimal aarrggbb\n");
exit(1);
}
if (color_argb_invalid(&state.user_request.text_color)) {
fprintf(stderr, "Invalid --font color, must be hexadecimal aarrggbb\n");
exit(1);
}
wlo_wl_main(&state);
free(state.user_request.graphics_filename);
if (state.user_request.text != NULL) {
free(state.user_request.text);
}
free(state.user_request.font);
return 0;
}