forked from BearWare/TeamTalk5
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
393 lines (341 loc) · 12.3 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* Copyright (c) 2005-2018, BearWare.dk
*
* Contact Information:
*
* Bjoern D. Rasmussen
* Kirketoften 5
* DK-8260 Viby J
* Denmark
* Email: [email protected]
* Phone: +45 20 20 54 59
* Web: http://www.bearware.dk
*
* This source code is part of the TeamTalk SDK owned by
* BearWare.dk. Use of this file, or its compiled unit, requires a
* TeamTalk SDK License Key issued by BearWare.dk.
*
* The TeamTalk SDK License Agreement along with its Terms and
* Conditions are outlined in the file License.txt included with the
* TeamTalk SDK distribution.
*
*/
#include <QApplication>
#include <QtPlugin>
#include <QFileOpenEvent>
#include <QUrl>
#include <QSettings>
#include <QStyleFactory>
#include <QPalette>
#include <QColor>
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <QAbstractNativeEventFilter>
#endif
#include "mainwindow.h"
#include "license.h"
TTInstance* ttInst = nullptr;
#if defined(Q_OS_WIN32)
class MyQApplication
: public QApplication
{
public:
MyQApplication(int& argc, char **argv)
: QApplication(argc, argv), m_mainwindow(nullptr)
{
QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", QSettings::NativeFormat);
if (settings.value("AppsUseLightTheme", -1).toUInt() == 0)
{
setStyle(QStyleFactory::create("Fusion"));
QPalette darkPalette;
QColor darkColor = QColor(45, 45, 45);
QColor disabledColor = QColor(127, 127, 127);
darkPalette.setColor(QPalette::Window, darkColor);
darkPalette.setColor(QPalette::WindowText, Qt::white);
darkPalette.setColor(QPalette::Base, QColor(18, 18, 18));
darkPalette.setColor(QPalette::AlternateBase, darkColor);
darkPalette.setColor(QPalette::ToolTipBase, Qt::white);
darkPalette.setColor(QPalette::ToolTipText, Qt::white);
darkPalette.setColor(QPalette::Text, Qt::white);
darkPalette.setColor(QPalette::Disabled, QPalette::Text, disabledColor);
darkPalette.setColor(QPalette::Button, darkColor);
darkPalette.setColor(QPalette::ButtonText, Qt::white);
darkPalette.setColor(QPalette::Disabled, QPalette::ButtonText, disabledColor);
darkPalette.setColor(QPalette::BrightText, Qt::red);
darkPalette.setColor(QPalette::Link, QColor(42, 130, 218));
darkPalette.setColor(QPalette::Highlight, QColor(42, 130, 218));
darkPalette.setColor(QPalette::HighlightedText, Qt::black);
darkPalette.setColor(QPalette::Disabled, QPalette::HighlightedText, disabledColor);
setPalette(darkPalette);
setStyleSheet("QToolTip { color: #ffffff; background-color: #2a82da; border: 1px solid white; }");
}
}
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
//TeamTalk event handling for Win32
bool winEventFilter ( MSG * msg, long * result )
{
if(msg->message == WM_TEAMALK_CLIENTEVENT)
{
TTMessage ttmsg;
INT32 wait_ms = 0;
if(TT_GetMessage(ttInst, &ttmsg, &wait_ms) && m_mainwindow)
m_mainwindow->processTTMessage(ttmsg);
}
return QApplication::winEventFilter(msg, result);
}
#endif
MainWindow* m_mainwindow;
};
#elif defined(Q_OS_LINUX)
//For hotkeys on X11
#include <QX11Info>
#include <X11/Xlib.h>
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <xcb/xcb.h> // used by Qt5
#endif
struct x_auto_repeat_data
{
KeyCode keycode;
Time timestamp;
bool release;
bool error;
};
static Bool qt_keypress_scanner(Display *, XEvent *event, XPointer arg)
{
if (event->type != KeyPress && event->type != KeyRelease)
return false;
x_auto_repeat_data *data = (x_auto_repeat_data *) arg;
if (data->error)
return false;
if (event->xkey.keycode != data->keycode)
{
data->error = true;
return false;
}
if (event->type == KeyPress)
{
data->error = (! data->release || event->xkey.time - data->timestamp > 10);
return (! data->error);
}
// must be XKeyRelease event
if (data->release)
{
// found a second release
data->error = true;
return false;
}
// found a single release
data->release = true;
data->timestamp = event->xkey.time;
return false;
}
class MyQApplication : public QApplication
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
, public QAbstractNativeEventFilter
#endif
{
public:
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result)
{
Q_UNUSED(result);
if(eventType == "xcb_generic_event_t")
{
xcb_generic_event_t *ev = static_cast<xcb_generic_event_t *>(message);
xcb_key_press_event_t *keyevent = 0;
unsigned int keycode = 0, mods = 0, type = (ev->response_type & ~0x80);
switch(type)
{
case XCB_KEY_PRESS :
case XCB_KEY_RELEASE :
{
keyevent = static_cast<xcb_key_press_event_t *>(message);
keycode = keyevent->detail;
if(keyevent->state & XCB_MOD_MASK_1)
mods |= Mod1Mask;
if(keyevent->state & XCB_MOD_MASK_CONTROL)
mods |= ControlMask;
if(keyevent->state & XCB_MOD_MASK_4)
mods |= Mod4Mask;
if(keyevent->state & XCB_MOD_MASK_SHIFT)
mods |= ShiftMask;
if(m_mainwindow)
m_mainwindow->keysActive(keycode, mods, type == XCB_KEY_PRESS);
break;
}
}
}
return false;
}
#endif
MyQApplication(int& argc, char **argv)
: QApplication(argc, argv), m_mainwindow(nullptr)
{
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
installNativeEventFilter(this);
#endif
}
bool x11EventFilter ( XEvent * event )
{
if(event->type == KeyPress || event->type == KeyRelease)
{
XKeyEvent* key = reinterpret_cast<XKeyEvent*> (event);
bool autor = false;
static uint curr_autorep = 0;
if (event->type == KeyPress)
{
if (curr_autorep == event->xkey.keycode)
{
autor = true;
curr_autorep = 0;
}
}
else
{
// look ahead for auto-repeat
XEvent nextpress;
Display* dpy = QX11Info::display();
// was this the last auto-repeater?
x_auto_repeat_data auto_repeat_data;
auto_repeat_data.keycode = event->xkey.keycode;
auto_repeat_data.timestamp = event->xkey.time;
auto_repeat_data.release = true;
auto_repeat_data.error = false;
if (XCheckIfEvent(dpy, &nextpress, &qt_keypress_scanner,
(XPointer) &auto_repeat_data))
{
autor = true;
XPutBackEvent(dpy,&nextpress);
}
curr_autorep = autor ? event->xkey.keycode : 0;
}
if(!autor)
m_mainwindow->keysActive(key->keycode, key->state, event->type == KeyPress);
}
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
return true; //x11EventFilter is not supported in Qt5, so just return true
#else
return QApplication::x11EventFilter(event);
#endif
}
MainWindow* m_mainwindow;
};
#elif defined(Q_OS_DARWIN)
OSStatus mac_callback(EventHandlerCallRef nextHandler, EventRef event, void*);
class MyQApplication : public QApplication
{
public:
MyQApplication(int& argc, char **argv)
: QApplication(argc, argv), m_mainwindow(nullptr)
{
EventTypeSpec hkEvents[2];
hkEvents[0].eventClass = kEventClassKeyboard;
hkEvents[0].eventKind = kEventHotKeyPressed;
hkEvents[1].eventClass = kEventClassKeyboard;
hkEvents[1].eventKind = kEventHotKeyReleased;
OSStatus oss = InstallApplicationEventHandler(&mac_callback, 2, hkEvents, nullptr, nullptr);
Q_ASSERT(oss == 0);
#if QT_VERSION >= QT_VERSION_CHECK(5,4,0)
QApplication::setQuitOnLastWindowClosed(false);
#endif
}
//TeamTalk event handling for macOS (Carbon). In QT 4 this is an
//inherited method from QCoreApplication. In QT 5 this is a
//standalone method.
bool macEventFilter(EventHandlerCallRef caller, EventRef event)
{
Q_UNUSED(caller);
if(GetEventClass(event) == kEventClassKeyboard)
{
UInt32 kind = GetEventKind(event);
if(kind == kEventHotKeyPressed ||
kind == kEventHotKeyReleased)
{
EventHotKeyID keyID;
GetEventParameter(event, kEventParamDirectObject,
typeEventHotKeyID,
nullptr, sizeof(keyID), nullptr, &keyID);
m_mainwindow->hotkeyToggle((HotKeyID)keyID.id, kind == kEventHotKeyPressed);
}
}
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
return true; //Just return what ever...
#else
return QApplication::macEventFilter(caller, event);
#endif
}
MainWindow* m_mainwindow;
protected:
bool event(QEvent* e)
{
//Opened through Launch Service
if(e->type() == QEvent::FileOpen)
{
QString tturi = static_cast<QFileOpenEvent*>(e)->file();
if(tturi.isEmpty())
tturi = static_cast<QFileOpenEvent*>(e)->url().toString();
if(m_mainwindow && tturi.size())
m_mainwindow->parseArgs(QStringList() << "abc" << tturi);
}
#if QT_VERSION >= QT_VERSION_CHECK(5,4,0)
// This handles press in Dock on macOS
if (e->type() == QEvent::ApplicationActivated)
{
if(m_mainwindow->isHidden())
m_mainwindow->show();
}
#endif
return QApplication::event(e);
}
};
OSStatus mac_callback(EventHandlerCallRef nextHandler, EventRef event, void*)
{
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
MyQApplication* myApp = dynamic_cast<MyQApplication*>(qApp);
Q_ASSERT(myApp);
myApp->macEventFilter(nextHandler, event);
#else
qApp->macEventFilter(nextHandler, event);
#endif
return noErr;
}
#endif
int main(int argc, char *argv[])
{
#if defined(Q_OS_WIN32)
// Use QWindowsIntegration plugin to distinguish Alt+Gr from Ctrl+Alt on Windows
// https://qthub.com/static/doc/qt5/qtdoc/qpa.html
std::vector< const char* > argv_;
for (int i = 0; i < argc; ++i)
argv_.push_back(argv[i]);
argv_.push_back("-platform");
argv_.push_back("windows:altgr");
argc = int(argv_.size());
MyQApplication app(argc, const_cast<char**>(&argv_[0]));
#elif defined(Q_OS_LINUX) || defined(Q_OS_DARWIN)
MyQApplication app(argc, argv);
#else
QApplication app(argc, argv);
#endif
QString cfgfile;
int idx = QApplication::arguments().indexOf("-cfg");
if(idx >= 0 && ++idx < QApplication::arguments().size())
cfgfile = QApplication::arguments()[idx];
MainWindow window(cfgfile);
/* Set license information before creating the first client instance */
TT_SetLicenseInformation(_W(QString(REGISTRATION_NAME)), _W(QString(REGISTRATION_KEY)));
#if defined(Q_OS_WIN32)
HWND hWnd = reinterpret_cast<HWND>(window.winId());
app.m_mainwindow = &window;
ttInst = TT_InitTeamTalk(hWnd, WM_TEAMALK_CLIENTEVENT);
#elif defined(Q_OS_LINUX) || defined(Q_OS_DARWIN)
app.m_mainwindow = &window;
ttInst = TT_InitTeamTalkPoll();
#else
ttInst = TT_InitTeamTalkPoll();
#endif
window.loadSettings(); //load settings now that we have ttInst
window.show();
int ret = app.exec();
TT_CloseTeamTalk(ttInst);
return ret;
}