1
/************************************************************************************
3
AstroMenace (Hardcore 3D space shooter with spaceship upgrade possibilities)
4
Copyright © 2006-2012 Michael Kurinnoy, Viewizard
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.
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.
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/>.
21
Web Site: http://www.viewizard.com/
22
Project: http://sourceforge.net/projects/openastromenace/
23
E-mail: viewizard@viewizard.com
25
*************************************************************************************/
35
//------------------------------------------------------------------------------------
36
// проверка расширения файла
37
//------------------------------------------------------------------------------------
38
bool vw_TestFileExtension(const char *name, const char *extension)
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--)
46
if(!strcmp(&name[i+1],extension)) return true;
61
//------------------------------------------------------------------------------------
62
// все что ниже, нужно для открытия броузера
63
//------------------------------------------------------------------------------------
75
static LONG GetRegistryKey(HKEY key, LPCTSTR subkey, LPTSTR retdata)
78
LONG retval = RegOpenKeyEx(key, subkey, 0, KEY_QUERY_VALUE, &hkey);
79
if (retval == ERROR_SUCCESS)
81
long datasize = MAX_PATH;
83
RegQueryValue(hkey, NULL, data, &datasize);
84
_tcscpy(retdata,data);
95
#if defined(__APPLE__) && defined(__MACH__)
97
#include <CoreFoundation/CFBundle.h>
98
#include <ApplicationServices/ApplicationServices.h>
106
// поиск пути для запуска броузера
110
Runtime dependencies:
120
#include <sys/stat.h>
121
#include <sys/types.h>
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;
130
null_path = (char *)malloc(strlen(path) + 1);
132
for(cstr = path, str = null_path; *cstr; ++cstr, ++str) {
145
tokenized_path = (char **)malloc(sizeof(char *) * (parts+1));
146
tokenized_path[parts] = NULL;
148
for(a = 0, str = null_path; a < parts; ++a) {
149
tokenized_path[a] = str;
151
do { ++str; } while(str[-1]);
154
return tokenized_path;
156
void free_path(char **tokenized_path) {
157
free(tokenized_path[0]);
158
free(tokenized_path);
161
int executable_exists_in_path(char **tokenized_path, const char *app_name) {
165
for(a = 0; !found && tokenized_path[a]; ++a) {
166
DIR *dir = opendir(tokenized_path[a]);
169
struct dirent *dirent;
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);
178
snprintf(full_path, full_path_length, "%s/%s", tokenized_path[a], app_name);
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))
185
/* FIXME It would be more accurate to test if this user can
186
* execute the file rather than if any user can.
201
char **get_browsers(void) {
202
static const char *envvar_list[] = {
205
static const char *metabrowser_list[] = {
206
"sensible-browser" /* Debian */
208
static const char *browser_list[] = {
209
"firefox", /* Listed in the order I feel like ;) */
219
char **tokenized_path = get_path(), **browsers = NULL;
222
/* Check for an environment variable telling us what browser to use */
224
for(a = 0; a < sizeof(envvar_list)/sizeof(envvar_list[0]); ++a) {
225
char *value = getenv(envvar_list[a]);
228
// Assume the user is correct
229
browsers = (char **)malloc(sizeof(char *) * (1+1));
231
browsers[0] = strdup(value);
241
/* Check for any browser */
243
unsigned int a, b, browser_list_count = sizeof(browser_list)/sizeof(browser_list[0]);
245
browsers = (char **)calloc(browser_list_count, sizeof(char *));
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]);
259
/* Check for distro-specific browser detection type stuff */
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));
266
browsers[0] = strdup(metabrowser_list[a]);
276
free_path(tokenized_path);
280
void free_browsers(char **browsers) {
284
for(a = 0; browsers[a]; ++a)
320
bool vw_OpenBrouser(const char *url)
325
result = ShellExecute(NULL, _T("open"), url, NULL,NULL, SW_NORMAL);
326
if ((UINT)result > HINSTANCE_ERROR) return true;
328
// если не получилось, делаем по второму сценарию
329
TCHAR key[MAX_PATH + MAX_PATH];
330
if (GetRegistryKey(HKEY_CLASSES_ROOT, _T(".htm"), key) == ERROR_SUCCESS)
332
lstrcat(key, _T("\\shell\\open\\command"));
334
if (GetRegistryKey(HKEY_CLASSES_ROOT,key,key) == ERROR_SUCCESS)
337
pos = _tcsstr(key, _T("\"%1\""));
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;
344
*pos = _T('\0'); // Remove the parameter
347
*pos = _T('\0'); // Remove the parameter
349
lstrcat(pos, _T(" "));
351
result = (HINSTANCE) WinExec(key,SW_NORMAL);
358
#if defined(__APPLE__) && defined(__MACH__)
360
CFURLRef openurl = CFURLCreateWithBytes (
362
(BYTE*)url, // URLBytes
363
strlen(url), // length
364
kCFStringEncodingASCII, // encoding
366
LSOpenCFURLRef(openurl,0);
372
char **browsers = get_browsers();
380
// не перебираем!!! берем первый броузер
384
// проверка, если установлен 93 дисплей - это компиз... открываем в 0-м
385
char *value = getenv("DISPLAY");
386
bool NeedSetDisplay = false;
389
char *display = new char[strlen(value)+1];
390
display = strdup(value);
391
if (!strcmp(display, ":93"))
393
NeedSetDisplay = true;
394
printf("DISPLAY=%s \n", display);
400
printf("%u:\t%s\n", a, browsers[a]);
405
sprintf(GotoUrl,"DISPLAY=:0 %s %s",browsers[a], url);
407
sprintf(GotoUrl,"%s %s",browsers[a], url);
417
printf("error, unable to fork process!\n");
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");
427
//printf("This is the original process!\n");
432
printf("Could not open Web page. Please, visit %s\n", url);
438
free_browsers(browsers);