]> git.tdb.fi Git - libs/gui.git/commitdiff
Avoid using std::list
authorMikko Rasa <tdb@tdb.fi>
Sun, 27 Nov 2022 14:18:31 +0000 (16:18 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 27 Nov 2022 14:27:05 +0000 (16:27 +0200)
source/graphics/android/display_platform.h
source/graphics/display.h
source/graphics/imageloader.cpp
source/graphics/imageloader.h
source/graphics/monitor.h
source/graphics/windows/display.cpp
source/graphics/x11/display.cpp

index e07cb2ebeacaa76a7962a35bc5a7d7db6ff31efc..6795f38239165b1a04e247d17fda585a9a763130 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GRAPHICS_DISPLAY_PLATFORM_H_
 #define MSP_GRAPHICS_DISPLAY_PLATFORM_H_
 
+#include <deque>
 #include <android/input.h>
 #include <android/native_window.h>
 #include <sigc++/trackable.h>
@@ -15,7 +16,7 @@ typedef void *DisplayHandle;
 struct PlatformDisplayPrivate: public sigc::trackable
 {
        AInputQueue *input_queue = nullptr;
-       std::list<Window::Event> events;
+       std::deque<Window::Event> events;
        Mutex event_mutex;
        WindowHandle native_window = nullptr;
        Mutex window_mutex;
index f78aaff48448a67cd5532945a187e181ca72cc17..23ab57113ef0f626d8c4120468da6f60e6159c2b 100644 (file)
@@ -1,9 +1,9 @@
 #ifndef MSP_GRAPHICS_DISPLAY_H_
 #define MSP_GRAPHICS_DISPLAY_H_
 
-#include <list>
 #include <stdexcept>
 #include <string>
+#include <vector>
 #include <sigc++/signal.h>
 #include "errordialog.h"
 #include "monitor.h"
@@ -23,9 +23,9 @@ public:
        sigc::signal<void> signal_lost_focus;
 
 private:
-       std::list<Monitor> monitors;
+       std::vector<Monitor> monitors;
        Monitor *primary_monitor = nullptr;
-       std::list<VideoMode> modes;
+       std::vector<VideoMode> modes;
        Private *priv = nullptr;
        ErrorDialog *err_dialog = nullptr;
        Window *focus_window = nullptr;
@@ -39,8 +39,8 @@ public:
        void add_window(Window &);
        void remove_window(Window &);
 
-       const std::list<Monitor> &get_monitors() const { return monitors; }
-       const std::list<VideoMode> &get_modes() const { return modes; }
+       const std::vector<Monitor> &get_monitors() const { return monitors; }
+       const std::vector<VideoMode> &get_modes() const { return modes; }
        const VideoMode &get_desktop_mode() const;
        void set_mode(const VideoMode &, bool = false);
        void restore_mode();
index a9383401a6f46c10a5f58fcfa0ef55369ff006b3..64033661360741443185bc9fe004d1da85953dfb 100644 (file)
@@ -1,4 +1,5 @@
 #include "imageloader.h"
+#include <msp/core/algorithm.h>
 #include <msp/core/refptr.h>
 #include <msp/io/file.h>
 #include <msp/strings/format.h>
@@ -57,7 +58,7 @@ ImageLoader *ImageLoader::open_io(IO::Seekable &io)
        if(registry.changed)
        {
                registry.changed = false;
-               registry.loaders.sort(signature_size_compare);
+               sort(registry.loaders, signature_size_compare);
        }
 
        if(registry.loaders.empty())
index ca69459f22601dcd2ee520f419b3881c279c42dc..53c9e7b7c88ffe04c5fc5ad098cc14ac7acafaf2 100644 (file)
@@ -53,7 +53,7 @@ protected:
 
        struct Registry
        {
-               std::list<RegisterBase *> loaders;
+               std::vector<RegisterBase *> loaders;
                bool changed = false;
 
                ~Registry();
index b74cea898a6398390cce90d21ad20bcea06fb000..8d86d4e0c9507673df77444195dc3e250574d2ad 100644 (file)
@@ -1,8 +1,8 @@
 #ifndef MSP_GRAPHICS_MONITOR_H_
 #define MSP_GRAPHICS_MONITOR_H_
 
-#include <list>
 #include <string>
+#include <vector>
 #include "videomode.h"
 
 namespace Msp {
@@ -20,7 +20,7 @@ struct Monitor
 
        unsigned index = 0;
        std::string name;
-       std::list<const VideoMode *> video_modes;
+       std::vector<const VideoMode *> video_modes;
        Settings desktop_settings;
        Settings current_settings;
        const Monitor *next_left = nullptr;
index bea18a64d0f8b05d372e8e900f2e89c4f7d1608f..09ca2e9edab55530bdc796dedc796141e6fa01ef 100644 (file)
@@ -1,5 +1,6 @@
 #include "display.h"
 #include "display_private.h"
+#include <algorithm>
 #include <windows.h>
 
 using namespace std;
@@ -12,6 +13,9 @@ Display::Display(const string &):
 {
        static ErrorDialog err_dlg(nullptr);
 
+       int primary_index = -1;
+       vector<unsigned> mode_monitor_indices;
+       vector<int> desktop_mode_indices;
        for(unsigned i=0;; ++i)
        {
                DISPLAY_DEVICE adapter_dev;
@@ -29,33 +33,54 @@ Display::Display(const string &):
                priv->monitors.push_back(adapter_dev.DeviceName);
 
                if(adapter_dev.StateFlags&DISPLAY_DEVICE_PRIMARY_DEVICE)
-                       primary_monitor = &monitor;
+                       primary_index = i;
 
                DEVMODE current;
                bool have_current = EnumDisplaySettings(adapter_dev.DeviceName, ENUM_CURRENT_SETTINGS, &current);
 
+               unsigned first_mode = modes.size();
+               int desktop_mode_index = -1;
                for(unsigned j=0;; ++j)
                {
                        DEVMODE info;
                        if(!EnumDisplaySettings(adapter_dev.DeviceName, j, &info))
                                break;
 
+                       if(any_of(modes.begin()+first_mode, modes.end(), [&info](const VideoMode &m){
+                                       return (m.width==info.dmPelsWidth && m.height==info.dmPelsHeight && m.rate==info.dmDisplayFrequency);
+                               }))
+                               continue;
+
                        VideoMode mode(info.dmPelsWidth, info.dmPelsHeight);
                        mode.index = modes.size();
-                       mode.monitor = &monitor;
                        mode.rate = info.dmDisplayFrequency;
-                       if(find_mode(mode))
-                               continue;
 
                        modes.push_back(mode);
-                       monitor.video_modes.push_back(&modes.back());
+                       mode_monitor_indices.push_back(monitor.index);
 
                        if(have_current && info.dmPelsWidth==current.dmPelsWidth && info.dmPelsHeight==current.dmPelsHeight && info.dmDisplayFrequency==current.dmDisplayFrequency)
-                               monitor.desktop_settings.mode = &modes.back();
+                               desktop_mode_index = mode.index;
                }
+               desktop_mode_indices.push_back(desktop_mode_index);
+       }
 
-               monitor.current_settings = monitor.desktop_settings;
+       for(unsigned i=0; i<monitors.size(); ++i)
+       {
+               int j = desktop_mode_indices[i];
+               if(j>=0)
+                       monitors[i].desktop_settings.mode = &modes[j];
+               monitors[i].current_settings = monitors[i].desktop_settings;
        }
+
+       for(unsigned i=0; i<modes.size(); ++i)
+       {
+               Monitor &monitor = monitors[mode_monitor_indices[i]];
+               modes[i].monitor = &monitor;
+               monitor.video_modes.push_back(&modes[i]);
+       }
+
+       if(primary_index>=0)
+               primary_monitor = &monitors[primary_index];
 }
 
 Display::~Display()
index fd6048566d3faec09b0108440e7b4f008a985a33..544f9589fb664cfc3381db0d36fbf7b8c5e1c8d6 100644 (file)
@@ -5,6 +5,7 @@
 #ifdef WITH_XRANDR
 #include <X11/extensions/Xrandr.h>
 #endif
+#include <msp/core/algorithm.h>
 #include <msp/io/print.h>
 #include <msp/strings/format.h>
 #include <msp/strings/lexicalcast.h>
@@ -116,6 +117,9 @@ Display::Display(const string &disp_name):
                        for(int i=0; i<res->nmode; ++i)
                                modes_by_id[res->modes[i].id] = &res->modes[i];
 
+                       int primary_index = -1;
+                       vector<unsigned> mode_monitor_indices;
+                       vector<int> desktop_mode_indices;
                        for(int i=0; i<res->noutput; ++i)
                        {
                                XRROutputInfo *output = XRRGetOutputInfo(priv->display, res, res->outputs[i]);
@@ -159,8 +163,10 @@ Display::Display(const string &disp_name):
                                }
 
                                if(res->outputs[i]==primary)
-                                       primary_monitor = &monitor;
+                                       primary_index = monitor.index;
 
+                               unsigned first_mode = modes.size();
+                               int desktop_mode_index = -1;
                                for(int j=0; j<output->nmode; ++j)
                                {
                                        auto k = modes_by_id.find(output->modes[j]);
@@ -168,23 +174,25 @@ Display::Display(const string &disp_name):
                                                continue;
 
                                        XRRModeInfo *info = k->second;
+                                       float rate = static_cast<float>(info->dotClock)/(info->hTotal*info->vTotal);
+
+                                       if(any_of(modes.begin()+first_mode, modes.end(), [&info, rate](const VideoMode &m){
+                                                       return (m.width==info->width && m.height==info->height && abs(m.rate-rate)<0.01f);
+                                               }))
+                                               continue;
 
                                        VideoMode mode(info->width, info->height);
                                        mode.index = modes.size();
-                                       mode.monitor = &monitor;
-                                       mode.rate = static_cast<float>(info->dotClock)/(info->hTotal*info->vTotal);
-                                       if(find_mode(mode, 0.01f))
-                                               continue;
+                                       mode.rate = rate;
 
                                        modes.push_back(mode);
                                        priv->modes.push_back(info->id);
-                                       monitor.video_modes.push_back(&modes.back());
+                                       mode_monitor_indices.push_back(monitor.index);
 
                                        if(crtc && info->id==crtc->mode)
-                                               monitor.desktop_settings.mode = &modes.back();
+                                               desktop_mode_index = mode.index;
                                }
-
-                               monitor.current_settings = monitor.desktop_settings;
+                               desktop_mode_indices.push_back(desktop_mode_index);
 
                                XRRFreeOutputInfo(output);
                                if(crtc)
@@ -193,7 +201,23 @@ Display::Display(const string &disp_name):
 
                        XRRFreeScreenResources(res);
 
-                       monitors.sort(monitor_x_compare);
+                       for(unsigned i=0; i<monitors.size(); ++i)
+                       {
+                               int j = desktop_mode_indices[i];
+                               if(j>=0)
+                                       monitors[i].desktop_settings.mode = &modes[j];
+                               monitors[i].current_settings = monitors[i].desktop_settings;
+                       }
+
+                       sort(monitors, monitor_x_compare);
+
+                       for(unsigned i=0; i<modes.size(); ++i)
+                       {
+                               auto j = find_member(monitors, mode_monitor_indices[i], &Monitor::index);
+                               modes[i].monitor = &*j;
+                               j->video_modes.push_back(&modes[i]);
+                       }
+
                        Monitor *prev_enabled = nullptr;
                        for(Monitor &m: monitors)
                                if(m.desktop_settings.mode)
@@ -204,6 +228,9 @@ Display::Display(const string &disp_name):
                                        prev_enabled = &m;
                                }
 
+                       if(primary_index>=0)
+                               primary_monitor = &*find_member(monitors, static_cast<unsigned>(primary_index), &Monitor::index);
+
                        if(!primary_monitor || !primary_monitor->desktop_settings.mode)
                        {
                                // XRandR didn't give a sensible primary monitor.  Try to guess one.