|
|
@ -7,37 +7,51 @@ namespace Avalonia.FreeDesktop |
|
|
{ |
|
|
{ |
|
|
internal static class DBusHelper |
|
|
internal static class DBusHelper |
|
|
{ |
|
|
{ |
|
|
public static Connection? Connection { get; private set; } |
|
|
private static Connection? s_defaultConntection; |
|
|
|
|
|
private static bool s_defaultConnectionFailed; |
|
|
|
|
|
public static Connection? DefaultConnection |
|
|
|
|
|
{ |
|
|
|
|
|
get |
|
|
|
|
|
{ |
|
|
|
|
|
if (s_defaultConntection == null && !s_defaultConnectionFailed) |
|
|
|
|
|
{ |
|
|
|
|
|
s_defaultConntection = TryCreateNewConnection(); |
|
|
|
|
|
if (s_defaultConntection == null) |
|
|
|
|
|
s_defaultConnectionFailed = true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public static Connection? TryInitialize(string? dbusAddress = null) |
|
|
return s_defaultConntection; |
|
|
=> Connection ?? TryCreateNewConnection(dbusAddress); |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public static Connection? TryCreateNewConnection(string? dbusAddress = null) |
|
|
public static Connection? TryCreateNewConnection(string? dbusAddress = null) |
|
|
{ |
|
|
{ |
|
|
var oldContext = SynchronizationContext.Current; |
|
|
var oldContext = SynchronizationContext.Current; |
|
|
|
|
|
Connection? conn = null; |
|
|
try |
|
|
try |
|
|
{ |
|
|
{ |
|
|
var conn = new Connection(new ClientConnectionOptions(dbusAddress ?? Address.Session!) |
|
|
SynchronizationContext.SetSynchronizationContext(null); |
|
|
|
|
|
conn = new Connection(new ClientConnectionOptions(dbusAddress ?? Address.Session!) |
|
|
{ |
|
|
{ |
|
|
AutoConnect = false |
|
|
AutoConnect = false, |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
// Connect synchronously
|
|
|
// Connect synchronously
|
|
|
conn.ConnectAsync().GetAwaiter().GetResult(); |
|
|
conn.ConnectAsync().GetAwaiter().GetResult(); |
|
|
|
|
|
return conn; |
|
|
Connection = conn; |
|
|
|
|
|
} |
|
|
} |
|
|
catch (Exception e) |
|
|
catch (Exception e) |
|
|
{ |
|
|
{ |
|
|
Logger.TryGet(LogEventLevel.Error, "DBUS") |
|
|
Logger.TryGet(LogEventLevel.Error, "DBUS") |
|
|
?.Log(null, "Unable to connect to DBus: " + e); |
|
|
?.Log(null, "Unable to connect to DBus: " + e); |
|
|
|
|
|
conn?.Dispose(); |
|
|
} |
|
|
} |
|
|
finally |
|
|
finally |
|
|
{ |
|
|
{ |
|
|
SynchronizationContext.SetSynchronizationContext(oldContext); |
|
|
SynchronizationContext.SetSynchronizationContext(oldContext); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return Connection; |
|
|
return null; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|