Debian Codegraph @

terps/alan3/glkstart.c

raw symbols SCIP
1 /* glkstart.c: Unix-specific startup code
2 Adapted for Alan by Joe Mason <[email protected]>
3 And tweaked by Thomas Nilefalk <[email protected]>
4
5 Based on the sample file designed by
6 Andrew Plotkin <[email protected]>
7 http://www.eblong.com/zarf/glk/index.html
8
9 Original release note follows:
10
11 This is Unix startup code for the simplest possible kind of Glk
12 program -- no command-line arguments; no startup files; no nothing.
13
14 Remember, this is a sample file. You should copy it into the Glk
15 program you are compiling, and modify it to your needs. This should
16 *not* be compiled into the Glk library itself.
17 */
18
19 #include "args.h"
20 #include "main.h"
21 #include "glk.h"
22 #include "glkstart.h"
23 #include "glkio.h"
24 #include "resources.h"
25 #include "gi_blorb.h"
26 #include "utils.h"
27
28 #ifdef HAVE_WINGLK
29 #include "WinGlk.h"
30 #include <windows.h>
31 #endif
32
33
34 #ifdef HAVE_GARGLK
35 #include "alan.version.h"
36 #endif
37
38 glkunix_argumentlist_t glkunix_arguments[] = {
39 { "-h", glkunix_arg_NoValue, "this help" },
40 { "-u", glkunix_arg_NoValue, "use UTF-8 encoding for input and output" },
41 { "-i", glkunix_arg_NoValue, "use ISO8859-1 encoding for input and output" },
42 { "-v", glkunix_arg_NoValue, "verbose mode" },
43 { "-l", glkunix_arg_NoValue, "log game transcript to a file ('.a3t')" },
44 { "-c", glkunix_arg_NoValue, "log player command input (the solution) to a file ('.a3s')" },
45 { "-n", glkunix_arg_NoValue, "don't show the Status Line" },
46 { "-p", glkunix_arg_NoValue, "don't page output" },
47 { "-d", glkunix_arg_NoValue, "enter debug mode immediately" },
48 { "-t", glkunix_arg_ValueCanFollow, "[<n>] trace game execution, higher <n> gives more trace" },
49 { "-r", glkunix_arg_NoValue, "make regression testing easier (don't timestamp, page break, randomize...)" },
50 { "-e", glkunix_arg_NoValue, "ignore version and checksum errors (dangerous)" },
51 { "--version", glkunix_arg_NoValue, "print version and exit" },
52 { "", glkunix_arg_ValueFollows, "filename: The game file to load." },
53 { NULL, glkunix_arg_End, NULL }
54 };
55
56 /* Resources */
57 static strid_t resourceFile;
58
59 /*----------------------------------------------------------------------*/
60 static void openGlkWindows() {
61 glkMainWin = glk_window_open(0, 0, 0, wintype_TextBuffer, 0);
62 if (glkMainWin == NULL) {
63 printf("FATAL ERROR: Cannot open initial window");
64 glk_exit();
65 }
66 #ifdef HAVE_GARGLK
67 glk_stylehint_set (wintype_TextGrid, style_User1, stylehint_ReverseColor, 1);
68 #endif
69 glkStatusWin = glk_window_open(glkMainWin, winmethod_Above |
70 winmethod_Fixed, 1, wintype_TextGrid, 0);
71 glk_set_window(glkStatusWin);
72 glk_set_style(style_Preformatted);
73 glk_set_window(glkMainWin);
74 }
75
76 /*----------------------------------------------------------------------*/
77 static void openResourceFile() {
78 char *originalFileName = strdup(adventureFileName);
79 char *resourceFileName = originalFileName;
80 char *extension = strrchr(resourceFileName, '.');
81 frefid_t resourceFileRef;
82 giblorb_err_t ecode;
83
84 #ifdef HAVE_GARGLK
85 if (strrchr(resourceFileName, '/'))
86 resourceFileName = strrchr(resourceFileName, '/') + 1;
87 else if (strrchr(resourceFileName, '\\'))
88 resourceFileName = strrchr(resourceFileName, '\\') + 1;
89 if (!resourceFileName)
90 resourceFileName = originalFileName;
91
92 if (extension)
93 strcpy(extension, ".a3r");
94 else
95 strcat(resourceFileName, ".a3r");
96 #else
97 strcpy(extension, ".a3r");
98 #endif
99
100 #ifdef HAVE_WINGLK
101 resourceFileRef = winglk_fileref_create_by_name(fileusage_BinaryMode,
102 resourceFileName, 0, false);
103 #else
104 resourceFileRef = glk_fileref_create_by_name(fileusage_BinaryMode,
105 resourceFileName, 0);
106 #endif
107 if (glk_fileref_does_file_exist(resourceFileRef)) {
108 resourceFile = glk_stream_open_file(resourceFileRef, filemode_Read, 0);
109 ecode = giblorb_set_resource_map(resourceFile);
110 (void)ecode;
111 }
112 free(originalFileName);
113 }
114
115
116 /*======================================================================*/
117 int glkunix_startup_code(glkunix_startup_t *data)
118 {
119 glk_stylehint_set(wintype_AllTypes, style_Emphasized, stylehint_Weight, 0);
120 glk_stylehint_set(wintype_AllTypes, style_Emphasized, stylehint_Oblique, 1);
121 glk_stylehint_set(wintype_AllTypes, style_BlockQuote, stylehint_Indentation, 10);
122
123 /* first, open a window for error output */
124 openGlkWindows();
125
126 #ifdef HAVE_GARGLK
127 #if (BUILD+0) != 0
128 {
129 char name[100];
130 sprintf(name, "%s-%d", alan.shortHeader, BUILD);
131 garglk_set_program_name(name);
132 }
133 #else
134 garglk_set_program_name(alan.shortHeader);
135 #endif
136 char info[200];
137 #ifdef GIT_VERSION
138 sprintf(info, "%s Interpreter by Thomas Nilefalk\nFrom git %s\n", alan.shortHeader, GIT_VERSION);
139 #else
140 sprintf(info, "%s Interpreter by Thomas Nilefalk\n", alan.shortHeader);
141 #endif
142 garglk_set_program_info(info);
143 #endif
144
145 /* now process the command line arguments */
146 args(data->argc, data->argv);
147
148 if (adventureFileName == NULL || strcmp(adventureFileName, "") == 0) {
149 printf("You should supply a game file to play.\n");
150 usage("arun"); // TODO Find real programname from arguments
151 terminate(1);
152 }
153
154 /* Open any possible blorb resource file */
155 openResourceFile();
156
157 return true;
158 }
159
160
161
162 #ifdef HAVE_WINGLK
163 static int argCount;
164 static char *argumentVector[10];
165
166 /*----------------------------------------------------------------------*/
167 static void splitArgs(char *commandLine) {
168 unsigned char *cp = (unsigned char *)commandLine;
169
170 while (*cp) {
171 while (*cp && isspace(*cp)) cp++;
172 if (*cp) {
173 argumentVector[argCount++] = (char *)cp;
174 if (*cp == '"') {
175 do {
176 cp++;
177 } while (*cp != '"');
178 cp++;
179 } else
180 while (*cp && !isspace(*cp))
181 cp++;
182 if (*cp) {
183 *cp = '\0';
184 cp++;
185 }
186 }
187 }
188 }
189
190
191 /*======================================================================*/
192 int winglk_startup_code(const char* cmdline)
193 {
194 char windowTitle[200];
195
196 /* Process the command line arguments */
197 argumentVector[0] = "";
198 argCount = 1;
199
200 splitArgs(strdup(cmdline));
201
202 args(argCount, argumentVector);
203
204
205 if (adventureFileName == NULL || strcmp(adventureFileName, "") == 0) {
206 adventureFileName = (char*)winglk_get_initial_filename(NULL, "Arun : Select an Alan game file",
207 "Alan Game Files (*.a3c)|*.a3c||");
208 if (adventureFileName == NULL) {
209 terminate(0);
210 }
211 adventureName = gameName(adventureFileName);
212 }
213
214 winglk_app_set_name("WinArun");
215 winglk_set_gui(IDR_ARUN);
216
217 sprintf(windowTitle, "WinArun : %s", adventureName);
218 winglk_window_set_title(windowTitle);
219 openGlkWindows();
220
221 /* Open any possible blorb resource file */
222 openResourceFile();
223
224 return true;
225 }
226 #endif