|
1 /* $Id$ */ |
|
2 |
|
3 #include "stdafx.h" |
|
4 #include "openttd.h" |
|
5 #include "variables.h" |
|
6 #include "string.h" |
|
7 #include "table/strings.h" |
|
8 #include "gfx.h" |
|
9 #include "gui.h" |
|
10 #include "functions.h" |
|
11 #include "macros.h" |
|
12 |
|
13 #include <direct.h> |
|
14 #include <unistd.h> |
|
15 #include <sys/stat.h> |
|
16 #include <stdlib.h> |
|
17 #include <time.h> |
|
18 #include <dos.h> |
|
19 |
|
20 #define INCL_WIN |
|
21 #define INCL_WINCLIPBOARD |
|
22 |
|
23 #include <os2.h> |
|
24 #include <i86.h> |
|
25 |
|
26 bool FiosIsRoot(const char *file) |
|
27 { |
|
28 return path[3] == '\0'; |
|
29 } |
|
30 |
|
31 void FiosGetDrives(void) |
|
32 { |
|
33 FiosItem *fios; |
|
34 unsigned disk, disk2, save, total; |
|
35 |
|
36 _dos_getdrive(&save); // save original drive |
|
37 |
|
38 /* get an available drive letter */ |
|
39 for (disk = 1;; disk++) { |
|
40 _dos_setdrive(disk, &total); |
|
41 if (disk >= total) return; |
|
42 _dos_getdrive(&disk2); |
|
43 |
|
44 if (disk == disk2) { |
|
45 FiosItem *fios = FiosAlloc(); |
|
46 fios->type = FIOS_TYPE_DRIVE; |
|
47 fios->mtime = 0; |
|
48 snprintf(fios->name, lengthof(fios->name), "%c:", 'A' + disk - 1); |
|
49 ttd_strlcpy(fios->title, fios->name, lengthof(fios->title)); |
|
50 } |
|
51 } |
|
52 |
|
53 _dos_setdrive(save, &total); // restore the original drive |
|
54 } |
|
55 |
|
56 bool FiosGetDiskFreeSpace(const char *path, uint32 *tot) |
|
57 { |
|
58 struct diskfree_t free; |
|
59 char drive = path[0] - 'A' + 1; |
|
60 |
|
61 if (tot != NULL && _getdiskfree(drive, &free) == 0) { |
|
62 *tot = free.avail_clusters * free.sectors_per_cluster * free.bytes_per_sector; |
|
63 return true; |
|
64 } |
|
65 |
|
66 return false; |
|
67 } |
|
68 |
|
69 bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb) |
|
70 { |
|
71 char filename[MAX_PATH]; |
|
72 |
|
73 snprintf(filename, lengthof(filename), "%s" PATHSEP "%s", path, ent->d_name); |
|
74 if (stat(filename, sb) != 0) return false; |
|
75 |
|
76 return (ent->d_name[0] != '.'); // hidden file |
|
77 } |
|
78 |
|
79 static void ChangeWorkingDirectory(char *exe) |
|
80 { |
|
81 char *s = strrchr(exe, '\\'); |
|
82 if (s != NULL) { |
|
83 *s = '\0'; |
|
84 chdir(exe); |
|
85 *s = '\\'; |
|
86 } |
|
87 } |
|
88 |
|
89 void ShowInfo(const char *str) |
|
90 { |
|
91 HAB hab; |
|
92 HMQ hmq; |
|
93 ULONG rc; |
|
94 |
|
95 // init PM env. |
|
96 hmq = WinCreateMsgQueue((hab = WinInitialize(0)), 0); |
|
97 |
|
98 // display the box |
|
99 rc = WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, str, "OpenTTD", 0, MB_OK | MB_MOVEABLE | MB_INFORMATION); |
|
100 |
|
101 // terminate PM env. |
|
102 WinDestroyMsgQueue(hmq); |
|
103 WinTerminate(hab); |
|
104 } |
|
105 |
|
106 void ShowOSErrorBox(const char *buf) |
|
107 { |
|
108 HAB hab; |
|
109 HMQ hmq; |
|
110 ULONG rc; |
|
111 |
|
112 // init PM env. |
|
113 hmq = WinCreateMsgQueue((hab = WinInitialize(0)), 0); |
|
114 |
|
115 // display the box |
|
116 rc = WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, buf, "OpenTTD", 0, MB_OK | MB_MOVEABLE | MB_ERROR); |
|
117 |
|
118 // terminate PM env. |
|
119 WinDestroyMsgQueue(hmq); |
|
120 WinTerminate(hab); |
|
121 } |
|
122 |
|
123 int CDECL main(int argc, char* argv[]) |
|
124 { |
|
125 // change the working directory to enable doubleclicking in UIs |
|
126 ChangeWorkingDirectory(argv[0]); |
|
127 |
|
128 _random_seeds[1][1] = _random_seeds[1][0] = _random_seeds[0][1] = _random_seeds[0][0] = time(NULL); |
|
129 |
|
130 return ttd_main(argc, argv); |
|
131 } |
|
132 |
|
133 void DeterminePaths(void) |
|
134 { |
|
135 char *s; |
|
136 |
|
137 _paths.game_data_dir = malloc(MAX_PATH); |
|
138 ttd_strlcpy(_paths.game_data_dir, GAME_DATA_DIR, MAX_PATH); |
|
139 #if defined SECOND_DATA_DIR |
|
140 _paths.second_data_dir = malloc(MAX_PATH); |
|
141 ttd_strlcpy(_paths.second_data_dir, SECOND_DATA_DIR, MAX_PATH); |
|
142 #endif |
|
143 |
|
144 #if defined(USE_HOMEDIR) |
|
145 { |
|
146 const char *homedir = getenv("HOME"); |
|
147 |
|
148 if (homedir == NULL) { |
|
149 const struct passwd *pw = getpwuid(getuid()); |
|
150 if (pw != NULL) homedir = pw->pw_dir; |
|
151 } |
|
152 |
|
153 _paths.personal_dir = str_fmt("%s" PATHSEP "%s", homedir, PERSONAL_DIR); |
|
154 } |
|
155 |
|
156 #else /* not defined(USE_HOMEDIR) */ |
|
157 |
|
158 _paths.personal_dir = malloc(MAX_PATH); |
|
159 ttd_strlcpy(_paths.personal_dir, PERSONAL_DIR, MAX_PATH); |
|
160 |
|
161 // check if absolute or relative path |
|
162 s = strchr(_paths.personal_dir, '\\'); |
|
163 |
|
164 // add absolute path |
|
165 if (s == NULL || _paths.personal_dir != s) { |
|
166 getcwd(_paths.personal_dir, MAX_PATH); |
|
167 s = strchr(_paths.personal_dir, 0); |
|
168 *s++ = '\\'; |
|
169 ttd_strlcpy(s, PERSONAL_DIR, MAX_PATH); |
|
170 } |
|
171 |
|
172 #endif /* defined(USE_HOMEDIR) */ |
|
173 |
|
174 s = strchr(_paths.personal_dir, 0); |
|
175 |
|
176 // append a / ? |
|
177 if (s[-1] != '\\') strcpy(s, "\\"); |
|
178 |
|
179 _paths.save_dir = str_fmt("%ssave", _paths.personal_dir); |
|
180 _paths.autosave_dir = str_fmt("%s\\autosave", _paths.save_dir); |
|
181 _paths.scenario_dir = str_fmt("%sscenario", _paths.personal_dir); |
|
182 _paths.heightmap_dir = str_fmt("%sscenario\\heightmap", _paths.personal_dir); |
|
183 _paths.gm_dir = str_fmt("%sgm\\", _paths.game_data_dir); |
|
184 _paths.data_dir = str_fmt("%sdata\\", _paths.game_data_dir); |
|
185 |
|
186 if (_config_file == NULL) |
|
187 _config_file = str_fmt("%sopenttd.cfg", _paths.personal_dir); |
|
188 |
|
189 _highscore_file = str_fmt("%shs.dat", _paths.personal_dir); |
|
190 _log_file = str_fmt("%sopenttd.log", _paths.personal_dir); |
|
191 |
|
192 #if defined CUSTOM_LANG_DIR |
|
193 // sets the search path for lng files to the custom one |
|
194 _paths.lang_dir = malloc( MAX_PATH ); |
|
195 ttd_strlcpy( _paths.lang_dir, CUSTOM_LANG_DIR, MAX_PATH); |
|
196 #else |
|
197 _paths.lang_dir = str_fmt("%slang\\", _paths.game_data_dir); |
|
198 #endif |
|
199 |
|
200 // create necessary folders |
|
201 mkdir(_paths.personal_dir); |
|
202 mkdir(_paths.save_dir); |
|
203 mkdir(_paths.autosave_dir); |
|
204 mkdir(_paths.scenario_dir); |
|
205 mkdir(_paths.heightmap_dir); |
|
206 } |
|
207 |
|
208 /** |
|
209 * Insert a chunk of text from the clipboard onto the textbuffer. Get TEXT clipboard |
|
210 * and append this up to the maximum length (either absolute or screenlength). If maxlength |
|
211 * is zero, we don't care about the screenlength but only about the physical length of the string |
|
212 * @param tb @Textbuf type to be changed |
|
213 * @return Return true on successfull change of Textbuf, or false otherwise |
|
214 */ |
|
215 bool InsertTextBufferClipboard(Textbuf *tb) |
|
216 { |
|
217 HAB hab = 0; |
|
218 |
|
219 if (WinOpenClipbrd(hab)) |
|
220 { |
|
221 const char* text = (const char*)WinQueryClipbrdData(hab, CF_TEXT); |
|
222 |
|
223 if (text != NULL) |
|
224 { |
|
225 uint length = 0; |
|
226 uint width = 0; |
|
227 const char* i; |
|
228 |
|
229 for (i = text; IsValidAsciiChar(*i); i++) |
|
230 { |
|
231 uint w; |
|
232 |
|
233 if (tb->length + length >= tb->maxlength - 1) break; |
|
234 |
|
235 w = GetCharacterWidth(FS_NORMAL, (byte)*i); |
|
236 if (tb->maxwidth != 0 && width + tb->width + w > tb->maxwidth) break; |
|
237 |
|
238 width += w; |
|
239 length++; |
|
240 } |
|
241 |
|
242 memmove(tb->buf + tb->caretpos + length, tb->buf + tb->caretpos, tb->length - tb->caretpos + 1); |
|
243 memcpy(tb->buf + tb->caretpos, text, length); |
|
244 tb->width += width; |
|
245 tb->caretxoffs += width; |
|
246 tb->length += length; |
|
247 tb->caretpos += length; |
|
248 |
|
249 WinCloseClipbrd(hab); |
|
250 return true; |
|
251 } |
|
252 |
|
253 WinCloseClipbrd(hab); |
|
254 } |
|
255 |
|
256 return false; |
|
257 } |
|
258 |
|
259 |
|
260 void CSleep(int milliseconds) |
|
261 { |
|
262 delay(milliseconds); |
|
263 } |
|
264 |
|
265 const char *FS2OTTD(const char *name) {return name;} |
|
266 const char *OTTD2FS(const char *name) {return name;} |