Example #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            string response;

            srv = new XServerConnection(log);
            srv.connect();
            srv.sendMessage(textBox1.Text);
            response = srv.receiveMessage();
            html     = response.Substring(response.IndexOf("#H#") + 3);
            textBox2.AppendText("\r\nRESPONSE:'" + response + "'");
        }
        /// <summary>
        /// Gets a value indicating whether the window is valid.
        /// </summary>
        /// <param name="window">The pointer to the window.</param>
        /// <returns><see langword="true"/> if the window is valid; otherwise, <see langword="false"/>.</returns>
        internal static bool IsWindowValid(IntPtr window)
        {
            bool validWindow = false;
            XWindowAttributes attributes = new XWindowAttributes();
            XErrorHandler handler = new XErrorHandler(IgnoreBadWindowHandler);
            originalEventHandler = XSetErrorHandler(handler);
            using (XServerConnection serverConnection = new XServerConnection())
            {
                validWindow = XGetWindowAttributes(serverConnection.Display, window, out attributes) != 0;
            }

            XSetErrorHandler(originalEventHandler);
            originalEventHandler = null;
            return validWindow;
        }
        /// <summary>
        /// Gets a value indicating if the window is viewable.
        /// </summary>
        /// <param name="window">A pointer to the window.</param>
        /// <returns><see langword="true"/> if the window is viewable; otherwise, <see langword="false"/>.</returns>
        internal static bool IsWindowViewable(IntPtr window)
        {
            bool viewableWindow = false;
            XWindowAttributes attributes = new XWindowAttributes();
            using (XServerConnection serverConnection = new XServerConnection())
            {
                XGetWindowAttributes(serverConnection.Display, window, out attributes);
            }

            viewableWindow = attributes.map_state == XMapState.IsViewable;
            return viewableWindow;
        }
        /// <summary>
        /// Gets the text of the window.
        /// </summary>
        /// <param name="windowPointer">The pointer to the window.</param>
        /// <returns>The text of the window.</returns>
        internal static string GetWindowText(IntPtr windowPointer)
        {
            string windowText = string.Empty;
            using (XServerConnection server = new XServerConnection())
            {
                IntPtr display = server.Display;
                IntPtr namePointer = IntPtr.Zero;
                int success = XFetchName(display, windowPointer, ref namePointer);
                string name = Marshal.PtrToStringAuto(namePointer);
                if (success != 0)
                {
                    windowText = name;
                }

                XFree(namePointer);
            }

            return windowText;
        }
        /// <summary>
        /// Gets a value indicating whether the window is a dialog window.
        /// </summary>
        /// <param name="window">The pointer to the window.</param>
        /// <returns><see langword="true"/> if the window is a dialog window; otherwise, <see langword="false"/>.</returns>
        internal static bool IsDialogWindow(IntPtr window)
        {
            bool windowIsDialog = false;
            using (XServerConnection serverConnection = new XServerConnection())
            {
                int dialogAtom = (int)XInternAtom(serverConnection.Display, AtomDialogWindowType, true);
                uint returnType = 0;
                ulong itemCount = 0L;
                IntPtr prop_return = GetWindowProperty(serverConnection.Display, window, PropertyWindowType, 0, 4, out returnType, out itemCount);
                if (prop_return != IntPtr.Zero && itemCount > 0)
                {
                    // Possible window type Atoms are returned in an
                    // array, with the preferred type in the first
                    // element. Assume that if element 0 is dialog,
                    // the window will be a dialog.
                    int[] windowTypes = new int[itemCount];
                    Marshal.Copy(prop_return, windowTypes, 0, (int)itemCount);
                    windowIsDialog = windowTypes[0] == dialogAtom;
                    XFree(prop_return);
                }
            }

            return windowIsDialog;
        }
        /// <summary>
        /// Gets the class of a window.
        /// </summary>
        /// <param name="windowPointer">The pointer to the window.</param>
        /// <returns>The class of the window.</returns>
        internal static string GetWindowClass(IntPtr windowPointer)
        {
            string windowClass = string.Empty;
            using (XServerConnection server = new XServerConnection())
            {
                IntPtr display = server.Display;
                XClassHint hints = new XClassHint();
                int success = XGetClassHint(display, windowPointer, ref hints);
                if (success != 0)
                {
                    windowClass = Marshal.PtrToStringAnsi(hints.res_class);
                    XFree(hints.res_class);
                    XFree(hints.res_name);
                }
            }

            return windowClass;
        }
        /// <summary>
        /// Gets the rectangle of the specified window.
        /// </summary>
        /// <param name="window">A pointer to the window.</param>
        /// <returns>The rectangle defining the display of the window.</returns>
        internal static Rectangle GetWindowRectangle(IntPtr window)
        {
            XWindowAttributes attributes;
            int absoluteX = 0;
            int absoluteY = 0;
            IntPtr childWindow = IntPtr.Zero;
            using (XServerConnection serverConnection = new XServerConnection())
            {
                int success = XGetWindowAttributes(serverConnection.Display, window, out attributes);
                if (success != 0)
                {
                    XTranslateCoordinates(serverConnection.Display, window, attributes.root, attributes.x, attributes.y, out absoluteX, out absoluteY, out childWindow);
                }
            }

            return new Rectangle(absoluteX - attributes.x, absoluteY - attributes.y, attributes.width, attributes.height);
        }
        /// <summary>
        /// Gets the transient value for the window.
        /// </summary>
        /// <param name="window">The name of the window.</param>
        /// <returns>A pointer to the window.</returns>
        internal static IntPtr GetTransientForWindow(IntPtr window)
        {
            IntPtr transientForWindowHandle = IntPtr.Zero;
            using (XServerConnection serverConnection = new XServerConnection())
            {
                int success = XGetTransientForHint(serverConnection.Display, window, out transientForWindowHandle);
                if (success == 0 || transientForWindowHandle == window)
                {
                    transientForWindowHandle = IntPtr.Zero;
                }
            }

            return transientForWindowHandle;
        }
        /// <summary>
        /// Gets the process ID for the window.
        /// </summary>
        /// <param name="windowPointer">The pointer to the window.</param>
        /// <returns>The process ID for the window.</returns>
        internal static int GetProcessIdForWindow(IntPtr windowPointer)
        {
            int procId = 0;
            using (XServerConnection server = new XServerConnection())
            {
                procId = GetProcessId(server.Display, windowPointer);
            }

            return procId;
        }
        /// <summary>
        /// Gets the frame extents for the specified window.
        /// </summary>
        /// <param name="window">A pointer to the window.</param>
        /// <returns>The <see cref="WindowManagerFrameExtents"/> of the window.</returns>
        internal static WindowManagerFrameExtents GetFrameExtents(IntPtr window)
        {
            WindowManagerFrameExtents extents = WindowManagerFrameExtents.Empty;
            using (XServerConnection serverConnection = new XServerConnection())
            {
                IntPtr prop_return = GetWindowProperty(serverConnection.Display, window, PropertyWindowFrameExtents, 0, 4 * 32);
                if (prop_return != IntPtr.Zero)
                {
                    int[] returnedExtents = new int[4] { 0, 0, 0, 0 };
                    Marshal.Copy(prop_return, returnedExtents, 0, 4);
                    extents = new WindowManagerFrameExtents();
                    extents.Left = returnedExtents[0];
                    extents.Right = returnedExtents[1];
                    extents.Top = returnedExtents[2];
                    extents.Bottom = returnedExtents[3];
                    XFree(prop_return);
                }
            }

            return extents;
        }
        /// <summary>
        /// Gets all of the top-level windows for the specified process ID.
        /// </summary>
        /// <param name="processId">The ID of the process.</param>
        /// <returns>A list of pointers to the process's top-level windows.</returns>
        internal static List<IntPtr> FindTopLevelWindowsForProcess(int processId)
        {
            List<IntPtr> windowList = new List<IntPtr>();
            using (XServerConnection serverConnection = new XServerConnection())
            {
                IntPtr rootWindow = GetRootWindow(serverConnection.Display);
                windowList = GetProcessTopLevelWindows(serverConnection.Display, rootWindow, processId);
            }

            return windowList;
        }
 /// <summary>
 /// Closes the specified window.
 /// </summary>
 /// <param name="window">The pointer to the window.</param>
 internal static void CloseWindow(IntPtr window)
 {
     using (XServerConnection serverConnection = new XServerConnection())
     {
         SendClientMessage(serverConnection.Display, window, MessageCloseWindow, new int[5] { 0, 0, 0, 0, 0 });
         XFlush(serverConnection.Display);
     }
 }