# os2.cpp

Go to the documentation of this file.

    00001 /* $Id: os2.cpp 11828 2008-01-13 01:21:35Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "openttd.h"
    00007 #include "variables.h"
    00008 #include "gui.h"
    00009 #include "fileio.h"
    00010 #include "fios.h" // opendir/readdir/closedir
    00011 #include "functions.h"
    00012 #include "core/random_func.hpp"
    00013 #include "string_func.h"
    00014 #include "textbuf_gui.h"
    00015
    00016 #include "table/strings.h"
    00017
    00018 #include <dirent.h>
    00019 #include <unistd.h>
    00020 #include <sys/stat.h>
    00021 #include <stdlib.h>
    00022 #include <time.h>
    00023 #ifndef __INNOTEK_LIBC__
    00024   #include <dos.h>
    00025 #endif
    00026
    00027 #define INCL_WIN
    00028 #define INCL_WINCLIPBOARD
    00029
    00030 #include <os2.h>
    00031 #ifndef __INNOTEK_LIBC__
    00032   #include <i86.h>
    00033 #endif
    00034
    00035 bool FiosIsRoot(const char *file)
    00036 {
    00037   return file[3] == '\0';
    00038 }
    00039
    00040 void FiosGetDrives()
    00041 {
    00042   unsigned disk, disk2, save, total;
    00043
    00044 #ifndef __INNOTEK_LIBC__
    00045   _dos_getdrive(&save); // save original drive
    00046 #else
    00047   save = _getdrive(); // save original drive
    00048   char wd[MAX_PATH];
    00049   getcwd(wd, MAX_PATH);
    00050   total = 'z';
    00051 #endif
    00052
    00053   /* get an available drive letter */
    00054 #ifndef __INNOTEK_LIBC__
    00055   for (disk = 1;; disk++) {
    00056     _dos_setdrive(disk, &total);
    00057 #else
    00058   for (disk = 'A';; disk++) {
    00059     _chdrive(disk);
    00060 #endif
    00061     if (disk >= total)  break;
    00062
    00063 #ifndef __INNOTEK_LIBC__
    00064     _dos_getdrive(&disk2);
    00065 #else
    00066     disk2 = _getdrive();
    00067 #endif
    00068
    00069     if (disk == disk2) {
    00070       FiosItem *fios = FiosAlloc();
    00071       fios->type = FIOS_TYPE_DRIVE;
    00072       fios->mtime = 0;
    00073 #ifndef __INNOTEK_LIBC__
    00074       snprintf(fios->name, lengthof(fios->name),  "%c:", 'A' + disk - 1);
    00075 #else
    00076       snprintf(fios->name, lengthof(fios->name),  "%c:", disk);
    00077 #endif
    00078       ttd_strlcpy(fios->title, fios->name, lengthof(fios->title));
    00079     }
    00080   }
    00081
    00082   /* Restore the original drive */
    00083 #ifndef __INNOTEK_LIBC__
    00084   _dos_setdrive(save, &total);
    00085 #else
    00086   chdir(wd);
    00087 #endif
    00088 }
    00089
    00090 bool FiosGetDiskFreeSpace(const char *path, uint32 *tot)
    00091 {
    00092 #ifndef __INNOTEK_LIBC__
    00093   struct diskfree_t free;
    00094   char drive = path[0] - 'A' + 1;
    00095
    00096   if (tot != NULL && _getdiskfree(drive, &free) == 0) {
    00097     *tot = free.avail_clusters * free.sectors_per_cluster * free.bytes_per_sector;
    00098     return true;
    00099   }
    00100
    00101   return false;
    00102 #else
    00103   uint32 free = 0;
    00104
    00105 #ifdef HAS_STATVFS
    00106   {
    00107     struct statvfs s;
    00108
    00109     if (statvfs(path, &s) != 0) return false;
    00110     free = (uint64)s.f_frsize * s.f_bavail >> 20;
    00111   }
    00112 #endif
    00113   if (tot != NULL) *tot = free;
    00114   return true;
    00115 #endif
    00116 }
    00117
    00118 bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb)
    00119 {
    00120   char filename[MAX_PATH];
    00121
    00122   snprintf(filename, lengthof(filename), "%s" PATHSEP "%s", path, ent->d_name);
    00123   return stat(filename, sb) == 0;
    00124 }
    00125
    00126 bool FiosIsHiddenFile(const struct dirent *ent)
    00127 {
    00128   return ent->d_name[0] == '.';
    00129 }
    00130
    00131 void ShowInfo(const char *str)
    00132 {
    00133   HAB hab;
    00134   HMQ hmq;
    00135   ULONG rc;
    00136
    00137   // init PM env.
    00138   hmq = WinCreateMsgQueue((hab = WinInitialize(0)), 0);
    00139
    00140   // display the box
    00141   rc = WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, (const unsigned char *)str, (const unsigned char *)"OpenTTD", 0, MB_OK | MB_MOVEABLE | MB_INFORMATION);
    00142
    00143   // terminate PM env.
    00144   WinDestroyMsgQueue(hmq);
    00145   WinTerminate(hab);
    00146 }
    00147
    00148 void ShowOSErrorBox(const char *buf)
    00149 {
    00150   HAB hab;
    00151   HMQ hmq;
    00152   ULONG rc;
    00153
    00154   // init PM env.
    00155   hmq = WinCreateMsgQueue((hab = WinInitialize(0)), 0);
    00156
    00157   // display the box
    00158   rc = WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, (const unsigned char *)buf, (const unsigned char *)"OpenTTD", 0, MB_OK | MB_MOVEABLE | MB_ERROR);
    00159
    00160   // terminate PM env.
    00161   WinDestroyMsgQueue(hmq);
    00162   WinTerminate(hab);
    00163 }
    00164
    00165 int CDECL main(int argc, char* argv[])
    00166 {
    00167   SetRandomSeed(time(NULL));
    00168
    00169   return ttd_main(argc, argv);
    00170 }
    00171
    00179 bool InsertTextBufferClipboard(Textbuf *tb)
    00180 {
    00181 /* XXX -- Currently no clipboard support implemented with GCC */
    00182 #ifndef __INNOTEK_LIBC__
    00183   HAB hab = 0;
    00184
    00185   if (WinOpenClipbrd(hab))
    00186   {
    00187     const char* text = (const char*)WinQueryClipbrdData(hab, CF_TEXT);
    00188
    00189     if (text != NULL)
    00190     {
    00191       uint length = 0;
    00192       uint width = 0;
    00193       const char* i;
    00194
    00195       for (i = text; IsValidAsciiChar(*i); i++)
    00196       {
    00197         uint w;
    00198
    00199         if (tb->length + length >= tb->maxlength - 1) break;
    00200
    00201         w = GetCharacterWidth(FS_NORMAL, (byte)*i);
    00202         if (tb->maxwidth != 0 && width + tb->width + w > tb->maxwidth) break;
    00203
    00204         width += w;
    00205         length++;
    00206       }
    00207
    00208       memmove(tb->buf + tb->caretpos + length, tb->buf + tb->caretpos, tb->length - tb->caretpos + 1);
    00209       memcpy(tb->buf + tb->caretpos, text, length);
    00210       tb->width += width;
    00211       tb->caretxoffs += width;
    00212       tb->length += length;
    00213       tb->caretpos += length;
    00214
    00215       WinCloseClipbrd(hab);
    00216       return true;
    00217     }
    00218
    00219     WinCloseClipbrd(hab);
    00220   }
    00221 #endif
    00222   return false;
    00223 }
    00224
    00225
    00226 void CSleep(int milliseconds)
    00227 {
    00228 #ifndef __INNOTEK_LIBC__
    00229   delay(milliseconds);
    00230 #else
    00231   usleep(milliseconds * 1000);
    00232 #endif
    00233 }
    00234
    00235 const char *FS2OTTD(const char *name) {return name;}
    00236 const char *OTTD2FS(const char *name) {return name;}