Debian Codegraph @

helpers/g15helper/g15helper_emu.cpp

raw
1 // Copyright 2022-2023 The Mumble Developers. All rights reserved.
2 // Use of this source code is governed by a BSD-style license
3 // that can be found in the LICENSE file at the root of the
4 // Mumble source tree or at <https://www.mumble.info/LICENSE>.
5
6 /*
7 * G15 Helper Emulator
8 */
9
10 #include <windows.h>
11 #include <cstdio>
12
13 #include <QApplication>
14
15 #include "g15helper.h"
16 #include "g15helper_emu.h"
17
18 #if defined(Q_OS_WIN)
19 # include <fcntl.h>
20 # include <io.h>
21 #endif
22
23 G15Reader::G15Reader() : QThread() {
24 moveToThread(this);
25
26 m_isRunning = true;
27 }
28
29 G15Reader::~G15Reader() {
30 m_isRunning = false;
31 }
32
33 void G15Reader::run() {
34 #if defined(Q_OS_WIN)
35 _setmode(_fileno(stdin), O_BINARY);
36 #endif
37
38 quint8 buf[G15_MAX_FBMEM];
39
40 QImage img(QSize(G15_MAX_WIDTH, G15_MAX_HEIGHT), QImage::Format_RGB32);
41
42 while (m_isRunning) {
43 quint8 priority = 0;
44 size_t nread = 0;
45 size_t ntotal = 0;
46
47 memset(&buf, 0, G15_MAX_FBMEM);
48
49 nread = fread(&priority, 1, sizeof(priority), stdin);
50 if (nread <= 0) {
51 qFatal("g15helper_emu: unable to read stdin, retval %lli", static_cast< long long >(nread));
52 }
53
54 // The priority flag is not used by the emulator.
55 Q_UNUSED(priority);
56
57 do {
58 nread = fread(&buf[0] + ntotal, 1, G15_MAX_FBMEM - ntotal, stdin);
59 if (nread <= 0) {
60 qFatal("g15helper_emu: unable to read stdin, retval %lli", static_cast< long long >(nread));
61 }
62 ntotal += nread;
63 } while (ntotal < G15_MAX_FBMEM);
64
65 for (int w = 0; w < G15_MAX_WIDTH; w++) {
66 for (int h = 0; h < G15_MAX_HEIGHT; h++) {
67 quint8 color = buf[G15_MAX_WIDTH * h + w];
68 uint val = 0xff000000;
69 if (color == 0xff) {
70 val = 0xffffffff;
71 }
72 img.setPixel(w, h, val);
73 }
74 }
75
76 QPixmap p = QPixmap::fromImage(img);
77
78 emit readFrame(p);
79 }
80 }
81
82 G15Emulator::G15Emulator() : QMainWindow() {
83 setWindowTitle(QLatin1String("Mumble G15 Emulator"));
84 setStyleSheet("QMainWindow {background: #cacaca;}");
85
86 m_displayLabel = new QLabel(this);
87 m_displayLabel->setMinimumSize(QSize(G15_MAX_WIDTH, G15_MAX_HEIGHT));
88 setCentralWidget(m_displayLabel);
89 }
90
91 void G15Emulator::drawFrame(QPixmap p) {
92 m_displayLabel->setPixmap(p);
93 }
94
95 int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
96 Q_UNUSED(hInstance);
97 Q_UNUSED(hPrevInstance);
98 Q_UNUSED(nCmdShow);
99
100 if (lpCmdLine && (strcmp(lpCmdLine, "/detect") == 0)) {
101 return 0;
102 } else if (!lpCmdLine || (strcmp(lpCmdLine, "/mumble") != 0)) {
103 MessageBox(nullptr, L"This program is run by Mumble, and should not be started separately.",
104 L"Nothing to see here, move along", MB_OK | MB_ICONERROR);
105 return 0;
106 }
107
108 char *argvec[1];
109 argvec[0] = nullptr;
110
111 int argc = 0;
112 char **argv = &argvec[0];
113
114 QApplication app(argc, argv);
115
116 G15Reader reader;
117 G15Emulator emu;
118
119 QObject::connect(&reader, SIGNAL(readFrame(QPixmap)), &emu, SLOT(drawFrame(QPixmap)));
120
121 reader.start();
122 emu.show();
123
124 app.exec();
125
126 return 0;
127 }