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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
|
/**
** TQt->glib main event loop integration by Norbert Frese 2005
** code based on qeventloop_x11.cpp 3.3.5
**
*/
/****************************************************************************
** $Id: qt/qeventloop_x11_glib.cpp
**
** Implementation of TQEventLoop class
**
** Copyright (C) 2000-2005 Trolltech AS. All rights reserved.
**
** This file is part of the kernel module of the TQt GUI Toolkit.
**
** This file may be distributed under the terms of the Q Public License
** as defined by Trolltech AS of Norway and appearing in the file
** LICENSE.TQPL included in the packaging of this file.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** Licensees holding valid TQt Enterprise Edition or TQt Professional Edition
** licenses for Unix/X11 may use this file in accordance with the TQt Commercial
** License Agreement provided with the Software.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
** information about TQt Commercial License Agreements.
** See http://www.trolltech.com/qpl/ for TQPL licensing information.
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
#include "qeventloop_glib_p.h" // includes qplatformdefs.h
#include "ntqeventloop.h"
#include "ntqapplication.h"
#include "ntqbitarray.h"
#include "qcolor_p.h"
#include "qt_x11_p.h"
#if defined(QT_THREAD_SUPPORT)
# include "ntqmutex.h"
# include "ntqthread.h"
#endif // QT_THREAD_SUPPORT
#include <errno.h>
#include <glib.h>
// #define DEBUG_QT_GLIBMAINLOOP 1
// TQt-GSource Structure and Callbacks
typedef struct {
GSource source;
TQEventLoop * qeventLoop;
} TQtGSource;
static gboolean qt_gsource_prepare ( GSource *source, gint *timeout );
static gboolean qt_gsource_check ( GSource *source );
static gboolean qt_gsource_dispatch ( GSource *source, GSourceFunc callback, gpointer user_data );
static GSourceFuncs qt_gsource_funcs = {
qt_gsource_prepare,
qt_gsource_check,
qt_gsource_dispatch,
NULL,
NULL,
NULL
};
// forward main loop callbacks to TQEventLoop methods!
static gboolean qt_gsource_prepare ( GSource *source, gint *timeout )
{
TQtGSource * qtGSource = (TQtGSource*) source;
TQEventLoop* candidateEventLoop = qtGSource->qeventLoop;
TQEventLoop* activeThreadEventLoop = TQApplication::eventLoop();
if (candidateEventLoop == activeThreadEventLoop) {
return candidateEventLoop->gsourcePrepare(source, timeout);
}
else {
// Prepare failed
return FALSE;
}
}
static gboolean qt_gsource_check ( GSource *source )
{
TQtGSource * qtGSource = (TQtGSource*) source;
TQEventLoop* candidateEventLoop = qtGSource->qeventLoop;
TQEventLoop* activeThreadEventLoop = TQApplication::eventLoop();
if (candidateEventLoop == activeThreadEventLoop) {
return candidateEventLoop->gsourceCheck(source);
}
else {
// Check failed
return FALSE;
}
}
static gboolean qt_gsource_dispatch ( GSource *source, GSourceFunc callback, gpointer user_data )
{
Q_UNUSED(callback);
Q_UNUSED(user_data);
TQtGSource * qtGSource = (TQtGSource*) source;
TQEventLoop* candidateEventLoop = qtGSource->qeventLoop;
TQEventLoop* activeThreadEventLoop = TQApplication::eventLoop();
if (candidateEventLoop == activeThreadEventLoop) {
return candidateEventLoop->gsourceDispatch(source);
}
else {
// Dispatch failed
return FALSE;
}
}
// -------------------------------------------------
// resolve the conflict between X11's FocusIn and TQEvent::FocusIn
#undef FocusOut
#undef FocusIn
static const int XKeyPress = KeyPress;
static const int XKeyRelease = KeyRelease;
#undef KeyPress
#undef KeyRelease
// from qapplication.cpp
extern bool tqt_is_gui_used;
// from qeventloop_unix.cpp
extern timeval *qt_wait_timer();
extern void cleanupTimers();
// ### this needs to go away at some point...
typedef void (*VFPTR)();
typedef TQValueList<VFPTR> TQVFuncList;
void qt_install_preselect_handler( VFPTR );
void qt_remove_preselect_handler( VFPTR );
static TQVFuncList *qt_preselect_handler = 0;
void qt_install_postselect_handler( VFPTR );
void qt_remove_postselect_handler( VFPTR );
static TQVFuncList *qt_postselect_handler = 0;
void qt_install_preselect_handler( VFPTR handler )
{
if ( !qt_preselect_handler ) {
qt_preselect_handler = new TQVFuncList;
}
qt_preselect_handler->append( handler );
}
void qt_remove_preselect_handler( VFPTR handler )
{
if ( qt_preselect_handler ) {
TQVFuncList::Iterator it = qt_preselect_handler->find( handler );
if ( it != qt_preselect_handler->end() ) {
qt_preselect_handler->remove( it );
}
}
}
void qt_install_postselect_handler( VFPTR handler )
{
if ( !qt_postselect_handler ) {
qt_postselect_handler = new TQVFuncList;
}
qt_postselect_handler->prepend( handler );
}
void qt_remove_postselect_handler( VFPTR handler )
{
if ( qt_postselect_handler ) {
TQVFuncList::Iterator it = qt_postselect_handler->find( handler );
if ( it != qt_postselect_handler->end() ) {
qt_postselect_handler->remove( it );
}
}
}
void TQEventLoop::init()
{
// initialize ProcessEventFlags (all events & wait for more)
d->pev_flags = AllEvents | WaitForMore;
// initialize the common parts of the event loop
if (pipe( d->thread_pipe ) < 0) {
// Error!
}
fcntl(d->thread_pipe[0], F_SETFD, FD_CLOEXEC);
fcntl(d->thread_pipe[1], F_SETFD, FD_CLOEXEC);
// intitialize the X11 parts of the event loop
d->xfd = -1;
if ( tqt_is_gui_used && TQApplication::isGuiThread() ) {
d->xfd = XConnectionNumber( TQPaintDevice::x11AppDisplay() );
}
// new main context for thread
d->ctx = g_main_context_new();
g_main_context_push_thread_default(d->ctx);
d->ctx_is_default = true;
// new GSource
TQtGSource * qtGSource = (TQtGSource*) g_source_new(&qt_gsource_funcs, sizeof(TQtGSource));
g_source_set_can_recurse ((GSource*)qtGSource, TRUE);
qtGSource->qeventLoop = this;
// init main loop and attach gsource
#ifdef DEBUG_QT_GLIBMAINLOOP
printf("inside init(1)\n");
#endif
g_main_loop_new (d->ctx, 1);
g_source_attach( (GSource*)qtGSource, d->ctx );
d->gSource = (GSource*)qtGSource;
// poll for X11 events
if ( tqt_is_gui_used && TQApplication::isGuiThread() ) {
d->x_gPollFD.fd = d->xfd;
d->x_gPollFD.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
g_source_add_poll(d->gSource, &d->x_gPollFD);
}
// poll thread-pipe
d->threadPipe_gPollFD.fd = d->thread_pipe[0];
d->threadPipe_gPollFD.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
g_source_add_poll(d->gSource, &d->threadPipe_gPollFD);
#ifdef DEBUG_QT_GLIBMAINLOOP
printf("inside init(2)\n");
#endif
}
void TQEventLoop::cleanup()
{
// cleanup the common parts of the event loop
close( d->thread_pipe[0] );
close( d->thread_pipe[1] );
cleanupTimers();
// cleanup the X11 parts of the event loop
d->xfd = -1;
// unref the main context
g_main_context_unref(d->ctx);
// todo: destroy gsource
}
bool TQEventLoop::processEvents( ProcessEventsFlags flags )
{
#ifdef DEBUG_QT_GLIBMAINLOOP
printf("inside processEvents(1) looplevel=%d\n", d->looplevel );
#endif
ProcessEventsFlags save_flags;
int rval;
save_flags = d->pev_flags;
d->pev_flags = flags;
rval = g_main_context_iteration(d->ctx, flags & WaitForMore ? TRUE : FALSE);
d->pev_flags = save_flags;
#ifdef DEBUG_QT_GLIBMAINLOOP
printf("inside processEvents(2) looplevel=%d rval=%d\n", d->looplevel, rval );
#endif
return rval; // were events processed?
}
bool TQEventLoop::processX11Events()
{
ProcessEventsFlags flags = d->pev_flags;
// process events from the X server
XEvent event;
int nevents = 0;
#if defined(QT_THREAD_SUPPORT)
TQMutexLocker locker( TQApplication::tqt_mutex );
#endif
// handle gui and posted events
if ( tqt_is_gui_used ) {
TQApplication::sendPostedEvents();
if (TQApplication::isGuiThread()) {
// Two loops so that posted events accumulate
while ( XPending( TQPaintDevice::x11AppDisplay() ) ) {
// also flushes output buffer
while ( XPending( TQPaintDevice::x11AppDisplay() ) ) {
if ( d->shortcut ) {
return FALSE;
}
XNextEvent( TQPaintDevice::x11AppDisplay(), &event );
if ( flags & ExcludeUserInput ) {
switch ( event.type ) {
case ButtonPress:
case ButtonRelease:
case MotionNotify:
case XKeyPress:
case XKeyRelease:
case EnterNotify:
case LeaveNotify:
continue;
case ClientMessage:
{
// from qapplication_x11.cpp
extern Atom tqt_wm_protocols;
extern Atom tqt_wm_take_focus;
extern Atom qt_qt_scrolldone;
// only keep the wm_take_focus and
// qt_qt_scrolldone protocols, discard all
// other client messages
if ( event.xclient.format != 32 )
continue;
if ( event.xclient.message_type == tqt_wm_protocols ||
(Atom) event.xclient.data.l[0] == tqt_wm_take_focus )
break;
if ( event.xclient.message_type == qt_qt_scrolldone )
break;
}
default: break;
}
}
nevents++;
if ( tqApp->x11ProcessEvent( &event ) == 1 ) {
return TRUE;
}
}
}
}
}
if ( d->shortcut ) {
return FALSE;
}
TQApplication::sendPostedEvents();
const uint exclude_all = ExcludeSocketNotifiers | 0x08;
// 0x08 == ExcludeTimers for X11 only
if ( nevents > 0 && ( flags & exclude_all ) == exclude_all && ( flags & WaitForMore ) ) {
return TRUE;
}
return FALSE;
}
bool TQEventLoop::gsourcePrepare(GSource *gs, int * timeout)
{
Q_UNUSED(gs);
#ifdef DEBUG_QT_GLIBMAINLOOP
printf("inside gsourcePrepare(1)\n");
#endif
ProcessEventsFlags flags = d->pev_flags;
#if defined(QT_THREAD_SUPPORT)
TQMutexLocker locker( TQApplication::tqt_mutex );
#endif
// don't block if exitLoop() or exit()/quit() has been called.
bool canWait = d->exitloop || d->quitnow ? FALSE : (flags & WaitForMore);
// Process timers and socket notifiers - the common UNIX stuff
// return the maximum time we can wait for an event.
static timeval zerotm;
timeval *tm = 0;
if ( ! ( flags & 0x08 ) ) { // 0x08 == ExcludeTimers for X11 only
tm = qt_wait_timer(); // wait for timer or X event
if ( !canWait ) {
if ( !tm ) {
tm = &zerotm;
}
tm->tv_sec = 0; // no time to wait
tm->tv_usec = 0;
}
}
// include or exclude SocketNotifiers (by setting or cleaning poll events)
if ( ! ( flags & ExcludeSocketNotifiers ) ) {
TQPtrListIterator<TQSockNotGPollFD> it( d->sn_list );
TQSockNotGPollFD *sn;
while ( (sn=it.current()) ) {
++it;
sn->gPollFD.events = sn->events; // restore poll events
}
}
else {
TQPtrListIterator<TQSockNotGPollFD> it( d->sn_list );
TQSockNotGPollFD *sn;
while ( (sn=it.current()) ) {
++it;
sn->gPollFD.events = 0; // delete poll events
}
}
#ifdef DEBUG_QT_GLIBMAINLOOP
printf("inside gsourcePrepare(2) canwait=%d\n", canWait);
#endif
if ( canWait ) {
emit aboutToBlock();
}
if ( qt_preselect_handler ) {
TQVFuncList::Iterator it, end = qt_preselect_handler->end();
for ( it = qt_preselect_handler->begin(); it != end; ++it )
(**it)();
}
#ifdef DEBUG_QT_GLIBMAINLOOP
printf("inside gsourcePrepare(2.1) canwait=%d\n", canWait);
#endif
// do we have to dispatch events?
if (hasPendingEvents()) {
*timeout = 0; // no time to stay in poll
#ifdef DEBUG_QT_GLIBMAINLOOP
printf("inside gsourcePrepare(3a)\n");
#endif
return FALSE;
}
// stay in poll until something happens?
if (!tm) { // fixme
*timeout = -1; // wait forever
#ifdef DEBUG_QT_GLIBMAINLOOP
printf("inside gsourcePrepare(3b) timeout=%d \n", *timeout);
#endif
return FALSE;
}
// else timeout >=0
*timeout = tm->tv_sec * 1000 + tm->tv_usec/1000;
#ifdef DEBUG_QT_GLIBMAINLOOP
printf("inside gsourcePrepare(3c) timeout=%d \n", *timeout);
#endif
return FALSE;
}
bool TQEventLoop::gsourceCheck(GSource *gs) {
Q_UNUSED(gs);
#ifdef DEBUG_QT_GLIBMAINLOOP
printf("inside gsourceCheck(1)\n");
#endif
// Socketnotifier events?
TQPtrList<TQSockNotGPollFD> *list = &d->sn_list;
//if ( list ) {
TQSockNotGPollFD *sn = list->first();
while ( sn ) {
if ( sn->gPollFD.revents )
return TRUE;
sn = list->next();
}
//}
if (d->x_gPollFD.revents) {
#ifdef DEBUG_QT_GLIBMAINLOOP
printf("inside gsourceCheck(2) xfd!\n");
#endif
return TRUE; // we got events!
}
if (d->threadPipe_gPollFD.revents) {
#ifdef DEBUG_QT_GLIBMAINLOOP
printf("inside gsourceCheck(2) threadpipe!!\n");
#endif
return TRUE; // we got events!
}
if (hasPendingEvents()) {
#ifdef DEBUG_QT_GLIBMAINLOOP
printf("inside gsourceCheck(2) pendingEvents!\n");
#endif
return TRUE; // we got more X11 events!
}
// check if we have timers to activate?
timeval * tm =qt_wait_timer();
if (tm && (tm->tv_sec == 0 && tm->tv_usec == 0 )) {
#ifdef DEBUG_QT_GLIBMAINLOOP
printf("inside gsourceCheck(2) qtwaittimer!\n");
#endif
return TRUE;
}
// nothing to dispatch
#ifdef DEBUG_QT_GLIBMAINLOOP
printf("inside gsourceCheck(2) nothing to dispatch!\n");
#endif
return FALSE;
}
bool TQEventLoop::gsourceDispatch(GSource *gs) {
Q_UNUSED(gs);
// relock the GUI mutex before processing any pending events
#if defined(QT_THREAD_SUPPORT)
TQMutexLocker locker( TQApplication::tqt_mutex );
#endif
#if defined(QT_THREAD_SUPPORT)
if (locker.mutex()) locker.mutex()->lock();
#endif
int nevents=0;
ProcessEventsFlags flags = d->pev_flags;
#ifdef DEBUG_QT_GLIBMAINLOOP
printf("inside gsourceDispatch(1)\n");
#endif
// we are awake, broadcast it
emit awake();
emit tqApp->guiThreadAwake();
// some other thread woke us up... consume the data on the thread pipe so that
// select doesn't immediately return next time
if ( d->threadPipe_gPollFD.revents) {
char c;
if (::read( d->thread_pipe[0], &c, 1 ) < 0) {
// Error!
}
}
if ( qt_postselect_handler ) {
TQVFuncList::Iterator it, end = qt_postselect_handler->end();
for ( it = qt_postselect_handler->begin(); it != end; ++it )
(**it)();
}
// activate socket notifiers
if ( ! ( flags & ExcludeSocketNotifiers )) {
// if select says data is ready on any socket, then set the socket notifier
// to pending
// if ( &d->sn_list ) {
TQPtrList<TQSockNotGPollFD> *list = &d->sn_list;
TQSockNotGPollFD *sn = list->first();
while ( sn ) {
if ( sn->gPollFD.revents )
setSocketNotifierPending( sn->obj );
sn = list->next();
}
// }
nevents += activateSocketNotifiers();
}
// activate timers
if ( ! ( flags & 0x08 ) ) {
// 0x08 == ExcludeTimers for X11 only
nevents += activateTimers();
}
// return true if we handled events, false otherwise
//return (nevents > 0);
// now process x11 events!
#ifdef DEBUG_QT_GLIBMAINLOOP
printf("inside gsourceDispatch(2) hasPendingEvents=%d\n", hasPendingEvents());
#endif
if (hasPendingEvents()) {
// color approx. optimization - only on X11
qt_reset_color_avail();
#if defined(QT_THREAD_SUPPORT)
if (locker.mutex()) locker.mutex()->unlock();
#endif
processX11Events();
}
else {
#if defined(QT_THREAD_SUPPORT)
if (locker.mutex()) locker.mutex()->unlock();
#endif
}
if (d->singletoolkit) {
return TRUE; // Eat the event
}
else {
return FALSE; // Pass the event on to GTK
}
}
bool TQEventLoop::hasPendingEvents() const
{
extern uint qGlobalPostedEventsCount(); // from qapplication.cpp
return ( qGlobalPostedEventsCount() || ( (tqt_is_gui_used && TQApplication::isGuiThread()) ? XPending( TQPaintDevice::x11AppDisplay() ) : 0));
}
void TQEventLoop::appStartingUp()
{
if ( tqt_is_gui_used ) {
d->xfd = XConnectionNumber( TQPaintDevice::x11AppDisplay() );
if ( (d->x_gPollFD.fd == -1) && TQApplication::isGuiThread() ) {
d->x_gPollFD.fd = d->xfd;
d->x_gPollFD.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
g_source_add_poll(d->gSource, &d->x_gPollFD);
}
}
}
void TQEventLoop::appClosingDown()
{
d->xfd = -1;
}
void TQEventLoop::setSingleToolkitEventHandling(bool enabled) {
d->singletoolkit = enabled;
if (!d->singletoolkit) {
if (d->ctx_is_default) {
d->ctx_is_default = false;
g_main_context_pop_thread_default(d->ctx);
}
}
else {
if (!d->ctx_is_default) {
g_main_context_push_thread_default(d->ctx);
d->ctx_is_default = true;
}
}
}
|