reparenting X windows - Xwindows

This is a discussion on reparenting X windows - Xwindows ; Hi I'm trying to write a very simple X-windows application and I've run into a problem with XReparentWindow(). For the moment, all I want is the following: - when an xterm (or some other application) is started, a new window ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: reparenting X windows

  1. reparenting X windows

    Hi

    I'm trying to write a very simple X-windows application and I've run into
    a problem with XReparentWindow(). For the moment, all I want is the following:

    - when an xterm (or some other application) is started, a new window must
    be created (XCreateWindow()) and the xterm must be reparented to that
    new window. (note: I start the xterm manually with
    xterm -display :1 -geometry +100+100)

    - Ideally, the xterm window should be properly resized to fit the newly
    created window.

    I'm at a point where I can intercept the xterm window being created. I
    create my own window and call XReparentWindow(). The result is:

    - If the SubstructureRedirectEvent mask is set in my new window, I see the
    new window but not the xterm window
    - If the SubstructureRedirectEvent mask is not set, then the xterm is visible,
    but not necessarily in the correct position. eg. I say
    XReparentWindow(dpy, w, parent, 0, 0) but the reparented window does not
    move to position 0, 0 relative to the parent.


    I'm brand new to this, so any and all help will be appreciated. I've been
    going through the Xlib docs but so far I've had no joy. I've included the
    code for the program below. It's only the beginning and very incomplete,
    but for now, all I need is what is stated above. I attempt the reparent
    in the handleCreateNotify() function.

    Thanks in advance
    kodgehopper

    /*******************Main.h************************/

    #ifndef _MAIN_H
    #define _MAIN_H
    #include
    #include
    #include
    #include
    #include
    #include

    using namespace std;

    class Main
    {
    public:
    Main();
    ~Main();
    int processEvents();

    protected:
    Display *dpy;
    Window root;
    int screen;

    private:
    void init();
    void handleCreateNotify(XEvent *);
    void handleReparentNotify(XEvent *);

    Window leftWin;
    };

    #endif

    /************************Main.cc******************* ****/
    #include "Main.h"

    Main::Main()
    {
    dpy = NULL;
    init();
    }

    Main::~Main()
    {
    }

    void Main::init()
    {
    dpy=XOpenDisplay(":1");
    if (!dpy)
    {
    cerr << "Failed to open display" << endl;
    exit(-1);
    }

    screen=DefaultScreen(dpy);
    root=RootWindow(dpy, screen);

    XSelectInput(dpy, root, ButtonPressMask|VisibilityChangeMask|ExposureMask|
    StructureNotifyMask|EnterWindowMask|LeaveWindowMas k|
    SubstructureNotifyMask);
    XMapWindow(dpy, root);
    XFlush(dpy);
    }

    void Main::handleCreateNotify(XEvent *ev)
    {
    Window newWindow;

    XCreateWindowEvent e = ev->xcreatewindow;
    newWindow=e.window;

    if (newWindow == leftWin)
    {
    return;
    }

    XSelectInput(dpy, newWindow, StructureNotifyMask);

    /* create our own window of size 300x300 */
    XSetWindowAttributes attr;

    /* WITH THIS STATEMENT, I SEE A BLACK BOX BUT NO XTERM WINDOW */
    attr.event_mask=SubstructureNotifyMask|Substructur eRedirectMask;

    /* IF I USE THE STATEMENT BELOW INSTEAD, I SEE A BLACK BOX, WITH AN
    INCORRECTLY PLACED XTERM WINDOW */
    //attr.event_mask=SubstructureNotifyMask;
    leftWin = XCreateWindow(dpy, root, 0, 0, 300, 300, 0, CopyFromParent,
    InputOutput, CopyFromParent, CWEventMask, &attr);
    XSetWindowBackground(dpy, leftWin, 0);

    /* reparent */
    XReparentWindow(dpy, newWindow, leftWin, 0, 0);
    XMapWindow(dpy, leftWin);
    }

    void Main::handleReparentNotify(XEvent *ev)
    {
    // XReparentEvent e=ev->xreparent;
    // Window currWindow = e.window;
    }

    int Main:rocessEvents()
    {
    while (1)
    {
    XEvent e;

    XNextEvent(dpy, &e);

    switch (e.type)
    {
    case CreateNotify:
    {
    handleCreateNotify(&e);
    }
    break;

    case ReparentNotify:
    {
    handleReparentNotify(&e);
    }
    break;

    case ConfigureRequest:
    {
    cout << "got configure request" << endl;
    }
    break;
    case KeyPress:
    case KeyRelease:
    case ButtonRelease:
    case MotionNotify:
    case ButtonPress:
    {
    cout << "Other Event" << endl;
    }
    break;
    }
    }
    }

    int main(int argc, char *argv[])
    {
    Main m;

    m.processEvents();
    return 0;
    }

  2. Re: reparenting X windows

    kodgehopper wrote:
    >Hi
    >
    >I'm trying to write a very simple X-windows application and I've run into
    >a problem with XReparentWindow(). For the moment, all I want is the following:
    >
    >- when an xterm (or some other application) is started, a new window must
    > be created (XCreateWindow()) and the xterm must be reparented to that
    > new window. (note: I start the xterm manually with
    > xterm -display :1 -geometry +100+100)
    >
    >- Ideally, the xterm window should be properly resized to fit the newly
    > created window.
    >
    >I'm at a point where I can intercept the xterm window being created. I
    >create my own window and call XReparentWindow(). The result is:
    >
    >- If the SubstructureRedirectEvent mask is set in my new window, I see the
    > new window but not the xterm window
    >- If the SubstructureRedirectEvent mask is not set, then the xterm is visible,
    > but not necessarily in the correct position. eg. I say
    > XReparentWindow(dpy, w, parent, 0, 0) but the reparented window does not
    > move to position 0, 0 relative to the parent.
    >
    >
    >I'm brand new to this, so any and all help will be appreciated. I've been
    >going through the Xlib docs but so far I've had no joy. I've included the
    >code for the program below. It's only the beginning and very incomplete,
    >but for now, all I need is what is stated above. I attempt the reparent
    >in the handleCreateNotify() function.
    >
    >Thanks in advance
    >kodgehopper

    [snip]

    Embedding applications in other applications is somewhat tricky --
    your approach is a bit too simplistic.

    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include

    static void
    EH(Widget w, XtPointer clientdata, XEvent * event, Boolean * cont)
    {
    switch (event->type) {
    case MapRequest:
    XReparentWindow(XtDisplay(w), event->xmaprequest.window,
    XtWindow(w), 0, 0);
    XMapWindow(XtDisplay(w), event->xmaprequest.window);
    break;
    case ConfigureRequest:
    XResizeWindow(XtDisplay(w), event->xmaprequest.window,
    event->xconfigurerequest.width,
    event->xconfigurerequest.height);
    XtResizeWidget(w, event->xconfigurerequest.width,
    event->xconfigurerequest.height, 0);
    break;
    }
    }

    int
    main(int argc, char * argv[])
    {
    XtAppContext ac;
    Display * dpy;
    Widget shell;
    int major, minor;
    XAppGroup appgrp;
    Xauth * auth1;
    Xauth * auth2;
    struct utsname uts;
    FILE * file;
    XSecurityAuthorizationAttributes secattr;
    XSecurityAuthorization secid;

    shell = XtVaOpenApplication(&ac, "Test", 0, 0, &argc, argv, 0,
    applicationShellWidgetClass,
    XtNgeometry, "100x100+100+100",
    XtNinput, True,
    0);
    XtRealizeWidget(shell);
    XtAddRawEventHandler(shell, SubstructureRedirectMask, False, EH, 0);

    dpy = XtDisplay(shell);

    XagQueryVersion(dpy, &major, &minor);
    XSecurityQueryExtension(dpy, &major, &minor);
    XagCreateEmbeddedApplicationGroup(dpy, None, None, 0, 0, &appgrp);

    XtRegisterDrawable(dpy, appgrp, shell);

    auth1 = XSecurityAllocXauth();
    auth1->family = FamilyWild;
    auth1->name = strdup("MIT-MAGIC-COOKIE-1");
    auth1->name_length = strlen(auth1->name);
    auth1->data = 0; auth1->data_length = 0;

    secattr.timeout = 0;
    secattr.trust_level = XSecurityClientUntrusted;
    secattr.group = appgrp;

    uname(&uts);
    auth2 = XSecurityGenerateAuthorization(dpy, auth1,
    XSecurityTimeout|XSecurityTrustLevel|XSecurityGrou p,
    &secattr, &secid);
    auth2->family = FamilyLocal;
    auth2->address = strdup(uts.nodename);
    auth2->address_length = strlen(auth2->address);
    auth2->number = "0";
    auth2->number_length = strlen(auth2->number);

    file = fopen("secfile", "w");
    XauWriteAuth(file, auth2);
    fclose(file);
    XSecurityFreeXauth(auth1); XSecurityFreeXauth(auth2);

    if (fork() == 0) {
    putenv("XAUTHORITY=secfile");
    execlp("xterm", "xterm", 0);
    exit(0);
    }

    XtAppMainLoop(ac);

    return 0;
    }

    And here is the Imakefile:

    CCOPTIONS = -g -I. -Wall -pedantic
    AllTarget(appgroup)
    NormalProgramTarget(appgroup, appgroup.o,,-lXau -lXext -lXt -lX11,)


+ Reply to Thread