2022-10-20 13:43:37 +00:00
|
|
|
|
/*
|
|
|
|
|
* 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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-09-06 21:36:03 +00:00
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2022-09-07 18:10:19 +00:00
|
|
|
|
#include <getopt.h>
|
2022-09-06 21:36:03 +00:00
|
|
|
|
#include <wayland-client.h>
|
2022-09-09 20:12:06 +00:00
|
|
|
|
#include "common.h"
|
|
|
|
|
#include "wayland.h"
|
2022-09-06 21:36:03 +00:00
|
|
|
|
|
2022-09-07 18:10:19 +00:00
|
|
|
|
#define DEFAULT_BORDER_RADIUS 15
|
2022-09-07 19:12:46 +00:00
|
|
|
|
#define DEFAULT_PADDING 10
|
2022-09-07 18:10:19 +00:00
|
|
|
|
#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})
|
2022-10-20 18:48:59 +00:00
|
|
|
|
#define DEFAULT_TIME -1
|
2022-09-07 18:10:19 +00:00
|
|
|
|
|
2022-09-06 21:36:03 +00:00
|
|
|
|
|
2022-09-07 18:10:19 +00:00
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
2022-10-20 13:43:37 +00:00
|
|
|
|
fprintf(stderr, "wl-overlay: show overlays with images and text in Wayland\n"
|
|
|
|
|
"Usage: wl-overlay [options...] <image.png> [text]\n\n"
|
2022-09-07 18:10:19 +00:00
|
|
|
|
"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"
|
2022-10-20 13:43:37 +00:00
|
|
|
|
" --padding=<int> Set padding around image and text (default %d)\n"
|
2022-10-20 18:48:59 +00:00
|
|
|
|
" --time=<int> Make overlay disappear after this amount of milliseconds, or -1 to never disappear (default %d)\n"
|
2022-10-20 13:43:37 +00:00
|
|
|
|
"\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",
|
2022-09-07 18:10:19 +00:00
|
|
|
|
DEFAULT_BORDER_RADIUS,
|
|
|
|
|
DEFAULT_WIDTH, DEFAULT_HEIGHT,
|
2022-10-20 18:48:59 +00:00
|
|
|
|
DEFAULT_PADDING,
|
|
|
|
|
DEFAULT_TIME);
|
2022-09-07 18:10:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-20 13:43:37 +00:00
|
|
|
|
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"
|
2023-10-19 12:54:35 +00:00
|
|
|
|
"Copyright (c) 2021 Drew DeVault\n"
|
2022-10-20 13:43:37 +00:00
|
|
|
|
"\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");
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-06 21:36:03 +00:00
|
|
|
|
int
|
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
struct client_state state = { 0 };
|
2022-09-07 18:10:19 +00:00
|
|
|
|
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;
|
2022-09-07 19:12:46 +00:00
|
|
|
|
state.user_request.padding = DEFAULT_PADDING;
|
2022-10-20 18:48:59 +00:00
|
|
|
|
state.user_request.time = DEFAULT_TIME;
|
2022-09-07 18:10:19 +00:00
|
|
|
|
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
int option_index = 0;
|
|
|
|
|
static struct option long_options[] = {
|
|
|
|
|
{"help", no_argument, 0, 'h'},
|
2022-10-20 13:43:37 +00:00
|
|
|
|
{"copyright", no_argument, 0, 0 },
|
2022-09-07 18:10:19 +00:00
|
|
|
|
{"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 },
|
2022-09-07 19:12:46 +00:00
|
|
|
|
{"padding", required_argument, 0, 0 },
|
2022-10-20 18:48:59 +00:00
|
|
|
|
{"time", required_argument, 0, 0 },
|
|
|
|
|
{0, required_argument, 0, 0 }
|
2022-09-07 18:10:19 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
c = getopt_long(argc, argv, "h", long_options, &option_index);
|
|
|
|
|
if (c == -1)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
case 0:
|
|
|
|
|
switch (option_index) {
|
2022-10-20 13:43:37 +00:00
|
|
|
|
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;
|
2022-10-20 18:48:59 +00:00
|
|
|
|
case 10: state.user_request.time = atoi(optarg); break;
|
2022-09-07 18:10:19 +00:00
|
|
|
|
default: break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'h':
|
|
|
|
|
print_usage();
|
|
|
|
|
exit(0);
|
|
|
|
|
break;
|
|
|
|
|
default: break;
|
|
|
|
|
}
|
2022-09-06 21:36:03 +00:00
|
|
|
|
}
|
2022-09-07 18:10:19 +00:00
|
|
|
|
|
|
|
|
|
if (optind < argc) {
|
|
|
|
|
state.user_request.graphics_filename = strdup(argv[optind]);
|
2022-09-07 14:28:43 +00:00
|
|
|
|
} else {
|
2022-09-07 18:10:19 +00:00
|
|
|
|
print_usage();
|
|
|
|
|
exit(1);
|
2022-09-07 14:28:43 +00:00
|
|
|
|
}
|
2022-09-07 18:10:19 +00:00
|
|
|
|
optind++;
|
|
|
|
|
if (optind < argc) {
|
|
|
|
|
state.user_request.text = strdup(argv[optind]);
|
2022-09-07 14:28:43 +00:00
|
|
|
|
} else {
|
|
|
|
|
state.user_request.text = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-09 20:12:06 +00:00
|
|
|
|
if (state.user_request.font == NULL) {
|
|
|
|
|
state.user_request.font = strdup(DEFAULT_FONT);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-07 18:10:19 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-09-09 20:12:06 +00:00
|
|
|
|
wlo_wl_main(&state);
|
2022-09-07 18:10:19 +00:00
|
|
|
|
|
2022-09-07 19:50:25 +00:00
|
|
|
|
|
2022-09-07 14:28:43 +00:00
|
|
|
|
free(state.user_request.graphics_filename);
|
|
|
|
|
if (state.user_request.text != NULL) {
|
|
|
|
|
free(state.user_request.text);
|
|
|
|
|
}
|
2022-09-09 20:12:06 +00:00
|
|
|
|
free(state.user_request.font);
|
2022-09-06 21:36:03 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|