Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9583abeaf1 | |||
| d286cecaef | |||
| 15276f20fe | |||
| 13c06c274d | |||
| 94e911a4c5 | |||
| c24db4dcbd | |||
| 18e6f1a70f | |||
| 3aac31a8e5 | |||
| 2eb289ead1 |
@@ -1,3 +1,10 @@
|
|||||||
ssdd
|
ssdd
|
||||||
resources.c
|
resources.c
|
||||||
resources.h
|
resources.h
|
||||||
|
|
||||||
|
# Atlas Scout local data
|
||||||
|
.atlas/
|
||||||
|
|
||||||
|
# ZaguanBlade local data
|
||||||
|
.zblade/
|
||||||
|
.pnpm-store/
|
||||||
|
|||||||
@@ -2,8 +2,13 @@
|
|||||||
CC = gcc
|
CC = gcc
|
||||||
|
|
||||||
# Compiler and optimization flags
|
# Compiler and optimization flags
|
||||||
CFLAGS = `pkg-config --cflags gtk+-3.0` -O3
|
CFLAGS ?= -O2 -Wall -Wextra
|
||||||
LDFLAGS = `pkg-config --libs gtk+-3.0`
|
GTK_CFLAGS := $(shell pkg-config --cflags gtk4)
|
||||||
|
GTK_LIBS := $(shell pkg-config --libs gtk4)
|
||||||
|
VERSION_FLAGS = -DGTK_VERSION_MAX_ALLOWED=GTK_VERSION_4_0 \
|
||||||
|
-DGTK_VERSION_MIN_REQUIRED=GTK_VERSION_4_0 \
|
||||||
|
-DGDK_VERSION_MAX_ALLOWED=GDK_VERSION_4_0 \
|
||||||
|
-DGDK_VERSION_MIN_REQUIRED=GDK_VERSION_4_0
|
||||||
|
|
||||||
# Source files
|
# Source files
|
||||||
SRC = ssdd.c resources.c
|
SRC = ssdd.c resources.c
|
||||||
@@ -14,7 +19,7 @@ TARGET = ssdd
|
|||||||
# Resource files
|
# Resource files
|
||||||
RESOURCE_XML = resources.gresource.xml
|
RESOURCE_XML = resources.gresource.xml
|
||||||
RESOURCE_C = resources.c
|
RESOURCE_C = resources.c
|
||||||
RESOURCE_H = resources.h
|
RESOURCE_FILES = ssdd-icon.png
|
||||||
|
|
||||||
# Installation directories
|
# Installation directories
|
||||||
PREFIX ?= /usr/local
|
PREFIX ?= /usr/local
|
||||||
@@ -25,28 +30,28 @@ DATADIR = $(PREFIX)/share/ssdd
|
|||||||
all: $(TARGET)
|
all: $(TARGET)
|
||||||
|
|
||||||
# Build the target
|
# Build the target
|
||||||
$(TARGET): $(RESOURCE_C) $(SRC)
|
$(TARGET): $(SRC)
|
||||||
$(CC) $(CFLAGS) -o $(TARGET) $(SRC) $(LDFLAGS)
|
$(CC) $(CPPFLAGS) $(CFLAGS) $(GTK_CFLAGS) $(VERSION_FLAGS) \
|
||||||
|
-o $(TARGET) $(SRC) $(LDFLAGS) $(GTK_LIBS)
|
||||||
|
|
||||||
# Compile resources
|
# Compile resources
|
||||||
$(RESOURCE_C) $(RESOURCE_H): $(RESOURCE_XML)
|
$(RESOURCE_C): $(RESOURCE_XML) $(RESOURCE_FILES)
|
||||||
glib-compile-resources $(RESOURCE_XML) --generate-source --target=$(RESOURCE_C)
|
glib-compile-resources $(RESOURCE_XML) --generate-source --target=$(RESOURCE_C)
|
||||||
glib-compile-resources $(RESOURCE_XML) --generate-header --target=$(RESOURCE_H)
|
|
||||||
|
|
||||||
# Install target
|
# Install target
|
||||||
install: $(TARGET)
|
install: $(TARGET)
|
||||||
install -d $(BINDIR)
|
install -d $(DESTDIR)$(BINDIR)
|
||||||
install -m 755 $(TARGET) $(BINDIR)
|
install -m 755 $(TARGET) $(DESTDIR)$(BINDIR)
|
||||||
install -d $(DATADIR)
|
install -d $(DESTDIR)$(DATADIR)
|
||||||
install -m 644 $(RESOURCE_XML) $(DATADIR)
|
install -m 644 $(RESOURCE_XML) $(DESTDIR)$(DATADIR)
|
||||||
|
|
||||||
# Uninstall target
|
# Uninstall target
|
||||||
uninstall:
|
uninstall:
|
||||||
rm -f $(BINDIR)/$(TARGET)
|
rm -f $(DESTDIR)$(BINDIR)/$(TARGET)
|
||||||
rm -rf $(DATADIR)
|
rm -rf $(DESTDIR)$(DATADIR)
|
||||||
|
|
||||||
# Clean target
|
# Clean target
|
||||||
clean:
|
clean:
|
||||||
rm -f $(TARGET) $(RESOURCE_C) $(RESOURCE_H)
|
rm -f $(TARGET) $(RESOURCE_C)
|
||||||
|
|
||||||
.PHONY: all clean install uninstall
|
.PHONY: all clean install uninstall
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
# ssdd: Simple Shutdown Dialog for Openbox
|
# ssdd: Simple Shutdown Dialog for Openbox
|
||||||
|
|
||||||
A simple Shutdown Dialog for Openbox written in C using GTK
|
A simple Shutdown Dialog for Openbox written in C using GTK 4
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
**Simple Shutdown Dialog (ssdd)** is a simple yet stylish shutdown dialog for Openbox, crafted in C using GTK.
|

