1 /******************************************************************************
2     Copyright (C) 2014 by Hugh Bailey <obs.jim@gmail.com>
3 
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation, either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
17 
18 #include "obs-app.hpp"
19 #include "window-basic-interaction.hpp"
20 #include "window-basic-main.hpp"
21 #include "qt-wrappers.hpp"
22 #include "display-helpers.hpp"
23 
24 #include <QKeyEvent>
25 #include <QCloseEvent>
26 #include <QScreen>
27 #include <QWindow>
28 
29 using namespace std;
30 
OBSBasicInteraction(QWidget * parent,OBSSource source_)31 OBSBasicInteraction::OBSBasicInteraction(QWidget *parent, OBSSource source_)
32 	: QDialog(parent),
33 	  main(qobject_cast<OBSBasic *>(parent)),
34 	  ui(new Ui::OBSBasicInteraction),
35 	  source(source_),
36 	  removedSignal(obs_source_get_signal_handler(source), "remove",
37 			OBSBasicInteraction::SourceRemoved, this),
38 	  renamedSignal(obs_source_get_signal_handler(source), "rename",
39 			OBSBasicInteraction::SourceRenamed, this),
40 	  eventFilter(BuildEventFilter())
41 {
42 	int cx = (int)config_get_int(App()->GlobalConfig(), "InteractionWindow",
43 				     "cx");
44 	int cy = (int)config_get_int(App()->GlobalConfig(), "InteractionWindow",
45 				     "cy");
46 
47 	Qt::WindowFlags flags = windowFlags();
48 	Qt::WindowFlags helpFlag = Qt::WindowContextHelpButtonHint;
49 	setWindowFlags(flags & (~helpFlag));
50 
51 	ui->setupUi(this);
52 
53 	ui->preview->setMouseTracking(true);
54 	ui->preview->setFocusPolicy(Qt::StrongFocus);
55 	ui->preview->installEventFilter(eventFilter.get());
56 
57 	if (cx > 400 && cy > 400)
58 		resize(cx, cy);
59 
60 	const char *name = obs_source_get_name(source);
61 	setWindowTitle(QTStr("Basic.InteractionWindow").arg(QT_UTF8(name)));
62 
63 	auto addDrawCallback = [this]() {
64 		obs_display_add_draw_callback(ui->preview->GetDisplay(),
65 					      OBSBasicInteraction::DrawPreview,
66 					      this);
67 	};
68 
69 	connect(ui->preview, &OBSQTDisplay::DisplayCreated, addDrawCallback);
70 }
71 
~OBSBasicInteraction()72 OBSBasicInteraction::~OBSBasicInteraction()
73 {
74 	// since QT fakes a mouse movement while destructing a widget
75 	// remove our event filter
76 	ui->preview->removeEventFilter(eventFilter.get());
77 }
78 
BuildEventFilter()79 OBSEventFilter *OBSBasicInteraction::BuildEventFilter()
80 {
81 	return new OBSEventFilter([this](QObject *obj, QEvent *event) {
82 		UNUSED_PARAMETER(obj);
83 
84 		switch (event->type()) {
85 		case QEvent::MouseButtonPress:
86 		case QEvent::MouseButtonRelease:
87 		case QEvent::MouseButtonDblClick:
88 			return this->HandleMouseClickEvent(
89 				static_cast<QMouseEvent *>(event));
90 		case QEvent::MouseMove:
91 		case QEvent::Enter:
92 		case QEvent::Leave:
93 			return this->HandleMouseMoveEvent(
94 				static_cast<QMouseEvent *>(event));
95 
96 		case QEvent::Wheel:
97 			return this->HandleMouseWheelEvent(
98 				static_cast<QWheelEvent *>(event));
99 		case QEvent::FocusIn:
100 		case QEvent::FocusOut:
101 			return this->HandleFocusEvent(
102 				static_cast<QFocusEvent *>(event));
103 		case QEvent::KeyPress:
104 		case QEvent::KeyRelease:
105 			return this->HandleKeyEvent(
106 				static_cast<QKeyEvent *>(event));
107 		default:
108 			return false;
109 		}
110 	});
111 }
112 
SourceRemoved(void * data,calldata_t * params)113 void OBSBasicInteraction::SourceRemoved(void *data, calldata_t *params)
114 {
115 	QMetaObject::invokeMethod(static_cast<OBSBasicInteraction *>(data),
116 				  "close");
117 
118 	UNUSED_PARAMETER(params);
119 }
120 
SourceRenamed(void * data,calldata_t * params)121 void OBSBasicInteraction::SourceRenamed(void *data, calldata_t *params)
122 {
123 	const char *name = calldata_string(params, "new_name");
124 	QString title = QTStr("Basic.InteractionWindow").arg(QT_UTF8(name));
125 
126 	QMetaObject::invokeMethod(static_cast<OBSBasicProperties *>(data),
127 				  "setWindowTitle", Q_ARG(QString, title));
128 }
129 
DrawPreview(void * data,uint32_t cx,uint32_t cy)130 void OBSBasicInteraction::DrawPreview(void *data, uint32_t cx, uint32_t cy)
131 {
132 	OBSBasicInteraction *window = static_cast<OBSBasicInteraction *>(data);
133 
134 	if (!window->source)
135 		return;
136 
137 	uint32_t sourceCX = max(obs_source_get_width(window->source), 1u);
138 	uint32_t sourceCY = max(obs_source_get_height(window->source), 1u);
139 
140 	int x, y;
141 	int newCX, newCY;
142 	float scale;
143 
144 	GetScaleAndCenterPos(sourceCX, sourceCY, cx, cy, x, y, scale);
145 
146 	newCX = int(scale * float(sourceCX));
147 	newCY = int(scale * float(sourceCY));
148 
149 	gs_viewport_push();
150 	gs_projection_push();
151 	const bool previous = gs_set_linear_srgb(true);
152 
153 	gs_ortho(0.0f, float(sourceCX), 0.0f, float(sourceCY), -100.0f, 100.0f);
154 	gs_set_viewport(x, y, newCX, newCY);
155 	obs_source_video_render(window->source);
156 
157 	gs_set_linear_srgb(previous);
158 	gs_projection_pop();
159 	gs_viewport_pop();
160 }
161 
closeEvent(QCloseEvent * event)162 void OBSBasicInteraction::closeEvent(QCloseEvent *event)
163 {
164 	QDialog::closeEvent(event);
165 	if (!event->isAccepted())
166 		return;
167 
168 	config_set_int(App()->GlobalConfig(), "InteractionWindow", "cx",
169 		       width());
170 	config_set_int(App()->GlobalConfig(), "InteractionWindow", "cy",
171 		       height());
172 
173 	obs_display_remove_draw_callback(ui->preview->GetDisplay(),
174 					 OBSBasicInteraction::DrawPreview,
175 					 this);
176 }
177 
TranslateQtKeyboardEventModifiers(QInputEvent * event,bool mouseEvent)178 static int TranslateQtKeyboardEventModifiers(QInputEvent *event,
179 					     bool mouseEvent)
180 {
181 	int obsModifiers = INTERACT_NONE;
182 
183 	if (event->modifiers().testFlag(Qt::ShiftModifier))
184 		obsModifiers |= INTERACT_SHIFT_KEY;
185 	if (event->modifiers().testFlag(Qt::AltModifier))
186 		obsModifiers |= INTERACT_ALT_KEY;
187 #ifdef __APPLE__
188 	// Mac: Meta = Control, Control = Command
189 	if (event->modifiers().testFlag(Qt::ControlModifier))
190 		obsModifiers |= INTERACT_COMMAND_KEY;
191 	if (event->modifiers().testFlag(Qt::MetaModifier))
192 		obsModifiers |= INTERACT_CONTROL_KEY;
193 #else
194 	// Handle windows key? Can a browser even trap that key?
195 	if (event->modifiers().testFlag(Qt::ControlModifier))
196 		obsModifiers |= INTERACT_CONTROL_KEY;
197 #endif
198 
199 	if (!mouseEvent) {
200 		if (event->modifiers().testFlag(Qt::KeypadModifier))
201 			obsModifiers |= INTERACT_IS_KEY_PAD;
202 	}
203 
204 	return obsModifiers;
205 }
206 
TranslateQtMouseEventModifiers(QMouseEvent * event)207 static int TranslateQtMouseEventModifiers(QMouseEvent *event)
208 {
209 	int modifiers = TranslateQtKeyboardEventModifiers(event, true);
210 
211 	if (event->buttons().testFlag(Qt::LeftButton))
212 		modifiers |= INTERACT_MOUSE_LEFT;
213 	if (event->buttons().testFlag(Qt::MiddleButton))
214 		modifiers |= INTERACT_MOUSE_MIDDLE;
215 	if (event->buttons().testFlag(Qt::RightButton))
216 		modifiers |= INTERACT_MOUSE_RIGHT;
217 
218 	return modifiers;
219 }
220 
GetSourceRelativeXY(int mouseX,int mouseY,int & relX,int & relY)221 bool OBSBasicInteraction::GetSourceRelativeXY(int mouseX, int mouseY, int &relX,
222 					      int &relY)
223 {
224 	float pixelRatio = devicePixelRatioF();
225 	int mouseXscaled = (int)roundf(mouseX * pixelRatio);
226 	int mouseYscaled = (int)roundf(mouseY * pixelRatio);
227 
228 	QSize size = GetPixelSize(ui->preview);
229 
230 	uint32_t sourceCX = max(obs_source_get_width(source), 1u);
231 	uint32_t sourceCY = max(obs_source_get_height(source), 1u);
232 
233 	int x, y;
234 	float scale;
235 
236 	GetScaleAndCenterPos(sourceCX, sourceCY, size.width(), size.height(), x,
237 			     y, scale);
238 
239 	if (x > 0) {
240 		relX = int(float(mouseXscaled - x) / scale);
241 		relY = int(float(mouseYscaled / scale));
242 	} else {
243 		relX = int(float(mouseXscaled / scale));
244 		relY = int(float(mouseYscaled - y) / scale);
245 	}
246 
247 	// Confirm mouse is inside the source
248 	if (relX < 0 || relX > int(sourceCX))
249 		return false;
250 	if (relY < 0 || relY > int(sourceCY))
251 		return false;
252 
253 	return true;
254 }
255 
HandleMouseClickEvent(QMouseEvent * event)256 bool OBSBasicInteraction::HandleMouseClickEvent(QMouseEvent *event)
257 {
258 	bool mouseUp = event->type() == QEvent::MouseButtonRelease;
259 	int clickCount = 1;
260 	if (event->type() == QEvent::MouseButtonDblClick)
261 		clickCount = 2;
262 
263 	struct obs_mouse_event mouseEvent = {};
264 
265 	mouseEvent.modifiers = TranslateQtMouseEventModifiers(event);
266 
267 	int32_t button = 0;
268 
269 	switch (event->button()) {
270 	case Qt::LeftButton:
271 		button = MOUSE_LEFT;
272 		break;
273 	case Qt::MiddleButton:
274 		button = MOUSE_MIDDLE;
275 		break;
276 	case Qt::RightButton:
277 		button = MOUSE_RIGHT;
278 		break;
279 	default:
280 		blog(LOG_WARNING, "unknown button type %d", event->button());
281 		return false;
282 	}
283 
284 	// Why doesn't this work?
285 	//if (event->flags().testFlag(Qt::MouseEventCreatedDoubleClick))
286 	//	clickCount = 2;
287 
288 	bool insideSource = GetSourceRelativeXY(event->x(), event->y(),
289 						mouseEvent.x, mouseEvent.y);
290 
291 	if (mouseUp || insideSource)
292 		obs_source_send_mouse_click(source, &mouseEvent, button,
293 					    mouseUp, clickCount);
294 
295 	return true;
296 }
297 
HandleMouseMoveEvent(QMouseEvent * event)298 bool OBSBasicInteraction::HandleMouseMoveEvent(QMouseEvent *event)
299 {
300 	struct obs_mouse_event mouseEvent = {};
301 
302 	bool mouseLeave = event->type() == QEvent::Leave;
303 
304 	if (!mouseLeave) {
305 		mouseEvent.modifiers = TranslateQtMouseEventModifiers(event);
306 		mouseLeave = !GetSourceRelativeXY(event->x(), event->y(),
307 						  mouseEvent.x, mouseEvent.y);
308 	}
309 
310 	obs_source_send_mouse_move(source, &mouseEvent, mouseLeave);
311 
312 	return true;
313 }
314 
HandleMouseWheelEvent(QWheelEvent * event)315 bool OBSBasicInteraction::HandleMouseWheelEvent(QWheelEvent *event)
316 {
317 	struct obs_mouse_event mouseEvent = {};
318 
319 	mouseEvent.modifiers = TranslateQtKeyboardEventModifiers(event, true);
320 
321 	int xDelta = 0;
322 	int yDelta = 0;
323 
324 	const QPoint angleDelta = event->angleDelta();
325 	if (!event->pixelDelta().isNull()) {
326 		if (angleDelta.x())
327 			xDelta = event->pixelDelta().x();
328 		else
329 			yDelta = event->pixelDelta().y();
330 	} else {
331 		if (angleDelta.x())
332 			xDelta = angleDelta.x();
333 		else
334 			yDelta = angleDelta.y();
335 	}
336 
337 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
338 	const QPointF position = event->position();
339 	const int x = position.x();
340 	const int y = position.y();
341 #else
342 	const int x = event->x();
343 	const int y = event->y();
344 #endif
345 
346 	if (GetSourceRelativeXY(x, y, mouseEvent.x, mouseEvent.y)) {
347 		obs_source_send_mouse_wheel(source, &mouseEvent, xDelta,
348 					    yDelta);
349 	}
350 
351 	return true;
352 }
353 
HandleFocusEvent(QFocusEvent * event)354 bool OBSBasicInteraction::HandleFocusEvent(QFocusEvent *event)
355 {
356 	bool focus = event->type() == QEvent::FocusIn;
357 
358 	obs_source_send_focus(source, focus);
359 
360 	return true;
361 }
362 
HandleKeyEvent(QKeyEvent * event)363 bool OBSBasicInteraction::HandleKeyEvent(QKeyEvent *event)
364 {
365 	struct obs_key_event keyEvent;
366 
367 	QByteArray text = event->text().toUtf8();
368 	keyEvent.modifiers = TranslateQtKeyboardEventModifiers(event, false);
369 	keyEvent.text = text.data();
370 	keyEvent.native_modifiers = event->nativeModifiers();
371 	keyEvent.native_scancode = event->nativeScanCode();
372 	keyEvent.native_vkey = event->nativeVirtualKey();
373 
374 	bool keyUp = event->type() == QEvent::KeyRelease;
375 
376 	obs_source_send_key_click(source, &keyEvent, keyUp);
377 
378 	return true;
379 }
380 
Init()381 void OBSBasicInteraction::Init()
382 {
383 	show();
384 }
385