2011-02-23 10:52:22 +00:00
|
|
|
/*
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
2008-04-16 22:40:48 +00:00
|
|
|
* of the License, or (at your option) any later version.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
*
|
|
|
|
* Contributor(s): none yet.
|
|
|
|
*
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
2002-10-12 11:37:38 +00:00
|
|
|
* Windows-posix compatibility layer, windows-specific functions.
|
|
|
|
*/
|
|
|
|
|
2011-02-27 20:37:56 +00:00
|
|
|
/** \file blender/blenlib/intern/winstuff.c
|
|
|
|
* \ingroup bli
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2011-04-08 12:49:38 +00:00
|
|
|
#include <conio.h>
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2010-09-15 16:13:32 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
2009-12-13 17:46:30 +00:00
|
|
|
#include "BLI_path_util.h"
|
|
|
|
#include "BLI_string.h"
|
2011-01-07 23:54:58 +00:00
|
|
|
|
|
|
|
#include "BKE_utildefines.h"
|
2011-03-29 13:00:03 +00:00
|
|
|
#include "BKE_global.h"
|
2011-01-07 23:54:58 +00:00
|
|
|
|
2005-03-19 20:04:25 +00:00
|
|
|
#define WIN32_SKIP_HKEY_PROTECTION // need to use HKEY
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "BLI_winstuff.h"
|
|
|
|
|
2011-11-26 04:07:38 +00:00
|
|
|
/* FILE_MAX */
|
2003-05-22 18:06:25 +00:00
|
|
|
|
2011-12-17 00:52:36 +00:00
|
|
|
int BLI_getInstallationDir( char * str )
|
|
|
|
{
|
2003-06-11 20:26:35 +00:00
|
|
|
char dir[FILE_MAXDIR];
|
2004-01-07 21:03:11 +00:00
|
|
|
int a;
|
2003-05-22 18:06:25 +00:00
|
|
|
|
2011-11-26 04:07:38 +00:00
|
|
|
GetModuleFileName(NULL,str,FILE_MAX);
|
2011-10-21 02:13:36 +00:00
|
|
|
BLI_split_dir_part(str, dir, sizeof(dir)); /* shouldn't be relative */
|
2004-01-07 21:03:11 +00:00
|
|
|
a = strlen(dir);
|
|
|
|
if(dir[a-1] == '\\') dir[a-1]=0;
|
2003-06-11 20:26:35 +00:00
|
|
|
|
|
|
|
strcpy(str,dir);
|
|
|
|
|
|
|
|
return 1;
|
2003-05-22 18:06:25 +00:00
|
|
|
}
|
|
|
|
|
2011-03-29 13:00:03 +00:00
|
|
|
void RegisterBlendExtension_Fail(HKEY root)
|
|
|
|
{
|
|
|
|
printf("failed\n");
|
|
|
|
if (root)
|
|
|
|
RegCloseKey(root);
|
|
|
|
if (!G.background)
|
|
|
|
MessageBox(0,"Could not register file extension.","Blender error",MB_OK|MB_ICONERROR);
|
|
|
|
TerminateProcess(GetCurrentProcess(),1);
|
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2011-12-17 00:52:36 +00:00
|
|
|
void RegisterBlendExtension(void)
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
LONG lresult;
|
|
|
|
HKEY hkey = 0;
|
2011-03-29 13:00:03 +00:00
|
|
|
HKEY root = 0;
|
|
|
|
BOOL usr_mode = FALSE;
|
2002-10-12 11:37:38 +00:00
|
|
|
DWORD dwd = 0;
|
2011-03-29 13:00:03 +00:00
|
|
|
char buffer[256];
|
|
|
|
|
|
|
|
char BlPath[MAX_PATH];
|
|
|
|
char InstallDir[FILE_MAXDIR];
|
|
|
|
char SysDir[FILE_MAXDIR];
|
2011-04-10 09:37:04 +00:00
|
|
|
const char* ThumbHandlerDLL;
|
2011-03-29 13:00:03 +00:00
|
|
|
char RegCmd[MAX_PATH*2];
|
|
|
|
char MBox[256];
|
|
|
|
BOOL IsWOW64;
|
|
|
|
|
|
|
|
printf("Registering file extension...");
|
|
|
|
GetModuleFileName(0,BlPath,MAX_PATH);
|
|
|
|
|
|
|
|
// root is HKLM by default
|
|
|
|
lresult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Classes", 0, KEY_ALL_ACCESS, &root);
|
|
|
|
if (lresult != ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
// try HKCU on failure
|
|
|
|
usr_mode = TRUE;
|
|
|
|
lresult = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Classes", 0, KEY_ALL_ACCESS, &root);
|
|
|
|
if (lresult != ERROR_SUCCESS)
|
|
|
|
RegisterBlendExtension_Fail(0);
|
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2011-03-29 13:00:03 +00:00
|
|
|
lresult = RegCreateKeyEx(root, "blendfile", 0,
|
|
|
|
NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
|
2002-10-12 11:37:38 +00:00
|
|
|
if (lresult == ERROR_SUCCESS) {
|
2011-10-09 06:03:38 +00:00
|
|
|
strcpy(buffer,"Blender File");
|
2011-03-29 13:00:03 +00:00
|
|
|
lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, (BYTE*)buffer, strlen(buffer) + 1);
|
2002-10-12 11:37:38 +00:00
|
|
|
RegCloseKey(hkey);
|
|
|
|
}
|
2011-03-29 13:00:03 +00:00
|
|
|
if (lresult != ERROR_SUCCESS)
|
|
|
|
RegisterBlendExtension_Fail(root);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2011-03-29 13:00:03 +00:00
|
|
|
lresult = RegCreateKeyEx(root, "blendfile\\shell\\open\\command", 0,
|
|
|
|
NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
|
2002-10-12 11:37:38 +00:00
|
|
|
if (lresult == ERROR_SUCCESS) {
|
2011-03-29 13:00:03 +00:00
|
|
|
sprintf(buffer, "\"%s\" \"%%1\"", BlPath);
|
|
|
|
lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, (BYTE*)buffer, strlen(buffer) + 1);
|
2002-10-12 11:37:38 +00:00
|
|
|
RegCloseKey(hkey);
|
|
|
|
}
|
2011-03-29 13:00:03 +00:00
|
|
|
if (lresult != ERROR_SUCCESS)
|
|
|
|
RegisterBlendExtension_Fail(root);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2011-03-29 13:00:03 +00:00
|
|
|
lresult = RegCreateKeyEx(root, "blendfile\\DefaultIcon", 0,
|
|
|
|
NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
|
|
|
|
if (lresult == ERROR_SUCCESS) {
|
|
|
|
sprintf(buffer, "\"%s\",1", BlPath);
|
|
|
|
lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, (BYTE*)buffer, strlen(buffer) + 1);
|
|
|
|
RegCloseKey(hkey);
|
|
|
|
}
|
|
|
|
if (lresult != ERROR_SUCCESS)
|
|
|
|
RegisterBlendExtension_Fail(root);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2011-03-29 13:00:03 +00:00
|
|
|
lresult = RegCreateKeyEx(root, ".blend", 0,
|
|
|
|
NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
|
2002-10-12 11:37:38 +00:00
|
|
|
if (lresult == ERROR_SUCCESS) {
|
|
|
|
sprintf(buffer, "%s", "blendfile");
|
2011-03-29 13:00:03 +00:00
|
|
|
lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, (BYTE*)buffer, strlen(buffer) + 1);
|
2002-10-12 11:37:38 +00:00
|
|
|
RegCloseKey(hkey);
|
|
|
|
}
|
2011-03-29 13:00:03 +00:00
|
|
|
if (lresult != ERROR_SUCCESS)
|
|
|
|
RegisterBlendExtension_Fail(root);
|
|
|
|
|
|
|
|
BLI_getInstallationDir(InstallDir);
|
|
|
|
GetSystemDirectory(SysDir,FILE_MAXDIR);
|
|
|
|
#ifdef WIN64
|
|
|
|
ThumbHandlerDLL = "BlendThumb64.dll";
|
|
|
|
#else
|
|
|
|
IsWow64Process(GetCurrentProcess(),&IsWOW64);
|
|
|
|
if (IsWOW64 == TRUE)
|
|
|
|
ThumbHandlerDLL = "BlendThumb64.dll";
|
|
|
|
else
|
|
|
|
ThumbHandlerDLL = "BlendThumb.dll";
|
|
|
|
#endif
|
|
|
|
snprintf(RegCmd,MAX_PATH*2,"%s\\regsvr32 /s \"%s\\%s\"",SysDir,InstallDir,ThumbHandlerDLL);
|
|
|
|
system(RegCmd);
|
|
|
|
|
|
|
|
RegCloseKey(root);
|
|
|
|
printf("success (%s)\n",usr_mode ? "user" : "system");
|
|
|
|
if (!G.background)
|
|
|
|
{
|
|
|
|
sprintf(MBox,"File extension registered for %s.",usr_mode ? "the current user. To register for all users, run as an administrator" : "all users");
|
|
|
|
MessageBox(0,MBox,"Blender",MB_OK|MB_ICONINFORMATION);
|
|
|
|
}
|
|
|
|
TerminateProcess(GetCurrentProcess(),0);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2011-12-17 00:52:36 +00:00
|
|
|
DIR *opendir (const char *path)
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
if (GetFileAttributes(path) & FILE_ATTRIBUTE_DIRECTORY) {
|
|
|
|
DIR *newd= MEM_mallocN(sizeof(DIR), "opendir");
|
|
|
|
|
|
|
|
newd->handle = INVALID_HANDLE_VALUE;
|
2006-08-20 14:41:13 +00:00
|
|
|
sprintf(newd->path, "%s\\*",path);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
newd->direntry.d_ino= 0;
|
|
|
|
newd->direntry.d_off= 0;
|
|
|
|
newd->direntry.d_reclen= 0;
|
|
|
|
newd->direntry.d_name= NULL;
|
|
|
|
|
|
|
|
return newd;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-17 00:52:36 +00:00
|
|
|
struct dirent *readdir(DIR *dp)
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
if (dp->direntry.d_name) {
|
|
|
|
MEM_freeN(dp->direntry.d_name);
|
|
|
|
dp->direntry.d_name= NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dp->handle==INVALID_HANDLE_VALUE) {
|
|
|
|
dp->handle= FindFirstFile(dp->path, &(dp->data));
|
|
|
|
if (dp->handle==INVALID_HANDLE_VALUE)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
dp->direntry.d_name= BLI_strdup(dp->data.cFileName);
|
|
|
|
|
|
|
|
return &dp->direntry;
|
|
|
|
} else if (FindNextFile (dp->handle, &(dp->data))) {
|
|
|
|
dp->direntry.d_name= BLI_strdup(dp->data.cFileName);
|
|
|
|
|
|
|
|
return &dp->direntry;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-17 00:52:36 +00:00
|
|
|
int closedir (DIR *dp)
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
if (dp->direntry.d_name) MEM_freeN(dp->direntry.d_name);
|
|
|
|
if (dp->handle!=INVALID_HANDLE_VALUE) FindClose(dp->handle);
|
|
|
|
|
|
|
|
MEM_freeN(dp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-17 00:52:36 +00:00
|
|
|
void get_default_root(char* root)
|
|
|
|
{
|
2007-02-28 20:11:10 +00:00
|
|
|
char str[MAX_PATH+1];
|
|
|
|
|
|
|
|
/* the default drive to resolve a directory without a specified drive
|
|
|
|
should be the Windows installation drive, since this was what the OS
|
|
|
|
assumes. */
|
|
|
|
if (GetWindowsDirectory(str,MAX_PATH+1)) {
|
|
|
|
root[0] = str[0];
|
|
|
|
root[1] = ':';
|
|
|
|
root[2] = '\\';
|
|
|
|
root[3] = '\0';
|
|
|
|
} else {
|
|
|
|
/* if GetWindowsDirectory fails, something has probably gone wrong,
|
|
|
|
we are trying the blender install dir though */
|
|
|
|
if (GetModuleFileName(NULL,str,MAX_PATH+1)) {
|
|
|
|
printf("Error! Could not get the Windows Directory - Defaulting to Blender installation Dir!");
|
|
|
|
root[0] = str[0];
|
2006-08-20 14:41:13 +00:00
|
|
|
root[1] = ':';
|
|
|
|
root[2] = '\\';
|
|
|
|
root[3] = '\0';
|
2007-02-28 20:11:10 +00:00
|
|
|
} else {
|
|
|
|
DWORD tmp;
|
|
|
|
int i;
|
|
|
|
int rc = 0;
|
|
|
|
/* now something has gone really wrong - still trying our best guess */
|
|
|
|
printf("Error! Could not get the Windows Directory - Defaulting to first valid drive! Path might be invalid!");
|
|
|
|
tmp= GetLogicalDrives();
|
|
|
|
for (i=2; i < 26; i++) {
|
|
|
|
if ((tmp>>i) & 1) {
|
|
|
|
root[0] = 'a'+i;
|
|
|
|
root[1] = ':';
|
|
|
|
root[2] = '\\';
|
|
|
|
root[3] = '\0';
|
|
|
|
if (GetFileAttributes(root) != 0xFFFFFFFF) {
|
|
|
|
rc = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (0 == rc) {
|
|
|
|
printf("ERROR in 'get_default_root': can't find a valid drive!");
|
|
|
|
root[0] = 'C';
|
|
|
|
root[1] = ':';
|
|
|
|
root[2] = '\\';
|
|
|
|
root[3] = '\0';
|
|
|
|
}
|
|
|
|
}
|
2006-08-20 14:41:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 21:43:17 +00:00
|
|
|
int check_file_chars(char *filename)
|
|
|
|
{
|
|
|
|
char *p = filename;
|
|
|
|
while (*p) {
|
|
|
|
switch (*p) {
|
|
|
|
case ':':
|
|
|
|
case '?':
|
|
|
|
case '*':
|
|
|
|
case '|':
|
|
|
|
case '\\':
|
|
|
|
case '/':
|
|
|
|
case '\"':
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-10-20 19:09:12 +00:00
|
|
|
/* Copied from http://sourceware.org/ml/newlib/2005/msg00248.html */
|
|
|
|
/* Copyright 2005 Shaun Jackman
|
|
|
|
* Permission to use, copy, modify, and distribute this software
|
|
|
|
* is freely granted, provided that this notice is preserved.
|
|
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
char* dirname(char *path)
|
|
|
|
{
|
2011-04-21 15:53:30 +00:00
|
|
|
char *p;
|
|
|
|
if( path == NULL || *path == '\0' )
|
2011-09-27 01:32:27 +00:00
|
|
|
return ".";
|
2011-04-21 15:53:30 +00:00
|
|
|
p = path + strlen(path) - 1;
|
|
|
|
while( *p == '/' ) {
|
|
|
|
if( p == path )
|
|
|
|
return path;
|
|
|
|
*p-- = '\0';
|
|
|
|
}
|
|
|
|
while( p >= path && *p != '/' )
|
2011-09-27 01:32:27 +00:00
|
|
|
p--;
|
2011-04-21 15:53:30 +00:00
|
|
|
return
|
2011-09-27 01:32:27 +00:00
|
|
|
p < path ? "." :
|
|
|
|
p == path ? "/" :
|
|
|
|
(*p = '\0', path);
|
2009-10-20 19:09:12 +00:00
|
|
|
}
|
|
|
|
/* End of copied part */
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
#else
|
|
|
|
|
2009-01-17 00:51:42 +00:00
|
|
|
/* intentionally empty for UNIX */
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
#endif
|