Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
61 andreas 1
/*
2
 * Copyright (C) 2021 by Andreas Theofilu <andreas@theosys.at>
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 3 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, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#include "tqemitqueue.h"
20
#include "terror.h"
21
 
22
TQManageQueue::~TQManageQueue()
23
{
24
    DECL_TRACER("TQManageQueue::~TQManageQueue()");
25
 
26
    TQEmitQueue *next, *p = mEmitQueue;
27
    next = nullptr;
28
 
29
    while (p)
30
    {
31
        next = p->next;
32
        delete p;
33
        p = next;
34
    }
35
}
36
 
37
void TQManageQueue::addBackground(ulong handle, unsigned char* image, size_t size, size_t rowBytes, int width, int height, ulong color)
38
{
39
    DECL_TRACER("TQManageQueue::addBackground(ulong handle, unsigned char* image, size_t size, size_t rowBytes, int width, int height, ulong color)");
40
 
41
    TQEmitQueue *eq = addEntity(ET_BACKGROUND);
42
 
43
    if (!eq)
44
        return;
45
 
46
    eq->handle = handle;
47
    eq->image = image;
48
    eq->size = size;
49
    eq->rowBytes = rowBytes;
50
    eq->width = width;
51
    eq->height = height;
52
    eq->color = color;
53
    removeDuplicates();
54
}
55
 
56
bool TQManageQueue::getBackground(ulong* handle, unsigned char **image, size_t* size, size_t* rowBytes, int* width, int* height, ulong* color)
57
{
58
    DECL_TRACER("TQManageQueue::getBackground(ulong* handle, unsigned char ** image, size_t* size, size_t* rowBytes, int* width, int* height, ulong* color)");
59
 
60
    TQEmitQueue *eq = mEmitQueue;
61
 
62
    while(eq)
63
    {
64
        if (eq->etype == ET_BACKGROUND)
65
        {
66
            *handle = eq->handle;
67
            *image = eq->image;
68
            *size = eq->size;
69
            *rowBytes = eq->rowBytes;
70
            *width = eq->width;
71
            *height = eq->height;
72
            *color = eq->color;
73
            return true;
74
        }
75
 
76
        eq = eq->next;
77
    }
78
 
79
    return false;
80
}
81
 
82
void TQManageQueue::addButton(ulong handle, ulong parent, unsigned char* buffer, int pixline, int left, int top, int width, int height)
83
{
84
    DECL_TRACER("TQManageQueue::addButton(ulong handle, ulong parent, unsigned char* buffer, int pixline, int left, int top, int width, int height)");
85
 
86
    TQEmitQueue *eq = addEntity(ET_BUTTON);
87
 
88
    if (!eq)
89
        return;
90
 
91
    eq->handle = handle;
92
    eq->parent = parent;
93
    eq->buffer = buffer;
94
    eq->pixline = pixline;
95
    eq->left = left;
96
    eq->top = top;
97
    eq->width = width;
98
    eq->height = height;
99
    removeDuplicates();
100
}
101
 
102
bool TQManageQueue::getButton(ulong* handle, ulong* parent, unsigned char ** buffer, int* pixline, int* left, int* top, int* width, int* height)
103
{
104
    DECL_TRACER("TQManageQueue::getButton(ulong* handle, ulong* parent, unsigned char ** buffer, int* pixline, int* left, int* top, int* width, int* height)");
105
 
106
    TQEmitQueue *eq = mEmitQueue;
107
 
108
    while(eq)
109
    {
110
        if (eq->etype == ET_BUTTON)
111
        {
112
            *handle = eq->handle;
113
            *parent = eq->parent;
114
            *buffer = eq->buffer;
115
            *pixline = eq->pixline;
116
            *left = eq->left;
117
            *top = eq->top;
118
            *width = eq->width;
119
            *height = eq->height;
120
            return true;
121
        }
122
 
123
        eq = eq->next;
124
    }
125
 
126
    return false;
127
}
128
 
192 andreas 129
void TQManageQueue::addInText(ulong handle, Button::TButton* button, Button::BITMAP_t bm, int frame)
61 andreas 130
{
192 andreas 131
    DECL_TRACER("TQManageQueue::addInText(ulong handle, Button::TButton* button, Button::BITMAP_t bm, int frame)");
61 andreas 132
 
133
    TQEmitQueue *eq = addEntity(ET_INTEXT);
134
 
135
    if (!eq)
136
        return;
137
 
138
    eq->handle = handle;
139
    eq->button = button;
140
    eq->bm = bm;
192 andreas 141
    eq->frame = frame;
61 andreas 142
    removeDuplicates();
143
}
144
 
200 andreas 145
void TQManageQueue::addListBox(Button::TButton* button, Button::BITMAP_t bm, int frame)
146
{
147
    DECL_TRACER("TQManageQueue::addListBox(Button::TButton* button, Button::BITMAP_t bm, int frame)");
148
 
149
    TQEmitQueue *eq = addEntity(ET_LISTBOX);
150
 
151
    if (!eq)
152
        return;
153
 
154
    eq->handle = button->getHandle();
155
    eq->button = button;
156
    eq->bm = bm;
157
    eq->frame = frame;
158
    removeDuplicates();
159
}
160
 
192 andreas 161
bool TQManageQueue::getInText(ulong* handle, Button::TButton ** button, Button::BITMAP_t* bm, int *frame)
61 andreas 162
{
192 andreas 163
    DECL_TRACER("TQManageQueue::getInText(ulong* handle, Button::TButton ** button, Button::BITMAP_t* bm, int *frame)");
61 andreas 164
 
165
    TQEmitQueue *eq = mEmitQueue;
166
 
167
    while(eq)
168
    {
169
        if (eq->etype == ET_INTEXT)
170
        {
171
            *handle = eq->handle;
172
            *button = eq->button;
173
            *bm = eq->bm;
192 andreas 174
            *frame = eq->frame;
61 andreas 175
            return true;
176
        }
177
    }
178
 
179
    return false;
180
}
181
 
182
void TQManageQueue::addPage(ulong handle, int width, int height)
183
{
184
    DECL_TRACER("TQManageQueue::addPage(ulong handle, int width, int height)");
185
 
186
    TQEmitQueue *eq = addEntity(ET_PAGE);
187
 
188
    if (!eq)
189
        return;
190
 
191
    eq->handle = handle;
192
    eq->width = width;
193
    eq->height = height;
194
    removeDuplicates();
195
}
196
 
197
bool TQManageQueue::getPage(ulong* handle, int* width, int* height)
198
{
199
    DECL_TRACER("TQManageQueue::getPage(ulong* handle, int* width, int* height)");
200
 
201
    TQEmitQueue *eq = mEmitQueue;
202
 
203
    while(eq)
204
    {
205
        if (eq->etype == ET_PAGE)
206
        {
207
            *handle = eq->handle;
208
            *width = eq->width;
209
            *height = eq->height;
210
            return true;
211
        }
212
    }
213
 
214
    return false;
215
}
216
 
217 andreas 217
void TQManageQueue::addSubPage(ulong handle, ulong parent, int left, int top, int width, int height, ANIMATION_t anim)
61 andreas 218
{
217 andreas 219
    DECL_TRACER("TQManageQueue::addSubPage(ulong handle, ulong parent, int left, int top, int width, int height, ANIMATION_t anim)");
61 andreas 220
 
221
    TQEmitQueue *eq = addEntity(ET_SUBPAGE);
222
 
223
    if (!eq)
224
        return;
225
 
226
    eq->handle = handle;
217 andreas 227
    eq->parent = parent;
61 andreas 228
    eq->left = left;
229
    eq->top = top;
230
    eq->width = width;
231
    eq->height = height;
232
    eq->animate = anim;
233
    removeDuplicates();
234
}
235
 
217 andreas 236
bool TQManageQueue::getSubPage(ulong* handle, ulong *parent, int* left, int* top, int* width, int* height, ANIMATION_t* anim)
61 andreas 237
{
217 andreas 238
    DECL_TRACER("TQManageQueue::getSubPage(ulong* handle, ulong *parent, int* left, int* top, int* width, int* height, ANIMATION_t* anim)");
61 andreas 239
 
240
    TQEmitQueue *eq = mEmitQueue;
241
 
242
    while(eq)
243
    {
244
        if (eq->etype == ET_SUBPAGE)
245
        {
246
            *handle = eq->handle;
217 andreas 247
            *parent = eq->parent;
61 andreas 248
            *left = eq->left;
249
            *top = eq->top;
250
            *width = eq->width;
251
            *height = eq->height;
252
            *anim = eq->animate;
253
            return true;
254
        }
255
 
256
        eq = eq->next;
257
    }
258
 
259
    return false;
260
}
261
 
262
void TQManageQueue::addVideo(ulong handle, ulong parent, ulong left, ulong top, ulong width, ulong height, std::string url, std::string user, std::string pw)
263
{
264
    DECL_TRACER("TQManageQueue::addVideo(ulong handle, ulong parent, ulong left, ulong top, ulong width, ulong height, std::string url, std::string user, std::string pw)");
265
 
266
    TQEmitQueue *eq = addEntity(ET_VIDEO);
267
 
268
    if (!eq)
269
        return;
270
 
271
    eq->handle = handle;
272
    eq->parent = parent;
273
    eq->left = left;
274
    eq->top = top;
275
    eq->width = width;
276
    eq->height = height;
277
    eq->url = url;
278
    eq->user = user;
279
    eq->pw = pw;
280
    removeDuplicates();
281
}
282
 
283
bool TQManageQueue::getVideo(ulong* handle, ulong* parent, int* left, int* top, int* width, int* height, std::string* url, std::string* user, std::string* pw)
284
{
285
    DECL_TRACER("TQManageQueue::getVideo(ulong* handle, ulong* parent, int* left, int* top, int* width, int* height, std::string* url, std::string* user, std::string* pw)");
286
 
287
    TQEmitQueue *eq = mEmitQueue;
288
 
289
    while(eq)
290
    {
291
        if (eq->etype == ET_VIDEO)
292
        {
293
            *handle = eq->handle;
294
            *parent = eq->parent;
295
            *left = eq->left;
296
            *top = eq->top;
297
            *width = eq->width;
298
            *height = eq->height;
299
            *url = eq->url;
300
            *user = eq->user;
301
            *pw = eq->pw;
302
            return true;
303
        }
304
 
305
        eq = eq->next;
306
    }
307
 
308
    return false;
309
}
310
 
311
_EMIT_TYPE_t TQManageQueue::getNextType()
312
{
313
    DECL_TRACER("TQManageQueue::getNextType()");
314
 
315
    if (!mEmitQueue)
316
        return ET_NONE;
317
 
318
    return mEmitQueue->etype;
319
}
320
 
321
bool TQManageQueue::isDeleted()
322
{
323
    DECL_TRACER("TQManageQueue::isDeleted()");
324
 
325
    if (!mEmitQueue)
326
        return false;
327
 
328
    return mEmitQueue->isDropped();
329
}
330
 
331
/**
332
 * This deletes the first occurance of the \p handle in the queue.
333
 *
334
 * @param handle    The handle number.
335
 * @return If the \p handle was found in the queue TRUE is returned.
336
 */
337
bool TQManageQueue::dropHandle(ulong handle)
338
{
339
    DECL_TRACER("TQManageQueue::dropHandle(ulong handle)");
340
 
341
    TQEmitQueue *prev, *next, *p = mEmitQueue;
342
    prev = next = nullptr;
343
 
344
    while (p)
345
    {
346
        next = p->next;
347
 
348
        if (p->handle == handle)
349
        {
350
            if (prev)
351
                prev->next = next;
352
 
353
            if (p == mEmitQueue)
354
                mEmitQueue = next;
355
 
356
            delete p;
357
            return true;
358
        }
359
 
360
        prev = p;
361
        p = p->next;
362
    }
363
 
364
    return false;
365
}
366
 
367
/**
368
 * This deletes the first occurance of the \p t in the queue.
369
 *
370
 * @param t     The type.
371
 * @return If \p t was found in the queue TRUE is returned.
372
 */
373
bool TQManageQueue::dropType(_EMIT_TYPE_t t)
374
{
375
    DECL_TRACER("TQManageQueue::dropType(_EMIT_TYPE_t t)");
376
 
377
    TQEmitQueue *prev, *next, *p = mEmitQueue;
378
    prev = next = nullptr;
379
 
380
    while (p)
381
    {
382
        next = p->next;
383
 
384
        if (p->etype == t)
385
        {
386
            if (prev)
387
                prev->next = next;
388
 
389
            if (p == mEmitQueue)
390
                mEmitQueue = next;
391
 
392
            delete p;
393
            return true;
394
        }
395
 
396
        prev = p;
397
        p = p->next;
398
    }
399
 
400
    return false;
401
}
402
 
