/[rdesktop]/jpeg/rdesktop/trunk/xwin.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /jpeg/rdesktop/trunk/xwin.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1453 by astrand, Fri Mar 14 07:39:38 2008 UTC revision 1459 by astrand, Wed Mar 26 16:44:55 2008 UTC
# Line 86  static unsigned long g_seamless_focused Line 86  static unsigned long g_seamless_focused
86  static RD_BOOL g_seamless_started = False;      /* Server end is up and running */  static RD_BOOL g_seamless_started = False;      /* Server end is up and running */
87  static RD_BOOL g_seamless_active = False;       /* We are currently in seamless mode */  static RD_BOOL g_seamless_active = False;       /* We are currently in seamless mode */
88  static RD_BOOL g_seamless_hidden = False;       /* Desktop is hidden on server */  static RD_BOOL g_seamless_hidden = False;       /* Desktop is hidden on server */
89    static RD_BOOL g_seamless_broken_restack = False;       /* WM does not properly restack */
90  extern RD_BOOL g_seamless_rdp;  extern RD_BOOL g_seamless_rdp;
91    
92  extern uint32 g_embed_wnd;  extern uint32 g_embed_wnd;
# Line 529  mwm_hide_decorations(Window wnd) Line 530  mwm_hide_decorations(Window wnd)
530    
531  }  }
532    
533    typedef struct _sw_configurenotify_context
534    {
535            Window window;
536            unsigned long serial;
537    } sw_configurenotify_context;
538    
539    /* Predicate procedure for sw_wait_configurenotify */
540    static Bool
541    sw_configurenotify_p(Display * display, XEvent * xevent, XPointer arg)
542    {
543            sw_configurenotify_context *context = (sw_configurenotify_context *) arg;
544            if (xevent->xany.type == ConfigureNotify
545                && xevent->xconfigure.window == context->window
546                && xevent->xany.serial >= context->serial)
547                    return True;
548    
549            return False;
550    }
551    
552    /* Wait for a ConfigureNotify, with a equal or larger serial, on the
553       specified window. The event will be removed from the queue. We
554       could use XMaskEvent(StructureNotifyMask), but we would then risk
555       throwing away crucial events like DestroyNotify.
556    
557       After a ConfigureWindow, according to ICCCM section 4.1.5, we
558       should recieve a ConfigureNotify, either a real or synthetic
559       one. This indicates that the configure has been "completed".
560       However, some WMs such as several versions of Metacity fails to
561       send synthetic events. See bug
562       http://bugzilla.gnome.org/show_bug.cgi?id=322840. We need to use a
563       timeout to avoid a hang. Tk uses the same approach. */
564    static void
565    sw_wait_configurenotify(Window wnd, unsigned long serial)
566    {
567            XEvent xevent;
568            sw_configurenotify_context context;
569            time_t start;
570            RD_BOOL got = False;
571    
572            context.window = wnd;
573            context.serial = serial;
574            start = time(NULL);
575    
576            do
577            {
578                    if (XCheckIfEvent(g_display, &xevent, sw_configurenotify_p, (XPointer) & context))
579                    {
580                            got = True;
581                            break;
582                    }
583                    usleep(100000);
584            }
585            while (time(NULL) - start < 2);
586    
587            if (!got)
588            {
589                    warning("Broken Window Manager: Timeout while waiting for ConfigureNotify\n");
590            }
591    }
592    
593    /* Get the toplevel window, in case of reparenting */
594    static Window
595    sw_get_toplevel(Window wnd)
596    {
597            Window root, parent;
598            Window *child_list;
599            unsigned int num_children;
600    
601            while (1)
602            {
603                    XQueryTree(g_display, wnd, &root, &parent, &child_list, &num_children);
604                    if (root == parent)
605                    {
606                            break;
607                    }
608                    else if (!parent)
609                    {
610                            warning("Internal error: sw_get_toplevel called with root window\n");
611                    }
612    
613                    wnd = parent;
614            }
615    
616            return wnd;
617    }
618    
619    
620    /* Check if wnd is already behind a window wrt stacking order */
621    static RD_BOOL
622    sw_window_is_behind(Window wnd, Window behind)
623    {
624            Window dummy1, dummy2;
625            Window *child_list;
626            unsigned int num_children;
627            unsigned int i;
628            RD_BOOL found_behind = False;
629            RD_BOOL found_wnd = False;
630    
631            wnd = sw_get_toplevel(wnd);
632            behind = sw_get_toplevel(behind);
633    
634            XQueryTree(g_display, RootWindowOfScreen(g_screen), &dummy1, &dummy2, &child_list,
635                       &num_children);
636    
637            for (i = num_children - 1; i >= 0; i--)
638            {
639                    if (child_list[i] == behind)
640                    {
641                            found_behind = True;
642                    }
643                    else if (child_list[i] == wnd)
644                    {
645                            found_wnd = True;
646                            break;
647                    }
648            }
649    
650            if (child_list)
651                    XFree(child_list);
652    
653            if (!found_wnd)
654            {
655                    warning("sw_window_is_behind: Unable to find window 0x%lx\n", wnd);
656    
657                    if (!found_behind)
658                    {
659                            warning("sw_window_is_behind: Unable to find behind window 0x%lx\n",
660                                    behind);
661                    }
662            }
663    
664            return found_behind;
665    }
666    
667    
668    /* Test if the window manager correctly handles window restacking. In
669       particular, we are testing if it's possible to place a window
670       between two other windows. Many WMs such as Metacity can only stack
671       windows on the top or bottom. The window creation should mostly
672       match ui_seamless_create_window. */
673    static void
674    seamless_restack_test()
675    {
676            /* The goal is to have the middle window between top and
677               bottom.  The middle window is initially at the top,
678               though. */
679            Window wnds[3];         /* top, middle and bottom */
680            int i;
681            XEvent xevent;
682            XWindowChanges values;
683            unsigned long restack_serial;
684    
685            for (i = 0; i < 3; i++)
686            {
687                    char name[64];
688                    wnds[i] =
689                            XCreateSimpleWindow(g_display, RootWindowOfScreen(g_screen), 0, 0, 20, 20,
690                                                0, 0, 0);
691                    snprintf(name, sizeof(name), "SeamlessRDP restack test - window %d", i);
692                    XStoreName(g_display, wnds[i], name);
693                    ewmh_set_wm_name(wnds[i], name);
694    
695                    /* Hide decorations. Often this means that no
696                       reparenting will be done, which makes the restack
697                       easier. Besides, we want to mimic our other
698                       seamless windows as much as possible. We must still
699                       handle the case with reparenting, though. */
700                    mwm_hide_decorations(wnds[i]);
701    
702                    /* Prevent windows from appearing in task bar */
703                    XSetTransientForHint(g_display, wnds[i], RootWindowOfScreen(g_screen));
704                    ewmh_set_window_popup(wnds[i]);
705    
706                    /* We need to catch MapNotify/ConfigureNotify */
707                    XSelectInput(g_display, wnds[i], StructureNotifyMask);
708            }
709    
710            /* Map Windows. Currently, we assume that XMapRaised places
711               the window on the top of the stack. Should be fairly safe;
712               the window is configured before it's mapped. */
713            XMapRaised(g_display, wnds[2]); /* bottom */
714            do
715            {
716                    XWindowEvent(g_display, wnds[2], StructureNotifyMask, &xevent);
717            }
718            while (xevent.type != MapNotify);
719            XMapRaised(g_display, wnds[0]); /* top */
720            do
721            {
722                    XWindowEvent(g_display, wnds[0], StructureNotifyMask, &xevent);
723            }
724            while (xevent.type != MapNotify);
725            XMapRaised(g_display, wnds[1]); /* middle */
726            do
727            {
728                    XWindowEvent(g_display, wnds[1], StructureNotifyMask, &xevent);
729            }
730            while (xevent.type != MapNotify);
731    
732            /* The stacking order should now be 1 - 0 - 2 */
733            if (!sw_window_is_behind(wnds[0], wnds[1]) || !sw_window_is_behind(wnds[2], wnds[1]))
734            {
735                    /* Ok, technically a WM is allowed to stack windows arbitrarily, but... */
736                    warning("Broken Window Manager: Unable to test window restacking\n");
737                    g_seamless_broken_restack = True;
738                    for (i = 0; i < 3; i++)
739                            XDestroyWindow(g_display, wnds[i]);
740                    return;
741            }
742    
743            /* Restack, using XReconfigureWMWindow, which should correctly
744               handle reparented windows as well as nonreparenting WMs. */
745            values.stack_mode = Below;
746            values.sibling = wnds[0];
747            restack_serial = XNextRequest(g_display);
748            XReconfigureWMWindow(g_display, wnds[1], DefaultScreen(g_display), CWStackMode | CWSibling,
749                                 &values);
750            sw_wait_configurenotify(wnds[1], restack_serial);
751    
752            /* Now verify that middle is behind top but not behind
753               bottom */
754            if (!sw_window_is_behind(wnds[1], wnds[0]))
755            {
756                    warning("Broken Window Manager: doesn't handle restack (restack request was ignored)\n");
757                    g_seamless_broken_restack = True;
758            }
759            else if (sw_window_is_behind(wnds[1], wnds[2]))
760            {
761                    warning("Broken Window Manager: doesn't handle restack (window was moved to bottom)\n");
762                    g_seamless_broken_restack = True;
763            }
764    
765            /* Destroy windows */
766            for (i = 0; i < 3; i++)
767                    XDestroyWindow(g_display, wnds[i]);
768    }
769    
770  #define SPLITCOLOUR15(colour, rv) \  #define SPLITCOLOUR15(colour, rv) \
771  { \  { \
772          rv.red = ((colour >> 7) & 0xf8) | ((colour >> 12) & 0x7); \          rv.red = ((colour >> 7) & 0xf8) | ((colour >> 12) & 0x7); \
# Line 1536  select_visual(int screen_num) Line 1774  select_visual(int screen_num)
1774  }  }
1775    
1776  static XErrorHandler g_old_error_handler;  static XErrorHandler g_old_error_handler;
1777    static RD_BOOL g_error_expected = False;
1778    
1779    /* Check if the X11 window corresponding to a seamless window with
1780       specified id exists. */
1781    RD_BOOL
1782    sw_window_exists(unsigned long id)
1783    {
1784            seamless_window *sw;
1785            char *name;
1786            Status sts = 0;
1787    
1788            sw = sw_get_window_by_id(id);
1789            if (!sw)
1790                    return False;
1791    
1792            g_error_expected = True;
1793            sts = XFetchName(g_display, sw->wnd, &name);
1794            g_error_expected = False;
1795            if (sts)
1796            {
1797                    XFree(name);
1798            }
1799    
1800            return sts;
1801    }
1802    
1803  static int  static int
1804  error_handler(Display * dpy, XErrorEvent * eev)  error_handler(Display * dpy, XErrorEvent * eev)
1805  {  {
1806          if ((eev->error_code == BadMatch) && (eev->request_code == X_ConfigureWindow))          if (g_error_expected)
         {  
                 fprintf(stderr, "Got \"BadMatch\" when trying to restack windows.\n");  
                 fprintf(stderr,  
                         "This is most likely caused by a broken window manager (commonly KWin).\n");  
1807                  return 0;                  return 0;
         }  
1808    
1809          return g_old_error_handler(dpy, eev);          return g_old_error_handler(dpy, eev);
1810  }  }
# Line 1656  ui_init(void) Line 1914  ui_init(void)
1914          xclip_init();          xclip_init();
1915          ewmh_init();          ewmh_init();
1916          if (g_seamless_rdp)          if (g_seamless_rdp)
1917            {
1918                    seamless_restack_test();
1919                  seamless_init();                  seamless_init();
1920            }
1921    
1922          DEBUG_RDP5(("server bpp %d client bpp %d depth %d\n", g_server_depth, g_bpp, g_depth));          DEBUG_RDP5(("server bpp %d client bpp %d depth %d\n", g_server_depth, g_bpp, g_depth));
1923    
# Line 2138  xwin_process_events(void) Line 2399  xwin_process_events(void)
2399                                  if (!sw)                                  if (!sw)
2400                                          break;                                          break;
2401    
2402                                    /* Menu windows are real X11 windows,
2403                                       with focus. When such a window is
2404                                       destroyed, focus is reverted to the
2405                                       main application window, which
2406                                       would cause us to send FOCUS. This
2407                                       breaks window switching in, say,
2408                                       Seamonkey. We shouldn't need to
2409                                       send FOCUS: Windows should also
2410                                       revert focus to some other window
2411                                       when the menu window is
2412                                       destroyed. So, we only send FOCUS
2413                                       if the previous focus window still
2414                                       exists. */
2415                                  if (sw->id != g_seamless_focused)                                  if (sw->id != g_seamless_focused)
2416                                  {                                  {
2417                                          seamless_send_focus(sw->id, 0);  
2418                                            if (sw_window_exists(g_seamless_focused))
2419                                                    seamless_send_focus(sw->id, 0);
2420                                          g_seamless_focused = sw->id;                                          g_seamless_focused = sw->id;
2421                                  }                                  }
2422                                  break;                                  break;
# Line 3683  void Line 3959  void
3959  ui_seamless_restack_window(unsigned long id, unsigned long behind, unsigned long flags)  ui_seamless_restack_window(unsigned long id, unsigned long behind, unsigned long flags)
3960  {  {
3961          seamless_window *sw;          seamless_window *sw;
3962            XWindowChanges values;
3963            unsigned long restack_serial;
3964    
3965          if (!g_seamless_active)          if (!g_seamless_active)
3966                  return;                  return;
# Line 3697  ui_seamless_restack_window(unsigned long Line 3975  ui_seamless_restack_window(unsigned long
3975          if (behind)          if (behind)
3976          {          {
3977                  seamless_window *sw_behind;                  seamless_window *sw_behind;
                 Window wnds[2];  
3978    
3979                  sw_behind = sw_get_window_by_id(behind);                  sw_behind = sw_get_window_by_id(behind);
3980                  if (!sw_behind)                  if (!sw_behind)
# Line 3706  ui_seamless_restack_window(unsigned long Line 3983  ui_seamless_restack_window(unsigned long
3983                          return;                          return;
3984                  }                  }
3985    
3986                  wnds[1] = sw->wnd;                  if (!g_seamless_broken_restack)
3987                  wnds[0] = sw_behind->wnd;                  {
3988                            values.stack_mode = Below;
3989                  XRestackWindows(g_display, wnds, 2);                          values.sibling = sw_behind->wnd;
3990                            restack_serial = XNextRequest(g_display);
3991                            XReconfigureWMWindow(g_display, sw->wnd, DefaultScreen(g_display),
3992                                                 CWStackMode | CWSibling, &values);
3993                            sw_wait_configurenotify(sw->wnd, restack_serial);
3994                    }
3995          }          }
3996          else          else
3997          {          {
3998                  XRaiseWindow(g_display, sw->wnd);                  values.stack_mode = Above;
3999                    restack_serial = XNextRequest(g_display);
4000                    XReconfigureWMWindow(g_display, sw->wnd, DefaultScreen(g_display), CWStackMode,
4001                                         &values);
4002                    sw_wait_configurenotify(sw->wnd, restack_serial);
4003          }          }
4004    
4005          sw_restack_window(sw, behind);          sw_restack_window(sw, behind);

Legend:
Removed from v.1453  
changed lines
  Added in v.1459

  ViewVC Help
Powered by ViewVC 1.1.26