LinuxFramebufferPlatform.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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<IPlatformSettings>().ToSingleton<PlatformSettings>()
  34. .Bind<IRendererFactory>().ToConstant(ImmediateRenderer.Factory)
  35. .Bind<IPlatformThreadingInterface>().ToConstant(PlatformThreadingInterface.Instance)
  36. .Bind<IRenderLoop>().ToConstant(PlatformThreadingInterface.Instance);
  37. }
  38. internal static TopLevel Initialize<T>(T builder, string fbdev = null) where T : AppBuilderBase<T>, new()
  39. {
  40. var platform = new LinuxFramebufferPlatform(fbdev);
  41. builder.UseSkia().UseWindowingSubsystem(platform.Initialize, "fbdev")
  42. .SetupWithoutStarting();
  43. var tl = new EmbeddableControlRoot(TopLevel = new FramebufferToplevelImpl(platform._fb));
  44. tl.Prepare();
  45. return tl;
  46. }
  47. }
  48. }
  49. public static class LinuxFramebufferPlatformExtensions
  50. {
  51. class TokenClosable : ICloseable
  52. {
  53. public event EventHandler Closed;
  54. public TokenClosable(CancellationToken token)
  55. {
  56. token.Register(() => Dispatcher.UIThread.InvokeAsync(() => Closed?.Invoke(this, new EventArgs())));
  57. }
  58. }
  59. public static void InitializeWithLinuxFramebuffer<T>(this T builder, Action<TopLevel> setup,
  60. CancellationToken stop = default(CancellationToken), string fbdev = null)
  61. where T : AppBuilderBase<T>, new()
  62. {
  63. setup(LinuxFramebufferPlatform.Initialize(builder, fbdev));
  64. builder.BeforeStartCallback(builder);
  65. builder.Instance.Run(new TokenClosable(stop));
  66. }
  67. }