LinuxFramebufferPlatform.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4. using Avalonia;
  5. using Avalonia.Controls;
  6. using Avalonia.Controls.ApplicationLifetimes;
  7. using Avalonia.Controls.Embedding;
  8. using Avalonia.Controls.Platform;
  9. using Avalonia.Input;
  10. using Avalonia.Input.Platform;
  11. using Avalonia.LinuxFramebuffer;
  12. using Avalonia.LinuxFramebuffer.Input;
  13. using Avalonia.LinuxFramebuffer.Input.EvDev;
  14. using Avalonia.LinuxFramebuffer.Input.LibInput;
  15. using Avalonia.LinuxFramebuffer.Output;
  16. using Avalonia.Platform;
  17. using Avalonia.Rendering;
  18. using Avalonia.Rendering.Composition;
  19. #nullable enable
  20. namespace Avalonia.LinuxFramebuffer
  21. {
  22. class LinuxFramebufferPlatform
  23. {
  24. IOutputBackend _fb;
  25. private static readonly Stopwatch St = Stopwatch.StartNew();
  26. internal static uint Timestamp => (uint)St.ElapsedTicks;
  27. public static InternalPlatformThreadingInterface? Threading;
  28. internal static Compositor Compositor { get; private set; } = null!;
  29. LinuxFramebufferPlatform(IOutputBackend backend)
  30. {
  31. _fb = backend;
  32. }
  33. void Initialize()
  34. {
  35. Threading = new InternalPlatformThreadingInterface();
  36. if (_fb is IGlOutputBackend gl)
  37. AvaloniaLocator.CurrentMutable.Bind<IPlatformGraphics>().ToConstant(gl.PlatformGraphics);
  38. var opts = AvaloniaLocator.Current.GetService<LinuxFramebufferPlatformOptions>() ?? new LinuxFramebufferPlatformOptions();
  39. AvaloniaLocator.CurrentMutable
  40. .Bind<IPlatformThreadingInterface>().ToConstant(Threading)
  41. .Bind<IRenderTimer>().ToConstant(new DefaultRenderTimer(opts.Fps))
  42. .Bind<IRenderLoop>().ToConstant(new RenderLoop())
  43. .Bind<ICursorFactory>().ToTransient<CursorFactoryStub>()
  44. .Bind<IKeyboardDevice>().ToConstant(new KeyboardDevice())
  45. .Bind<IPlatformSettings>().ToSingleton<DefaultPlatformSettings>()
  46. .Bind<PlatformHotkeyConfiguration>().ToSingleton<PlatformHotkeyConfiguration>();
  47. Compositor = new Compositor(
  48. AvaloniaLocator.Current.GetRequiredService<IRenderLoop>(),
  49. AvaloniaLocator.Current.GetService<IPlatformGraphics>());
  50. }
  51. internal static LinuxFramebufferLifetime Initialize(AppBuilder builder, IOutputBackend outputBackend, IInputBackend? inputBackend)
  52. {
  53. var platform = new LinuxFramebufferPlatform(outputBackend);
  54. builder.UseSkia().UseWindowingSubsystem(platform.Initialize, "fbdev");
  55. return new LinuxFramebufferLifetime(platform._fb, inputBackend);
  56. }
  57. }
  58. class LinuxFramebufferLifetime : IControlledApplicationLifetime, ISingleViewApplicationLifetime
  59. {
  60. private readonly IOutputBackend _fb;
  61. private readonly IInputBackend? _inputBackend;
  62. private TopLevel? _topLevel;
  63. private readonly CancellationTokenSource _cts = new CancellationTokenSource();
  64. public CancellationToken Token => _cts.Token;
  65. public LinuxFramebufferLifetime(IOutputBackend fb)
  66. {
  67. _fb = fb;
  68. }
  69. public LinuxFramebufferLifetime(IOutputBackend fb, IInputBackend? input)
  70. {
  71. _fb = fb;
  72. _inputBackend = input;
  73. }
  74. public Control? MainView
  75. {
  76. get => (Control?)_topLevel?.Content;
  77. set
  78. {
  79. if (_topLevel == null)
  80. {
  81. var inputBackend = _inputBackend;
  82. if (inputBackend == null)
  83. {
  84. if (Environment.GetEnvironmentVariable("AVALONIA_USE_EVDEV") == "1")
  85. inputBackend = EvDevBackend.CreateFromEnvironment();
  86. else
  87. inputBackend = new LibInputBackend();
  88. }
  89. var tl = new EmbeddableControlRoot(new FramebufferToplevelImpl(_fb, inputBackend));
  90. tl.Prepare();
  91. _topLevel = tl;
  92. _topLevel.Renderer.Start();
  93. if (_topLevel is IFocusScope scope)
  94. {
  95. FocusManager.Instance?.SetFocusScope(scope);
  96. }
  97. }
  98. _topLevel.Content = value;
  99. }
  100. }
  101. public int ExitCode { get; private set; }
  102. public event EventHandler<ControlledApplicationLifetimeStartupEventArgs>? Startup;
  103. public event EventHandler<ControlledApplicationLifetimeExitEventArgs>? Exit;
  104. public void Start(string[] args)
  105. {
  106. Startup?.Invoke(this, new ControlledApplicationLifetimeStartupEventArgs(args));
  107. }
  108. public void Shutdown(int exitCode)
  109. {
  110. ExitCode = exitCode;
  111. var e = new ControlledApplicationLifetimeExitEventArgs(exitCode);
  112. Exit?.Invoke(this, e);
  113. ExitCode = e.ApplicationExitCode;
  114. _cts.Cancel();
  115. }
  116. }
  117. }
  118. public static class LinuxFramebufferPlatformExtensions
  119. {
  120. public static int StartLinuxFbDev(this AppBuilder builder, string[] args, string? fbdev = null, double scaling = 1, IInputBackend? inputBackend = default)
  121. => StartLinuxDirect(builder, args, new FbdevOutput(fileName: fbdev, format: null) { Scaling = scaling }, inputBackend);
  122. public static int StartLinuxFbDev(this AppBuilder builder, string[] args, string fbdev, PixelFormat? format, double scaling, IInputBackend? inputBackend = default)
  123. => StartLinuxDirect(builder, args, new FbdevOutput(fileName: fbdev, format: format) { Scaling = scaling }, inputBackend);
  124. public static int StartLinuxDrm(this AppBuilder builder, string[] args, string? card = null, double scaling = 1, IInputBackend? inputBackend = default)
  125. => StartLinuxDirect(builder, args, new DrmOutput(card) { Scaling = scaling }, inputBackend);
  126. public static int StartLinuxDrm(this AppBuilder builder, string[] args, string? card = null, bool connectorsForceProbe = false, DrmOutputOptions? options = null, IInputBackend? inputBackend = default)
  127. => StartLinuxDirect(builder, args, new DrmOutput(card, connectorsForceProbe, options), inputBackend);
  128. public static int StartLinuxDirect(this AppBuilder builder, string[] args, IOutputBackend outputBackend, IInputBackend? inputBackend = default)
  129. {
  130. var lifetime = LinuxFramebufferPlatform.Initialize(builder, outputBackend, inputBackend);
  131. builder.SetupWithLifetime(lifetime);
  132. lifetime.Start(args);
  133. builder.Instance.Run(lifetime.Token);
  134. return lifetime.ExitCode;
  135. }
  136. }