Beispiel #1
0
        public LinuxX11WindowImpl(LinuxX11Platform platform,
                                  int width,
                                  int height)
        {
            _platform = platform;
            X11.XSetWindowAttributes attributes = new X11.XSetWindowAttributes();
            var screen = Xlib.XDefaultScreen(_platform.Display);

            _visual = Xlib.XDefaultVisual(_platform.Display, screen);
            var depth = Xlib.XDefaultDepth(_platform.Display, screen);

            attributes.background_pixel = 0;
            attributes.bit_gravity      = 10;

            _window = Xlib.XCreateWindow(platform.Display,
                                         Xlib.XDefaultRootWindow(platform.Display),
                                         300,
                                         300,
                                         (uint)width,
                                         (uint)height,
                                         1,
                                         depth,
                                         1,
                                         _visual,
                                         1 << 1 | 1 << 4,
                                         ref attributes);

            Xlib.XSelectInput(platform.Display, _window, X11.EventMask.ExposureMask |
                              X11.EventMask.StructureNotifyMask |
                              X11.EventMask.EnterWindowMask |
                              X11.EventMask.LeaveWindowMask |
                              X11.EventMask.PointerMotionMask);
        }
Beispiel #2
0
        private void XSetWMProtocols(LinuxX11WindowImpl window)
        {
            GCHandle valueHandle = default;

            try
            {
                var atom = (ulong)WM_DELETE_PROTOCOL;
                valueHandle = GCHandle.Alloc(atom, GCHandleType.Pinned);

                int res = Xlib.XChangeProperty(Display,
                                               window.XWindow,
                                               property: WM_PROTOCOL,
                                               type: X11.Atom.Atom,
                                               format: 32,  //bits,
                                               (int)X11.PropertyMode.Replace,
                                               data: valueHandle.AddrOfPinnedObject(),
                                               nelements: 1 /*count*/);
            }
            finally
            {
                if (valueHandle.IsAllocated)
                {
                    valueHandle.Free();
                }
            }
        }
Beispiel #3
0
        public void Dispose()
        {
            _cairoSurface.Dispose();

            Xlib.XUnmapWindow(_platform.Display, _window);
            Xlib.XDestroyWindow(_platform.Display, _window);
        }
Beispiel #4
0
        public void UpdateBackground(IBrush brush)
        {
            if (brush is not SolidColorBrush solidColor)
            {
                return;
            }

            X11.XColor color    = new X11.XColor();
            var        colormap = Xlib.XDefaultColormap(_platform.Display,
                                                        Xlib.XDefaultScreen(_platform.Display));

            var status = Xlib.XParseColor(_platform.Display,
                                          colormap,
                                          $"RGBi:{solidColor.Red}/{solidColor.Green}/{solidColor.Blue}",
                                          ref color);

            //Not sure if XLib.XFreeColors should be called here.
            //The API is different and XColor is allocated by the .Net
            //It is probably should be called to remove Colormap entry
            status = Xlib.XAllocColor(_platform.Display, colormap, ref color);

            Xlib.XSetWindowBackground(_platform.Display,
                                      XWindow,
                                      color.pixel);
        }
Beispiel #5
0
        private void WaitForEvent(Dispatcher d, IntPtr ev)
        {
            Services.Logger.LogWindowingSystemEvent("Waiting for X11 events...");
            Xlib.XNextEvent(Display, ev);

            var xevent = Marshal.PtrToStructure <X11.XAnyEvent>(ev);

            HandleEvent(xevent, ev, d);
        }
Beispiel #6
0
        public LinuxX11Platform()
        {
            Display = Xlib.XOpenDisplay(null);

            //Xlib.XSetErrorHandler(OnError);

            Dispatcher.CurrentDispatcher.Initialize(ListenToEvents,
                                                    WakeUp);

            _deleteProtocolAtomProvider =
                new Lazy <X11.Atom>(() => XInternAtom(Display, "WM_DELETE_WINDOW", false));
        }
Beispiel #7
0
        public void Resize(int width, int height)
        {
            var conf = new X11.XWindowChanges()
            {
                width  = width,
                height = height
            };

            Xlib.XConfigureWindow(_platform.Display,
                                  _window,
                                  1 << 2 | 1 << 3,
                                  ref conf);
        }
