-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
114 lines (96 loc) · 2.98 KB
/
util.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
107
108
109
110
111
112
113
114
/*
* util.c
*
* Copyright (C) 2020 DirtBagXon
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdbool.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gtk/gtkx.h>
#include <ctype.h>
#include <sys/mman.h>
GtkWidget *window;
char binary[64];
void run_game(char *GAME);
void on_destroy()
{
gtk_main_quit();
}
void on_start_clicked(GtkButton *b)
{
run_game(binary);
}
void on_logobutton_clicked(GtkButton *b)
{
on_destroy();
}
void display_error(char *ERROR)
{
GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT;
GtkWidget *dialog = gtk_message_dialog_new(NULL, flags, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", ERROR);
gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(window));
gtk_window_set_title(GTK_WINDOW(dialog), "Error");
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy (dialog);
return;
}
void loadResCSS(char *CSS)
{
GtkCssProvider *provider;
GdkDisplay *display;
GdkScreen *screen;
provider = gtk_css_provider_new ();
display = gdk_display_get_default ();
screen = gdk_display_get_default_screen (display);
gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
// Debug route
// const gchar *myCssFile = "mystyle.css";
// GError *error = 0;
// gtk_css_provider_load_from_file(provider, g_file_new_for_path(myCssFile), &error);
// gtk_css_provider_load_from_resource(provider, "/id/css/main.css");
gtk_css_provider_load_from_resource(provider, CSS);
g_object_unref (provider);
}
const char * check_fd(int fd)
{
char line[1024];
char buffer[1024];
char *s_ptr = line;
ssize_t nbytes = read(fd, buffer, sizeof(buffer));
if (nbytes <= 0) {
return "EOF";
} else {
sprintf(line, "%.*s\n", (int)nbytes - 1, buffer);
return s_ptr;
}
}
void watch_game(GPid pid, gint status, gpointer user_data)
{
char err_line[1024] = "";
g_autoptr(GError) game_error = NULL;
gint fd = GPOINTER_TO_INT(user_data);
sprintf(err_line, "%s", check_fd(fd));
if (strstr(err_line, "EOF") == NULL) display_error(err_line);
if (g_spawn_check_exit_status (status, &game_error) != TRUE ) {
display_error(game_error->message);
}
g_spawn_close_pid (pid);
}