Blender V2.61 - r43446

winstuff.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  * Windows-posix compatibility layer, windows-specific functions.
00027  */
00028 
00034 #ifdef WIN32
00035 
00036 #include <stdlib.h>
00037 #include <stdio.h>
00038 #include <conio.h>
00039 
00040 #include "MEM_guardedalloc.h"
00041 #include "BLI_path_util.h"
00042 #include "BLI_string.h"
00043 
00044 #include "BKE_utildefines.h"
00045 #include "BKE_global.h"
00046 
00047 #define WIN32_SKIP_HKEY_PROTECTION      // need to use HKEY
00048 #include "BLI_winstuff.h"
00049 
00050  /* FILE_MAX */
00051 
00052 int BLI_getInstallationDir( char * str )
00053 {
00054     char dir[FILE_MAXDIR];
00055     int a;
00056     
00057     GetModuleFileName(NULL,str,FILE_MAX);
00058     BLI_split_dir_part(str, dir, sizeof(dir)); /* shouldn't be relative */
00059     a = strlen(dir);
00060     if(dir[a-1] == '\\') dir[a-1]=0;
00061     
00062     strcpy(str,dir);
00063     
00064     return 1;
00065 }
00066 
00067 void RegisterBlendExtension_Fail(HKEY root)
00068 {
00069     printf("failed\n");
00070     if (root)
00071         RegCloseKey(root);
00072     if (!G.background)
00073         MessageBox(0,"Could not register file extension.","Blender error",MB_OK|MB_ICONERROR);
00074     TerminateProcess(GetCurrentProcess(),1);
00075 }
00076 
00077 void RegisterBlendExtension(void)
00078 {
00079     LONG lresult;
00080     HKEY hkey = 0;
00081     HKEY root = 0;
00082     BOOL usr_mode = FALSE;
00083     DWORD dwd = 0;
00084     char buffer[256];
00085 
00086     char BlPath[MAX_PATH];
00087     char InstallDir[FILE_MAXDIR];
00088     char SysDir[FILE_MAXDIR];
00089     const char* ThumbHandlerDLL;
00090     char RegCmd[MAX_PATH*2];
00091     char MBox[256];
00092     BOOL IsWOW64;
00093 
00094     printf("Registering file extension...");
00095     GetModuleFileName(0,BlPath,MAX_PATH);
00096 
00097     // root is HKLM by default
00098     lresult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Classes", 0, KEY_ALL_ACCESS, &root);
00099     if (lresult != ERROR_SUCCESS)
00100     {
00101         // try HKCU on failure
00102         usr_mode = TRUE;
00103         lresult = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Classes", 0, KEY_ALL_ACCESS, &root);
00104         if (lresult != ERROR_SUCCESS)
00105             RegisterBlendExtension_Fail(0);
00106     }
00107 
00108     lresult = RegCreateKeyEx(root, "blendfile", 0,
00109         NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
00110     if (lresult == ERROR_SUCCESS) {
00111         strcpy(buffer,"Blender File");
00112         lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, (BYTE*)buffer, strlen(buffer) + 1);
00113         RegCloseKey(hkey);
00114     }
00115     if (lresult != ERROR_SUCCESS)
00116         RegisterBlendExtension_Fail(root);
00117 
00118     lresult = RegCreateKeyEx(root, "blendfile\\shell\\open\\command", 0,
00119         NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
00120     if (lresult == ERROR_SUCCESS) {
00121         sprintf(buffer, "\"%s\" \"%%1\"", BlPath);
00122         lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, (BYTE*)buffer, strlen(buffer) + 1);
00123         RegCloseKey(hkey);
00124     }
00125     if (lresult != ERROR_SUCCESS)
00126         RegisterBlendExtension_Fail(root);
00127 
00128     lresult = RegCreateKeyEx(root, "blendfile\\DefaultIcon", 0,
00129         NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
00130     if (lresult == ERROR_SUCCESS) {
00131         sprintf(buffer, "\"%s\",1", BlPath);
00132         lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, (BYTE*)buffer, strlen(buffer) + 1);
00133         RegCloseKey(hkey);
00134     }
00135     if (lresult != ERROR_SUCCESS)
00136         RegisterBlendExtension_Fail(root);
00137 
00138     lresult = RegCreateKeyEx(root, ".blend", 0,
00139         NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
00140     if (lresult == ERROR_SUCCESS) {
00141         sprintf(buffer, "%s", "blendfile");
00142         lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, (BYTE*)buffer, strlen(buffer) + 1);
00143         RegCloseKey(hkey);
00144     }
00145     if (lresult != ERROR_SUCCESS)
00146         RegisterBlendExtension_Fail(root);
00147     
00148     BLI_getInstallationDir(InstallDir);
00149     GetSystemDirectory(SysDir,FILE_MAXDIR);
00150 #ifdef WIN64
00151     ThumbHandlerDLL = "BlendThumb64.dll";
00152 #else
00153     IsWow64Process(GetCurrentProcess(),&IsWOW64);
00154     if (IsWOW64 == TRUE)
00155         ThumbHandlerDLL = "BlendThumb64.dll";
00156     else
00157         ThumbHandlerDLL = "BlendThumb.dll";
00158 #endif  
00159     snprintf(RegCmd,MAX_PATH*2,"%s\\regsvr32 /s \"%s\\%s\"",SysDir,InstallDir,ThumbHandlerDLL);
00160     system(RegCmd);
00161 
00162     RegCloseKey(root);
00163     printf("success (%s)\n",usr_mode ? "user" : "system");
00164     if (!G.background)
00165     {
00166         sprintf(MBox,"File extension registered for %s.",usr_mode ? "the current user. To register for all users, run as an administrator" : "all users");
00167         MessageBox(0,MBox,"Blender",MB_OK|MB_ICONINFORMATION);
00168     }
00169     TerminateProcess(GetCurrentProcess(),0);
00170 }
00171 
00172 DIR *opendir (const char *path)
00173 {
00174     if (GetFileAttributes(path) & FILE_ATTRIBUTE_DIRECTORY) {
00175         DIR *newd= MEM_mallocN(sizeof(DIR), "opendir");
00176 
00177         newd->handle = INVALID_HANDLE_VALUE;
00178         sprintf(newd->path, "%s\\*",path);
00179         
00180         newd->direntry.d_ino= 0;
00181         newd->direntry.d_off= 0;
00182         newd->direntry.d_reclen= 0;
00183         newd->direntry.d_name= NULL;
00184         
00185         return newd;
00186     } else {
00187         return NULL;
00188     }
00189 }
00190 
00191 struct dirent *readdir(DIR *dp)
00192 {
00193     if (dp->direntry.d_name) {
00194         MEM_freeN(dp->direntry.d_name);
00195         dp->direntry.d_name= NULL;
00196     }
00197         
00198     if (dp->handle==INVALID_HANDLE_VALUE) {
00199         dp->handle= FindFirstFile(dp->path, &(dp->data));
00200         if (dp->handle==INVALID_HANDLE_VALUE)
00201             return NULL;
00202             
00203         dp->direntry.d_name= BLI_strdup(dp->data.cFileName);
00204 
00205         return &dp->direntry;
00206     } else if (FindNextFile (dp->handle, &(dp->data))) {
00207         dp->direntry.d_name= BLI_strdup(dp->data.cFileName);
00208 
00209         return &dp->direntry;
00210     } else {
00211         return NULL;
00212     }
00213 }
00214 
00215 int closedir (DIR *dp)
00216 {
00217     if (dp->direntry.d_name) MEM_freeN(dp->direntry.d_name);
00218     if (dp->handle!=INVALID_HANDLE_VALUE) FindClose(dp->handle);
00219 
00220     MEM_freeN(dp);
00221     
00222     return 0;
00223 }
00224 
00225 void get_default_root(char* root)
00226 {
00227     char str[MAX_PATH+1];
00228     
00229     /* the default drive to resolve a directory without a specified drive 
00230        should be the Windows installation drive, since this was what the OS
00231        assumes. */
00232     if (GetWindowsDirectory(str,MAX_PATH+1)) {
00233         root[0] = str[0];
00234         root[1] = ':';
00235         root[2] = '\\';
00236         root[3] = '\0';
00237     } else {        
00238         /* if GetWindowsDirectory fails, something has probably gone wrong, 
00239            we are trying the blender install dir though */
00240         if (GetModuleFileName(NULL,str,MAX_PATH+1)) {
00241             printf("Error! Could not get the Windows Directory - Defaulting to Blender installation Dir!");
00242             root[0] = str[0];
00243             root[1] = ':';
00244             root[2] = '\\';
00245             root[3] = '\0';
00246         } else {
00247             DWORD tmp;
00248             int i;
00249             int rc = 0;
00250             /* now something has gone really wrong - still trying our best guess */
00251             printf("Error! Could not get the Windows Directory - Defaulting to first valid drive! Path might be invalid!");
00252             tmp= GetLogicalDrives();
00253             for (i=2; i < 26; i++) {
00254                 if ((tmp>>i) & 1) {
00255                     root[0] = 'a'+i;
00256                     root[1] = ':';
00257                     root[2] = '\\';
00258                     root[3] = '\0';
00259                     if (GetFileAttributes(root) != 0xFFFFFFFF) {
00260                         rc = i;
00261                         break;          
00262                     }
00263                 }
00264             }
00265             if (0 == rc) {
00266                 printf("ERROR in 'get_default_root': can't find a valid drive!");
00267                 root[0] = 'C';
00268                 root[1] = ':';
00269                 root[2] = '\\';
00270                 root[3] = '\0';
00271             }
00272         }       
00273     }
00274 }
00275 
00276 int check_file_chars(char *filename)
00277 {
00278     char *p = filename;
00279     while (*p) {
00280         switch (*p) {
00281             case ':':
00282             case '?':
00283             case '*':
00284             case '|':
00285             case '\\':
00286             case '/':
00287             case '\"':
00288                 return 0;
00289                 break;
00290         }
00291 
00292         p++;
00293     }
00294     return 1;
00295 }
00296 
00297 /* Copied from http://sourceware.org/ml/newlib/2005/msg00248.html */
00298 /* Copyright 2005 Shaun Jackman
00299  * Permission to use, copy, modify, and distribute this software
00300  * is freely granted, provided that this notice is preserved.
00301  */
00302 #include <string.h>
00303 char* dirname(char *path)
00304 {
00305     char *p;
00306     if( path == NULL || *path == '\0' )
00307         return ".";
00308     p = path + strlen(path) - 1;
00309     while( *p == '/' ) {
00310         if( p == path )
00311             return path;
00312         *p-- = '\0';
00313     }
00314     while( p >= path && *p != '/' )
00315         p--;
00316     return
00317         p < path ? "." :
00318         p == path ? "/" :
00319         (*p = '\0', path);
00320 }
00321 /* End of copied part */
00322 
00323 #else
00324 
00325 /* intentionally empty for UNIX */
00326 
00327 #endif