229 lines
7.7 KiB
C
229 lines
7.7 KiB
C
/*
|
||
* This file is part of wl-overlay: show overlays with images and text in Wayland
|
||
* Copyright © 2022 Midgard
|
||
*
|
||
* This program is free software: you can redistribute it and/or modify
|
||
* it under the terms of the GNU General Public License as published by
|
||
* the Free Software Foundation, either version 3 of the License, or
|
||
* (at your option) any later version.
|
||
*
|
||
* This program is distributed in the hope that it will be useful,
|
||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
* GNU General Public License for more details.
|
||
*
|
||
* You should have received a copy of the GNU General Public License
|
||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
*/
|
||
|
||
#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, "wl-overlay: show overlays with images and text in Wayland\n"
|
||
"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"
|
||
"\n"
|
||
"Copyright information:\n"
|
||
" This software is © 2022 Midgard, released under GPLv3+\n"
|
||
" Portions are © 2021 Drew DeVault\n"
|
||
" Call wl-overlay --copyright for more information\n",
|
||
DEFAULT_BORDER_RADIUS,
|
||
DEFAULT_WIDTH, DEFAULT_HEIGHT,
|
||
DEFAULT_PADDING);
|
||
}
|
||
|
||
static void
|
||
print_copyright()
|
||
{
|
||
fprintf(stderr, "wl-overlay: show overlays with images and text in Wayland\n"
|
||
"Copyright © 2022 Midgard\n"
|
||
"\n"
|
||
"This program is free software: you can redistribute it and/or modify\n"
|
||
"it under the terms of the GNU General Public License as published by\n"
|
||
"the Free Software Foundation, either version 3 of the License, or\n"
|
||
"(at your option) any later version.\n"
|
||
"\n"
|
||
"This program is distributed in the hope that it will be useful,\n"
|
||
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
|
||
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
|
||
"GNU General Public License for more details.\n"
|
||
"\n"
|
||
"You should have received a copy of the GNU General Public License\n"
|
||
"along with this program. If not, see <http://www.gnu.org/licenses/>.\n"
|
||
"\n"
|
||
"---\n"
|
||
"\n"
|
||
"This program uses example code from the Wayland Book (https://wayland-book.com/).\n"
|
||
"This code was licensed to Midgard under the MIT license in an email:\n"
|
||
"\n"
|
||
"Copyright (c) 2021 Drew Devault\n"
|
||
"\n"
|
||
"Permission is hereby granted, free of charge, to any person obtaining a copy\n"
|
||
"of this software and associated documentation files (the \"Software\"), to deal\n"
|
||
"in the Software without restriction, including without limitation the rights\n"
|
||
"to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n"
|
||
"copies of the Software, and to permit persons to whom the Software is\n"
|
||
"furnished to do so, subject to the following conditions:\n"
|
||
"\n"
|
||
"The above copyright notice and this permission notice shall be included in all\n"
|
||
"copies or substantial portions of the Software.\n"
|
||
"\n"
|
||
"THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n"
|
||
"IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n"
|
||
"FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n"
|
||
"AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n"
|
||
"LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n"
|
||
"OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n"
|
||
"SOFTWARE.\n");
|
||
}
|
||
|
||
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'},
|
||
{"copyright", no_argument, 0, 0 },
|
||
{"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: print_copyright(); exit(0); break;
|
||
case 2: parse_hex(optarg, &state.user_request.backdrop); break;
|
||
case 3: case 4: parse_hex(optarg, &state.user_request.text_color); break;
|
||
case 5: state.user_request.border_radius = atoi(optarg); break;
|
||
case 6: state.user_request.font = strdup(optarg); break;
|
||
case 7: state.user_request.width = atoi(optarg); break;
|
||
case 8: state.user_request.height = atoi(optarg); break;
|
||
case 9: 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;
|
||
}
|