forked from Dman95/SASM
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdisplayWindow.cpp
executable file
·361 lines (339 loc) · 12.8 KB
/
displayWindow.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
/****************************************************************************
** SASM - simple IDE for assembler development
** Copyright (C) 2013 Dmitriy Manushin
** Contact: site: http://dman95.github.io/SASM/
** e-mail: [email protected]
**
** This file is part of SASM.
**
** SASM is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** SASM is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with SASM. If not, see <http://www.gnu.org/licenses/>.
**
** Этот файл — часть SASM.
**
** SASM - свободная программа: вы можете перераспространять ее и/или
** изменять ее на условиях Стандартной общественной лицензии GNU в том виде,
** в каком она была опубликована Фондом свободного программного обеспечения;
** либо версии 3 лицензии, либо (по вашему выбору) любой более поздней
** версии.
**
** SASM распространяется в надежде, что она будет полезной,
** но БЕЗО ВСЯКИХ ГАРАНТИЙ; даже без неявной гарантии ТОВАРНОГО ВИДА
** или ПРИГОДНОСТИ ДЛЯ ОПРЕДЕЛЕННЫХ ЦЕЛЕЙ. Подробнее см. в Стандартной
** общественной лицензии GNU.
**
** Вы должны были получить копию Стандартной общественной лицензии GNU
** вместе с этой программой. Если это не так, см.
** <http://www.gnu.org/licenses/>.)
**
****************************************************************************/
#include "displayWindow.h"
#define IPC_RESULT_ERROR (-1)
#define FILENAME "/tmp"
DisplayWindow::DisplayWindow(QWidget *parent) :
QWidget(parent)
{
zoom = 1;
this->resize(QSize(512+50, 512+125));
this->setStyleSheet("background-color:grey;");
verticalLayout = new QVBoxLayout(this);
zoomComboBox = new QComboBox(this);
QStringList comboBoxList;
comboBoxList << "1" << "2" << "4" << "8" << "16" << "32";
zoomComboBox->insertItems(0, comboBoxList);
connect(zoomComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(zoomSettingsChanged(int)));
verticalLayout->addWidget(zoomComboBox);
scrollArea = new QScrollArea(this);
scrollArea->setWidgetResizable(true);
scrollAreaWidgetContents = new BufferFrame(this);
scrollArea->setWidget(scrollAreaWidgetContents);
verticalLayout->addWidget(scrollArea);
}
void DisplayWindow::changeDisplay(int msgid){
loop = true;
zoomComboBox->setEnabled(false);
displayPicture = new QImage(512, 512, QImage::Format_RGB32);
displayPicture->fill(qRgb(255, 255, 255));
memset(buffer, 0xff, 512*512);
scrollAreaWidgetContents->setFixedSize(512*zoom+26, 512*zoom+26);
this->msgid = msgid;
res_x = 512;
res_y = 512;
mode = 0;
qint64 fps = 30;
QElapsedTimer programExecutionTime;
QElapsedTimer programExecutionTime2;
this->resize(QSize(512+60, 512+92));
scrollAreaWidgetContents->update();
programExecutionTime.start();
programExecutionTime2.start();
#ifdef Q_OS_WIN32
if(!ConnectNamedPipe(hCreateNamedPipe, NULL)){
if(GetLastError()!=535)
emit printLog(QString("Connection Failed with Error (")+QString::number(GetLastError())+")\n", Qt::red);
}
DWORD dwNoBytesRead;
BOOL readSuccess = ReadFile(
hCreateNamedPipe,
buffer,
BLOCK_SIZE,
&dwNoBytesRead,
NULL);
if(readSuccess && loop){
res_x = buffer[0];
res_y = buffer[4];
for(int i = 1; i < 4; i++){
res_x += buffer[i] << (8*i);
res_y += buffer[4+i] << (8*i);
}
mode = buffer[8];
fps = buffer[9];
display_size = (mode) ? res_x*res_y*3 : res_x*res_y;
displayPicture = new QImage(res_x, res_y, QImage::Format_RGB32);
displayPicture->fill(qRgb(255, 255, 255));
this->resize(QSize(res_x+60, res_y+92));
scrollAreaWidgetContents->setFixedSize(res_x*zoom, res_y*zoom);
scrollAreaWidgetContents->update();
} else {
if(GetLastError()!=109&&GetLastError()!=0)
emit printLog(QString("Read Failed with Error (")+QString::number(GetLastError())+")\n", Qt::red);
loop = false;
}
while(loop){
DWORD dwNoBytesRead;
BOOL readSuccess = ReadFile(
hCreateNamedPipe,
buffer,
BLOCK_SIZE,
&dwNoBytesRead,
NULL);
if(!loop)
break;
if(!readSuccess){
if(GetLastError()!=109)
emit printLog(QString("Read Failed with Error (")+QString::number(GetLastError())+")\n", Qt::red);
break;
}
// display the message and print on display
int currentcharx;
int currentchary;
int needed_pixel = (mode) ? res_x*res_y*3 : res_x*res_y;
if(mode){
for(int l = 0; l < needed_pixel; l+=3) {
currentcharx = (l/3)%res_x;
currentchary = l/3/res_x;
displayPicture->setPixel(currentcharx, currentchary, qRgb(buffer[l], buffer[l+1], buffer[l+2]));
}
} else {
for(int l = 0; l < needed_pixel; l++){
currentcharx = l%res_x;
currentchary = l/res_x;
if(buffer[l]){
displayPicture->setPixel(currentcharx, currentchary, qRgb(255, 255, 255));
} else {
displayPicture->setPixel(currentcharx, currentchary, qRgb(0, 0, 0));
}
}
}
qint64 elapsed_time = programExecutionTime.elapsed();
if(elapsed_time < 1000/fps)
usleep((1000/fps - elapsed_time)*1000);
programExecutionTime.start();
scrollAreaWidgetContents->update();
}
CloseHandle(hCreateNamedPipe);
#else
// wait sem_wait(sem_producer);
struct sembuf sb = {0, -1, 0};
if (semop(sem_pro_id, &sb, 1) == -1 && loop){
emit printLog(QString("sem_pro failed (semop)"+QString(strerror(errno))+"\n"), Qt::red);
}
if(loop){
//setup the shared memory
key_t key = ftok(FILENAME, 'f');
if(key == IPC_RESULT_ERROR){}
shared_block_id = shmget(key, BLOCK_SIZE, 0666 | IPC_CREAT);
if(shared_block_id == IPC_RESULT_ERROR){
emit printLog(QString("shmget failed\n"), Qt::red);
}
block_values = (uint8_t*)shmat(shared_block_id, NULL, 0);
if(block_values == (uint8_t*)IPC_RESULT_ERROR){
emit printLog(QString("shmat failed\n"), Qt::red);
}
/// read setup
res_x = block_values[0];
res_y = block_values[4];
for(int i = 1; i < 4; i++){
res_x += block_values[i] << (8*i);
res_y += block_values[4+i] << (8*i);
}
mode = block_values[8];
fps = block_values[9];
if(fps < 1 || fps > 60){
emit printLog(QString("fps has wrong settings\n"), Qt::red);
fps = 1;
}
display_size = (mode) ? res_x*res_y*3 : res_x*res_y;
displayPicture = new QImage(res_x, res_y, QImage::Format_RGB32);
displayPicture->fill(qRgb(255, 255, 255));
scrollAreaWidgetContents->setFixedSize(res_x*zoom+26, res_y*zoom+26);
this->resize(QSize(res_x+60, res_y+92));
scrollAreaWidgetContents->update();
// sem_post(sem_consumer);
sb.sem_op = 1;
if (semop(sem_con_id, &sb, 1) == -1) {
emit printLog(QString("1 sem_con failed (semop)\n"), Qt::red);
}
}
///
while(loop){
// receive message
// sem_wait(sem_producer);
sb.sem_op = -1;
if (semop(sem_pro_id, &sb, 1) == -1 && loop) {
emit printLog(QString("sem_pro failed (semop)\n"), Qt::red);
}
if(!loop)
break;
// display the message and print on display
int currentcharx, currentchary;
if (mode){
for(int l = 0; l < display_size; l+=3) {
currentcharx = (l/3)%res_x;
currentchary = l/3/res_x;
displayPicture->setPixel(currentcharx, currentchary, qRgb(block_values[l], block_values[l+1], block_values[l+2]));
}
} else {
for(int l = 0; l < display_size; l++){
currentcharx = l%res_x;
currentchary = l/res_x;
if(block_values[l]){
displayPicture->setPixel(currentcharx, currentchary, qRgb(255, 255, 255));
} else {
displayPicture->setPixel(currentcharx, currentchary, qRgb(0, 0, 0));
}
}
}
qint64 elapsed_time = programExecutionTime.elapsed();
//emit printLog("time: "+QString::number(elapsed_time)+"\n");
if(elapsed_time < 1000/fps)
usleep((1000/fps - elapsed_time)*1000);
programExecutionTime.start();
scrollAreaWidgetContents->update();
//sem_post(sem_consumer);
sb.sem_op = 1;
if (semop(sem_con_id, &sb, 1) == -1 && loop) {
emit printLog(QString("2 sem_con failed (semop)\n"), Qt::red);
}
}
//sem_close(sem_consumer);
//sem_close(sem_producer);
/*if(shmctl(shared_block_id, IPC_RMID, NULL) == IPC_RESULT_ERROR){
//emit printLog(QString("shmctl failed\n"), Qt::red);
}
if (semctl(sem_pro_id, 0, IPC_RMID, arg) == -1){
emit printLog(QString("sem_pro failed (close)\n"), Qt::red);
}
if (semctl(sem_con_id, 0, IPC_RMID, arg) == -1){
emit printLog(QString("sem_con failed (close)\n"), Qt::red);
}*/
#endif
loop = false;
zoomComboBox->setEnabled(true);
emit closeDisplay();
}
void DisplayWindow::finish(int msgid){
this->msgid = msgid;
loop = false;
#ifdef Q_OS_WIN32
HANDLE hFile = CreateFileW(
L"\\\\.\\pipe\\SASMPIPE",
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if(hFile == INVALID_HANDLE_VALUE){
emit printLog("Could not create file object ("+QString::number(GetLastError())+").\n", Qt::red);
}
DWORD dwNoBytesWrote;
BOOL writeSuccess = WriteFile(
hFile,
&buffer,
BLOCK_SIZE,
&dwNoBytesWrote,
NULL);
CloseHandle(hFile);
#else
if(loop){
if(shmctl(shared_block_id, IPC_RMID, NULL) == IPC_RESULT_ERROR){
emit printLog(QString("shmctl failed\n"), Qt::red);
}
}
if (semctl(sem_pro_id, 0, IPC_RMID, arg) == -1){
emit printLog(QString("sem_pro failed (close)\n"), Qt::red);
}
if (semctl(sem_con_id, 0, IPC_RMID, arg) == -1){
emit printLog(QString("sem_con failed (close)\n"), Qt::red);
}
#endif
}
void DisplayWindow::zoomSettingsChanged(int value){
if(!loop){
zoom = std::pow(2, value);
scrollAreaWidgetContents->setFixedSize(res_x*zoom+26, res_y*zoom+26);
this->resize(QSize(res_x+60, res_y+92));
scrollAreaWidgetContents->update();
}
}
void DisplayWindow::updateDisplay() {
// display the message and print on display
int currentcharx;
int currentchary;
int needed_pixel = (mode) ? res_x*res_y*3 : res_x*res_y;
if (mode){
for(int l = 0; l < needed_pixel; l+=3) {
currentcharx = (l/3)%res_x;
currentchary = l/3/res_x;
displayPicture->setPixel(currentcharx, currentchary, qRgb(buffer[l], buffer[l+1], buffer[l+2]));
}
} else {
for(int l = 0; l < needed_pixel; l++){
currentcharx = l%res_x;
currentchary = l/res_x;
if(buffer[l]){
displayPicture->setPixel(currentcharx, currentchary, qRgb(255, 255, 255));
} else {
displayPicture->setPixel(currentcharx, currentchary, qRgb(0, 0, 0));
}
}
}
}
void DisplayWindow::closeEvent(QCloseEvent *) {
emit closeSignal();
}
DisplayWindow::~DisplayWindow(){
delete displayImageLabel;
//delete layout;
}
BufferFrame::BufferFrame(DisplayWindow *parent) :
QWidget(parent){
d = parent;
}
void BufferFrame::paintEvent(QPaintEvent*){
QPainter painter(this);
//if(!painter.isActive()){
painter.drawImage(QRect(0,0,d->res_x*d->zoom,d->res_y*d->zoom),*(d->displayPicture));
//}
}