]> git.tdb.fi Git - libs/gui.git/commitdiff
Use float rather than unsigned for refresh rate
authorMikko Rasa <tdb@tdb.fi>
Tue, 20 Oct 2015 16:31:41 +0000 (19:31 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 20 Oct 2015 16:31:41 +0000 (19:31 +0300)
Display::set_mode now accepts a rate which is within +-0.5 Hz of
requested and chooses the closest match if there are more than one.

source/graphics/display.cpp
source/graphics/display.h
source/graphics/videomode.h
source/graphics/x11/display.cpp

index de5a06952a58e8efa8f3e956ee0f949126775426..40112a35f9408760c5f6ab05ea286033403282b1 100644 (file)
@@ -1,3 +1,4 @@
+#include <cmath>
 #include "display.h"
 #include "display_private.h"
 #include "window.h"
@@ -31,19 +32,33 @@ void Display::restore_mode()
                        set_mode(*i->desktop_mode, false);
 }
 
-const VideoMode *Display::find_mode(const VideoMode &mode) const
+const VideoMode *Display::find_mode(const VideoMode &mode, float rate_tolerance) const
 {
+       const VideoMode *closest = 0;
+       float rate_diff = mode.rate;
        for(list<VideoMode>::const_iterator i=modes.begin(); i!=modes.end(); ++i)
        {
                if(mode.monitor && i->monitor!=mode.monitor)
                        continue;
-               if(mode.rate && i->rate!=mode.rate)
+               if(mode.rate && (i->rate<mode.rate-rate_tolerance || i->rate>mode.rate+rate_tolerance))
                        continue;
-               if(i->width==mode.width && i->height==mode.height)
+               if(i->width!=mode.width || i->height!=mode.height)
+                       continue;
+
+               if(mode.rate)
+               {
+                       float d = abs(i->rate-mode.rate);
+                       if(d<rate_diff)
+                       {
+                               closest = &*i;
+                               rate_diff = d;
+                       }
+               }
+               else
                        return &*i;
        }
 
-       return 0;
+       return closest;
 }
 
 const VideoMode *Display::find_mode(unsigned width, unsigned height) const
index 03912de864dbdc95d7d3181406554853316fad0c..9c62b51ee854d1fe3fb98858a4f924f97cdce5d4 100644 (file)
@@ -39,7 +39,7 @@ public:
        const VideoMode &get_desktop_mode() const;
        void set_mode(const VideoMode &, bool = false);
        void restore_mode();
-       const VideoMode *find_mode(const VideoMode &) const;
+       const VideoMode *find_mode(const VideoMode &, float = 0.5f) const;
        const VideoMode *find_mode(unsigned, unsigned) const;
 
        void tick();
index 2836e11a5c134f00d0f610b54833089efeaa04ef..a9c58bd003d1cb26c0f8c052832194cc1446bcb6 100644 (file)
@@ -32,7 +32,7 @@ struct VideoMode
        const Monitor *monitor;
        unsigned width;
        unsigned height;
-       unsigned rate;
+       float rate;
        VideoRotation rotation;
 
        VideoMode();
index 15c69a9c604495132718adb1d526408f4a209441..50c6d57a706ee707f24085754c87eebb3672a016 100644 (file)
@@ -147,7 +147,7 @@ Display::Display(const string &disp_name):
                                        VideoMode mode(info->width, info->height);
                                        mode.index = modes.size();
                                        mode.monitor = &monitor;
-                                       mode.rate = info->dotClock/(info->hTotal*info->vTotal);
+                                       mode.rate = static_cast<float>(info->dotClock)/(info->hTotal*info->vTotal);
                                        if(find_mode(mode))
                                                continue;