📄 sdl_x11video.c
字号:
XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNResourceName, classname, XNResourceClass, classname, NULL); if (SDL_IC == NULL) { SDL_SetError("no input context could be created"); XCloseIM(SDL_IM); SDL_IM = NULL; } else { /* We need to receive X events that an IM wants and to pass them to the IM through XFilterEvent. The set of events may vary depending on the IM implementation and the options specified through various routes. Although unlikely, the xlib specification allows IM to change the event requirement with its own circumstances, it is safe to call SelectInput whenever we re-create an IC. */ unsigned long mask = 0; char *ret = pXGetICValues(SDL_IC, XNFilterEvents, &mask, NULL); if (ret != NULL) { XUnsetICFocus(SDL_IC); XDestroyIC(SDL_IC); SDL_IC = NULL; SDL_SetError("no input context could be created"); XCloseIM(SDL_IM); SDL_IM = NULL; } else { XSelectInput(SDL_Display, WMwindow, app_event_mask | mask); XSetICFocus(SDL_IC); } } } } #endif /* Allow the window to be deleted by the window manager */ XSetWMProtocols(SDL_Display, WMwindow, &WM_DELETE_WINDOW, 1);}static int X11_VideoInit(_THIS, SDL_PixelFormat *vformat){ const char *env = NULL; char *display; int i; /* Open the X11 display */ display = NULL; /* Get it from DISPLAY environment variable */ if ( (SDL_strncmp(XDisplayName(display), ":", 1) == 0) || (SDL_strncmp(XDisplayName(display), "unix:", 5) == 0) ) { local_X11 = 1; } else { local_X11 = 0; } SDL_Display = XOpenDisplay(display);#if defined(__osf__) && defined(SDL_VIDEO_DRIVER_X11_DYNAMIC) /* On Tru64 if linking without -lX11, it fails and you get following message. * Xlib: connection to ":0.0" refused by server * Xlib: XDM authorization key matches an existing client! * * It succeeds if retrying 1 second later * or if running xhost +localhost on shell. * */ if ( SDL_Display == NULL ) { SDL_Delay(1000); SDL_Display = XOpenDisplay(display); }#endif 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 ) { XCloseDisplay(SDL_Display); SDL_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); /* Set the X extension error handler */ Xext_handler = XSetExtensionErrorHandler(xext_errhandler); /* use default screen (from $DISPLAY) */ SDL_Screen = DefaultScreen(SDL_Display);#ifndef NO_SHARED_MEMORY /* Check for MIT shared memory extension */ use_mitshm = 0; if ( local_X11 ) { use_mitshm = XShmQueryExtension(SDL_Display); }#endif /* NO_SHARED_MEMORY */ /* Get the available video modes */ if(X11_GetVideoModes(this) < 0) { XCloseDisplay(GFX_Display); GFX_Display = NULL; XCloseDisplay(SDL_Display); SDL_Display = NULL; return -1; } /* Determine the current screen size */ this->info.current_w = DisplayWidth(SDL_Display, SDL_Screen); this->info.current_h = DisplayHeight(SDL_Display, SDL_Screen); /* 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; if(i == this->hidden->nvisuals) { /* default visual was useless, take the deepest one instead */ i = 0; } SDL_Visual = this->hidden->visuals[i].visual; if ( SDL_Visual == DefaultVisual(SDL_Display, SDL_Screen) ) { SDL_XColorMap = SDL_DisplayColormap; } else { SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root, SDL_Visual, AllocNone); } this->hidden->depth = this->hidden->visuals[i].depth; vformat->BitsPerPixel = this->hidden->visuals[i].bpp; if ( vformat->BitsPerPixel > 8 ) { vformat->Rmask = SDL_Visual->red_mask; vformat->Gmask = SDL_Visual->green_mask; vformat->Bmask = SDL_Visual->blue_mask; } if ( this->hidden->depth == 32 ) { vformat->Amask = (0xFFFFFFFF & ~(vformat->Rmask|vformat->Gmask|vformat->Bmask)); } X11_SaveVidModeGamma(this); /* Allow environment override of screensaver disable. */ env = SDL_getenv("SDL_VIDEO_ALLOW_SCREENSAVER"); this->hidden->allow_screensaver = ( (env && SDL_atoi(env)) ? 1 : 0 ); /* Save DPMS and screensaver settings */ X11_SaveScreenSaver(SDL_Display, &screensaver_timeout, &dpms_enabled); X11_DisableScreenSaver(this, SDL_Display); /* See if we have been passed a window to use */ SDL_windowid = SDL_getenv("SDL_WINDOWID"); /* Create the fullscreen and managed windows */ create_aux_windows(this); /* Create the blank cursor */ SDL_BlankCursor = this->CreateWMCursor(this, blank_cdata, blank_cmask, BLANK_CWIDTH, BLANK_CHEIGHT, BLANK_CHOTX, BLANK_CHOTY); /* Fill in some window manager capabilities */ this->info.wm_available = 1; /* We're done! */ XFlush(SDL_Display); return(0);}static void X11_DestroyWindow(_THIS, SDL_Surface *screen){ /* Clean up OpenGL */ if ( screen ) { screen->flags &= ~(SDL_OPENGL|SDL_OPENGLBLIT); } X11_GL_Shutdown(this); if ( ! SDL_windowid ) { /* Hide the managed window */ if ( WMwindow ) { XUnmapWindow(SDL_Display, WMwindow); } if ( screen && (screen->flags & SDL_FULLSCREEN) ) { screen->flags &= ~SDL_FULLSCREEN; X11_LeaveFullScreen(this); } /* Destroy the output window */ if ( SDL_Window ) { XDestroyWindow(SDL_Display, SDL_Window); } /* Free the colormap entries */ if ( SDL_XPixels ) { int numcolors; unsigned long pixel; numcolors = SDL_Visual->map_entries; for ( pixel=0; pixel<numcolors; ++pixel ) { while ( SDL_XPixels[pixel] > 0 ) { XFreeColors(GFX_Display, SDL_DisplayColormap,&pixel,1,0); --SDL_XPixels[pixel]; } } SDL_free(SDL_XPixels); SDL_XPixels = NULL; } /* Free the graphics context */ if ( SDL_GC ) { XFreeGC(SDL_Display, SDL_GC); SDL_GC = 0; } }}static SDL_bool X11_WindowPosition(_THIS, int *x, int *y, int w, int h){ const char *window = SDL_getenv("SDL_VIDEO_WINDOW_POS"); const char *center = SDL_getenv("SDL_VIDEO_CENTERED"); if ( window ) { if ( SDL_sscanf(window, "%d,%d", x, y) == 2 ) { return SDL_TRUE; } if ( SDL_strcmp(window, "center") == 0 ) { center = window; } } if ( center ) { *x = (DisplayWidth(SDL_Display, SDL_Screen) - w)/2; *y = (DisplayHeight(SDL_Display, SDL_Screen) - h)/2; return SDL_TRUE; } return SDL_FALSE;}static void X11_SetSizeHints(_THIS, int w, int h, Uint32 flags){ XSizeHints *hints; hints = XAllocSizeHints(); if ( hints ) { if ( flags & SDL_RESIZABLE ) { hints->min_width = 32; hints->min_height = 32; hints->max_height = 4096; hints->max_width = 4096; } else { hints->min_width = hints->max_width = w; hints->min_height = hints->max_height = h; } hints->flags = PMaxSize | PMinSize; if ( flags & SDL_FULLSCREEN ) { hints->x = 0; hints->y = 0; hints->flags |= USPosition; } else /* Center it, if desired */ if ( X11_WindowPosition(this, &hints->x, &hints->y, w, h) ) { hints->flags |= USPosition; XMoveWindow(SDL_Display, WMwindow, hints->x, hints->y); /* Flush the resize event so we don't catch it later */ XSync(SDL_Display, True); } XSetWMNormalHints(SDL_Display, WMwindow, hints); XFree(hints); } /* Respect the window caption style */ if ( flags & SDL_NOFRAME ) { SDL_bool set; Atom WM_HINTS; /* We haven't modified the window manager hints yet */ set = SDL_FALSE; /* First try to set MWM hints */ WM_HINTS = XInternAtom(SDL_Display, "_MOTIF_WM_HINTS", True); if ( WM_HINTS != None ) { /* Hints used by Motif compliant window managers */ struct { unsigned long flags; unsigned long functions; unsigned long decorations; long input_mode; unsigned long status; } MWMHints = { (1L << 1), 0, 0, 0, 0 }; XChangeProperty(SDL_Display, WMwindow, WM_HINTS, WM_HINTS, 32, PropModeReplace, (unsigned char *)&MWMHints, sizeof(MWMHints)/sizeof(long)); set = SDL_TRUE; } /* Now try to set KWM hints */ WM_HINTS = XInternAtom(SDL_Display, "KWM_WIN_DECORATION", True); if ( WM_HINTS != None ) { long KWMHints = 0; XChangeProperty(SDL_Display, WMwindow, WM_HINTS, WM_HINTS, 32, PropModeReplace, (unsigned char *)&KWMHints, sizeof(KWMHints)/sizeof(long)); set = SDL_TRUE; } /* Now try to set GNOME hints */ WM_HINTS = XInternAtom(SDL_Display, "_WIN_HINTS", True); if ( WM_HINTS != None ) { long GNOMEHints = 0; XChangeProperty(SDL_Display, WMwindow, WM_HINTS, WM_HINTS, 32, PropModeReplace, (unsigned char *)&GNOMEHints, sizeof(GNOMEHints)/sizeof(long)); set = SDL_TRUE; } /* Finally set the transient hints if necessary */ if ( ! set ) { XSetTransientForHint(SDL_Display, WMwindow, SDL_Root); } } else { SDL_bool set; Atom WM_HINTS; /* We haven't modified the window manager hints yet */ set = SDL_FALSE; /* First try to unset MWM hints */ WM_HINTS = XInternAtom(SDL_Display, "_MOTIF_WM_HINTS", True); if ( WM_HINTS != None ) { XDeleteProperty(SDL_Display, WMwindow, WM_HINTS); set = SDL_TRUE; } /* Now try to unset KWM hints */ WM_HINTS = XInternAtom(SDL_Display, "KWM_WIN_DECORATION", True); if ( WM_HINTS != None ) { XDeleteProperty(SDL_Display, WMwindow, WM_HINTS); set = SDL_TRUE; } /* Now try to unset GNOME hints */ WM_HINTS = XInternAtom(SDL_Display, "_WIN_HINTS", True); if ( WM_HINTS != None ) { XDeleteProperty(SDL_Display, WMwindow, WM_HINTS); set = SDL_TRUE; } /* Finally unset the transient hints if necessary */ if ( ! set ) { /* NOTE: Does this work? */ XSetTransientForHint(SDL_Display, WMwindow, None); } }}static int X11_CreateWindow(_THIS, SDL_Surface *screen, int w, int h, int bpp, Uint32 flags){ int i, depth; Visual *vis; int vis_change; Uint32 Amask; /* If a window is already present, destroy it and start fresh */ if ( SDL_Window ) { X11_DestroyWindow(this, screen); switch_waiting = 0; /* Prevent jump back to now-meaningless state. */ } /* See if we have been given a window id */ if ( SDL_windowid ) { SDL_Window = SDL_strtol(SDL_windowid, NULL, 0); } else { SDL_Window = 0; } /* find out which visual we are going to use */ if ( flags & SDL_OPENGL ) { XVisualInfo *vi; vi = X11_GL_GetVisual(this); if( !vi ) { return -1; } vis = vi->visual; depth = vi->depth; } else if ( SDL_windowid ) { XWindowAttributes a; XGetWindowAttributes(SDL_Display, SDL_Window, &a); vis = a.visual; depth = a.depth; } else { for ( i = 0; i < this->hidden->nvisuals; i++ ) { if ( this->hidden->visuals[i].bpp == bpp ) break; } if ( i == this->hidden->nvisuals ) { SDL_SetError("No matching visual for requested depth"); return -1; /* should never happen */ } vis = this->hidden->visuals[i].visual; depth = this->hidden->visuals[i].depth; }#ifdef X11_DEBUG printf("Choosing %s visual at %d bpp - %d colormap entries\n", vis->class == PseudoColor ? "PseudoColor" : (vis->class == TrueColor ? "TrueColor" : (vis->class == DirectColor ? "DirectColor" : "Unknown")), depth, vis->map_entries);#endif vis_change = (vis != SDL_Visual); SDL_Visual = vis; this->hidden->depth = depth; /* Allocate the new pixel format for this video mode */ if ( this->hidden->depth == 32 ) { Amask = (0xFFFFFFFF & ~(vis->red_mask|vis->green_mask|vis->blue_mask)); } else { Amask = 0; } if ( ! SDL_ReallocFormat(screen, bpp, vis->red_mask, vis->green_mask, vis->blue_mask, Amask) ) { return -1; } /* Create the appropriate colormap */ if ( SDL_XColorMap != SDL_DisplayColormap ) { XFreeColormap(SDL_Display, SDL_XColorMap); } if ( SDL_Visual->class == PseudoColor ) { int ncolors; /* Allocate the pixel flags */ ncolors = SDL_Visual->map_entries; SDL_XPixels = SDL_malloc(ncolors * sizeof(int)); if(SDL_XPixels == NULL) { SDL_OutOfMemory(); return -1; } SDL_memset(SDL_XPixels, 0, ncolors * sizeof(*SDL_XPixels)); /* always allocate a private colormap on non-default visuals */ if ( SDL_Visual != DefaultVisual(SDL_Display, SDL_Screen) ) { flags |= SDL_HWPALETTE; } if ( flags & SDL_HWPALETTE ) { screen->flags |= SDL_HWPALETTE; SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root, SDL_Visual, AllocAll); } else { SDL_XColorMap = SDL_DisplayColormap; } } else if ( SDL_Visual->class == DirectColor ) { /* Create a colormap which we can manipulate for gamma */ SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root, SDL_Visual, AllocAll); XSync(SDL_Display, False); /* Initialize the colormap to the identity mapping */ SDL_GetGammaRamp(0, 0, 0); this->screen = screen; X11_SetGammaRamp(this, this->gamma); this->screen = NULL; } else { /* Create a read-only colormap for our window */ SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root, SDL_Visual, AllocNone); } /* Recreate the auxiliary windows, if needed (required for GL) */ if ( vis_change ) create_aux_windows(this); if(screen->flags & SDL_HWPALETTE) { /* Since the full-screen window might have got a nonzero background colour (0 is white on some displays), we should reset the background to 0 here since that is what the user expects with a private colormap */ XSetWindowBackground(SDL_Display, FSwindow, 0); XClearWindow(SDL_Display, FSwindow); } /* resize the (possibly new) window manager window */ if( !SDL_windowid ) { X11_SetSizeHints(this, w, h, flags); window_w = w; window_h = h; XResizeWindow(SDL_Display, WMwindow, w, h); } /* Create (or use) the X11 display window */ if ( !SDL_windowid ) { if ( flags & SDL_OPENGL ) { if ( X11_GL_CreateWindow(this, w, h) < 0 ) { return(-1); } } else { XSetWindowAttributes swa; swa.background_pixel = 0; swa.border_pixel = 0; swa.colormap = SDL_XColorMap; SDL_Window = XCreateWindow(SDL_Display, WMwindow, 0, 0, w, h, 0, depth, InputOutput, SDL_Visual, CWBackPixel | CWBorderPixel | CWColormap, &swa);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -