]> git.lyx.org Git - lyx.git/blob - src/support/os_unix.cpp
declare specialisations, needed by the merge build. add cpp minizip file
[lyx.git] / src / support / os_unix.cpp
1 /**
2  * \file os_unix.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Ruurd A. Reitsma
7  *
8  * Full author contact details are available in file CREDITS.
9  *
10  * Various OS specific functions
11  */
12
13 #include <config.h>
14
15 #include "support/os.h"
16 #include "debug.h"
17
18 #ifdef __APPLE__
19 #include <Carbon/Carbon.h>
20 #include <ApplicationServices/ApplicationServices.h>
21 #elif defined(HAVE_FONTCONFIG_FONTCONFIG_H)
22 #include "support/filetools.h"
23 #include "support/Package.h"
24 #include <fontconfig/fontconfig.h>
25 using lyx::support::addPath;
26 #endif
27
28 using std::endl;
29 using std::string;
30
31
32 namespace lyx {
33 namespace support {
34 namespace os {
35
36 void init(int, char *[])
37 {}
38
39
40 string current_root()
41 {
42         return "/";
43 }
44
45
46 docstring::size_type common_path(docstring const & p1, docstring const & p2)
47 {
48         docstring::size_type i = 0;
49         docstring::size_type const p1_len = p1.length();
50         docstring::size_type const p2_len = p2.length();
51         while (i < p1_len && i < p2_len && p1[i] == p2[i])
52                 ++i;
53         if ((i < p1_len && i < p2_len)
54             || (i < p1_len && p1[i] != '/' && i == p2_len)
55             || (i < p2_len && p2[i] != '/' && i == p1_len))
56         {
57                 if (i)
58                         --i;     // here was the last match
59                 while (i && p1[i] != '/')
60                         --i;
61         }
62         return i;
63 }
64
65
66 string external_path(string const & p)
67 {
68         return p;
69 }
70
71
72 string internal_path(string const & p)
73 {
74         return p;
75 }
76
77
78 string external_path_list(string const & p)
79 {
80         return p;
81 }
82
83
84 string internal_path_list(string const & p)
85 {
86         return p;
87 }
88
89
90 string latex_path(string const & p)
91 {
92         return p;
93 }
94
95
96 bool is_absolute_path(string const & p)
97 {
98         return !p.empty() && p[0] == '/';
99 }
100
101
102 char const * popen_read_mode()
103 {
104         return "r";
105 }
106
107
108 string const & nulldev()
109 {
110         static string const nulldev_ = "/dev/null";
111         return nulldev_;
112 }
113
114
115 shell_type shell()
116 {
117         return UNIX;
118 }
119
120
121 char path_separator()
122 {
123         return ':';
124 }
125
126
127 void windows_style_tex_paths(bool)
128 {}
129
130 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
131 {
132 #ifdef __APPLE__
133 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
134         CFStringRef cfs_ext = CFStringCreateWithBytes(kCFAllocatorDefault,
135                                         (UInt8 *) ext.c_str(), ext.length(),
136                                         kCFStringEncodingISOLatin1, false);
137         // this is what we would like to do but it seems that the
138         // viewer for PDF is often quicktime...
139         //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer :  kLSRolesEditor;
140         (void)mode;
141         LSRolesMask role = kLSRolesAll;
142         FSRef outAppRef;
143         OSStatus status =
144                 LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator,
145                                         cfs_ext, role, &outAppRef, NULL);
146         CFRelease(cfs_ext);
147
148         return status != kLSApplicationNotFoundErr;
149 #else
150         // silence compiler warnings
151         (void)ext;
152         (void)mode;
153
154         // currently, no default viewer is tried for non-windows system
155         // support for KDE/Gnome/Macintosh may be added later
156         return false;
157 #endif
158 }
159
160
161 bool autoOpenFile(string const & filename, auto_open_mode const mode)
162 {
163 #ifdef __APPLE__
164 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
165         FSRef fileref;
166         OSStatus status =
167                 FSPathMakeRef((UInt8 *) filename.c_str(), &fileref, NULL);
168         if (status != 0)
169                 return false;
170
171         // this is what we would like to do but it seems that the
172         // viewer for PDF is often quicktime...
173         //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer :  kLSRolesEditor;
174         (void)mode;
175         LSRolesMask role = kLSRolesAll;
176         FSRef outAppRef;
177
178         status = LSGetApplicationForItem(&fileref, role, &outAppRef, NULL);
179         if (status == kLSApplicationNotFoundErr)
180                 return false;
181
182         LSLaunchFSRefSpec inLaunchSpec;
183         inLaunchSpec.appRef = &outAppRef;
184         inLaunchSpec.numDocs = 1;
185         inLaunchSpec.itemRefs = &fileref;
186         inLaunchSpec.passThruParams = NULL;
187         inLaunchSpec.launchFlags = kLSLaunchDefaults;
188         inLaunchSpec.asyncRefCon = NULL;
189         status = LSOpenFromRefSpec(&inLaunchSpec, NULL);
190
191         return status != kLSApplicationNotFoundErr;
192 #else
193         // silence compiler warnings
194         (void)filename;
195         (void)mode;
196
197         // currently, no default viewer is tried for non-windows system
198         // support for KDE/Gnome/Macintosh may be added later
199         return false;
200 #endif
201 }
202
203
204 void addFontResources()
205 {
206 #ifdef __APPLE__
207         CFBundleRef  myAppBundle = CFBundleGetMainBundle();
208         CFURLRef  myAppResourcesURL, FontsURL;
209         FSRef  fontDirRef;
210         FSSpec  fontDirSpec;
211         CFStringRef  filePath = CFStringCreateWithBytes(kCFAllocatorDefault,
212                                         (UInt8 *) "fonts", strlen("fonts"),
213                                         kCFStringEncodingISOLatin1, false);
214
215         myAppResourcesURL = CFBundleCopyResourcesDirectoryURL(myAppBundle);
216         FontsURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault,
217                         myAppResourcesURL, filePath, true);
218         if (lyxerr.debugging(Debug::FONT)) {
219                 UInt8  buf[255];
220                 if (CFURLGetFileSystemRepresentation(FontsURL, true, buf, 255))
221                         lyxerr << "Adding Fonts directory: " << buf << endl;
222         }
223         CFURLGetFSRef (FontsURL, &fontDirRef);
224         OSStatus err = FSGetCatalogInfo (&fontDirRef, kFSCatInfoNone,
225                                          NULL, NULL, &fontDirSpec, NULL);
226         if (err)
227                 lyxerr << "FSGetCatalogInfo err = " << err << endl;
228         err = FMActivateFonts (&fontDirSpec, NULL, NULL,
229                                kFMLocalActivationContext);
230         if (err)
231                 lyxerr << "FMActivateFonts err = " << err << endl;
232 #elif defined(HAVE_FONTCONFIG_FONTCONFIG_H)
233         // Register BaKoMa truetype fonts with fontconfig
234         string const fonts_dir =
235                 addPath(package().system_support().absFilename(), "fonts");
236         if (!FcConfigAppFontAddDir(0, (FcChar8 const *)fonts_dir.c_str()))
237                 lyxerr << "Unable to register fonts with fontconfig." << endl;
238 #endif
239 }
240
241
242 void restoreFontResources()
243 {
244 #if defined(HAVE_FONTCONFIG_FONTCONFIG_H) && !defined(__APPLE__)
245         FcConfigAppFontClear(0);
246 #endif
247 }
248
249 } // namespace os
250 } // namespace support
251 } // namespace lyx