Fix T67009, T67013: crash with Intel HD Graphics and some older drivers
[blender.git] / intern / ghost / intern / GHOST_SystemPathsWin32.cpp
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2011 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup GHOST
22  */
23
24 #include "GHOST_SystemPathsWin32.h"
25
26 #ifndef _WIN32_IE
27 #  define _WIN32_IE 0x0501
28 #endif
29 #include <shlobj.h>
30 #include "utfconv.h"
31
32 GHOST_SystemPathsWin32::GHOST_SystemPathsWin32()
33 {
34 }
35
36 GHOST_SystemPathsWin32::~GHOST_SystemPathsWin32()
37 {
38 }
39
40 const GHOST_TUns8 *GHOST_SystemPathsWin32::getSystemDir(int, const char *versionstr) const
41 {
42   static char knownpath[MAX_PATH * 3 + 128] = {
43       0}; /* 1 utf-16 might translante into 3 utf-8. 2 utf-16 translates into 4 utf-8*/
44   wchar_t knownpath_16[MAX_PATH];
45
46   HRESULT hResult = SHGetFolderPathW(
47       NULL, CSIDL_COMMON_APPDATA, NULL, SHGFP_TYPE_CURRENT, knownpath_16);
48
49   if (hResult == S_OK) {
50     conv_utf_16_to_8(knownpath_16, knownpath, MAX_PATH * 3);
51     strcat(knownpath, "\\Blender Foundation\\Blender\\");
52     strcat(knownpath, versionstr);
53     return (GHOST_TUns8 *)knownpath;
54   }
55
56   return NULL;
57 }
58
59 const GHOST_TUns8 *GHOST_SystemPathsWin32::getUserDir(int, const char *versionstr) const
60 {
61   static char knownpath[MAX_PATH * 3 + 128] = {0};
62   wchar_t knownpath_16[MAX_PATH];
63
64   HRESULT hResult = SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, knownpath_16);
65
66   if (hResult == S_OK) {
67     conv_utf_16_to_8(knownpath_16, knownpath, MAX_PATH * 3);
68     strcat(knownpath, "\\Blender Foundation\\Blender\\");
69     strcat(knownpath, versionstr);
70     return (GHOST_TUns8 *)knownpath;
71   }
72
73   return NULL;
74 }
75
76 const GHOST_TUns8 *GHOST_SystemPathsWin32::getBinaryDir() const
77 {
78   static char fullname[MAX_PATH * 3] = {0};
79   wchar_t fullname_16[MAX_PATH * 3];
80
81   if (GetModuleFileNameW(0, fullname_16, MAX_PATH)) {
82     conv_utf_16_to_8(fullname_16, fullname, MAX_PATH * 3);
83     return (GHOST_TUns8 *)fullname;
84   }
85
86   return NULL;
87 }
88
89 void GHOST_SystemPathsWin32::addToSystemRecentFiles(const char *filename) const
90 {
91   /* SHARD_PATH resolves to SHARD_PATHA for non-UNICODE build */
92   UTF16_ENCODE(filename);
93   SHAddToRecentDocs(SHARD_PATHW, filename_16);
94   UTF16_UN_ENCODE(filename);
95 }