📄 sdl_x11video.c
字号:
/* SDL - Simple DirectMedia Layer Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Sam Lantinga slouken@libsdl.org*/#ifdef SAVE_RCSIDstatic char rcsid = "@(#) $Id: SDL_x11video.c,v 1.13 2002/09/30 00:35:25 slouken Exp $";#endif/* X11 based SDL video driver implementation. Note: This implementation does not currently need X11 thread locking, since the event thread uses a separate X connection and any additional locking necessary is handled internally. However, if full locking is neccessary, take a look at XInitThreads().*/#include <stdlib.h>#include <stdio.h>#include <unistd.h>#include <string.h>#include <sys/ioctl.h>#ifdef MTRR_SUPPORT#include <asm/mtrr.h>#include <sys/fcntl.h>#endif#ifdef HAVE_ALLOCA_H#include <alloca.h>#endif#ifdef HAVE_ALLOCA#define ALLOCA(n) ((void*)alloca(n))#define FREEA(p)#else#define ALLOCA(n) malloc(n)#define FREEA(p) free(p)#endif#include "SDL.h"#include "SDL_error.h"#include "SDL_timer.h"#include "SDL_thread.h"#include "SDL_video.h"#include "SDL_mouse.h"#include "SDL_endian.h"#include "SDL_sysvideo.h"#include "SDL_pixels_c.h"#include "SDL_events_c.h"#include "SDL_x11video.h"#include "SDL_x11wm_c.h"#include "SDL_x11mouse_c.h"#include "SDL_x11events_c.h"#include "SDL_x11modes_c.h"#include "SDL_x11image_c.h"#include "SDL_x11yuv_c.h"#include "SDL_x11gl_c.h"#include "SDL_x11gamma_c.h"#include "blank_cursor.h"/* Initialization/Query functions */static int X11_VideoInit(_THIS, SDL_PixelFormat *vformat);static SDL_Surface *X11_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);static int X11_ToggleFullScreen(_THIS, int on);static void X11_UpdateMouse(_THIS);static int X11_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);static int X11_SetGammaRamp(_THIS, Uint16 *ramp);static void X11_VideoQuit(_THIS);/* X11 driver bootstrap functions */static int X11_Available(void){ Display *display; display = XOpenDisplay(NULL); if ( display != NULL ) { XCloseDisplay(display); } return(display != NULL);}static void X11_DeleteDevice(SDL_VideoDevice *device){ if ( device ) { if ( device->hidden ) { free(device->hidden); } if ( device->gl_data ) { free(device->gl_data); } free(device); }}static SDL_VideoDevice *X11_CreateDevice(int devindex){ SDL_VideoDevice *device; /* Initialize all variables that we clean on shutdown */ device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice)); if ( device ) { memset(device, 0, (sizeof *device)); device->hidden = (struct SDL_PrivateVideoData *) malloc((sizeof *device->hidden)); device->gl_data = (struct SDL_PrivateGLData *) malloc((sizeof *device->gl_data)); } if ( (device == NULL) || (device->hidden == NULL) || (device->gl_data == NULL) ) { SDL_OutOfMemory(); X11_DeleteDevice(device); return(0); } memset(device->hidden, 0, (sizeof *device->hidden)); memset(device->gl_data, 0, (sizeof *device->gl_data)); /* Set the driver flags */ device->handles_any_size = 1; /* Set the function pointers */ device->VideoInit = X11_VideoInit; device->ListModes = X11_ListModes; device->SetVideoMode = X11_SetVideoMode; device->ToggleFullScreen = X11_ToggleFullScreen; device->UpdateMouse = X11_UpdateMouse;#ifdef XFREE86_XV device->CreateYUVOverlay = X11_CreateYUVOverlay;#endif device->SetColors = X11_SetColors; device->UpdateRects = NULL; device->VideoQuit = X11_VideoQuit; device->AllocHWSurface = X11_AllocHWSurface; device->CheckHWBlit = NULL; device->FillHWRect = NULL; device->SetHWColorKey = NULL; device->SetHWAlpha = NULL; device->LockHWSurface = X11_LockHWSurface; device->UnlockHWSurface = X11_UnlockHWSurface; device->FlipHWSurface = X11_FlipHWSurface; device->FreeHWSurface = X11_FreeHWSurface; device->SetGamma = X11_SetVidModeGamma; device->GetGamma = X11_GetVidModeGamma; device->SetGammaRamp = X11_SetGammaRamp; device->GetGammaRamp = NULL;#ifdef HAVE_OPENGL device->GL_LoadLibrary = X11_GL_LoadLibrary; device->GL_GetProcAddress = X11_GL_GetProcAddress; device->GL_GetAttribute = X11_GL_GetAttribute; device->GL_MakeCurrent = X11_GL_MakeCurrent; device->GL_SwapBuffers = X11_GL_SwapBuffers;#endif device->SetCaption = X11_SetCaption; device->SetIcon = X11_SetIcon; device->IconifyWindow = X11_IconifyWindow; device->GrabInput = X11_GrabInput; device->GetWMInfo = X11_GetWMInfo; device->FreeWMCursor = X11_FreeWMCursor; device->CreateWMCursor = X11_CreateWMCursor; device->ShowWMCursor = X11_ShowWMCursor; device->WarpWMCursor = X11_WarpWMCursor; device->CheckMouseMode = X11_CheckMouseMode; device->InitOSKeymap = X11_InitOSKeymap; device->PumpEvents = X11_PumpEvents; device->free = X11_DeleteDevice; return device;}VideoBootStrap X11_bootstrap = { "x11", "X Window System", X11_Available, X11_CreateDevice};/* Shared memory information */extern int XShmQueryExtension(Display *dpy); /* Not in X11 headers *//* Normal X11 error handler routine */static int (*X_handler)(Display *, XErrorEvent *) = NULL;static int x_errhandler(Display *d, XErrorEvent *e){#ifdef XFREE86_VM extern int vm_error;#endif#ifdef XFREE86_DGAMOUSE extern int dga_error;#endif#ifdef XFREE86_VM /* VidMode errors are non-fatal. :) */ /* Are the errors offset by one from the error base? e.g. the error base is 143, the code is 148, and the actual error is XF86VidModeExtensionDisabled (4) ? */ if ( (vm_error >= 0) && (((e->error_code == BadRequest)&&(e->request_code == vm_error)) || ((e->error_code > vm_error) && (e->error_code <= (vm_error+XF86VidModeNumberErrors)))) ) {#ifdef XFREE86_DEBUG{ char errmsg[1024]; XGetErrorText(d, e->error_code, errmsg, sizeof(errmsg));printf("VidMode error: %s\n", errmsg);}#endif return(0); }#endif /* XFREE86_VM */#ifdef XFREE86_DGAMOUSE /* DGA errors can be non-fatal. :) */ if ( (dga_error >= 0) && ((e->error_code > dga_error) && (e->error_code <= (dga_error+XF86DGANumberErrors))) ) {#ifdef XFREE86_DEBUG{ char errmsg[1024]; XGetErrorText(d, e->error_code, errmsg, sizeof(errmsg));printf("DGA error: %s\n", errmsg);}#endif return(0); }#endif /* XFREE86_DGAMOUSE */ return(X_handler(d,e));}/* X11 I/O error handler routine */static int (*XIO_handler)(Display *) = NULL;static int xio_errhandler(Display *d){ /* Ack! Lost X11 connection! */ /* We will crash if we try to clean up our display */ if ( current_video->hidden->Ximage ) { SDL_VideoSurface->pixels = NULL; } current_video->hidden->X11_Display = NULL; /* Continue with the standard X11 error handler */ return(XIO_handler(d));}/* Create auxiliary (toplevel) windows with the current visual */static void create_aux_windows(_THIS){ XSetWindowAttributes xattr; XWMHints *hints; XTextProperty titleprop, iconprop; int def_vis = (SDL_Visual == DefaultVisual(SDL_Display, SDL_Screen)); /* Don't create any extra windows if we are being managed */ if ( SDL_windowid ) { FSwindow = 0; WMwindow = strtol(SDL_windowid, NULL, 0); return; } if(FSwindow) XDestroyWindow(SDL_Display, FSwindow); xattr.override_redirect = True; xattr.background_pixel = def_vis ? BlackPixel(SDL_Display, SDL_Screen) : 0; xattr.border_pixel = 0; xattr.colormap = SDL_XColorMap; FSwindow = XCreateWindow(SDL_Display, SDL_Root, xinerama_x, xinerama_y, 32, 32, 0, this->hidden->depth, InputOutput, SDL_Visual, CWOverrideRedirect | CWBackPixel | CWBorderPixel | CWColormap, &xattr); XSelectInput(SDL_Display, FSwindow, StructureNotifyMask); /* Tell KDE to keep the fullscreen window on top */ { XEvent ev; long mask; memset(&ev, 0, sizeof(ev)); ev.xclient.type = ClientMessage; ev.xclient.window = SDL_Root; ev.xclient.message_type = XInternAtom(SDL_Display, "KWM_KEEP_ON_TOP", False); ev.xclient.format = 32; ev.xclient.data.l[0] = FSwindow; ev.xclient.data.l[1] = CurrentTime; mask = SubstructureRedirectMask; XSendEvent(SDL_Display, SDL_Root, False, mask, &ev); } hints = NULL; titleprop.value = iconprop.value = NULL; if(WMwindow) { /* All window attributes must survive the recreation */ hints = XGetWMHints(SDL_Display, WMwindow); XGetWMName(SDL_Display, WMwindow, &titleprop); XGetWMIconName(SDL_Display, WMwindow, &iconprop); XDestroyWindow(SDL_Display, WMwindow); } /* Create the window for windowed management */ /* (reusing the xattr structure above) */ WMwindow = XCreateWindow(SDL_Display, SDL_Root, 0, 0, 32, 32, 0, this->hidden->depth, InputOutput, SDL_Visual, CWBackPixel | CWBorderPixel | CWColormap, &xattr); /* Set the input hints so we get keyboard input */ if(!hints) { hints = XAllocWMHints(); hints->input = True; hints->flags = InputHint; } XSetWMHints(SDL_Display, WMwindow, hints); XFree(hints); if(titleprop.value) { XSetWMName(SDL_Display, WMwindow, &titleprop); XFree(titleprop.value); } if(iconprop.value) { XSetWMIconName(SDL_Display, WMwindow, &iconprop); XFree(iconprop.value); } XSelectInput(SDL_Display, WMwindow, FocusChangeMask | KeyPressMask | KeyReleaseMask | PropertyChangeMask | StructureNotifyMask | KeymapStateMask); /* Set the class hints so we can get an icon (AfterStep) */ { XClassHint *classhints; classhints = XAllocClassHint(); if(classhints != NULL) { char *classname = getenv("SDL_VIDEO_X11_WMCLASS"); if ( ! classname ) { classname = "SDL_App"; } classhints->res_name = classname; classhints->res_class = classname; XSetClassHint(SDL_Display, WMwindow, classhints); XFree(classhints); } } /* Allow the window to be deleted by the window manager */ WM_DELETE_WINDOW = XInternAtom(SDL_Display, "WM_DELETE_WINDOW", False); XSetWMProtocols(SDL_Display, WMwindow, &WM_DELETE_WINDOW, 1);}static int X11_VideoInit(_THIS, SDL_PixelFormat *vformat){ char *display; int i; /* Open the X11 display */ display = NULL; /* Get it from DISPLAY environment variable */ if ( (strncmp(XDisplayName(display), ":", 1) == 0) || (strncmp(XDisplayName(display), "unix:", 5) == 0) ) { local_X11 = 1; } else { local_X11 = 0; } SDL_Display = XOpenDisplay(display); if ( SDL_Display == NULL ) { SDL_SetError("Couldn't open X11 display"); return(-1); }#ifdef X11_DEBUG XSynchronize(SDL_Display, True);#endif /* Create an alternate X display for graphics updates -- allows us to do graphics updates in a separate thread from event handling. Thread-safe X11 doesn't seem to exist. */ GFX_Display = XOpenDisplay(display); if ( GFX_Display == NULL ) { SDL_SetError("Couldn't open X11 display"); return(-1); } /* Set the normal X error handler */ X_handler = XSetErrorHandler(x_errhandler); /* Set the error handler if we lose the X display */ XIO_handler = XSetIOErrorHandler(xio_errhandler); /* use default screen (from $DISPLAY) */ SDL_Screen = DefaultScreen(SDL_Display); use_mitshm = 0;#ifndef NO_SHARED_MEMORY /* Check for MIT shared memory extension */ if ( local_X11 ) { use_mitshm = XShmQueryExtension(SDL_Display); }#endif /* NO_SHARED_MEMORY */ /* Get the available video modes */ if(X11_GetVideoModes(this) < 0) return -1; /* Determine the default screen depth: Use the default visual (or at least one with the same depth) */ SDL_DisplayColormap = DefaultColormap(SDL_Display, SDL_Screen); for(i = 0; i < this->hidden->nvisuals; i++) if(this->hidden->visuals[i].depth == DefaultDepth(SDL_Display, SDL_Screen)) break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -