~ubuntu-branches/debian/sid/astromenace/sid

« back to all changes in this revision

Viewing changes to AstroMenaceSource/Core/System/System.cpp

  • Committer: Package Import Robot
  • Author(s): Boris Pek
  • Date: 2013-04-09 02:04:25 UTC
  • Revision ID: package-import@ubuntu.com-20130409020425-a7fl9xk4diamw6di
Tags: upstream-1.3.1+repack
Import upstream version 1.3.1+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/************************************************************************************
 
2
 
 
3
        AstroMenace (Hardcore 3D space shooter with spaceship upgrade possibilities)
 
4
        Copyright © 2006-2012 Michael Kurinnoy, Viewizard
 
5
 
 
6
 
 
7
        AstroMenace is free software: you can redistribute it and/or modify
 
8
        it under the terms of the GNU General Public License as published by
 
9
        the Free Software Foundation, either version 3 of the License, or
 
10
        (at your option) any later version.
 
11
 
 
12
        AstroMenace is distributed in the hope that it will be useful,
 
13
        but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
15
        GNU General Public License for more details.
 
16
 
 
17
        You should have received a copy of the GNU General Public License
 
18
        along with AstroMenace. If not, see <http://www.gnu.org/licenses/>.
 
19
 
 
20
 
 
21
        Web Site: http://www.viewizard.com/
 
22
        Project: http://sourceforge.net/projects/openastromenace/
 
23
        E-mail: viewizard@viewizard.com
 
24
 
 
25
*************************************************************************************/
 
26
 
 
27
 
 
28
#include "System.h"
 
29
 
 
30
 
 
31
 
 
32
 
 
33
 
 
34
 
 
35
//------------------------------------------------------------------------------------
 
36
// проверка расширения файла
 
37
//------------------------------------------------------------------------------------
 
38
bool vw_TestFileExtension(const char *name, const char *extension)
 
39
{
 
40
        if(name==0||extension==0) return false;
 
41
        size_t LengthName=strlen(name), LengthString=strlen(extension);
 
42
        if(LengthName<LengthString) return false;
 
43
        for(int i=LengthName-1;i>=0;i--)
 
44
                if(name[i]=='.')
 
45
                {
 
46
                        if(!strcmp(&name[i+1],extension)) return true;
 
47
                        else return false;
 
48
                }
 
49
        return false;
 
50
}
 
51
 
 
52
 
 
53
 
 
54
 
 
55
 
 
56
 
 
57
 
 
58
 
 
59
 
 
60
 
 
61
//------------------------------------------------------------------------------------
 
62
// все что ниже, нужно для открытия броузера
 
63
//------------------------------------------------------------------------------------
 
64
 
 
65
 
 
66
 
 
67
 
 
68
 
 
69
 
 
70
 
 
71
 
 
72
#ifdef WIN32
 
73
#include <tchar.h>
 
74
#include <shellapi.h>
 
75
static LONG GetRegistryKey(HKEY key, LPCTSTR subkey, LPTSTR retdata)
 
76
{
 
77
        HKEY hkey;
 
78
        LONG retval = RegOpenKeyEx(key, subkey, 0, KEY_QUERY_VALUE, &hkey);
 
79
        if (retval == ERROR_SUCCESS)
 
80
        {
 
81
                long datasize = MAX_PATH;
 
82
                TCHAR data[MAX_PATH];
 
83
                RegQueryValue(hkey, NULL, data, &datasize);
 
84
                _tcscpy(retdata,data);
 
85
                RegCloseKey(hkey);
 
86
        }
 
87
        return retval;
 
88
}
 
89
#endif // WIN32
 
90
 
 
91
 
 
92
 
 
93
 
 
94
 
 
95
#if defined(__APPLE__) && defined(__MACH__)
 
96
 
 
97
#include <CoreFoundation/CFBundle.h>
 
98
#include <ApplicationServices/ApplicationServices.h>
 
99
 
 
100
#endif // __APPLE__
 
101
 
 
102
 
 
103
 
 
104
 
 
105
 
 
106
// поиск пути для запуска броузера
 
