make dashboard happy
[kdenetwork.git] / krdc / vidmode.cpp
blob889882a0cb262256becfd071ab30fb06edb94f91
1 /***************************************************************************
2 vidmode.cpp - video mode switching
3 -------------------
4 begin : Tue June 3 03:08:00 CET 2002
5 copyright : (C) 2002 by Tim Jansen
6 email : tim@tjansen.de
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #include <config-krdc.h>
19 #include <X11/Xlib.h>
21 #ifdef HAVE_VIDMODE_EXTENSION
22 #include <X11/extensions/xf86vmode.h>
23 #endif
25 #include "vidmode.h"
27 #ifdef HAVE_VIDMODE_EXTENSION
29 void vidmodeNormalSwitch(Display *dpy, Resolution oldResolution)
31 if (!oldResolution.valid)
32 return;
34 XF86VidModeModeInfo **modes;
35 int modecount;
36 int eventB, errorB;
38 if (!XF86VidModeQueryExtension(dpy, &eventB, &errorB))
39 return;
41 if (!XF86VidModeGetAllModeLines(dpy,oldResolution.screen,&modecount, &modes))
42 return;
44 for (int i = 0; i < modecount; i++) {
45 int w = (*modes[i]).hdisplay;
46 int h = (*modes[i]).vdisplay;
48 if ((oldResolution.width == w) &&
49 (oldResolution.height == h)) {
50 XF86VidModeSwitchToMode(dpy,oldResolution.screen,modes[i]);
51 XFlush(dpy);
52 XF86VidModeSetViewPort(dpy,DefaultScreen(dpy),0,0);
53 XFlush(dpy);
54 return;
59 Resolution vidmodeFullscreenSwitch(Display *dpy, int screen,
60 int sw, int sh, int &nx, int &ny)
62 XF86VidModeModeInfo **modes;
63 int modecount;
64 int bestmode = -1;
65 int bestw, besth;
66 int eventB, errorB;
68 if (screen < 0)
69 return Resolution();
71 if (!XF86VidModeQueryExtension(dpy, &eventB, &errorB))
72 return Resolution();
74 if (!XF86VidModeGetAllModeLines(dpy,screen,&modecount, &modes))
75 return Resolution();
77 int cw = (*modes[0]).hdisplay;
78 int ch = (*modes[0]).vdisplay;
79 nx = cw;
80 ny = ch;
81 if ((cw == sw) && (ch == sh))
82 return Resolution();
83 bool foundLargeEnoughRes = (cw>=sw) && (ch>=sh);
84 bestw = cw;
85 besth = ch;
87 for (int i = 1; i < modecount; i++) {
88 int w = (*modes[i]).hdisplay;
89 int h = (*modes[i]).vdisplay;
91 if ((w == cw) && (h == ch))
92 continue;
94 /* If resolution matches framebuffer, take it */
95 if ((w == sw) && (h == sh)) {
96 bestw = w;
97 besth = h;
98 bestmode = i;
99 break;
101 /* if resolution larger than framebuffer... */
102 if ((w>=sw) && (h>=sh)) {
103 /* and no other previously found resoltion was smaller or
104 this is smaller than the best resolution so far, take it*/
105 if ((!foundLargeEnoughRes) ||
106 (w*h < bestw*besth)) {
107 bestw = w;
108 besth = h;
109 bestmode = i;
110 foundLargeEnoughRes = true;
113 /* If all resolutions so far were smaller than framebuffer... */
114 else if (!foundLargeEnoughRes) {
115 /* take this one it it is bigger then they were */
116 if (w*h > bestw*besth) {
117 bestw = w;
118 besth = h;
119 bestmode = i;
124 if (bestmode == -1)
125 return Resolution();
127 nx = bestw;
128 ny = besth;
129 XF86VidModeSwitchToMode(dpy,screen,modes[bestmode]);
130 XF86VidModeSetViewPort(dpy,screen,0,0);
131 XFlush(dpy);
133 return Resolution(cw, ch, screen);
136 #else
138 void vidmodeNormalSwitch(Display *dpy, Resolution oldResolution)
142 Resolution vidmodeFullscreenSwitch(Display *dpy, int screen, int sw, int sh, int &nx, int &ny)
144 return Resolution();
147 #endif
149 void grabInput(Display *dpy, unsigned int winId) {
150 XGrabPointer(dpy, winId, True, 0,
151 GrabModeAsync, GrabModeAsync,
152 winId, None, CurrentTime);
153 XFlush(dpy);
156 void ungrabInput(Display *dpy) {
157 XUngrabPointer(dpy, CurrentTime);