-
Notifications
You must be signed in to change notification settings - Fork 1
/
xsr1-gtk.c
332 lines (282 loc) · 8.24 KB
/
xsr1-gtk.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* xsr1-gtk.c - Wraps XSR1 in a WebKitGtk widget. */
/* XSR1, a clone of 1979 Atari Star Raiders
* Copyright (C) 2022,2024 Alan Manuel K. Gloria
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <webkit2/webkit2.h>
static char const* alloc_getcwd(void) {
size_t size;
char *cwd;
char *ncwd;
size = 1024;
cwd = malloc(size);
if (!cwd)
return NULL;
for (;;) {
ncwd = getcwd(cwd, size);
if (ncwd)
return cwd;
size = size * 2;
ncwd = realloc(cwd, size);
if (!ncwd) {
free((void*) cwd);
return NULL;
}
cwd = ncwd;
}
}
static char const* index_html = "/index.html";
/* Returns an index.html file in the current directory. */
static char const* cwd_index_html(void) {
char const* cwd;
size_t cwd_len;
size_t index_html_len;
char* path;
cwd = alloc_getcwd();
if (!cwd)
return NULL;
cwd_len = strlen(cwd);
index_html_len = strlen(index_html);
path = malloc(cwd_len + index_html_len + 1);
if (!path) {
free((void*) cwd);
return NULL;
}
/* Concatenate. */
strcpy(path, cwd);
strcat(path, index_html);
free((void*) cwd);
return path;
}
/* Return an index.html file in the PKGDATADIR. */
static char const* pkgdatadir_index_html(void) {
#if !defined(PKGDATADIR)
return NULL;
#else
char* path;
path = malloc(strlen(PKGDATADIR) + strlen(index_html) + 1);
if (!path)
return NULL;
/* Concatenate. */
strcpy(path, PKGDATADIR);
strcat(path, index_html);
return path;
#endif
}
/* The first few lines of our index.html. */
static char const* index_html_start =
"<!DOCTYPE html>\n<html>\n<head>\n<meta charset=utf-8>\n<title>XSR1</title>"
;
/* Checks if the given index.html file is correct. */
static int is_correct_index_html(char const* path) {
FILE* fpath = NULL;
int result;
size_t index_html_start_len;
char* starttext = NULL;
size_t starttext_len;
result = 0;
fpath = fopen(path, "r");
if (!fpath)
return 0;
/* Allocate a buffer. */
index_html_start_len = strlen(index_html_start);
starttext = malloc(index_html_start_len);
if (!starttext)
goto end;
/* Read the initial few lines. If file too short,
* fail. */
starttext_len = fread(starttext, 1, index_html_start_len, fpath);
if (starttext_len != index_html_start_len)
goto end;
/* Check the initial few lines match. */
result = strncmp(starttext, index_html_start,
index_html_start_len) == 0;
end:
if (starttext)
free((void*) starttext);
if (fpath)
fclose(fpath);
return result;
}
/* Looks for our index.html file. */
static char const* find_index_html(void) {
char const* the_index_html;
/* Check for one in the CWD. */
the_index_html = cwd_index_html();
if (the_index_html) {
if (is_correct_index_html(the_index_html))
return the_index_html;
free((char*) the_index_html);
}
/* Check for one in the pkgdatadir. */
the_index_html = pkgdatadir_index_html();
if (the_index_html) {
if (is_correct_index_html(the_index_html))
return the_index_html;
free((char*) the_index_html);
}
return NULL;
}
static char const* url_prefix = "file://";
/* Looks for the URL to load into the webkitgtk widget. */
static char const* find_start_url(void) {
size_t url_prefix_len;
char const* index_html;
size_t index_html_len;
char* url;
index_html = find_index_html();
/* Not found? */
if (!index_html)
return NULL;
/* Allocate space for the URL. */
url_prefix_len = strlen(url_prefix);
index_html_len = strlen(index_html);
url = malloc(url_prefix_len + index_html_len + 1);
if (!url) {
free((void*) index_html);
return NULL;
}
/* Concatenate URL. */
strcpy(url, url_prefix);
strcat(url, index_html);
/* Free up the path. */
free((void*) index_html);
return url;
}
/*****************************************************************************/
static void close_window(GtkWidget* _, GtkWidget* __) {
gtk_main_quit();
}
static gboolean destroy_browser(WebKitWebView* _, GtkWidget* window) {
gtk_widget_destroy(window);
return TRUE;
}
static void
ignore_javascript_result(GObject* _, GAsyncResult* __, gpointer ___) {
return;
}
/* This is invoked whenever the browser has a load event, i.e.
* "load-changed" signal. */
static void
run_javascript_injection(WebKitWebView* browser,
WebKitLoadEvent type,
char const* start_url) {
char const* curr_url;
/* Only invoke this when the game has finished loading. */
if (type != WEBKIT_LOAD_FINISHED)
return;
/* Make sure it is the actual game, not another URL (such as
* the help doc). */
curr_url = webkit_web_view_get_uri(browser);
if (0 != strcmp(start_url, curr_url))
return;
webkit_web_view_evaluate_javascript(browser,
"document.isWebKitGtk = true;", -1,
NULL, NULL, NULL,
&ignore_javascript_result, NULL);
}
/* This is invoked whenever the title is changed.
* If the JavaScript code says "EXIT", quit it.
*
* This hack is needed since window.close() will only
* work if the document was loaded via JavaScript.
* webkit_web_view_load_uri counts as being loaded via
* JavaScript, but if the user selects "HELP" then we
* load the help.html page, and the return to the game
* from help.html is a "normal" link that the WebKit
* engine thinks is "not via JavaScript", so
* window.close() will stop working.
* However, changing title is still allowed and we
* can still see that over here in C-land.
*/
static void
on_title_changed(WebKitWebView* browser,
GParamSpec* _, gpointer __) {
char const* curr_title;
curr_title = webkit_web_view_get_title(browser);
if (0 == strcmp(curr_title, "EXIT")) {
gtk_main_quit();
}
}
int main(int argc, char** argv) {
int exit_code = 1;
char const* start_url = NULL;
gtk_init(&argc, &argv);
if (argc > 1) {
if ((0 == strcmp(argv[1], "--version")) ||
(0 == strcmp(argv[1], "-V"))) {
#if defined(PACKAGE_VERSION)
printf("XSR1 %s\n", PACKAGE_VERSION);
#else
printf("XSR1 Unknown version\n");
#endif
printf("Copyright (C) 2014,2015,2022,2024 Alan Manuel K. Gloria\n"
"This is free software; see the source for copying conditions. There is NO\n"
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
return 0;
}
if ((0 == strcmp(argv[1], "--help")) ||
(0 == strcmp(argv[1], "-H"))) {
printf("Usage: xsr1-gtk [--help|--version]\n");
printf("Report bugs to: <almkglor@gmail.com>\n");
return 0;
}
fprintf(stderr, "%s: Unrecognized option: %s\n",
argv[0], argv[1]);
return 1;
}
start_url = find_start_url();
if (!start_url) {
fprintf(stderr, "%s: Cannot find index.html for XSR1\n",
argv[0]);
goto end;
}
/* Webkit-GTK. */
{
GtkWidget* window;
WebKitWebView* browser;
/* Build the window. */
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "XSR1");
gtk_window_set_default_size(GTK_WINDOW(window), 800, 600);
gtk_window_maximize(GTK_WINDOW(window));
g_signal_connect(window, "destroy", G_CALLBACK(&close_window), NULL);
/* Build the browser. */
browser = WEBKIT_WEB_VIEW(webkit_web_view_new());
g_signal_connect(browser, "close", G_CALLBACK(&destroy_browser), window);
g_signal_connect(browser, "load-changed", G_CALLBACK(&run_javascript_injection), (gpointer) start_url);
g_signal_connect(browser, "notify::title", G_CALLBACK(&on_title_changed), NULL);
webkit_web_view_load_uri(browser, start_url);
/* Put the browser in the window. */
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(browser));
gtk_widget_grab_focus(GTK_WIDGET(browser));
/* Launch. */
gtk_widget_show_all(window);
gtk_main();
}
exit_code = 0;
end:
if (start_url)
free((void*) start_url);
return exit_code;
}