34 Commits
v1.0 ... master

Author SHA1 Message Date
94e911a4c5 Update to version 2.1 and hopefully fix the bugs 2025-04-14 23:46:02 +02:00
c24db4dcbd Update to version 2.0. Port to GTK4. 2024-09-13 01:49:48 +02:00
18e6f1a70f Update to version 1.6. Change format of config file to use KEY=VALUE. 2024-08-10 09:35:15 +02:00
3aac31a8e5 Cleanup 2024-08-09 23:20:01 +02:00
2eb289ead1 Update screenshot and add screenshot of settings to README.md 2024-08-09 23:15:28 +02:00
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
020aac91ff Make PREFIX configurable in the Makefile 2024-07-28 18:37:18 +02:00
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
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
152a0d648c Update screenshot 2024-07-12 00:41:05 +02:00
40f35759b9 Update after rebuild of resources 2024-07-12 00:39:32 +02:00
2a53ed3845 Rename to Simple ShutDown Dialog again 2024-07-12 00:37:57 +02:00
8edde79fa0 Rename to Simple ShutDown Dialog 2024-07-12 00:35:34 +02:00
19c42e9a52 Update README.md with better and more concise information about the project 2024-07-11 23:29:09 +02:00
7e9b159204 Remove test1234 2024-07-05 12:29:16 +02:00
872159b8ab A test commit1234 2024-07-05 12:28:33 +02:00
875c768cea Remove test123 2024-07-05 12:26:36 +02:00
9e08851964 A test commit 2024-07-05 12:18:23 +02:00
3a16bc04d5 Remove test 2024-07-05 12:14:39 +02:00
76ae1590f6 A test commit 2024-07-05 12:14:09 +02:00
20113cc0c9 Remove test 2024-07-05 12:04:34 +02:00
0e92c38db0 A test commit 2024-07-05 12:02:17 +02:00
9d98d6056f README.md: Updated info about Makefile 2024-07-05 11:55:09 +02:00
a54cac53f2 Added Makefile 2024-07-05 10:55:35 +02:00
2d10e570be Fixed typo in README.md 2024-07-05 10:35:48 +02:00
ef25b54a22 Rename app from example to ssdd 2024-07-04 08:11:08 +02:00
86c43720cb Update to version 1.2 2024-07-01 23:02:08 +02:00
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
23c3d1aebf Add question to all 6 actions 2024-07-01 22:29:12 +02:00
871c268bc3 Update README.me 2024-06-30 12:57:34 +02:00
fcdabcb59d Update to version 1.1 2024-06-30 12:05:52 +02:00
1cba6e01e7 Updated README.md to fix typo 2024-06-30 12:02:56 +02:00
911060ecf7 Updated README.md info on how to compile ssdd 2024-06-30 12:02:05 +02:00
8b3bde4c5a Added option to use Escape to close the window. Program icon compiled into the program. 2024-06-30 12:00:29 +02:00
10 changed files with 616 additions and 147 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
ssdd ssdd
resources.c
resources.h

55
Makefile Normal file
View File

@ -0,0 +1,55 @@
# Compiler
CC = gcc
# Compiler and optimization flags
CFLAGS ?= -Wall -O2
CFLAGS += $(shell pkg-config --cflags gtk4)
CFLAGS += -DGDK_VERSION_MAX_ALLOWED=GDK_VERSION_4_0 -DGDK_VERSION_MIN_REQUIRED=GDK_VERSION_4_0
LDFLAGS ?=
LDFLAGS += $(shell pkg-config --libs gtk4)
# Source files
SRC = ssdd.c resources.c
# Output executable
TARGET = ssdd
# Resource files
RESOURCE_XML = resources.gresource.xml
RESOURCE_C = resources.c
RESOURCE_H = resources.h
# Installation directories
PREFIX ?= /usr/local
BINDIR = $(PREFIX)/bin
DATADIR = $(PREFIX)/share/ssdd
# Default target
all: $(TARGET)
# Build the target
$(TARGET): $(RESOURCE_C) $(SRC)
$(CC) $(CFLAGS) -o $(TARGET) $(SRC) $(LDFLAGS)
# Compile resources
$(RESOURCE_C) $(RESOURCE_H): $(RESOURCE_XML)
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 -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) $(RESOURCE_H)
.PHONY: all clean install uninstall

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 -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 -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
com.example.ssdd.desktop Normal file
View File

