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