107
/*
 
108
Build dependencies:
 
109
        libc6-dev
 
110
Runtime dependencies:
 
111
        libc6
 
112
*/
 
113
#ifdef __unix
 
114
 
 
115
#include <stdio.h>
 
116
#include <string.h>
 
117
 
 
118
#include <dirent.h>
 
119
#include <pwd.h>
 
120
#include <sys/stat.h>
 
121
#include <sys/types.h>
 
122
#include <unistd.h>
 
123
 
 
124
 
 
125
char **get_path(void) {
 
126
        const char *path = getenv("PATH"), *cstr;
 
127
        char *null_path, **tokenized_path, *str;
 
128
        unsigned int parts = (path[0] != '\0') ? 1 : 0, a;
 
129
 
 
130
        null_path = (char *)malloc(strlen(path) + 1);
 
131
 
 
132
        for(cstr = path, str = null_path; *cstr; ++cstr, ++str) {
 
133
                switch(*cstr) {
 
134
                        case ':':
 
135
                                *str = '\0';
 
136
                                ++parts;
 
137
                                break;
 
138
                        default:
 
139
                                *str = *cstr;
 
140
 
 
141
                }
 
142
        }
 
143
        *str = '\0';
 
144
 
 
145
        tokenized_path = (char **)malloc(sizeof(char *) * (parts+1));
 
146
        tokenized_path[parts] = NULL;
 
147
 
 
148
        for(a = 0, str = null_path; a < parts; ++a) {
 
149
                tokenized_path[a] = str;
 
150
 
 
151
                do { ++str; } while(str[-1]);
 
152
        }
 
153
 
 
154
        return tokenized_path;
 
155
}
 
156
void free_path(char **tokenized_path) {
 
157
        free(tokenized_path[0]);
 
158
        free(tokenized_path);
 
159
}
 
160
 
 
161
int executable_exists_in_path(char **tokenized_path, const char *app_name) {
 
162
        unsigned int a;
 
163
        int found = 0;
 
164
 
 
165
        for(a = 0; !found && tokenized_path[a]; ++a) {
 
166
                DIR *dir = opendir(tokenized_path[a]);
 
167
 
 
168
                if(dir) {
 
169
                        struct dirent *dirent;
 
170
 
 
171
                        for(dirent = readdir(dir); dirent != NULL; dirent = readdir(dir)) {
 
172
                                if(strcmp(dirent->d_name, app_name) == 0) {
 
173
                                        /* We found something with a correct name, is it a proper executable? */
 
174
                                        size_t full_path_length = strlen(tokenized_path[a]) + 1 + strlen(app_name) + 1;
 
175
                                        char *full_path = (char *)malloc(full_path_length);
 
176
                                        struct stat buf;
 
177
 
 
178
                                        snprintf(full_path, full_path_length, "%s/%s", tokenized_path[a], app_name);
 
179
 
 
180
                                        if(stat(full_path, &buf) == 0) {
 
181
                                                /* Is is a regular file and is it executable by anyone? */
 
182
                                                if(S_ISREG(buf.st_mode) && (buf.st_mode & S_IXOTH))
 
183
                                                        found = 1;
 
184
 
 
185
                                                /* FIXME It would be more accurate to test if this user can
 
186
                                                 * execute the file rather than if any user can.
 
187
                                                 */
 
188
                                        }
 
189
 
 
190
                                        free(full_path);
 
191
                                }
 
192
                        }
 
193
 
 
194
                        closedir(dir);
 
195
                }
 
196
        }
 
197
 
 
198
        return found;
 
199
}
 
