internal X11PlatformLifetimeEvents(AvaloniaX11Platform platform)
        {
            _platform = platform;

            if (ICELib.IceAddConnectionWatch(
                    Marshal.GetFunctionPointerForDelegate(s_iceWatchProcDelegate),
                    IntPtr.Zero) == 0)
            {
                Logger.TryGet(LogEventLevel.Warning, LogArea.X11Platform)?.Log(this,
                                                                               "SMLib was unable to add an ICE connection watcher.");
                return;
            }

            byte[] errorBuf    = new byte[255];
            IntPtr clientIdRet = IntPtr.Zero;
            var    smcConn     = SMLib.SmcOpenConnection(null,
                                                         IntPtr.Zero, 1, 0,
                                                         SmcSaveYourselfProcMask |
                                                         SmcSaveCompleteProcMask |
                                                         SmcShutdownCancelledProcMask |
                                                         SmcDieProcMask,
                                                         ref s_callbacks,
                                                         null,
                                                         ref clientIdRet,
                                                         errorBuf.Length,
                                                         errorBuf);

            if (smcConn == IntPtr.Zero)
            {
                Logger.TryGet(LogEventLevel.Warning, LogArea.X11Platform)?.Log(this,
                                                                               $"SMLib/ICELib reported a new error: {Encoding.ASCII.GetString(errorBuf)}");
                return;
            }

            if (!s_nativeToManagedMapper.TryAdd(smcConn, this))
            {
                Logger.TryGet(LogEventLevel.Warning, LogArea.X11Platform)?.Log(this,
                                                                               "SMLib was unable to add this instance to the native to managed map.");
                return;
            }

            _ = SMLib.SmcSetErrorHandler(Marshal.GetFunctionPointerForDelegate(s_smcErrorHandlerDelegate));
            _ = ICELib.IceSetErrorHandler(Marshal.GetFunctionPointerForDelegate(s_iceErrorHandlerDelegate));
            _ = ICELib.IceSetIOErrorHandler(Marshal.GetFunctionPointerForDelegate(s_iceIoErrorHandlerDelegate));

            _currentSmcConn = smcConn;
            _currentIceConn = SMLib.SmcGetIceConnection(smcConn);

            Task.Run(() =>
            {
                var token = _cancellationTokenSource.Token;
                while (!token.IsCancellationRequested)
                {
                    HandleRequests();
                }
            }, _cancellationTokenSource.Token);
        }
        private static void IceWatchHandler(IntPtr iceConn, IntPtr clientData, bool opening, IntPtr *watchData)
        {
            if (!opening)
            {
                return;
            }

            ICELib.IceRemoveConnectionWatch(Marshal.GetFunctionPointerForDelegate(s_iceWatchProcDelegate),
                                            IntPtr.Zero);
        }
 private void HandleRequests()
 {
     if (ICELib.IceProcessMessages(_currentIceConn, out _, out _) ==
         ICELib.IceProcessMessagesStatus.IceProcessMessagesIoError)
     {
         Logger.TryGet(LogEventLevel.Warning, LogArea.X11Platform)?.Log(this,
                                                                        "SMLib lost its underlying ICE connection.");
         Dispose();
     }
 }