LinuxFramebufferPlatform.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Text;
  5. using System.Threading;
  6. using Avalonia.Controls;
  7. using Avalonia.Controls.Embedding;
  8. using Avalonia.Input;
  9. using Avalonia.Input.Platform;
  10. using Avalonia.LinuxFramebuffer;
  11. using Avalonia.Platform;
  12. using Avalonia.Rendering;
  13. using Avalonia.Threading;
  14. namespace Avalonia.LinuxFramebuffer
  15. {
  16. class LinuxFramebufferPlatform
  17. {
  18. LinuxFramebuffer _fb;
  19. public static KeyboardDevice KeyboardDevice = new KeyboardDevice();
  20. public static MouseDevice MouseDevice = new MouseDevice();
  21. private static readonly Stopwatch St = Stopwatch.StartNew();
  22. internal static uint Timestamp => (uint)St.ElapsedTicks;
  23. public static FramebufferToplevelImpl TopLevel;
  24. LinuxFramebufferPlatform(string fbdev = null)
  25. {
  26. _fb = new LinuxFramebuffer(fbdev);
  27. }
  28. void Initialize()
  29. {
  30. AvaloniaLocator.CurrentMutable
  31. .Bind<IStandardCursorFactory>().ToTransient<CursorFactoryStub>()
  32. .Bind<IKeyboardDevice>().ToConstant(KeyboardDevice)
  33. .Bind<IMouseDevice>().ToConstant(MouseDevice)
  34. .Bind<IPlatformSettings>().ToSingleton<PlatformSettings>()
  35. .Bind<IRendererFactory>().ToConstant(ImmediateRenderer.Factory)
  36. .Bind<IPlatformThreadingInterface>().ToConstant(PlatformThreadingInterface.Instance)
  37. .Bind<IRenderLoop>().ToConstant(PlatformThreadingInterface.Instance);
  38. }
  39. internal static TopLevel Initialize<T>(T builder, string fbdev = null) where T : AppBuilderBase<T>, new()
  40. {
  41. var platform = new LinuxFramebufferPlatform(fbdev);
  42. builder.UseSkia().UseWindowingSubsystem(platform.Initialize, "fbdev")
  43. .SetupWithoutStarting();
  44. var tl = new EmbeddableControlRoot(TopLevel = new FramebufferToplevelImpl(platform._fb));
  45. tl.Prepare();
  46. return tl;
  47. }
  48. }
  49. }
  50. public static class LinuxFramebufferPlatformExtensions
  51. {
  52. class TokenClosable : ICloseable
  53. {
  54. public event EventHandler Closed;
  55. public TokenClosable(CancellationToken token)
  56. {
  57. token.Register(() => Dispatcher.UIThread.InvokeAsync(() => Closed?.Invoke(this, new EventArgs())));
  58. }
  59. }
  60. public static void InitializeWithLinuxFramebuffer<T>(this T builder, Action<TopLevel> setup,
  61. CancellationToken stop = default(CancellationToken), string fbdev = null)
  62. where T : AppBuilderBase<T>, new()
  63. {
  64. setup(LinuxFramebufferPlatform.Initialize(builder, fbdev));
  65. builder.BeforeStartCallback(builder);
  66. builder.Instance.Run(new TokenClosable(stop));
  67. }
  68. }