200
 
 
201
char **get_browsers(void) {
 
202
        static const char *envvar_list[] = {
 
203
                "BROWSER"
 
204
        };
 
205
        static const char *metabrowser_list[] = {
 
206
                "sensible-browser" /* Debian */
 
207
        };
 
208
        static const char *browser_list[] = {
 
209
                "firefox", /* Listed in the order I feel like ;) */
 
210
                "opera",
 
211
                "chrome",
 
212
                "konqueror",
 
213
                "chromium",
 
214
                "epiphany",
 
215
                "midori",
 
216
                "dillo",
 
217
                "rekonq"
 
218
        };
 
219
        char **tokenized_path = get_path(), **browsers = NULL;
 
220
        unsigned int a;
 
221
 
 
222
        /* Check for an environment variable telling us what browser to use */
 
223
        if(!browsers) {
 
224
                for(a = 0; a < sizeof(envvar_list)/sizeof(envvar_list[0]); ++a) {
 
225
                        char *value = getenv(envvar_list[a]);
 
226
 
 
227
                        if(value != NULL) {
 
228
                                // Assume the user is correct
 
229
                                browsers = (char **)malloc(sizeof(char *) * (1+1));
 
230
 
 
231
                                browsers[0] = strdup(value);
 
232
                                browsers[1] = NULL;
 
233
 
 
234
                                break;
 
235
                        }
 
236
                }
 
237
        }
 
238
 
 
239
 
 
240
 
 
241
        /* Check for any browser */
 
242
        if(!browsers) {
 
243
                unsigned int a, b, browser_list_count = sizeof(browser_list)/sizeof(browser_list[0]);
 
244
 
 
245
                browsers = (char **)calloc(browser_list_count, sizeof(char *));
 
246
 
 
247
                for(a = b = 0; a < browser_list_count; ++a) {
 
248
                        if(executable_exists_in_path(tokenized_path, browser_list[a]))
 
249
                                browsers[b++] = strdup(browser_list[a]);
 
250
                }
 
251
 
 
252
                if(b == 0) {
 
253
                        free(browsers);
 
254
                        browsers = NULL;
 
255
                }
 
256
        }
 
257
 
 
258
 
 
259
        /* Check for distro-specific browser detection type stuff */
 
260
        if(!browsers) {
 
261
                for(a = 0; a < sizeof(metabrowser_list)/sizeof(metabrowser_list[0]); ++a) {
 
262
                        if(executable_exists_in_path(tokenized_path, metabrowser_list[a])) {
 
263
                                // Let it handle the work
 
264
                                browsers = (char **)malloc(sizeof(char *) * (1+1));
 
265
 
 
266
                                browsers[0] = strdup(metabrowser_list[a]);
 
267
                                browsers[1] = NULL;
 
268
 
 
269
                                break;
 
270
                        }
 
271
                }
 
272
        }
 
273
 
 
274
 
 
275
 
 
276
        free_path(tokenized_path);
 
277
 
 
278
        return browsers;
 
279
}
 
280
void free_browsers(char **browsers) {
 
281
        unsigned int a;
 
282
 
 
283
        if(browsers) {
 
284
                for(a = 0; browsers[a]; ++a)
 
285
                        free(browsers[a]);
 
286
                free(browsers);
 
287
        }
 
288
}
 
289
 
 
290
#endif// __unix
 
291
 
 
292
 
 
293
 
 
294
 
 
295
 
 
296
 
 
297
 
 
298
 
 
299
 
 
300
 
 
301
 
 
302
 
 
303
 
 
304
 
 
305
 
 
306
 
 
307
 
 
308
 
 
309
 
 
310
 
 
311
 
 
312
 
 
313
 
 
314
 
 
315
 
 
316
 
 
317
 
 
318
 
 
319
// открываем браузер
 
320
bool vw_OpenBrouser(const char *url)
 
