Ejemplo n.º 1
0
static int X11_DispatchEvent(_THIS)
{
	int posted;
	XEvent xevent;

	XNextEvent(SDL_Display, &xevent);

	posted = 0;
	switch (xevent.type) {

	    /* Gaining mouse coverage? */
	    case EnterNotify: {
#ifdef DEBUG_XEVENTS
printf("EnterNotify! (%d,%d)\n", xevent.xcrossing.x, xevent.xcrossing.y);
if ( xevent.xcrossing.mode == NotifyGrab )
printf("Mode: NotifyGrab\n");
if ( xevent.xcrossing.mode == NotifyUngrab )
printf("Mode: NotifyUngrab\n");
#endif
		if ( (xevent.xcrossing.mode != NotifyGrab) &&
		     (xevent.xcrossing.mode != NotifyUngrab) ) {
			if ( this->input_grab == SDL_GRAB_OFF ) {
				posted = SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
			} else {
				posted = SDL_PrivateMouseMotion(0, 0,
						xevent.xcrossing.x,
						xevent.xcrossing.y);
			}
		}
	    }
	    break;

	    /* Losing mouse coverage? */
	    case LeaveNotify: {
#ifdef DEBUG_XEVENTS
printf("LeaveNotify! (%d,%d)\n", xevent.xcrossing.x, xevent.xcrossing.y);
if ( xevent.xcrossing.mode == NotifyGrab )
printf("Mode: NotifyGrab\n");
if ( xevent.xcrossing.mode == NotifyUngrab )
printf("Mode: NotifyUngrab\n");
#endif
		if ( (xevent.xcrossing.mode != NotifyGrab) &&
		     (xevent.xcrossing.mode != NotifyUngrab) &&
		     (xevent.xcrossing.detail != NotifyInferior) ) {
			if ( this->input_grab == SDL_GRAB_OFF ) {
				posted = SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
			} else {
				posted = SDL_PrivateMouseMotion(0, 0,
						xevent.xcrossing.x,
						xevent.xcrossing.y);
			}
		}
	    }
	    break;

	    /* Gaining input focus? */
	    case FocusIn: {
#ifdef DEBUG_XEVENTS
printf("FocusIn!\n");
#endif
		posted = SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);

		/* Queue entry into fullscreen mode */
		switch_waiting = 0x01 | SDL_FULLSCREEN;
		switch_time = SDL_GetTicks() + 1500;
	    }
	    break;

	    /* Losing input focus? */
	    case FocusOut: {
#ifdef DEBUG_XEVENTS
printf("FocusOut!\n");
#endif
		posted = SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);

		/* Queue leaving fullscreen mode */
		switch_waiting = 0x01;
		switch_time = SDL_GetTicks() + 200;
	    }
	    break;

	    /* Generated upon EnterWindow and FocusIn */
	    case KeymapNotify: {
#ifdef DEBUG_XEVENTS
printf("KeymapNotify!\n");
#endif
		X11_SetKeyboardState(SDL_Display, xevent.xkeymap.key_vector);
	    }
	    break;

	    /* Mouse motion? */
	    case MotionNotify: {
		if ( SDL_VideoSurface ) {
			if ( mouse_relative ) {
				if ( using_dga & DGA_MOUSE ) {
#ifdef DEBUG_MOTION
  printf("DGA motion: %d,%d\n", xevent.xmotion.x_root, xevent.xmotion.y_root);
#endif
					posted = SDL_PrivateMouseMotion(0, 1,
							xevent.xmotion.x_root,
							xevent.xmotion.y_root);
				} else {
					posted = X11_WarpedMotion(this,&xevent);
				}
			} else {
#ifdef DEBUG_MOTION
  printf("X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
#endif
				posted = SDL_PrivateMouseMotion(0, 0,
						xevent.xmotion.x,
						xevent.xmotion.y);
			}
		}
	    }
	    break;

	    /* Mouse button press? */
	    case ButtonPress: {
		posted = SDL_PrivateMouseButton(SDL_PRESSED, 
					xevent.xbutton.button, 0, 0);
	    }
	    break;

	    /* Mouse button release? */
	    case ButtonRelease: {
		posted = SDL_PrivateMouseButton(SDL_RELEASED, 
					xevent.xbutton.button, 0, 0);
	    }
	    break;

	    /* Key press? */
	    case KeyPress: {
		SDL_keysym keysym;

#ifdef DEBUG_XEVENTS
printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
#endif
		posted = SDL_PrivateKeyboard(SDL_PRESSED,
				X11_TranslateKey(SDL_Display, &xevent.xkey,
						 xevent.xkey.keycode,
						 &keysym));
	    }
	    break;

	    /* Key release? */
	    case KeyRelease: {
		SDL_keysym keysym;

#ifdef DEBUG_XEVENTS
printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
#endif
		/* Check to see if this is a repeated key */
		if ( ! X11_KeyRepeat(SDL_Display, &xevent) ) {
			posted = SDL_PrivateKeyboard(SDL_RELEASED, 
				X11_TranslateKey(SDL_Display, &xevent.xkey,
						 xevent.xkey.keycode,
						 &keysym));
		}
	    }
	    break;

	    /* Have we been iconified? */
	    case UnmapNotify: {
#ifdef DEBUG_XEVENTS
printf("UnmapNotify!\n");
#endif
		/* If we're active, make ourselves inactive */
		if ( SDL_GetAppState() & SDL_APPACTIVE ) {
			/* Swap out the gamma before we go inactive */
			X11_SwapVidModeGamma(this);

			/* Send an internal deactivate event */
			posted = SDL_PrivateAppActive(0,
					SDL_APPACTIVE|SDL_APPINPUTFOCUS);
		}
	    }
	    break;

	    /* Have we been restored? */
	    case MapNotify: {
#ifdef DEBUG_XEVENTS
printf("MapNotify!\n");
#endif
		/* If we're not active, make ourselves active */
		if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
			/* Send an internal activate event */
			posted = SDL_PrivateAppActive(1, SDL_APPACTIVE);

			/* Now that we're active, swap the gamma back */
			X11_SwapVidModeGamma(this);
		}

		if ( SDL_VideoSurface &&
		     (SDL_VideoSurface->flags & SDL_FULLSCREEN) ) {
			X11_EnterFullScreen(this);
		} else {
			X11_GrabInputNoLock(this, this->input_grab);
		}
		X11_CheckMouseModeNoLock(this);

		if ( SDL_VideoSurface ) {
			X11_RefreshDisplay(this);
		}
	    }
	    break;

	    /* Have we been resized or moved? */
	    case ConfigureNotify: {
#ifdef DEBUG_XEVENTS
printf("ConfigureNotify! (resize: %dx%d)\n", xevent.xconfigure.width, xevent.xconfigure.height);
#endif
		if ( SDL_VideoSurface ) {
		    if ((xevent.xconfigure.width != SDL_VideoSurface->w) ||
		        (xevent.xconfigure.height != SDL_VideoSurface->h)) {
			/* FIXME: Find a better fix for the bug with KDE 1.2 */
			if ( ! ((xevent.xconfigure.width == 32) &&
			        (xevent.xconfigure.height == 32)) ) {
				SDL_PrivateResize(xevent.xconfigure.width,
				                  xevent.xconfigure.height);
			}
		    } else {
			/* OpenGL windows need to know about the change */
			if ( SDL_VideoSurface->flags & (SDL_OPENGL | SDL_OPENGLES) ) {
				SDL_PrivateExpose();
			}
		    }
		}
	    }
	    break;

	    /* Have we been requested to quit (or another client message?) */
	    case ClientMessage: {
		if ( (xevent.xclient.format == 32) &&
		     (xevent.xclient.data.l[0] == WM_DELETE_WINDOW) )
		{
			posted = SDL_PrivateQuit();
		} else
		if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
			SDL_SysWMmsg wmmsg;

			SDL_VERSION(&wmmsg.version);
			wmmsg.subsystem = SDL_SYSWM_X11;
			wmmsg.event.xevent = xevent;
			posted = SDL_PrivateSysWMEvent(&wmmsg);
		}
	    }
	    break;

	    /* Do we need to refresh ourselves? */
	    case Expose: {
#ifdef DEBUG_XEVENTS
printf("Expose (count = %d)\n", xevent.xexpose.count);
#endif
		if ( SDL_VideoSurface && (xevent.xexpose.count == 0) ) {
			X11_RefreshDisplay(this);
		}
	    }
	    break;

	    default: {
#ifdef DEBUG_XEVENTS
printf("Unhandled event %d\n", xevent.type);
#endif
		/* Only post the event if we're watching for it */
		if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
			SDL_SysWMmsg wmmsg;

			SDL_VERSION(&wmmsg.version);
			wmmsg.subsystem = SDL_SYSWM_X11;
			wmmsg.event.xevent = xevent;
			posted = SDL_PrivateSysWMEvent(&wmmsg);
		}
	    }
	    break;
	}
	return(posted);
}
Ejemplo n.º 2
0
SDL_Overlay *X11_CreateYUVOverlay(_THIS, int width, int height, Uint32 format, SDL_Surface *display)
{
	SDL_Overlay *overlay;
	struct private_yuvhwdata *hwdata;
	int xv_port;
	unsigned int i, j, k;
	unsigned int adaptors;
	SDL_NAME(XvAdaptorInfo) *ainfo;
	int bpp;
#ifndef NO_SHARED_MEMORY
	XShmSegmentInfo *yuvshm;
#endif
#ifdef INTEL_XV_BADALLOC_WORKAROUND
	int intel_adapter = False;
#endif

	
	xv_port = -1;
	if ( (Success == SDL_NAME(XvQueryExtension)(GFX_Display, &j, &j, &j, &j, &j)) &&
	     (Success == SDL_NAME(XvQueryAdaptors)(GFX_Display,
	                                 RootWindow(GFX_Display, SDL_Screen),
	                                 &adaptors, &ainfo)) ) {
#ifdef USE_LAST_ADAPTOR
		for ( i=0; i < adaptors; ++i )
#else
		for ( i=0; (i < adaptors) && (xv_port == -1); ++i )
#endif 
		{
			
			if ( BUGGY_XFREE86(<=, 4001) ) {
				int visual_ok = 0;
				for ( j=0; j<ainfo[i].num_formats; ++j ) {
					if ( ainfo[i].formats[j].visual_id ==
							SDL_Visual->visualid ) {
						visual_ok = 1;
						break;
					}
				}
				if ( ! visual_ok ) {
					continue;
				}
			}
#ifdef INTEL_XV_BADALLOC_WORKAROUND
			if ( !strcmp(ainfo[i].name, "Intel(R) Video Overla"))
				intel_adapter = True;
			else
				intel_adapter = False;
#endif
			if ( (ainfo[i].type & XvInputMask) &&
			     (ainfo[i].type & XvImageMask) ) {
				int num_formats;
				SDL_NAME(XvImageFormatValues) *formats;
				formats = SDL_NAME(XvListImageFormats)(GFX_Display,
				              ainfo[i].base_id, &num_formats);
#ifdef USE_LAST_ADAPTOR
				for ( j=0; j < num_formats; ++j )
#else
				for ( j=0; (j < num_formats) && (xv_port == -1); ++j )
#endif 
				{
					if ( (Uint32)formats[j].id == format ) {
						for ( k=0; k < ainfo[i].num_ports; ++k ) {
							if ( Success == SDL_NAME(XvGrabPort)(GFX_Display, ainfo[i].base_id+k, CurrentTime) ) {
								xv_port = ainfo[i].base_id+k;
								break;
							}
						}
					}
				}
				if ( formats ) {
					XFree(formats);
				}
			}
		}
		SDL_NAME(XvFreeAdaptorInfo)(ainfo);
	}

	
	switch (format) {
	    
	    case SDL_YUY2_OVERLAY:
	    case SDL_UYVY_OVERLAY:
	    case SDL_YVYU_OVERLAY:
		bpp = 2;
		break;
	    default:
		bpp = 1;
		break;
	}

#if 0
 	for ( i=0; i < adaptors; ++i ) {
 	  if (ainfo[i].name != NULL) Xfree(ainfo[i].name);
 	  if (ainfo[i].formats != NULL) Xfree(ainfo[i].formats);
   	}
 	Xfree(ainfo);
#endif

	if ( xv_port == -1 ) {
		SDL_SetError("No available video ports for requested format");
		return(NULL);
	}

	
	{
		static const char *attr[] = { "XV_AUTOPAINT_COLORKEY", "XV_AUTOPAINT_COLOURKEY" };
		unsigned int i;

		SDL_NAME(XvSelectPortNotify)(GFX_Display, xv_port, True);
		X_handler = XSetErrorHandler(xv_errhandler);
		for ( i=0; i < sizeof(attr)/(sizeof attr[0]); ++i ) {
			Atom a;

			xv_error = False;
			a = XInternAtom(GFX_Display, attr[i], True);
			if ( a != None ) {
     				SDL_NAME(XvSetPortAttribute)(GFX_Display, xv_port, a, 1);
				XSync(GFX_Display, True);
				if ( ! xv_error ) {
					break;
				}
			}
		}
		XSetErrorHandler(X_handler);
		SDL_NAME(XvSelectPortNotify)(GFX_Display, xv_port, False);
	}

	
	overlay = (SDL_Overlay *)SDL_malloc(sizeof *overlay);
	if ( overlay == NULL ) {
		SDL_NAME(XvUngrabPort)(GFX_Display, xv_port, CurrentTime);
		SDL_OutOfMemory();
		return(NULL);
	}
	SDL_memset(overlay, 0, (sizeof *overlay));

	
	overlay->format = format;
	overlay->w = width;
	overlay->h = height;

	
	overlay->hwfuncs = &x11_yuvfuncs;
	overlay->hw_overlay = 1;

	
	hwdata = (struct private_yuvhwdata *)SDL_malloc(sizeof *hwdata);
	overlay->hwdata = hwdata;
	if ( hwdata == NULL ) {
		SDL_NAME(XvUngrabPort)(GFX_Display, xv_port, CurrentTime);
		SDL_OutOfMemory();
		SDL_FreeYUVOverlay(overlay);
		return(NULL);
	}
	hwdata->port = xv_port;
#ifndef NO_SHARED_MEMORY
	yuvshm = &hwdata->yuvshm;
	SDL_memset(yuvshm, 0, sizeof(*yuvshm));
	hwdata->image = SDL_NAME(XvShmCreateImage)(GFX_Display, xv_port, format,
						   0, width, height, yuvshm);
#ifdef PITCH_WORKAROUND
	if ( hwdata->image != NULL && hwdata->image->pitches[0] != (width*bpp) ) {
		 
		width = hwdata->image->pitches[0] / bpp;
		XFree(hwdata->image);
		hwdata->image = SDL_NAME(XvShmCreateImage)(GFX_Display, xv_port, format,
							   0, width, height, yuvshm);
	}
#endif 
	hwdata->yuv_use_mitshm = (hwdata->image != NULL);
	if ( hwdata->yuv_use_mitshm ) {
		yuvshm->shmid = shmget(IPC_PRIVATE, hwdata->image->data_size,
				       IPC_CREAT | 0777);
		if ( yuvshm->shmid >= 0 ) {
			yuvshm->shmaddr = (char *)shmat(yuvshm->shmid, 0, 0);
			yuvshm->readOnly = False;
			if ( yuvshm->shmaddr != (char *)-1 ) {
				shm_error = False;
				X_handler = XSetErrorHandler(shm_errhandler);
				XShmAttach(GFX_Display, yuvshm);
				XSync(GFX_Display, True);
				XSetErrorHandler(X_handler);
				if ( shm_error )
					shmdt(yuvshm->shmaddr);
			} else {
				shm_error = True;
			}
			shmctl(yuvshm->shmid, IPC_RMID, NULL);
		} else {
			shm_error = True;
		}
		if ( shm_error ) {
			XFree(hwdata->image);
			hwdata->yuv_use_mitshm = 0;
		} else {
			hwdata->image->data = yuvshm->shmaddr;
		}
	}
	if ( !hwdata->yuv_use_mitshm )
#endif 
	{
		hwdata->image = SDL_NAME(XvCreateImage)(GFX_Display, xv_port, format,
							0, width, height);

#ifdef PITCH_WORKAROUND
		if ( hwdata->image != NULL && hwdata->image->pitches[0] != (width*bpp) ) {
			 
			XFree(hwdata->image);
			width = hwdata->image->pitches[0] / bpp;
			hwdata->image = SDL_NAME(XvCreateImage)(GFX_Display, xv_port, format,
								0, width, height);
		}
#endif 
		if ( hwdata->image == NULL ) {
			SDL_SetError("Couldn't create XVideo image");
			SDL_FreeYUVOverlay(overlay);
			return(NULL);
		}
		hwdata->image->data = SDL_malloc(hwdata->image->data_size);
		if ( hwdata->image->data == NULL ) {
			SDL_OutOfMemory();
			SDL_FreeYUVOverlay(overlay);
			return(NULL);
		}
	}

	
	overlay->planes = hwdata->image->num_planes;
	overlay->pitches = (Uint16 *)SDL_malloc(overlay->planes * sizeof(Uint16));
	overlay->pixels = (Uint8 **)SDL_malloc(overlay->planes * sizeof(Uint8 *));
	if ( !overlay->pitches || !overlay->pixels ) {
		SDL_OutOfMemory();
		SDL_FreeYUVOverlay(overlay);
		return(NULL);
	}
	for ( i=0; i<overlay->planes; ++i ) {
		overlay->pitches[i] = hwdata->image->pitches[i];
		overlay->pixels[i] = (Uint8 *)hwdata->image->data +
		                              hwdata->image->offsets[i];
	}

#ifdef XFREE86_REFRESH_HACK
	X11_DisableAutoRefresh(this);
#endif

#ifdef INTEL_XV_BADALLOC_WORKAROUND
	if (intel_adapter)
	{
		xv_error = False;
		X_handler = XSetErrorHandler(intel_errhandler);
		
		X11_ClearYUVOverlay(overlay);

#ifndef NO_SHARED_MEMORY
		if ( hwdata->yuv_use_mitshm ) {
			SDL_NAME(XvShmPutImage)(GFX_Display, hwdata->port,
				SDL_Window, SDL_GC,
				hwdata->image,
				0, 0, overlay->w, overlay->h,
				0, 0, 1, 1, False);
		}
		else
#endif
		{
			SDL_NAME(XvPutImage)(GFX_Display, hwdata->port,
				SDL_Window, SDL_GC,
				hwdata->image,
				0, 0, overlay->w, overlay->h,
				0, 0, 1, 1);
		}
		XSync(GFX_Display, False);
		XSetErrorHandler(X_handler);

		if (xv_error)
		{
			X11_FreeYUVOverlay(this, overlay);
			return NULL;
		}
		
		X11_RefreshDisplay(this);
	}
#endif

	
	return(overlay);
}
Ejemplo n.º 3
0
SDL_Overlay *X11_CreateYUVOverlay(_THIS, int width, int height, Uint32 format, SDL_Surface *display)
{
	SDL_Overlay *overlay;
	struct private_yuvhwdata *hwdata;
	int xv_port;
	unsigned int i, j, k;
	unsigned int adaptors;
	SDL_NAME(XvAdaptorInfo) *ainfo;
	int bpp;
#ifndef NO_SHARED_MEMORY
	XShmSegmentInfo *yuvshm;
#endif
#ifdef INTEL_XV_BADALLOC_WORKAROUND
	int intel_adapter = False;
#endif

	/* Look for the XVideo extension with a valid port for this format */
	xv_port = -1;
	if ( (Success == SDL_NAME(XvQueryExtension)(GFX_Display, &j, &j, &j, &j, &j)) &&
	     (Success == SDL_NAME(XvQueryAdaptors)(GFX_Display,
	                                 RootWindow(GFX_Display, SDL_Screen),
	                                 &adaptors, &ainfo)) ) {
#ifdef USE_LAST_ADAPTOR
		for ( i=0; i < adaptors; ++i )
#else
		for ( i=0; (i < adaptors) && (xv_port == -1); ++i )
#endif /* USE_LAST_ADAPTOR */
		{
			/* Check to see if the visual can be used */
			if ( BUGGY_XFREE86(<=, 4001) ) {
				int visual_ok = 0;
				for ( j=0; j<ainfo[i].num_formats; ++j ) {
					if ( ainfo[i].formats[j].visual_id ==
							SDL_Visual->visualid ) {
						visual_ok = 1;
						break;
					}
				}
				if ( ! visual_ok ) {
					continue;
				}
			}
#ifdef INTEL_XV_BADALLOC_WORKAROUND
			if ( !strcmp(ainfo[i].name, "Intel(R) Video Overla"))
				intel_adapter = True;
			else
				intel_adapter = False;
#endif
			if ( (ainfo[i].type & XvInputMask) &&
			     (ainfo[i].type & XvImageMask) ) {
				int num_formats;
				SDL_NAME(XvImageFormatValues) *formats;
				formats = SDL_NAME(XvListImageFormats)(GFX_Display,
				              ainfo[i].base_id, &num_formats);
#ifdef USE_LAST_ADAPTOR
				for ( j=0; j < num_formats; ++j )
#else
				for ( j=0; (j < num_formats) && (xv_port == -1); ++j )
#endif /* USE_LAST_ADAPTOR */
				{
					if ( (Uint32)formats[j].id == format ) {
						for ( k=0; k < ainfo[i].num_ports; ++k ) {
							if ( Success == SDL_NAME(XvGrabPort)(GFX_Display, ainfo[i].base_id+k, CurrentTime) ) {
								xv_port = ainfo[i].base_id+k;
								break;
							}
						}
					}
				}
				if ( formats ) {
					XFree(formats);
				}
			}
		}
		SDL_NAME(XvFreeAdaptorInfo)(ainfo);
	}

	/* Precalculate the bpp for the pitch workaround below */
	switch (format) {
	    /* Add any other cases we need to support... */
	    case SDL_YUY2_OVERLAY:
	    case SDL_UYVY_OVERLAY:
	    case SDL_YVYU_OVERLAY:
		bpp = 2;
		break;
	    default:
		bpp = 1;
		break;
	}

#if 0
    /*
     * !!! FIXME:
     * "Here are some diffs for X11 and yuv.  Note that the last part 2nd
     *  diff should probably be a new call to XvQueryAdaptorFree with ainfo
     *  and the number of adaptors, instead of the loop through like I did."
     *
     *  ACHTUNG: This is broken! It looks like XvFreeAdaptorInfo does this
     *  for you, so we end up with a double-free. I need to look at this
     *  more closely...  --ryan.
     */
 	for ( i=0; i < adaptors; ++i ) {
 	  if (ainfo[i].name != NULL) Xfree(ainfo[i].name);
 	  if (ainfo[i].formats != NULL) Xfree(ainfo[i].formats);
   	}
 	Xfree(ainfo);
#endif

	if ( xv_port == -1 ) {
		SDL_SetError("No available video ports for requested format");
		return(NULL);
	}

	/* Enable auto-painting of the overlay colorkey */
	{
		static const char *attr[] = { "XV_AUTOPAINT_COLORKEY", "XV_AUTOPAINT_COLOURKEY" };
		unsigned int i;

		SDL_NAME(XvSelectPortNotify)(GFX_Display, xv_port, True);
		X_handler = XSetErrorHandler(xv_errhandler);
		for ( i=0; i < sizeof(attr)/(sizeof attr[0]); ++i ) {
			Atom a;

			xv_error = False;
			a = XInternAtom(GFX_Display, attr[i], True);
			if ( a != None ) {
     				SDL_NAME(XvSetPortAttribute)(GFX_Display, xv_port, a, 1);
				XSync(GFX_Display, True);
				if ( ! xv_error ) {
					break;
				}
			}
		}
		XSetErrorHandler(X_handler);
		SDL_NAME(XvSelectPortNotify)(GFX_Display, xv_port, False);
	}

	/* Create the overlay structure */
	overlay = (SDL_Overlay *)SDL_malloc(sizeof *overlay);
	if ( overlay == NULL ) {
		SDL_NAME(XvUngrabPort)(GFX_Display, xv_port, CurrentTime);
		SDL_OutOfMemory();
		return(NULL);
	}
	SDL_memset(overlay, 0, (sizeof *overlay));

	/* Fill in the basic members */
	overlay->format = format;
	overlay->w = width;
	overlay->h = height;

	/* Set up the YUV surface function structure */
	overlay->hwfuncs = &x11_yuvfuncs;
	overlay->hw_overlay = 1;

	/* Create the pixel data and lookup tables */
	hwdata = (struct private_yuvhwdata *)SDL_malloc(sizeof *hwdata);
	overlay->hwdata = hwdata;
	if ( hwdata == NULL ) {
		SDL_NAME(XvUngrabPort)(GFX_Display, xv_port, CurrentTime);
		SDL_OutOfMemory();
		SDL_FreeYUVOverlay(overlay);
		return(NULL);
	}
	hwdata->port = xv_port;
#ifndef NO_SHARED_MEMORY
	yuvshm = &hwdata->yuvshm;
	SDL_memset(yuvshm, 0, sizeof(*yuvshm));
	hwdata->image = SDL_NAME(XvShmCreateImage)(GFX_Display, xv_port, format,
						   0, width, height, yuvshm);
#ifdef PITCH_WORKAROUND
	if ( hwdata->image != NULL && hwdata->image->pitches[0] != (width*bpp) ) {
		/* Ajust overlay width according to pitch */ 
		XFree(hwdata->image);
		width = hwdata->image->pitches[0] / bpp;
		hwdata->image = SDL_NAME(XvShmCreateImage)(GFX_Display, xv_port, format,
							   0, width, height, yuvshm);
	}
#endif /* PITCH_WORKAROUND */
	hwdata->yuv_use_mitshm = (hwdata->image != NULL);
	if ( hwdata->yuv_use_mitshm ) {
		yuvshm->shmid = shmget(IPC_PRIVATE, hwdata->image->data_size,
				       IPC_CREAT | 0777);
		if ( yuvshm->shmid >= 0 ) {
			yuvshm->shmaddr = (char *)shmat(yuvshm->shmid, 0, 0);
			yuvshm->readOnly = False;
			if ( yuvshm->shmaddr != (char *)-1 ) {
				shm_error = False;
				X_handler = XSetErrorHandler(shm_errhandler);
				XShmAttach(GFX_Display, yuvshm);
				XSync(GFX_Display, True);
				XSetErrorHandler(X_handler);
				if ( shm_error )
					shmdt(yuvshm->shmaddr);
			} else {
				shm_error = True;
			}
			shmctl(yuvshm->shmid, IPC_RMID, NULL);
		} else {
			shm_error = True;
		}
		if ( shm_error ) {
			XFree(hwdata->image);
			hwdata->yuv_use_mitshm = 0;
		} else {
			hwdata->image->data = yuvshm->shmaddr;
		}
	}
	if ( !hwdata->yuv_use_mitshm )
#endif /* NO_SHARED_MEMORY */
	{
		hwdata->image = SDL_NAME(XvCreateImage)(GFX_Display, xv_port, format,
							0, width, height);

#ifdef PITCH_WORKAROUND
		if ( hwdata->image != NULL && hwdata->image->pitches[0] != (width*bpp) ) {
			/* Ajust overlay width according to pitch */ 
			XFree(hwdata->image);
			width = hwdata->image->pitches[0] / bpp;
			hwdata->image = SDL_NAME(XvCreateImage)(GFX_Display, xv_port, format,
								0, width, height);
		}
#endif /* PITCH_WORKAROUND */
		if ( hwdata->image == NULL ) {
			SDL_SetError("Couldn't create XVideo image");
			SDL_FreeYUVOverlay(overlay);
			return(NULL);
		}
		hwdata->image->data = SDL_malloc(hwdata->image->data_size);
		if ( hwdata->image->data == NULL ) {
			SDL_OutOfMemory();
			SDL_FreeYUVOverlay(overlay);
			return(NULL);
		}
	}

	/* Find the pitch and offset values for the overlay */
	overlay->planes = hwdata->image->num_planes;
	overlay->pitches = (Uint16 *)SDL_malloc(overlay->planes * sizeof(Uint16));
	overlay->pixels = (Uint8 **)SDL_malloc(overlay->planes * sizeof(Uint8 *));
	if ( !overlay->pitches || !overlay->pixels ) {
		SDL_OutOfMemory();
		SDL_FreeYUVOverlay(overlay);
		return(NULL);
	}
	for ( i=0; i<overlay->planes; ++i ) {
		overlay->pitches[i] = hwdata->image->pitches[i];
		overlay->pixels[i] = (Uint8 *)hwdata->image->data +
		                              hwdata->image->offsets[i];
	}

#ifdef XFREE86_REFRESH_HACK
	/* Work around an XFree86 X server bug (?)
	   We can't perform normal updates in windows that have video
	   being output to them.  See SDL_x11image.c for more details.
	 */
	X11_DisableAutoRefresh(this);
#endif

#ifdef INTEL_XV_BADALLOC_WORKAROUND
	/* HACK, GRRR sometimes (i810) creating the overlay succeeds, but the
	   first call to XvShm[Put]Image to a mapped window fails with:
	   "BadAlloc (insufficient resources for operation)". This happens with
	   certain formats when the XvImage is too large to the i810's liking.

	   We work around this by doing a test XvShm[Put]Image with a black
	   Xv image, this may cause some flashing, so only do this check if we
	   are running on an intel Xv-adapter. */
	if (intel_adapter)
	{
		xv_error = False;
		X_handler = XSetErrorHandler(intel_errhandler);
		
		X11_ClearYUVOverlay(overlay);

		/* We set the destination height and width to 1 pixel to avoid
		   putting a large black rectangle over the screen, thus
		   strongly reducing possible flashing. */
#ifndef NO_SHARED_MEMORY
		if ( hwdata->yuv_use_mitshm ) {
			SDL_NAME(XvShmPutImage)(GFX_Display, hwdata->port,
				SDL_Window, SDL_GC,
				hwdata->image,
				0, 0, overlay->w, overlay->h,
				0, 0, 1, 1, False);
		}
		else
#endif
		{
			SDL_NAME(XvPutImage)(GFX_Display, hwdata->port,
				SDL_Window, SDL_GC,
				hwdata->image,
				0, 0, overlay->w, overlay->h,
				0, 0, 1, 1);
		}
		XSync(GFX_Display, False);
		XSetErrorHandler(X_handler);

		if (xv_error)
		{
			X11_FreeYUVOverlay(this, overlay);
			return NULL;
		}
		/* Repair the (1 pixel worth of) damage we've just done */
		X11_RefreshDisplay(this);
	}
#endif

	/* We're all done.. */
	return(overlay);
}