OpenTTD Source  12.0-beta2
os2.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "../../stdafx.h"
11 #include "../../openttd.h"
12 #include "../../gui.h"
13 #include "../../fileio_func.h"
14 #include "../../fios.h"
15 #include "../../openttd.h"
16 #include "../../core/random_func.hpp"
17 #include "../../string_func.h"
18 #include "../../textbuf_gui.h"
19 #include "../../thread.h"
20 
21 #include "table/strings.h"
22 
23 #include <dirent.h>
24 #include <unistd.h>
25 #include <sys/stat.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #ifndef __INNOTEK_LIBC__
29 # include <dos.h>
30 #endif
31 
32 #include "../../safeguards.h"
33 
34 #define INCL_WIN
35 #define INCL_WINCLIPBOARD
36 
37 #include <os2.h>
38 #ifndef __INNOTEK_LIBC__
39 # include <i86.h>
40 #endif
41 
42 bool FiosIsRoot(const char *file)
43 {
44  return file[3] == '\0';
45 }
46 
47 void FiosGetDrives(FileList &file_list)
48 {
49  uint disk, disk2, save, total;
50 
51 #ifndef __INNOTEK_LIBC__
52  _dos_getdrive(&save); // save original drive
53 #else
54  save = _getdrive(); // save original drive
55  char wd[MAX_PATH];
56  getcwd(wd, MAX_PATH);
57  total = 'z';
58 #endif
59 
60  /* get an available drive letter */
61 #ifndef __INNOTEK_LIBC__
62  for (disk = 1;; disk++) {
63  _dos_setdrive(disk, &total);
64 #else
65  for (disk = 'A';; disk++) {
66  _chdrive(disk);
67 #endif
68  if (disk >= total) break;
69 
70 #ifndef __INNOTEK_LIBC__
71  _dos_getdrive(&disk2);
72 #else
73  disk2 = _getdrive();
74 #endif
75 
76  if (disk == disk2) {
77  FiosItem *fios = file_list.Append();
78  fios->type = FIOS_TYPE_DRIVE;
79  fios->mtime = 0;
80 #ifndef __INNOTEK_LIBC__
81  snprintf(fios->name, lengthof(fios->name), "%c:", 'A' + disk - 1);
82 #else
83  snprintf(fios->name, lengthof(fios->name), "%c:", disk);
84 #endif
85  strecpy(fios->title, fios->name, lastof(fios->title));
86  }
87  }
88 
89  /* Restore the original drive */
90 #ifndef __INNOTEK_LIBC__
91  _dos_setdrive(save, &total);
92 #else
93  chdir(wd);
94 #endif
95 }
96 
97 bool FiosGetDiskFreeSpace(const char *path, uint64 *tot)
98 {
99 #ifndef __INNOTEK_LIBC__
100  struct diskfree_t free;
101  char drive = path[0] - 'A' + 1;
102 
103  if (tot != nullptr && _getdiskfree(drive, &free) == 0) {
104  *tot = free.avail_clusters * free.sectors_per_cluster * free.bytes_per_sector;
105  return true;
106  }
107 
108  return false;
109 #else
110  uint64 free = 0;
111 
112 #ifdef HAS_STATVFS
113  {
114  struct statvfs s;
115 
116  if (statvfs(path, &s) != 0) return false;
117  free = (uint64)s.f_frsize * s.f_bavail;
118  }
119 #endif
120  if (tot != nullptr) *tot = free;
121  return true;
122 #endif
123 }
124 
125 bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb)
126 {
127  char filename[MAX_PATH];
128 
129  snprintf(filename, lengthof(filename), "%s" PATHSEP "%s", path, ent->d_name);
130  return stat(filename, sb) == 0;
131 }
132 
133 bool FiosIsHiddenFile(const struct dirent *ent)
134 {
135  return ent->d_name[0] == '.';
136 }
137 
138 void ShowInfo(const char *str)
139 {
140  HAB hab;
141  HMQ hmq;
142  ULONG rc;
143 
144  /* init PM env. */
145  hmq = WinCreateMsgQueue((hab = WinInitialize(0)), 0);
146 
147  /* display the box */
148  rc = WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, (const unsigned char *)str, (const unsigned char *)"OpenTTD", 0, MB_OK | MB_MOVEABLE | MB_INFORMATION);
149 
150  /* terminate PM env. */
151  WinDestroyMsgQueue(hmq);
152  WinTerminate(hab);
153 }
154 
155 void ShowOSErrorBox(const char *buf, bool system)
156 {
157  HAB hab;
158  HMQ hmq;
159  ULONG rc;
160 
161  /* init PM env. */
162  hmq = WinCreateMsgQueue((hab = WinInitialize(0)), 0);
163 
164  /* display the box */
165  rc = WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, (const unsigned char *)buf, (const unsigned char *)"OpenTTD", 0, MB_OK | MB_MOVEABLE | MB_ERROR);
166 
167  /* terminate PM env. */
168  WinDestroyMsgQueue(hmq);
169  WinTerminate(hab);
170 }
171 
172 int CDECL main(int argc, char *argv[])
173 {
174  SetRandomSeed(time(nullptr));
175 
176  /* Make sure our arguments contain only valid UTF-8 characters. */
177  for (int i = 0; i < argc; i++) StrMakeValidInPlace(argv[i]);
178 
179  return openttd_main(argc, argv);
180 }
181 
182 bool GetClipboardContents(char *buffer, const char *last)
183 {
184 /* XXX -- Currently no clipboard support implemented with GCC */
185 #ifndef __INNOTEK_LIBC__
186  HAB hab = 0;
187 
188  if (WinOpenClipbrd(hab))
189  {
190  const char *text = (const char*)WinQueryClipbrdData(hab, CF_TEXT);
191 
192  if (text != nullptr)
193  {
194  strecpy(buffer, text, last);
195  WinCloseClipbrd(hab);
196  return true;
197  }
198 
199  WinCloseClipbrd(hab);
200  }
201 #endif
202  return false;
203 }
204 
205 
206 void OSOpenBrowser(const char *url)
207 {
208  // stub only
209  Debug(misc, 0, "Failed to open url: {}", url);
210 }
211 
212 void SetCurrentThreadName(const char *)
213 {
214 }
openttd_main
int openttd_main(int argc, char *argv[])
Main entry point for this lovely game.
Definition: openttd.cpp:523
SetCurrentThreadName
void SetCurrentThreadName(const char *)
Name the thread this function is called on for the debugger.
Definition: os2.cpp:212
FileList
List of file information.
Definition: fios.h:96
StrMakeValidInPlace
void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings)
Scans the string for invalid characters and replaces then with a question mark '?' (if not ignored).
Definition: string.cpp:255
FiosItem
Deals with finding savegames.
Definition: fios.h:87
main
int CDECL main(int argc, char *argv[])
And the main program (what else?)
Definition: settingsgen.cpp:455
SetRandomSeed
void SetRandomSeed(uint32 seed)
(Re)set the state of the random number generators.
Definition: random_func.cpp:65
GetClipboardContents
bool GetClipboardContents(char *buffer, const char *last)
Try to retrieve the current clipboard contents.
Definition: os2.cpp:182
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:378
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:460
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:394