403
void TQManageQueue::markDrop(ulong handle)
404
{
405
    DECL_TRACER("TQManageQueue::markDrop(ulong handle)");
406
 
407
    TQEmitQueue *eq = mEmitQueue;
408
 
409
    while (eq)
410
    {
411
        if (eq->handle == handle)
412
        {
413
            eq->markDropped();
414
            return;
415
        }
416
 
417
        eq = eq->next;
418
    }
419
}
420
 
421
TQEmitQueue *TQManageQueue::addEntity(_EMIT_TYPE_t etype)
422
{
423
    DECL_TRACER("TQManageQueue::addEntity(_EMIT_TYPE_t etype)");
424
 
425
    TQEmitQueue *eq;
426
 
427
    try
428
    {
429
        eq = new TQEmitQueue;
430
        eq->etype = etype;
431
 
432
        if (!mEmitQueue)
433
            mEmitQueue = eq;
434
        else
435
        {
436
            TQEmitQueue *p = mEmitQueue;
437
 
438
            while(p->next)
439
                p = p->next;
440
 
441
            p->next = eq;
442
        }
443
    }
444
    catch (std::exception& e)
445
    {
446
        MSG_ERROR("Memory error: " << e.what());
447
        return nullptr;
448
    }
449
 
450
    return eq;
451
}
452
 
