mirror of
https://github.com/artizirk/wdisplays.git
synced 2026-10-03 21:05:43 +03:00
Add command-line options for the menu toggles
This commit is contained in:
+65
@@ -923,6 +923,59 @@ static void window_state_changed(GtkWidget *window, GdkEventWindowState *event,
|
||||
}
|
||||
}
|
||||
|
||||
static int startup_autoapply = -1;
|
||||
static int startup_capture = -1;
|
||||
static int startup_overlay = -1;
|
||||
|
||||
static const GOptionEntry startup_option_entries[] = {
|
||||
{ "auto-apply", 0, 0, G_OPTION_ARG_NONE, NULL, "Apply changes automatically", NULL },
|
||||
{ "no-auto-apply", 0, 0, G_OPTION_ARG_NONE, NULL, "Apply changes only when asked to", NULL },
|
||||
{ "preview", 0, 0, G_OPTION_ARG_NONE, NULL, "Show screen contents in the preview", NULL },
|
||||
{ "no-preview", 0, 0, G_OPTION_ARG_NONE, NULL, "Do not show screen contents in the preview", NULL },
|
||||
{ "overlay", 0, 0, G_OPTION_ARG_NONE, NULL, "Overlay screen names on the screens", NULL },
|
||||
{ "no-overlay", 0, 0, G_OPTION_ARG_NONE, NULL, "Do not overlay screen names", NULL },
|
||||
{ "version", 0, 0, G_OPTION_ARG_NONE, NULL, "Print the version and exit", NULL },
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
static bool read_startup_option(GVariantDict *options, const char *on,
|
||||
const char *off, int *value) {
|
||||
bool has_on = g_variant_dict_contains(options, on);
|
||||
bool has_off = g_variant_dict_contains(options, off);
|
||||
if (has_on && has_off) {
|
||||
fprintf(stderr, "--%s and --%s cannot be used together\n", on, off);
|
||||
return false;
|
||||
}
|
||||
if (has_on || has_off) {
|
||||
*value = has_on;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static gint handle_local_options(GApplication *app, GVariantDict *options,
|
||||
gpointer data) {
|
||||
if (g_variant_dict_contains(options, "version")) {
|
||||
printf("wdisplays %s\n", WDISPLAYS_VERSION);
|
||||
return 0;
|
||||
}
|
||||
if (!read_startup_option(options, "auto-apply", "no-auto-apply", &startup_autoapply)
|
||||
|| !read_startup_option(options, "preview", "no-preview", &startup_capture)
|
||||
|| !read_startup_option(options, "overlay", "no-overlay", &startup_overlay)) {
|
||||
return 1;
|
||||
}
|
||||
if (startup_autoapply != -1 || startup_capture != -1 || startup_overlay != -1) {
|
||||
g_autoptr(GError) error = NULL;
|
||||
if (!g_application_register(app, NULL, &error)) {
|
||||
fprintf(stderr, "%s\n", error->message);
|
||||
return 1;
|
||||
}
|
||||
if (g_application_get_is_remote(app)) {
|
||||
fprintf(stderr, "wdisplays is already running, ignoring startup options\n");
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void activate(GtkApplication* app, gpointer user_data) {
|
||||
GdkDisplay *gdk_display = gdk_display_get_default();
|
||||
if (!GDK_IS_WAYLAND_DISPLAY(gdk_display)) {
|
||||
@@ -1038,6 +1091,16 @@ static void activate(GtkApplication* app, gpointer user_data) {
|
||||
state->capture = g_settings_get_boolean(state->settings, "capture-screens");
|
||||
state->show_overlay = g_settings_get_boolean(state->settings, "show-overlay");
|
||||
}
|
||||
if (startup_autoapply != -1) {
|
||||
state->autoapply = startup_autoapply;
|
||||
}
|
||||
if (startup_capture != -1) {
|
||||
state->capture = startup_capture;
|
||||
}
|
||||
if (startup_overlay != -1) {
|
||||
state->show_overlay = startup_overlay;
|
||||
}
|
||||
startup_autoapply = startup_capture = startup_overlay = -1;
|
||||
|
||||
action = g_simple_action_new_stateful("auto-apply", NULL,
|
||||
g_variant_new_boolean(state->autoapply));
|
||||
@@ -1106,6 +1169,8 @@ int main(int argc, char *argv[]) {
|
||||
g_setenv("GDK_GL", "gles", FALSE);
|
||||
GtkApplication *app = gtk_application_new(WDISPLAYS_APP_ID, G_APPLICATION_FLAGS_NONE);
|
||||
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
|
||||
g_signal_connect(app, "handle-local-options", G_CALLBACK(handle_local_options), NULL);
|
||||
g_application_add_main_option_entries(G_APPLICATION(app), startup_option_entries);
|
||||
int status = g_application_run(G_APPLICATION(app), argc, argv);
|
||||
g_object_unref(app);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user