Compare commits
21 Commits
Author | SHA1 | Date | |
---|---|---|---|
08e2988776 | |||
020aac91ff | |||
8ae0b72624 | |||
65946c76fc | |||
152a0d648c | |||
40f35759b9 | |||
2a53ed3845 | |||
8edde79fa0 | |||
19c42e9a52 | |||
7e9b159204 | |||
872159b8ab | |||
875c768cea | |||
9e08851964 | |||
3a16bc04d5 | |||
76ae1590f6 | |||
20113cc0c9 | |||
0e92c38db0 | |||
9d98d6056f | |||
a54cac53f2 | |||
2d10e570be | |||
ef25b54a22 |
2
.gitignore
vendored
2
.gitignore
vendored
@ -1 +1,3 @@
|
|||||||
ssdd
|
ssdd
|
||||||
|
resources.c
|
||||||
|
resources.h
|
||||||
|
52
Makefile
Normal file
52
Makefile
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
# Compiler
|
||||||
|
CC = gcc
|
||||||
|
|
||||||
|
# Compiler and optimization flags
|
||||||
|
CFLAGS = `pkg-config --cflags gtk+-3.0` -O3
|
||||||
|
LDFLAGS = `pkg-config --libs gtk+-3.0`
|
||||||
|
|
||||||
|
# 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 $(BINDIR)
|
||||||
|
install -m 755 $(TARGET) $(BINDIR)
|
||||||
|
install -d $(DATADIR)
|
||||||
|
install -m 644 $(RESOURCE_XML) $(DATADIR)
|
||||||
|
|
||||||
|
# Uninstall target
|
||||||
|
uninstall:
|
||||||
|
rm -f $(BINDIR)/$(TARGET)
|
||||||
|
rm -rf $(DATADIR)
|
||||||
|
|
||||||
|
# Clean target
|
||||||
|
clean:
|
||||||
|
rm -f $(TARGET) $(RESOURCE_C) $(RESOURCE_H)
|
||||||
|
|
||||||
|
.PHONY: all clean install uninstall
|
76
README.md
76
README.md
@ -1,42 +1,80 @@
|
|||||||
# 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
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Why?
|
**Simple Shutdown Dialog (ssdd)** is a simple yet stylish shutdown dialog for Openbox, crafted in C using GTK.
|
||||||
|
|
||||||
Been using Openbox many many years and loving it. One of the main issue I have with it is its incredibly simple Exit dialog. On a modern system, a better solution is needed.
|
## Why ssdd?
|
||||||
I guess there are many programs such as this one out there, but one more doesn't hurt, does it?
|
|
||||||
|
|
||||||
Anyway, 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.
|
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?
|
||||||
|
|
||||||
## Dependencies and compilation
|
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.
|
||||||
|
|
||||||
This app requires GTK+ 3.0, Glib 2 development libraries and gcc or clang.
|
## Features
|
||||||
|
|
||||||
I am using this command to compile the program:
|
* **Clean and Intuitive Interface:** ssdd presents a clear choice between Shutdown, Reboot, Logout, and Exit options.
|
||||||
|
* **Clean and minimal code:** `ssdd` is built on a clean and minimal codebase, making it easy to maintain, understand, and extend.
|
||||||
|
* **Lightweight and Efficient:** ssdd is designed to be fast and resource-friendly, perfectly suited for Openbox's minimalist philosophy.
|
||||||
|
|
||||||
|
## Dependencies and Compilation
|
||||||
|
|
||||||
|
ssdd requires:
|
||||||
|
|
||||||
|
* GTK+ 3.0
|
||||||
|
* 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
|
|
||||||
|
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
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Using GCC:
|
||||||
|
% gcc ssdd.c resources.c -o ssdd `pkg-config --cflags --libs gtk+-3.0`
|
||||||
|
|
||||||
|
# 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 gtk+-3.0`
|
||||||
```
|
```
|
||||||
|
|
||||||
This produces the binary `ssdd` which you can place in your $PATH. I place mine in `~/bin`. You can strip if, but it'll only save you a few kilobytes, so it's basically unnecessary...
|
Place the `ssdd` binary in your `$PATH` (e.g., `~/bin`).
|
||||||
|
|
||||||
## Configure Openbox to use it.
|
### Integrate with Openbox
|
||||||
|
|
||||||
`% sudo nvim /etc/xdg/openbox/menu.xml`
|
1. Edit your Openbox menu:
|
||||||
|
|
||||||
Find the line with the standard Openbox Exit option and change it to
|
```bash
|
||||||
|
% sudo nvim /etc/xdg/openbox/menu.xml
|
||||||
|
```
|
||||||
|
|
||||||
`<item label="Log Out"><action name="Execute"><execute>ssdd</execute></item>`
|
2. Replace the default Exit entry with:
|
||||||
|
|
||||||
The reconfigure Openbox to use the new setting.
|
```xml
|
||||||
|
<item label="Log Out"><action name="Execute"><execute>ssdd</execute></action></item>
|
||||||
|
```
|
||||||
|
|
||||||
`% openbox --reconfigure`
|
3. Reconfigure Openbox:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
% openbox --reconfigure
|
||||||
|
```
|
||||||
|
|
||||||
|
### Contributing
|
||||||
|
|
||||||
|
Contributions are welcome! Feel free to open issues or submit pull requests.
|
||||||
|
3844
resources.c
3844
resources.c
File diff suppressed because it is too large
Load Diff
@ -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>
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
#ifndef __RESOURCE_resources_H__
|
|
||||||
#define __RESOURCE_resources_H__
|
|
||||||
|
|
||||||
#include <gio/gio.h>
|
|
||||||
|
|
||||||
extern GResource *resources_get_resource (void);
|
|
||||||
#endif
|
|
353
ssdd.c
353
ssdd.c
@ -1,36 +1,191 @@
|
|||||||
#include <gtk/gtk.h>
|
#include <gtk/gtk.h>
|
||||||
#include <stdlib.h>
|
#include <glib.h>
|
||||||
#include "resources.h" // Include the generated resource header
|
#include <glib/gstdio.h>
|
||||||
|
#include "resources.h"
|
||||||
|
|
||||||
|
// Function declarations (prototypes)
|
||||||
|
static void execute_command(const gchar *command);
|
||||||
|
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 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 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) {
|
||||||
int ret = system(command);
|
GError *error = NULL;
|
||||||
if (ret != 0) {
|
gboolean ret = g_spawn_command_line_async(command, &error);
|
||||||
|
if (!ret) {
|
||||||
GtkWidget *dialog;
|
GtkWidget *dialog;
|
||||||
dialog = gtk_message_dialog_new(NULL,
|
dialog = gtk_message_dialog_new(NULL,
|
||||||
GTK_DIALOG_DESTROY_WITH_PARENT,
|
GTK_DIALOG_DESTROY_WITH_PARENT,
|
||||||
GTK_MESSAGE_ERROR,
|
GTK_MESSAGE_ERROR,
|
||||||
GTK_BUTTONS_CLOSE,
|
GTK_BUTTONS_CLOSE,
|
||||||
"Error executing command: %s",
|
"Error executing command: %s\n%s",
|
||||||
command);
|
command,
|
||||||
|
error->message);
|
||||||
gtk_dialog_run(GTK_DIALOG(dialog));
|
gtk_dialog_run(GTK_DIALOG(dialog));
|
||||||
gtk_widget_destroy(dialog);
|
gtk_widget_destroy(dialog);
|
||||||
|
g_error_free(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void show_confirmation_dialog(GtkWidget *widget, const gchar *label, const gchar *command) {
|
static void show_settings_dialog(GtkWidget *widget) {
|
||||||
|
GtkWidget *dialog;
|
||||||
|
GtkWidget *content_area;
|
||||||
|
GtkWidget *notebook;
|
||||||
|
GtkWidget *settings_tab;
|
||||||
|
GtkWidget *about_tab;
|
||||||
|
GtkWidget *close_button;
|
||||||
|
|
||||||
|
dialog = gtk_dialog_new_with_buttons("Settings",
|
||||||
|
NULL,
|
||||||
|
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) {
|
||||||
|
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");
|
||||||
|
gtk_image_set_pixel_size(GTK_IMAGE(image), 250);
|
||||||
|
gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0);
|
||||||
|
|
||||||
|
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_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void button_clicked(GtkWidget *widget, gpointer data) {
|
||||||
|
const gchar *command = (const gchar *)data;
|
||||||
|
const gchar *label = g_object_get_data(G_OBJECT(widget), "label");
|
||||||
|
|
||||||
|
if (g_strcmp0(command, "exit") == 0) {
|
||||||
|
g_application_quit(G_APPLICATION(g_object_get_data(G_OBJECT(widget), "app")));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (g_strcmp0(command, "settings") == 0) {
|
||||||
|
show_settings_dialog(widget);
|
||||||
|
} else {
|
||||||
|
GtkWidget *window = gtk_widget_get_toplevel(widget);
|
||||||
|
show_confirmation_dialog(window, label, command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data) {
|
||||||
|
if (event->keyval == GDK_KEY_Escape) {
|
||||||
|
g_application_quit(G_APPLICATION(data));
|
||||||
|
return TRUE; // Event handled
|
||||||
|
}
|
||||||
|
return FALSE; // Event not handled
|
||||||
|
}
|
||||||
|
|
||||||
|
static void show_confirmation_dialog(GtkWidget *parent_window, const gchar *label, const gchar *command) {
|
||||||
GtkWidget *dialog;
|
GtkWidget *dialog;
|
||||||
gint response;
|
gint response;
|
||||||
|
|
||||||
gchar *message = g_strdup_printf("Are you sure you want to %s?", label);
|
gchar *message = g_strdup_printf("Are you sure you want to %s?", label);
|
||||||
|
|
||||||
dialog = gtk_message_dialog_new(NULL,
|
dialog = gtk_dialog_new();
|
||||||
GTK_DIALOG_DESTROY_WITH_PARENT,
|
gtk_window_set_title(GTK_WINDOW(dialog), "Confirmation");
|
||||||
GTK_MESSAGE_QUESTION,
|
|
||||||
GTK_BUTTONS_NONE,
|
gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent_window));
|
||||||
"%s",
|
|
||||||
message);
|
gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
|
||||||
gtk_dialog_add_button(GTK_DIALOG(dialog), "Yes", GTK_RESPONSE_YES);
|
gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
|
||||||
gtk_dialog_add_button(GTK_DIALOG(dialog), "No", GTK_RESPONSE_NO);
|
|
||||||
|
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));
|
response = gtk_dialog_run(GTK_DIALOG(dialog));
|
||||||
gtk_widget_destroy(dialog);
|
gtk_widget_destroy(dialog);
|
||||||
@ -41,70 +196,6 @@ static void show_confirmation_dialog(GtkWidget *widget, const gchar *label, cons
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void show_about_dialog(GtkWidget *widget) {
|
|
||||||
GtkWidget *dialog;
|
|
||||||
GtkWidget *content_area;
|
|
||||||
GtkWidget *label;
|
|
||||||
GtkWidget *image;
|
|
||||||
GtkWidget *box;
|
|
||||||
const gchar *about_text =
|
|
||||||
"\n<b>About Stig's ShutDown Dialog</b>\n\n"
|
|
||||||
"<b>Version:</b> 1.2\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",
|
|
||||||
NULL,
|
|
||||||
GTK_DIALOG_DESTROY_WITH_PARENT,
|
|
||||||
"_Close",
|
|
||||||
GTK_RESPONSE_CLOSE,
|
|
||||||
NULL);
|
|
||||||
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");
|
|
||||||
gtk_image_set_pixel_size(GTK_IMAGE(image), 250); // Assuming original size is 500x500
|
|
||||||
gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0);
|
|
||||||
|
|
||||||
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_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) {
|
|
||||||
const gchar *command = (const gchar *) data;
|
|
||||||
const gchar *label = gtk_button_get_label(GTK_BUTTON(widget));
|
|
||||||
|
|
||||||
if (g_strcmp0(command, "exit") == 0) {
|
|
||||||
g_application_quit(G_APPLICATION(g_object_get_data(G_OBJECT(widget), "app")));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (g_strcmp0(command, "about") == 0) {
|
|
||||||
show_about_dialog(widget);
|
|
||||||
} else {
|
|
||||||
show_confirmation_dialog(widget, label, command);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data) {
|
|
||||||
if (event->keyval == GDK_KEY_Escape) {
|
|
||||||
g_application_quit(G_APPLICATION(data));
|
|
||||||
return TRUE; // Event handled
|
|
||||||
}
|
|
||||||
return FALSE; // Event not handled
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
||||||
GtkWidget *button;
|
GtkWidget *button;
|
||||||
GtkWidget *box;
|
GtkWidget *box;
|
||||||
@ -112,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, 5);
|
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);
|
||||||
@ -121,24 +212,73 @@ 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);
|
||||||
|
|
||||||
|
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), "app", app);
|
||||||
g_signal_connect(button, "clicked", G_CALLBACK(button_clicked), (gpointer) command);
|
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);
|
gtk_grid_attach(GTK_GRID(grid), button, pos % 4, pos / 4, 1, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void save_configuration(const gchar *commands[]) {
|
||||||
|
GError *error = NULL;
|
||||||
|
g_mkdir_with_parents(CONFIG_DIR, 0755);
|
||||||
|
|
||||||
|
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",
|
||||||
|
"systemctl reboot",
|
||||||
|
"systemctl poweroff",
|
||||||
|
"dm-tool switch-to-greeter",
|
||||||
|
"systemctl suspend",
|
||||||
|
"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 void activate(GtkApplication *app, gpointer user_data) {
|
||||||
GtkWidget *window;
|
GtkWidget *window;
|
||||||
GtkWidget *grid;
|
GtkWidget *grid;
|
||||||
const gchar *buttons[] = {
|
gchar *commands[6];
|
||||||
"openbox --exit",
|
load_configuration(commands);
|
||||||
"systemctl reboot",
|
|
||||||
"systemctl poweroff",
|
|
||||||
"dm-tool switch-to-greeter",
|
|
||||||
"systemctl suspend",
|
|
||||||
"systemctl hibernate",
|
|
||||||
"about",
|
|
||||||
"exit"
|
|
||||||
};
|
|
||||||
const gchar *icons[] = {
|
const gchar *icons[] = {
|
||||||
"system-log-out",
|
"system-log-out",
|
||||||
"view-refresh",
|
"view-refresh",
|
||||||
@ -146,7 +286,7 @@ 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[] = {
|
const gchar *labels[] = {
|
||||||
@ -156,27 +296,37 @@ static void activate(GtkApplication *app, gpointer user_data) {
|
|||||||
"Switch User",
|
"Switch User",
|
||||||
"Suspend",
|
"Suspend",
|
||||||
"Hibernate",
|
"Hibernate",
|
||||||
"About",
|
"Settings",
|
||||||
"Exit"
|
"Exit"
|
||||||
};
|
};
|
||||||
|
|
||||||
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_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
|
||||||
|
|
||||||
// Load the icon from the resource and set it as the window icon
|
GError *error = NULL;
|
||||||
GdkPixbuf *icon_pixbuf = gdk_pixbuf_new_from_resource("/org/gtk/example/ssdd-icon.png", NULL);
|
GdkPixbuf *icon_pixbuf = gdk_pixbuf_new_from_resource("/org/gtk/ssdd/ssdd-icon.png", &error);
|
||||||
gtk_window_set_icon(GTK_WINDOW(window), icon_pixbuf);
|
if (icon_pixbuf) {
|
||||||
g_object_unref(icon_pixbuf); // Free the icon pixbuf after setting it
|
gtk_window_set_icon(GTK_WINDOW(window), icon_pixbuf);
|
||||||
|
g_object_unref(icon_pixbuf);
|
||||||
|
} else {
|
||||||
|
g_warning("Failed to load icon: %s", error->message);
|
||||||
|
g_error_free(error);
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
@ -185,10 +335,9 @@ 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.example", G_APPLICATION_DEFAULT_FLAGS);
|
app = gtk_application_new("org.gtk.ssdd", 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);
|
status = g_application_run(G_APPLICATION(app), argc, argv);
|
||||||
g_object_unref(app);
|
g_object_unref(app);
|
||||||
|
Reference in New Issue
Block a user