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
 
129
void TQManageQueue::addInText(ulong handle, Button::TButton* button, Button::BITMAP_t bm)
130
{
131
    DECL_TRACER("TQManageQueue::addInText(ulong handle, Button::TButton* button, Button::BITMAP_t bm)");
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;
141
    removeDuplicates();
142
}
143
 
144
bool TQManageQueue::getInText(ulong* handle, Button::TButton ** button, Button::BITMAP_t* bm)
145
{
146
    DECL_TRACER("TQManageQueue::getInText(ulong* handle, Button::TButton ** button, Button::BITMAP_t* bm)");
147
 
148
    TQEmitQueue *eq = mEmitQueue;
149
 
150
    while(eq)
151
    {
152
        if (eq->etype == ET_INTEXT)
153
        {
154
            *handle = eq->handle;
155
            *button = eq->button;
156
            *bm = eq->bm;
157
            return true;
158
        }
159
    }
160
 
161
    return false;
162
}
163
 
164
void TQManageQueue::addPage(ulong handle, int width, int height)
165
{
166
    DECL_TRACER("TQManageQueue::addPage(ulong handle, int width, int height)");
167
 
168
    TQEmitQueue *eq = addEntity(ET_PAGE);
169
 
170
    if (!eq)
171
        return;
172
 
173
    eq->handle = handle;
174
    eq->width = width;
175
    eq->height = height;
176
    removeDuplicates();
177
}
178
 
179
bool TQManageQueue::getPage(ulong* handle, int* width, int* height)
180
{
181
    DECL_TRACER("TQManageQueue::getPage(ulong* handle, int* width, int* height)");
182
 
183
    TQEmitQueue *eq = mEmitQueue;
184
 
185
    while(eq)
186
    {
187
        if (eq->etype == ET_PAGE)
188
        {
189
            *handle = eq->handle;
190
            *width = eq->width;
191
            *height = eq->height;
192
            return true;
193
        }
194
    }
195
 
196
    return false;
197
}
198
 
199
void TQManageQueue::addSubPage(ulong handle, int left, int top, int width, int height, ANIMATION_t anim)
200
{
201
    DECL_TRACER("TQManageQueue::addSubPage(ulong handle, int left, int top, int width, int height, ANIMATION_t anim)");
202
 
203
    TQEmitQueue *eq = addEntity(ET_SUBPAGE);
204
 
205
    if (!eq)
206
        return;
207
 
208
    eq->handle = handle;
209
    eq->left = left;
210
    eq->top = top;
211
    eq->width = width;
212
    eq->height = height;
213
    eq->animate = anim;
214
    removeDuplicates();
215
}
216
 
217
bool TQManageQueue::getSubPage(ulong* handle, int* left, int* top, int* width, int* height, ANIMATION_t* anim)
218
{
219
    DECL_TRACER("TQManageQueue::getSubPage(ulong* handle, int* left, int* top, int* width, int* height, ANIMATION_t* anim)");
220
 
221
    TQEmitQueue *eq = mEmitQueue;
222
 
223
    while(eq)
224
    {
225
        if (eq->etype == ET_SUBPAGE)
226
        {
227
            *handle = eq->handle;
228
            *left = eq->left;
229
            *top = eq->top;
230
            *width = eq->width;
231
            *height = eq->height;
232
            *anim = eq->animate;
233
            return true;
234
        }
235
 
236
        eq = eq->next;
237
    }
238
 
239
    return false;
240
}
241
 
242
void TQManageQueue::addVideo(ulong handle, ulong parent, ulong left, ulong top, ulong width, ulong height, std::string url, std::string user, std::string pw)
243
{
244
    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)");
245
 
246
    TQEmitQueue *eq = addEntity(ET_VIDEO);
247
 
248
    if (!eq)
249
        return;
250
 
251
    eq->handle = handle;
252
    eq->parent = parent;
253
    eq->left = left;
254
    eq->top = top;
255
    eq->width = width;
256
    eq->height = height;
257
    eq->url = url;
258
    eq->user = user;
259
    eq->pw = pw;
260
    removeDuplicates();
261
}
262
 
263
bool TQManageQueue::getVideo(ulong* handle, ulong* parent, int* left, int* top, int* width, int* height, std::string* url, std::string* user, std::string* pw)
264
{
265
    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)");
266
 
267
    TQEmitQueue *eq = mEmitQueue;
268
 
269
    while(eq)
270
    {
271
        if (eq->etype == ET_VIDEO)
272
        {
273
            *handle = eq->handle;
274
            *parent = eq->parent;
275
            *left = eq->left;
276
            *top = eq->top;
277
            *width = eq->width;
278
            *height = eq->height;
279
            *url = eq->url;
280
            *user = eq->user;
281
            *pw = eq->pw;
282
            return true;
283
        }
284
 
285
        eq = eq->next;
286
    }
287
 
288
    return false;
289
}
290
 
291
_EMIT_TYPE_t TQManageQueue::getNextType()
292
{
293
    DECL_TRACER("TQManageQueue::getNextType()");
294
 
295
    if (!mEmitQueue)
296
        return ET_NONE;
297
 
298
    return mEmitQueue->etype;
299
}
300
 
