Blender  V2.93
GHOST_SystemPathsUnix.cpp
Go to the documentation of this file.
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) 2010 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include <cstdio>
25 #include <sstream>
26 
27 #include "GHOST_SystemPathsUnix.h"
28 
29 #include "GHOST_Debug.h"
30 
31 // For timing
32 
33 #include <sys/time.h>
34 #include <unistd.h>
35 
36 #include <cstdlib> /* for exit */
37 #include <stdio.h> /* for fprintf only */
38 
39 #include <pwd.h> /* for get home without use getenv() */
40 #include <string>
41 
42 using std::string;
43 
44 #ifdef PREFIX
45 static const char *static_path = PREFIX "/share";
46 #else
47 static const char *static_path = NULL;
48 #endif
49 
51 {
52 }
53 
55 {
56 }
57 
58 const GHOST_TUns8 *GHOST_SystemPathsUnix::getSystemDir(int, const char *versionstr) const
59 {
60  /* XXX On Debian ignore versionstr when building the system path */
61  versionstr = "";
62 
63  /* no prefix assumes a portable build which only uses bundled scripts */
64  if (static_path) {
65  static string system_path = string(static_path) + "/blender/" + versionstr;
66  return (GHOST_TUns8 *)system_path.c_str();
67  }
68 
69  return NULL;
70 }
71 
72 const GHOST_TUns8 *GHOST_SystemPathsUnix::getUserDir(int version, const char *versionstr) const
73 {
74  static string user_path = "";
75  static int last_version = 0;
76 
77  /* in blender 2.64, we migrate to XDG. to ensure the copy previous settings
78  * operator works we give a different path depending on the requested version */
79  if (version < 264) {
80  if (user_path.empty() || last_version != version) {
81  const char *home = getenv("HOME");
82 
83  last_version = version;
84 
85  if (home) {
86  user_path = string(home) + "/.blender/" + versionstr;
87  }
88  else {
89  return NULL;
90  }
91  }
92  return (GHOST_TUns8 *)user_path.c_str();
93  }
94  else {
95  if (user_path.empty() || last_version != version) {
96  const char *home = getenv("XDG_CONFIG_HOME");
97 
98  last_version = version;
99 
100  if (home) {
101  user_path = string(home) + "/blender/" + versionstr;
102  }
103  else {
104  home = getenv("HOME");
105 
106  if (home == NULL)
107  home = getpwuid(getuid())->pw_dir;
108 
109  user_path = string(home) + "/.config/blender/" + versionstr;
110  }
111  }
112 
113  return (const GHOST_TUns8 *)user_path.c_str();
114  }
115 }
116 
118 {
119  const char *type_str;
120 
121  switch (type) {
123  type_str = "DESKTOP";
124  break;
126  type_str = "DOCUMENTS";
127  break;
129  type_str = "DOWNLOAD";
130  break;
132  type_str = "MUSIC";
133  break;
135  type_str = "PICTURES";
136  break;
138  type_str = "VIDEOS";
139  break;
140  default:
141  GHOST_ASSERT(
142  false,
143  "GHOST_SystemPathsUnix::getUserSpecialDir(): Invalid enum value for type parameter");
144  return NULL;
145  }
146 
147  static string path = "";
148  /* Pipe stderr to /dev/null to avoid error prints. We will fail gracefully still. */
149  string command = string("xdg-user-dir ") + type_str + " 2> /dev/null";
150 
151  FILE *fstream = popen(command.c_str(), "r");
152  if (fstream == NULL) {
153  return NULL;
154  }
155  std::stringstream path_stream;
156  while (!feof(fstream)) {
157  char c = fgetc(fstream);
158  /* xdg-user-dir ends the path with '\n'. */
159  if (c == '\n') {
160  break;
161  }
162  path_stream << c;
163  }
164  if (pclose(fstream) == -1) {
165  perror("GHOST_SystemPathsUnix::getUserSpecialDir failed at pclose()");
166  return NULL;
167  }
168 
169  path = path_stream.str();
170  return path[0] ? (const GHOST_TUns8 *)path.c_str() : NULL;
171 }
172 
174 {
175  return NULL;
176 }
177 
178 void GHOST_SystemPathsUnix::addToSystemRecentFiles(const char * /*filename*/) const
179 {
180  /* TODO: implement for X11 */
181 }
#define GHOST_ASSERT(x, info)
Definition: GHOST_Debug.h:79
static const char * static_path
GHOST_TUserSpecialDirTypes
Definition: GHOST_Types.h:568
@ GHOST_kUserSpecialDirDesktop
Definition: GHOST_Types.h:569
@ GHOST_kUserSpecialDirMusic
Definition: GHOST_Types.h:572
@ GHOST_kUserSpecialDirPictures
Definition: GHOST_Types.h:573
@ GHOST_kUserSpecialDirVideos
Definition: GHOST_Types.h:574
@ GHOST_kUserSpecialDirDownloads
Definition: GHOST_Types.h:571
@ GHOST_kUserSpecialDirDocuments
Definition: GHOST_Types.h:570
unsigned char GHOST_TUns8
Definition: GHOST_Types.h:60
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
const GHOST_TUns8 * getUserSpecialDir(GHOST_TUserSpecialDirTypes type) const
const GHOST_TUns8 * getBinaryDir() const
const GHOST_TUns8 * getUserDir(int version, const char *versionstr) const
void addToSystemRecentFiles(const char *filename) const
const GHOST_TUns8 * getSystemDir(int version, const char *versionstr) const
static unsigned c
Definition: RandGen.cpp:97