Remove C++17-ism.
[dcpomatic.git] / src / wx / grok / gpu_config_panel.h
1 #pragma once
2
3 static std::vector<std::string> get_gpu_names(std::string binary, std::string filename)
4 {
5     // Execute the GPU listing program and redirect its output to a file
6     if (std::system((binary + " > " +  filename).c_str()) < 0) {
7             return {};
8     }
9
10     std::vector<std::string> gpu_names;
11     std::ifstream file(filename);
12     if (file.is_open())
13     {
14         std::string line;
15         while (std::getline(file, line))
16             gpu_names.push_back(line);
17         file.close();
18     }
19
20     return gpu_names;
21 }
22
23 class GpuList : public wxPanel
24 {
25 public:
26     GpuList(wxPanel* parent) : wxPanel(parent, wxID_ANY), selection(0) {
27         comboBox = new wxComboBox(this, wxID_ANY, "", wxDefaultPosition, wxSize(400, -1));
28         comboBox->Bind(wxEVT_COMBOBOX, &GpuList::OnComboBox, this);
29         update();
30
31         wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
32
33         sizer->Add(comboBox, 0, wxALIGN_CENTER_VERTICAL); // Vertically center the comboBox
34
35         this->SetSizerAndFit(sizer);
36     }
37     void update(void) {
38         auto cfg = Config::instance();
39         auto lister_binary = cfg->gpu_binary_location() + "/" + "gpu_lister";
40         auto lister_file = cfg->gpu_binary_location () + "/" + "gpus.txt";
41         if (boost::filesystem::exists(lister_binary)) {
42                         auto gpu_names = get_gpu_names(lister_binary, lister_file);
43
44                         comboBox->Clear();
45                         for (const auto& name : gpu_names)
46                                  comboBox->Append(name);
47         }
48     }
49
50     int getSelection(void) {
51         return selection;
52     }
53     void setSelection(int sel) {
54         if ((int)comboBox->GetCount() > sel)
55             comboBox->SetSelection(sel);
56     }
57
58 private:
59     void OnComboBox(wxCommandEvent&)
60     {
61         selection = comboBox->GetSelection();
62         if (selection != wxNOT_FOUND)
63                 Config::instance ()->set_selected_gpu(selection);
64     }
65
66     wxComboBox* comboBox;
67     int selection;
68 };
69
70 class GPUPage : public Page
71 {
72 public:
73         GPUPage (wxSize panel_size, int border)
74                 : Page (panel_size, border),
75                   _enable_gpu(nullptr), _binary_location(nullptr), _gpu_list_control(nullptr)
76         {}
77
78         wxString GetName () const override
79         {
80                 return _("GPU");
81         }
82
83 #ifdef DCPOMATIC_OSX
84         wxBitmap GetLargeIcon () const override
85         {
86                 return wxBitmap(icon_path("tms"), wxBITMAP_TYPE_PNG);
87         }
88 #endif
89
90 private:
91         void setup () override
92         {
93                 auto config = Config::instance ();
94
95                 _enable_gpu = new CheckBox(_panel, _("Enable GPU acceleration"));
96                 _panel->GetSizer()->Add (_enable_gpu, 0, wxALL | wxEXPAND, _border);
97
98                 wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
99                 table->AddGrowableCol (1, 1);
100                 _panel->GetSizer()->Add (table, 1, wxALL | wxEXPAND, _border);
101
102                 add_label_to_sizer(table, _panel, _("Acceleration binary folder"), true, 0, wxLEFT | wxLEFT | wxALIGN_CENTRE_VERTICAL);
103                 _binary_location = new wxDirPickerCtrl (_panel, wxDD_DIR_MUST_EXIST);
104                 table->Add (_binary_location, 1, wxEXPAND);
105
106                 add_label_to_sizer(table, _panel, _("GPU selection"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
107                 _gpu_list_control = new GpuList(_panel);
108                 table->Add (_gpu_list_control, 1, wxEXPAND);
109
110                 add_label_to_sizer(table, _panel, _("License server"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
111                 _server = new wxTextCtrl (_panel, wxID_ANY);
112                 table->Add (_server, 1, wxEXPAND | wxALL);
113
114                 add_label_to_sizer (table, _panel, _("Port"), false, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
115                 _port = new wxSpinCtrl (_panel, wxID_ANY);
116                 _port->SetRange (0, 65535);
117                 table->Add (_port);
118
119                 add_label_to_sizer (table, _panel, _("License"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
120                 _license = new PasswordEntry (_panel);
121                 table->Add (_license->get_panel(), 1, wxEXPAND | wxALL);
122
123                 _enable_gpu->bind(&GPUPage::enable_gpu_changed, this);
124                 _binary_location->Bind (wxEVT_DIRPICKER_CHANGED, boost::bind (&GPUPage::binary_location_changed, this));
125                 _server->Bind (wxEVT_TEXT, boost::bind(&GPUPage::server_changed, this));
126                 _port->Bind (wxEVT_SPINCTRL, boost::bind(&GPUPage::port_changed, this));
127                 _license->Changed.connect (boost::bind(&GPUPage::license_changed, this));
128
129                 _binary_location->Enable(config->enable_gpu());
130                 _gpu_list_control->Enable(config->enable_gpu());
131                 _server->Enable(config->enable_gpu());
132                 _port->Enable(config->enable_gpu());
133                 _license->get_panel()->Enable(config->enable_gpu());
134         }
135
136
137         void config_changed () override
138         {
139                 auto config = Config::instance ();
140
141                 checked_set (_enable_gpu, config->enable_gpu());
142                 _binary_location->SetPath(config->gpu_binary_location ());
143                 _gpu_list_control->update();
144                 _gpu_list_control->setSelection(config->selected_gpu());
145                 checked_set (_server, config->gpu_license_server());
146                 checked_set (_port, config->gpu_license_port());
147                 checked_set (_license, config->gpu_license());
148         }
149
150         void enable_gpu_changed ()
151         {
152                 auto config = Config::instance ();
153
154                 config->set_enable_gpu (_enable_gpu->GetValue());
155                 _binary_location->Enable(config->enable_gpu());
156                 _gpu_list_control->Enable(config->enable_gpu());
157                 _server->Enable(config->enable_gpu());
158                 _port->Enable(config->enable_gpu());
159                 _license->get_panel()->Enable(config->enable_gpu());
160         }
161
162         void binary_location_changed ()
163         {
164                 Config::instance()->set_gpu_binary_location (wx_to_std (_binary_location->GetPath ()));
165                 _gpu_list_control->update();
166         }
167
168         void server_changed ()
169         {
170                 Config::instance()->set_gpu_license_server(wx_to_std(_server->GetValue()));
171         }
172
173         void port_changed ()
174         {
175                 Config::instance()->set_gpu_license_port(_port->GetValue());
176         }
177
178         void license_changed ()
179         {
180                 Config::instance()->set_gpu_license(_license->get());
181         }
182
183         CheckBox* _enable_gpu;
184         wxDirPickerCtrl* _binary_location;
185         GpuList *_gpu_list_control;
186         wxTextCtrl* _server;
187         wxSpinCtrl* _port;
188         PasswordEntry* _license;
189 };