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

This commit is contained in:
Stig-Ørjan Smelror 2024-08-09 23:02:46 +02:00
parent 020aac91ff
commit e1bed788d2
2 changed files with 155 additions and 53 deletions

View File

@ -33,6 +33,7 @@ Edit the `Makefile` or use the following commands:
```shell ```shell
% make all # Compile % make all # Compile
% sudo make install # Install to /usr/local % sudo make install # Install to /usr/local
% sudo make install PREFIX=/usr # Install to /usr
``` ```
### Manual compilation ### Manual compilation

197
ssdd.c
View File

@ -1,14 +1,22 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <glib.h> #include <glib.h>
#include <glib/gstdio.h>
#include "resources.h" #include "resources.h"
// Function declarations (prototypes) // Function declarations (prototypes)
static void execute_command(const gchar *command); static void execute_command(const gchar *command);
static void show_about_dialog(GtkWidget *widget); static void show_settings_dialog(GtkWidget *widget);
static void show_about_tab(GtkWidget *box);
static void show_settings_tab(GtkWidget *box);
static void button_clicked(GtkWidget *widget, gpointer data); static void button_clicked(GtkWidget *widget, gpointer data);
static gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data); static gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data);
static void show_confirmation_dialog(GtkWidget *widget, const gchar *label, const gchar *command); static void show_confirmation_dialog(GtkWidget *widget, const gchar *label, const gchar *command);
static void create_button(GtkWidget *grid, GtkApplication *app, const gchar *label_text, const gchar *icon_name, const gchar *command, int pos); static void create_button(GtkWidget *grid, GtkApplication *app, const gchar *label_text, const gchar *icon_name, const gchar *command, int pos);
static void save_configuration(const gchar *commands[]);
static void load_configuration(gchar *commands[]);
#define CONFIG_PATH g_build_filename(g_get_home_dir(), ".config/ssdd/config", NULL)
#define CONFIG_DIR g_build_filename(g_get_home_dir(), ".config/ssdd", NULL)
static void execute_command(const gchar *command) { static void execute_command(const gchar *command) {
GError *error = NULL; GError *error = NULL;
@ -28,31 +36,87 @@ static void execute_command(const gchar *command) {
} }
} }
static void show_about_dialog(GtkWidget *widget) { static void show_settings_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 = GtkWidget *close_button;
"\n<b>About Simple ShutDown Dialog</b>\n\n"
"<b>Version:</b> 1.4\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 Simple ShutDown Dialog", dialog = gtk_dialog_new_with_buttons("Settings",
NULL, NULL,
GTK_DIALOG_DESTROY_WITH_PARENT, GTK_DIALOG_DESTROY_WITH_PARENT,
"_Close",
GTK_RESPONSE_CLOSE,
NULL); NULL);
content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add(GTK_CONTAINER(content_area), box); 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) {
const gchar *labels[] = {
"Logout Command",
"Reboot Command",
"Shutdown Command",
"Switch User Command",
"Suspend Command",
"Hibernate Command"
};
gchar *commands[6];
load_configuration(commands);
GtkWidget *grid = gtk_grid_new();
gtk_grid_set_row_spacing(GTK_GRID(grid), 10); // Space between rows
gtk_grid_set_column_spacing(GTK_GRID(grid), 10); // Space between columns
gtk_box_pack_start(GTK_BOX(box), grid, TRUE, TRUE, 0);
for (int i = 0; i < 6; i++) {
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);
gtk_grid_attach(GTK_GRID(grid), entry, 1, i, 1, 1);
}
for (int i = 0; i < 6; i++) {
g_free(commands[i]);
}
}
static void show_about_tab(GtkWidget *box) {
GtkWidget *label;
GtkWidget *image;
const gchar *about_text =
"\n<b>About Simple ShutDown Dialog</b>\n\n"
"<b>Version:</b> 1.5\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";
image = gtk_image_new_from_resource("/org/gtk/ssdd/ssdd-icon.png"); image = gtk_image_new_from_resource("/org/gtk/ssdd/ssdd-icon.png");
gtk_image_set_pixel_size(GTK_IMAGE(image), 250); // Assuming original size is 500x500 gtk_image_set_pixel_size(GTK_IMAGE(image), 250);
gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0);
label = gtk_label_new(NULL); label = gtk_label_new(NULL);
@ -61,11 +125,6 @@ static void show_about_dialog(GtkWidget *widget) {
gtk_widget_set_halign(label, GTK_ALIGN_START); gtk_widget_set_halign(label, GTK_ALIGN_START);
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_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
gtk_widget_show_all(dialog);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
} }
static void button_clicked(GtkWidget *widget, gpointer data) { static void button_clicked(GtkWidget *widget, gpointer data) {
@ -77,8 +136,8 @@ static void button_clicked(GtkWidget *widget, gpointer data) {
return; return;
} }
if (g_strcmp0(command, "about") == 0) { if (g_strcmp0(command, "settings") == 0) {
show_about_dialog(widget); show_settings_dialog(widget);
} else { } else {
GtkWidget *window = gtk_widget_get_toplevel(widget); GtkWidget *window = gtk_widget_get_toplevel(widget);
show_confirmation_dialog(window, label, command); show_confirmation_dialog(window, label, command);
@ -102,7 +161,6 @@ static void show_confirmation_dialog(GtkWidget *parent_window, const gchar *labe
dialog = gtk_dialog_new(); dialog = gtk_dialog_new();
gtk_window_set_title(GTK_WINDOW(dialog), "Confirmation"); gtk_window_set_title(GTK_WINDOW(dialog), "Confirmation");
// Set the transient parent correctly
gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent_window)); gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent_window));
gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
@ -121,13 +179,13 @@ static void show_confirmation_dialog(GtkWidget *parent_window, const gchar *labe
gtk_dialog_add_buttons(GTK_DIALOG(dialog), gtk_dialog_add_buttons(GTK_DIALOG(dialog),
"Yes", GTK_RESPONSE_YES, "Yes", GTK_RESPONSE_YES,
"No", GTK_RESPONSE_NO, "No", GTK_RESPONSE_NO,
NULL); // Add NULL to terminate the list of buttons NULL);
GtkWidget *button_box = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); // Get content area instead of action area GtkWidget *button_box = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
gtk_box_set_spacing(GTK_BOX(button_box), 10); // Adjust spacing as needed gtk_box_set_spacing(GTK_BOX(button_box), 10);
gtk_widget_set_margin_top(button_box, 10); gtk_widget_set_margin_top(button_box, 10);
gtk_widget_show_all(dialog); // Show the dialog and its children gtk_widget_show_all(dialog);
response = gtk_dialog_run(GTK_DIALOG(dialog)); response = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog); gtk_widget_destroy(dialog);
@ -145,7 +203,7 @@ static void create_button(GtkWidget *grid, GtkApplication *app, const gchar *lab
GtkWidget *label; GtkWidget *label;
button = gtk_button_new(); button = gtk_button_new();
box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); // Set internal box spacing to 10 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
gtk_container_add(GTK_CONTAINER(button), box); gtk_container_add(GTK_CONTAINER(button), box);
image = gtk_image_new_from_icon_name(icon_name, GTK_ICON_SIZE_BUTTON); image = gtk_image_new_from_icon_name(icon_name, GTK_ICON_SIZE_BUTTON);
@ -154,7 +212,6 @@ static void create_button(GtkWidget *grid, GtkApplication *app, const gchar *lab
label = gtk_label_new(label_text); label = gtk_label_new(label_text);
gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
// Set margins for the button contents
gtk_widget_set_margin_top(box, 5); gtk_widget_set_margin_top(box, 5);
gtk_widget_set_margin_bottom(box, 5); gtk_widget_set_margin_bottom(box, 5);
gtk_widget_set_margin_start(box, 5); gtk_widget_set_margin_start(box, 5);
@ -166,19 +223,62 @@ static void create_button(GtkWidget *grid, GtkApplication *app, const gchar *lab
gtk_grid_attach(GTK_GRID(grid), button, pos % 4, pos / 4, 1, 1); gtk_grid_attach(GTK_GRID(grid), button, pos % 4, pos / 4, 1, 1);
} }
static void activate(GtkApplication *app, gpointer user_data) { static void save_configuration(const gchar *commands[]) {
GtkWidget *window; GError *error = NULL;
GtkWidget *grid; g_mkdir_with_parents(CONFIG_DIR, 0755);
const gchar *buttons[] = {
gchar *config_data = g_strjoinv("\n", (gchar **)commands);
g_file_set_contents(CONFIG_PATH, config_data, -1, &error);
if (error) {
g_warning("Failed to save configuration: %s", error->message);
g_error_free(error);
}
g_free(config_data);
}
static void load_configuration(gchar *commands[]) {
GError *error = NULL;
gchar *config_data = NULL;
g_mkdir_with_parents(CONFIG_DIR, 0755);
if (!g_file_test(CONFIG_PATH, G_FILE_TEST_EXISTS)) {
const gchar *default_commands[] = {
"openbox --exit", "openbox --exit",
"systemctl reboot", "systemctl reboot",
"systemctl poweroff", "systemctl poweroff",
"dm-tool switch-to-greeter", "dm-tool switch-to-greeter",
"systemctl suspend", "systemctl suspend",
"systemctl hibernate", "systemctl hibernate"
"about",
"exit"
}; };
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) {
GtkWidget *window;
GtkWidget *grid;
gchar *commands[6];
load_configuration(commands);
const gchar *icons[] = { const gchar *icons[] = {
"system-log-out", "system-log-out",
"view-refresh", "view-refresh",
@ -186,17 +286,17 @@ static void activate(GtkApplication *app, gpointer user_data) {
"system-users", "system-users",
"media-playback-pause", "media-playback-pause",
"media-playback-stop", "media-playback-stop",
"help-about", "preferences-system",
"application-exit" "application-exit"
}; };
const gchar *labels[] = { // Define the labels array here const gchar *labels[] = {
"Logout", "Logout",
"Reboot", "Reboot",
"Shutdown", "Shutdown",
"Switch User", "Switch User",
"Suspend", "Suspend",
"Hibernate", "Hibernate",
"About", "Settings",
"Exit" "Exit"
}; };
@ -204,12 +304,11 @@ static void activate(GtkApplication *app, gpointer user_data) {
gtk_window_set_title(GTK_WINDOW(window), "Simple ShutDown Dialog"); gtk_window_set_title(GTK_WINDOW(window), "Simple ShutDown Dialog");
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
// Load the icon with error handling
GError *error = NULL; GError *error = NULL;
GdkPixbuf *icon_pixbuf = gdk_pixbuf_new_from_resource("/org/gtk/ssdd/ssdd-icon.png", &error); GdkPixbuf *icon_pixbuf = gdk_pixbuf_new_from_resource("/org/gtk/ssdd/ssdd-icon.png", &error);
if (icon_pixbuf) { if (icon_pixbuf) {
gtk_window_set_icon(GTK_WINDOW(window), icon_pixbuf); gtk_window_set_icon(GTK_WINDOW(window), icon_pixbuf);
g_object_unref(icon_pixbuf); // Free the pixbuf g_object_unref(icon_pixbuf);
} else { } else {
g_warning("Failed to load icon: %s", error->message); g_warning("Failed to load icon: %s", error->message);
g_error_free(error); g_error_free(error);
@ -218,13 +317,16 @@ static void activate(GtkApplication *app, gpointer user_data) {
g_signal_connect(window, "key-press-event", G_CALLBACK(on_key_press), app); g_signal_connect(window, "key-press-event", G_CALLBACK(on_key_press), app);
grid = gtk_grid_new(); grid = gtk_grid_new();
gtk_grid_set_row_spacing(GTK_GRID(grid), 0); // Set the row spacing to 0 gtk_grid_set_row_spacing(GTK_GRID(grid), 0);
gtk_grid_set_column_spacing(GTK_GRID(grid), 0); // Set the column spacing to 0 gtk_grid_set_column_spacing(GTK_GRID(grid), 0);
gtk_container_add(GTK_CONTAINER(window), grid); gtk_container_add(GTK_CONTAINER(window), grid);
for (int i = 0; i < 8; i++) { for (int i = 0; i < 6; i++) {
create_button(grid, app, labels[i], icons[i], buttons[i], i); create_button(grid, app, labels[i], icons[i], commands[i], i);
g_free(commands[i]); // Free the memory allocated for commands
} }
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); gtk_widget_show_all(window);
} }
@ -233,7 +335,6 @@ int main(int argc, char **argv) {
GtkApplication *app; GtkApplication *app;
int status; int status;
// Register the resource
g_resources_register(resources_get_resource()); g_resources_register(resources_get_resource());
app = gtk_application_new("org.gtk.ssdd", G_APPLICATION_DEFAULT_FLAGS); app = gtk_application_new("org.gtk.ssdd", G_APPLICATION_DEFAULT_FLAGS);