-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.c
106 lines (84 loc) · 3.07 KB
/
gui.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "complex.h"
#include <ctype.h>
#include <gtk/gtk.h>
#include <unistd.h>
// Function declarations
void on_button_clicked(GtkWidget *widget, gpointer data);
extern void run_commands();
// Activate function: Sets up the GTK window and widgets
void activate(GtkApplication *app, gpointer user_data) {
GtkWidget *window;
GtkWidget *button;
GtkWidget *entry;
GtkWidget *box;
// Create a new window, and set its title
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "Complex Calculator");
// Construct the container
box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2);
gtk_container_add(GTK_CONTAINER(window), box);
// Create a new text entry field
entry = gtk_entry_new();
gtk_container_add(GTK_CONTAINER(box), entry);
// Create a new button
button = gtk_button_new_with_label("Enter");
gtk_container_add(GTK_CONTAINER(box), button);
// Connect the button's "clicked" signal to the on_button_clicked function
g_signal_connect(button, "clicked", G_CALLBACK(on_button_clicked), entry);
// Show all widgets within the window
gtk_widget_show_all(window);
}
void on_button_clicked(GtkWidget *widget, gpointer data) {
int pipefd[2] = {-1, -1};
pid_t pid = -1;
if (pipefd[0] == -1 && pipefd[1] == -1) {
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) { // Child process
close(pipefd[1]); // Close the write end of the pipe
dup2(pipefd[0], STDIN_FILENO); // Redirect stdin
close(pipefd[0]); // Close the original read end
// Execute commands
run_commands();
exit(EXIT_SUCCESS);
} else { // Parent process
close(pipefd[0]); // Close the read end of the pipe
}
}
char *input = (char *) gtk_entry_get_text(GTK_ENTRY(data));
char *input_with_newline = malloc(strlen(input) + 2);
if (input_with_newline == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
strcpy(input_with_newline, input);
input_with_newline[strlen(input)] = '\n';
if (write(pipefd[1], input_with_newline, strlen(input_with_newline)) ==
-1) {
perror("write");
exit(EXIT_FAILURE);
}
// Clear the input entry field
gtk_entry_set_text(GTK_ENTRY(data), "");
free(input_with_newline);
// No need to wait for the child process here, as it should keep running
// TODO: Add a way to kill the child process and close the pipe
}
// Main function
int main(int argc, char **argv) {
GtkApplication *app;
int status;
app = gtk_application_new("my.calc", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
if ((status = g_application_run(G_APPLICATION(app), argc, argv)))
perror("g_application_run");
g_object_unref(app);
return status;
}