]> git.sesse.net Git - nageru/blob - glwidget.cpp
Let settings follow buses when editing the mapping.
[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 <mutex>
18 #include <movit/effect_chain.h>
19 #include <movit/resource_pool.h>
20
21 #include "context.h"
22 #include "flags.h"
23 #include "mainwindow.h"
24 #include "mixer.h"
25 #include "qnamespace.h"
26 #include "ref_counted_gl_sync.h"
27
28 class QMouseEvent;
29 class QWidget;
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         }
68         if (output >= Mixer::OUTPUT_INPUT0) {
69                 global_mixer->set_name_updated_callback(output, [this](const string &name){
70                         emit name_updated(output, name);
71                 });
72                 global_mixer->set_color_updated_callback(output, [this](const string &color){
73                         emit color_updated(output, color);
74                 });
75
76                 int signal_num = global_mixer->get_channel_signal(output);
77                 if (signal_num != -1) {
78                         setContextMenuPolicy(Qt::CustomContextMenu);
79                         connect(this, &QWidget::customContextMenuRequested,
80                                 bind(&GLWidget::show_context_menu, this, signal_num, _1));
81                 }
82         }
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         frame.chain->render_to_screen();
111         check_error();
112
113         if (resource_pool == nullptr) {
114                 resource_pool = frame.chain->get_resource_pool();
115         } else {
116                 assert(resource_pool == frame.chain->get_resource_pool());
117         }
118 }
119
120 void GLWidget::mousePressEvent(QMouseEvent *event)
121 {
122         emit clicked();
123 }
124
125 void GLWidget::show_context_menu(unsigned signal_num, const QPoint &pos)
126 {
127         QPoint global_pos = mapToGlobal(pos);
128
129         QMenu menu;
130
131         // Add a submenu for selecting input card, with an action for each card.
132         QMenu card_submenu;
133         QActionGroup card_group(&card_submenu);
134
135         unsigned num_cards = global_mixer->get_num_cards();
136         unsigned current_card = global_mixer->map_signal(signal_num);
137         for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
138                 QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
139                 QAction *action = new QAction(description, &card_group);
140                 action->setCheckable(true);
141                 if (current_card == card_index) {
142                         action->setChecked(true);
143                 }
144                 action->setData(QList<QVariant>{"card", card_index});
145                 card_submenu.addAction(action);
146         }
147
148         card_submenu.setTitle("Input source");
149         menu.addMenu(&card_submenu);
150
151         // --- The choices in the next few options depend a lot on which card is active ---
152
153         // Add a submenu for selecting video input, with an action for each input.
154         QMenu video_input_submenu;
155         QActionGroup video_input_group(&video_input_submenu);
156         std::map<uint32_t, string> video_inputs = global_mixer->get_available_video_inputs(current_card);
157         uint32_t current_video_input = global_mixer->get_current_video_input(current_card);
158         for (const auto &mode : video_inputs) {
159                 QString description(QString::fromStdString(mode.second));
160                 QAction *action = new QAction(description, &video_input_group);
161                 action->setCheckable(true);
162                 if (mode.first == current_video_input) {
163                         action->setChecked(true);
164                 }
165                 action->setData(QList<QVariant>{"video_input", mode.first});
166                 video_input_submenu.addAction(action);
167         }
168
169         video_input_submenu.setTitle("Video input");
170         menu.addMenu(&video_input_submenu);
171
172         // The same for audio input.
173         QMenu audio_input_submenu;
174         QActionGroup audio_input_group(&audio_input_submenu);
175         std::map<uint32_t, string> audio_inputs = global_mixer->get_available_audio_inputs(current_card);
176         uint32_t current_audio_input = global_mixer->get_current_audio_input(current_card);
177         for (const auto &mode : audio_inputs) {
178                 QString description(QString::fromStdString(mode.second));
179                 QAction *action = new QAction(description, &audio_input_group);
180                 action->setCheckable(true);
181                 if (mode.first == current_audio_input) {
182                         action->setChecked(true);
183                 }
184                 action->setData(QList<QVariant>{"audio_input", mode.first});
185                 audio_input_submenu.addAction(action);
186         }
187
188         audio_input_submenu.setTitle("Audio input");
189         menu.addMenu(&audio_input_submenu);
190
191         // The same for resolution.
192         QMenu mode_submenu;
193         QActionGroup mode_group(&mode_submenu);
194         std::map<uint32_t, bmusb::VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
195         uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
196         bool has_auto_mode = false;
197         for (const auto &mode : video_modes) {
198                 QString description(QString::fromStdString(mode.second.name));
199                 QAction *action = new QAction(description, &mode_group);
200                 action->setCheckable(true);
201                 if (mode.first == current_video_mode) {
202                         action->setChecked(true);
203                 }
204                 action->setData(QList<QVariant>{"video_mode", mode.first});
205                 mode_submenu.addAction(action);
206
207                 // TODO: Relying on the 0 value here (from bmusb.h) is ugly, it should be a named constant.
208                 if (mode.first == 0) {
209                         has_auto_mode = true;
210                 }
211         }
212
213         // Add a “scan” menu if there's no “auto” mode.
214         if (!has_auto_mode) {
215                 QAction *action = new QAction("Scan", &mode_group);
216                 action->setData(QList<QVariant>{"video_mode", 0});
217                 mode_submenu.addSeparator();
218                 mode_submenu.addAction(action);
219         }
220
221         mode_submenu.setTitle("Input mode");
222         menu.addMenu(&mode_submenu);
223
224         // --- End of card-dependent choices ---
225
226         // Add an audio source selector.
227         QAction *audio_source_action = new QAction("Use as audio source", &menu);
228         audio_source_action->setCheckable(true);
229         if (global_mixer->get_audio_source() == signal_num) {
230                 audio_source_action->setChecked(true);
231                 audio_source_action->setEnabled(false);
232         }
233         menu.addAction(audio_source_action);
234
235         // And a master clock selector.
236         QAction *master_clock_action = new QAction("Use as master clock", &menu);
237         master_clock_action->setCheckable(true);
238         if (global_mixer->get_master_clock() == signal_num) {
239                 master_clock_action->setChecked(true);
240                 master_clock_action->setEnabled(false);
241         }
242         menu.addAction(master_clock_action);
243
244         // Show the menu and look at the result.
245         QAction *selected_item = menu.exec(global_pos);
246         if (selected_item == audio_source_action) {
247                 global_mixer->set_audio_source(signal_num);
248         } else if (selected_item == master_clock_action) {
249                 global_mixer->set_master_clock(signal_num);
250         } else if (selected_item != nullptr) {
251                 QList<QVariant> selected = selected_item->data().toList();
252                 if (selected[0].toString() == "video_mode") {
253                         uint32_t mode = selected[1].toUInt(nullptr);
254                         if (mode == 0 && !has_auto_mode) {
255                                 global_mixer->start_mode_scanning(current_card);
256                         } else {
257                                 global_mixer->set_video_mode(current_card, mode);
258                         }
259                 } else if (selected[0].toString() == "video_input") {
260                         uint32_t input = selected[1].toUInt(nullptr);
261                         global_mixer->set_video_input(current_card, input);
262                 } else if (selected[0].toString() == "audio_input") {
263                         uint32_t input = selected[1].toUInt(nullptr);
264                         global_mixer->set_audio_input(current_card, input);
265                 } else if (selected[0].toString() == "card") {
266                         unsigned card_index = selected[1].toUInt(nullptr);
267                         global_mixer->set_signal_mapping(signal_num, card_index);
268                 } else {
269                         assert(false);
270                 }
271         }
272 }