>
> On 30 Oct 2012, at 09:52, leowang wrote:
>
> > Dear All,
> > I have two process named A and B, and each has a window. I run process =
> A first, then run process B. Now only window B can receive key events, =
> then I called XMapRaised in process B to set Window A to X stack top, =
> after that only window A can receive key events, then I called =
> XMapRaised in process A to set Window B to X stack top, the Window B can =
> receive key events.
>
> Can you post a minimal, complete, compilable example that manifests the =
> failure?
>
> I can't tell from your description what it is you are doing, and hence =
> why it might fail.
>
> I suspect there is an issue with modality of your windows, but we need =
> to see your code to know.
>
> In particular, I'm curious why you are using XMapRaised to alter the =
> z-order of the windows; that is unnecesary. It should only be necessary =
> to call show() on the window to raise it to the top of the stack, and I =
> wonder whether bypassing fltk's built-in mechanisms and making platform =
> specific calls is breaking things...
>
>
>
>
>

Thanks for your reply, please see the code as below:

Process A:

#include <stdlib.h>
#include <stdio.h>
#include <X11/Xlib.h>
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>

static char *window_id_format = "0x%lx";
void focusWindow1() {
    unsigned int numkids, i, mapped, scrn;
    Window r, p, *kids;
    Window pm_win, browser_win;
    XWindowAttributes attr;
    Window root;
    char *win_name;
    char *display_name = getenv("DISPLAY");
    Display *dipsy = XOpenDisplay(display_name);
    if (dipsy == NULL) {
      printf("Can't connect to X server %s\n",display_name);
      exit(1);
    }

        scrn = DefaultScreen (dipsy);
        root = RootWindow (dipsy, scrn);
        mapped = 0;
        XQueryTree (dipsy, root, &r, &p, &kids, &numkids);
        for (i = 0; i < numkids; ++i)
        {
                XGetWindowAttributes (dipsy, kids[i], &attr);
                if (attr.map_state == IsViewable)
                {
                        ++mapped;
                        printf (window_id_format, kids[i]);
                        if (!XFetchName (dipsy, kids[i], &win_name))
                        {            /* Get window name if any */
                                printf (" (has no name)\n");
                                printf (" ");
                        }
                        else if (win_name)
                        {
                                if(strcasecmp(win_name, "browser window") == 0) 
{
                                    browser_win = kids[i];
                                }
                                XFree (win_name);
                        }
                }
        }

    XMapRaised(dipsy, browser_win);
}

class CPmWindow : public Fl_Double_Window {
public:
    CPmWindow(int w, int h);
    ~CPmWindow() {}

        int handle(int );
};

CPmWindow::CPmWindow(int w, int h) : Fl_Double_Window(w, h) {
        focus(this);
}

int CPmWindow::handle(int event) {
        switch(event) {
                case FL_KEYBOARD:
                {
                        switch(Fl::event_key())
                        {
                                case FL_Up:
                                        printf("test window key up\n");
                                        break;
                                case FL_Left:
                                        focusWindow1();
                                        break;
                                default:
                                        break;
                        }
                }
                default:
                        break;
        }
        return Fl_Double_Window::handle(event);
}

int main(int argc, char ** argv) {
  CPmWindow *window = new CPmWindow(1280,800);
  window->show();
  return Fl::run();
}


Process B:

#include <X11/Xlib.h>
#include <X11/keysym.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Window createWindow(Display* display, int width, int height, int x, int y)
{
 int screen_num = DefaultScreen(display);
 int win_border_width = 0;
 Window win;
 win = XCreateSimpleWindow(display, RootWindow(display, screen_num),
       x, y, width, height, win_border_width,
 BlackPixel(display, screen_num),
 WhitePixel(display, screen_num));
 /* make the window actually appear on the screen. */
 XMapWindow(display, win);
 /* flush all pending requests to the X server. */
 XFlush(display);
 return win;
}

