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