Beispiel #8
0
        private int OnError(IntPtr display, ref X11.XErrorEvent ev)
        {
            int size = 256;
            var mem  = Marshal.AllocHGlobal(sizeof(char) * size);

            var status = Xlib.XGetErrorText(display,
                                            ev.error_code,
                                            mem,
                                            size);

            string msg = Marshal.PtrToStringAuto(mem);

            return(0);
        }
Beispiel #9
0
        private void HandleEvent(X11.XAnyEvent xevent, IntPtr ev, Dispatcher d)
        {
            Services.Logger.LogWindowingSystemEvent("Processing event {0}...", xevent.type);

            switch (xevent.type)
            {
            case (int)X11.Event.Expose:
                var exposeEvent = Marshal.PtrToStructure <X11.XExposeEvent>(ev);
                HandleExposeEvent(exposeEvent, d);
                break;

            case (int)X11.Event.ResizeRequest:
                while (Xlib.XCheckMaskEvent(Display, X11.EventMask.ResizeRedirectMask, ev))
                {
                }

                var resizeEvent = Marshal.PtrToStructure <X11.XResizeRequestEvent>(ev);
                HandleResizeEvent(resizeEvent, d);
                break;

            case (int)X11.Event.ConfigureNotify:
                var configuraEvent = Marshal.PtrToStructure <X11.XConfigureRequestEvent>(ev);
                HandleConfigureEvent(configuraEvent, d);
                break;

            case (int)X11.Event.DestroyNotify:
                var destroyEvent = Marshal.PtrToStructure <X11.XDestroyWindowEvent>(ev);
                HandleDestroyEvent(destroyEvent, d);
                break;

            case (int)X11.Event.ClientMessage:
                var clientMessage = Marshal.PtrToStructure <X11.XClientMessageEvent>(ev);
                HandleClientMessage(clientMessage, d);
                break;

            case (int)X11.Event.EnterNotify:
            case (int)X11.Event.LeaveNotify:
                var crossingEvent = Marshal.PtrToStructure <X11.XCrossingEvent>(ev);
                HandleCrossingEvent(crossingEvent, d);
                break;

            case (int)X11.Event.MotionNotify:
                var motionEvent = Marshal.PtrToStructure <X11.XMotionEvent>(ev);
                HandleMoveEvent(motionEvent, d);
                break;
            }
        }
Beispiel #10
0
        public void ListenToEvents()
        {
            var    d  = Dispatcher.CurrentDispatcher;
            IntPtr ev = Marshal.AllocHGlobal(24 * sizeof(long));

            try
            {
                WaitForEvent(d, ev);

                while (Xlib.XPending(Display) > 0)
                {
                    WaitForEvent(d, ev);
                }
            }
            finally
            {
                Marshal.FreeHGlobal(ev);
            }
        }
Beispiel #11
0
        private void WakeUp()
        {
            Services.Logger.LogWindowingSystemEvent("Received wake up call.");

            if (!_windows.Any())
            {
                Services.Logger.LogWindowingSystemEvent("No windows to wake. Request skipped.");
                return;
            }

            var connection = Xlib.XOpenDisplay(null);

            IntPtr ev = Marshal.AllocHGlobal(24 * sizeof(long));

            try
            {
                var msg = new X11.XClientMessageEvent()
                {
                    message_type = X11.Atom.None,
                    window       = _windows[0].XWindow,
                    display      = connection,
                    type         = (int)X11.Event.ClientMessage,
                    format       = 32
                };

                Marshal.StructureToPtr(msg, ev, false);

                X11.Status status = Xlib.XSendEvent(connection, _windows[0].XWindow, true, 0, ev);
                Services.Logger.LogWindowingSystemEvent("Message to XOrg sent. Status - {0}", status);
            }
            finally
            {
                Marshal.FreeHGlobal(ev);
                Xlib.XCloseDisplay(connection);
            }
        }
Beispiel #12
0
        public void Dispose()
        {
            Services.Logger.LogWindowingSystemEvent("Disposing platform...");

            Xlib.XCloseDisplay(Display);
        }
Beispiel #13
0
 public void Show()
 {
     Xlib.XMapWindow(_platform.Display, _window);
 }