1#include <AppKit/NSOpenPanel.h> 2#include <Foundation/NSString.h> 3#include <limits.h> 4#include <pwd.h> 5#include <unistd.h> 6#include <sys/stat.h> 7#include <sys/types.h> 8#include <stdio.h> 9#include <errno.h> 10#include <sys/fcntl.h> 11#include <string.h> 12#include <stdlib.h> 13 14#include "externals.h" 15#include "internals.h" 16 17static char save_path[PATH_MAX]; 18static char cfg_path[PATH_MAX]; 19static char local_games_path[PATH_MAX]; 20static char local_themes_path[PATH_MAX]; 21static char local_stead_path[PATH_MAX]; 22 23char *game_locale(void) 24{ 25 char *p; 26 char *s; 27 p = getenv("LANG"); 28 if (!p || !(s = strdup(p))) 29 return NULL; 30 if ((p = strchr(s, '_'))) 31 *p = 0; 32 return s; 33} 34 35char *game_tmp_path(void) 36{ 37 static time_t t = 0; 38 static char tmp[PATH_MAX]="/tmp/instead-games"; 39 if (!t) { 40 t = time(NULL); 41 sprintf(tmp, "/tmp/instead-games-%ld", (unsigned long)t); 42 } 43 if (mkdir(tmp, S_IRWXU) && errno != EEXIST) 44 return NULL; 45 return tmp; 46} 47 48char *appdir(void) 49{ 50 static char dir[PATH_MAX]; 51 struct passwd *pw; 52#ifdef _LOCAL_APPDATA 53 strcpy(dir, game_cwd); 54 strcat(dir, "/appdata"); 55 if (!access(dir, W_OK)) 56 return dir; 57#endif 58 pw = getpwuid(getuid()); 59 if (!pw) 60 return NULL; 61 snprintf(dir, sizeof(dir) - 1 , "%s/.instead", pw->pw_dir); 62 return dir; 63} 64 65char *game_local_games_path(int cr) 66{ 67 char *app = appdir(); 68 if (!app) 69 return NULL; 70 strcpy(local_games_path, app); 71 if (cr) { 72 if (mkdir(local_games_path, S_IRWXU) && errno != EEXIST) 73 return NULL; 74 } 75 strcat(local_games_path,"/games"); 76 if (cr) { 77 if (mkdir(local_games_path, S_IRWXU) && errno != EEXIST) 78 return NULL; 79 } 80 return local_games_path; 81} 82 83char *game_local_themes_path(void) 84{ 85 char *app = appdir(); 86 if (!app) 87 return NULL; 88 snprintf(local_themes_path, sizeof(local_themes_path) - 1 , "%s/themes", app); 89 return local_themes_path; 90} 91 92char *game_local_stead_path(void) 93{ 94 char *app = appdir(); 95 if (!app) 96 return NULL; 97 snprintf(local_stead_path, sizeof(local_stead_path) - 1 , "%s/stead", app); 98 return local_stead_path; 99} 100 101char *game_cfg_path(void) 102{ 103 char *app = appdir(); 104 struct passwd *pw; 105 pw = getpwuid(getuid()); 106 if (!pw) 107 return NULL; 108 snprintf(cfg_path, sizeof(cfg_path) - 1 , "%s/.insteadrc", pw->pw_dir); /* at home */ 109 if (!access(cfg_path, R_OK)) 110 return cfg_path; 111/* no at home? Try in dir */ 112 if (app) 113 snprintf(cfg_path, sizeof(cfg_path) - 1 , "%s/", app); 114 if (!app || (mkdir(cfg_path, S_IRWXU) && errno != EEXIST)) 115 snprintf(cfg_path, sizeof(cfg_path) - 1 , "%s/.insteadrc", pw->pw_dir); /* fallback to home */ 116 else 117 snprintf(cfg_path, sizeof(cfg_path) - 1 , "%s/insteadrc", app); 118 return cfg_path; 119} 120 121char *game_save_path(int cr, int nr) 122{ 123 char *app = appdir(); 124 if (!curgame_dir) 125 return NULL; 126 if (!access("saves", R_OK)) { 127 if (nr) 128 snprintf(save_path, sizeof(save_path) - 1, "saves/save%d", nr); 129 else 130 snprintf(save_path, sizeof(save_path) - 1, "saves/autosave"); 131 return save_path; 132 } 133 if (!app) 134 return NULL; 135 snprintf(save_path, sizeof(save_path) - 1 , "%s/", app); 136 if (cr && mkdir(save_path, S_IRWXU) && errno != EEXIST) 137 return NULL; 138 snprintf(save_path, sizeof(save_path) - 1 , "%s/saves", app); 139 if (cr && mkdir(save_path, S_IRWXU) && errno != EEXIST) 140 return NULL; 141 snprintf(save_path, sizeof(save_path) - 1, "%s/saves/%s/", app, curgame_dir); 142 if (cr && mkdir(save_path, S_IRWXU) && errno != EEXIST) 143 return NULL; 144 if (nr) 145 snprintf(save_path, sizeof(save_path) - 1, "%s/saves/%s/save%d", app, curgame_dir, nr); 146 else 147 snprintf(save_path, sizeof(save_path) - 1, "%s/saves/%s/autosave", app, curgame_dir); 148 return save_path; 149} 150 151int debug_init(void) 152{ 153 return 0; 154} 155 156void debug_done() 157{ 158 159} 160 161char *sdl_path(char *p) 162{ 163 unix_path(p); 164 return p; 165} 166 167 168char *open_file_dialog(void) 169{ 170 const char *filename; 171 static char *file_name[PATH_MAX]; 172 NSArray* fileTypes = [NSArray arrayWithObjects: @"zip", @"lua", @"idf", nil]; 173 174 NSOpenPanel * panel = [NSOpenPanel openPanel]; 175 [panel setCanChooseDirectories:NO]; 176 [panel setCanChooseFiles:YES]; 177 [panel setAllowsMultipleSelection:NO]; 178 179 if ([panel runModalForTypes:fileTypes] == NSOKButton) { 180#ifdef __POWERPC__ 181 filename = [[panel filename] cString]; 182#else 183 filename = [[panel filename] cStringUsingEncoding:NSUTF8StringEncoding]; 184#endif 185 strcpy(file_name, filename); 186 return file_name; 187 } 188 return NULL; 189} 190#if 0 191int setdir(const char *path) 192{ 193 return chdir(path); 194} 195 196char *getdir(char *path, size_t size) 197{ 198 return getcwd(path, size); 199} 200 201char *dirpath(const char *path) 202{ 203 return (char*)path; 204} 205 206int is_absolute_path(const char *path) 207{ 208 if (!path || !*path) 209 return 0; 210 return (*path == '/'); 211} 212#endif 213