/* ============================================================
 *
 * This file is a part of kipi-plugins project
 * http://www.kipi-plugins.org
 *
 * Date        : 2007-11-14
 * Description : a kipi plugin to slide images.
 *
 * Copyright (C) 2007 by Valerio Fuoglio <valerio dot fuoglio at gmail dot com>
 *
 * Parts of this code are based on smoothslidesaver by Carsten Weinhold 
 * <carsten dot weinhold at gmx dot de>                                           
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation;
 * either version 2, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * ============================================================ */

// TQt includes.

#include <tqapplication.h>

// X11 includes.

#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>

// Local includes.

#include "screenproperties.h"

namespace KIPISlideShowPlugin
{

  ScreenProperties::ScreenProperties(TQWidget *mainWidget) {
  
      activeScreen = TQApplication::desktop()->screenNumber(mainWidget);
  }
    
  unsigned ScreenProperties::suggestFrameRate() {
  
      int eventBase, errorBase;
      if ( !XRRQueryExtension(qt_xdisplay(), &eventBase, &errorBase)) {
          // No information, make a lucky guess on based on that ;)
          return 25;
      }
  
      // ask X11 for the refresh rate of the current screen
      XRRScreenConfiguration* config;
      int screenRate;
  
      config        = XRRGetScreenInfo(qt_xdisplay(), RootWindow(qt_xdisplay(),
                                      activeScreen));
      screenRate    = XRRConfigCurrentRate(config);
      XRRFreeScreenConfigInfo(config);
      //qDebug("monitor refresh rate %d Hz", screenRate);
  
      // Find the frame rate, that matches the monitor's refresh rate best.
      // We will choose between 25, 28 and 30 Hz, to get smooth animations.
      // The following frame rate will be chosen according to the monitor's
      // refresh rate:
      //
      // Frame rate:   Monitor refresh rate
      // 25 Hz         (50), 75, 100 Hz (PAL compliant setups)
      // 28 Hz         85 Hz, 110 Hz    (seems to work ok)
      // 30 Hz         60, 90, 120 Hz   (NTSC compliant setups)
      //
      // However, this will only work, if the kernel can schedule the
      // screensaver at the right time (a 2.6.x kernel should work fine,
      // because of the high HZ value).
      int candidateRate[3] = { 30, 25, 28 };
      int bestRate = candidateRate[0];
      int smallestError = 1000, i = 0;
      do {
          int r     = candidateRate[i];
          int error = TQMIN(screenRate % r, (screenRate + r) % r);
          
          if (error < smallestError) {
              smallestError = error;
              bestRate      = r;
          }
      } while (++i < 3);
  
      //qDebug("using %d Hz as framerate for effects", bestRate);
      return bestRate;
  }
  
    bool ScreenProperties::enableVSync() {
  
      // currently only supported on NVidia hardware using the
      // proprietary driver
  
      // For NVidia graphics cards: always use sync-to-vblank
    //  return (setenv("__GL_SYNC_TO_VBLANK", "1", 1) == 0);
        return false;
  }

}  // NameSpace KIPISlideShowPlugin