@ -0,0 +1,7 @@
[Desktop Entry]
Type=Application
Name=Simple Shutdown Dialog
Exec=ssdd
Icon=com.example.ssdd
Categories=Utility;

7
resources.gresource.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/org/gtk/ssdd">
<file>ssdd-icon.png</file>
</gresource>
</gresources>

BIN
ssdd-about.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 176 KiB

After

Width:  |  Height:  |  Size: 57 KiB

BIN
ssdd-settings.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

607
ssdd.c
View File

@ -1,177 +1,528 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <glib/gstdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "resources.h" // Assuming this header declares resources_get_resource()
static void execute_command(const gchar *command) { // Function declarations
int ret = system(command); static void execute_command(const gchar *command, GtkWindow *parent);
if (ret != 0) { static void show_settings_dialog(GtkWindow *parent);
GtkWidget *dialog; static void show_about_tab(GtkWidget *box);
dialog = gtk_message_dialog_new(NULL, static void show_settings_tab(GtkWidget *box);
GTK_DIALOG_DESTROY_WITH_PARENT, static void button_clicked(GtkWidget *widget, gpointer data); // data is unused now, could be NULL
GTK_MESSAGE_ERROR, static gboolean on_key_pressed(GtkEventControllerKey *controller, guint keyval, guint keycode, GdkModifierType state, gpointer user_data);
GTK_BUTTONS_CLOSE, static void show_confirmation_dialog(GtkWindow *parent_window, const gchar *label, const gchar *command);
"Error executing command: %s", static void create_button(GtkWidget *grid, GtkApplication *app, const gchar *label_text, const gchar *icon_name, const gchar *command, int pos);
command); static void save_configuration(const gchar *commands[]);
gtk_dialog_run(GTK_DIALOG(dialog)); static void load_configuration(gchar *commands[]);
gtk_widget_destroy(dialog); static gchar *get_config_path(void);
static gchar *get_config_dir(void);
static void on_save_button_clicked(GtkButton *button, gpointer user_data);
static void on_confirmation_response(GtkDialog *dialog, gint response_id, gpointer user_data);
// Gets the configuration directory path (~/.config/ssdd)
static gchar *get_config_dir(void) {
return g_build_filename(g_get_user_config_dir(), "ssdd", NULL);
}
// Gets the full configuration file path (~/.config/ssdd/config)
// Ensures the directory exists.
static gchar *get_config_path(void) {
gchar *config_dir = get_config_dir();
// Create the directory if it doesn't exist
g_mkdir_with_parents(config_dir, 0755);
gchar *config_path = g_build_filename(config_dir, "config", NULL);
g_free(config_dir); // Free the dir path returned by get_config_dir
return config_path;
}
// Executes a shell command asynchronously
static void execute_command(const gchar *command, GtkWindow *parent) {
GError *error = NULL;
// Use g_spawn_command_line_async for non-blocking execution
gboolean ret = g_spawn_command_line_async(command, &error);
if (!ret) {
// Format an error message if spawning failed
gchar *error_message = g_strdup_printf("Error executing command:\n'%s'\n\nReason: %s", command, error ? error->message : "Unknown error");
// Show an error dialog
GtkWidget *dialog = gtk_message_dialog_new(parent,
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
"%s", error_message);
g_signal_connect(dialog, "response", G_CALLBACK(gtk_window_destroy), NULL);
gtk_window_present(GTK_WINDOW(dialog));
g_free(error_message);
if (error) {
g_error_free(error);
}
} }
} }
static void show_confirmation_dialog(GtkWidget *widget, const gchar *label, const gchar *command) { // Shows the settings dialog with multiple tabs
GtkWidget *dialog; static void show_settings_dialog(GtkWindow *parent) {
gint response;
gchar *message = g_strdup_printf("Are you sure you want to %s?", label);
dialog = gtk_message_dialog_new(NULL,
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_QUESTION,
GTK_BUTTONS_NONE,
"%s",
message);
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));
gtk_widget_destroy(dialog);
g_free(message);
if (response == GTK_RESPONSE_YES) {
execute_command(command);
}
}
static void show_about_dialog(GtkWidget *widget) {
GtkWidget *dialog; GtkWidget *dialog;
GtkWidget *content_area; GtkWidget *content_area;
GtkWidget *label; GtkWidget *notebook;
GtkWidget *image; GtkWidget *settings_tab;
GtkWidget *box; GtkWidget *about_tab;
const gchar *about_text =
"About Stig's ShutDown Dialog\n\n"
"<b>Version:</b> 1.0\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.";
dialog = gtk_dialog_new_with_buttons("About Stig's ShutDown Dialog", // Create the dialog window
NULL, dialog = gtk_dialog_new();
GTK_DIALOG_DESTROY_WITH_PARENT, gtk_window_set_title(GTK_WINDOW(dialog), "Settings");
"_Close", gtk_window_set_transient_for(GTK_WINDOW(dialog), parent); // Set parent window
GTK_RESPONSE_CLOSE, gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); // Make dialog modal
NULL); gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE); // Destroy with parent
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_file("ssdd-icon.png"); // Get the content area of the dialog (GTK4 uses gtk_window_set_child)
gtk_image_set_pixel_size(GTK_IMAGE(image), 250); // Assuming original size is 500x500 content_area = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0); gtk_window_set_child(GTK_WINDOW(dialog), content_area);
label = gtk_label_new(NULL); // Create a notebook for tabs
gtk_label_set_markup(GTK_LABEL(label), about_text); notebook = gtk_notebook_new();
gtk_label_set_selectable(GTK_LABEL(label), TRUE); gtk_box_append(GTK_BOX(content_area), notebook);
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); // Create and add Settings Tab
settings_tab = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); // Box for settings content
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); // Populate the settings tab
gtk_dialog_run(GTK_DIALOG(dialog)); // Create and add About Tab
gtk_widget_destroy(dialog); about_tab = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); // Box for about content
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); // Populate the about tab
// Add a standard "Close" button to the dialog's action area
gtk_dialog_add_button(GTK_DIALOG(dialog), "_Close", GTK_RESPONSE_CLOSE);
// Connect the response signal to destroy the dialog
g_signal_connect(dialog, "response", G_CALLBACK(gtk_window_destroy), NULL);
// Show the dialog
gtk_window_present(GTK_WINDOW(dialog));
} }
static void button_clicked(GtkWidget *widget, gpointer data) { // Populates the "Settings" tab with command entries
const gchar *command = (const gchar *) data; static void show_settings_tab(GtkWidget *box) {
const gchar *label = gtk_button_get_label(GTK_BUTTON(widget)); const gchar *labels[] = {
"Logout Command:",
"Reboot Command:",
"Shutdown Command:",
"Switch User Command:",
"Suspend Command:",
"Hibernate Command:"
};
if (g_strcmp0(command, "exit") == 0) { gchar *commands[6] = { NULL }; // Initialize command array to NULL
g_application_quit(G_APPLICATION(g_object_get_data(G_OBJECT(widget), "app"))); load_configuration(commands); // Load commands from config file
// Create a grid layout for labels and entries
GtkWidget *grid = gtk_grid_new();
gtk_grid_set_row_spacing(GTK_GRID(grid), 10);
gtk_grid_set_column_spacing(GTK_GRID(grid), 10);
gtk_box_append(GTK_BOX(box), grid);
// Array to hold the GtkEntry widgets
GtkWidget **entries = g_new(GtkWidget*, 6);
// Create label and entry for each command
for (int i = 0; i < 6; i++) {
GtkWidget *label = gtk_label_new(labels[i]);
GtkWidget *entry = gtk_entry_new();
// Set entry text (handle potential NULL from load_configuration)
gtk_editable_set_text(GTK_EDITABLE(entry), commands[i] ? commands[i] : "");
gtk_widget_set_hexpand(entry, TRUE); // Make entry expand horizontally
gtk_widget_set_halign(label, GTK_ALIGN_END); // Align label to the right
// Attach label and entry to the grid
gtk_grid_attach(GTK_GRID(grid), label, 0, i, 1, 1);
gtk_grid_attach(GTK_GRID(grid), entry, 1, i, 1, 1);
entries[i] = entry; // Store entry widget
g_free(commands[i]); // Free the command string loaded earlier
}
// Create the "Save" button
GtkWidget *save_button = gtk_button_new_with_label("Save");
gtk_widget_set_halign(save_button, GTK_ALIGN_END); // Align button to the right
gtk_widget_set_margin_top(save_button, 10);
// Store the entries array with the button, ensuring it's freed when button is destroyed
g_object_set_data_full(G_OBJECT(save_button), "entries", entries, (GDestroyNotify)g_free);
// Connect the save button's clicked signal
g_signal_connect(save_button, "clicked", G_CALLBACK(on_save_button_clicked), NULL);
gtk_box_append(GTK_BOX(box), save_button); // Add button to the tab's box
}
// Callback function for the "Save" button in settings
static void on_save_button_clicked(GtkButton *button, gpointer user_data) {
// Retrieve the GtkEntry widgets array stored with the button
GtkWidget **entries = g_object_get_data(G_OBJECT(button), "entries");
const gchar *commands_to_save[6]; // Array to hold command strings from entries
// Get text from each entry
for (int i = 0; i < 6; i++) {
// This pointer is owned by the GtkEditable, do not free it here.
commands_to_save[i] = gtk_editable_get_text(GTK_EDITABLE(entries[i]));
}
// Save the retrieved commands to the configuration file
save_configuration(commands_to_save);
// Show an information dialog confirming the save
GtkWindow *parent_window = GTK_WINDOW(gtk_widget_get_root(GTK_WIDGET(button)));
GtkWidget *info_dialog = gtk_message_dialog_new(parent_window,
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO,
GTK_BUTTONS_OK,
"Settings saved successfully.");
g_signal_connect(info_dialog, "response", G_CALLBACK(gtk_window_destroy), NULL);
gtk_window_present(GTK_WINDOW(info_dialog));
}
// Populates the "About" tab
static void show_about_tab(GtkWidget *box) {
GtkWidget *label;
GtkWidget *image;
const gchar *about_text =
"\n<b>Simple ShutDown Dialog</b>\n\n"
"<b>Version:</b> 2.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> A Simple ShutDown Dialog for session management.\n";
// Load icon from GResource
image = gtk_image_new_from_resource("/org/gtk/ssdd/ssdd-icon.png");
gtk_image_set_pixel_size(GTK_IMAGE(image), 128); // Set icon size
gtk_widget_set_halign(image, GTK_ALIGN_CENTER); // Center align image
gtk_widget_set_margin_bottom(image, 10);
gtk_box_append(GTK_BOX(box), image);
// Create and configure the about text label
label = gtk_label_new(NULL);
gtk_label_set_markup(GTK_LABEL(label), about_text); // Use markup for formatting
gtk_label_set_selectable(GTK_LABEL(label), TRUE); // Allow text selection
gtk_label_set_wrap(GTK_LABEL(label), TRUE); // Wrap text if needed
gtk_widget_set_halign(label, GTK_ALIGN_CENTER); // Center align text
gtk_widget_set_valign(label, GTK_ALIGN_START);
gtk_box_append(GTK_BOX(box), label);
}
// Callback function for main window button clicks
static void button_clicked(GtkWidget *widget, gpointer data) {
// Retrieve data stored with the button
const gchar *command = g_object_get_data(G_OBJECT(widget), "command");
const gchar *label = g_object_get_data(G_OBJECT(widget), "label");
GtkApplication *app = g_object_get_data(G_OBJECT(widget), "app");
GtkWindow *parent_window = GTK_WINDOW(gtk_widget_get_root(widget));
// Safety check for command data
if (!command) {
g_warning("Command data not found for button '%s'", label ? label : "(unknown)");
return; return;
} }
if (g_strcmp0(command, "about") == 0) { // Handle special commands: "exit" and "settings"
show_about_dialog(widget); if (g_strcmp0(command, "exit") == 0) {
g_application_quit(G_APPLICATION(app)); // Quit the application
return;
}
if (g_strcmp0(command, "settings") == 0) {
show_settings_dialog(parent_window); // Show the settings dialog
} else { } else {
show_confirmation_dialog(widget, label, command); // For other commands, show a confirmation dialog
show_confirmation_dialog(parent_window, label, command);
} }
} }
// Callback function for key press events on the main window
static gboolean on_key_pressed(GtkEventControllerKey *controller, guint keyval, guint keycode, GdkModifierType state, gpointer user_data) {
// Check if the Escape key was pressed
if (keyval == GDK_KEY_Escape) {
GtkApplication *app = GTK_APPLICATION(user_data);
g_application_quit(G_APPLICATION(app)); // Quit the application
return TRUE; // Event handled
}
return FALSE; // Event not handled
}
// Shows a confirmation dialog before executing a command
static void show_confirmation_dialog(GtkWindow *parent_window, const gchar *label, const gchar *command) {
GtkWidget *dialog;
// Create the confirmation message string
gchar *message = g_strdup_printf("Are you sure you want to %s?", label ? label : "perform this action");
// Create the message dialog
dialog = gtk_message_dialog_new(parent_window,
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_QUESTION, // Question icon
GTK_BUTTONS_YES_NO, // Yes/No buttons
"%s", message);
gtk_window_set_title(GTK_WINDOW(dialog), "Confirmation");
// Store a copy of the command with the dialog, freed on destroy
g_object_set_data_full(G_OBJECT(dialog), "command", g_strdup(command), g_free);
// Connect the response signal
g_signal_connect(dialog, "response", G_CALLBACK(on_confirmation_response), NULL);
gtk_window_present(GTK_WINDOW(dialog));
g_free(message); // Free the message string
}
// Callback function for confirmation dialog responses
static void on_confirmation_response(GtkDialog *dialog, gint response_id, gpointer user_data) {
// Check if the "Yes" button was clicked
if (response_id == GTK_RESPONSE_YES) {
// Retrieve the command stored with the dialog
const gchar *command = g_object_get_data(G_OBJECT(dialog), "command");
// Get the parent window
GtkWindow *parent = GTK_WINDOW(gtk_window_get_transient_for(GTK_WINDOW(dialog)));
// Execute the command if valid
if(command && parent) {
execute_command(command, parent);
} else if (!command) {
g_warning("Command data missing in confirmation dialog response.");
}
}
// Destroy the dialog regardless of the response
gtk_window_destroy(GTK_WINDOW(dialog));
}
// Creates a button with an icon and label, and sets up its signal
static void create_button(GtkWidget *grid, GtkApplication *app, const gchar *label_text,
const gchar *icon_name, const gchar *command, int pos) {
GtkWidget *button;
GtkWidget *box;
GtkWidget *image;
GtkWidget *label;
// Create the button
button = gtk_button_new();
gtk_widget_set_hexpand(button, TRUE); // Allow horizontal expansion
gtk_widget_set_vexpand(button, TRUE); // Allow vertical expansion
// Create a vertical box for the icon and label inside the button
box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5); // 5px spacing
gtk_widget_set_valign(box, GTK_ALIGN_CENTER);
gtk_widget_set_halign(box, GTK_ALIGN_CENTER);
gtk_button_set_child(GTK_BUTTON(button), box);
// Create and add the icon
image = gtk_image_new_from_icon_name(icon_name);
gtk_image_set_icon_size(GTK_IMAGE(image), GTK_ICON_SIZE_LARGE); // Use a standard large icon size
gtk_box_append(GTK_BOX(box), image);
// Create and add the label
label = gtk_label_new(label_text);
gtk_box_append(GTK_BOX(box), label);
// Store necessary data with the button widget
g_object_set_data(G_OBJECT(button), "app", app); // Store app pointer
// Store copies of label and command, freed automatically when button is destroyed
g_object_set_data_full(G_OBJECT(button), "label", g_strdup(label_text), g_free);
g_object_set_data_full(G_OBJECT(button), "command", g_strdup(command), g_free);
// Connect the button's clicked signal to the callback function
g_signal_connect(button, "clicked", G_CALLBACK(button_clicked), NULL); // Pass NULL as data
// Attach the button to the main grid layout
gtk_grid_attach(GTK_GRID(grid), button, pos % 4, pos / 4, 1, 1); // 4 columns grid
}
// Saves the current command configuration to the file using GKeyFile
static void save_configuration(const gchar *commands[]) {
GError *error = NULL;
gchar *config_path = get_config_path(); // Get config file path (ensures dir exists)
// Use GKeyFile for robust configuration handling
GKeyFile *key_file = g_key_file_new();
// Set command strings in the key file under the [Commands] group
g_key_file_set_string(key_file, "Commands", "LOGOUT_COMMAND", commands[0] ? commands[0] : "");
g_key_file_set_string(key_file, "Commands", "REBOOT_COMMAND", commands[1] ? commands[1] : "");
g_key_file_set_string(key_file, "Commands", "SHUTDOWN_COMMAND", commands[2] ? commands[2] : "");
g_key_file_set_string(key_file, "Commands", "SWITCH_USER_COMMAND", commands[3] ? commands[3] : "");
g_key_file_set_string(key_file, "Commands", "SUSPEND_COMMAND", commands[4] ? commands[4] : "");
g_key_file_set_string(key_file, "Commands", "HIBERNATE_COMMAND", commands[5] ? commands[5] : "");
// Convert key file data to string format
gchar *config_data = g_key_file_to_data(key_file, NULL, &error);
if (error) {
g_warning("Failed to generate configuration data: %s", error->message);
g_error_free(error);
g_key_file_free(key_file);
g_free(config_path);
return;
}
// Save the configuration string data to the file
if (!g_file_set_contents(config_path, config_data, -1, &error)) {
g_warning("Failed to save configuration to '%s': %s", config_path, error->message);
g_error_free(error);
}
// Clean up allocated memory
g_free(config_data);
g_key_file_free(key_file);
g_free(config_path);
}
// Loads command configuration from the file using GKeyFile
// Populates the `commands` array (caller must free elements).
// Uses defaults if file/keys are missing or invalid.
static void load_configuration(gchar *commands[]) {
GError *error = NULL;
gchar *config_path = get_config_path(); // Get config file path (ensures dir exists)
// Default commands used if config is missing or invalid
const gchar *default_commands[] = {
"openbox --exit", // Default Logout
"systemctl reboot", // Default Reboot
"systemctl poweroff", // Default Shutdown
"dm-tool switch-to-greeter",// Default Switch User
"systemctl suspend", // Default Suspend
"systemctl hibernate" // Default Hibernate
};
GKeyFile *key_file = g_key_file_new();
// Attempt to load the configuration file
if (!g_key_file_load_from_file(key_file, config_path, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error)) {
// If loading fails (file not found, invalid format, etc.)
g_warning("Configuration file '%s' not found or invalid (%s). Using defaults.", config_path, error ? error->message : "unknown error");
if (error) g_error_free(error);
error = NULL; // Reset error status
// Save a default configuration file for next time
save_configuration(default_commands);
// Load default commands directly into the output array
for (int i = 0; i < 6; i++) {
commands[i] = g_strdup(default_commands[i]); // Duplicate default strings
}
} else {
// File loaded successfully, read values
commands[0] = g_key_file_get_string(key_file, "Commands", "LOGOUT_COMMAND", NULL);
commands[1] = g_key_file_get_string(key_file, "Commands", "REBOOT_COMMAND", NULL);
commands[2] = g_key_file_get_string(key_file, "Commands", "SHUTDOWN_COMMAND", NULL);
commands[3] = g_key_file_get_string(key_file, "Commands", "SWITCH_USER_COMMAND", NULL);
commands[4] = g_key_file_get_string(key_file, "Commands", "SUSPEND_COMMAND", NULL);
commands[5] = g_key_file_get_string(key_file, "Commands", "HIBERNATE_COMMAND", NULL);
// Replace NULL (missing keys) or empty strings with defaults
for (int i = 0; i < 6; i++) {
if (commands[i] == NULL || g_strcmp0(commands[i], "") == 0) {
g_free(commands[i]); // Free if empty string was returned or NULL
commands[i] = g_strdup(default_commands[i]); // Use default
g_warning("Configuration key missing or empty for index %d, using default: '%s'", i, default_commands[i]);
}
}
}
// Clean up
g_key_file_free(key_file);
g_free(config_path);
}
// Activation function called when the application starts
static void activate(GtkApplication *app, gpointer user_data) { static void activate(GtkApplication *app, gpointer user_data) {
GtkWidget *window; GtkWidget *window;
GtkWidget *grid; GtkWidget *grid;
GtkWidget *button; gchar *commands[6] = { NULL }; // Array to hold loaded commands
GtkWidget *image; load_configuration(commands); // Load commands from config file
GtkWidget *box;
GtkWidget *label; // Icon names (using symbolic for better theme integration)
const gchar *buttons[] = {
"openbox --exit",
"dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager.Reboot boolean:true",
"dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager.PowerOff boolean:true",
"dm-tool switch-to-greeter",
"dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager.Suspend boolean:true",
"dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager.Hibernate boolean:true",
"about",
"exit"
};
const gchar *icons[] = { const gchar *icons[] = {
"system-log-out", "system-log-out-symbolic",
"view-refresh", "system-reboot-symbolic",
"system-shutdown", "system-shutdown-symbolic",
"system-users", "system-users-symbolic",
"media-playback-pause", "media-playback-pause-symbolic", // Consider alternatives if needed
"media-playback-stop", "document-save-symbolic", // Hibernate icon choice
"help-about", "preferences-system-symbolic", // Settings icon
"application-exit" "window-close-symbolic" // Exit icon
}; };
// Button labels
const gchar *labels[] = { const gchar *labels[] = {
"Logout", "Logout", "Reboot", "Shutdown", "Switch User",
"Reboot", "Suspend", "Hibernate", "Settings", "Exit"
"Shutdown", };
"Switch User", // Special command identifiers for settings and exit
"Suspend", const gchar *special_commands[] = {
"Hibernate", "settings", "exit"
"About",
"Exit"
}; };
// Create the main application window
window = gtk_application_window_new(app); window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "Exit Openbox"); gtk_window_set_title(GTK_WINDOW(window), "Simple ShutDown Dialog");
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); gtk_window_set_resizable(GTK_WINDOW(window), FALSE); // Disable resizing
gtk_window_set_icon_from_file(GTK_WINDOW(window), "ssdd-icon.png", NULL); // --- Obsolete GTK3 calls removed ---
// gtk_window_set_position(...) removed - Handled by WM
// gtk_window_set_icon(...) removed - Handled by .desktop file
// Create the main grid layout
grid = gtk_grid_new(); grid = gtk_grid_new();
gtk_container_add(GTK_CONTAINER(window), grid); 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); // Set grid as child of window
for (int i = 0; i < 8; i++) { // Create buttons for the 6 main actions (Logout, Reboot, etc.)
button = gtk_button_new(); for (int i = 0; i < 6; i++) {
box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5); // Pass the loaded command; create_button makes a copy. Handle NULL.
gtk_container_add(GTK_CONTAINER(button), box); create_button(grid, app, labels[i], icons[i], commands[i] ? commands[i] : "", i);
g_free(commands[i]); // Free the original string from load_configuration
image = gtk_image_new_from_icon_name(icons[i], GTK_ICON_SIZE_BUTTON);
gtk_box_pack_start(GTK_BOX(box), image, TRUE, TRUE, 0);
label = gtk_label_new(labels[i]);
gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
gtk_button_set_label(GTK_BUTTON(button), labels[i]);
g_object_set_data(G_OBJECT(button), "app", app);
g_signal_connect(button, "clicked", G_CALLBACK(button_clicked), (gpointer) buttons[i]);
gtk_grid_attach(GTK_GRID(grid), button, i % 4, i / 4, 1, 1);
} }
gtk_widget_show_all(window); // Create Settings and Exit buttons
create_button(grid, app, labels[6], icons[6], special_commands[0], 6); // "settings"
create_button(grid, app, labels[7], icons[7], special_commands[1], 7); // "exit"
// Add key event controller to handle Escape key press
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);
// Show the main window
gtk_window_present(GTK_WINDOW(window));
} }
// Main function
int main(int argc, char **argv) { int main(int argc, char **argv) {
GtkApplication *app; GtkApplication *app;
int status; int status;
app = gtk_application_new("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS); // Register GResources (ensure resources.c/h are compiled and linked)
// Verify 'resources_get_resource()' matches the function in generated resources.c
g_resources_register(resources_get_resource());
// Create the GTK application instance
// Use reverse DNS notation for the application ID
app = gtk_application_new("com.kekepower.ssdd", G_APPLICATION_DEFAULT_FLAGS);
// Connect the activate signal to the activate function
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL); g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
// Run the application
status = g_application_run(G_APPLICATION(app), argc, argv); status = g_application_run(G_APPLICATION(app), argc, argv);
// Clean up the application object
g_object_unref(app); g_object_unref(app);
return status; // Optional: Unregister resources (usually handled automatically on exit)
// g_resources_unregister(resources_get_resource());
return status; // Return the application exit status
} }

BIN
ssdd.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB