KiCad PCB EDA Suite
Loading...
Searching...
No Matches
unix/environment.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2020 Ian McInerney <Ian.S.McInerney at ieee.org>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <glib.h>
22#include <gio/gio.h>
24#include <wx/filename.h>
25#include <wx/uri.h>
26#include <wx/utils.h>
27#include <wx/sysopt.h>
28#include <cstdlib>
29
30
32{
33 // Disable proxy menu in Unity window manager. Only usual menubar works with
34 // wxWidgets (at least <= 3.1). When the proxy menu menubar is enable, some
35 // important things for us do not work: menuitems UI events and shortcuts.
36 wxString wm;
37
38 if( wxGetEnv( wxT( "XDG_CURRENT_DESKTOP" ), &wm ) && wm.CmpNoCase( wxT( "Unity" ) ) == 0 )
39 wxSetEnv( wxT( "UBUNTU_MENUPROXY" ), wxT( "0" ) );
40
41 bool forceX11 = false;
42
43#if wxCHECK_VERSION( 3, 3, 2 )
44 #if wxHAS_EGL
45 // Prefer GLX backend on X11 and EGL backend on Wayland.
46 if( !wxGetEnv( wxT( "WAYLAND_DISPLAY" ), nullptr ) )
47 wxSystemOptions::SetOption( "opengl.egl", 0 );
48 #else
49 // Forces GLX on X11 and XWayland
50 forceX11 = true;
51 #endif
52#else
53 #if !wxUSE_GLCANVAS_EGL
54 // Forces GLX on X11 and XWayland
55 forceX11 = true;
56 #endif
57#endif
58
59 if( forceX11 )
60 wxSetEnv( wxT( "GDK_BACKEND" ), wxT( "x11" ) );
61
62 // Disable XInput 2 smooth scrolling to prevent duplicate scroll events on X11.
63 // On Wayland (including XWayland sessions), skip this: the Wayland compositor does
64 // not produce the duplicate events, and forcing XInput 1 breaks GtkComboBox popup
65 // grabs, causing dropdowns to close immediately on mouse-button release.
66 if( !wxGetEnv( wxT( "WAYLAND_DISPLAY" ), nullptr ) )
67 wxSetEnv( wxT( "GDK_CORE_DEVICE_EVENTS" ), wxT( "1" ) );
68}
69
70
71bool KIPLATFORM::ENV::MoveToTrash( const wxString& aPath, wxString& aError )
72{
73 GError* err = nullptr;
74 GFile* file = g_file_new_for_path( aPath.fn_str() );
75
76 bool retVal = g_file_trash( file, nullptr, &err );
77
78 // Extract the error string if the operation failed
79 if( !retVal && err )
80 aError = err->message;
81
82 g_clear_error( &err );
83 g_object_unref( file );
84
85 return retVal;
86}
87
88
89bool KIPLATFORM::ENV::IsNetworkPath( const wxString& aPath )
90{
91 // placeholder, we "nerf" behavior if its a network path so return false by default
92 return false;
93}
94
95
96bool KIPLATFORM::ENV::IsRemovablePath( const wxString& aPath )
97{
98 char* resolved = realpath( aPath.fn_str(), nullptr );
99
100 if( !resolved )
101 return false;
102
103 GFile* file = g_file_new_for_path( resolved );
104 free( resolved );
105
106 GMount* mount = g_file_find_enclosing_mount( file, nullptr, nullptr );
107 g_object_unref( file );
108
109 if( !mount )
110 return false;
111
112 // A remote or virtual mount has no local hardware drive.
113 GDrive* drive = g_mount_get_drive( mount );
114 bool removable = drive && g_drive_is_removable( drive );
115
116 if( drive )
117 g_object_unref( drive );
118
119 g_object_unref( mount );
120 return removable;
121}
122
123
125{
126 wxString docsPath = g_get_user_data_dir();
127
128 if( docsPath.IsEmpty() )
129 {
130 wxFileName fallback;
131
132 fallback.AssignDir( g_get_home_dir() );
133 fallback.AppendDir( wxT( ".local" ) );
134 fallback.AppendDir( wxT( "share" ) );
135 fallback.MakeAbsolute();
136
137 docsPath = fallback.GetFullPath();
138 }
139
140 return docsPath;
141}
142
143
145{
146 return g_get_user_config_dir();
147}
148
149
151{
152 return g_get_user_data_dir();
153}
154
155
157{
158 return g_get_user_data_dir();
159}
160
161
163{
164 return g_get_user_cache_dir();
165}
166
167
168bool KIPLATFORM::ENV::GetSystemProxyConfig( const wxString& aURL, PROXY_CONFIG& aCfg )
169{
170 bool success = false;
171
172 GProxyResolver* resolver = g_proxy_resolver_get_default();
173
174 if( !resolver )
175 return false;
176
177 GError* error = nullptr;
178 char** proxyList = g_proxy_resolver_lookup( resolver, aURL.utf8_str(), nullptr, &error );
179
180 if( error )
181 {
182 g_error_free( error );
183 return false;
184 }
185
186 if( !proxyList )
187 return false;
188
189 // GProxyResolver returns a NULL-terminated list of proxy URIs.
190 // We use the first non-direct proxy.
191 for( int i = 0; proxyList[i] != nullptr; i++ )
192 {
193 wxString proxyUriStr( proxyList[i], wxConvUTF8 );
194
195 // "direct://" means no proxy needed
196 if( proxyUriStr == wxT( "direct://" ) )
197 continue;
198
199 wxURI proxyUri( proxyUriStr );
200
201 // Clear any stale data from previous lookups
202 aCfg.host.clear();
203 aCfg.username.clear();
204 aCfg.password.clear();
205
206 // Build scheme://host:port string for curl.
207 // The scheme is required for non-HTTP proxies (socks5, https, etc.)
208 wxString scheme = proxyUri.GetScheme();
209
210 if( !scheme.IsEmpty() )
211 aCfg.host = scheme + wxT( "://" );
212
213 wxString server = proxyUri.GetServer();
214
215 // Bracket IPv6 addresses when port will be appended
216 bool isIPv6 = server.Contains( wxT( ":" ) );
217
218 if( isIPv6 && proxyUri.HasPort() )
219 aCfg.host += wxT( "[" ) + server + wxT( "]" );
220 else
221 aCfg.host += server;
222
223 if( proxyUri.HasPort() )
224 aCfg.host += wxT( ":" ) + proxyUri.GetPort();
225
226 // Extract credentials from userinfo (format: user[:password])
227 wxString userInfo = proxyUri.GetUserInfo();
228
229 if( !userInfo.IsEmpty() )
230 {
231 int colonPos = userInfo.Find( wxT( ":" ) );
232
233 if( colonPos != wxNOT_FOUND )
234 {
235 aCfg.username = userInfo.Left( colonPos );
236 aCfg.password = userInfo.Mid( colonPos + 1 );
237 }
238 else
239 {
240 aCfg.username = userInfo;
241 }
242 }
243
244 success = true;
245 break;
246 }
247
248 g_strfreev( proxyList );
249
250 return success;
251}
252
253
254bool KIPLATFORM::ENV::VerifyFileSignature( const wxString& aPath )
255{
256 return true;
257}
258
259
261{
262 return wxEmptyString;
263}
264
265
266void KIPLATFORM::ENV::SetAppDetailsForWindow( wxWindow* aWindow, const wxString& aRelaunchCommand,
267 const wxString& aRelaunchDisplayName )
268{
269}
270
271
273{
274 return wxEmptyString;
275}
276
277
278void KIPLATFORM::ENV::AddToRecentDocs( const wxString& aPath )
279{
280}
static FILENAME_RESOLVER * resolver
bool IsNetworkPath(const wxString &aPath)
Determines if a given path is a network shared file apth On Windows for example, any form of path is ...
void Init()
Perform environment initialization tasks.
bool IsRemovablePath(const wxString &aPath)
wxString GetCommandLineStr()
bool GetSystemProxyConfig(const wxString &aURL, PROXY_CONFIG &aCfg)
Retrieves platform level proxying requirements to reach the given url.
wxString GetUserDataPath()
Retrieves the operating system specific path for a user's data store.
wxString GetDocumentsPath()
Retrieves the operating system specific path for a user's documents.
wxString GetAppUserModelId()
Retrieves the app user model id, a special string used for taskbar grouping on Windows 7 and later.
void SetAppDetailsForWindow(wxWindow *aWindow, const wxString &aRelaunchCommand, const wxString &aRelaunchDisplayName)
Sets the relaunch command for taskbar pins, this is intended for Windows.
void AddToRecentDocs(const wxString &aPath)
bool MoveToTrash(const wxString &aPath, wxString &aError)
Move the specified file/directory to the trash bin/recycle bin.
wxString GetUserLocalDataPath()
Retrieves the operating system specific path for a user's local data store.
wxString GetUserConfigPath()
Retrieves the operating system specific path for a user's configuration store.
wxString GetUserCachePath()
Retrieves the operating system specific path for user's application cache.
bool VerifyFileSignature(const wxString &aPath)
Validates the code signing signature of a given file This is most likely only ever going to be applic...