GC createGC(Display* display, Window win, int reverse_video)
{
 int screen_num = DefaultScreen(display);
 unsigned long valuemask = 0;
 XGCValues values;
 GC gc = XCreateGC(display, win, valuemask, &values);
 if (gc < 0){
  fprintf(stderr, "XCreateGC: \n");
 }


 /* allocate foreground and background colors for this GC. */
 if (reverse_video) {
  XSetForeground(display, gc, WhitePixel(display, screen_num));
  XSetBackground(display, gc, BlackPixel(display, screen_num));
 }
 else{
  XSetForeground(display, gc, BlackPixel(display, screen_num));
  XSetBackground(display, gc, WhitePixel(display, screen_num));
 }


 unsigned int line_width = 1;          /* line width for the GC.       */
 int line_style = LineSolid;           /* style for lines drawing and  */
 int cap_style = CapButt;              /* style of the line's edje and */
 int join_style = JoinBevel;           /*  joined lines.               */
 /* define the style of lines that will be drawn using this GC. */
 XSetLineAttributes(display, gc,line_width, line_style, cap_style, join_style); 
                                                                /* define the 
fill style for the GC. to be 'solid filling'. */
 XSetFillStyle(display, gc, FillSolid);
 return gc;
}

static char *window_id_format = "0x%lx";
void focusWindow2(Display *dipsy) {
        unsigned int numkids, i, mapped, scrn;
        Window r, p, *kids;
        Window pm_win, browser_win;
        XWindowAttributes attr;
        Window root;
        char *win_name;
        scrn = DefaultScreen (dipsy);
        root = RootWindow (dipsy, scrn);
        mapped = 0;
        XQueryTree (dipsy, root, &r, &p, &kids, &numkids);
        for (i = 0; i < numkids; ++i)
        {
                XGetWindowAttributes (dipsy, kids[i], &attr);
                if (attr.map_state == IsViewable)
                {
                        ++mapped;
                        printf (window_id_format, kids[i]);
                        if (!XFetchName (dipsy, kids[i], &win_name))
                        {            /* Get window name if any */
                                printf (" (has no name)\n");
                                printf (" ");
                        }
                        else if (win_name)
                        {
                                                                                
                  if(strlen(win_name) == 0) {
                              pm_win = kids[i];
                          }
                          XFree (win_name);
                        }
                }
        }

        XMapRaised(dipsy, pm_win);
}

int main(int argc, char* argv[])
{
char *display_name = getenv("DISPLAY");
 Display *display = XOpenDisplay(display_name);
 if (display == NULL) {
  printf("Can't connect to X server %s\n",display_name);
  exit(1);
 }
 int screen_num = DefaultScreen(display);
 unsigned int display_width, display_height;
 display_width = DisplayWidth(display, screen_num);
 display_height = DisplayHeight(display, screen_num);
 int win_width = display_width;
 int win_height = display_height;
 Window win = createWindow(display, win_width, win_height, 0, 0);
 XStoreName(display, win, "browser window");
 /* allocate two new GCs (graphics contexts) for drawing in the window. */
 GC gc = createGC(display, win, 0);
 Atom wmDelete=XInternAtom(display, "WM_DELETE_WINDOW", True);
 XSetWMProtocols(display, win, &wmDelete, 1);
 /* subscribe to the given set of event types. */
 XSelectInput(display, win, ExposureMask | KeyPressMask |
  ButtonPressMask | ButtonMotionMask|
  ButtonReleaseMask|StructureNotifyMask);
 /* perform an events loop */
 int done = 0;
XEvent an_event;
  int x, y;

while (!done){
  XNextEvent(display, &an_event);
  switch (an_event.type) {
   case KeyPress:{
    x = an_event.xkey.x;
    y = an_event.xkey.y;
        printf("an_event.xkey.keycode = %d\n", an_event.xkey.keycode);
    /* translate the key code to a key symbol. */
    KeySym key_symbol = XKeycodeToKeysym(display, an_event.xkey.keycode, 0);
    switch (key_symbol) {
     case XK_q:
      break;
         case XK_Return:
          printf("browser win press OK key\n");
          break;
         case XK_Down:
          printf("browser win press down key\n");
          focusWindow2(display);
      break;
     case XK_Up:
          printf("browser win press up key\n");
          break;
     case XK_Tab:
          printf("browser win press Tab Key\n");
      break;
     case XK_BackSpace:
          printf("browser win press Backspace key\n");
          break;
     default:
      break;
    }
    break;
   }
   default:
    break;
  }
 }
 XFreeGC(display, gc);
 XCloseDisplay(display);

 return 0;
}
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to