| 
									
										
										
										
											2018-11-25 12:12:48 +01:00
										 |  |  | #define _POSIX_C_SOURCE 200809L
 | 
					
						
							| 
									
										
										
										
											2018-08-03 01:23:39 +10:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | #include <signal.h>
 | 
					
						
							|  |  |  | #include "log.h"
 | 
					
						
							|  |  |  | #include "list.h"
 | 
					
						
							| 
									
										
										
										
											2018-07-28 22:56:12 -04:00
										 |  |  | #include "swaynag/config.h"
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | #include "swaynag/swaynag.h"
 | 
					
						
							| 
									
										
										
										
											2018-07-28 09:34:25 -04:00
										 |  |  | #include "swaynag/types.h"
 | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | static struct swaynag swaynag; | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | void sig_handler(int signal) { | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	swaynag_destroy(&swaynag); | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 	exit(EXIT_FAILURE); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void sway_terminate(int code) { | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	swaynag_destroy(&swaynag); | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 	exit(code); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 09:34:25 -04:00
										 |  |  | int main(int argc, char **argv) { | 
					
						
							| 
									
										
										
										
											2022-02-28 17:29:56 -06:00
										 |  |  | 	int status = EXIT_SUCCESS; | 
					
						
							| 
									
										
										
										
											2018-07-28 09:34:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	list_t *types = create_list(); | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	swaynag_types_add_default(types); | 
					
						
							| 
									
										
										
										
											2018-07-28 09:34:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	swaynag.buttons = create_list(); | 
					
						
							| 
									
										
										
										
											2020-03-29 09:53:52 +02:00
										 |  |  | 	wl_list_init(&swaynag.outputs); | 
					
						
							|  |  |  | 	wl_list_init(&swaynag.seats); | 
					
						
							| 
									
										
										
										
											2018-07-28 09:34:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-08 09:21:30 -05:00
										 |  |  | 	struct swaynag_button *button_close = calloc(1, sizeof(struct swaynag_button)); | 
					
						
							|  |  |  | 	button_close->text = strdup("X"); | 
					
						
							|  |  |  | 	button_close->type = SWAYNAG_ACTION_DISMISS; | 
					
						
							|  |  |  | 	list_add(swaynag.buttons, button_close); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	swaynag.details.details_text = strdup("Toggle details"); | 
					
						
							| 
									
										
										
										
											2022-06-05 10:37:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 09:34:25 -04:00
										 |  |  | 	char *config_path = NULL; | 
					
						
							|  |  |  | 	bool debug = false; | 
					
						
							| 
									
										
										
										
											2022-02-28 17:29:56 -06:00
										 |  |  | 	status = swaynag_parse_options(argc, argv, NULL, NULL, NULL, | 
					
						
							| 
									
										
										
										
											2018-07-28 09:34:25 -04:00
										 |  |  | 			&config_path, &debug); | 
					
						
							| 
									
										
										
										
											2022-02-28 17:29:56 -06:00
										 |  |  | 	if (status != 0)  { | 
					
						
							| 
									
										
										
										
											2018-07-28 09:34:25 -04:00
										 |  |  | 		goto cleanup; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-01-20 13:51:12 -05:00
										 |  |  | 	sway_log_init(debug ? SWAY_DEBUG : SWAY_ERROR, NULL); | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 09:34:25 -04:00
										 |  |  | 	if (!config_path) { | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 		config_path = swaynag_get_config_path(); | 
					
						
							| 
									
										
										
										
											2018-07-28 09:34:25 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if (config_path) { | 
					
						
							| 
									
										
										
										
											2019-01-20 13:51:12 -05:00
										 |  |  | 		sway_log(SWAY_DEBUG, "Loading config file: %s", config_path); | 
					
						
							| 
									
										
										
										
											2022-02-28 17:29:56 -06:00
										 |  |  | 		status = swaynag_load_config(config_path, &swaynag, types); | 
					
						
							|  |  |  | 		if (status != 0) { | 
					
						
							| 
									
										
										
										
											2018-07-28 09:34:25 -04:00
										 |  |  | 			goto cleanup; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-28 17:02:40 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 09:34:25 -04:00
										 |  |  | 	if (argc > 1) { | 
					
						
							| 
									
										
										
										
											2019-04-19 22:44:11 -04:00
										 |  |  | 		struct swaynag_type *type_args = swaynag_type_new("<args>"); | 
					
						
							| 
									
										
										
										
											2018-07-29 22:42:03 -04:00
										 |  |  | 		list_add(types, type_args); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-28 17:29:56 -06:00
										 |  |  | 		status = swaynag_parse_options(argc, argv, &swaynag, types, | 
					
						
							| 
									
										
										
										
											2018-07-29 22:42:03 -04:00
										 |  |  | 				type_args, NULL, NULL); | 
					
						
							| 
									
										
										
										
											2022-02-28 17:29:56 -06:00
										 |  |  | 		if (status != 0) { | 
					
						
							| 
									
										
										
										
											2018-07-28 09:34:25 -04:00
										 |  |  | 			goto cleanup; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	if (!swaynag.message) { | 
					
						
							| 
									
										
										
										
											2019-01-20 13:51:12 -05:00
										 |  |  | 		sway_log(SWAY_ERROR, "No message passed. Please provide --message/-m"); | 
					
						
							| 
									
										
										
										
											2022-02-28 17:29:56 -06:00
										 |  |  | 		status = EXIT_FAILURE; | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 		goto cleanup; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	if (!swaynag.type) { | 
					
						
							|  |  |  | 		swaynag.type = swaynag_type_get(types, "error"); | 
					
						
							| 
									
										
										
										
											2018-07-28 09:34:25 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-19 22:44:11 -04:00
										 |  |  | 	// Construct a new type with the defaults as the base, the general config
 | 
					
						
							|  |  |  | 	// on top of that, followed by the type config, and finally any command
 | 
					
						
							|  |  |  | 	// line arguments
 | 
					
						
							|  |  |  | 	struct swaynag_type *type = swaynag_type_new(swaynag.type->name); | 
					
						
							| 
									
										
										
										
											2018-07-29 22:42:03 -04:00
										 |  |  | 	swaynag_type_merge(type, swaynag_type_get(types, "<defaults>")); | 
					
						
							| 
									
										
										
										
											2019-04-19 22:44:11 -04:00
										 |  |  | 	swaynag_type_merge(type, swaynag_type_get(types, "<config>")); | 
					
						
							|  |  |  | 	swaynag_type_merge(type, swaynag.type); | 
					
						
							|  |  |  | 	swaynag_type_merge(type, swaynag_type_get(types, "<args>")); | 
					
						
							| 
									
										
										
										
											2018-07-29 22:42:03 -04:00
										 |  |  | 	swaynag.type = type; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	if (swaynag.details.message) { | 
					
						
							| 
									
										
										
										
											2023-01-08 09:21:30 -05:00
										 |  |  | 		swaynag.details.button_details = calloc(1, sizeof(struct swaynag_button)); | 
					
						
							|  |  |  | 		swaynag.details.button_details->text = strdup(swaynag.details.details_text); | 
					
						
							|  |  |  | 		swaynag.details.button_details->type = SWAYNAG_ACTION_EXPAND; | 
					
						
							|  |  |  | 		list_add(swaynag.buttons, swaynag.details.button_details); | 
					
						
							| 
									
										
										
										
											2018-07-27 01:30:35 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-20 13:51:12 -05:00
										 |  |  | 	sway_log(SWAY_DEBUG, "Output: %s", swaynag.type->output); | 
					
						
							| 
									
										
										
										
											2020-06-04 15:43:42 +02:00
										 |  |  | 	sway_log(SWAY_DEBUG, "Anchors: %" PRIu32, swaynag.type->anchors); | 
					
						
							| 
									
										
										
										
											2019-01-20 13:51:12 -05:00
										 |  |  | 	sway_log(SWAY_DEBUG, "Type: %s", swaynag.type->name); | 
					
						
							|  |  |  | 	sway_log(SWAY_DEBUG, "Message: %s", swaynag.message); | 
					
						
							| 
									
										
										
										
											2022-12-21 11:19:04 +01:00
										 |  |  | 	char *font = pango_font_description_to_string(swaynag.type->font_description); | 
					
						
							|  |  |  | 	sway_log(SWAY_DEBUG, "Font: %s", font); | 
					
						
							|  |  |  | 	free(font); | 
					
						
							| 
									
										
										
										
											2019-01-20 13:51:12 -05:00
										 |  |  | 	sway_log(SWAY_DEBUG, "Buttons"); | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	for (int i = 0; i < swaynag.buttons->length; i++) { | 
					
						
							|  |  |  | 		struct swaynag_button *button = swaynag.buttons->items[i]; | 
					
						
							| 
									
										
										
										
											2019-01-20 13:51:12 -05:00
										 |  |  | 		sway_log(SWAY_DEBUG, "\t[%s] `%s`", button->text, button->action); | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	signal(SIGTERM, sig_handler); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	swaynag_setup(&swaynag); | 
					
						
							|  |  |  | 	swaynag_run(&swaynag); | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | cleanup: | 
					
						
							| 
									
										
										
										
											2018-07-28 23:15:12 -04:00
										 |  |  | 	swaynag_types_free(types); | 
					
						
							|  |  |  | 	swaynag_destroy(&swaynag); | 
					
						
							| 
									
										
										
										
											2022-02-28 17:29:56 -06:00
										 |  |  | 	return status; | 
					
						
							| 
									
										
										
										
											2018-07-25 21:57:19 -04:00
										 |  |  | } |