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
29
31{
32 // Disable proxy menu in Unity window manager. Only usual menubar works with
33 // wxWidgets (at least <= 3.1). When the proxy menu menubar is enable, some
34 // important things for us do not work: menuitems UI events and shortcuts.
35 wxString wm;
36
37 if( wxGetEnv( wxT( "XDG_CURRENT_DESKTOP" ), &wm ) && wm.CmpNoCase( wxT( "Unity" ) ) == 0 )
38 wxSetEnv( wxT( "UBUNTU_MENUPROXY" ), wxT( "0" ) );
39
40 bool forceX11 = false;
41
42#if wxCHECK_VERSION( 3, 3, 2 )
43 #if wxHAS_EGL
44 // Prefer GLX backend on X11 and EGL backend on Wayland.
45 if( !wxGetEnv( wxT( "WAYLAND_DISPLAY" ), nullptr ) )
46 wxSystemOptions::SetOption( "opengl.egl", 0 );
47 #else
48 // Forces GLX on X11 and XWayland
49 forceX11 = true;
50 #endif
51#else
52 #if !wxUSE_GLCANVAS_EGL
53 // Forces GLX on X11 and XWayland
54 forceX11 = true;
55 #endif
56#endif
57
58 if( forceX11 )
59 wxSetEnv( wxT( "GDK_BACKEND" ), wxT( "x11" ) );
60
61 // Disable XInput 2 smooth scrolling to prevent duplicate scroll events on X11.
62 // On Wayland (including XWayland sessions), skip this: the Wayland compositor does
63 // not produce the duplicate events, and forcing XInput 1 breaks GtkComboBox popup
64 // grabs, causing dropdowns to close immediately on mouse-button release.
65 if( !wxGetEnv( wxT( "WAYLAND_DISPLAY" ), nullptr ) )
66 wxSetEnv( wxT( "GDK_CORE_DEVICE_EVENTS" ), wxT( "1" ) );
67}
68
69
70bool KIPLATFORM::ENV::MoveToTrash( const wxString& aPath, wxString& aError )
71{
72 GError* err = nullptr;
73 GFile* file = g_file_new_for_path( aPath.fn_str() );
74
75 bool retVal = g_file_trash( file, nullptr, &err );
76
77 // Extract the error string if the operation failed
78 if( !retVal && err )
79 aError = err->message;
80
81 g_clear_error( &err );
82 g_object_unref( file );
83
84 return retVal;
85}
86
87
88bool KIPLATFORM::ENV::IsNetworkPath( const wxString& aPath )
89{
90 // placeholder, we "nerf" behavior if its a network path so return false by default
91 return false;
92}
93
94
96{
97 wxString docsPath = g_get_user_data_dir();
98
99 if( docsPath.IsEmpty() )
100 {
101 wxFileName fallback;
102
103 fallback.AssignDir( g_get_home_dir() );
104 fallback.AppendDir( wxT( ".local" ) );
105 fallback.AppendDir( wxT( "share" ) );
106 fallback.MakeAbsolute();
107
108 docsPath = fallback.GetFullPath();
109 }
110
111 return docsPath;
112}
113
114
116{
117 return g_get_user_config_dir();
118}
119
120
122{
123 return g_get_user_data_dir();
124}
125
126
128{
129 return g_get_user_data_dir();
130}
131
132
134{
135 return g_get_user_cache_dir();
136}
137
138
139bool KIPLATFORM::ENV::GetSystemProxyConfig( const wxString& aURL, PROXY_CONFIG& aCfg )
140{
141 bool success = false;
142
143 GProxyResolver* resolver = g_proxy_resolver_get_default();
144
145 if( !resolver )
146 return false;
147
148 GError* error = nullptr;
149 char** proxyList = g_proxy_resolver_lookup( resolver, aURL.utf8_str(), nullptr, &error );
150
151 if( error )
152 {
153 g_error_free( error );
154 return false;
155 }
156
157 if( !proxyList )
158 return false;
159
160 // GProxyResolver returns a NULL-terminated list of proxy URIs.
161 // We use the first non-direct proxy.
162 for( int i = 0; proxyList[i] != nullptr; i++ )
163 {
164 wxString proxyUriStr( proxyList[i], wxConvUTF8 );
165
166 // "direct://" means no proxy needed
167 if( proxyUriStr == wxT( "direct://" ) )
168 continue;
169
170 wxURI proxyUri( proxyUriStr );
171
172 // Clear any stale data from previous lookups
173 aCfg.host.clear();
174 aCfg.username.clear();
175 aCfg.password.clear();
176
177 // Build scheme://host:port string for curl.
178 // The scheme is required for non-HTTP proxies (socks5, https, etc.)
179 wxString scheme = proxyUri.GetScheme();
180
181 if( !scheme.IsEmpty() )
182 aCfg.host = scheme + wxT( "://" );
183
184 wxString server = proxyUri.GetServer();
185
186 // Bracket IPv6 addresses when port will be appended
187 bool isIPv6 = server.Contains( wxT( ":" ) );
188
189 if( isIPv6 && proxyUri.HasPort() )
190 aCfg.host += wxT( "[" ) + server + wxT( "]" );
191 else
192 aCfg.host += server;
193
194 if( proxyUri.HasPort() )
195 aCfg.host += wxT( ":" ) + proxyUri.GetPort();
196
197 // Extract credentials from userinfo (format: user[:password])
198 wxString userInfo = proxyUri.GetUserInfo();
199
200 if( !userInfo.IsEmpty() )
201 {
202 int colonPos = userInfo.Find( wxT( ":" ) );
203
204 if( colonPos != wxNOT_FOUND )
205 {
206 aCfg.username = userInfo.Left( colonPos );
207 aCfg.password = userInfo.Mid( colonPos + 1 );
208 }
209 else
210 {
211 aCfg.username = userInfo;
212 }
213 }
214
215 success = true;
216 break;
217 }
218
219 g_strfreev( proxyList );
220
221 return success;
222}
223
224
225bool KIPLATFORM::ENV::VerifyFileSignature( const wxString& aPath )
226{
227 return true;
228}
229
230
232{
233 return wxEmptyString;
234}
235
236
237void KIPLATFORM::ENV::SetAppDetailsForWindow( wxWindow* aWindow, const wxString& aRelaunchCommand,
238 const wxString& aRelaunchDisplayName )
239{
240}
241
242
244{
245 return wxEmptyString;
246}
247
248
249void KIPLATFORM::ENV::AddToRecentDocs( const wxString& aPath )
250{
251}
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.
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...