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