|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
**Simple Shutdown Dialog (ssdd)** is a simple yet stylish shutdown dialog for Openbox, crafted in C using GTK 4.
|
||||||
|
|
||||||
## Why ssdd?
|
## Why ssdd?
|
||||||
|
|
||||||
@@ -14,15 +18,16 @@ Inspired by the elegant `ssd` from Sawfish, I decided to create my own tailored
|
|||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
* **Clean and Intuitive Interface:** ssdd presents a clear choice between Shutdown, Reboot, Logout, and Exit options.
|
- **Clean and Intuitive Interface:** ssdd presents clear options for Logout, Reboot, Shutdown, Switch User, Suspend, Hibernate, Settings, and Exit.
|
||||||
* **Clean and minimal code:** `ssdd` is built on a clean and minimal codebase, making it easy to maintain, understand, and extend.
|
- **Configurable Commands:** Easily customize the commands executed for each action via the settings dialog.
|
||||||
* **Lightweight and Efficient:** ssdd is designed to be fast and resource-friendly, perfectly suited for Openbox's minimalist philosophy.
|
- **Lightweight and Efficient:** Designed to be fast and resource-friendly, perfectly suited for Openbox's minimalist philosophy.
|
||||||
|
- **Modern GTK 4 Interface:** Built with GTK 4 for a modern look and feel.
|
||||||
|
|
||||||
## Dependencies and Compilation
|
## Dependencies and Compilation
|
||||||
|
|
||||||
ssdd requires:
|
ssdd requires:
|
||||||
|
|
||||||
* GTK+ 3.0
|
* GTK 4 development libraries
|
||||||
* Glib 2 development libraries
|
* Glib 2 development libraries
|
||||||
* gcc or clang
|
* gcc or clang
|
||||||
|
|
||||||
@@ -47,10 +52,10 @@ First generate the resources.
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Using GCC:
|
# Using GCC:
|
||||||
% gcc ssdd.c resources.c -o ssdd `pkg-config --cflags --libs gtk+-3.0`
|
% gcc ssdd.c resources.c -o ssdd `pkg-config --cflags --libs gtk4`
|
||||||
|
|
||||||
# Using Clang:
|
# Using Clang:
|
||||||
% clang ssdd.c resources.c -o ssdd `pkg-config --cflags --libs gtk+-3.0`
|
% clang ssdd.c resources.c -o ssdd `pkg-config --cflags --libs gtk4`
|
||||||
```
|
```
|
||||||
|
|
||||||
Place the `ssdd` binary in your `$PATH` (e.g., `~/bin`).
|
Place the `ssdd` binary in your `$PATH` (e.g., `~/bin`).
|
||||||
@@ -78,3 +83,4 @@ Place the `ssdd` binary in your `$PATH` (e.g., `~/bin`).
|
|||||||
### Contributing
|
### Contributing
|
||||||
|
|
||||||
Contributions are welcome! Feel free to open issues or submit pull requests.
|
Contributions are welcome! Feel free to open issues or submit pull requests.
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
[Desktop Entry]
|
||||||
|
Type=Application
|
||||||
|
Name=Simple Shutdown Dialog
|
||||||
|
Exec=ssdd
|
||||||
|
Icon=com.example.ssdd
|
||||||
|
Categories=Utility;
|
||||||
|
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 74 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 35 KiB |
@@ -1,346 +1,467 @@
|
|||||||
#include <gtk/gtk.h>
|
#include <errno.h>
|
||||||
#include <glib.h>
|
|
||||||
#include <glib/gstdio.h>
|
#include <glib/gstdio.h>
|
||||||
#include "resources.h"
|
#include <gtk/gtk.h>
|
||||||
|
|
||||||
// Function declarations (prototypes)
|
#define CONFIG_DIR_NAME "ssdd"
|
||||||
static void execute_command(const gchar *command);
|
#define CONFIG_FILE_NAME "config"
|
||||||
static void show_settings_dialog(GtkWidget *widget);
|
#define CONFIG_GROUP "Commands"
|
||||||
|
#define APP_VERSION "2.2"
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
ACTION_LOGOUT,
|
||||||
|
ACTION_REBOOT,
|
||||||
|
ACTION_SHUTDOWN,
|
||||||
|
ACTION_SWITCH_USER,
|
||||||
|
ACTION_SUSPEND,
|
||||||
|
ACTION_HIBERNATE,
|
||||||
|
N_CONFIG_ACTIONS,
|
||||||
|
ACTION_SETTINGS = N_CONFIG_ACTIONS,
|
||||||
|
ACTION_EXIT,
|
||||||
|
N_ACTIONS
|
||||||
|
} ActionId;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const gchar *label;
|
||||||
|
const gchar *icon_name;
|
||||||
|
const gchar *config_key;
|
||||||
|
const gchar *default_command;
|
||||||
|
} ActionSpec;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
gchar *commands[N_CONFIG_ACTIONS];
|
||||||
|
} AppState;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
AppState *state;
|
||||||
|
GtkWidget *entries[N_CONFIG_ACTIONS];
|
||||||
|
} SettingsData;
|
||||||
|
|
||||||
|
static const ActionSpec actions[N_ACTIONS] = {
|
||||||
|
[ACTION_LOGOUT] = {"Logout", "system-log-out-symbolic", "LOGOUT_COMMAND",
|
||||||
|
"openbox --exit"},
|
||||||
|
[ACTION_REBOOT] = {"Reboot", "system-reboot-symbolic", "REBOOT_COMMAND",
|
||||||
|
"systemctl reboot"},
|
||||||
|
[ACTION_SHUTDOWN] = {"Shutdown", "system-shutdown-symbolic",
|
||||||
|
"SHUTDOWN_COMMAND", "systemctl poweroff"},
|
||||||
|
[ACTION_SWITCH_USER] = {"Switch User", "system-users-symbolic",
|
||||||
|
"SWITCH_USER_COMMAND", "dm-tool switch-to-greeter"},
|
||||||
|
[ACTION_SUSPEND] = {"Suspend", "media-playback-pause-symbolic",
|
||||||
|
"SUSPEND_COMMAND", "systemctl suspend"},
|
||||||
|
[ACTION_HIBERNATE] = {"Hibernate", "document-save-symbolic",
|
||||||
|
"HIBERNATE_COMMAND", "systemctl hibernate"},
|
||||||
|
[ACTION_SETTINGS] = {"Settings", "preferences-system-symbolic", NULL, NULL},
|
||||||
|
[ACTION_EXIT] = {"Exit", "window-close-symbolic", NULL, NULL}};
|
||||||
|
|
||||||
|
G_STATIC_ASSERT(G_N_ELEMENTS(actions) == N_ACTIONS);
|
||||||
|
|
||||||
|
static void activate(GtkApplication *app, gpointer user_data);
|
||||||
|
static void app_state_free(gpointer data);
|
||||||
|
static void button_clicked(GtkButton *button, gpointer user_data);
|
||||||
|
static void create_button(GtkWidget *grid, ActionId action_id);
|
||||||
|
static void execute_command(const gchar *command, GtkWindow *parent);
|
||||||
|
static gchar *get_config_path(GError **error);
|
||||||
|
static void load_configuration(AppState *state);
|
||||||
|
static gboolean on_key_pressed(GtkEventControllerKey *controller, guint keyval,
|
||||||
|
guint keycode, GdkModifierType state,
|
||||||
|
gpointer user_data);
|
||||||
|
static void on_confirmation_response(GtkDialog *dialog, gint response_id,
|
||||||
|
gpointer user_data);
|
||||||
|
static void on_save_button_clicked(GtkButton *button, gpointer user_data);
|
||||||
|
static gboolean save_configuration(const gchar *const commands[],
|
||||||
|
GError **error);
|
||||||
static void show_about_tab(GtkWidget *box);
|
static void show_about_tab(GtkWidget *box);
|
||||||
static void show_settings_tab(GtkWidget *box);
|
static void show_confirmation_dialog(GtkWindow *parent, const gchar *label,
|
||||||
static void button_clicked(GtkWidget *widget, gpointer data);
|
const gchar *command);
|
||||||
static gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data);
|
static void show_message_dialog(GtkWindow *parent, GtkMessageType type,
|
||||||
static void show_confirmation_dialog(GtkWidget *widget, const gchar *label, const gchar *command);
|
GtkButtonsType buttons, const gchar *title,
|
||||||
static void create_button(GtkWidget *grid, GtkApplication *app, const gchar *label_text, const gchar *icon_name, const gchar *command, int pos);
|
const gchar *message);
|
||||||
static void save_configuration(const gchar *commands[]);
|
static void show_settings_dialog(GtkWindow *parent, AppState *state);
|
||||||
static void load_configuration(gchar *commands[]);
|
static void show_settings_tab(GtkWidget *box, AppState *state);
|
||||||
|
|
||||||
#define CONFIG_PATH g_build_filename(g_get_home_dir(), ".config/ssdd/config", NULL)
|
static void app_state_free(gpointer data) {
|
||||||
#define CONFIG_DIR g_build_filename(g_get_home_dir(), ".config/ssdd", NULL)
|
AppState *state = data;
|
||||||
|
|
||||||
static void execute_command(const gchar *command) {
|
for (guint i = 0; i < N_CONFIG_ACTIONS; i++)
|
||||||
GError *error = NULL;
|
g_free(state->commands[i]);
|
||||||
gboolean ret = g_spawn_command_line_async(command, &error);
|
|
||||||
if (!ret) {
|
g_free(state);
|
||||||
GtkWidget *dialog;
|
|
||||||
dialog = gtk_message_dialog_new(NULL,
|
|
||||||
GTK_DIALOG_DESTROY_WITH_PARENT,
|
|
||||||
GTK_MESSAGE_ERROR,
|
|
||||||
GTK_BUTTONS_CLOSE,
|
|
||||||
"Error executing command: %s\n%s",
|
|
||||||
command,
|
|
||||||
error->message);
|
|
||||||
gtk_dialog_run(GTK_DIALOG(dialog));
|
|
||||||
gtk_widget_destroy(dialog);
|
|
||||||
g_error_free(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void show_settings_dialog(GtkWidget *widget) {
|
static void app_state_set_command(AppState *state, guint index,
|
||||||
GtkWidget *dialog;
|
const gchar *command) {
|
||||||
GtkWidget *content_area;
|
g_return_if_fail(index < N_CONFIG_ACTIONS);
|
||||||
GtkWidget *notebook;
|
|
||||||
GtkWidget *settings_tab;
|
|
||||||
GtkWidget *about_tab;
|
|
||||||
GtkWidget *close_button;
|
|
||||||
|
|
||||||
dialog = gtk_dialog_new_with_buttons("Settings",
|
g_free(state->commands[index]);
|
||||||
NULL,
|
state->commands[index] = g_strdup(command);
|
||||||
GTK_DIALOG_DESTROY_WITH_PARENT,
|
|
||||||
NULL);
|
|
||||||
content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
|
|
||||||
|
|
||||||
notebook = gtk_notebook_new();
|
|
||||||
gtk_box_pack_start(GTK_BOX(content_area), notebook, TRUE, TRUE, 0);
|
|
||||||
|
|
||||||
settings_tab = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
|
|
||||||
gtk_container_set_border_width(GTK_CONTAINER(settings_tab), 10);
|
|
||||||
gtk_notebook_append_page(GTK_NOTEBOOK(notebook), settings_tab, gtk_label_new("Settings"));
|
|
||||||
|
|
||||||
about_tab = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
|
|
||||||
gtk_container_set_border_width(GTK_CONTAINER(about_tab), 10);
|
|
||||||
gtk_notebook_append_page(GTK_NOTEBOOK(notebook), about_tab, gtk_label_new("About"));
|
|
||||||
|
|
||||||
show_settings_tab(settings_tab);
|
|
||||||
show_about_tab(about_tab);
|
|
||||||
|
|
||||||
close_button = gtk_button_new_with_label("Close");
|
|
||||||
g_signal_connect_swapped(close_button, "clicked", G_CALLBACK(gtk_widget_destroy), dialog);
|
|
||||||
gtk_box_pack_start(GTK_BOX(content_area), close_button, FALSE, FALSE, 10);
|
|
||||||
|
|
||||||
gtk_widget_show_all(dialog);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void show_settings_tab(GtkWidget *box) {
|
static gchar *get_config_path(GError **error) {
|
||||||
const gchar *labels[] = {
|
g_autofree gchar *config_dir =
|
||||||
"Logout Command",
|
g_build_filename(g_get_user_config_dir(), CONFIG_DIR_NAME, NULL);
|
||||||
"Reboot Command",
|
|
||||||
"Shutdown Command",
|
|
||||||
"Switch User Command",
|
|
||||||
"Suspend Command",
|
|
||||||
"Hibernate Command"
|
|
||||||
};
|
|
||||||
|
|
||||||
gchar *commands[6];
|
if (g_mkdir_with_parents(config_dir, 0755) == -1) {
|
||||||
load_configuration(commands);
|
gint saved_errno = errno;
|
||||||
|
|
||||||
GtkWidget *grid = gtk_grid_new();
|
g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(saved_errno),
|
||||||
gtk_grid_set_row_spacing(GTK_GRID(grid), 10); // Space between rows
|
"Could not create configuration directory '%s': %s", config_dir,
|
||||||
gtk_grid_set_column_spacing(GTK_GRID(grid), 10); // Space between columns
|
g_strerror(saved_errno));
|
||||||
gtk_box_pack_start(GTK_BOX(box), grid, TRUE, TRUE, 0);
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < 6; i++) {
|
return g_build_filename(config_dir, CONFIG_FILE_NAME, NULL);
|
||||||
GtkWidget *label = gtk_label_new(labels[i]);
|
}
|
||||||
GtkWidget *entry = gtk_entry_new();
|
|
||||||
gtk_entry_set_text(GTK_ENTRY(entry), commands[i]);
|
|
||||||
gtk_widget_set_hexpand(entry, TRUE); // Allow the entry to expand
|
|
||||||
gtk_widget_set_halign(label, GTK_ALIGN_END); // Align the label to the end
|
|
||||||
|
|
||||||
gtk_grid_attach(GTK_GRID(grid), label, 0, i, 1, 1);
|
static gboolean save_configuration(const gchar *const commands[],
|
||||||
gtk_grid_attach(GTK_GRID(grid), entry, 1, i, 1, 1);
|
GError **error) {
|
||||||
|
g_autofree gchar *config_path = get_config_path(error);
|
||||||
|
g_autoptr(GKeyFile) key_file = NULL;
|
||||||
|
|
||||||
|
if (config_path == NULL)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
key_file = g_key_file_new();
|
||||||
|
|
||||||
|
for (guint i = 0; i < N_CONFIG_ACTIONS; i++) {
|
||||||
|
g_key_file_set_string(key_file, CONFIG_GROUP, actions[i].config_key,
|
||||||
|
commands[i] != NULL ? commands[i] : "");
|
||||||
|
}
|
||||||
|
|
||||||
|
return g_key_file_save_to_file(key_file, config_path, error);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void load_configuration(AppState *state) {
|
||||||
|
g_autoptr(GError) error = NULL;
|
||||||
|
g_autofree gchar *config_path = get_config_path(&error);
|
||||||
|
g_autoptr(GKeyFile) key_file = g_key_file_new();
|
||||||
|
gboolean needs_save = FALSE;
|
||||||
|
|
||||||
|
if (config_path == NULL ||
|
||||||
|
!g_key_file_load_from_file(
|
||||||
|
key_file, config_path,
|
||||||
|
G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error)) {
|
||||||
|
g_warning("Could not load configuration%s%s. Using defaults.",
|
||||||
|
error != NULL ? ": " : "", error != NULL ? error->message : "");
|
||||||
|
|
||||||
|
for (guint i = 0; i < N_CONFIG_ACTIONS; i++)
|
||||||
|
app_state_set_command(state, i, actions[i].default_command);
|
||||||
|
|
||||||
|
if (config_path != NULL) {
|
||||||
|
const gchar *commands[N_CONFIG_ACTIONS];
|
||||||
|
g_autoptr(GError) save_error = NULL;
|
||||||
|
|
||||||
|
for (guint i = 0; i < N_CONFIG_ACTIONS; i++)
|
||||||
|
commands[i] = state->commands[i];
|
||||||
|
|
||||||
|
if (!save_configuration(commands, &save_error))
|
||||||
|
g_warning("Could not save default configuration: %s",
|
||||||
|
save_error->message);
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < 6; i++) {
|
for (guint i = 0; i < N_CONFIG_ACTIONS; i++) {
|
||||||
g_free(commands[i]);
|
g_autofree gchar *command = g_key_file_get_string(
|
||||||
|
key_file, CONFIG_GROUP, actions[i].config_key, NULL);
|
||||||
|
|
||||||
|
if (command == NULL || *command == '\0') {
|
||||||
|
g_warning(
|
||||||
|
"Configuration key '%s' is missing or empty; using default '%s'",
|
||||||
|
actions[i].config_key, actions[i].default_command);
|
||||||
|
app_state_set_command(state, i, actions[i].default_command);
|
||||||
|
needs_save = TRUE;
|
||||||
|
} else {
|
||||||
|
app_state_set_command(state, i, command);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (needs_save) {
|
||||||
|
const gchar *commands[N_CONFIG_ACTIONS];
|
||||||
|
g_autoptr(GError) save_error = NULL;
|
||||||
|
|
||||||
|
for (guint i = 0; i < N_CONFIG_ACTIONS; i++)
|
||||||
|
commands[i] = state->commands[i];
|
||||||
|
|
||||||
|
if (!save_configuration(commands, &save_error))
|
||||||
|
g_warning("Could not repair configuration: %s", save_error->message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void show_message_dialog(GtkWindow *parent, GtkMessageType type,
|
||||||
|
GtkButtonsType buttons, const gchar *title,
|
||||||
|
const gchar *message) {
|
||||||
|
GtkWidget *dialog = gtk_message_dialog_new(
|
||||||
|
parent, GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, type, buttons,
|
||||||
|
"%s", message);
|
||||||
|
|
||||||
|
gtk_window_set_title(GTK_WINDOW(dialog), title);
|
||||||
|
g_signal_connect(dialog, "response", G_CALLBACK(gtk_window_destroy), NULL);
|
||||||
|
gtk_window_present(GTK_WINDOW(dialog));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void execute_command(const gchar *command, GtkWindow *parent) {
|
||||||
|
g_autoptr(GError) error = NULL;
|
||||||
|
|
||||||
|
if (!g_spawn_command_line_async(command, &error)) {
|
||||||
|
g_autofree gchar *message = g_strdup_printf(
|
||||||
|
"Could not execute:\n'%s'\n\nReason: %s", command, error->message);
|
||||||
|
|
||||||
|
show_message_dialog(parent, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
|
||||||
|
"Error Executing Command", message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void show_about_tab(GtkWidget *box) {
|
static void show_about_tab(GtkWidget *box) {
|
||||||
GtkWidget *label;
|
static const gchar about_text[] =
|
||||||
GtkWidget *image;
|
"\n<b>Simple ShutDown Dialog</b>\n\n"
|
||||||
const gchar *about_text =
|
"<b>Version:</b> " APP_VERSION "\n"
|
||||||
"\n<b>About Simple ShutDown Dialog</b>\n\n"
|
"<b>Author:</b> kekePower\n"
|
||||||
"<b>Version:</b> 1.5\n"
|
"<b>URL: </b><a href=\"https://git.kekepower.com/kekePower/ssdd\">"
|
||||||
"<b>Author:</b> kekePower\n"
|
"https://git.kekepower.com/kekePower/ssdd</a>\n"
|
||||||
"<b>URL: </b><a href=\"https://git.kekepower.com/kekePower/ssdd\">https://git.kekepower.com/kekePower/ssdd</a>\n"
|
"<b>Description:</b> A Simple ShutDown Dialog for session management.\n";
|
||||||
"<b>Description:</b> This is a Simple ShutDown Dialog for Openbox.\n";
|
GtkWidget *image = gtk_image_new_from_resource("/org/gtk/ssdd/ssdd-icon.png");
|
||||||
|
GtkWidget *label = gtk_label_new(NULL);
|
||||||
|
|
||||||
image = gtk_image_new_from_resource("/org/gtk/ssdd/ssdd-icon.png");
|
gtk_image_set_pixel_size(GTK_IMAGE(image), 128);
|
||||||
gtk_image_set_pixel_size(GTK_IMAGE(image), 250);
|
gtk_widget_set_halign(image, GTK_ALIGN_CENTER);
|
||||||
gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0);
|
gtk_widget_set_margin_bottom(image, 10);
|
||||||
|
gtk_box_append(GTK_BOX(box), image);
|
||||||
|
|
||||||
label = gtk_label_new(NULL);
|
gtk_label_set_markup(GTK_LABEL(label), about_text);
|
||||||
gtk_label_set_markup(GTK_LABEL(label), about_text);
|
gtk_label_set_selectable(GTK_LABEL(label), TRUE);
|
||||||
gtk_label_set_selectable(GTK_LABEL(label), TRUE);
|
gtk_label_set_wrap(GTK_LABEL(label), TRUE);
|
||||||
gtk_widget_set_halign(label, GTK_ALIGN_START);
|
gtk_widget_set_halign(label, GTK_ALIGN_CENTER);
|
||||||
gtk_widget_set_valign(label, GTK_ALIGN_START);
|
gtk_widget_set_valign(label, GTK_ALIGN_START);
|
||||||
gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
|
gtk_box_append(GTK_BOX(box), label);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void button_clicked(GtkWidget *widget, gpointer data) {
|
static void show_settings_tab(GtkWidget *box, AppState *state) {
|
||||||
const gchar *command = (const gchar *)data;
|
GtkWidget *grid = gtk_grid_new();
|
||||||
const gchar *label = g_object_get_data(G_OBJECT(widget), "label");
|
GtkWidget *save_button = gtk_button_new_with_label("Save");
|
||||||
|
SettingsData *settings = g_new0(SettingsData, 1);
|
||||||
|
|
||||||
if (g_strcmp0(command, "exit") == 0) {
|
settings->state = state;
|
||||||
g_application_quit(G_APPLICATION(g_object_get_data(G_OBJECT(widget), "app")));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (g_strcmp0(command, "settings") == 0) {
|
gtk_grid_set_row_spacing(GTK_GRID(grid), 10);
|
||||||
show_settings_dialog(widget);
|
gtk_grid_set_column_spacing(GTK_GRID(grid), 10);
|
||||||
} else {
|
gtk_box_append(GTK_BOX(box), grid);
|
||||||
GtkWidget *window = gtk_widget_get_toplevel(widget);
|
|
||||||
show_confirmation_dialog(window, label, command);
|
for (guint i = 0; i < N_CONFIG_ACTIONS; i++) {
|
||||||
}
|
g_autofree gchar *label_text =
|
||||||
|
g_strdup_printf("%s Command:", actions[i].label);
|
||||||
|
GtkWidget *label = gtk_label_new(label_text);
|
||||||
|
GtkWidget *entry = gtk_entry_new();
|
||||||
|
|
||||||
|
gtk_editable_set_text(GTK_EDITABLE(entry), state->commands[i]);
|
||||||
|
gtk_widget_set_hexpand(entry, TRUE);
|
||||||
|
gtk_widget_set_halign(label, GTK_ALIGN_END);
|
||||||
|
gtk_grid_attach(GTK_GRID(grid), label, 0, (gint)i, 1, 1);
|
||||||
|
gtk_grid_attach(GTK_GRID(grid), entry, 1, (gint)i, 1, 1);
|
||||||
|
settings->entries[i] = entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
gtk_widget_set_halign(save_button, GTK_ALIGN_END);
|
||||||
|
gtk_widget_set_margin_top(save_button, 10);
|
||||||
|
g_object_set_data_full(G_OBJECT(save_button), "settings", settings, g_free);
|
||||||
|
g_signal_connect(save_button, "clicked", G_CALLBACK(on_save_button_clicked),
|
||||||
|
NULL);
|
||||||
|
gtk_box_append(GTK_BOX(box), save_button);
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data) {
|
static void show_settings_dialog(GtkWindow *parent, AppState *state) {
|
||||||
if (event->keyval == GDK_KEY_Escape) {
|
GtkWidget *dialog = gtk_dialog_new();
|
||||||
g_application_quit(G_APPLICATION(data));
|
GtkWidget *content = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
||||||
return TRUE; // Event handled
|
GtkWidget *notebook = gtk_notebook_new();
|
||||||
}
|
GtkWidget *settings_tab = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
|
||||||
return FALSE; // Event not handled
|
GtkWidget *about_tab = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
|
||||||
|
|
||||||
|
gtk_window_set_title(GTK_WINDOW(dialog), "Settings");
|
||||||
|
gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
|
||||||
|
gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
|
||||||
|
gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
|
||||||
|
gtk_window_set_child(GTK_WINDOW(dialog), content);
|
||||||
|
gtk_box_append(GTK_BOX(content), notebook);
|
||||||
|
|
||||||
|
gtk_widget_set_margin_top(settings_tab, 10);
|
||||||
|
gtk_widget_set_margin_bottom(settings_tab, 10);
|
||||||
|
gtk_widget_set_margin_start(settings_tab, 10);
|
||||||
|
gtk_widget_set_margin_end(settings_tab, 10);
|
||||||
|
gtk_notebook_append_page(GTK_NOTEBOOK(notebook), settings_tab,
|
||||||
|
gtk_label_new("Settings"));
|
||||||
|
show_settings_tab(settings_tab, state);
|
||||||
|
|
||||||
|
gtk_widget_set_margin_top(about_tab, 10);
|
||||||
|
gtk_widget_set_margin_bottom(about_tab, 10);
|
||||||
|
gtk_widget_set_margin_start(about_tab, 10);
|
||||||
|
gtk_widget_set_margin_end(about_tab, 10);
|
||||||
|
gtk_notebook_append_page(GTK_NOTEBOOK(notebook), about_tab,
|
||||||
|
gtk_label_new("About"));
|
||||||
|
show_about_tab(about_tab);
|
||||||
|
|
||||||
|
gtk_dialog_add_button(GTK_DIALOG(dialog), "_Close", GTK_RESPONSE_CLOSE);
|
||||||
|
g_signal_connect(dialog, "response", G_CALLBACK(gtk_window_destroy), NULL);
|
||||||
|
gtk_window_present(GTK_WINDOW(dialog));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void show_confirmation_dialog(GtkWidget *parent_window, const gchar *label, const gchar *command) {
|
static void on_save_button_clicked(GtkButton *button,
|
||||||
GtkWidget *dialog;
|
gpointer user_data G_GNUC_UNUSED) {
|
||||||
gint response;
|
SettingsData *settings = g_object_get_data(G_OBJECT(button), "settings");
|
||||||
|
const gchar *commands[N_CONFIG_ACTIONS];
|
||||||
|
g_autoptr(GError) error = NULL;
|
||||||
|
GtkWindow *parent = GTK_WINDOW(gtk_widget_get_root(GTK_WIDGET(button)));
|
||||||
|
|
||||||
gchar *message = g_strdup_printf("Are you sure you want to %s?", label);
|
for (guint i = 0; i < N_CONFIG_ACTIONS; i++) {
|
||||||
|
GtkEditable *entry = GTK_EDITABLE(settings->entries[i]);
|
||||||
|
const gchar *command = gtk_editable_get_text(entry);
|
||||||
|
|
||||||
dialog = gtk_dialog_new();
|
if (*command == '\0') {
|
||||||
gtk_window_set_title(GTK_WINDOW(dialog), "Confirmation");
|
gtk_editable_set_text(entry, actions[i].default_command);
|
||||||
|
command = gtk_editable_get_text(entry);
|
||||||
gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent_window));
|
|
||||||
|
|
||||||
gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
|
|
||||||
gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
|
|
||||||
|
|
||||||
GtkWidget *content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
|
|
||||||
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
|
|
||||||
gtk_container_add(GTK_CONTAINER(content_area), box);
|
|
||||||
|
|
||||||
GtkWidget *image = gtk_image_new_from_icon_name("dialog-warning", GTK_ICON_SIZE_DIALOG);
|
|
||||||
gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0);
|
|
||||||
|
|
||||||
GtkWidget *label_widget = gtk_label_new(message);
|
|
||||||
gtk_box_pack_start(GTK_BOX(box), label_widget, TRUE, TRUE, 0);
|
|
||||||
|
|
||||||
gtk_dialog_add_buttons(GTK_DIALOG(dialog),
|
|
||||||
"Yes", GTK_RESPONSE_YES,
|
|
||||||
"No", GTK_RESPONSE_NO,
|
|
||||||
NULL);
|
|
||||||
|
|
||||||
GtkWidget *button_box = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
|
|
||||||
gtk_box_set_spacing(GTK_BOX(button_box), 10);
|
|
||||||
gtk_widget_set_margin_top(button_box, 10);
|
|
||||||
|
|
||||||
gtk_widget_show_all(dialog);
|
|
||||||
|
|
||||||
response = gtk_dialog_run(GTK_DIALOG(dialog));
|
|
||||||
gtk_widget_destroy(dialog);
|
|
||||||
g_free(message);
|
|
||||||
|
|
||||||
if (response == GTK_RESPONSE_YES) {
|
|
||||||
execute_command(command);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
commands[i] = command;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!save_configuration(commands, &error)) {
|
||||||
|
g_autofree gchar *message = g_strdup_printf(
|
||||||
|
"Could not save the settings.\n\nReason: %s", error->message);
|
||||||
|
show_message_dialog(parent, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
|
||||||
|
"Error Saving Settings", message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (guint i = 0; i < N_CONFIG_ACTIONS; i++)
|
||||||
|
app_state_set_command(settings->state, i, commands[i]);
|
||||||
|
|
||||||
|
show_message_dialog(parent, GTK_MESSAGE_INFO, GTK_BUTTONS_OK,
|
||||||
|
"Settings Saved", "Settings saved successfully.");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void create_button(GtkWidget *grid, GtkApplication *app, const gchar *label_text, const gchar *icon_name, const gchar *command, int pos) {
|
static void show_confirmation_dialog(GtkWindow *parent, const gchar *label,
|
||||||
GtkWidget *button;
|
const gchar *command) {
|
||||||
GtkWidget *box;
|
g_autofree gchar *message =
|
||||||
GtkWidget *image;
|
g_strdup_printf("Are you sure you want to %s?", label);
|
||||||
GtkWidget *label;
|
GtkWidget *dialog = gtk_message_dialog_new(
|
||||||
|
parent, GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
|
||||||
|
GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, "%s", message);
|
||||||
|
|
||||||
button = gtk_button_new();
|
gtk_window_set_title(GTK_WINDOW(dialog), "Confirmation");
|
||||||
box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
|
g_object_set_data_full(G_OBJECT(dialog), "command", g_strdup(command),
|
||||||
gtk_container_add(GTK_CONTAINER(button), box);
|
g_free);
|
||||||
|
g_signal_connect(dialog, "response", G_CALLBACK(on_confirmation_response),
|
||||||
image = gtk_image_new_from_icon_name(icon_name, GTK_ICON_SIZE_BUTTON);
|
NULL);
|
||||||
gtk_box_pack_start(GTK_BOX(box), image, TRUE, TRUE, 0);
|
gtk_window_present(GTK_WINDOW(dialog));
|
||||||
|
|
||||||
label = gtk_label_new(label_text);
|
|
||||||
gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
|
|
||||||
|
|
||||||
gtk_widget_set_margin_top(box, 5);
|
|
||||||
gtk_widget_set_margin_bottom(box, 5);
|
|
||||||
gtk_widget_set_margin_start(box, 5);
|
|
||||||
gtk_widget_set_margin_end(box, 5);
|
|
||||||
|
|
||||||
g_object_set_data(G_OBJECT(button), "app", app);
|
|
||||||
g_object_set_data(G_OBJECT(button), "label", (gpointer)label_text);
|
|
||||||
g_signal_connect(button, "clicked", G_CALLBACK(button_clicked), (gpointer)command);
|
|
||||||
gtk_grid_attach(GTK_GRID(grid), button, pos % 4, pos / 4, 1, 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void save_configuration(const gchar *commands[]) {
|
static void on_confirmation_response(GtkDialog *dialog, gint response_id,
|
||||||
GError *error = NULL;
|
gpointer user_data G_GNUC_UNUSED) {
|
||||||
g_mkdir_with_parents(CONFIG_DIR, 0755);
|
if (response_id == GTK_RESPONSE_YES) {
|
||||||
|
const gchar *command = g_object_get_data(G_OBJECT(dialog), "command");
|
||||||
|
GtkWindow *parent = gtk_window_get_transient_for(GTK_WINDOW(dialog));
|
||||||
|
|
||||||
gchar *config_data = g_strjoinv("\n", (gchar **)commands);
|
if (command != NULL && parent != NULL)
|
||||||
g_file_set_contents(CONFIG_PATH, config_data, -1, &error);
|
execute_command(command, parent);
|
||||||
|
else
|
||||||
|
g_warning("Confirmation dialog is missing its command or parent");
|
||||||
|
}
|
||||||
|
|
||||||
if (error) {
|
gtk_window_destroy(GTK_WINDOW(dialog));
|
||||||
g_warning("Failed to save configuration: %s", error->message);
|
|
||||||
g_error_free(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
g_free(config_data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void load_configuration(gchar *commands[]) {
|
static void button_clicked(GtkButton *button,
|
||||||
GError *error = NULL;
|
gpointer user_data G_GNUC_UNUSED) {
|
||||||
gchar *config_data = NULL;
|
ActionId action_id = (ActionId)(GPOINTER_TO_UINT(g_object_get_data(
|
||||||
|
G_OBJECT(button), "action-id")) -
|
||||||
|
1);
|
||||||
|
GtkWindow *window = GTK_WINDOW(gtk_widget_get_root(GTK_WIDGET(button)));
|
||||||
|
GtkApplication *app = gtk_window_get_application(window);
|
||||||
|
AppState *state = g_object_get_data(G_OBJECT(app), "app-state");
|
||||||
|
|
||||||
g_mkdir_with_parents(CONFIG_DIR, 0755);
|
g_return_if_fail(action_id >= 0 && action_id < N_ACTIONS);
|
||||||
|
|
||||||
if (!g_file_test(CONFIG_PATH, G_FILE_TEST_EXISTS)) {
|
if (action_id == ACTION_EXIT) {
|
||||||
const gchar *default_commands[] = {
|
g_application_quit(G_APPLICATION(app));
|
||||||
"openbox --exit",
|
} else if (action_id == ACTION_SETTINGS) {
|
||||||
"systemctl reboot",
|
show_settings_dialog(window, state);
|
||||||
"systemctl poweroff",
|
} else {
|
||||||
"dm-tool switch-to-greeter",
|
show_confirmation_dialog(window, actions[action_id].label,
|
||||||
"systemctl suspend",
|
state->commands[action_id]);
|
||||||
"systemctl hibernate"
|
}
|
||||||
};
|
|
||||||
save_configuration(default_commands);
|
|
||||||
}
|
|
||||||
|
|
||||||
g_file_get_contents(CONFIG_PATH, &config_data, NULL, &error);
|
|
||||||
|
|
||||||
if (error) {
|
|
||||||
g_warning("Failed to load configuration: %s", error->message);
|
|
||||||
g_error_free(error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
gchar **lines = g_strsplit(config_data, "\n", 6);
|
|
||||||
for (int i = 0; i < 6; i++) {
|
|
||||||
commands[i] = g_strdup(lines[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
g_strfreev(lines);
|
|
||||||
g_free(config_data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void activate(GtkApplication *app, gpointer user_data) {
|
static gboolean on_key_pressed(GtkEventControllerKey *controller G_GNUC_UNUSED,
|
||||||
GtkWidget *window;
|
guint keyval, guint keycode G_GNUC_UNUSED,
|
||||||
GtkWidget *grid;
|
GdkModifierType state G_GNUC_UNUSED,
|
||||||
gchar *commands[6];
|
gpointer user_data) {
|
||||||
load_configuration(commands);
|
if (keyval != GDK_KEY_Escape)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
const gchar *icons[] = {
|
g_application_quit(G_APPLICATION(user_data));
|
||||||
"system-log-out",
|
return TRUE;
|
||||||
"view-refresh",
|
}
|
||||||
"system-shutdown",
|
|
||||||
"system-users",
|
|
||||||
"media-playback-pause",
|
|
||||||
"media-playback-stop",
|
|
||||||
"preferences-system",
|
|
||||||
"application-exit"
|
|
||||||
};
|
|
||||||
const gchar *labels[] = {
|
|
||||||
"Logout",
|
|
||||||
"Reboot",
|
|
||||||
"Shutdown",
|
|
||||||
"Switch User",
|
|
||||||
"Suspend",
|
|
||||||
"Hibernate",
|
|
||||||
"Settings",
|
|
||||||
"Exit"
|
|
||||||
};
|
|
||||||
|
|
||||||
window = gtk_application_window_new(app);
|
static void create_button(GtkWidget *grid, ActionId action_id) {
|
||||||
gtk_window_set_title(GTK_WINDOW(window), "Simple ShutDown Dialog");
|
const ActionSpec *action = &actions[action_id];
|
||||||
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
|
GtkWidget *button = gtk_button_new();
|
||||||
|
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
|
||||||
|
GtkWidget *image = gtk_image_new_from_icon_name(action->icon_name);
|
||||||
|
GtkWidget *label = gtk_label_new(action->label);
|
||||||
|
|
||||||
GError *error = NULL;
|
gtk_widget_set_hexpand(button, TRUE);
|
||||||
GdkPixbuf *icon_pixbuf = gdk_pixbuf_new_from_resource("/org/gtk/ssdd/ssdd-icon.png", &error);
|
gtk_widget_set_vexpand(button, TRUE);
|
||||||
if (icon_pixbuf) {
|
gtk_widget_set_valign(box, GTK_ALIGN_CENTER);
|
||||||
gtk_window_set_icon(GTK_WINDOW(window), icon_pixbuf);
|
gtk_widget_set_halign(box, GTK_ALIGN_CENTER);
|
||||||
g_object_unref(icon_pixbuf);
|
gtk_button_set_child(GTK_BUTTON(button), box);
|
||||||
} else {
|
gtk_image_set_icon_size(GTK_IMAGE(image), GTK_ICON_SIZE_LARGE);
|
||||||
g_warning("Failed to load icon: %s", error->message);
|
gtk_box_append(GTK_BOX(box), image);
|
||||||
g_error_free(error);
|
gtk_box_append(GTK_BOX(box), label);
|
||||||
}
|
|
||||||
|
|
||||||
g_signal_connect(window, "key-press-event", G_CALLBACK(on_key_press), app);
|
g_object_set_data(G_OBJECT(button), "action-id",
|
||||||
|
GUINT_TO_POINTER((guint)action_id + 1));
|
||||||
|
g_signal_connect(button, "clicked", G_CALLBACK(button_clicked), NULL);
|
||||||
|
gtk_grid_attach(GTK_GRID(grid), button, (gint)action_id % 4,
|
||||||
|
(gint)action_id / 4, 1, 1);
|
||||||
|
}
|
||||||
|
|
||||||
grid = gtk_grid_new();
|
static void activate(GtkApplication *app, gpointer user_data G_GNUC_UNUSED) {
|
||||||
gtk_grid_set_row_spacing(GTK_GRID(grid), 0);
|
GtkWindow *existing_window = gtk_application_get_active_window(app);
|
||||||
gtk_grid_set_column_spacing(GTK_GRID(grid), 0);
|
|
||||||
gtk_container_add(GTK_CONTAINER(window), grid);
|
|
||||||
|
|
||||||
for (int i = 0; i < 6; i++) {
|
if (existing_window != NULL) {
|
||||||
create_button(grid, app, labels[i], icons[i], commands[i], i);
|
gtk_window_present(existing_window);
|
||||||
g_free(commands[i]); // Free the memory allocated for commands
|
return;
|
||||||
}
|
}
|
||||||
create_button(grid, app, labels[6], icons[6], "settings", 6);
|
|
||||||
create_button(grid, app, labels[7], icons[7], "exit", 7);
|
|
||||||
|
|
||||||
gtk_widget_show_all(window);
|
GtkWidget *window = gtk_application_window_new(app);
|
||||||
|
GtkWidget *grid = gtk_grid_new();
|
||||||
|
|
||||||
|
gtk_window_set_title(GTK_WINDOW(window), "Simple ShutDown Dialog");
|
||||||
|
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
|
||||||
|
gtk_grid_set_row_spacing(GTK_GRID(grid), 5);
|
||||||
|
gtk_grid_set_column_spacing(GTK_GRID(grid), 5);
|
||||||
|
gtk_widget_set_margin_top(grid, 15);
|
||||||
|
gtk_widget_set_margin_bottom(grid, 15);
|
||||||
|
gtk_widget_set_margin_start(grid, 15);
|
||||||
|
gtk_widget_set_margin_end(grid, 15);
|
||||||
|
gtk_window_set_child(GTK_WINDOW(window), grid);
|
||||||
|
|
||||||
|
for (guint i = 0; i < N_ACTIONS; i++)
|
||||||
|
create_button(grid, (ActionId)i);
|
||||||
|
|
||||||
|
GtkEventController *key_controller = gtk_event_controller_key_new();
|
||||||
|
gtk_widget_add_controller(window, key_controller);
|
||||||
|
g_signal_connect(key_controller, "key-pressed", G_CALLBACK(on_key_pressed),
|
||||||
|
app);
|
||||||
|
gtk_window_present(GTK_WINDOW(window));
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
GtkApplication *app;
|
g_autoptr(GtkApplication) app =
|
||||||
int status;
|
gtk_application_new("com.kekepower.ssdd", G_APPLICATION_DEFAULT_FLAGS);
|
||||||
|
AppState *state = g_new0(AppState, 1);
|
||||||
|
|
||||||
g_resources_register(resources_get_resource());
|
load_configuration(state);
|
||||||
|
g_object_set_data_full(G_OBJECT(app), "app-state", state, app_state_free);
|
||||||
|
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
|
||||||
|
|
||||||
app = gtk_application_new("org.gtk.ssdd", G_APPLICATION_DEFAULT_FLAGS);
|
return g_application_run(G_APPLICATION(app), argc, argv);
|
||||||
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
|
|
||||||
status = g_application_run(G_APPLICATION(app), argc, argv);
|
|
||||||
g_object_unref(app);
|
|
||||||
|
|
||||||
return status;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user