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