301
bool TQManageQueue::isDeleted()
302
{
303
    DECL_TRACER("TQManageQueue::isDeleted()");
304
 
305
    if (!mEmitQueue)
306
        return false;
307
 
308
    return mEmitQueue->isDropped();
309
}
310
 
311
/**
312
 * This deletes the first occurance of the \p handle in the queue.
313
 *
314
 * @param handle    The handle number.
315
 * @return If the \p handle was found in the queue TRUE is returned.
316
 */
317
bool TQManageQueue::dropHandle(ulong handle)
318
{
319
    DECL_TRACER("TQManageQueue::dropHandle(ulong handle)");
320
 
321
    TQEmitQueue *prev, *next, *p = mEmitQueue;
322
    prev = next = nullptr;
323
 
324
    while (p)
325
    {
326
        next = p->next;
327
 
328
        if (p->handle == handle)
329
        {
330
            if (prev)
331
                prev->next = next;
332
 
333
            if (p == mEmitQueue)
334
                mEmitQueue = next;
335
 
336
            delete p;
337
            return true;
338
        }
339
 
340
        prev = p;
341
        p = p->next;
342
    }
343
 
344
    return false;
345
}
346
 
347
/**
348
 * This deletes the first occurance of the \p t in the queue.
349
 *
350
 * @param t     The type.
351
 * @return If \p t was found in the queue TRUE is returned.
352
 */
353
bool TQManageQueue::dropType(_EMIT_TYPE_t t)
354
{
355
    DECL_TRACER("TQManageQueue::dropType(_EMIT_TYPE_t t)");
356
 
357
    TQEmitQueue *prev, *next, *p = mEmitQueue;
358
    prev = next = nullptr;
359
 
360
    while (p)
361
    {
362
        next = p->next;
363
 
364
        if (p->etype == t)
365
        {
366
            if (prev)
367
                prev->next = next;
368
 
369
            if (p == mEmitQueue)
370
                mEmitQueue = next;
371
 
372
            delete p;
373
            return true;
374
        }
375
 
376
        prev = p;
377
        p = p->next;
378
    }
379
 
380
    return false;
381
}
382
 
383
void TQManageQueue::markDrop(ulong handle)
384
{
385
    DECL_TRACER("TQManageQueue::markDrop(ulong handle)");
386
 
387
    TQEmitQueue *eq = mEmitQueue;
388
 
389
    while (eq)
390
    {
391
        if (eq->handle == handle)
392
        {
393
            eq->markDropped();
394
            return;
395
        }
396
 
397
        eq = eq->next;
398
    }
399
}
400
 
401
TQEmitQueue *TQManageQueue::addEntity(_EMIT_TYPE_t etype)
402
{
403
    DECL_TRACER("TQManageQueue::addEntity(_EMIT_TYPE_t etype)");
404
 
405
    TQEmitQueue *eq;
406
 
407
    try
408
    {
409
        eq = new TQEmitQueue;
410
        eq->etype = etype;
411
 
412
        if (!mEmitQueue)
413
            mEmitQueue = eq;
414
        else
415
        {
416
            TQEmitQueue *p = mEmitQueue;
417
 
418
            while(p->next)
419
                p = p->next;
420
 
421
            p->next = eq;
422
        }
423
    }
424
    catch (std::exception& e)
425
    {
426
        MSG_ERROR("Memory error: " << e.what());
427
        return nullptr;
428
    }
429
 
430
    return eq;
431
}
432
 
433
/**
434
 * This is a kind of garbage collector. It scans the queue for duplicate
435
 * entries and removes the oldest ones. An entry is duplicate if it has the
436
 * same handle number and, to be sure, the same type.
437
 * This method makes sure, that the queue contains no more events than
438
 * necessary. This reduces the drawings on the surface and an object is drawn
439
 * only once after the application became active.
440
 */
441
void TQManageQueue::removeDuplicates()
442
{
443
    DECL_TRACER("TQManageQueue::removeDuplicates()");
444
 
445
    TQEmitQueue *prev, *eq = mEmitQueue;
446
    prev = nullptr;
447
    bool noInc = false;
448
 
449
    if (!eq)
450
        return;
451
 
452
    while(eq)
453
    {
454
        TQEmitQueue *p = mEmitQueue;
455
 
456
        while(p)
457
        {
458
            if (p == eq)
459
            {
460
                p = p->next;
461
                continue;
462
            }
463
 
464
            if (p->handle == eq->handle && p->etype == eq->etype)   // Remove the duplicate;
465
            {
466
                if (!prev)
467
                {
468
                    mEmitQueue = mEmitQueue->next;
469
                    delete eq;
470
                    eq = mEmitQueue;
471
                    noInc = true;
472
                }
473
                else
474
                {
475
                    prev->next = eq->next;
476
                    delete eq;
477
                    eq = prev;
478
                }
479
 
480
                break;
481
            }
482
 
483
            p = p->next;
484
        }
485
 
486
        if (noInc)
487
        {
488
            noInc = false;
489
            continue;
490
        }
491
 
492
        prev = eq;
493
        eq = eq->next;
494
    }
495
}