2 * Xephyr - A kdrive X server thats runs in a host X window.
3 * Authored by Matthew Allum <mallum@o-hand.com>
5 * Copyright © 2004 Nokia
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of Nokia not be used in
12 * advertising or publicity pertaining to distribution of the software without
13 * specific, written prior permission. Nokia makes no
14 * representations about the suitability of this software for any purpose. It
15 * is provided "as is" without express or implied warranty.
17 * NOKIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL NOKIA BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
23 * PERFORMANCE OF THIS SOFTWARE.
27 #include <kdrive-config.h>
31 * including some server headers (like kdrive-config.h)
32 * might define the macro _XSERVER64
33 * on 64 bits machines. That macro must _NOT_ be defined for Xlib
34 * client code, otherwise bad things happen.
35 * So let's undef that macro if necessary.
47 #include <string.h> /* for memset */
55 #include <X11/Xutil.h>
56 #include <X11/Xatom.h>
57 #include <X11/keysym.h>
58 #include <X11/extensions/XShm.h>
59 #include <X11/extensions/shape.h>
66 extern Bool XF86DRIQueryExtension (Display *dpy,
72 * All xlib calls go here, which gets built as its own .a .
73 * Mixing kdrive and xlib headers causes all sorts of types
77 struct EphyrHostScreen
80 Window win_pre_existing; /* Set via -parent option like xnest */
81 Window peer_win; /* Used for GL; should be at most one */
83 int win_width, win_height;
85 unsigned char *fb_data; /* only used when host bpp != server bpp */
86 XShmSegmentInfo shminfo;
88 void *info; /* Pointer to the screen this is associated with */
89 int mynum; /* Screen number */
94 char *server_dpy_name;
101 Bool use_host_cursor;
106 struct EphyrHostScreen *screens;
108 long damage_debug_msec;
110 unsigned long cmap[256];
113 /* memset ( missing> ) instead of below */
114 /*static EphyrHostXVars HostX = { "?", 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};*/
115 static EphyrHostXVars HostX;
117 static int HostXWantDamageDebug = 0;
119 extern EphyrKeySyms ephyrKeySyms;
121 extern int monitorResolution;
123 char *ephyrResName = NULL;
124 int ephyrResNameFromCmd = 0;
125 char *ephyrTitle = NULL;
128 hostx_set_fullscreen_hint(void);
132 static int trapped_error_code = 0;
133 static int (*old_error_handler) (Display *d, XErrorEvent *e);
135 #define host_depth_matches_server(_vars) (HostX.depth == (_vars)->server_depth)
137 static struct EphyrHostScreen *
138 host_screen_from_screen_info (EphyrScreenInfo *screen)
142 for (i = 0 ; i < HostX.n_screens ; i++)
144 if ( HostX.screens[i].info == screen)
146 return &HostX.screens[i];
153 error_handler(Display *display,
156 trapped_error_code = error->error_code;
161 hostx_errors_trap(void)
163 trapped_error_code = 0;
164 old_error_handler = XSetErrorHandler(error_handler);
168 hostx_errors_untrap(void)
170 XSetErrorHandler(old_error_handler);
171 return trapped_error_code;
175 hostx_want_screen_size (EphyrScreenInfo screen, int *width, int *height )
177 struct EphyrHostScreen *host_screen = host_screen_from_screen_info (screen);
180 (host_screen->win_pre_existing != None ||
181 HostX.use_fullscreen == True))
183 *width = host_screen->win_width;
184 *height = host_screen->win_height;
192 hostx_add_screen (EphyrScreenInfo screen,
193 unsigned long win_id,
196 int index = HostX.n_screens;
198 HostX.n_screens += 1;
199 HostX.screens = realloc (HostX.screens,
200 HostX.n_screens * sizeof(struct EphyrHostScreen));
201 memset (&HostX.screens[index], 0, sizeof (struct EphyrHostScreen));
203 HostX.screens[index].info = screen;
204 HostX.screens[index].win_pre_existing = win_id;
209 hostx_set_display_name (char *name)
211 HostX.server_dpy_name = strdup (name);
215 hostx_set_screen_number(EphyrScreenInfo screen, int number)
217 struct EphyrHostScreen *host_screen = host_screen_from_screen_info (screen);
219 host_screen->mynum = number;
220 hostx_set_win_title (host_screen->info, "") ;
224 hostx_set_win_title (EphyrScreenInfo screen, char *extra_text)
226 struct EphyrHostScreen *host_screen = host_screen_from_screen_info (screen);
232 XStoreName(HostX.dpy, host_screen->win, ephyrTitle);
237 memset (buf, 0, BUF_LEN+1) ;
238 snprintf (buf, BUF_LEN, "Xephyr on %s.%d %s",
239 HostX.server_dpy_name,
241 (extra_text != NULL) ? extra_text : "");
243 XStoreName (HostX.dpy, host_screen->win, buf);
248 hostx_want_host_cursor (void)
250 return HostX.use_host_cursor;
254 hostx_use_host_cursor (void)
256 HostX.use_host_cursor = True;
260 hostx_want_preexisting_window (EphyrScreenInfo screen)
262 struct EphyrHostScreen *host_screen = host_screen_from_screen_info (screen);
264 if (host_screen && host_screen->win_pre_existing)
275 hostx_use_fullscreen (void)
277 HostX.use_fullscreen = True;
281 hostx_want_fullscreen (void)
283 return HostX.use_fullscreen;
287 hostx_set_fullscreen_hint (void)
289 Atom atom_WINDOW_STATE, atom_WINDOW_STATE_FULLSCREEN;
293 = XInternAtom(HostX.dpy, "_NET_WM_STATE", False);
294 atom_WINDOW_STATE_FULLSCREEN
295 = XInternAtom(HostX.dpy, "_NET_WM_STATE_FULLSCREEN",False);
297 for (index = 0 ; index < HostX.n_screens ; index++)
299 XChangeProperty (HostX.dpy, HostX.screens[index].win,
300 atom_WINDOW_STATE, XA_ATOM, 32,
302 (unsigned char *)&atom_WINDOW_STATE_FULLSCREEN, 1);
308 hostx_toggle_damage_debug (void)
310 HostXWantDamageDebug ^= 1;
314 hostx_handle_signal (int signum)
316 hostx_toggle_damage_debug();
317 EPHYR_DBG ("Signal caught. Damage Debug:%i\n",
318 HostXWantDamageDebug);
322 hostx_use_resname (char *name, int fromcmd)
325 ephyrResNameFromCmd = fromcmd;
329 hostx_set_title (char *title)
337 XSetWindowAttributes attr;
343 XClassHint *class_hint;
355 if ((HostX.dpy = XOpenDisplay(getenv("DISPLAY"))) == NULL)
357 fprintf(stderr, "\nXephyr cannot open host display. Is DISPLAY set?\n");
361 HostX.screen = DefaultScreen(HostX.dpy);
362 HostX.winroot = RootWindow(HostX.dpy, HostX.screen);
363 HostX.gc = XCreateGC(HostX.dpy, HostX.winroot, 0, NULL);
364 HostX.depth = DefaultDepth(HostX.dpy, HostX.screen);
365 HostX.visual = DefaultVisual(HostX.dpy, HostX.screen);
367 class_hint = XAllocClassHint();
369 for (index = 0 ; index < HostX.n_screens ; index++)
371 struct EphyrHostScreen *host_screen = &HostX.screens[index];
373 host_screen->server_depth = HostX.depth;
374 if (host_screen->win_pre_existing != None)
377 XWindowAttributes prewin_attr;
379 /* Get screen size from existing window */
383 result = XGetWindowAttributes (HostX.dpy,
384 host_screen->win_pre_existing,
388 if (hostx_errors_untrap() || !result)
390 fprintf (stderr, "\nXephyr -parent window' does not exist!\n");
394 host_screen->win_width = prewin_attr.width;
395 host_screen->win_height = prewin_attr.height;
397 host_screen->win = XCreateWindow (HostX.dpy,
398 host_screen->win_pre_existing,
400 host_screen->win_width,
401 host_screen->win_height,
411 host_screen->win = XCreateWindow (HostX.dpy,
413 0,0,100,100, /* will resize */
421 hostx_set_win_title (host_screen->info,
422 "(ctrl+shift grabs mouse and keyboard)");
424 if (HostX.use_fullscreen)
426 host_screen->win_width = DisplayWidth(HostX.dpy, HostX.screen);
427 host_screen->win_height = DisplayHeight(HostX.dpy, HostX.screen);
429 hostx_set_fullscreen_hint();
434 tmpstr = getenv("RESOURCE_NAME");
435 if (tmpstr && (!ephyrResNameFromCmd))
436 ephyrResName = tmpstr;
437 class_hint->res_name = ephyrResName;
438 class_hint->res_class = "Xephyr";
439 XSetClassHint(hostx_get_display(), host_screen->win, class_hint);
449 XParseColor (HostX.dpy, DefaultColormap (HostX.dpy,HostX.screen),
451 XAllocColor (HostX.dpy, DefaultColormap (HostX.dpy, HostX.screen),
453 XSetForeground (HostX.dpy, HostX.gc, col.pixel);
455 if (!hostx_want_host_cursor ())
457 /* Ditch the cursor, we provide our 'own' */
458 cursor_pxm = XCreatePixmap (HostX.dpy, HostX.winroot, 1, 1, 1);
459 memset (&col, 0, sizeof (col));
460 empty_cursor = XCreatePixmapCursor (HostX.dpy,
461 cursor_pxm, cursor_pxm,
463 for ( index = 0 ; index < HostX.n_screens ; index++ )
465 XDefineCursor (HostX.dpy,
466 HostX.screens[index].win,
469 XFreePixmap (HostX.dpy, cursor_pxm);
472 for (index = 0 ; index < HostX.n_screens ; index++)
474 HostX.screens[index].ximg = NULL;
476 /* Try to get share memory ximages for a little bit more speed */
478 if (!XShmQueryExtension(HostX.dpy) || getenv("XEPHYR_NO_SHM"))
480 fprintf(stderr, "\nXephyr unable to use SHM XImages\n");
481 HostX.have_shm = False;
485 /* Really really check we have shm - better way ?*/
486 XShmSegmentInfo shminfo;
488 HostX.have_shm = True;
490 shminfo.shmid=shmget(IPC_PRIVATE, 1, IPC_CREAT|0777);
491 shminfo.shmaddr=shmat(shminfo.shmid,0,0);
492 shminfo.readOnly=True;
496 XShmAttach(HostX.dpy, &shminfo);
497 XSync(HostX.dpy, False);
499 if (hostx_errors_untrap())
501 fprintf(stderr, "\nXephyr unable to use SHM XImages\n");
502 HostX.have_shm = False;
505 shmdt(shminfo.shmaddr);
506 shmctl(shminfo.shmid, IPC_RMID, 0);
511 /* Setup the pause time between paints when debugging updates */
513 HostX.damage_debug_msec = 20000; /* 1/50 th of a second */
515 if (getenv ("XEPHYR_PAUSE"))
517 HostX.damage_debug_msec = strtol (getenv ("XEPHYR_PAUSE"), NULL, 0);
518 EPHYR_DBG ("pause is %li\n", HostX.damage_debug_msec);
525 hostx_get_depth (void)
531 hostx_get_server_depth (EphyrScreenInfo screen)
533 struct EphyrHostScreen *host_screen = host_screen_from_screen_info (screen);
535 return host_screen ? host_screen->server_depth : 0;
539 hostx_set_server_depth (EphyrScreenInfo screen, int depth)
541 struct EphyrHostScreen *host_screen = host_screen_from_screen_info (screen);
544 host_screen->server_depth = depth;
548 hostx_get_bpp (EphyrScreenInfo screen)
550 struct EphyrHostScreen *host_screen = host_screen_from_screen_info (screen);
555 if (host_depth_matches_server (host_screen))
556 return HostX.visual->bits_per_rgb;
558 return host_screen->server_depth; /*XXX correct ?*/
562 hostx_get_visual_masks (EphyrScreenInfo screen,
567 struct EphyrHostScreen *host_screen = host_screen_from_screen_info (screen);
572 if (host_depth_matches_server(host_screen))
574 *rmsk = HostX.visual->red_mask;
575 *gmsk = HostX.visual->green_mask;
576 *bmsk = HostX.visual->blue_mask;
578 else if (host_screen->server_depth == 16)
580 /* Assume 16bpp 565 */
594 hostx_calculate_color_shift(unsigned long mask)
597 /* count # of bits in mask */
598 while ((mask = (mask >> 1))) shift++;
599 /* cmap entry is an unsigned char so adjust it by size of that */
600 shift = shift - sizeof(unsigned char) * 8;
601 if (shift < 0) shift = 0;
606 hostx_set_cmap_entry(unsigned char idx,
611 /* need to calculate the shifts for RGB because server could be BGR. */
612 /* XXX Not sure if this is correct for 8 on 16, but this works for 8 on 24.*/
613 static int rshift, bshift, gshift = 0;
614 static int first_time = 1;
617 rshift = hostx_calculate_color_shift(HostX.visual->red_mask);
618 gshift = hostx_calculate_color_shift(HostX.visual->green_mask);
619 bshift = hostx_calculate_color_shift(HostX.visual->blue_mask);
621 HostX.cmap[idx] = ((r << rshift) & HostX.visual->red_mask) |
622 ((g << gshift) & HostX.visual->green_mask) |
623 ((b << bshift) & HostX.visual->blue_mask);
627 * hostx_screen_init creates the XImage that will contain the front buffer of
628 * the ephyr screen, and possibly offscreen memory.
630 * @param width width of the screen
631 * @param height height of the screen
632 * @param buffer_height height of the rectangle to be allocated.
634 * hostx_screen_init() creates an XImage, using MIT-SHM if it's available.
635 * buffer_height can be used to create a larger offscreen buffer, which is used
636 * by fakexa for storing offscreen pixmap data.
639 hostx_screen_init (EphyrScreenInfo screen,
640 int width, int height,
644 Bool shm_success = False;
645 XSizeHints *size_hints;
647 struct EphyrHostScreen *host_screen = host_screen_from_screen_info (screen);
650 fprintf (stderr, "%s: Error in accessing hostx data\n", __func__ );
654 EPHYR_DBG ("host_screen=%p wxh=%dx%d, buffer_height=%d",
655 host_screen, width, height, buffer_height);
657 if (host_screen->ximg != NULL)
659 /* Free up the image data if previously used
660 * i.ie called by server reset
665 XShmDetach(HostX.dpy, &host_screen->shminfo);
666 XDestroyImage (host_screen->ximg);
667 shmdt(host_screen->shminfo.shmaddr);
668 shmctl(host_screen->shminfo.shmid, IPC_RMID, 0);
672 free(host_screen->ximg->data);
673 host_screen->ximg->data = NULL;
675 XDestroyImage(host_screen->ximg);
681 host_screen->ximg = XShmCreateImage (HostX.dpy, HostX.visual, HostX.depth,
682 ZPixmap, NULL, &host_screen->shminfo,
683 width, buffer_height );
685 host_screen->shminfo.shmid =
687 host_screen->ximg->bytes_per_line * buffer_height,
689 host_screen->ximg->data = shmat(host_screen->shminfo.shmid, 0, 0);
690 host_screen->shminfo.shmaddr = host_screen->ximg->data;
692 if (host_screen->ximg->data == (char *)-1)
694 EPHYR_DBG("Can't attach SHM Segment, falling back to plain XImages");
695 HostX.have_shm = False;
696 XDestroyImage(host_screen->ximg);
697 shmctl(host_screen->shminfo.shmid, IPC_RMID, 0);
701 EPHYR_DBG("SHM segment attached %p", host_screen->shminfo.shmaddr);
702 host_screen->shminfo.readOnly = False;
703 XShmAttach(HostX.dpy, &host_screen->shminfo);
710 bitmap_pad = ( HostX.depth > 16 )? 32 : (( HostX.depth > 8 )? 16 : 8 );
712 EPHYR_DBG("Creating image %dx%d for screen host_screen=%p\n",
713 width, buffer_height, host_screen );
714 host_screen->ximg = XCreateImage (HostX.dpy,
723 host_screen->ximg->data =
724 malloc (host_screen->ximg->bytes_per_line * buffer_height);
727 XResizeWindow (HostX.dpy, host_screen->win, width, height);
729 /* Ask the WM to keep our size static */
730 size_hints = XAllocSizeHints();
731 size_hints->max_width = size_hints->min_width = width;
732 size_hints->max_height = size_hints->min_height = height;
733 size_hints->flags = PMinSize|PMaxSize;
734 XSetWMNormalHints(HostX.dpy, host_screen->win, size_hints);
737 XMapWindow(HostX.dpy, host_screen->win);
739 XSync(HostX.dpy, False);
741 host_screen->win_width = width;
742 host_screen->win_height = height;
744 if (host_depth_matches_server(host_screen))
746 EPHYR_DBG("Host matches server");
747 return host_screen->ximg->data;
751 EPHYR_DBG("server bpp %i", host_screen->server_depth>>3);
752 host_screen->fb_data = malloc(width*buffer_height*(host_screen->server_depth>>3));
753 return host_screen->fb_data;
757 static void hostx_paint_debug_rect (struct EphyrHostScreen *host_screen,
759 int width, int height);
762 hostx_paint_rect (EphyrScreenInfo screen,
765 int width, int height)
767 struct EphyrHostScreen *host_screen = host_screen_from_screen_info (screen);
769 EPHYR_DBG ("painting in screen %d\n", host_screen->mynum) ;
772 * Copy the image data updated by the shadow layer
776 if (HostXWantDamageDebug)
778 hostx_paint_debug_rect(host_screen, dx, dy, width, height);
782 * If the depth of the ephyr server is less than that of the host,
783 * the kdrive fb does not point to the ximage data but to a buffer
784 * ( fb_data ), we shift the various bits from this onto the XImage
785 * so they match the host.
787 * Note, This code is pretty new ( and simple ) so may break on
788 * endian issues, 32 bpp host etc.
789 * Not sure if 8bpp case is right either.
790 * ... and it will be slower than the matching depth case.
793 if (!host_depth_matches_server(host_screen))
795 int x,y,idx, bytes_per_pixel = (host_screen->server_depth>>3);
797 unsigned long host_pixel;
799 EPHYR_DBG("Unmatched host depth host_screen=%p\n", host_screen);
800 for (y=sy; y<sy+height; y++)
801 for (x=sx; x<sx+width; x++)
803 idx = (host_screen->win_width*y*bytes_per_pixel)+(x*bytes_per_pixel);
805 switch (host_screen->server_depth)
809 unsigned short pixel = *(unsigned short*)(host_screen->fb_data+idx);
811 r = ((pixel & 0xf800) >> 8);
812 g = ((pixel & 0x07e0) >> 3);
813 b = ((pixel & 0x001f) << 3);
815 host_pixel = (r << 16) | (g << 8) | (b);
817 XPutPixel(host_screen->ximg, x, y, host_pixel);
822 unsigned char pixel = *(unsigned char*)(host_screen->fb_data+idx);
823 XPutPixel(host_screen->ximg, x, y, HostX.cmap[pixel]);
834 XShmPutImage (HostX.dpy, host_screen->win,
835 HostX.gc, host_screen->ximg,
836 sx, sy, dx, dy, width, height, False);
840 XPutImage (HostX.dpy, host_screen->win, HostX.gc, host_screen->ximg,
841 sx, sy, dx, dy, width, height);
844 XSync (HostX.dpy, False);
848 hostx_paint_debug_rect (struct EphyrHostScreen *host_screen,
850 int width, int height)
852 struct timespec tspec;
854 tspec.tv_sec = HostX.damage_debug_msec / (1000000);
855 tspec.tv_nsec = (HostX.damage_debug_msec % 1000000) * 1000;
857 EPHYR_DBG("msec: %li tv_sec %li, tv_msec %li",
858 HostX.damage_debug_msec, tspec.tv_sec, tspec.tv_nsec);
860 /* fprintf(stderr, "Xephyr updating: %i+%i %ix%i\n", x, y, width, height); */
862 XFillRectangle (HostX.dpy, host_screen->win, HostX.gc, x, y, width,height);
863 XSync (HostX.dpy, False);
865 /* nanosleep seems to work better than usleep for me... */
866 nanosleep(&tspec, NULL);
870 hostx_load_keymap(void)
873 int host_width, min_keycode, max_keycode, width;
876 XDisplayKeycodes (HostX.dpy, &min_keycode, &max_keycode);
878 EPHYR_DBG ("min: %d, max: %d", min_keycode, max_keycode);
880 keymap = XGetKeyboardMapping (HostX.dpy,
882 max_keycode - min_keycode + 1,
885 /* Try and copy the hosts keymap into our keymap to avoid loads
888 * kdrive cannot can have more than 4 keysyms per keycode
889 * so we only copy at most the first 4 ( xorg has 6 per keycode, XVNC 2 )
891 width = (host_width > 4) ? 4 : host_width;
893 ephyrKeySyms.map = (CARD32 *)calloc(sizeof(CARD32),
894 (max_keycode - min_keycode + 1) *
896 if (!ephyrKeySyms.map)
899 for (i=0; i<(max_keycode - min_keycode+1); i++)
900 for (j=0; j<width; j++)
901 ephyrKeySyms.map[(i*width)+j] = (CARD32) keymap[(i*host_width) + j];
903 EPHYR_DBG("keymap width, host:%d kdrive:%d", host_width, width);
905 ephyrKeySyms.minKeyCode = min_keycode;
906 ephyrKeySyms.maxKeyCode = max_keycode;
907 ephyrKeySyms.mapWidth = width;
912 static struct EphyrHostScreen *
913 host_screen_from_window (Window w)
916 struct EphyrHostScreen *result = NULL;
918 for (index = 0 ; index < HostX.n_screens ; index++)
920 if (HostX.screens[index].win == w || HostX.screens[index].peer_win == w)
922 result = &HostX.screens[index];
932 hostx_get_event(EphyrHostXEvent *ev)
935 static int grabbed_screen = -1;
937 if (XPending(HostX.dpy))
939 XNextEvent(HostX.dpy, &xev);
944 /* Not so great event compression, but works ok */
945 while (XCheckTypedWindowEvent(HostX.dpy, xev.xexpose.window,
948 struct EphyrHostScreen *host_screen =
949 host_screen_from_window (xev.xexpose.window);
952 hostx_paint_rect (host_screen->info, 0, 0, 0, 0,
953 host_screen->win_width,
954 host_screen->win_height);
958 EPHYR_LOG_ERROR ("failed to get host screen\n");
959 ev->type = EPHYR_EV_EXPOSE;
960 ev->data.expose.window = xev.xexpose.window;
968 struct EphyrHostScreen *host_screen =
969 host_screen_from_window (xev.xmotion.window);
971 ev->type = EPHYR_EV_MOUSE_MOTION;
972 ev->data.mouse_motion.x = xev.xmotion.x;
973 ev->data.mouse_motion.y = xev.xmotion.y;
974 ev->data.mouse_motion.window = xev.xmotion.window;
975 ev->data.mouse_motion.screen = (host_screen ? host_screen->mynum : -1);
980 ev->type = EPHYR_EV_MOUSE_PRESS;
981 ev->key_state = xev.xkey.state;
983 * This is a bit hacky. will break for button 5 ( defined as 0x10 )
984 * Check KD_BUTTON defines in kdrive.h
986 ev->data.mouse_down.button_num = 1<<(xev.xbutton.button-1);
990 ev->type = EPHYR_EV_MOUSE_RELEASE;
991 ev->key_state = xev.xkey.state;
992 ev->data.mouse_up.button_num = 1<<(xev.xbutton.button-1);
997 ev->type = EPHYR_EV_KEY_PRESS;
998 ev->key_state = xev.xkey.state;
999 ev->data.key_down.scancode = xev.xkey.keycode;
1004 if ((XKeycodeToKeysym(HostX.dpy,xev.xkey.keycode,0) == XK_Shift_L
1005 || XKeycodeToKeysym(HostX.dpy,xev.xkey.keycode,0) == XK_Shift_R)
1006 && (xev.xkey.state & ControlMask))
1008 struct EphyrHostScreen *host_screen =
1009 host_screen_from_window (xev.xexpose.window);
1011 if (grabbed_screen != -1)
1013 XUngrabKeyboard (HostX.dpy, CurrentTime);
1014 XUngrabPointer (HostX.dpy, CurrentTime);
1015 grabbed_screen = -1;
1016 hostx_set_win_title (host_screen->info,
1017 "(ctrl+shift grabs mouse and keyboard)");
1022 if (XGrabKeyboard (HostX.dpy, host_screen->win, True,
1027 if (XGrabPointer (HostX.dpy, host_screen->win, True,
1031 host_screen->win, None, CurrentTime) == 0)
1033 grabbed_screen = host_screen->mynum;
1036 "(ctrl+shift releases mouse and keyboard)");
1038 else /* Failed pointer grabm ungrab keyboard */
1039 XUngrabKeyboard (HostX.dpy, CurrentTime);
1044 /* Still send the release event even if above has happened
1045 * server will get confused with just an up event.
1046 * Maybe it would be better to just block shift+ctrls getting to
1047 * kdrive all togeather.
1049 ev->type = EPHYR_EV_KEY_RELEASE;
1050 ev->key_state = xev.xkey.state;
1051 ev->data.key_up.scancode = xev.xkey.keycode;
1063 hostx_get_display(void)
1069 hostx_get_window (int a_screen_number)
1071 if (a_screen_number < 0 || a_screen_number >= HostX.n_screens) {
1072 EPHYR_LOG_ERROR ("bad screen number:%d\n", a_screen_number) ;
1075 return HostX.screens[a_screen_number].win ;
1079 hostx_get_window_attributes (int a_window, EphyrHostWindowAttributes *a_attrs)
1081 XWindowAttributes attrs ;
1083 memset (&attrs, 0, sizeof (attrs)) ;
1085 if (!XGetWindowAttributes (hostx_get_display (),
1090 a_attrs->x = attrs.x ;
1091 a_attrs->y = attrs.y ;
1092 a_attrs->width = attrs.width ;
1093 a_attrs->height = attrs.height ;
1095 a_attrs->visualid = attrs.visual->visualid ;
1100 hostx_get_extension_info (const char *a_ext_name,
1101 int *a_major_opcode,
1105 if (!a_ext_name || !a_major_opcode || !a_first_event || !a_first_error)
1107 if (!XQueryExtension (HostX.dpy,
1119 hostx_get_visuals_info (EphyrHostVisualInfo **a_visuals,
1122 Display *dpy=hostx_get_display () ;
1124 XVisualInfo templ, *visuals=NULL;
1125 EphyrHostVisualInfo *host_visuals=NULL ;
1126 int nb_items=0, i=0;
1128 EPHYR_RETURN_VAL_IF_FAIL (a_visuals && a_num_entries && dpy,
1130 EPHYR_LOG ("enter\n") ;
1131 memset (&templ, 0, sizeof (templ)) ;
1132 visuals = XGetVisualInfo (dpy, VisualNoMask, &templ, &nb_items) ;
1134 EPHYR_LOG_ERROR ("host does not advertise any visual\n") ;
1137 EPHYR_LOG ("host advertises %d visuals\n", nb_items) ;
1138 host_visuals = calloc (nb_items, sizeof (EphyrHostVisualInfo)) ;
1139 for (i=0; i<nb_items; i++) {
1140 host_visuals[i].visualid = visuals[i].visualid ;
1141 host_visuals[i].screen = visuals[i].screen ;
1142 host_visuals[i].depth = visuals[i].depth ;
1143 host_visuals[i].class = visuals[i].class ;
1144 host_visuals[i].red_mask = visuals[i].red_mask ;
1145 host_visuals[i].green_mask = visuals[i].green_mask ;
1146 host_visuals[i].blue_mask = visuals[i].blue_mask ;
1147 host_visuals[i].colormap_size = visuals[i].colormap_size ;
1148 host_visuals[i].bits_per_rgb = visuals[i].bits_per_rgb ;
1150 *a_visuals = host_visuals ;
1151 *a_num_entries = nb_items;
1161 host_visuals = NULL;
1162 EPHYR_LOG ("leave\n") ;
1168 hostx_create_window (int a_screen_number,
1169 EphyrBox *a_geometry,
1171 int *a_host_peer /*out parameter*/)
1174 Display *dpy=hostx_get_display () ;
1175 XVisualInfo *visual_info=NULL, visual_info_templ;
1176 int visual_mask=VisualIDMask ;
1178 int nb_visuals=0, winmask=0;
1179 XSetWindowAttributes attrs;
1181 EPHYR_RETURN_VAL_IF_FAIL (dpy && a_geometry, FALSE) ;
1183 EPHYR_LOG ("enter\n") ;
1186 memset (&visual_info, 0, sizeof (visual_info)) ;
1187 visual_info_templ.visualid = a_visual_id ;
1188 visual_info = XGetVisualInfo (dpy, visual_mask,
1192 EPHYR_LOG_ERROR ("argh, could not find a remote visual with id:%d\n",
1196 memset (&attrs, 0, sizeof (attrs)) ;
1197 attrs.colormap = XCreateColormap (dpy,
1199 visual_info->screen),
1200 visual_info->visual,
1202 attrs.event_mask = ButtonPressMask
1208 winmask = CWColormap|CWEventMask;
1210 win = XCreateWindow (dpy, hostx_get_window (a_screen_number),
1211 a_geometry->x, a_geometry->y,
1212 a_geometry->width, a_geometry->height, 0,
1213 visual_info->depth, CopyFromParent,
1214 visual_info->visual, winmask, &attrs) ;
1216 EPHYR_LOG_ERROR ("failed to create peer window\n") ;
1219 if (HostX.screens[a_screen_number].peer_win == None) {
1220 HostX.screens[a_screen_number].peer_win = win;
1222 EPHYR_LOG_ERROR ("multiple peer windows created for same screen\n") ;
1225 XMapWindow (dpy, win) ;
1226 *a_host_peer = win ;
1229 EPHYR_LOG ("leave\n") ;
1234 hostx_destroy_window (int a_win)
1236 Display *dpy=hostx_get_display () ;
1238 EPHYR_RETURN_VAL_IF_FAIL (dpy, FALSE) ;
1239 XDestroyWindow (dpy, a_win) ;
1245 hostx_set_window_geometry (int a_win, EphyrBox *a_geo)
1247 Display *dpy=hostx_get_display ();
1249 EPHYR_RETURN_VAL_IF_FAIL (dpy && a_geo, FALSE) ;
1251 EPHYR_LOG ("enter. x,y,w,h:(%d,%d,%d,%d)\n",
1253 a_geo->width, a_geo->height) ;
1255 XMoveWindow (dpy, a_win, a_geo->x, a_geo->y) ;
1256 XResizeWindow (dpy, a_win, a_geo->width, a_geo->height) ;
1257 EPHYR_LOG ("leave\n") ;
1262 hostx_set_window_bounding_rectangles (int a_window,
1267 Display *dpy=hostx_get_display () ;
1269 XRectangle *rects=NULL ;
1271 EPHYR_RETURN_VAL_IF_FAIL (dpy && a_rects, FALSE) ;
1273 EPHYR_LOG ("enter. num rects:%d\n", a_num_rects) ;
1275 rects = calloc (a_num_rects, sizeof (XRectangle)) ;
1276 for (i=0; i<a_num_rects; i++) {
1277 rects[i].x = a_rects[i].x1 ;
1278 rects[i].y = a_rects[i].y1 ;
1279 rects[i].width = abs (a_rects[i].x2 - a_rects[i].x1);
1280 rects[i].height = abs (a_rects[i].y2 - a_rects[i].y1) ;
1281 EPHYR_LOG ("borders clipped to rect[x:%d,y:%d,w:%d,h:%d]\n",
1282 rects[i].x, rects[i].y,
1283 rects[i].width, rects[i].height) ;
1285 /*this aways returns 1*/
1286 XShapeCombineRectangles (dpy, a_window, ShapeBounding, 0, 0,
1287 rects, a_num_rects, ShapeSet, YXBanded) ;
1292 EPHYR_LOG ("leave\n") ;
1297 hostx_set_window_clipping_rectangles (int a_window,
1302 Display *dpy=hostx_get_display () ;
1304 XRectangle *rects=NULL ;
1306 EPHYR_RETURN_VAL_IF_FAIL (dpy && a_rects, FALSE) ;
1308 EPHYR_LOG ("enter. num rects:%d\n", a_num_rects) ;
1310 rects = calloc (a_num_rects, sizeof (XRectangle)) ;
1311 for (i=0; i<a_num_rects; i++) {
1312 rects[i].x = a_rects[i].x1 ;
1313 rects[i].y = a_rects[i].y1 ;
1314 rects[i].width = abs (a_rects[i].x2 - a_rects[i].x1);
1315 rects[i].height = abs (a_rects[i].y2 - a_rects[i].y1) ;
1316 EPHYR_LOG ("clipped to rect[x:%d,y:%d,w:%d,h:%d]\n",
1317 rects[i].x, rects[i].y,
1318 rects[i].width, rects[i].height) ;
1320 /*this aways returns 1*/
1321 XShapeCombineRectangles (dpy, a_window, ShapeClip, 0, 0,
1322 rects, a_num_rects, ShapeSet, YXBanded) ;
1327 EPHYR_LOG ("leave\n") ;
1332 hostx_has_xshape (void)
1334 int event_base=0, error_base=0 ;
1335 Display *dpy=hostx_get_display () ;
1336 if (!XShapeQueryExtension (dpy,
1351 #define RESOURCE_PEERS_SIZE 1024*10
1352 static ResourcePair resource_peers[RESOURCE_PEERS_SIZE] ;
1356 hostx_allocate_resource_id_peer (int a_local_resource_id,
1357 int *a_remote_resource_id)
1360 ResourcePair *peer=NULL ;
1361 Display *dpy=hostx_get_display ();
1364 * first make sure a resource peer
1365 * does not exist already for
1366 * a_local_resource_id
1368 for (i=0; i<RESOURCE_PEERS_SIZE; i++) {
1369 if (resource_peers[i].is_valid
1370 && resource_peers[i].local_id == a_local_resource_id) {
1371 peer = &resource_peers[i] ;
1376 * find one free peer entry, an feed it with
1379 for (i=0; i<RESOURCE_PEERS_SIZE; i++) {
1380 if (!resource_peers[i].is_valid) {
1381 peer = &resource_peers[i] ;
1386 peer->remote_id = XAllocID (dpy);
1387 peer->local_id = a_local_resource_id ;
1388 peer->is_valid = TRUE ;
1392 *a_remote_resource_id = peer->remote_id ;
1399 hostx_get_resource_id_peer (int a_local_resource_id,
1400 int *a_remote_resource_id)
1403 ResourcePair *peer=NULL ;
1404 for (i=0; i<RESOURCE_PEERS_SIZE; i++) {
1405 if (resource_peers[i].is_valid
1406 && resource_peers[i].local_id == a_local_resource_id) {
1407 peer = &resource_peers[i] ;
1412 *a_remote_resource_id = peer->remote_id ;
1419 hostx_has_dri (void)
1421 int event_base=0, error_base=0 ;
1422 Display *dpy=hostx_get_display () ;
1427 if (!XF86DRIQueryExtension (dpy,
1436 hostx_has_glx (void)
1438 Display *dpy=hostx_get_display () ;
1439 int event_base=0, error_base=0 ;
1441 if (!glXQueryExtension (dpy, &event_base, &error_base)) {
1447 #endif /* XF86DRI */