LinuxFramebufferPlatform.cs 2.8 KB

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