]> git.sesse.net Git - nageru/blob - glwidget.cpp
Add a copy of the right-click HDMI/SDI output video menu on the main menu bar.
[nageru] / glwidget.cpp
1 #include "glwidget.h"
2
3 #include <assert.h>
4 #include <bmusb/bmusb.h>
5 #include <movit/effect_chain.h>
6 #include <movit/resource_pool.h>
7 #include <stdbool.h>
8 #include <stdint.h>
9 #include <QAction>
10 #include <QActionGroup>
11 #include <QList>
12 #include <QMenu>
13 #include <QPoint>
14 #include <QVariant>
15 #include <QWidget>
16 #include <functional>
17 #include <map>
18 #include <mutex>
19 #include <utility>
20
21 #include "audio_mixer.h"
22 #include "context.h"
23 #include "context_menus.h"
24 #include "flags.h"
25 #include "mainwindow.h"
26 #include "mixer.h"
27 #include "ref_counted_gl_sync.h"
28
29 class QMouseEvent;
30
31 #undef Success
32 #include <movit/util.h>
33 #include <string>
34
35 using namespace std;
36 using namespace std::placeholders;
37
38 GLWidget::GLWidget(QWidget *parent)
39     : QGLWidget(parent, global_share_widget)
40 {
41 }
42
43 void GLWidget::clean_context()
44 {
45         if (resource_pool != nullptr) {
46                 makeCurrent();
47                 resource_pool->clean_context();
48         }
49 }
50
51 void GLWidget::initializeGL()
52 {
53         static once_flag flag;
54         call_once(flag, [this]{
55                 global_mixer = new Mixer(QGLFormat::toSurfaceFormat(format()), global_flags.num_cards);
56                 global_audio_mixer = global_mixer->get_audio_mixer();
57                 global_mainwindow->mixer_created(global_mixer);
58                 global_mixer->start();
59         });
60         global_mixer->set_frame_ready_callback(output, [this]{
61                 QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
62         });
63         if (output == Mixer::OUTPUT_LIVE) {
64                 global_mixer->set_transition_names_updated_callback(output, [this](const vector<string> &names){
65                         emit transition_names_updated(names);
66                 });
67                 setContextMenuPolicy(Qt::CustomContextMenu);
68                 connect(this, &QWidget::customContextMenuRequested,
69                         bind(&GLWidget::show_live_context_menu, this, _1));
70         }
71         if (output >= Mixer::OUTPUT_INPUT0) {
72                 global_mixer->set_name_updated_callback(output, [this](const string &name){
73                         emit name_updated(output, name);
74                 });
75                 global_mixer->set_color_updated_callback(output, [this](const string &color){
76                         emit color_updated(output, color);
77                 });
78
79                 int signal_num = global_mixer->get_channel_signal(output);
80                 if (signal_num != -1) {
81                         setContextMenuPolicy(Qt::CustomContextMenu);
82                         connect(this, &QWidget::customContextMenuRequested,
83                                 bind(&GLWidget::show_preview_context_menu, this, signal_num, _1));
84                 }
85         }
86
87         glDisable(GL_BLEND);
88         glDisable(GL_DEPTH_TEST);
89         glDepthMask(GL_FALSE);
90 }
91
92 void GLWidget::resizeGL(int width, int height)
93 {
94         glViewport(0, 0, width, height);
95 }
96
97 void GLWidget::paintGL()
98 {
99         Mixer::DisplayFrame frame;
100         if (!global_mixer->get_display_frame(output, &frame)) {
101                 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
102                 check_error();
103                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
104                 check_error();
105                 return;
106         }
107
108         check_error();
109         glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
110         check_error();
111         frame.setup_chain();
112         check_error();
113         frame.chain->render_to_screen();
114         check_error();
115
116         if (resource_pool == nullptr) {
117                 resource_pool = frame.chain->get_resource_pool();
118         } else {
119                 assert(resource_pool == frame.chain->get_resource_pool());
120         }
121 }
122
123 void GLWidget::mousePressEvent(QMouseEvent *event)
124 {
125         emit clicked();
126 }
127
128 void GLWidget::show_live_context_menu(const QPoint &pos)
129 {
130         QPoint global_pos = mapToGlobal(pos);
131
132         QMenu menu;
133
134         // Add a submenu for selecting output card, with an action for each card.
135         QMenu card_submenu;
136         fill_hdmi_sdi_output_device_menu(&card_submenu);
137         card_submenu.setTitle("HDMI/SDI output device");
138         menu.addMenu(&card_submenu);
139
140         // Add a submenu for choosing the output resolution. Since this is
141         // card-dependent, it is disabled if we haven't chosen a card
142         // (but it's still there so that the user will know it exists).
143         QMenu resolution_submenu;
144         fill_hdmi_sdi_output_resolution_menu(&resolution_submenu);
145         resolution_submenu.setTitle("HDMI/SDI output resolution");
146         menu.addMenu(&resolution_submenu);
147
148         // Show the menu; if there's an action selected, it will deal with it itself.
149         menu.exec(global_pos);
150 }
151
152 void GLWidget::show_preview_context_menu(unsigned signal_num, const QPoint &pos)
153 {
154         QPoint global_pos = mapToGlobal(pos);
155
156         QMenu menu;
157
158         // Add a submenu for selecting input card, with an action for each card.
159         QMenu card_submenu;
160         QActionGroup card_group(&card_submenu);
161
162         unsigned num_cards = global_mixer->get_num_cards();
163         unsigned current_card = global_mixer->map_signal(signal_num);
164         for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
165                 QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
166                 QAction *action = new QAction(description, &card_group);
167                 action->setCheckable(true);
168                 if (current_card == card_index) {
169                         action->setChecked(true);
170                 }
171                 action->setData(QList<QVariant>{"card", card_index});
172                 card_submenu.addAction(action);
173         }
174
175         card_submenu.setTitle("Input source");
176         menu.addMenu(&card_submenu);
177
178         // --- The choices in the next few options depend a lot on which card is active ---
179
180         // Add a submenu for selecting video input, with an action for each input.
181         QMenu video_input_submenu;
182         QActionGroup video_input_group(&video_input_submenu);
183         std::map<uint32_t, string> video_inputs = global_mixer->get_available_video_inputs(current_card);
184         uint32_t current_video_input = global_mixer->get_current_video_input(current_card);
185         for (const auto &mode : video_inputs) {
186                 QString description(QString::fromStdString(mode.second));
187                 QAction *action = new QAction(description, &video_input_group);
188                 action->setCheckable(true);
189                 if (mode.first == current_video_input) {
190                         action->setChecked(true);
191                 }
192                 action->setData(QList<QVariant>{"video_input", mode.first});
193                 video_input_submenu.addAction(action);
194         }
195
196         video_input_submenu.setTitle("Video input");
197         menu.addMenu(&video_input_submenu);
198
199         // The same for audio input.
200         QMenu audio_input_submenu;
201         QActionGroup audio_input_group(&audio_input_submenu);
202         std::map<uint32_t, string> audio_inputs = global_mixer->get_available_audio_inputs(current_card);
203         uint32_t current_audio_input = global_mixer->get_current_audio_input(current_card);
204         for (const auto &mode : audio_inputs) {
205                 QString description(QString::fromStdString(mode.second));
206                 QAction *action = new QAction(description, &audio_input_group);
207                 action->setCheckable(true);
208                 if (mode.first == current_audio_input) {
209                         action->setChecked(true);
210                 }
211                 action->setData(QList<QVariant>{"audio_input", mode.first});
212                 audio_input_submenu.addAction(action);
213         }
214
215         audio_input_submenu.setTitle("Audio input");
216         menu.addMenu(&audio_input_submenu);
217
218         // The same for resolution.
219         QMenu mode_submenu;
220         QActionGroup mode_group(&mode_submenu);
221         std::map<uint32_t, bmusb::VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
222         uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
223         bool has_auto_mode = false;
224         for (const auto &mode : video_modes) {
225                 QString description(QString::fromStdString(mode.second.name));
226                 QAction *action = new QAction(description, &mode_group);
227                 action->setCheckable(true);
228                 if (mode.first == current_video_mode) {
229                         action->setChecked(true);
230                 }
231                 action->setData(QList<QVariant>{"video_mode", mode.first});
232                 mode_submenu.addAction(action);
233
234                 // TODO: Relying on the 0 value here (from bmusb.h) is ugly, it should be a named constant.
235                 if (mode.first == 0) {
236                         has_auto_mode = true;
237                 }
238         }
239
240         // Add a “scan” menu if there's no “auto” mode.
241         if (!has_auto_mode) {
242                 QAction *action = new QAction("Scan", &mode_group);
243                 action->setData(QList<QVariant>{"video_mode", 0});
244                 mode_submenu.addSeparator();
245                 mode_submenu.addAction(action);
246         }
247
248         mode_submenu.setTitle("Input mode");
249         menu.addMenu(&mode_submenu);
250
251         // --- End of card-dependent choices ---
252
253         // Add an audio source selector.
254         QAction *audio_source_action = nullptr;
255         if (global_audio_mixer->get_mapping_mode() == AudioMixer::MappingMode::SIMPLE) {
256                 audio_source_action = new QAction("Use as audio source", &menu);
257                 audio_source_action->setCheckable(true);
258                 if (global_audio_mixer->get_simple_input() == signal_num) {
259                         audio_source_action->setChecked(true);
260                         audio_source_action->setEnabled(false);
261                 }
262                 menu.addAction(audio_source_action);
263         }
264
265         // And a master clock selector.
266         QAction *master_clock_action = new QAction("Use as master clock", &menu);
267         master_clock_action->setCheckable(true);
268         if (global_mixer->get_output_card_index() != -1) {
269                 master_clock_action->setChecked(false);
270                 master_clock_action->setEnabled(false);
271         } else if (global_mixer->get_master_clock() == signal_num) {
272                 master_clock_action->setChecked(true);
273                 master_clock_action->setEnabled(false);
274         }
275         menu.addAction(master_clock_action);
276
277         // Show the menu and look at the result.
278         QAction *selected_item = menu.exec(global_pos);
279         if (audio_source_action != nullptr && selected_item == audio_source_action) {
280                 global_audio_mixer->set_simple_input(signal_num);
281         } else if (selected_item == master_clock_action) {
282                 global_mixer->set_master_clock(signal_num);
283         } else if (selected_item != nullptr) {
284                 QList<QVariant> selected = selected_item->data().toList();
285                 if (selected[0].toString() == "video_mode") {
286                         uint32_t mode = selected[1].toUInt(nullptr);
287                         if (mode == 0 && !has_auto_mode) {
288                                 global_mixer->start_mode_scanning(current_card);
289                         } else {
290                                 global_mixer->set_video_mode(current_card, mode);
291                         }
292                 } else if (selected[0].toString() == "video_input") {
293                         uint32_t input = selected[1].toUInt(nullptr);
294                         global_mixer->set_video_input(current_card, input);
295                 } else if (selected[0].toString() == "audio_input") {
296                         uint32_t input = selected[1].toUInt(nullptr);
297                         global_mixer->set_audio_input(current_card, input);
298                 } else if (selected[0].toString() == "card") {
299                         unsigned card_index = selected[1].toUInt(nullptr);
300                         global_mixer->set_signal_mapping(signal_num, card_index);
301                 } else {
302                         assert(false);
303                 }
304         }
305 }