Skip to content

Commit

Permalink
Use Tmds.DBus.Protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
affederaffe committed Dec 29, 2022
1 parent 2bf384c commit d7904b3
Show file tree
Hide file tree
Showing 29 changed files with 4,272 additions and 907 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "nukebuild/il-repack"]
path = nukebuild/il-repack
url = https://github.com/Gillibald/il-repack
[submodule "Tmds.DBus"]
path = src/Linux/Tmds.DBus
url = https://github.com/affederaffe/Tmds.DBus
7 changes: 4 additions & 3 deletions Avalonia.Desktop.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"samples\\ControlCatalog\\ControlCatalog.csproj",
"samples\\IntegrationTestApp\\IntegrationTestApp.csproj",
"samples\\MiniMvvm\\MiniMvvm.csproj",
"samples\\ReactiveUIDemo\\ReactiveUIDemo.csproj",
"samples\\SampleControls\\ControlSamples.csproj",
"samples\\Sandbox\\Sandbox.csproj",
"samples\\ReactiveUIDemo\\ReactiveUIDemo.csproj",
"src\\Avalonia.Base\\Avalonia.Base.csproj",
"src\\Avalonia.Build.Tasks\\Avalonia.Build.Tasks.csproj",
"src\\Avalonia.Controls.ColorPicker\\Avalonia.Controls.ColorPicker.csproj",
Expand All @@ -31,15 +31,16 @@
"src\\Avalonia.Themes.Simple\\Avalonia.Themes.Simple.csproj",
"src\\Avalonia.X11\\Avalonia.X11.csproj",
"src\\Linux\\Avalonia.LinuxFramebuffer\\Avalonia.LinuxFramebuffer.csproj",
"src\\Linux\\Tmds.DBus\\src\\Tmds.DBus.Protocol\\Tmds.DBus.Protocol.csproj",
"src\\Markup\\Avalonia.Markup.Xaml.Loader\\Avalonia.Markup.Xaml.Loader.csproj",
"src\\Markup\\Avalonia.Markup.Xaml\\Avalonia.Markup.Xaml.csproj",
"src\\Markup\\Avalonia.Markup\\Avalonia.Markup.csproj",
"src\\Skia\\Avalonia.Skia\\Avalonia.Skia.csproj",
"src\\tools\\DevAnalyzers\\DevAnalyzers.csproj",
"src\\tools\\DevGenerators\\DevGenerators.csproj",
"src\\Windows\\Avalonia.Direct2D1\\Avalonia.Direct2D1.csproj",
"src\\Windows\\Avalonia.Win32.Interop\\Avalonia.Win32.Interop.csproj",
"src\\Windows\\Avalonia.Win32\\Avalonia.Win32.csproj",
"src\\tools\\DevAnalyzers\\DevAnalyzers.csproj",
"src\\tools\\DevGenerators\\DevGenerators.csproj",
"tests\\Avalonia.Base.UnitTests\\Avalonia.Base.UnitTests.csproj",
"tests\\Avalonia.Benchmarks\\Avalonia.Benchmarks.csproj",
"tests\\Avalonia.Controls.DataGrid.UnitTests\\Avalonia.Controls.DataGrid.UnitTests.csproj",
Expand Down
7 changes: 7 additions & 0 deletions Avalonia.sln
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ControlCatalog.Browser.Blaz
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ReactiveUIDemo", "samples\ReactiveUIDemo\ReactiveUIDemo.csproj", "{75C47156-C5D8-44BC-A5A7-E8657C2248D6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tmds.DBus.Protocol", "src\Linux\Tmds.DBus\src\Tmds.DBus.Protocol\Tmds.DBus.Protocol.csproj", "{29E25263-3CC3-4D55-A042-00BA136867D4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -542,6 +544,10 @@ Global
{75C47156-C5D8-44BC-A5A7-E8657C2248D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{75C47156-C5D8-44BC-A5A7-E8657C2248D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{75C47156-C5D8-44BC-A5A7-E8657C2248D6}.Release|Any CPU.Build.0 = Release|Any CPU
{29E25263-3CC3-4D55-A042-00BA136867D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{29E25263-3CC3-4D55-A042-00BA136867D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{29E25263-3CC3-4D55-A042-00BA136867D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{29E25263-3CC3-4D55-A042-00BA136867D4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -606,6 +612,7 @@ Global
{15B93A4C-1B46-43F6-B534-7B25B6E99932} = {9B9E3891-2366-4253-A952-D08BCEB71098}
{90B08091-9BBD-4362-B712-E9F2CC62B218} = {9B9E3891-2366-4253-A952-D08BCEB71098}
{75C47156-C5D8-44BC-A5A7-E8657C2248D6} = {9B9E3891-2366-4253-A952-D08BCEB71098}
{29E25263-3CC3-4D55-A042-00BA136867D4} = {86C53C40-57AA-45B8-AD42-FAE0EFDF0F2B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {87366D66-1391-4D90-8999-95A620AD786A}
Expand Down
1 change: 1 addition & 0 deletions NuGet.Config
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
<add key="api.nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="dotnet-eng" value="https://nuget.avaloniaui.net/repository/avalonia-devdeps/index.json" protocolVersion="3" />
<add key="skiasharp" value="https://aka.ms/skiasharp-eap/index.json" />
<add key="tmds-dbus" value="https://www.myget.org/F/tmds/api/v3/index.json" />
</packageSources>
</configuration>
176 changes: 176 additions & 0 deletions src/Avalonia.FreeDesktop/AppMenuRegistrar.DBus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
using System;
using System.Threading.Tasks;

using Tmds.DBus.Protocol;


namespace Avalonia.FreeDesktop
{
internal class Registrar : AppMenuRegistrarObject
{
private const string Interface = "com.canonical.AppMenu.Registrar";

public Registrar(RegistrarService service, ObjectPath path) : base(service, path) { }

public Task RegisterWindowAsync(uint windowId, ObjectPath menuObjectPath)
{
return Connection.CallMethodAsync(CreateMessage());

MessageBuffer CreateMessage()
{
using var writer = Connection.GetMessageWriter();
writer.WriteMethodCallHeader(
Service.Destination,
Path,
Interface,
signature: "uo",
member: "RegisterWindow",
flags: MessageFlags.NoReplyExpected);
writer.WriteUInt32(windowId);
writer.WriteObjectPath(menuObjectPath);
return writer.CreateMessage();
}
}

public Task UnregisterWindowAsync(uint windowId)
{
return Connection.CallMethodAsync(CreateMessage());

MessageBuffer CreateMessage()
{
using var writer = Connection.GetMessageWriter();
writer.WriteMethodCallHeader(
Service.Destination,
Path,
Interface,
signature: "u",
member: "UnregisterWindow");
writer.WriteUInt32(windowId);
return writer.CreateMessage();
}
}

public Task<(string Service, ObjectPath MenuObjectPath)> GetMenuForWindowAsync(uint windowId)
{
return Connection.CallMethodAsync(CreateMessage(), (Message m, object? s) => ReadMessage_so(m, (AppMenuRegistrarObject)s!), this);

MessageBuffer CreateMessage()
{
using var writer = Connection.GetMessageWriter();
writer.WriteMethodCallHeader(
Service.Destination,
Path,
Interface,
signature: "u",
member: "GetMenuForWindow");
writer.WriteUInt32(windowId);
return writer.CreateMessage();
}
}
}

internal class RegistrarService
{
public RegistrarService(Connection connection, string destination)
=> (Connection, Destination) = (connection, destination);

public Connection Connection { get; }
public string Destination { get; }
public Registrar CreateRegistrar(string path) => new Registrar(this, path);
}

internal class AppMenuRegistrarObject
{
protected AppMenuRegistrarObject(RegistrarService service, ObjectPath path)
=> (Service, Path) = (service, path);

public RegistrarService Service { get; }
public ObjectPath Path { get; }
protected Connection Connection => Service.Connection;

protected MessageBuffer CreateGetPropertyMessage(string @interface, string property)
{
using var writer = Connection.GetMessageWriter();
writer.WriteMethodCallHeader(
Service.Destination,
Path,
"org.freedesktop.DBus.Properties",
signature: "ss",
member: "Get");
writer.WriteString(@interface);
writer.WriteString(property);
return writer.CreateMessage();
}

protected MessageBuffer CreateGetAllPropertiesMessage(string @interface)
{
using var writer = Connection.GetMessageWriter();
writer.WriteMethodCallHeader(
Service.Destination,
Path,
"org.freedesktop.DBus.Properties",
signature: "s",
member: "GetAll");
writer.WriteString(@interface);
return writer.CreateMessage();
}

protected ValueTask<IDisposable> WatchPropertiesChangedAsync<TProperties>(string @interface,
MessageValueReader<PropertyChanges<TProperties>> reader, Action<Exception?, PropertyChanges<TProperties>> handler,
bool emitOnCapturedContext)
{
var rule = new MatchRule
{
Type = MessageType.Signal,
Sender = Service.Destination,
Path = Path,
Interface = "org.freedesktop.DBus.Properties",
Member = "PropertiesChanged",
Arg0 = @interface
};
return Connection.AddMatchAsync(rule, reader,
(Exception? ex, PropertyChanges<TProperties> changes, object? rs, object? hs) =>
((Action<Exception?, PropertyChanges<TProperties>>)hs!).Invoke(ex, changes),
this, handler, emitOnCapturedContext);
}

public ValueTask<IDisposable> WatchSignalAsync<TArg>(string sender, string @interface, ObjectPath path, string signal,
MessageValueReader<TArg> reader, Action<Exception?, TArg> handler, bool emitOnCapturedContext)
{
var rule = new MatchRule
{
Type = MessageType.Signal,
Sender = sender,
Path = path,
Member = signal,
Interface = @interface
};
return Connection.AddMatchAsync(rule, reader,
(Exception? ex, TArg arg, object? rs, object? hs) => ((Action<Exception?, TArg>)hs!).Invoke(ex, arg),
this, handler, emitOnCapturedContext);
}

public ValueTask<IDisposable> WatchSignalAsync(string sender, string @interface, ObjectPath path, string signal, Action<Exception?> handler,
bool emitOnCapturedContext)
{
var rule = new MatchRule
{
Type = MessageType.Signal,
Sender = sender,
Path = path,
Member = signal,
Interface = @interface
};
return Connection.AddMatchAsync<object>(rule, (Message message, object? state) => null!,
(Exception? ex, object v, object? rs, object? hs) => ((Action<Exception?>)hs!).Invoke(ex), this, handler, emitOnCapturedContext);
}

protected static (string, ObjectPath) ReadMessage_so(Message message, AppMenuRegistrarObject _)
{
var reader = message.GetBodyReader();
var arg0 = reader.ReadString();
var arg1 = reader.ReadObjectPath();
return (arg0, arg1);
}
}
}
3 changes: 2 additions & 1 deletion src/Avalonia.FreeDesktop/Avalonia.FreeDesktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<ItemGroup>
<ProjectReference Include="..\Avalonia.Controls\Avalonia.Controls.csproj" />
<ProjectReference Include="..\Avalonia.Dialogs\Avalonia.Dialogs.csproj" />
<PackageReference Include="Tmds.DBus" Version="0.9.0" />
<ProjectReference Include="..\Linux\Tmds.DBus\src\Tmds.DBus.Protocol\Tmds.DBus.Protocol.csproj" />
<!--<PackageReference Include="Tmds.DBus.Protocol" Version="0.11.1-151-9f126c13809fceab8388328aff84b0d8e89b2aae" />-->
</ItemGroup>
<ItemGroup Label="InternalsVisibleTo">
<InternalsVisibleTo Include="Avalonia.X11, PublicKey=$(AvaloniaPublicKey)" />
Expand Down
Loading

0 comments on commit d7904b3

Please sign in to comment.