+ do {
+ XRRScreenSize *ssizes;
+ short *rates;
+ Rotation rot;
+ int i, ssizes_count, rates_count, curr, res_idx = -1;
+ Time timestamp, cfg_timestamp;
+
+ if(xrr_config) {
+ XRRFreeScreenConfigInfo(xrr_config);
+ }
+
+ if(!(xrr_config = XRRGetScreenInfo(fgDisplay.Display, fgDisplay.RootWindow))) {
+ fgWarning("XRRGetScreenInfo failed");
+ break;
+ }
+ ssizes = XRRConfigSizes(xrr_config, &ssizes_count);
+ curr = XRRConfigCurrentConfiguration(xrr_config, &rot);
+ timestamp = XRRConfigTimes(xrr_config, &cfg_timestamp);
+
+ /* if either of xsz or ysz are unspecified, use the current values */
+ if(xsz <= 0)
+ xsz = fgState.GameModeSize.X = ssizes[curr].width;
+ if(ysz <= 0)
+ ysz = fgState.GameModeSize.Y = ssizes[curr].height;
+
+
+ if(xsz == ssizes[curr].width && ysz == ssizes[curr].height) {
+ /* no need to switch, we're already in the requested resolution */
+ res_idx = curr;
+ } else {
+ for(i=0; i<ssizes_count; i++) {
+ if(ssizes[i].width == xsz && ssizes[i].height == ysz) {
+ res_idx = i;
+ break; /* found it */
+ }
+ }
+ }
+ if(res_idx == -1)
+ break; /* no matching resolution */
+
+#if ( RANDR_MAJOR >= 1 ) || ( ( RANDR_MAJOR == 1 ) && ( RANDR_MINOR >= 1 ) )
+ if(rate <= 0) {
+ fgState.GameModeRefresh = XRRConfigCurrentRate(xrr_config);
+ }
+
+ if(use_rate) {
+ rate = fgState.GameModeRefresh;
+
+ /* for the selected resolution, let's find out if there is
+ * a matching refresh rate available.
+ */
+ rates = XRRConfigRates(xrr_config, res_idx, &rates_count);
+
+ for(i=0; i<rates_count; i++) {
+ if(rates[i] == rate) {
+ break;
+ }
+ }
+ if(i == rates_count) {
+ break; /* no matching rate */
+ }
+ }
+#endif
+
+ if(just_checking) {
+ result = 0;
+ break;
+ }
+
+#if ( RANDR_MAJOR >= 1 ) || ( ( RANDR_MAJOR == 1 ) && ( RANDR_MINOR >= 1 ) )
+ if(use_rate)
+ result = XRRSetScreenConfigAndRate(fgDisplay.Display, xrr_config,
+ fgDisplay.RootWindow, res_idx, rot, rate, timestamp);
+ else
+#endif
+ result = XRRSetScreenConfig(fgDisplay.Display, xrr_config,
+ fgDisplay.RootWindow, res_idx, rot, timestamp);
+
+ } while(result == RRSetConfigInvalidTime);
+
+ if(xrr_config) {
+ XRRFreeScreenConfigInfo(xrr_config);
+ }
+
+ if(result == 0) {
+ return 0;
+ }
+
+#endif /* HAVE_X11_EXTENSIONS_XRANDR_H */
+ return -1;
+}
+#endif /* TARGET_HOST_POSIX_X11 */
+
+/*
+ * Remembers the current visual settings, so that
+ * we can change them and restore later...
+ */
+static void fghRememberState( void )
+{
+#if TARGET_HOST_POSIX_X11
+ int event_base, error_base;