OpenTTD
os2.cpp
Go to the documentation of this file.
1 /* $Id: os2.cpp 27290 2015-05-20 18:18:26Z rubidium $ */
2 
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  */
9 
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"
21 
22 #include "table/strings.h"
23 
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
32 
33 #include "../../safeguards.h"
34 
35 #define INCL_WIN
36 #define INCL_WINCLIPBOARD
37 
38 #include <os2.h>
39 #ifndef __INNOTEK_LIBC__
40  #include <i86.h>
41 #endif
42 
43 bool FiosIsRoot(const char *file)
44 {
45  return file[3] == '\0';
46 }
47 
48 void FiosGetDrives()
49 {
50  uint disk, disk2, save, total;
51 
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
60 
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;
70 
71 #ifndef __INNOTEK_LIBC__
72  _dos_getdrive(&disk2);
73 #else
74  disk2 = _getdrive();
75 #endif
76 
77  if (disk == disk2) {
78  FiosItem *fios = _fios_items.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));
87  }
88  }
89 
90  /* Restore the original drive */
91 #ifndef __INNOTEK_LIBC__
92  _dos_setdrive(save, &total);
93 #else
94  chdir(wd);
95 #endif
96 }
97 
98 bool FiosGetDiskFreeSpace(const char *path, uint64 *tot)
99 {
100 #ifndef __INNOTEK_LIBC__
101  struct diskfree_t free;
102  char drive = path[0] - 'A' + 1;
103 
104  if (tot != NULL && _getdiskfree(drive, &free) == 0) {
105  *tot = free.avail_clusters * free.sectors_per_cluster * free.bytes_per_sector;
106  return true;
107  }
108 
109  return false;
110 #else
111  uint64 free = 0;
112 
113 #ifdef HAS_STATVFS
114  {
115  struct statvfs s;
116 
117  if (statvfs(path, &s) != 0) return false;
118  free = (uint64)s.f_frsize * s.f_bavail;
119  }
120 #endif
121  if (tot != NULL) *tot = free;
122  return true;
123 #endif
124 }
125 
126 bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb)
127 {
128  char filename[MAX_PATH];
129 
130  snprintf(filename, lengthof(filename), "%s" PATHSEP "%s", path, ent->d_name);
131  return stat(filename, sb) == 0;
132 }
133 
134 bool FiosIsHiddenFile(const struct dirent *ent)
135 {
136  return ent->d_name[0] == '.';
137 }
138 
139 void ShowInfo(const char *str)
140 {
141  HAB hab;
142  HMQ hmq;
143  ULONG rc;
144 
145  /* init PM env. */
146  hmq = WinCreateMsgQueue((hab = WinInitialize(0)), 0);
147 
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);
150 
151  /* terminate PM env. */
152  WinDestroyMsgQueue(hmq);
153  WinTerminate(hab);
154 }
155 
156 void ShowOSErrorBox(const char *buf, bool system)
157 {
158  HAB hab;
159  HMQ hmq;
160  ULONG rc;
161 
162  /* init PM env. */
163  hmq = WinCreateMsgQueue((hab = WinInitialize(0)), 0);
164 
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);
167 
168  /* terminate PM env. */
169  WinDestroyMsgQueue(hmq);
170  WinTerminate(hab);
171 }
172 
173 int CDECL main(int argc, char *argv[])
174 {
175  SetRandomSeed(time(NULL));
176 
177  /* Make sure our arguments contain only valid UTF-8 characters. */
178  for (int i = 0; i < argc; i++) ValidateString(argv[i]);
179 
180  return openttd_main(argc, argv);
181 }
182 
183 bool GetClipboardContents(char *buffer, const char *last)
184 {
185 /* XXX -- Currently no clipboard support implemented with GCC */
186 #ifndef __INNOTEK_LIBC__
187  HAB hab = 0;
188 
189  if (WinOpenClipbrd(hab))
190  {
191  const char *text = (const char*)WinQueryClipbrdData(hab, CF_TEXT);
192 
193  if (text != NULL)
194  {
195  strecpy(buffer, text, last);
196  WinCloseClipbrd(hab);
197  return true;
198  }
199 
200  WinCloseClipbrd(hab);
201  }
202 #endif
203  return false;
204 }
205 
206 
207 void CSleep(int milliseconds)
208 {
209 #ifndef __INNOTEK_LIBC__
210  delay(milliseconds);
211 #else
212  usleep(milliseconds * 1000);
213 #endif
214 }
215 
216 const char *FS2OTTD(const char *name) {return name;}
217 const char *OTTD2FS(const char *name) {return name;}
218 
220 {
221  return 1;
222 }
223 
224 void OSOpenBrowser(const char *url)
225 {
226  // stub only
227  DEBUG(misc, 0, "Failed to open url: %s", url);
228 }