(svn r27729) -Codechange: Do not count static NewGRF when checking for the maximum...
[openttd.git] / src / os / os2 / os2.cpp
blob7b34f528a630cc90214e747034c7613d96099786
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD 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, version 2.
6 * OpenTTD 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.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file os2.cpp OS2 related OS support. */
12 #include "../../stdafx.h"
13 #include "../../openttd.h"
14 #include "../../gui.h"
15 #include "../../fileio_func.h"
16 #include "../../fios.h"
17 #include "../../openttd.h"
18 #include "../../core/random_func.hpp"
19 #include "../../string_func.h"
20 #include "../../textbuf_gui.h"
22 #include "table/strings.h"
24 #include <dirent.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27 #include <stdlib.h>
28 #include <time.h>
29 #ifndef __INNOTEK_LIBC__
30 #include <dos.h>
31 #endif
33 #include "../../safeguards.h"
35 #define INCL_WIN
36 #define INCL_WINCLIPBOARD
38 #include <os2.h>
39 #ifndef __INNOTEK_LIBC__
40 #include <i86.h>
41 #endif
43 bool FiosIsRoot(const char *file)
45 return file[3] == '\0';
48 void FiosGetDrives(FileList &file_list)
50 uint disk, disk2, save, total;
52 #ifndef __INNOTEK_LIBC__
53 _dos_getdrive(&save); // save original drive
54 #else
55 save = _getdrive(); // save original drive
56 char wd[MAX_PATH];
57 getcwd(wd, MAX_PATH);
58 total = 'z';
59 #endif
61 /* get an available drive letter */
62 #ifndef __INNOTEK_LIBC__
63 for (disk = 1;; disk++) {
64 _dos_setdrive(disk, &total);
65 #else
66 for (disk = 'A';; disk++) {
67 _chdrive(disk);
68 #endif
69 if (disk >= total) break;
71 #ifndef __INNOTEK_LIBC__
72 _dos_getdrive(&disk2);
73 #else
74 disk2 = _getdrive();
75 #endif
77 if (disk == disk2) {
78 FiosItem *fios = file_list.Append();
79 fios->type = FIOS_TYPE_DRIVE;
80 fios->mtime = 0;
81 #ifndef __INNOTEK_LIBC__
82 snprintf(fios->name, lengthof(fios->name), "%c:", 'A' + disk - 1);
83 #else
84 snprintf(fios->name, lengthof(fios->name), "%c:", disk);
85 #endif
86 strecpy(fios->title, fios->name, lastof(fios->title));
90 /* Restore the original drive */
91 #ifndef __INNOTEK_LIBC__
92 _dos_setdrive(save, &total);
93 #else
94 chdir(wd);
95 #endif
98 bool FiosGetDiskFreeSpace(const char *path, uint64 *tot)
100 #ifndef __INNOTEK_LIBC__
101 struct diskfree_t free;
102 char drive = path[0] - 'A' + 1;
104 if (tot != NULL && _getdiskfree(drive, &free) == 0) {
105 *tot = free.avail_clusters * free.sectors_per_cluster * free.bytes_per_sector;
106 return true;
109 return false;
110 #else
111 uint64 free = 0;
113 #ifdef HAS_STATVFS
115 struct statvfs s;
117 if (statvfs(path, &s) != 0) return false;
118 free = (uint64)s.f_frsize * s.f_bavail;
120 #endif
121 if (tot != NULL) *tot = free;
122 return true;
123 #endif
126 bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb)
128 char filename[MAX_PATH];
130 snprintf(filename, lengthof(filename), "%s" PATHSEP "%s", path, ent->d_name);
131 return stat(filename, sb) == 0;
134 bool FiosIsHiddenFile(const struct dirent *ent)
136 return ent->d_name[0] == '.';
139 void ShowInfo(const char *str)
141 HAB hab;
142 HMQ hmq;
143 ULONG rc;
145 /* init PM env. */
146 hmq = WinCreateMsgQueue((hab = WinInitialize(0)), 0);
148 /* display the box */
149 rc = WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, (const unsigned char *)str, (const unsigned char *)"OpenTTD", 0, MB_OK | MB_MOVEABLE | MB_INFORMATION);
151 /* terminate PM env. */
152 WinDestroyMsgQueue(hmq);
153 WinTerminate(hab);
156 void ShowOSErrorBox(const char *buf, bool system)
158 HAB hab;
159 HMQ hmq;
160 ULONG rc;
162 /* init PM env. */
163 hmq = WinCreateMsgQueue((hab = WinInitialize(0)), 0);
165 /* display the box */
166 rc = WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, (const unsigned char *)buf, (const unsigned char *)"OpenTTD", 0, MB_OK | MB_MOVEABLE | MB_ERROR);
168 /* terminate PM env. */
169 WinDestroyMsgQueue(hmq);
170 WinTerminate(hab);
173 int CDECL main(int argc, char *argv[])
175 SetRandomSeed(time(NULL));
177 /* Make sure our arguments contain only valid UTF-8 characters. */
178 for (int i = 0; i < argc; i++) ValidateString(argv[i]);
180 return openttd_main(argc, argv);
183 bool GetClipboardContents(char *buffer, const char *last)
185 /* XXX -- Currently no clipboard support implemented with GCC */
186 #ifndef __INNOTEK_LIBC__
187 HAB hab = 0;
189 if (WinOpenClipbrd(hab))
191 const char *text = (const char*)WinQueryClipbrdData(hab, CF_TEXT);
193 if (text != NULL)
195 strecpy(buffer, text, last);
196 WinCloseClipbrd(hab);
197 return true;
200 WinCloseClipbrd(hab);
202 #endif
203 return false;
207 void CSleep(int milliseconds)
209 #ifndef __INNOTEK_LIBC__
210 delay(milliseconds);
211 #else
212 usleep(milliseconds * 1000);
213 #endif
216 const char *FS2OTTD(const char *name) {return name;}
217 const char *OTTD2FS(const char *name) {return name;}
219 uint GetCPUCoreCount()
221 return 1;
224 void OSOpenBrowser(const char *url)
226 // stub only
227 DEBUG(misc, 0, "Failed to open url: %s", url);