12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Text;
- using System.Threading;
- using Avalonia.Controls;
- using Avalonia.Controls.Embedding;
- using Avalonia.Input;
- using Avalonia.Input.Platform;
- using Avalonia.LinuxFramebuffer;
- using Avalonia.Platform;
- using Avalonia.Rendering;
- using Avalonia.Threading;
- namespace Avalonia.LinuxFramebuffer
- {
- class LinuxFramebufferPlatform
- {
- LinuxFramebuffer _fb;
- public static KeyboardDevice KeyboardDevice = new KeyboardDevice();
- public static MouseDevice MouseDevice = new MouseDevice();
- private static readonly Stopwatch St = Stopwatch.StartNew();
- internal static uint Timestamp => (uint)St.ElapsedTicks;
- public static FramebufferToplevelImpl TopLevel;
- LinuxFramebufferPlatform(string fbdev = null)
- {
- _fb = new LinuxFramebuffer(fbdev);
- }
- void Initialize()
- {
- AvaloniaLocator.CurrentMutable
- .Bind<IStandardCursorFactory>().ToTransient<CursorFactoryStub>()
- .Bind<IKeyboardDevice>().ToConstant(KeyboardDevice)
- .Bind<IMouseDevice>().ToConstant(MouseDevice)
- .Bind<IPlatformSettings>().ToSingleton<PlatformSettings>()
- .Bind<IRendererFactory>().ToConstant(ImmediateRenderer.Factory)
- .Bind<IPlatformThreadingInterface>().ToConstant(PlatformThreadingInterface.Instance)
- .Bind<IRenderLoop>().ToConstant(PlatformThreadingInterface.Instance);
- }
- internal static TopLevel Initialize<T>(T builder, string fbdev = null) where T : AppBuilderBase<T>, new()
- {
- var platform = new LinuxFramebufferPlatform(fbdev);
- builder.UseSkia().UseWindowingSubsystem(platform.Initialize, "fbdev")
- .SetupWithoutStarting();
- var tl = new EmbeddableControlRoot(TopLevel = new FramebufferToplevelImpl(platform._fb));
- tl.Prepare();
- return tl;
- }
- }
- }
- public static class LinuxFramebufferPlatformExtensions
- {
- class TokenClosable : ICloseable
- {
- public event EventHandler Closed;
- public TokenClosable(CancellationToken token)
- {
- token.Register(() => Dispatcher.UIThread.InvokeAsync(() => Closed?.Invoke(this, new EventArgs())));
- }
- }
- public static void InitializeWithLinuxFramebuffer<T>(this T builder, Action<TopLevel> setup,
- CancellationToken stop = default(CancellationToken), string fbdev = null)
- where T : AppBuilderBase<T>, new()
- {
- setup(LinuxFramebufferPlatform.Initialize(builder, fbdev));
- builder.BeforeStartCallback(builder);
- builder.Instance.Run(new TokenClosable(stop));
- }
- }
|