321
{
 
322
#ifdef WIN32
 
323
 
 
324
        HINSTANCE result;
 
325
        result = ShellExecute(NULL, _T("open"), url, NULL,NULL, SW_NORMAL);
 
326
        if ((UINT)result > HINSTANCE_ERROR) return true;
 
327
 
 
328
        // если не получилось, делаем по второму сценарию
 
329
        TCHAR key[MAX_PATH + MAX_PATH];
 
330
        if (GetRegistryKey(HKEY_CLASSES_ROOT, _T(".htm"), key) == ERROR_SUCCESS)
 
331
        {
 
332
                lstrcat(key, _T("\\shell\\open\\command"));
 
333
 
 
334
                if (GetRegistryKey(HKEY_CLASSES_ROOT,key,key) == ERROR_SUCCESS)
 
335
                {
 
336
                        TCHAR *pos;
 
337
                        pos = _tcsstr(key, _T("\"%1\""));
 
338
                        if (pos == NULL)
 
339
                        {                     // No quotes found
 
340
                                pos = strstr(key, _T("%1"));       // Check for %1, without quotes
 
341
                                if (pos == NULL)                   // No parameter at all...
 
342
                                        pos = key+_tcslen(key)-1;
 
343
                                else
 
344
                                        *pos = _T('\0');                   // Remove the parameter
 
345
                        }
 
346
                        else
 
347
                                *pos = _T('\0');                       // Remove the parameter
 
348
 
 
349
                        lstrcat(pos, _T(" "));
 
350
                        lstrcat(pos, url);
 
351
                        result = (HINSTANCE) WinExec(key,SW_NORMAL);
 
352
                }
 
353
        }
 
354
        else
 
355
                return false;
 
356
 
 
357
#endif // WIN32
 
358
#if defined(__APPLE__) && defined(__MACH__)
 
359
 
 
360
        CFURLRef openurl = CFURLCreateWithBytes (
 
361
                                NULL,                                           // allocator
 
362
                                (BYTE*)url,                                     // URLBytes
 
363
                                strlen(url),                            // length
 
364
                                kCFStringEncodingASCII,         // encoding
 
365
                                NULL);                                          // baseURL
 
366
        LSOpenCFURLRef(openurl,0);
 
367
        CFRelease(openurl);
 
368
 
 
369
#endif // __APPLE__
 
370
#ifdef __unix
 
371
 
 
372
        char **browsers = get_browsers();
 
373
        unsigned int a = 0;
 
374
 
 
375
 
 
376
 
 
377
 
 
378
        if(browsers)
 
379
    {
 
380
        // не перебираем!!! берем первый броузер
 
381
        a = 0;
 
382
 
 
383
 
 
384
                // проверка, если установлен 93 дисплей - это компиз... открываем в 0-м
 
385
                char *value = getenv("DISPLAY");
 
386
                bool NeedSetDisplay = false;
 
387
                if (value != 0)
 
388
                {
 
389
                        char *display = new char[strlen(value)+1];
 
390
                        display = strdup(value);
 
391
                        if (!strcmp(display, ":93"))
 
392
                        {
 
393
                                NeedSetDisplay = true;
 
394
                                printf("DISPLAY=%s \n", display);
 
395
                        }
 
396
                        delete [] display;
 
397
                }
 
398
 
 
399
 
 
400
        printf("%u:\t%s\n", a, browsers[a]);
 
401
 
 
402
        char GotoUrl[1024];
 
403
 
 
404
        if (NeedSetDisplay)
 
405
                        sprintf(GotoUrl,"DISPLAY=:0 %s %s",browsers[a], url);
 
406
                else
 
407
                        sprintf(GotoUrl,"%s %s",browsers[a], url);
 
408
 
 
409
 
 
410
 
 
411
        int x;
 
412
        x = fork();
 
413
 
 
414
            switch(x)
 
415
            {
 
416
                case -1:
 
417
                        printf("error, unable to fork process!\n");
 
418
                        break;
 
419
                case 0:
 
420
                        //printf("This is the forked process!\n");
 
421
                        //system(GotoUrl); - плохо...
 
422
                        execl(getenv("SHELL"), "sh", "-c", GotoUrl, (char *)0);
 
423
                        // should not be reached
 
424
                        printf("Error executing process!\n");
 
425
                        break;
 
426
                //default:
 
427
                        //printf("This is the original process!\n");
 
428
            }
 
429
        }
 
430
        else
 
431
        {
 
432
                printf("Could not open Web page. Please, visit %s\n", url);
 
433
                return false;
 
434
        }
 
435
 
 
436
 
 
437
 
 
438
        free_browsers(browsers);
 
439
 
 
440
#endif // unix
 
441
 
 
442
 
 
443
        return true;
 
444
}