34 Commits

Author SHA1 Message Date
kekePower 9583abeaf1 Merge version 2.2 modernization
Modernize the C implementation, improve configuration handling, simplify resource generation, preserve both repositories' ignore rules, and bump the application version to 2.2.
2026-08-08 23:03:36 +02:00
kekePower d286cecaef Merge GitHub master into version 2.2 branch 2026-08-08 23:03:15 +02:00
kekePower 15276f20fe Modernize ssdd and bump version to 2.2 2026-08-08 22:59:52 +02:00
kekePower 13c06c274d refactor: extract constants, deduplicate defaults, improve config loading
- Define NUM_COMMANDS and DEFAULT_COMMANDS to eliminate repeated arrays
- Initialize commands to NULL and fall back to defaults on config read errors
- Skip empty lines and handle malformed entries gracefully in load_configuration
- Show a "Configuration saved." toast on successful settings save
- Fix window parent lookup via gtk_application_get_active_window
2026-05-20 22:46:10 +02:00
kekePower 94e911a4c5 Update to version 2.1 and hopefully fix the bugs 2025-04-14 23:46:02 +02:00
kekePower c24db4dcbd Update to version 2.0. Port to GTK4. 2024-09-13 01:49:48 +02:00
kekePower 18e6f1a70f Update to version 1.6. Change format of config file to use KEY=VALUE. 2024-08-10 09:35:15 +02:00
kekePower 3aac31a8e5 Cleanup 2024-08-09 23:20:01 +02:00
kekePower 2eb289ead1 Update screenshot and add screenshot of settings to README.md 2024-08-09 23:15:28 +02:00
kekePower 08e2988776 Update to version 1.5. Rename About button to Settings and create a Settings dialog where you can configure the commands for each button and moved the About section as well 2024-08-09 23:02:46 +02:00
kekePower 020aac91ff Make PREFIX configurable in the Makefile 2024-07-28 18:37:18 +02:00
kekePower 8ae0b72624 Update to version 1.4. Place the buttons together and give them a bit of air. This looks, imho, a lot better. 2024-07-28 01:09:48 +02:00
kekePower 65946c76fc Update to version 1.3
- Added `resources.c` and `resources.h` to the `.gitignore` file.
- Modified the `execute_command` function to use `g_spawn_command_line_async` for asynchronous command execution.
- Updated the `show_confirmation_dialog` function to display a GTK dialog with a warning icon and improved layout.
- Added a margin to the buttons in the `create_button` function for better spacing.
- Improved error handling for loading the window icon in the `activate` function.
- Updated README.md with more information about manually compiling ssdd
2024-07-12 08:08:54 +02:00
kekePower 152a0d648c Update screenshot 2024-07-12 00:41:05 +02:00
kekePower 40f35759b9 Update after rebuild of resources 2024-07-12 00:39:32 +02:00
kekePower 2a53ed3845 Rename to Simple ShutDown Dialog again 2024-07-12 00:37:57 +02:00
kekePower 8edde79fa0 Rename to Simple ShutDown Dialog 2024-07-12 00:35:34 +02:00
kekePower 19c42e9a52 Update README.md with better and more concise information about the project 2024-07-11 23:29:09 +02:00
kekePower 7e9b159204 Remove test1234 2024-07-05 12:29:16 +02:00
kekePower 872159b8ab A test commit1234 2024-07-05 12:28:33 +02:00
kekePower 875c768cea Remove test123 2024-07-05 12:26:36 +02:00
kekePower 9e08851964 A test commit 2024-07-05 12:18:23 +02:00
kekePower 3a16bc04d5 Remove test 2024-07-05 12:14:39 +02:00
kekePower 76ae1590f6 A test commit 2024-07-05 12:14:09 +02:00
kekePower 20113cc0c9 Remove test 2024-07-05 12:04:34 +02:00
kekePower 0e92c38db0 A test commit 2024-07-05 12:02:17 +02:00
kekePower 9d98d6056f README.md: Updated info about Makefile 2024-07-05 11:55:09 +02:00
kekePower a54cac53f2 Added Makefile 2024-07-05 10:55:35 +02:00
kekePower 2d10e570be Fixed typo in README.md 2024-07-05 10:35:48 +02:00
kekePower ef25b54a22 Rename app from example to ssdd 2024-07-04 08:11:08 +02:00
kekePower 86c43720cb Update to version 1.2 2024-07-01 23:02:08 +02:00
kekePower 9c639978d0 Reduced size of About image and hence reduced the size of the binary from 199kb to 79kb 2024-07-01 22:34:48 +02:00
kekePower 23c3d1aebf Add question to all 6 actions 2024-07-01 22:29:12 +02:00
kekePower 871c268bc3 Update README.me 2024-06-30 12:57:34 +02:00
13 changed files with 572 additions and 11622 deletions
+9
View File
@@ -1 +1,10 @@
ssdd ssdd
resources.c
resources.h
# Atlas Scout local data
.atlas/
# ZaguanBlade local data
.zblade/
.pnpm-store/
+57
View File
@@ -0,0 +1,57 @@
# Compiler
CC = gcc
# Compiler and optimization flags
CFLAGS ?= -O2 -Wall -Wextra
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
SRC = ssdd.c resources.c
# Output executable
TARGET = ssdd
# Resource files
RESOURCE_XML = resources.gresource.xml
RESOURCE_C = resources.c
RESOURCE_FILES = ssdd-icon.png
# Installation directories
PREFIX ?= /usr/local
BINDIR = $(PREFIX)/bin
DATADIR = $(PREFIX)/share/ssdd
# Default target
all: $(TARGET)
# Build the target
$(TARGET): $(SRC)
$(CC) $(CPPFLAGS) $(CFLAGS) $(GTK_CFLAGS) $(VERSION_FLAGS) \
-o $(TARGET) $(SRC) $(LDFLAGS) $(GTK_LIBS)
# Compile resources
$(RESOURCE_C): $(RESOURCE_XML) $(RESOURCE_FILES)
glib-compile-resources $(RESOURCE_XML) --generate-source --target=$(RESOURCE_C)
# Install target
install: $(TARGET)
install -d $(DESTDIR)$(BINDIR)
install -m 755 $(TARGET) $(DESTDIR)$(BINDIR)
install -d $(DESTDIR)$(DATADIR)
install -m 644 $(RESOURCE_XML) $(DESTDIR)$(DATADIR)
# Uninstall target
uninstall:
rm -f $(DESTDIR)$(BINDIR)/$(TARGET)
rm -rf $(DESTDIR)$(DATADIR)
# Clean target
clean:
rm -f $(TARGET) $(RESOURCE_C)
.PHONY: all clean install uninstall
+66 -19
View File
@@ -1,39 +1,86 @@
# ssdd # 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
![Project Screenshot](ssdd.png) ![Project Screenshot](ssdd.png)
## Why? ![Settings screenshot](ssdd-settings.png)
I just bought a new laptop and on my workstation I was using [ssd from Sawfish](https://github.com/SawfishWM/ssd) which I loved. I didn't want to go through all the steps of installing the necessary libraries and dependencies to get it to work, so I decided to create my own. ![Settings screenshot](ssdd-about.png)
## Dependencies and compilation **Simple Shutdown Dialog (ssdd)** is a simple yet stylish shutdown dialog for Openbox, crafted in C using GTK 4.
This app requires GTK+ 3.0 development libraries and gcc or clang. ## Why ssdd?
I am using this command to compile the program: As a long-time Openbox enthusiast, I've always found the default exit dialog a bit lackluster. Modern systems deserve a more refined shutdown experience. While there are other options out there, I figured one more wouldn't hurt, right?
Inspired by the elegant `ssd` from Sawfish, I decided to create my own tailored solution for Openbox. This way, you can avoid the hassle of installing extra dependencies and enjoy a sleek shutdown dialog that complements your Openbox setup.
## Features
- **Clean and Intuitive Interface:** ssdd presents clear options for Logout, Reboot, Shutdown, Switch User, Suspend, Hibernate, Settings, and Exit.
- **Configurable Commands:** Easily customize the commands executed for each action via the settings dialog.
- **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
ssdd requires:
* GTK 4 development libraries
* Glib 2 development libraries
* gcc or clang
### Easy Compilation
Edit the `Makefile` or use the following commands:
Using GCC:
```shell ```shell
% gcc ssdd.c resources.c -o ssdd `pkg-config --cflags --libs gtk+-3.0` % make all # Compile
% sudo make install # Install to /usr/local
% sudo make install PREFIX=/usr # Install to /usr
``` ```
Using Clang: ### Manual compilation
```shell
% clang ssdd.c resources.c -o ssdd `pkg-config --cflags --libs gtk+-3.0` First generate the resources.
```bash
% glib-compile-resources resources.gresource.xml --generate-source --target=resources.c
% glib-compile-resources resources.gresource.xml --generate-header --target=resources.h
``` ```
This produces the binary `ssdd` which you can place in your $PATH. ```bash
# Using GCC:
% gcc ssdd.c resources.c -o ssdd `pkg-config --cflags --libs gtk4`
## Configure Openbox to use it. # Using Clang:
% clang ssdd.c resources.c -o ssdd `pkg-config --cflags --libs gtk4`
```
`% sudo nvim /etc/xdg/openbox/menu.xml` Place the `ssdd` binary in your `$PATH` (e.g., `~/bin`).
Find the line with the standard Openbox Exit option and change it to ### Integrate with Openbox
`<item label="Log Out"><action name="Execute"><execute>ssdd</execute></item>` 1. Edit your Openbox menu:
The reconfigure Openbox to use the new setting. ```bash
% sudo nvim /etc/xdg/openbox/menu.xml
```
2. Replace the default Exit entry with:
```xml
<item label="Log Out"><action name="Execute"><execute>ssdd</execute></action></item>
```
3. Reconfigure Openbox:
```bash
% openbox --reconfigure
```
### Contributing
Contributions are welcome! Feel free to open issues or submit pull requests.
`% openbox --reconfigure`
+7
View File
@@ -0,0 +1,7 @@
[Desktop Entry]
Type=Application
Name=Simple Shutdown Dialog
Exec=ssdd
Icon=com.example.ssdd
Categories=Utility;
-11439
View File
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<gresources> <gresources>
<gresource prefix="/org/gtk/example"> <gresource prefix="/org/gtk/ssdd">
<file>ssdd-icon.png</file> <file>ssdd-icon.png</file>
</gresource> </gresource>
</gresources> </gresources>
-7
View File
@@ -1,7 +0,0 @@
#ifndef __RESOURCE_resources_H__
#define __RESOURCE_resources_H__
#include <gio/gio.h>
extern GResource *resources_get_resource (void);
#endif
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 176 KiB

After

Width:  |  Height:  |  Size: 57 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

+425 -149
View File
@@ -1,191 +1,467 @@
#include <errno.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <stdlib.h>
#include "resources.h" // Include the generated resource header
static void execute_command(const gchar *command) { #define CONFIG_DIR_NAME "ssdd"
int ret = system(command); #define CONFIG_FILE_NAME "config"
if (ret != 0) { #define CONFIG_GROUP "Commands"
GtkWidget *dialog; #define APP_VERSION "2.2"
dialog = gtk_message_dialog_new(NULL,
GTK_DIALOG_DESTROY_WITH_PARENT, typedef enum {
GTK_MESSAGE_ERROR, ACTION_LOGOUT,
GTK_BUTTONS_CLOSE, ACTION_REBOOT,
"Error executing command: %s", ACTION_SHUTDOWN,
command); ACTION_SWITCH_USER,
gtk_dialog_run(GTK_DIALOG(dialog)); ACTION_SUSPEND,
gtk_widget_destroy(dialog); 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_confirmation_dialog(GtkWindow *parent, const gchar *label,
const gchar *command);
static void show_message_dialog(GtkWindow *parent, GtkMessageType type,
GtkButtonsType buttons, const gchar *title,
const gchar *message);
static void show_settings_dialog(GtkWindow *parent, AppState *state);
static void show_settings_tab(GtkWidget *box, AppState *state);
static void app_state_free(gpointer data) {
AppState *state = data;
for (guint i = 0; i < N_CONFIG_ACTIONS; i++)
g_free(state->commands[i]);
g_free(state);
}
static void app_state_set_command(AppState *state, guint index,
const gchar *command) {
g_return_if_fail(index < N_CONFIG_ACTIONS);
g_free(state->commands[index]);
state->commands[index] = g_strdup(command);
}
static gchar *get_config_path(GError **error) {
g_autofree gchar *config_dir =
g_build_filename(g_get_user_config_dir(), CONFIG_DIR_NAME, NULL);
if (g_mkdir_with_parents(config_dir, 0755) == -1) {
gint saved_errno = errno;
g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(saved_errno),
"Could not create configuration directory '%s': %s", config_dir,
g_strerror(saved_errno));
return NULL;
} }
return g_build_filename(config_dir, CONFIG_FILE_NAME, NULL);
} }
static void show_confirmation_dialog(GtkWidget *widget, gpointer data) { static gboolean save_configuration(const gchar *const commands[],
const gchar *command = (const gchar *) data; GError **error) {
GtkWidget *dialog; g_autofree gchar *config_path = get_config_path(error);
gint response; g_autoptr(GKeyFile) key_file = NULL;
dialog = gtk_message_dialog_new(NULL, if (config_path == NULL)
GTK_DIALOG_DESTROY_WITH_PARENT, return FALSE;
GTK_MESSAGE_QUESTION,
GTK_BUTTONS_NONE,
"Are you sure you want to execute: %s?",
command);
gtk_dialog_add_button(GTK_DIALOG(dialog), "Yes", GTK_RESPONSE_YES);
gtk_dialog_add_button(GTK_DIALOG(dialog), "No", GTK_RESPONSE_NO);
response = gtk_dialog_run(GTK_DIALOG(dialog)); key_file = g_key_file_new();
gtk_widget_destroy(dialog);
if (response == GTK_RESPONSE_YES) { for (guint i = 0; i < N_CONFIG_ACTIONS; i++) {
execute_command(command); 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 show_about_dialog(GtkWidget *widget) { static void load_configuration(AppState *state) {
GtkWidget *dialog; g_autoptr(GError) error = NULL;
GtkWidget *content_area; g_autofree gchar *config_path = get_config_path(&error);
GtkWidget *label; g_autoptr(GKeyFile) key_file = g_key_file_new();
GtkWidget *image; gboolean needs_save = FALSE;
GtkWidget *box;
const gchar *about_text =
"\nAbout Stig's ShutDown Dialog\n\n"
"<b>Version:</b> 1.1\n"
"<b>Author:</b> kekePower\n"
"<b>URL:</b> <a href=\"https://git.kekepower.com/kekePower/ssdd\">https://git.kekepower.com/kekePower/ssdd</a>\n"
"<b>Description:</b> This is a simple Shutdown Dialog for Openbox.\n";
dialog = gtk_dialog_new_with_buttons("About Stig's ShutDown Dialog", if (config_path == NULL ||
NULL, !g_key_file_load_from_file(
GTK_DIALOG_DESTROY_WITH_PARENT, key_file, config_path,
"_Close", G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error)) {
GTK_RESPONSE_CLOSE, g_warning("Could not load configuration%s%s. Using defaults.",
NULL); error != NULL ? ": " : "", error != NULL ? error->message : "");
content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
gtk_container_add(GTK_CONTAINER(content_area), box);
image = gtk_image_new_from_resource("/org/gtk/example/ssdd-icon.png"); for (guint i = 0; i < N_CONFIG_ACTIONS; i++)
gtk_image_set_pixel_size(GTK_IMAGE(image), 250); // Assuming original size is 500x500 app_state_set_command(state, i, actions[i].default_command);
gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0);
label = gtk_label_new(NULL); if (config_path != NULL) {
gtk_label_set_markup(GTK_LABEL(label), about_text); const gchar *commands[N_CONFIG_ACTIONS];
gtk_label_set_selectable(GTK_LABEL(label), TRUE); g_autoptr(GError) save_error = NULL;
gtk_widget_set_halign(label, GTK_ALIGN_START);
gtk_widget_set_valign(label, GTK_ALIGN_START);
gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
gtk_widget_show_all(dialog); for (guint i = 0; i < N_CONFIG_ACTIONS; i++)
commands[i] = state->commands[i];
gtk_dialog_run(GTK_DIALOG(dialog)); if (!save_configuration(commands, &save_error))
gtk_widget_destroy(dialog); g_warning("Could not save default configuration: %s",
} save_error->message);
}
static void button_clicked(GtkWidget *widget, gpointer data) {
const gchar *command = (const gchar *) data;
if (g_strcmp0(command, "exit") == 0) {
g_application_quit(G_APPLICATION(g_object_get_data(G_OBJECT(widget), "app")));
return; return;
} }
if (g_strcmp0(command, "openbox --exit") == 0 || for (guint i = 0; i < N_CONFIG_ACTIONS; i++) {
g_strcmp0(command, "systemctl reboot") == 0 || g_autofree gchar *command = g_key_file_get_string(
g_strcmp0(command, "systemctl poweroff") == 0) { key_file, CONFIG_GROUP, actions[i].config_key, NULL);
show_confirmation_dialog(widget, (gpointer) command);
} else if (g_strcmp0(command, "about") == 0) { if (command == NULL || *command == '\0') {
show_about_dialog(widget); 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 { } else {
execute_command(command); 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 gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data) { static void show_message_dialog(GtkWindow *parent, GtkMessageType type,
if (event->keyval == GDK_KEY_Escape) { GtkButtonsType buttons, const gchar *title,
g_application_quit(G_APPLICATION(data)); const gchar *message) {
return TRUE; // Event handled GtkWidget *dialog = gtk_message_dialog_new(
} parent, GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, type, buttons,
return FALSE; // Event not handled "%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 activate(GtkApplication *app, gpointer user_data) { static void execute_command(const gchar *command, GtkWindow *parent) {
GtkWidget *window; g_autoptr(GError) error = NULL;
GtkWidget *grid;
GtkWidget *button;
GtkWidget *image;
GtkWidget *box;
GtkWidget *label;
const gchar *buttons[] = {
"openbox --exit",
"systemctl reboot",
"systemctl poweroff",
"dm-tool switch-to-greeter",
"systemctl suspend",
"systemctl hibernate",
"about",
"exit"
};
const gchar *icons[] = {
"system-log-out",
"view-refresh",
"system-shutdown",
"system-users",
"media-playback-pause",
"media-playback-stop",
"help-about",
"application-exit"
};
const gchar *labels[] = {
"Logout",
"Reboot",
"Shutdown",
"Switch User",
"Suspend",
"Hibernate",
"About",
"Exit"
};
window = gtk_application_window_new(app); if (!g_spawn_command_line_async(command, &error)) {
gtk_window_set_title(GTK_WINDOW(window), "Exit Openbox"); g_autofree gchar *message = g_strdup_printf(
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); "Could not execute:\n'%s'\n\nReason: %s", command, error->message);
// Load the icon from the resource and set it as the window icon show_message_dialog(parent, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
GdkPixbuf *icon_pixbuf = gdk_pixbuf_new_from_resource("/org/gtk/example/ssdd-icon.png", NULL); "Error Executing Command", message);
gtk_window_set_icon(GTK_WINDOW(window), icon_pixbuf); }
}
g_signal_connect(window, "key-press-event", G_CALLBACK(on_key_press), app); static void show_about_tab(GtkWidget *box) {
static const gchar about_text[] =
"\n<b>Simple ShutDown Dialog</b>\n\n"
"<b>Version:</b> " APP_VERSION "\n"
"<b>Author:</b> kekePower\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";
GtkWidget *image = gtk_image_new_from_resource("/org/gtk/ssdd/ssdd-icon.png");
GtkWidget *label = gtk_label_new(NULL);
grid = gtk_grid_new(); gtk_image_set_pixel_size(GTK_IMAGE(image), 128);
gtk_container_add(GTK_CONTAINER(window), grid); gtk_widget_set_halign(image, GTK_ALIGN_CENTER);
gtk_widget_set_margin_bottom(image, 10);
gtk_box_append(GTK_BOX(box), image);
for (int i = 0; i < 8; i++) { gtk_label_set_markup(GTK_LABEL(label), about_text);
button = gtk_button_new(); gtk_label_set_selectable(GTK_LABEL(label), TRUE);
box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5); gtk_label_set_wrap(GTK_LABEL(label), TRUE);
gtk_container_add(GTK_CONTAINER(button), box); gtk_widget_set_halign(label, GTK_ALIGN_CENTER);
gtk_widget_set_valign(label, GTK_ALIGN_START);
gtk_box_append(GTK_BOX(box), label);
}
image = gtk_image_new_from_icon_name(icons[i], GTK_ICON_SIZE_BUTTON); static void show_settings_tab(GtkWidget *box, AppState *state) {
gtk_box_pack_start(GTK_BOX(box), image, TRUE, TRUE, 0); GtkWidget *grid = gtk_grid_new();
GtkWidget *save_button = gtk_button_new_with_label("Save");
SettingsData *settings = g_new0(SettingsData, 1);
label = gtk_label_new(labels[i]); settings->state = state;
gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
g_object_set_data(G_OBJECT(button), "app", app); gtk_grid_set_row_spacing(GTK_GRID(grid), 10);
g_signal_connect(button, "clicked", G_CALLBACK(button_clicked), (gpointer) buttons[i]); gtk_grid_set_column_spacing(GTK_GRID(grid), 10);
gtk_grid_attach(GTK_GRID(grid), button, i % 4, i / 4, 1, 1); gtk_box_append(GTK_BOX(box), grid);
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_show_all(window); 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 void show_settings_dialog(GtkWindow *parent, AppState *state) {
GtkWidget *dialog = gtk_dialog_new();
GtkWidget *content = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
GtkWidget *notebook = gtk_notebook_new();
GtkWidget *settings_tab = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
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 on_save_button_clicked(GtkButton *button,
gpointer user_data G_GNUC_UNUSED) {
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)));
for (guint i = 0; i < N_CONFIG_ACTIONS; i++) {
GtkEditable *entry = GTK_EDITABLE(settings->entries[i]);
const gchar *command = gtk_editable_get_text(entry);
if (*command == '\0') {
gtk_editable_set_text(entry, actions[i].default_command);
command = gtk_editable_get_text(entry);
}
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 show_confirmation_dialog(GtkWindow *parent, const gchar *label,
const gchar *command) {
g_autofree gchar *message =
g_strdup_printf("Are you sure you want to %s?", 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);
gtk_window_set_title(GTK_WINDOW(dialog), "Confirmation");
g_object_set_data_full(G_OBJECT(dialog), "command", g_strdup(command),
g_free);
g_signal_connect(dialog, "response", G_CALLBACK(on_confirmation_response),
NULL);
gtk_window_present(GTK_WINDOW(dialog));
}
static void on_confirmation_response(GtkDialog *dialog, gint response_id,
gpointer user_data G_GNUC_UNUSED) {
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));
if (command != NULL && parent != NULL)
execute_command(command, parent);
else
g_warning("Confirmation dialog is missing its command or parent");
}
gtk_window_destroy(GTK_WINDOW(dialog));
}
static void button_clicked(GtkButton *button,
gpointer user_data G_GNUC_UNUSED) {
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_return_if_fail(action_id >= 0 && action_id < N_ACTIONS);
if (action_id == ACTION_EXIT) {
g_application_quit(G_APPLICATION(app));
} else if (action_id == ACTION_SETTINGS) {
show_settings_dialog(window, state);
} else {
show_confirmation_dialog(window, actions[action_id].label,
state->commands[action_id]);
}
}
static gboolean on_key_pressed(GtkEventControllerKey *controller G_GNUC_UNUSED,
guint keyval, guint keycode G_GNUC_UNUSED,
GdkModifierType state G_GNUC_UNUSED,
gpointer user_data) {
if (keyval != GDK_KEY_Escape)
return FALSE;
g_application_quit(G_APPLICATION(user_data));
return TRUE;
}
static void create_button(GtkWidget *grid, ActionId action_id) {
const ActionSpec *action = &actions[action_id];
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);
gtk_widget_set_hexpand(button, TRUE);
gtk_widget_set_vexpand(button, TRUE);
gtk_widget_set_valign(box, GTK_ALIGN_CENTER);
gtk_widget_set_halign(box, GTK_ALIGN_CENTER);
gtk_button_set_child(GTK_BUTTON(button), box);
gtk_image_set_icon_size(GTK_IMAGE(image), GTK_ICON_SIZE_LARGE);
gtk_box_append(GTK_BOX(box), image);
gtk_box_append(GTK_BOX(box), label);
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);
}
static void activate(GtkApplication *app, gpointer user_data G_GNUC_UNUSED) {
GtkWindow *existing_window = gtk_application_get_active_window(app);
if (existing_window != NULL) {
gtk_window_present(existing_window);
return;
}
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);
// Register the resource load_configuration(state);
g_resources_register(resources_get_resource()); g_object_set_data_full(G_OBJECT(app), "app-state", state, app_state_free);
app = gtk_application_new("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL); g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status; return g_application_run(G_APPLICATION(app), argc, argv);
} }
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB