LinuxFramebufferPlatform.cs 2.7 KB

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