1
votes

I am very new to X11 programming.

I tried the following piece of code to display Window Id of the new window created but it is displayed Window Id twice (different Window Id). I am getting correct and once only for every new window created using FVWM (fvwm socket). If I launch firefox from command then it displays window around 5 times.

I want to display correct Window Id once for each new Window creation instead of twice.

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
int main()
{
                Display* display = XOpenDisplay(":0");
                XSetWindowAttributes attributes;
//                attributes.event_mask = SubstructureNotifyMask | StructureNotifyMask;
                attributes.event_mask = SubstructureNotifyMask | VisibilityChangeMask | EnterWindowMask;
                attributes.backing_store = Always;
                Window win = XDefaultRootWindow(display);
                printf("Deafult root window %x\n", win);
                XChangeWindowAttributes(display, win, CWEventMask, &attributes);

                while (1)
                {
                   XEvent event;
                   XNextEvent(display, &event);
                   if (event.type == CreateNotify)
                   {
                         puts("create Notify event occured\n");
                        printf("Window ID: %x\n", event.xcreatewindow.window);
                        printf("Window ID: %d\n", event.xcreatewindow.window);

                        printf("Parent Window ID: %x\n", event.xcreatewindow.parent);
                  }
                }
}

Following is the Output for "xclock".

[root@localhost src]# ./XWindow.out 
create Notify event occured

Window ID: 3e0000c
Window ID: 65011724
create Notify event occured

Window ID: 100118f
Window ID: 16781711
create Notify event occured

Following is to conform same from "xprop" command

[root@localhost src]# xclock &
[2] 6564
[root@localhost src]# xprop 
_NET_WM_STATE(ATOM) = 
WM_STATE(WM_STATE):
        window state: Normal
        icon window: 0x0
_NET_WM_DESKTOP(CARDINAL) = 0
_NET_FRAME_EXTENTS(CARDINAL) = 1, 1, 39, 1
_NET_WM_ALLOWED_ACTIONS(ATOM) = _NET_WM_ACTION_MOVE, _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_MINIMIZE, _NET_WM_ACTION_SHADE, _NET_WM_ACTION_MAXIMIZE_HORZ, _NET_WM_ACTION_MAXIMIZE_VERT, _NET_WM_ACTION_CHANGE_DESKTOP, _NET_WM_ACTION_CLOSE, _NET_WM_ACTION_ABOVE, _NET_WM_ACTION_BELOW
_NET_WM_PID(CARDINAL) = 6564
WM_PROTOCOLS(ATOM): protocols  WM_DELETE_WINDOW
SM_CLIENT_ID(STRING) = "10b83f0cefdd07ca73150167045190887900000031930078"
WM_CLIENT_LEADER(WINDOW): window id # 0x3e0000c
WM_LOCALE_NAME(STRING) = "C"
WM_CLASS(STRING) = "xclock", "XClock"
WM_HINTS(WM_HINTS):
        Client accepts input or input focus: False
        Initial state is Normal State.
        bitmap id # to use for icon: 0x3e00001
        bitmap id # of mask for icon: 0x3e00003
WM_NORMAL_HINTS(WM_SIZE_HINTS):
        program specified size: 164 by 164
        window gravity: NorthWest
WM_CLIENT_MACHINE(STRING) = "localhost.localdomain"
WM_COMMAND(STRING) = { "xclock" }
WM_ICON_NAME(STRING) = "xclock"
WM_NAME(STRING) = "xclock"
[root@localhost src]# 

Do you have any idea about the piece of code which displays Window id whenever new window (e.g. xclock or firefox) is created in Linux only once.

1
There are many more windows than you can see. You will get createnotify events about invisiblle windiws all the time. You need to formulate carefully which windows exactly you want to be notified about. If you want visible top-level windows, you need to register the SubstructureNotify mask and watch for MapNotify events, and then probably get the newly created window attributes and filter out those that are completely outside of the screen. - n. 1.8e9-where's-my-share m.

1 Answers

1
votes

Most window managers re-parent new toplevel windows to manage borders/top bar/buttons. I'm pretty sure what you see is application window id + wm window id. You can check that by looking at window's owner pid - it's in _NET_WM_PID poroperty ( xprop -id wid _NET_WM_PID ) - see How to convert a X11 window id to a process id