453
/**
454
 * This is a kind of garbage collector. It scans the queue for duplicate
455
 * entries and removes the oldest ones. An entry is duplicate if it has the
456
 * same handle number and, to be sure, the same type.
457
 * This method makes sure, that the queue contains no more events than
458
 * necessary. This reduces the drawings on the surface and an object is drawn
459
 * only once after the application became active.
460
 */
461
void TQManageQueue::removeDuplicates()
462
{
463
    DECL_TRACER("TQManageQueue::removeDuplicates()");
464
 
465
    TQEmitQueue *prev, *eq = mEmitQueue;
466
    prev = nullptr;
467
    bool noInc = false;
468
 
469
    if (!eq)
470
        return;
471
 
472
    while(eq)
473
    {
474
        TQEmitQueue *p = mEmitQueue;
475
 
476
        while(p)
477
        {
478
            if (p == eq)
479
            {
480
                p = p->next;
481
                continue;
482
            }
483
 
484
            if (p->handle == eq->handle && p->etype == eq->etype)   // Remove the duplicate;
485
            {
486
                if (!prev)
487
                {
488
                    mEmitQueue = mEmitQueue->next;
489
                    delete eq;
490
                    eq = mEmitQueue;
491
                    noInc = true;
492
                }
493
                else
494
                {
495
                    prev->next = eq->next;
496
                    delete eq;
497
                    eq = prev;
498
                }
499
 
500
                break;
501
            }
502
 
503
            p = p->next;
504
        }
505
 
506
        if (noInc)
507
        {
508
            noInc = false;
509
            continue;
510
        }
511
 
512
        prev = eq;
513
        eq = eq->next;
514
    }
515
}