src/unix.cpp
branchcpp_gui
changeset 6303 84c215fc8eb8
parent 6298 c30fe89622df
child 6307 f40e88cff863
equal deleted inserted replaced
6302:bd80897189ba 6303:84c215fc8eb8
    12 #include <unistd.h>
    12 #include <unistd.h>
    13 #include <sys/stat.h>
    13 #include <sys/stat.h>
    14 #include <time.h>
    14 #include <time.h>
    15 #include <signal.h>
    15 #include <signal.h>
    16 
    16 
    17 #ifdef USE_HOMEDIR
       
    18 #include <pwd.h>
       
    19 #endif
       
    20 
       
    21 #if (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) || defined(__GLIBC__)
    17 #if (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) || defined(__GLIBC__)
    22 	#define HAS_STATVFS
    18 	#define HAS_STATVFS
    23 #endif
    19 #endif
    24 
    20 
    25 #ifdef HAS_STATVFS
    21 #ifdef HAS_STATVFS
    98 bool FiosIsHiddenFile(const struct dirent *ent)
    94 bool FiosIsHiddenFile(const struct dirent *ent)
    99 {
    95 {
   100 	return ent->d_name[0] == '.';
    96 	return ent->d_name[0] == '.';
   101 }
    97 }
   102 
    98 
   103 #if defined(__BEOS__) || defined(__linux__)
       
   104 static void ChangeWorkingDirectory(char *exe)
       
   105 {
       
   106 	char *s = strrchr(exe, '/');
       
   107 	if (s != NULL) {
       
   108 		*s = '\0';
       
   109 		chdir(exe);
       
   110 		*s = '/';
       
   111 	}
       
   112 }
       
   113 #endif
       
   114 
       
   115 void ShowInfo(const char *str)
    99 void ShowInfo(const char *str)
   116 {
   100 {
   117 	fprintf(stderr, "%s\n", str);
   101 	fprintf(stderr, "%s\n", str);
   118 }
   102 }
   119 
   103 
   128 	fprintf(stderr, "\033[1;31mError: %s\033[0;39m\n", buf);
   112 	fprintf(stderr, "\033[1;31mError: %s\033[0;39m\n", buf);
   129 #endif
   113 #endif
   130 }
   114 }
   131 
   115 
   132 #ifdef WITH_COCOA
   116 #ifdef WITH_COCOA
   133 void cocoaSetWorkingDirectory();
       
   134 void cocoaSetupAutoreleasePool();
   117 void cocoaSetupAutoreleasePool();
   135 void cocoaReleaseAutoreleasePool();
   118 void cocoaReleaseAutoreleasePool();
   136 #endif
   119 #endif
   137 
   120 
   138 int CDECL main(int argc, char* argv[])
   121 int CDECL main(int argc, char* argv[])
   143 	cocoaSetupAutoreleasePool();
   126 	cocoaSetupAutoreleasePool();
   144 	/* This is passed if we are launched by double-clicking */
   127 	/* This is passed if we are launched by double-clicking */
   145 	if (argc >= 2 && strncmp(argv[1], "-psn", 4) == 0) {
   128 	if (argc >= 2 && strncmp(argv[1], "-psn", 4) == 0) {
   146 		argv[1] = NULL;
   129 		argv[1] = NULL;
   147 		argc = 1;
   130 		argc = 1;
   148 		cocoaSetWorkingDirectory();
   131 	}
   149 	}
       
   150 #endif
       
   151 
       
   152 	// change the working directory to enable doubleclicking in UIs
       
   153 #if defined(__BEOS__) || defined(__linux__)
       
   154 	ChangeWorkingDirectory(argv[0]);
       
   155 #endif
   132 #endif
   156 
   133 
   157 	_random_seeds[1][1] = _random_seeds[1][0] = _random_seeds[0][1] = _random_seeds[0][0] = time(NULL);
   134 	_random_seeds[1][1] = _random_seeds[1][0] = _random_seeds[0][1] = _random_seeds[0][0] = time(NULL);
   158 	SeedMT(_random_seeds[0][1]);
   135 	SeedMT(_random_seeds[0][1]);
   159 
   136 
   164 #ifdef WITH_COCOA
   141 #ifdef WITH_COCOA
   165 	cocoaReleaseAutoreleasePool();
   142 	cocoaReleaseAutoreleasePool();
   166 #endif
   143 #endif
   167 
   144 
   168 	return ret;
   145 	return ret;
   169 }
       
   170 
       
   171 void DeterminePaths()
       
   172 {
       
   173 	char *s;
       
   174 
       
   175 	_paths.game_data_dir = MallocT<char>(MAX_PATH);
       
   176 	ttd_strlcpy(_paths.game_data_dir, GAME_DATA_DIR, MAX_PATH);
       
   177 	#if defined SECOND_DATA_DIR
       
   178 	_paths.second_data_dir = MallocT<char>(MAX_PATH);
       
   179 	ttd_strlcpy(_paths.second_data_dir, SECOND_DATA_DIR, MAX_PATH);
       
   180 	#endif
       
   181 
       
   182 #if defined(USE_HOMEDIR)
       
   183 	{
       
   184 		const char *homedir = getenv("HOME");
       
   185 
       
   186 		if (homedir == NULL) {
       
   187 			const struct passwd *pw = getpwuid(getuid());
       
   188 			if (pw != NULL) homedir = pw->pw_dir;
       
   189 		}
       
   190 
       
   191 		_paths.personal_dir = str_fmt("%s" PATHSEP "%s", homedir, PERSONAL_DIR);
       
   192 	}
       
   193 
       
   194 #else /* not defined(USE_HOMEDIR) */
       
   195 
       
   196 	_paths.personal_dir = MallocT<char>(MAX_PATH);
       
   197 	ttd_strlcpy(_paths.personal_dir, PERSONAL_DIR, MAX_PATH);
       
   198 
       
   199 	// check if absolute or relative path
       
   200 	s = strchr(_paths.personal_dir, '/');
       
   201 
       
   202 	// add absolute path
       
   203 	if (s == NULL || _paths.personal_dir != s) {
       
   204 		getcwd(_paths.personal_dir, MAX_PATH);
       
   205 		s = strchr(_paths.personal_dir, 0);
       
   206 		*s++ = '/';
       
   207 		ttd_strlcpy(s, PERSONAL_DIR, MAX_PATH);
       
   208 	}
       
   209 
       
   210 #endif /* defined(USE_HOMEDIR) */
       
   211 
       
   212 	s = strchr(_paths.personal_dir, 0);
       
   213 
       
   214 	// append a / ?
       
   215 	if (s[-1] != '/') strcpy(s, "/");
       
   216 
       
   217 	_paths.save_dir = str_fmt("%ssave", _paths.personal_dir);
       
   218 	_paths.autosave_dir = str_fmt("%s/autosave", _paths.save_dir);
       
   219 	_paths.scenario_dir = str_fmt("%sscenario", _paths.personal_dir);
       
   220 	_paths.heightmap_dir = str_fmt("%sscenario/heightmap", _paths.personal_dir);
       
   221 	_paths.gm_dir = str_fmt("%sgm/", _paths.game_data_dir);
       
   222 	_paths.data_dir = str_fmt("%sdata/", _paths.game_data_dir);
       
   223 
       
   224 	if (_config_file == NULL)
       
   225 		_config_file = str_fmt("%sopenttd.cfg", _paths.personal_dir);
       
   226 
       
   227 	_highscore_file = str_fmt("%shs.dat", _paths.personal_dir);
       
   228 	_log_file = str_fmt("%sopenttd.log", _paths.personal_dir);
       
   229 
       
   230 #if defined CUSTOM_LANG_DIR
       
   231 	// sets the search path for lng files to the custom one
       
   232 	_paths.lang_dir = MallocT<char>(MAX_PATH);
       
   233 	ttd_strlcpy( _paths.lang_dir, CUSTOM_LANG_DIR, MAX_PATH);
       
   234 #else
       
   235 	_paths.lang_dir = str_fmt("%slang/", _paths.game_data_dir);
       
   236 #endif
       
   237 
       
   238 	// create necessary folders
       
   239 	mkdir(_paths.personal_dir, 0755);
       
   240 	mkdir(_paths.save_dir, 0755);
       
   241 	mkdir(_paths.autosave_dir, 0755);
       
   242 	mkdir(_paths.scenario_dir, 0755);
       
   243 	mkdir(_paths.heightmap_dir, 0755);
       
   244 }
   146 }
   245 
   147 
   246 bool InsertTextBufferClipboard(Textbuf *tb)
   148 bool InsertTextBufferClipboard(Textbuf *tb)
   247 {
   149 {
   248 	return false;
   150 	return false;