Skip to content

Commit

Permalink
DBus init and usage fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kekekeks authored and jmacato committed Jun 25, 2024
1 parent 066bcab commit 833da08
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 18 deletions.
30 changes: 22 additions & 8 deletions src/Avalonia.FreeDesktop/DBusHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,51 @@ namespace Avalonia.FreeDesktop
{
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)
=> Connection ?? TryCreateNewConnection(dbusAddress);
return s_defaultConntection;
}
}

public static Connection? TryCreateNewConnection(string? dbusAddress = null)
{
var oldContext = SynchronizationContext.Current;
Connection? conn = null;
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
conn.ConnectAsync().GetAwaiter().GetResult();

Connection = conn;
return conn;
}
catch (Exception e)
{
Logger.TryGet(LogEventLevel.Error, "DBUS")
?.Log(null, "Unable to connect to DBus: " + e);
conn?.Dispose();
}
finally
{
SynchronizationContext.SetSynchronizationContext(oldContext);
}

return Connection;
return null;
}
}
}
2 changes: 1 addition & 1 deletion src/Avalonia.FreeDesktop/DBusIme/X11DBusImeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static bool DetectAndRegister()
var factory = DetectInputMethod();
if (factory is not null)
{
var conn = DBusHelper.TryInitialize();
var conn = DBusHelper.DefaultConnection;
if (conn is not null)
{
AvaloniaLocator.CurrentMutable.Bind<IX11InputMethodFactory>().ToConstant(factory(conn));
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.FreeDesktop/DBusMenuExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Avalonia.FreeDesktop
internal class DBusMenuExporter
{
public static ITopLevelNativeMenuExporter? TryCreateTopLevelNativeMenu(IntPtr xid) =>
DBusHelper.Connection is null ? null : new DBusMenuExporterImpl(DBusHelper.Connection, xid);
DBusHelper.DefaultConnection is {} conn ? new DBusMenuExporterImpl(conn, xid) : null;

public static INativeMenuExporter TryCreateDetachedNativeMenu(string path, Connection currentConnection) =>
new DBusMenuExporterImpl(currentConnection, path);
Expand Down
6 changes: 4 additions & 2 deletions src/Avalonia.FreeDesktop/DBusPlatformSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using Avalonia.Media;
using Avalonia.Platform;
using Avalonia.Threading;
using Tmds.DBus.Protocol;
using Tmds.DBus.SourceGenerator;

Expand All @@ -17,10 +18,11 @@ internal class DBusPlatformSettings : DefaultPlatformSettings

public DBusPlatformSettings()
{
if (DBusHelper.Connection is null)
if (DBusHelper.DefaultConnection is not {} conn)
return;
using var restoreContext = AvaloniaSynchronizationContext.Ensure(DispatcherPriority.Input);

_settings = new OrgFreedesktopPortalSettings(DBusHelper.Connection, "org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop");
_settings = new OrgFreedesktopPortalSettings(conn, "org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop");
_ = _settings.WatchSettingChangedAsync(SettingsChangedHandler);
_ = TryGetInitialValuesAsync();
}
Expand Down
9 changes: 6 additions & 3 deletions src/Avalonia.FreeDesktop/DBusSystemDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Avalonia.Platform;
using Avalonia.Platform.Storage;
using Avalonia.Platform.Storage.FileIO;
using Avalonia.Threading;
using Tmds.DBus.Protocol;
using Tmds.DBus.SourceGenerator;

Expand All @@ -16,10 +17,12 @@ internal class DBusSystemDialog : BclStorageProvider
{
internal static async Task<IStorageProvider?> TryCreateAsync(IPlatformHandle handle)
{
if (DBusHelper.Connection is null)
if (DBusHelper.DefaultConnection is not {} conn)
return null;

var dbusFileChooser = new OrgFreedesktopPortalFileChooser(DBusHelper.Connection, "org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop");
using var restoreContext = AvaloniaSynchronizationContext.Ensure(DispatcherPriority.Input);

var dbusFileChooser = new OrgFreedesktopPortalFileChooser(conn, "org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop");
uint version;
try
{
Expand All @@ -30,7 +33,7 @@ internal class DBusSystemDialog : BclStorageProvider
return null;
}

return new DBusSystemDialog(DBusHelper.Connection, handle, dbusFileChooser, version);
return new DBusSystemDialog(conn, handle, dbusFileChooser, version);
}

private readonly Connection _connection;
Expand Down
2 changes: 2 additions & 0 deletions src/Avalonia.FreeDesktop/DBusTrayIconImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Avalonia.Controls.Platform;
using Avalonia.Logging;
using Avalonia.Platform;
using Avalonia.Threading;
using Tmds.DBus.Protocol;
using Tmds.DBus.SourceGenerator;

Expand Down Expand Up @@ -39,6 +40,7 @@ internal class DBusTrayIconImpl : ITrayIconImpl

public DBusTrayIconImpl()
{
using var restoreContext = AvaloniaSynchronizationContext.Ensure(DispatcherPriority.Input);
_connection = DBusHelper.TryCreateNewConnection();

if (_connection is null)
Expand Down
3 changes: 0 additions & 3 deletions src/Avalonia.X11/X11Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ public void Initialize(X11PlatformOptions options)
Info = new X11Info(Display, DeferredDisplay, useXim);
Globals = new X11Globals(this);
Resources = new XResources(this);
//TODO: log
if (options.UseDBusMenu)
DBusHelper.TryInitialize();

IRenderTimer timer = options.ShouldRenderOnUIThread
? new UiThreadRenderTimer(60)
Expand Down

0 comments on commit 833da08

Please sign in to comment.