Copy-edit GUI strings.
[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([[maybe_unused]] wxCommandEvent& event) {
60         selection = comboBox->GetSelection();
61         if (selection != wxNOT_FOUND)
62                 Config::instance ()->set_selected_gpu(selection);
63     }
64
65     wxComboBox* comboBox;
66     int selection;
67 };
68
69 class GPUPage : public Page
70 {
71 public:
72         GPUPage (wxSize panel_size, int border)
73                 : Page (panel_size, border),
74                   _enable_gpu(nullptr), _binary_location(nullptr), _gpu_list_control(nullptr)
75         {}
76
77         wxString GetName () const override
78         {
79                 return _("GPU");
80         }
81
82 #ifdef DCPOMATIC_OSX
83         wxBitmap GetLargeIcon () const override
84         {
85                 return wxBitmap(icon_path("tms"), wxBITMAP_TYPE_PNG);
86         }
87 #endif
88
89 private:
90         void setup () override
91         {
92                 auto config = Config::instance ();
93
94                 _enable_gpu = new CheckBox(_panel, _("Enable GPU acceleration"));
95                 _panel->GetSizer()->Add (_enable_gpu, 0, wxALL | wxEXPAND, _border);
96
97                 wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
98                 table->AddGrowableCol (1, 1);
99                 _panel->GetSizer()->Add (table, 1, wxALL | wxEXPAND, _border);
100
101                 add_label_to_sizer(table, _panel, _("Acceleration binary folder"), true, 0, wxLEFT | wxLEFT | wxALIGN_CENTRE_VERTICAL);
102                 _binary_location = new wxDirPickerCtrl (_panel, wxDD_DIR_MUST_EXIST);
103                 table->Add (_binary_location, 1, wxEXPAND);
104
105                 add_label_to_sizer(table, _panel, _("GPU selection"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
106                 _gpu_list_control = new GpuList(_panel);
107                 table->Add (_gpu_list_control, 1, wxEXPAND);
108
109                 add_label_to_sizer(table, _panel, _("License server"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
110                 _server = new wxTextCtrl (_panel, wxID_ANY);
111                 table->Add (_server, 1, wxEXPAND | wxALL);
112
113                 add_label_to_sizer (table, _panel, _("Port"), false, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
114                 _port = new wxSpinCtrl (_panel, wxID_ANY);
115                 _port->SetRange (0, 65535);
116                 table->Add (_port);
117
118                 add_label_to_sizer (table, _panel, _("License"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
119                 _license = new PasswordEntry (_panel);
120                 table->Add (_license->get_panel(), 1, wxEXPAND | wxALL);
121
122                 _enable_gpu->bind(&GPUPage::enable_gpu_changed, this);
123                 _binary_location->Bind (wxEVT_DIRPICKER_CHANGED, boost::bind (&GPUPage::binary_location_changed, this));
124                 _server->Bind (wxEVT_TEXT, boost::bind(&GPUPage::server_changed, this));
125                 _port->Bind (wxEVT_SPINCTRL, boost::bind(&GPUPage::port_changed, this));
126                 _license->Changed.connect (boost::bind(&GPUPage::license_changed, this));
127
128                 _binary_location->Enable(config->enable_gpu());
129                 _gpu_list_control->Enable(config->enable_gpu());
130                 _server->Enable(config->enable_gpu());
131                 _port->Enable(config->enable_gpu());
132                 _license->get_panel()->Enable(config->enable_gpu());
133         }
134
135
136         void config_changed () override
137         {
138                 auto config = Config::instance ();
139
140                 checked_set (_enable_gpu, config->enable_gpu());
141                 _binary_location->SetPath(config->gpu_binary_location ());
142                 _gpu_list_control->update();
143                 _gpu_list_control->setSelection(config->selected_gpu());
144                 checked_set (_server, config->gpu_license_server());
145                 checked_set (_port, config->gpu_license_port());
146                 checked_set (_license, config->gpu_license());
147         }
148
149         void enable_gpu_changed ()
150         {
151                 auto config = Config::instance ();
152
153                 config->set_enable_gpu (_enable_gpu->GetValue());
154                 _binary_location->Enable(config->enable_gpu());
155                 _gpu_list_control->Enable(config->enable_gpu());
156                 _server->Enable(config->enable_gpu());
157                 _port->Enable(config->enable_gpu());
158                 _license->get_panel()->Enable(config->enable_gpu());
159         }
160
161         void binary_location_changed ()
162         {
163                 Config::instance()->set_gpu_binary_location (wx_to_std (_binary_location->GetPath ()));
164                 _gpu_list_control->update();
165         }
166
167         void server_changed ()
168         {
169                 Config::instance()->set_gpu_license_server(wx_to_std(_server->GetValue()));
170         }
171
172         void port_changed ()
173         {
174                 Config::instance()->set_gpu_license_port(_port->GetValue());
175         }
176
177         void license_changed ()
178         {
179                 Config::instance()->set_gpu_license(_license->get());
180         }
181
182         CheckBox* _enable_gpu;
183         wxDirPickerCtrl* _binary_location;
184         GpuList *_gpu_list_control;
185         wxTextCtrl* _server;
186         wxSpinCtrl* _port;
187         PasswordEntry* _license;
188 };