aboutsummaryrefslogtreecommitdiff
path: root/engines/sludge/linuxstuff.cpp
blob: d5b76a5cf0071f6d51923fc4c0663f092a10dab8 (plain)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */
#if defined __unix__ && !(defined __APPLE__)
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <getopt.h>

#include "linuxstuff.h"
#include "platform-dependent.h"
#include "allfiles.h"
#include "language.h" // for settings
#include "debug.h"
#include "helpers.h"

extern settingsStruct gameSettings;
cmdlineSettingsStruct cmdlineSettings;

extern char **languageName;

/*
 * Functions declared in linuxstuff.h:
 */

void printCmdlineUsage() {
	fprintf(stdout, "OpenSLUDGE engine, usage: sludge-engine [<options>] <gamefile name>\n\n");
	fprintf(stdout, "Options:\n");
	fprintf(stdout, "-f,		--fullscreen		Set display mode to fullscreen\n");
	fprintf(stdout, "-w,		--window		Set display mode to windowed\n");
	fprintf(stdout, "-L,		--list-languages	Print available languages and their indices\n");
	fprintf(stdout, "-l<index>,	--language=<index>	Set language to <index> (look up with -L)\n");
	fprintf(stdout, "-a<number>,	--antialias=<number>	Turn antialiasing on (1) or off (0)\n");
	fprintf(stdout, "					or choose linear interpolation (-1)\n");
	fprintf(stdout, "-d<number>,	--debug=<number>	Turn debug mode on (1) or off (0)\n");
	fprintf(stdout, "-h,		--help			Print this help message\n\n");
	fprintf(stdout, "Options are saved, so you don't need to specify them every time.\n");
	fprintf(stdout, "If you entered a wrong language number, use -l0 to reset the language to the default setting.\n");
	fprintf(stdout, "You can always toggle between fullscreen and windowed mode with \"Alt+Enter\"\n");
	fprintf(stdout, "or antialiasing on and off with \"Alt+A\".\n");
}

void printLanguageTable() {
	if (gameSettings.numLanguages) {
		fprintf(stdout, "Index		Language\n");
		for (unsigned int i = 0; i <= gameSettings.numLanguages; i++) {
			if (languageName[i]) {
				fprintf(stdout, "%d		%s\n", i, languageName[i]);
			} else {
				fprintf(stdout, "%d		Language %d\n", i, i);
			}
		}
	} else {
		fprintf(stdout, "No translations available.\n");
	}
}

bool parseCmdlineParameters(int argc, char *argv[]) {
	int retval = true;
	cmdlineSettings.fullscreenSet = false;
	cmdlineSettings.languageSet = false;
	cmdlineSettings.aaSet = false;
	cmdlineSettings.debugModeSet = false;
	cmdlineSettings.listLanguages = false;
	while (1) {
		static struct option long_options[] = {
			{"fullscreen",      no_argument,       0, 'f' },
			{"window",      no_argument,       0, 'w' },
			{"list-languages",  no_argument,       0, 'L' },
			{"language",        required_argument, 0, 'l' },
			{"antialias",       required_argument, 0, 'a' },
			{"debug",       required_argument, 0, 'd' },
			{"help",        no_argument,       0, 'h' },
			{0, 0, 0, 0} /* This is a filler for -1 */
		};
		int option_index = 0;
		int c = getopt_long(argc, argv, "fwLl:a:d:h", long_options, &option_index);
		if (c == -1) break;
		switch (c) {
		case 'f':
			cmdlineSettings.fullscreenSet = true;
			cmdlineSettings.userFullScreen = true;
			break;
		case 'w':
			cmdlineSettings.fullscreenSet = true;
			cmdlineSettings.userFullScreen = false;
			break;
		case 'L':
			cmdlineSettings.listLanguages = true;
			break;
		case 'l':
			cmdlineSettings.languageSet = true;
			cmdlineSettings.languageID = atoi(optarg);
			break;
		case 'a':
			cmdlineSettings.aaSet = true;
			cmdlineSettings.antiAlias = atoi(optarg);
			break;
		case 'd':
			cmdlineSettings.debugModeSet = true;
			cmdlineSettings.debugMode = atoi(optarg);
			break;
		case 'h':
		default:
			retval = false;
			break;
		}
	}
	return retval;
}

/*
 * Functions declared in platform-dependent.h:
 */

char *grabFileName() {
	return NULL;
}

int showSetupWindow() {
	if (cmdlineSettings.listLanguages) {
		printLanguageTable();
		return 0;
	}
	if (cmdlineSettings.languageSet) {
		if (cmdlineSettings.languageID <= gameSettings.numLanguages) {
			gameSettings.languageID = cmdlineSettings.languageID;
		} else {
			fprintf(stdout, "Language index %d doesn't exist. Please specify an index between 0 and %d.\n\n",
			        cmdlineSettings.languageID, gameSettings.numLanguages);
			printLanguageTable();
			return 0;
		}
	}
	if (cmdlineSettings.fullscreenSet) {
		gameSettings.userFullScreen = cmdlineSettings.userFullScreen;
	}
	if (cmdlineSettings.aaSet) {
		gameSettings.antiAlias = cmdlineSettings.antiAlias;
	}
	if (cmdlineSettings.debugModeSet) {
		gameSettings.debugMode = cmdlineSettings.debugMode;
	}
	return 1;
}

void msgBox(const char *head, const char *msg) {
	fprintf(stderr, "%s\n%s\n", head, msg);
}

int msgBoxQuestion(const char *head, const char *msg) {
	return 1;
}

void changeToUserDir() {
	if (chdir(getenv("HOME"))) {
		debugOut("Error: Failed changing to directory %s\n", getenv("HOME"));
	}
	mkdir(".sludge-engine", 0000777);
	if (chdir(".sludge-engine")) {
		debugOut("Error: Failed changing to directory %s\n", ".sludge-engine");
	}
}

uint32_t launch(char *filename) {
	debugOut("Trying to launch: %s\n", filename);

	if (!fileExists("/usr/bin/xdg-open")) {
		debugOut("Launching failed due to missing /usr/bin/xdg-open.\n");
		return 0;
	}

	if (!(filename[0] == 'h' &&
	        filename[1] == 't' &&
	        filename[2] == 't' &&
	        filename[3] == 'p' &&
	        (filename[4] == ':' || (filename[4] == 's' && filename[5] == ':'))) &&
	        !fileExists(filename)) {
		return 0;
	}

	int status;

	pid_t pid = fork();
	if (pid < 0) {
		return 0;
	} else if (pid == 0) {
		execl("/usr/bin/xdg-open", "xdg-open", filename, (char *)0);
		exit(EXIT_FAILURE);
	} else {
		waitpid(pid, &status, 0);
	}

	if (status == EXIT_SUCCESS) {
		return 69;
	} else {
		return 0;
	}
}

bool defaultUserFullScreen() {
	return false;
}
#endif