From 440644eac809774bf0f0bf05ee857d90d78a2233 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stig-=C3=98rjan=20Smelror?= Date: Fri, 28 Jun 2024 16:30:52 +0200 Subject: [PATCH] Initial commit of ssdd --- README.md | 27 ++++++++- ssdd.c | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 ssdd.c diff --git a/README.md b/README.md index 03734b0..ee7428e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,28 @@ # ssdd -A simple Shutdown Dialog for Openbox written in C using GTK \ No newline at end of file +A simple Shutdown Dialog for Openbox written in C using GTK + +## Why? + +I just bought a new laptop and on my workstation I was using ssd from Sawfish 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. + +## Dependencies and compilation + +This app requires GTK+ 3.0 development libraries and gcc. + +I am using this command to compile the program: +```shell +% gcc ssdd.c -o ssdd `pkg-config --cflags --libs gtk+-3.0` +``` + +This produces the binary `ssdd` which you can place in your $PATH. + +## Configure Openbox to use it. + +`% sudo nvim /etc/xdg/openbox/menu.xml` + +Find the line with the standard Openbox Exit option and change it to +` /home/stig/bin/ssdd` + +The reconfigure Openbox to use the new setting. +`% openbox --reconfigure` diff --git a/ssdd.c b/ssdd.c new file mode 100644 index 0000000..887037d --- /dev/null +++ b/ssdd.c @@ -0,0 +1,163 @@ +#include +#include + +static void execute_command(const gchar *command) { + int ret = system(command); + if (ret != 0) { + GtkWidget *dialog; + dialog = gtk_message_dialog_new(NULL, + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_ERROR, + GTK_BUTTONS_CLOSE, + "Error executing command: %s", + command); + gtk_dialog_run(GTK_DIALOG(dialog)); + gtk_widget_destroy(dialog); + } +} + +static void show_confirmation_dialog(GtkWidget *widget, gpointer data) { + const gchar *command = (const gchar *) data; + GtkWidget *dialog; + gint response; + + dialog = gtk_message_dialog_new(NULL, + GTK_DIALOG_DESTROY_WITH_PARENT, + 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)); + gtk_widget_destroy(dialog); + + if (response == GTK_RESPONSE_YES) { + execute_command(command); + } +} + +static void show_about_dialog(GtkWidget *widget) { + GtkWidget *dialog; + GtkWidget *content_area; + GtkWidget *label; + const gchar *about_text = + "\nAbout Stig's ShutDown Dialog\n\n" + "Version: 1.0\n" + "Author: kekePower\n" + "URL: https://git.kekepower.com/kekePower/ssdd\n" + "Description: This is a simple Shutdown Dialog for Openbox.\n"; + + dialog = gtk_dialog_new_with_buttons("About Stig's ShutDown Dialog", + NULL, + GTK_DIALOG_DESTROY_WITH_PARENT, + "_Close", + GTK_RESPONSE_CLOSE, + NULL); + content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); + label = gtk_label_new(NULL); + gtk_label_set_markup(GTK_LABEL(label), about_text); + gtk_label_set_selectable(GTK_LABEL(label), TRUE); + gtk_widget_set_halign(label, GTK_ALIGN_START); + gtk_widget_set_valign(label, GTK_ALIGN_START); + gtk_container_add(GTK_CONTAINER(content_area), label); + gtk_widget_show_all(dialog); + + gtk_dialog_run(GTK_DIALOG(dialog)); + gtk_widget_destroy(dialog); +} + +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; + } + + if (g_strcmp0(command, "openbox --exit") == 0 || + g_strcmp0(command, "systemctl reboot") == 0 || + g_strcmp0(command, "systemctl poweroff") == 0) { + show_confirmation_dialog(widget, (gpointer) command); + } else if (g_strcmp0(command, "about") == 0) { + show_about_dialog(widget); + } else { + execute_command(command); + } +} + +static void activate(GtkApplication *app, gpointer user_data) { + GtkWidget *window; + 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); + gtk_window_set_title(GTK_WINDOW(window), "Exit Openbox"); + gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); + + grid = gtk_grid_new(); + gtk_container_add(GTK_CONTAINER(window), grid); + + for (int i = 0; i < 8; i++) { + button = gtk_button_new(); + box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5); + gtk_container_add(GTK_CONTAINER(button), box); + + 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); + + 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); +} + +int main(int argc, char **argv) { + GtkApplication *app; + int status; + + app = gtk_application_new("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS); + g_signal_connect(app, "activate", G_CALLBACK(activate), NULL); + status = g_application_run(G_APPLICATION(app), argc, argv); + g_object_unref(app); + + return status; +}