| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- using System;
- using System.Diagnostics;
- using System.Threading;
- using Avalonia.Controls;
- using Avalonia.Controls.ApplicationLifetimes;
- using Avalonia.Controls.Embedding;
- using Avalonia.Controls.Platform;
- using Avalonia.Input;
- using Avalonia.Input.Platform;
- using Avalonia.LinuxFramebuffer;
- using Avalonia.LinuxFramebuffer.Input;
- using Avalonia.LinuxFramebuffer.Input.EvDev;
- using Avalonia.LinuxFramebuffer.Input.LibInput;
- using Avalonia.LinuxFramebuffer.Output;
- using Avalonia.OpenGL;
- using Avalonia.Platform;
- using Avalonia.Rendering;
- using Avalonia.Rendering.Composition;
- using Avalonia.Threading;
- using JetBrains.Annotations;
- namespace Avalonia.LinuxFramebuffer
- {
- class LinuxFramebufferPlatform
- {
- IOutputBackend _fb;
- private static readonly Stopwatch St = Stopwatch.StartNew();
- internal static uint Timestamp => (uint)St.ElapsedTicks;
- public static InternalPlatformThreadingInterface Threading;
-
- internal static Compositor Compositor { get; private set; }
-
-
- LinuxFramebufferPlatform(IOutputBackend backend)
- {
- _fb = backend;
- }
- void Initialize()
- {
- Threading = new InternalPlatformThreadingInterface();
- if (_fb is IGlOutputBackend gl)
- AvaloniaLocator.CurrentMutable.Bind<IPlatformOpenGlInterface>().ToConstant(gl.PlatformOpenGlInterface);
-
- var opts = AvaloniaLocator.Current.GetService<LinuxFramebufferPlatformOptions>() ?? new LinuxFramebufferPlatformOptions();
-
- AvaloniaLocator.CurrentMutable
- .Bind<IPlatformThreadingInterface>().ToConstant(Threading)
- .Bind<IRenderTimer>().ToConstant(new DefaultRenderTimer(opts.Fps))
- .Bind<IRenderLoop>().ToConstant(new RenderLoop())
- .Bind<ICursorFactory>().ToTransient<CursorFactoryStub>()
- .Bind<IKeyboardDevice>().ToConstant(new KeyboardDevice())
- .Bind<IPlatformSettings>().ToSingleton<PlatformSettings>()
- .Bind<PlatformHotkeyConfiguration>().ToSingleton<PlatformHotkeyConfiguration>();
-
- Compositor = new Compositor(
- AvaloniaLocator.Current.GetRequiredService<IRenderLoop>(),
- AvaloniaLocator.Current.GetService<IPlatformOpenGlInterface>());
- }
-
- internal static LinuxFramebufferLifetime Initialize<T>(T builder, IOutputBackend outputBackend) where T : AppBuilderBase<T>, new()
- {
- var platform = new LinuxFramebufferPlatform(outputBackend);
- builder.UseSkia().UseWindowingSubsystem(platform.Initialize, "fbdev");
- return new LinuxFramebufferLifetime(platform._fb);
- }
- }
- class LinuxFramebufferLifetime : IControlledApplicationLifetime, ISingleViewApplicationLifetime
- {
- private readonly IOutputBackend _fb;
- [CanBeNull] private readonly IInputBackend _inputBackend;
- private TopLevel _topLevel;
- private readonly CancellationTokenSource _cts = new CancellationTokenSource();
- public CancellationToken Token => _cts.Token;
- public LinuxFramebufferLifetime(IOutputBackend fb)
- {
- _fb = fb;
- }
-
- public LinuxFramebufferLifetime(IOutputBackend fb, IInputBackend input)
- {
- _fb = fb;
- _inputBackend = input;
- }
-
- public Control MainView
- {
- get => (Control)_topLevel?.Content;
- set
- {
- if (_topLevel == null)
- {
- var inputBackend = _inputBackend;
- if (inputBackend == null)
- {
- if (Environment.GetEnvironmentVariable("AVALONIA_USE_EVDEV") == "1")
- inputBackend = EvDevBackend.CreateFromEnvironment();
- else
- inputBackend = new LibInputBackend();
- }
- var tl = new EmbeddableControlRoot(new FramebufferToplevelImpl(_fb, inputBackend));
- tl.Prepare();
- _topLevel = tl;
- _topLevel.Renderer.Start();
- if (_topLevel is IFocusScope scope)
- {
- FocusManager.Instance?.SetFocusScope(scope);
- }
- }
- _topLevel.Content = value;
- }
- }
- public int ExitCode { get; private set; }
- public event EventHandler<ControlledApplicationLifetimeStartupEventArgs> Startup;
- public event EventHandler<ControlledApplicationLifetimeExitEventArgs> Exit;
- public void Start(string[] args)
- {
- Startup?.Invoke(this, new ControlledApplicationLifetimeStartupEventArgs(args));
- }
-
- public void Shutdown(int exitCode)
- {
- ExitCode = exitCode;
- var e = new ControlledApplicationLifetimeExitEventArgs(exitCode);
- Exit?.Invoke(this, e);
- ExitCode = e.ApplicationExitCode;
- _cts.Cancel();
- }
- }
- }
- public static class LinuxFramebufferPlatformExtensions
- {
- public static int StartLinuxFbDev<T>(this T builder, string[] args, string fbdev = null, double scaling = 1)
- where T : AppBuilderBase<T>, new() =>
- StartLinuxDirect(builder, args, new FbdevOutput(fileName: fbdev, format: null) { Scaling = scaling });
- public static int StartLinuxFbDev<T>(this T builder, string[] args, string fbdev, PixelFormat? format, double scaling)
- where T : AppBuilderBase<T>, new() =>
- StartLinuxDirect(builder, args, new FbdevOutput(fileName: fbdev, format: format) { Scaling = scaling });
- public static int StartLinuxDrm<T>(this T builder, string[] args, string card = null, double scaling = 1)
- where T : AppBuilderBase<T>, new() => StartLinuxDirect(builder, args, new DrmOutput(card) {Scaling = scaling});
- public static int StartLinuxDrm<T>(this T builder, string[] args, string card = null, bool connectorsForceProbe = false, [CanBeNull] DrmOutputOptions options = null)
- where T : AppBuilderBase<T>, new() => StartLinuxDirect(builder, args, new DrmOutput(card, connectorsForceProbe, options));
-
- public static int StartLinuxDirect<T>(this T builder, string[] args, IOutputBackend backend)
- where T : AppBuilderBase<T>, new()
- {
- var lifetime = LinuxFramebufferPlatform.Initialize(builder, backend);
- builder.SetupWithLifetime(lifetime);
- lifetime.Start(args);
- builder.Instance.Run(lifetime.Token);
- return lifetime.ExitCode;
- }
- }
|