src/os2.cpp
changeset 5842 180c71b8fc14
parent 5835 e0ff603ae0b7
child 6298 c30fe89622df
child 6566 a780a47d6af3
child 9895 7bd07f43b0e3
equal deleted inserted replaced
5841:b36d6b560aaa 5842:180c71b8fc14
     7 #include "table/strings.h"
     7 #include "table/strings.h"
     8 #include "gfx.h"
     8 #include "gfx.h"
     9 #include "gui.h"
     9 #include "gui.h"
    10 #include "functions.h"
    10 #include "functions.h"
    11 #include "macros.h"
    11 #include "macros.h"
    12 
    12 #include "fios.h"
    13 #include <direct.h>
    13 
       
    14 #include <dirent.h>
    14 #include <unistd.h>
    15 #include <unistd.h>
    15 #include <sys/stat.h>
    16 #include <sys/stat.h>
    16 #include <stdlib.h>
    17 #include <stdlib.h>
    17 #include <time.h>
    18 #include <time.h>
    18 #include <dos.h>
    19 #ifndef __INNOTEK_LIBC__
       
    20 	#include <dos.h>
       
    21 #endif
    19 
    22 
    20 #define INCL_WIN
    23 #define INCL_WIN
    21 #define INCL_WINCLIPBOARD
    24 #define INCL_WINCLIPBOARD
    22 
    25 
    23 #include <os2.h>
    26 #include <os2.h>
    24 #include <i86.h>
    27 #ifndef __INNOTEK_LIBC__
       
    28 	#include <i86.h>
       
    29 #endif
    25 
    30 
    26 bool FiosIsRoot(const char *file)
    31 bool FiosIsRoot(const char *file)
    27 {
    32 {
    28 	return path[3] == '\0';
    33 	return file[3] == '\0';
    29 }
    34 }
    30 
    35 
    31 void FiosGetDrives(void)
    36 void FiosGetDrives(void)
    32 {
    37 {
    33 	FiosItem *fios;
       
    34 	unsigned disk, disk2, save, total;
    38 	unsigned disk, disk2, save, total;
    35 
    39 
       
    40 #ifndef __INNOTEK_LIBC__
    36 	_dos_getdrive(&save); // save original drive
    41 	_dos_getdrive(&save); // save original drive
       
    42 #else
       
    43 	save = _getdrive(); // save original drive
       
    44 	total = 'z';
       
    45 #endif
    37 
    46 
    38 	/* get an available drive letter */
    47 	/* get an available drive letter */
       
    48 #ifndef __INNOTEK_LIBC__
    39 	for (disk = 1;; disk++) {
    49 	for (disk = 1;; disk++) {
    40 		_dos_setdrive(disk, &total);
    50 		_dos_setdrive(disk, &total);
       
    51 #else
       
    52 	for (disk = 'A';; disk++) {
       
    53 		_chdrive(disk);
       
    54 #endif
    41 		if (disk >= total) return;
    55 		if (disk >= total) return;
       
    56 
       
    57 #ifndef __INNOTEK_LIBC__
    42 		_dos_getdrive(&disk2);
    58 		_dos_getdrive(&disk2);
       
    59 #else
       
    60 		disk2 = _getdrive();
       
    61 #endif
    43 
    62 
    44 		if (disk == disk2) {
    63 		if (disk == disk2) {
    45 			FiosItem *fios = FiosAlloc();
    64 			FiosItem *fios = FiosAlloc();
    46 			fios->type = FIOS_TYPE_DRIVE;
    65 			fios->type = FIOS_TYPE_DRIVE;
    47 			fios->mtime = 0;
    66 			fios->mtime = 0;
       
    67 #ifndef __INNOTEK_LIBC__
    48 			snprintf(fios->name, lengthof(fios->name),  "%c:", 'A' + disk - 1);
    68 			snprintf(fios->name, lengthof(fios->name),  "%c:", 'A' + disk - 1);
       
    69 #else
       
    70 			snprintf(fios->name, lengthof(fios->name),  "%c:", disk);
       
    71 #endif
    49 			ttd_strlcpy(fios->title, fios->name, lengthof(fios->title));
    72 			ttd_strlcpy(fios->title, fios->name, lengthof(fios->title));
    50 		}
    73 		}
    51 	}
    74 	}
    52 
    75 
    53 	_dos_setdrive(save, &total); // restore the original drive
    76 	/* Restore the original drive */
       
    77 #ifndef __INNOTEK_LIBC__
       
    78 	_dos_setdrive(save, &total);
       
    79 #else
       
    80 	_chdrive(save);
       
    81 #endif
    54 }
    82 }
    55 
    83 
    56 bool FiosGetDiskFreeSpace(const char *path, uint32 *tot)
    84 bool FiosGetDiskFreeSpace(const char *path, uint32 *tot)
    57 {
    85 {
       
    86 #ifndef __INNOTEK_LIBC__
    58 	struct diskfree_t free;
    87 	struct diskfree_t free;
    59 	char drive = path[0] - 'A' + 1;
    88 	char drive = path[0] - 'A' + 1;
    60 
    89 
    61 	if (tot != NULL && _getdiskfree(drive, &free) == 0) {
    90 	if (tot != NULL && _getdiskfree(drive, &free) == 0) {
    62 		*tot = free.avail_clusters * free.sectors_per_cluster * free.bytes_per_sector;
    91 		*tot = free.avail_clusters * free.sectors_per_cluster * free.bytes_per_sector;
    63 		return true;
    92 		return true;
    64 	}
    93 	}
    65 
    94 
    66 	return false;
    95 	return false;
       
    96 #else
       
    97 	uint32 free = 0;
       
    98 
       
    99 #ifdef HAS_STATVFS
       
   100 	{
       
   101 		struct statvfs s;
       
   102 
       
   103 		if (statvfs(path, &s) != 0) return false;
       
   104 		free = (uint64)s.f_frsize * s.f_bavail >> 20;
       
   105 	}
       
   106 #endif
       
   107 	if (tot != NULL) *tot = free;
       
   108 	return true;
       
   109 #endif
    67 }
   110 }
    68 
   111 
    69 bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb)
   112 bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb)
    70 {
   113 {
    71 	char filename[MAX_PATH];
   114 	char filename[MAX_PATH];
    76 	return (ent->d_name[0] != '.'); // hidden file
   119 	return (ent->d_name[0] != '.'); // hidden file
    77 }
   120 }
    78 
   121 
    79 static void ChangeWorkingDirectory(char *exe)
   122 static void ChangeWorkingDirectory(char *exe)
    80 {
   123 {
    81 	char *s = strrchr(exe, '\\');
   124 	char *s = strrchr(exe, PATHSEPCHAR);
       
   125 
    82 	if (s != NULL) {
   126 	if (s != NULL) {
    83 		*s = '\0';
   127 		*s = '\0';
    84 		chdir(exe);
   128 		chdir(exe);
    85 		*s = '\\';
   129 		*s = PATHSEPCHAR;
    86 	}
   130 	}
    87 }
   131 }
    88 
   132 
    89 void ShowInfo(const char *str)
   133 void ShowInfo(const unsigned char *str)
    90 {
   134 {
    91 	HAB hab;
   135 	HAB hab;
    92 	HMQ hmq;
   136 	HMQ hmq;
    93 	ULONG rc;
   137 	ULONG rc;
    94 
   138 
    95 	// init PM env.
   139 	// init PM env.
    96 	hmq = WinCreateMsgQueue((hab = WinInitialize(0)), 0);
   140 	hmq = WinCreateMsgQueue((hab = WinInitialize(0)), 0);
    97 
   141 
    98 	// display the box
   142 	// display the box
    99 	rc = WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, str, "OpenTTD", 0, MB_OK | MB_MOVEABLE | MB_INFORMATION);
   143 	rc = WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, str, (const unsigned char *)"OpenTTD", 0, MB_OK | MB_MOVEABLE | MB_INFORMATION);
   100 
   144 
   101 	// terminate PM env.
   145 	// terminate PM env.
   102 	WinDestroyMsgQueue(hmq);
   146 	WinDestroyMsgQueue(hmq);
   103 	WinTerminate(hab);
   147 	WinTerminate(hab);
   104 }
   148 }
   105 
   149 
   106 void ShowOSErrorBox(const char *buf)
   150 void ShowOSErrorBox(const unsigned char *buf)
   107 {
   151 {
   108 	HAB hab;
   152 	HAB hab;
   109 	HMQ hmq;
   153 	HMQ hmq;
   110 	ULONG rc;
   154 	ULONG rc;
   111 
   155 
   112 	// init PM env.
   156 	// init PM env.
   113 	hmq = WinCreateMsgQueue((hab = WinInitialize(0)), 0);
   157 	hmq = WinCreateMsgQueue((hab = WinInitialize(0)), 0);
   114 
   158 
   115 	// display the box
   159 	// display the box
   116 	rc = WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, buf, "OpenTTD", 0, MB_OK | MB_MOVEABLE | MB_ERROR);
   160 	rc = WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, buf, (const unsigned char *)"OpenTTD", 0, MB_OK | MB_MOVEABLE | MB_ERROR);
   117 
   161 
   118 	// terminate PM env.
   162 	// terminate PM env.
   119 	WinDestroyMsgQueue(hmq);
   163 	WinDestroyMsgQueue(hmq);
   120 	WinTerminate(hab);
   164 	WinTerminate(hab);
   121 }
   165 }
   132 
   176 
   133 void DeterminePaths(void)
   177 void DeterminePaths(void)
   134 {
   178 {
   135 	char *s;
   179 	char *s;
   136 
   180 
   137 	_paths.game_data_dir = malloc(MAX_PATH);
   181 	_paths.game_data_dir = (char *)malloc(MAX_PATH);
   138 	ttd_strlcpy(_paths.game_data_dir, GAME_DATA_DIR, MAX_PATH);
   182 	ttd_strlcpy(_paths.game_data_dir, GAME_DATA_DIR, MAX_PATH);
   139 	#if defined SECOND_DATA_DIR
   183 #if defined SECOND_DATA_DIR
   140 	_paths.second_data_dir = malloc(MAX_PATH);
   184 	_paths.second_data_dir = malloc(MAX_PATH);
   141 	ttd_strlcpy(_paths.second_data_dir, SECOND_DATA_DIR, MAX_PATH);
   185 	ttd_strlcpy(_paths.second_data_dir, SECOND_DATA_DIR, MAX_PATH);
   142 	#endif
   186 #endif
   143 
   187 
   144 #if defined(USE_HOMEDIR)
   188 #if defined(USE_HOMEDIR)
   145 	{
   189 	{
   146 		const char *homedir = getenv("HOME");
   190 		const char *homedir = getenv("HOME");
   147 
   191 
   153 		_paths.personal_dir = str_fmt("%s" PATHSEP "%s", homedir, PERSONAL_DIR);
   197 		_paths.personal_dir = str_fmt("%s" PATHSEP "%s", homedir, PERSONAL_DIR);
   154 	}
   198 	}
   155 
   199 
   156 #else /* not defined(USE_HOMEDIR) */
   200 #else /* not defined(USE_HOMEDIR) */
   157 
   201 
   158 	_paths.personal_dir = malloc(MAX_PATH);
   202 	_paths.personal_dir = (char *)malloc(MAX_PATH);
   159 	ttd_strlcpy(_paths.personal_dir, PERSONAL_DIR, MAX_PATH);
   203 	ttd_strlcpy(_paths.personal_dir, PERSONAL_DIR, MAX_PATH);
   160 
   204 
   161 	// check if absolute or relative path
   205 	// check if absolute or relative path
   162 	s = strchr(_paths.personal_dir, '\\');
   206 	s = strchr(_paths.personal_dir, PATHSEPCHAR);
   163 
   207 
   164 	// add absolute path
   208 	// add absolute path
   165 	if (s == NULL || _paths.personal_dir != s) {
   209 	if (s == NULL || _paths.personal_dir != s) {
   166 		getcwd(_paths.personal_dir, MAX_PATH);
   210 		getcwd(_paths.personal_dir, MAX_PATH);
   167 		s = strchr(_paths.personal_dir, 0);
   211 		s = strchr(_paths.personal_dir, 0);
   168 		*s++ = '\\';
   212 		*s++ = PATHSEPCHAR;
   169 		ttd_strlcpy(s, PERSONAL_DIR, MAX_PATH);
   213 		ttd_strlcpy(s, PERSONAL_DIR, MAX_PATH);
   170 	}
   214 	}
   171 
   215 
   172 #endif /* defined(USE_HOMEDIR) */
   216 #endif /* defined(USE_HOMEDIR) */
   173 
   217 
   174 	s = strchr(_paths.personal_dir, 0);
   218 	s = strchr(_paths.personal_dir, 0);
   175 
   219 
   176 	// append a / ?
   220 	// append a / ?
   177 	if (s[-1] != '\\') strcpy(s, "\\");
   221 	if (s[-1] != PATHSEPCHAR) strcpy(s, PATHSEP);
   178 
   222 
   179 	_paths.save_dir = str_fmt("%ssave", _paths.personal_dir);
   223 	_paths.save_dir = str_fmt("%ssave", _paths.personal_dir);
   180 	_paths.autosave_dir = str_fmt("%s\\autosave", _paths.save_dir);
   224 	_paths.autosave_dir = str_fmt("%s" PATHSEP "autosave", _paths.save_dir);
   181 	_paths.scenario_dir = str_fmt("%sscenario", _paths.personal_dir);
   225 	_paths.scenario_dir = str_fmt("%sscenario", _paths.personal_dir);
   182 	_paths.heightmap_dir = str_fmt("%sscenario\\heightmap", _paths.personal_dir);
   226 	_paths.heightmap_dir = str_fmt("%sscenario" PATHSEP "heightmap", _paths.personal_dir);
   183 	_paths.gm_dir = str_fmt("%sgm\\", _paths.game_data_dir);
   227 	_paths.gm_dir = str_fmt("%sgm" PATHSEP, _paths.game_data_dir);
   184 	_paths.data_dir = str_fmt("%sdata\\", _paths.game_data_dir);
   228 	_paths.data_dir = str_fmt("%sdata" PATHSEP, _paths.game_data_dir);
   185 
   229 
   186 	if (_config_file == NULL)
   230 	if (_config_file == NULL)
   187 		_config_file = str_fmt("%sopenttd.cfg", _paths.personal_dir);
   231 		_config_file = str_fmt("%sopenttd.cfg", _paths.personal_dir);
   188 
   232 
   189 	_highscore_file = str_fmt("%shs.dat", _paths.personal_dir);
   233 	_highscore_file = str_fmt("%shs.dat", _paths.personal_dir);
   192 #if defined CUSTOM_LANG_DIR
   236 #if defined CUSTOM_LANG_DIR
   193 	// sets the search path for lng files to the custom one
   237 	// sets the search path for lng files to the custom one
   194 	_paths.lang_dir = malloc( MAX_PATH );
   238 	_paths.lang_dir = malloc( MAX_PATH );
   195 	ttd_strlcpy( _paths.lang_dir, CUSTOM_LANG_DIR, MAX_PATH);
   239 	ttd_strlcpy( _paths.lang_dir, CUSTOM_LANG_DIR, MAX_PATH);
   196 #else
   240 #else
   197 	_paths.lang_dir = str_fmt("%slang\\", _paths.game_data_dir);
   241 	_paths.lang_dir = str_fmt("%slang" PATHSEP, _paths.game_data_dir);
   198 #endif
   242 #endif
   199 
   243 
   200 	// create necessary folders
   244 	// create necessary folders
       
   245 #ifndef __INNOTEK_LIBC__
   201 	mkdir(_paths.personal_dir);
   246 	mkdir(_paths.personal_dir);
   202 	mkdir(_paths.save_dir);
   247 	mkdir(_paths.save_dir);
   203 	mkdir(_paths.autosave_dir);
   248 	mkdir(_paths.autosave_dir);
   204 	mkdir(_paths.scenario_dir);
   249 	mkdir(_paths.scenario_dir);
   205 	mkdir(_paths.heightmap_dir);
   250 	mkdir(_paths.heightmap_dir);
       
   251 #else
       
   252 	mkdir(_paths.personal_dir, 0755);
       
   253 	mkdir(_paths.save_dir, 0755);
       
   254 	mkdir(_paths.autosave_dir, 0755);
       
   255 	mkdir(_paths.scenario_dir, 0755);
       
   256 	mkdir(_paths.heightmap_dir, 0755);
       
   257 #endif
   206 }
   258 }
   207 
   259 
   208 /**
   260 /**
   209  * Insert a chunk of text from the clipboard onto the textbuffer. Get TEXT clipboard
   261  * Insert a chunk of text from the clipboard onto the textbuffer. Get TEXT clipboard
   210  * and append this up to the maximum length (either absolute or screenlength). If maxlength
   262  * and append this up to the maximum length (either absolute or screenlength). If maxlength
   212  * @param tb @Textbuf type to be changed
   264  * @param tb @Textbuf type to be changed
   213  * @return Return true on successfull change of Textbuf, or false otherwise
   265  * @return Return true on successfull change of Textbuf, or false otherwise
   214  */
   266  */
   215 bool InsertTextBufferClipboard(Textbuf *tb)
   267 bool InsertTextBufferClipboard(Textbuf *tb)
   216 {
   268 {
       
   269 /* XXX -- Currently no clipboard support implemented with GCC */
       
   270 #ifndef __INNOTEK_LIBC__
   217 	HAB hab = 0;
   271 	HAB hab = 0;
   218 
   272 
   219 	if (WinOpenClipbrd(hab))
   273 	if (WinOpenClipbrd(hab))
   220 	{
   274 	{
   221 		const char* text = (const char*)WinQueryClipbrdData(hab, CF_TEXT);
   275 		const char* text = (const char*)WinQueryClipbrdData(hab, CF_TEXT);
   250 			return true;
   304 			return true;
   251 		}
   305 		}
   252 
   306 
   253 		WinCloseClipbrd(hab);
   307 		WinCloseClipbrd(hab);
   254 	}
   308 	}
   255 
   309 #endif
   256 	return false;
   310 	return false;
   257 }
   311 }
   258 
   312 
   259 
   313 
   260 void CSleep(int milliseconds)
   314 void CSleep(int milliseconds)
   261 {
   315 {
   262 	delay(milliseconds);
   316 #ifndef __INNOTEK_LIBC__
       
   317  	delay(milliseconds);
       
   318 #else
       
   319 	usleep(milliseconds * 1000);
       
   320 #endif
   263 }
   321 }
   264 
   322 
   265 const char *FS2OTTD(const char *name) {return name;}
   323 const char *FS2OTTD(const char *name) {return name;}
   266 const char *OTTD2FS(const char *name) {return name;}
   324 const char *OTTD2FS(const char *name) {return name;}