Skip to content

Commit

Permalink
Merge pull request #6 from govnorice/fix
Browse files Browse the repository at this point in the history
system() replaced with execl()
  • Loading branch information
progzone122 committed Apr 24, 2024
2 parents 149e790 + a403606 commit 8c0f786
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
30 changes: 24 additions & 6 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <iostream>
#include <gtk/gtk.h>
#include <fstream>
#include <unistd.h>

// Modules
#include "modules/CssManager.h"
Expand All @@ -22,8 +22,12 @@ const char *homeDir = getenv("HOME");
ActionWidget poweroff, suspend, reboot;

void power_clicked(GtkWidget *widget, gpointer data) {
char *command = (char *) data;
system(command);
auto *pw = (ActionWidget::PowerData *)data;
const char *path = pw->path.c_str();
const char *arg1 = pw->arg1.c_str();
const char *arg2 = pw->arg2.c_str() ? nullptr : pw->arg2.c_str();

execl(path, arg1, arg2, (char *)nullptr);
}

void gui(int argc, char *argv[]) {
Expand All @@ -43,9 +47,23 @@ void gui(int argc, char *argv[]) {
suspend.init("suspend", *imageData_suspend, IMAGE_WIDTH, IMAGE_HEIGHT, BYTES_PER_PIXEL, ACTION_WIDTH, ACTION_HEIGHT);
reboot.init("reboot", *imageData_reboot, IMAGE_WIDTH, IMAGE_HEIGHT, BYTES_PER_PIXEL, ACTION_WIDTH, ACTION_HEIGHT);

poweroff.onClicked(G_CALLBACK(power_clicked), (gpointer *)"poweroff");
suspend.onClicked(G_CALLBACK(power_clicked), (gpointer *)"systemctl suspend");
reboot.onClicked(G_CALLBACK(power_clicked), (gpointer *)"reboot");
// Clicked widget
ActionWidget::PowerData pw1;
pw1.path = "/sbin/poweroff";
pw1.arg1 = "poweroff";

ActionWidget::PowerData pw2;
pw2.path = "/usr/bin/systemctl";
pw2.arg1 = "systemctl";
pw2.arg2 = "suspend";

ActionWidget::PowerData pw3;
pw3.path = "/sbin/reboot";
pw3.arg1 = "reboot";

poweroff.onClicked(G_CALLBACK(power_clicked), &pw1);
suspend.onClicked(G_CALLBACK(power_clicked), &pw2);
reboot.onClicked(G_CALLBACK(power_clicked), &pw3);

gtk_container_add(GTK_CONTAINER(window), container);
poweroff.addToBox(container);
Expand Down
4 changes: 2 additions & 2 deletions widgets/ActionWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ void ActionWidget::addToBox(GtkWidget *box) {
}
}

void ActionWidget::onClicked(GCallback callback_func, gpointer *command) {
g_signal_connect(button, "clicked", G_CALLBACK(callback_func), (gpointer)command);
void ActionWidget::onClicked(GCallback callback_func, PowerData *pw) {
g_signal_connect(button, "clicked", G_CALLBACK(callback_func), (gpointer)pw);
}
9 changes: 8 additions & 1 deletion widgets/ActionWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@

#include <iostream>
#include <gtk/gtk.h>
#include <string>

using namespace std;

// Widget (poweroff, suspend, reboot)
class ActionWidget {
public:
typedef struct {
string path;
string arg1;
string arg2;
} PowerData;

ActionWidget();
void init(const char *name, guchar *data, int width, int height, int bytes_per_pixel, int size_width, int size_height);
void addToBox(GtkWidget *box);
void onClicked(GCallback callback_func, gpointer *command);
void onClicked(GCallback callback_func, PowerData *pw);
private:
GdkPixbuf *image_pixbuf;
GtkWidget *image;
Expand Down

0 comments on commit 8c0f786

Please sign in to comment.