📄 x11_util.cpp
字号:
/*
Copyright (c) 2008, Intel Corporation.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/#include <stdio.h>#include <X11/Xlib.h>#include <X11/Xlibint.h>#include <X11/Xproto.h>#include <X11/extensions/Xrandr.h>#include <X11/extensions/Xrender.h>#include <string.h>#include <stdlib.h>#include "x11_util.h"/* we need to be able to manipulate the Display structure on events */#include <X11/extensions/render.h>#include <X11/extensions/Xrender.h>#include <X11/extensions/extutil.h>#include <X11/extensions/randrproto.h>struct _XRRScreenConfiguration { Screen *screen; /* the root window in GetScreenInfo */ XRRScreenSize *psizes; Rotation rotations; Rotation currotation; int numsizes; int cursize; short current_rate; Time timestamp; Time config_timestamp; int subpixel_order; /* introduced in randr v0.1 */ short *rates; /* introduced in randr v1.1 */ int nrates;};Display *dpy = NULL;int X11DisplayInit(){ dpy = XOpenDisplay (NULL); //open the current dpy return 0;}int X11DisplayFinish(){ if(dpy != NULL) { XCloseDisplay(dpy); } return 0;}int X11GetScreenCount(){ if(dpy != NULL) return ScreenCount (dpy); else return -1;}int X11GetScreenSizeCount(unsigned int scrIndex){ Window root; XRRScreenConfiguration *sc; unsigned int screen; unsigned int nsize; if(dpy) { if(scrIndex >= ScreenCount (dpy)) return -1; root = RootWindow (dpy, scrIndex); sc = XRRGetScreenInfo (dpy, root); XRRConfigSizes(sc, &nsize); return nsize; } else { return -1; }}int X11GetScreenProperty(unsigned int scrIndex, unsigned int sizeIndex,unsigned int which){ Window root; XRRScreenConfiguration *sc; unsigned int screen; unsigned int nsize; XRRScreenSize *sizes; unsigned int color, xres, yres, rate; if(dpy) { if(scrIndex >= ScreenCount (dpy)) return -1; root = RootWindow (dpy, scrIndex); sc = XRRGetScreenInfo (dpy, root); sizes = XRRConfigSizes(sc, &nsize); if(sizeIndex >= nsize) return -1; xres = sizes[sizeIndex].width; yres = sizes[sizeIndex].height; //XXX rate = XRRConfigCurrentRate (sc); //XXX color = sc->screen->root_depth; switch(which) { case COLORDEP: return color; case XRES: return xres; case YRES: return yres; case RATE: return rate; default: return -1; } } else { return -1; }}int X11GetRotateSupported(unsigned int scrIndex){ Window root; XRRScreenConfiguration *sc; unsigned int nsize; unsigned short rotations, currotation; if(dpy) { if(scrIndex >= ScreenCount (dpy)) return -1; root = RootWindow (dpy, scrIndex); sc = XRRGetScreenInfo (dpy, root); XRRConfigSizes(sc, &nsize); rotations = XRRConfigRotations(sc, &currotation); if(rotations > 1) { //rotations support return rotations; } else return 0; } else { return -1; } }int X11GetCurrentRotate(unsigned int scrIndex){ Window root; XRRScreenConfiguration *sc; unsigned int nsize; unsigned short rotations, currotation; if(dpy) { if(scrIndex >= ScreenCount (dpy)) return -1; root = RootWindow (dpy, scrIndex); sc = XRRGetScreenInfo (dpy, root); XRRConfigSizes(sc, &nsize); rotations = XRRConfigRotations(sc, &currotation); return currotation; } else { return -1; } }extern "C" {XExtDisplayInfo *XRRFindDisplay (Display *dpy);}typedef struct _XRandRInfo { XRRScreenConfiguration **config; int major_version, minor_version; /* major_version = -1 means we don't know */ Bool has_rates; /* Server supports refresh rates */} XRandRInfo;Status XRRSetScreenConfigAndRate_test (Display *dpy, XRRScreenConfiguration *conf, Drawable draw, int size_index, Rotation rotation, short rate, Time timestamp){ XExtDisplayInfo *information = XRRFindDisplay (dpy); xRRSetScreenConfigReply reply; XRandRInfo *xrri; int major, minor; /* Make sure has_rates is set */ if (!XRRQueryVersion (dpy, &major, &minor)) return 0; LockDisplay (dpy); xrri = (XRandRInfo *) information->data; if (xrri->has_rates) { xRRSetScreenConfigReq *request; GetReq (RRSetScreenConfig, request); request->rotation = rotation; request->timestamp = timestamp; request->configTimestamp = conf->config_timestamp; request->reqType = information->codes->major_opcode; request->randrReqType = X_RRSetScreenConfig; request->drawable = draw; request->sizeID = size_index; } else { xRR1_0SetScreenConfigReq *request; GetReq (RR1_0SetScreenConfig, request); request->rotation = rotation; request->timestamp = timestamp; request->configTimestamp = conf->config_timestamp; request->reqType = information->codes->major_opcode; request->randrReqType = X_RRSetScreenConfig; request->drawable = draw; request->sizeID = size_index; } (void) _XReply (dpy, (xReply *) &reply, 0, xTrue); if (reply.status == RRSetConfigSuccess) { /* if we succeed, set our view of reality to what we set it to */ conf->config_timestamp = reply.newConfigTimestamp; conf->timestamp = reply.newTimestamp; conf->screen = ScreenOfDisplay (dpy, XRRRootToScreen(dpy, reply.root)); conf->cursize = size_index; conf->currotation = rotation; } UnlockDisplay (dpy); SyncHandle (); return(reply.status);}int X11SetRotation(unsigned int scrIndex, unsigned int rotate){ Window root; XRRScreenConfiguration *sc; unsigned int nsize, cursize, current_rate, current_time; unsigned short rotations, currotation; int ret; if(dpy) { if(scrIndex >= ScreenCount (dpy)) return -1; root = RootWindow (dpy, scrIndex); sc = XRRGetScreenInfo (dpy, root); cursize = XRRConfigSizes(sc, &nsize); current_rate = XRRConfigCurrentRate (sc); if((ret = XRRSetScreenConfigAndRate_test(dpy, sc, DefaultRootWindow (dpy), cursize, (Rotation)(RR_Rotate_0)/*(Rotation)(rotate)*/, 0, CurrentTime)) != 0) { //printf("XRRSetScreenConfigAndRate error, code = %d\n", ret); return -1; } return 0; } else { return -1; }}int X11SetRate(unsigned int scrIndex, unsigned int rate){ Window root; XRRScreenConfiguration *sc; unsigned int nsize, cursize, current_rate, current_time; unsigned short rotations, currotation; if(dpy) { if(scrIndex >= ScreenCount (dpy)) return -1; root = RootWindow (dpy, scrIndex); sc = XRRGetScreenInfo (dpy, root); cursize = XRRConfigSizes(sc, &nsize); //current_rate = XRRConfigCurrentRate (sc); XRRConfigRotations(sc, &currotation); XRRSetScreenConfigAndRate (dpy, sc, DefaultRootWindow (dpy), (SizeID) cursize, (Rotation) (currotation), rate, 0); return 0; } else { return -1; }}int X11SetSize(unsigned int scrIndex, unsigned int size){ Window root; XRRScreenConfiguration *sc; unsigned int nsize, cursize, current_rate, current_time; unsigned short rotations, currotation; if(dpy) { if(scrIndex >= ScreenCount (dpy)) return -1; root = RootWindow (dpy, scrIndex); sc = XRRGetScreenInfo (dpy, root); cursize = XRRConfigSizes(sc, &nsize); current_rate = XRRConfigCurrentRate (sc); XRRConfigRotations(sc, &currotation); if(size >= nsize) return -1; XRRSetScreenConfigAndRate (dpy, sc, DefaultRootWindow (dpy), (SizeID) size, (Rotation) (currotation), current_rate, 0); return 0; } else { return -1; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -