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