2 /* Copyright (c) Mark J. Kilgard, 1994, 1995, 1996, 1997, 1998. */
4 /* This program is freely distributable without licensing fees
5 and is provided without guarantee or warrantee expressed or
6 implied. This program is -not- in the public domain. */
9 #include <GL/vms_x_fix.h>
16 #include <string.h> /* Some FD_ZERO macros use memset without
17 prototyping memset. */
19 /* Much of the following #ifdef logic to include the proper
20 prototypes for the select system call is based on logic
21 from the X11R6.3 version of <X11/Xpoll.h>. */
25 # include <bstring.h> /* prototype for bzero used by FD_ZERO */
27 # if (defined(SVR4) || defined(CRAY) || defined(AIXV3)) && !defined(FD_SETSIZE)
28 # include <sys/select.h> /* select system call interface */
33 /* AIX 4.2 fubar-ed <sys/select.h>, so go to heroic measures to get it */
34 # if defined(AIXV4) && !defined(NFDBITS)
35 # include <sys/select.h>
39 #include <sys/types.h>
42 # if defined(__vms) && ( __VMS_VER < 70000000 )
43 # include <sys/time.h>
46 # include <sys/time.h>
50 # include <X11/Xlib.h>
51 # include <X11/keysym.h>
54 # include <sys/time.h>
56 # include <sys/timeb.h>
59 /* XXX Bert Gijsbers <bert@mc.bio.uva.nl> reports that HP-UX
60 needs different keysyms for the End, Insert, and Delete keys
61 to work on an HP 715. It would be better if HP generated
62 standard keysyms for standard keys. */
63 # include <X11/HPkeysym.h>
69 #if defined(__vms) && ( __VMS_VER < 70000000 )
72 extern int SYS$
CLREF(int efn
);
73 extern int SYS$
SETIMR(unsigned int efn
, struct timeval6
*timeout
, void *ast
,
74 unsigned int request_id
, unsigned int flags
);
75 extern int SYS$
WFLOR(unsigned int efn
, unsigned int mask
);
76 extern int SYS$
CANTIM(unsigned int request_id
, unsigned int mode
);
77 #endif /* __vms, VMs 6.2 or earlier */
79 static GLUTtimer
*freeTimerList
= NULL
;
81 GLUTidleCB __glutIdleFunc
= NULL
;
82 GLUTtimer
*__glutTimerList
= NULL
;
83 #ifdef SUPPORT_FORTRAN
84 GLUTtimer
*__glutNewTimer
;
86 GLUTwindow
*__glutWindowWorkList
= NULL
;
87 GLUTmenu
*__glutMappedMenu
;
88 GLUTmenu
*__glutCurrentMenu
= NULL
;
90 void (*__glutUpdateInputDeviceMaskFunc
) (GLUTwindow
*);
92 void (*__glutMenuItemEnterOrLeave
)(GLUTmenuItem
* item
, int num
, int type
) = NULL
;
93 void (*__glutFinishMenu
)(Window win
, int x
, int y
);
94 void (*__glutPaintMenu
)(GLUTmenu
* menu
);
95 void (*__glutStartMenu
)(GLUTmenu
* menu
, GLUTwindow
* window
, int x
, int y
, int x_win
, int y_win
);
96 GLUTmenu
* (*__glutGetMenuByNum
)(int menunum
);
97 GLUTmenuItem
* (*__glutGetMenuItem
)(GLUTmenu
* menu
, Window win
, int *which
);
98 GLUTmenu
* (*__glutGetMenu
)(Window win
);
101 Atom __glutMotifHints
= None
;
102 /* Modifier mask of ~0 implies not in core input callback. */
103 unsigned int __glutModifierMask
= (unsigned int) ~0;
104 int __glutWindowDamaged
= 0;
107 glutIdleFunc(GLUTidleCB idleFunc
)
109 __glutIdleFunc
= idleFunc
;
113 glutTimerFunc(unsigned int interval
, GLUTtimerCB timerFunc
, int value
)
115 GLUTtimer
*timer
, *other
;
127 timer
= freeTimerList
;
128 freeTimerList
= timer
->next
;
130 timer
= (GLUTtimer
*) malloc(sizeof(GLUTtimer
));
132 __glutFatalError("out of memory.");
135 timer
->func
= timerFunc
;
136 #if defined(__vms) && ( __VMS_VER < 70000000 )
137 /* VMS time is expressed in units of 100 ns */
138 timer
->timeout
.val
= interval
* TICKS_PER_MILLISECOND
;
140 timer
->timeout
.tv_sec
= (int) interval
/ 1000;
141 timer
->timeout
.tv_usec
= (int) (interval
% 1000) * 1000;
143 timer
->value
= value
;
146 ADD_TIME(timer
->timeout
, timer
->timeout
, now
);
147 prevptr
= &__glutTimerList
;
149 while (other
&& IS_AFTER(other
->timeout
, timer
->timeout
)) {
150 prevptr
= &other
->next
;
154 #ifdef SUPPORT_FORTRAN
155 __glutNewTimer
= timer
; /* for Fortran binding! */
170 /* Assumption is that __glutTimerList is already determined
173 while (IS_AT_OR_AFTER(__glutTimerList
->timeout
, now
)) {
174 timer
= __glutTimerList
;
175 __glutTimerList
= timer
->next
;
176 timer
->func(timer
->value
);
177 timer
->next
= freeTimerList
;
178 freeTimerList
= timer
;
179 if (!__glutTimerList
)
185 __glutPutOnWorkList(GLUTwindow
* window
, int workMask
)
187 if (window
->workMask
) {
188 /* Already on list; just OR in new workMask. */
189 window
->workMask
|= workMask
;
191 /* Update work mask and add to window work list. */
192 window
->workMask
= workMask
;
193 /* Assert that if the window does not have a
194 workMask already, the window should definitely
195 not be the head of the work list. */
196 assert(window
!= __glutWindowWorkList
);
197 window
->prevWorkWin
= __glutWindowWorkList
;
198 __glutWindowWorkList
= window
;
203 __glutPostRedisplay(GLUTwindow
* window
, int layerMask
)
205 int shown
= (layerMask
& (GLUT_REDISPLAY_WORK
| GLUT_REPAIR_WORK
)) ?
206 window
->shownState
: window
->overlay
->shownState
;
208 /* Post a redisplay if the window is visible (or the
209 visibility of the window is unknown, ie. window->visState
210 == -1) _and_ the layer is known to be shown. */
211 if (window
->visState
!= GLUT_HIDDEN
212 && window
->visState
!= GLUT_FULLY_COVERED
&& shown
) {
213 __glutPutOnWorkList(window
, layerMask
);
219 glutPostRedisplay(void)
221 __glutPostRedisplay(__glutCurrentWindow
, GLUT_REDISPLAY_WORK
);
224 /* The advantage of this routine is that it saves the cost of a
225 glutSetWindow call (entailing an expensive OpenGL context switch),
226 particularly useful when multiple windows need redisplays posted at
227 the same times. See also glutPostWindowOverlayRedisplay. */
229 glutPostWindowRedisplay(int win
)
231 __glutPostRedisplay(__glutWindowList
[win
- 1], GLUT_REDISPLAY_WORK
);
235 static GLUTeventParser
*eventParserList
= NULL
;
237 /* __glutRegisterEventParser allows another module to register
238 to intercept X events types not otherwise acted on by the
239 GLUT processEventsAndTimeouts routine. The X Input
240 extension support code uses an event parser for handling X
241 Input extension events. */
244 __glutRegisterEventParser(GLUTeventParser
* parser
)
246 parser
->next
= eventParserList
;
247 eventParserList
= parser
;
251 markWindowHidden(GLUTwindow
* window
)
253 if (GLUT_HIDDEN
!= window
->visState
) {
256 if (window
->windowStatus
) {
257 window
->visState
= GLUT_HIDDEN
;
258 __glutSetWindow(window
);
259 window
->windowStatus(GLUT_HIDDEN
);
261 /* An unmap is only reported on a single window; its
262 descendents need to know they are no longer visible. */
263 child
= window
->children
;
265 markWindowHidden(child
);
266 child
= child
->siblings
;
274 purgeStaleWindow(Window win
)
276 GLUTstale
**pEntry
= &__glutStaleWindowList
;
277 GLUTstale
*entry
= __glutStaleWindowList
;
279 /* Tranverse singly-linked stale window list look for the
282 if (entry
->win
== win
) {
283 /* Found it; delete it. */
284 *pEntry
= entry
->next
;
288 pEntry
= &entry
->next
;
294 /* Unlike XNextEvent, if a signal arrives,
295 interruptibleXNextEvent will return (with a zero return
296 value). This helps GLUT drop out of XNextEvent if a signal
297 is delivered. The intent is so that a GLUT program can call
298 glutIdleFunc in a signal handler to register an idle func
299 and then immediately get dropped into the idle func (after
300 returning from the signal handler). The idea is to make
301 GLUT's main loop reliably interruptible by signals. */
303 interruptibleXNextEvent(Display
* dpy
, XEvent
* event
)
308 /* Flush X protocol since XPending does not do this
310 XFlush(__glutDisplay
);
312 if (XPending(__glutDisplay
)) {
313 XNextEvent(dpy
, event
);
317 /* the combination ConectionNumber-select is buggy on VMS. Sometimes it
318 * fails. This part of the code hangs the program on VMS7.2. But even
319 * without it the program seems to run correctly.
320 * Note that this is a bug in the VMS/DECWindows run-time-libraries.
321 * Compaq engeneering does not want or is not able to make a fix.
322 * (last sentence is a quotation from Compaq when I reported the
323 * problem January 2000) */
325 FD_SET(__glutConnectionFD
, &fds
);
326 rc
= select(__glutConnectionFD
+ 1, &fds
, NULL
, NULL
, NULL
);
328 if (errno
== EINTR
) {
331 __glutFatalError("select error.");
341 processEventsAndTimeouts(void)
347 if(!GetMessage(&event
, NULL
, 0, 0)) /* bail if no more messages */
349 TranslateMessage(&event
); /* translate virtual-key messages */
350 DispatchMessage(&event
); /* call the window proc */
351 /* see win32_event.c for event (message) processing procedures */
353 static int mappedMenuButton
;
354 GLUTeventParser
*parser
;
357 GLUTkeyboardCB keyboard
;
358 GLUTspecialCB special
;
359 int gotEvent
, width
, height
;
361 gotEvent
= interruptibleXNextEvent(__glutDisplay
, &event
);
363 switch (event
.type
) {
365 XRefreshKeyboardMapping((XMappingEvent
*) & event
);
367 case ConfigureNotify
:
368 window
= __glutGetWindow(event
.xconfigure
.window
);
370 if (window
->win
!= event
.xconfigure
.window
) {
371 /* Ignore ConfigureNotify sent to the overlay
372 planes. GLUT could get here because overlays
373 select for StructureNotify events to receive
377 width
= event
.xconfigure
.width
;
378 height
= event
.xconfigure
.height
;
379 if (width
!= window
->width
|| height
!= window
->height
) {
380 if (window
->overlay
) {
381 XResizeWindow(__glutDisplay
, window
->overlay
->win
, width
, height
);
383 window
->width
= width
;
384 window
->height
= height
;
385 __glutSetWindow(window
);
386 /* Do not execute OpenGL out of sequence with
387 respect to the XResizeWindow request! */
389 window
->reshape(width
, height
);
390 window
->forceReshape
= False
;
391 /* A reshape should be considered like posting a
392 repair; this is necessary for the "Mesa
393 glXSwapBuffers to repair damage" hack to operate
394 correctly. Without it, there's not an initial
395 back buffer render from which to blit from when
396 damage happens to the window. */
397 __glutPostRedisplay(window
, GLUT_REPAIR_WORK
);
402 /* compress expose events */
403 while (XEventsQueued(__glutDisplay
, QueuedAfterReading
)
405 XPeekEvent(__glutDisplay
, &ahead
);
406 if (ahead
.type
!= Expose
||
407 ahead
.xexpose
.window
!= event
.xexpose
.window
) {
410 XNextEvent(__glutDisplay
, &event
);
412 if (event
.xexpose
.count
== 0) {
415 if (__glutMappedMenu
&&
416 (menu
= __glutGetMenu(event
.xexpose
.window
))) {
417 __glutPaintMenu(menu
);
419 window
= __glutGetWindow(event
.xexpose
.window
);
421 if (window
->win
== event
.xexpose
.window
) {
422 __glutPostRedisplay(window
, GLUT_REPAIR_WORK
);
423 } else if (window
->overlay
&& window
->overlay
->win
== event
.xexpose
.window
) {
424 __glutPostRedisplay(window
, GLUT_OVERLAY_REPAIR_WORK
);
429 /* there are more exposes to read; wait to redisplay */
434 if (__glutMappedMenu
&& event
.type
== ButtonRelease
435 && mappedMenuButton
== event
.xbutton
.button
) {
436 /* Menu is currently popped up and its button is
438 __glutFinishMenu(event
.xbutton
.window
, event
.xbutton
.x
, event
.xbutton
.y
);
440 window
= __glutGetWindow(event
.xbutton
.window
);
441 /* added button check for mice with > 3 buttons */
442 if (window
&& event
.xbutton
.button
<= GLUT_MAX_MENUS
) {
446 menuNum
= window
->menu
[event
.xbutton
.button
- 1];
447 /* Make sure that __glutGetMenuByNum is only called if there
448 really is a menu present. */
449 if ((menuNum
> 0) && (menu
= __glutGetMenuByNum(menuNum
))) {
450 if (event
.type
== ButtonPress
&& !__glutMappedMenu
) {
451 __glutStartMenu(menu
, window
,
452 event
.xbutton
.x_root
, event
.xbutton
.y_root
,
453 event
.xbutton
.x
, event
.xbutton
.y
);
454 mappedMenuButton
= event
.xbutton
.button
;
456 /* Ignore a release of a button with a menu
457 attatched to it when no menu is popped up,
458 or ignore a press when another menu is
459 already popped up. */
461 } else if (window
->mouse
) {
462 __glutSetWindow(window
);
463 __glutModifierMask
= event
.xbutton
.state
;
464 window
->mouse(event
.xbutton
.button
- 1,
465 event
.type
== ButtonRelease
?
467 event
.xbutton
.x
, event
.xbutton
.y
);
468 __glutModifierMask
= ~0;
470 /* Stray mouse events. Ignore. */
473 /* Window might have been destroyed and all the
474 events for the window may not yet be received. */
479 if (!__glutMappedMenu
) {
480 window
= __glutGetWindow(event
.xmotion
.window
);
482 /* If motion function registered _and_ buttons held
483 * down, call motion function... */
484 if (window
->motion
&& event
.xmotion
.state
&
485 (Button1Mask
| Button2Mask
| Button3Mask
)) {
486 __glutSetWindow(window
);
487 window
->motion(event
.xmotion
.x
, event
.xmotion
.y
);
489 /* If passive motion function registered _and_
490 buttons not held down, call passive motion
492 else if (window
->passive
&&
493 ((event
.xmotion
.state
&
494 (Button1Mask
| Button2Mask
| Button3Mask
)) ==
496 __glutSetWindow(window
);
497 window
->passive(event
.xmotion
.x
,
502 /* Motion events are thrown away when a pop up menu
508 window
= __glutGetWindow(event
.xkey
.window
);
512 if (event
.type
== KeyPress
) {
513 keyboard
= window
->keyboard
;
516 /* If we are ignoring auto repeated keys for this window,
517 check if the next event in the X event queue is a KeyPress
518 for the exact same key (and at the exact same time) as the
519 key being released. The X11 protocol will send auto
520 repeated keys as such KeyRelease/KeyPress pairs. */
522 if (window
->ignoreKeyRepeat
) {
523 if (XEventsQueued(__glutDisplay
, QueuedAfterReading
)) {
524 XPeekEvent(__glutDisplay
, &ahead
);
525 if (ahead
.type
== KeyPress
526 && ahead
.xkey
.window
== event
.xkey
.window
527 && ahead
.xkey
.keycode
== event
.xkey
.keycode
528 && ahead
.xkey
.time
== event
.xkey
.time
) {
529 /* Pop off the repeated KeyPress and ignore
530 the auto repeated KeyRelease/KeyPress pair. */
531 XNextEvent(__glutDisplay
, &event
);
536 keyboard
= window
->keyboardUp
;
542 rc
= XLookupString(&event
.xkey
, tmp
, sizeof(tmp
),
545 __glutSetWindow(window
);
546 __glutModifierMask
= event
.xkey
.state
;
548 event
.xkey
.x
, event
.xkey
.y
);
549 __glutModifierMask
= ~0;
553 if (event
.type
== KeyPress
) {
554 special
= window
->special
;
556 special
= window
->specialUp
;
562 /* Introduced in X11R6: (Partial list of) Keypad Functions. Define
563 in place in case compiling against an older pre-X11R6
564 X11/keysymdef.h file. */
566 #define XK_KP_Home 0xFF95
569 #define XK_KP_Left 0xFF96
572 #define XK_KP_Up 0xFF97
575 #define XK_KP_Right 0xFF98
578 #define XK_KP_Down 0xFF99
581 #define XK_KP_Prior 0xFF9A
584 #define XK_KP_Next 0xFF9B
587 #define XK_KP_End 0xFF9C
590 #define XK_KP_Insert 0xFF9E
593 #define XK_KP_Delete 0xFF9F
596 ks
= XLookupKeysym((XKeyEvent
*) & event
, 0);
597 /* XXX Verbose, but makes no assumptions about keysym
602 case XK_F1
: key
= GLUT_KEY_F1
; break;
603 case XK_F2
: key
= GLUT_KEY_F2
; break;
604 case XK_F3
: key
= GLUT_KEY_F3
; break;
605 case XK_F4
: key
= GLUT_KEY_F4
; break;
606 case XK_F5
: key
= GLUT_KEY_F5
; break;
607 case XK_F6
: key
= GLUT_KEY_F6
; break;
608 case XK_F7
: key
= GLUT_KEY_F7
; break;
609 case XK_F8
: key
= GLUT_KEY_F8
; break;
610 case XK_F9
: key
= GLUT_KEY_F9
; break;
611 case XK_F10
: key
= GLUT_KEY_F10
; break;
612 case XK_F11
: key
= GLUT_KEY_F11
; break;
613 case XK_F12
: key
= GLUT_KEY_F12
; break;
614 /* directional keys */
616 case XK_Left
: key
= GLUT_KEY_LEFT
; break;
617 case XK_KP_Up
: /* Introduced in X11R6. */
618 case XK_Up
: key
= GLUT_KEY_UP
; break;
619 case XK_KP_Right
: /* Introduced in X11R6. */
620 case XK_Right
: key
= GLUT_KEY_RIGHT
; break;
621 case XK_KP_Down
: /* Introduced in X11R6. */
622 case XK_Down
: key
= GLUT_KEY_DOWN
; break;
625 case XK_KP_Prior
: /* Introduced in X11R6. */
627 /* XK_Prior same as X11R6's XK_Page_Up */
628 key
= GLUT_KEY_PAGE_UP
;
630 case XK_KP_Next
: /* Introduced in X11R6. */
632 /* XK_Next same as X11R6's XK_Page_Down */
633 key
= GLUT_KEY_PAGE_DOWN
;
635 case XK_KP_Home
: /* Introduced in X11R6. */
642 case XK_KP_End
: /* Introduced in X11R6. */
649 case XK_KP_Insert
: /* Introduced in X11R6. */
651 key
= GLUT_KEY_INSERT
;
656 case XK_KP_Delete
: /* Introduced in X11R6. */
657 /* The Delete character is really an ASCII key. */
658 __glutSetWindow(window
);
659 keyboard(127, /* ASCII Delete character. */
660 event
.xkey
.x
, event
.xkey
.y
);
665 __glutSetWindow(window
);
666 __glutModifierMask
= event
.xkey
.state
;
667 special(key
, event
.xkey
.x
, event
.xkey
.y
);
668 __glutModifierMask
= ~0;
674 if (event
.xcrossing
.mode
!= NotifyNormal
||
675 event
.xcrossing
.detail
== NotifyNonlinearVirtual
||
676 event
.xcrossing
.detail
== NotifyVirtual
) {
678 /* Careful to ignore Enter/LeaveNotify events that
679 come from the pop-up menu pointer grab and ungrab.
680 Also, ignore "virtual" Enter/LeaveNotify events
681 since they represent the pointer passing through
682 the window hierarchy without actually entering or
683 leaving the actual real estate of a window. */
687 if (__glutMappedMenu
) {
691 item
= __glutGetMenuItem(__glutMappedMenu
,
692 event
.xcrossing
.window
, &num
);
694 __glutMenuItemEnterOrLeave(item
, num
, event
.type
);
698 window
= __glutGetWindow(event
.xcrossing
.window
);
701 if (event
.type
== EnterNotify
) {
703 /* With overlays established, X can report two
704 enter events for both the overlay and normal
705 plane window. Do not generate a second enter
706 callback if we reported one without an
707 intervening leave. */
709 if (window
->entryState
!= EnterNotify
) {
710 int num
= window
->num
;
711 Window xid
= window
->win
;
713 window
->entryState
= EnterNotify
;
714 __glutSetWindow(window
);
715 window
->entry(GLUT_ENTERED
);
717 if (__glutMappedMenu
) {
719 /* Do not generate any passive motion events
720 when menus are in use. */
724 /* An EnterNotify event can result in a
725 "compound" callback if a passive motion
726 callback is also registered. In this case,
727 be a little paranoid about the possibility
728 the window could have been destroyed in the
731 window
= __glutWindowList
[num
];
732 if (window
&& window
->passive
&& window
->win
== xid
) {
733 __glutSetWindow(window
);
734 window
->passive(event
.xcrossing
.x
, event
.xcrossing
.y
);
739 if (window
->entryState
!= LeaveNotify
) {
741 /* When an overlay is established for a window
742 already mapped and with the pointer in it,
743 the X server will generate a leave/enter
744 event pair as the pointer leaves (without
745 moving) from the normal plane X window to
746 the newly mapped overlay X window (or vice
747 versa). This enter/leave pair should not be
748 reported to the GLUT program since the pair
749 is a consequence of creating (or destroying)
750 the overlay, not an actual leave from the
753 if (XEventsQueued(__glutDisplay
, QueuedAfterReading
)) {
754 XPeekEvent(__glutDisplay
, &ahead
);
755 if (ahead
.type
== EnterNotify
&&
756 __glutGetWindow(ahead
.xcrossing
.window
) == window
) {
757 XNextEvent(__glutDisplay
, &event
);
761 window
->entryState
= LeaveNotify
;
762 __glutSetWindow(window
);
763 window
->entry(GLUT_LEFT
);
766 } else if (window
->passive
) {
767 __glutSetWindow(window
);
768 window
->passive(event
.xcrossing
.x
, event
.xcrossing
.y
);
773 /* MapNotify events are not needed to maintain
774 visibility state since VisibilityNotify events will
775 be delivered when a window becomes visible from
776 mapping. However, VisibilityNotify events are not
777 delivered when a window is unmapped (for the window
779 window
= __glutGetWindow(event
.xunmap
.window
);
781 if (window
->win
!= event
.xconfigure
.window
) {
782 /* Ignore UnmapNotify sent to the overlay planes.
783 GLUT could get here because overlays select for
784 StructureNotify events to receive DestroyNotify.
788 markWindowHidden(window
);
791 case VisibilityNotify
:
792 window
= __glutGetWindow(event
.xvisibility
.window
);
794 /* VisibilityUnobscured+1 = GLUT_FULLY_RETAINED,
795 VisibilityPartiallyObscured+1 =
796 GLUT_PARTIALLY_RETAINED, VisibilityFullyObscured+1
797 = GLUT_FULLY_COVERED. */
798 int visState
= event
.xvisibility
.state
+ 1;
800 if (visState
!= window
->visState
) {
801 if (window
->windowStatus
) {
802 window
->visState
= visState
;
803 __glutSetWindow(window
);
804 window
->windowStatus(visState
);
810 if (event
.xclient
.data
.l
[0] == __glutWMDeleteWindow
)
814 purgeStaleWindow(event
.xdestroywindow
.window
);
816 case CirculateNotify
:
820 /* Uninteresting to GLUT (but possible for GLUT to
824 /* Pass events not directly handled by the GLUT main
825 event loop to any event parsers that have been
826 registered. In this way, X Input extension events
827 are passed to the correct handler without forcing
828 all GLUT programs to support X Input event handling.
830 parser
= eventParserList
;
832 if (parser
->func(&event
))
834 parser
= parser
->next
;
840 if (__glutTimerList
) {
844 while (XPending(__glutDisplay
));
848 waitForSomething(void)
850 #if defined(__vms) && ( __VMS_VER < 70000000 )
851 static struct timeval6 zerotime
=
853 unsigned int timer_efn
;
854 #define timer_id 'glut' /* random :-) number */
855 unsigned int wait_mask
;
857 static struct timeval zerotime
=
864 struct timeval6 now
, timeout
, waittime
;
866 struct timeval now
, timeout
, waittime
;
872 /* Flush X protocol since XPending does not do this
874 XFlush(__glutDisplay
);
875 if (XPending(__glutDisplay
)) {
876 /* It is possible (but quite rare) that XFlush may have
877 needed to wait for a writable X connection file
878 descriptor, and in the process, may have had to read off
879 X protocol from the file descriptor. If XPending is true,
880 this case occured and we should avoid waiting in select
881 since X protocol buffered within Xlib is due to be
882 processed and potentially no more X protocol is on the
883 file descriptor, so we would risk waiting improperly in
885 goto immediatelyHandleXinput
;
887 #if defined(__vms) && ( __VMS_VER < 70000000 )
888 timeout
= __glutTimerList
->timeout
;
890 wait_mask
= 1 << (__glutConnectionFD
& 31);
891 if (IS_AFTER(now
, timeout
)) {
892 /* We need an event flag for the timer. */
893 /* XXX The `right' way to do this is to use LIB$GET_EF, but
894 since it needs to be in the same cluster as the EFN for
895 the display, we will have hack it. */
896 timer_efn
= __glutConnectionFD
- 1;
897 if ((timer_efn
/ 32) != (__glutConnectionFD
/ 32)) {
898 timer_efn
= __glutConnectionFD
+ 1;
900 rc
= SYS$
CLREF(timer_efn
);
901 rc
= SYS$
SETIMR(timer_efn
, &timeout
, NULL
, timer_id
, 0);
902 wait_mask
|= 1 << (timer_efn
& 31);
906 rc
= SYS$
WFLOR(__glutConnectionFD
, wait_mask
);
907 if (timer_efn
!= 0 && SYS$
CLREF(timer_efn
) == SS$_WASCLR
) {
908 rc
= SYS$
CANTIM(timer_id
, PSL$C_USER
);
910 /* XXX There does not seem to be checking of "rc" in the code
911 above. Can any of the SYS$ routines above fail? */
912 #else /* not vms6.2 or lower */
915 FD_SET(__glutConnectionFD
, &fds
);
917 timeout
= __glutTimerList
->timeout
;
919 if (IS_AFTER(now
, timeout
)) {
920 TIMEDELTA(waittime
, timeout
, now
);
925 rc
= select(__glutConnectionFD
+ 1, &fds
,
926 NULL
, NULL
, &waittime
);
927 if (rc
< 0 && errno
!= EINTR
)
928 __glutFatalError("select error.");
931 MsgWaitForMultipleObjects(0, NULL
, FALSE
,
932 waittime
.tv_sec
*1000 + waittime
.tv_usec
/1000, QS_ALLINPUT
);
935 #endif /* not vms6.2 or lower */
936 /* Without considering the cause of select unblocking, check
937 for pending X events and handle any timeouts (by calling
938 processEventsAndTimeouts). We always look for X events
939 even if select returned with 0 (indicating a timeout);
940 otherwise we risk starving X event processing by continous
942 if (XPending(__glutDisplay
)) {
943 immediatelyHandleXinput
:
944 processEventsAndTimeouts();
954 if (XPending(__glutDisplay
)) {
955 processEventsAndTimeouts();
957 if (__glutTimerList
) {
961 /* Make sure idle func still exists! */
962 if (__glutIdleFunc
) {
967 static GLUTwindow
**beforeEnd
;
970 processWindowWorkList(GLUTwindow
* window
)
974 if (window
->prevWorkWin
) {
975 window
->prevWorkWin
= processWindowWorkList(window
->prevWorkWin
);
977 beforeEnd
= &window
->prevWorkWin
;
980 /* Capture work mask for work that needs to be done to this
981 window, then clear the window's work mask (excepting the
982 dummy work bit, see below). Then, process the captured
983 work mask. This allows callbacks in the processing the
984 captured work mask to set the window's work mask for
985 subsequent processing. */
987 workMask
= window
->workMask
;
988 assert((workMask
& GLUT_DUMMY_WORK
) == 0);
990 /* Set the dummy work bit, clearing all other bits, to
991 indicate that the window is currently on the window work
992 list _and_ that the window's work mask is currently being
993 processed. This convinces __glutPutOnWorkList that this
994 window is on the work list still. */
995 window
->workMask
= GLUT_DUMMY_WORK
;
997 /* Optimization: most of the time, the work to do is a
998 redisplay and not these other types of work. Check for
999 the following cases as a group to before checking each one
1000 individually one by one. This saves about 25 MIPS
1001 instructions in the common redisplay only case. */
1002 if (workMask
& (GLUT_EVENT_MASK_WORK
| GLUT_DEVICE_MASK_WORK
|
1003 GLUT_CONFIGURE_WORK
| GLUT_COLORMAP_WORK
| GLUT_MAP_WORK
)) {
1004 #if !defined(_WIN32)
1005 /* Be sure to set event mask BEFORE map window is done. */
1006 if (workMask
& GLUT_EVENT_MASK_WORK
) {
1009 /* Make sure children are not propogating events this
1010 window is selecting for. Be sure to do this before
1011 enabling events on the children's parent. */
1012 if (window
->children
) {
1013 GLUTwindow
*child
= window
->children
;
1014 unsigned long attribMask
= CWDontPropagate
;
1015 XSetWindowAttributes wa
;
1017 wa
.do_not_propagate_mask
= window
->eventMask
& GLUT_DONT_PROPAGATE_FILTER_MASK
;
1018 if (window
->eventMask
& GLUT_HACK_STOP_PROPAGATE_MASK
) {
1019 wa
.event_mask
= child
->eventMask
| (window
->eventMask
& GLUT_HACK_STOP_PROPAGATE_MASK
);
1020 attribMask
|= CWEventMask
;
1023 XChangeWindowAttributes(__glutDisplay
, child
->win
,
1025 child
= child
->siblings
;
1028 eventMask
= window
->eventMask
;
1029 if (window
->parent
&& window
->parent
->eventMask
& GLUT_HACK_STOP_PROPAGATE_MASK
)
1030 eventMask
|= (window
->parent
->eventMask
& GLUT_HACK_STOP_PROPAGATE_MASK
);
1031 XSelectInput(__glutDisplay
, window
->win
, eventMask
);
1032 if (window
->overlay
)
1033 XSelectInput(__glutDisplay
, window
->overlay
->win
,
1034 window
->eventMask
& GLUT_OVERLAY_EVENT_FILTER_MASK
);
1036 #endif /* !_WIN32 */
1037 /* Be sure to set device mask BEFORE map window is done. */
1038 if (workMask
& GLUT_DEVICE_MASK_WORK
) {
1039 __glutUpdateInputDeviceMaskFunc(window
);
1041 /* Be sure to configure window BEFORE map window is done. */
1042 if (workMask
& GLUT_CONFIGURE_WORK
) {
1046 UINT flags
= SWP_NOACTIVATE
| SWP_NOMOVE
| SWP_NOOWNERZORDER
1047 | SWP_NOSENDCHANGING
| SWP_NOSIZE
| SWP_NOZORDER
;
1049 GetClientRect(window
->win
, &changes
);
1051 /* If this window is a toplevel window, translate the 0,0 client
1052 coordinate into a screen coordinate for proper placement. */
1053 if (!window
->parent
) {
1056 ClientToScreen(window
->win
, &point
);
1057 changes
.left
= point
.x
;
1058 changes
.top
= point
.y
;
1060 if (window
->desiredConfMask
& (CWX
| CWY
)) {
1061 changes
.left
= window
->desiredX
;
1062 changes
.top
= window
->desiredY
;
1063 flags
&= ~SWP_NOMOVE
;
1065 if (window
->desiredConfMask
& (CWWidth
| CWHeight
)) {
1066 changes
.right
= changes
.left
+ window
->desiredWidth
;
1067 changes
.bottom
= changes
.top
+ window
->desiredHeight
;
1068 flags
&= ~SWP_NOSIZE
;
1069 /* XXX If overlay exists, resize the overlay here, ie.
1070 if (window->overlay) ... */
1072 if (window
->desiredConfMask
& CWStackMode
) {
1073 flags
&= ~SWP_NOZORDER
;
1074 /* XXX Overlay support might require something special here. */
1077 /* Adjust the window rectangle because Win32 thinks that the x, y,
1078 width & height are the WHOLE window (including decorations),
1079 whereas GLUT treats the x, y, width & height as only the CLIENT
1080 area of the window. Only do this to top level windows
1081 that are not in game mode (since game mode windows do
1082 not have any decorations). */
1083 if (!window
->parent
&& window
!= __glutGameModeWindow
) {
1084 AdjustWindowRect(&changes
,
1085 WS_OVERLAPPEDWINDOW
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
,
1089 /* Do the repositioning, moving, and push/pop. */
1090 SetWindowPos(window
->win
,
1091 window
->desiredStack
== Above
? HWND_TOP
: HWND_NOTOPMOST
,
1092 changes
.left
, changes
.top
,
1093 changes
.right
- changes
.left
, changes
.bottom
- changes
.top
,
1096 /* Zero out the mask. */
1097 window
->desiredConfMask
= 0;
1099 /* This hack causes the window to go back to the right position
1100 when it is taken out of fullscreen mode. */
1101 if (workMask
& GLUT_FULL_SCREEN_WORK
) {
1102 window
->desiredConfMask
|= CWX
| CWY
;
1103 window
->desiredX
= point
.x
;
1104 window
->desiredY
= point
.y
;
1107 XWindowChanges changes
;
1109 changes
.x
= window
->desiredX
;
1110 changes
.y
= window
->desiredY
;
1111 if (window
->desiredConfMask
& (CWWidth
| CWHeight
)) {
1112 changes
.width
= window
->desiredWidth
;
1113 changes
.height
= window
->desiredHeight
;
1114 if (window
->overlay
)
1115 XResizeWindow(__glutDisplay
, window
->overlay
->win
,
1116 window
->desiredWidth
, window
->desiredHeight
);
1117 if (__glutMotifHints
!= None
) {
1118 if (workMask
& GLUT_FULL_SCREEN_WORK
) {
1121 hints
.flags
= MWM_HINTS_DECORATIONS
;
1122 hints
.decorations
= 0; /* Absolutely no
1124 XChangeProperty(__glutDisplay
, window
->win
,
1125 __glutMotifHints
, __glutMotifHints
, 32,
1126 PropModeReplace
, (unsigned char *) &hints
, 4);
1127 if (workMask
& GLUT_MAP_WORK
) {
1128 /* Handle case where glutFullScreen is called
1129 before the first time that the window is
1130 mapped. Some window managers will randomly or
1131 interactively position the window the first
1132 time it is mapped if the window's
1133 WM_NORMAL_HINTS property does not request an
1134 explicit position. We don't want any such
1135 window manager interaction when going
1136 fullscreen. Overwrite the WM_NORMAL_HINTS
1137 property installed by glutCreateWindow's
1138 XSetWMProperties property with one explicitly
1139 requesting a fullscreen window. */
1142 hints
.flags
= USPosition
| USSize
;
1145 hints
.width
= window
->desiredWidth
;
1146 hints
.height
= window
->desiredHeight
;
1147 XSetWMNormalHints(__glutDisplay
, window
->win
, &hints
);
1150 XDeleteProperty(__glutDisplay
, window
->win
, __glutMotifHints
);
1154 if (window
->desiredConfMask
& CWStackMode
) {
1155 changes
.stack_mode
= window
->desiredStack
;
1156 /* Do not let glutPushWindow push window beneath the
1158 if (window
->parent
&& window
->parent
->overlay
1159 && window
->desiredStack
== Below
) {
1160 changes
.stack_mode
= Above
;
1161 changes
.sibling
= window
->parent
->overlay
->win
;
1162 window
->desiredConfMask
|= CWSibling
;
1165 XConfigureWindow(__glutDisplay
, window
->win
,
1166 window
->desiredConfMask
, &changes
);
1167 window
->desiredConfMask
= 0;
1170 #if !defined(_WIN32)
1171 /* Be sure to establish the colormaps BEFORE map window is
1173 if (workMask
& GLUT_COLORMAP_WORK
) {
1174 __glutEstablishColormapsProperty(window
);
1177 if (workMask
& GLUT_MAP_WORK
) {
1178 switch (window
->desiredMapState
) {
1179 case WithdrawnState
:
1180 if (window
->parent
) {
1181 XUnmapWindow(__glutDisplay
, window
->win
);
1183 XWithdrawWindow(__glutDisplay
, window
->win
,
1186 window
->shownState
= 0;
1189 XMapWindow(__glutDisplay
, window
->win
);
1190 window
->shownState
= 1;
1193 case GameModeState
: /* Not an Xlib value. */
1194 ShowWindow(window
->win
, SW_SHOW
);
1195 window
->shownState
= 1;
1199 XIconifyWindow(__glutDisplay
, window
->win
, __glutScreen
);
1200 window
->shownState
= 0;
1205 if (workMask
& (GLUT_REDISPLAY_WORK
| GLUT_OVERLAY_REDISPLAY_WORK
| GLUT_REPAIR_WORK
| GLUT_OVERLAY_REPAIR_WORK
)) {
1206 if (window
->forceReshape
) {
1207 /* Guarantee that before a display callback is generated
1208 for a window, a reshape callback must be generated. */
1209 __glutSetWindow(window
);
1210 window
->reshape(window
->width
, window
->height
);
1211 window
->forceReshape
= False
;
1213 /* Setting the redisplay bit on the first reshape is
1214 necessary to make the "Mesa glXSwapBuffers to repair
1215 damage" hack operate correctly. Without indicating a
1216 redisplay is necessary, there's not an initial back
1217 buffer render from which to blit from when damage
1218 happens to the window. */
1219 workMask
|= GLUT_REDISPLAY_WORK
;
1221 /* The code below is more involved than otherwise necessary
1222 because it is paranoid about the overlay or entire window
1223 being removed or destroyed in the course of the callbacks.
1224 Notice how the global __glutWindowDamaged is used to record
1225 the layers' damage status. See the code in glutLayerGet for
1226 how __glutWindowDamaged is used. The point is to not have to
1227 update the "damaged" field after the callback since the
1228 window (or overlay) may be destroyed (or removed) when the
1229 callback returns. */
1231 if (window
->overlay
&& window
->overlay
->display
) {
1232 int num
= window
->num
;
1233 Window xid
= window
->overlay
? window
->overlay
->win
: None
;
1235 /* If an overlay display callback is registered, we
1236 differentiate between a redisplay needed for the
1237 overlay and/or normal plane. If there is no overlay
1238 display callback registered, we simply use the
1239 standard display callback. */
1241 if (workMask
& (GLUT_REDISPLAY_WORK
| GLUT_REPAIR_WORK
)) {
1242 if (__glutMesaSwapHackSupport
) {
1243 if (window
->usedSwapBuffers
) {
1244 if ((workMask
& (GLUT_REPAIR_WORK
| GLUT_REDISPLAY_WORK
)) == GLUT_REPAIR_WORK
) {
1245 SWAP_BUFFERS_WINDOW(window
);
1246 goto skippedDisplayCallback1
;
1250 /* Render to normal plane. */
1252 window
->renderDc
= window
->hdc
;
1254 window
->renderWin
= window
->win
;
1255 window
->renderCtx
= window
->ctx
;
1256 __glutWindowDamaged
= (workMask
& GLUT_REPAIR_WORK
);
1257 __glutSetWindow(window
);
1258 window
->usedSwapBuffers
= 0;
1260 __glutWindowDamaged
= 0;
1262 skippedDisplayCallback1
:;
1264 if (workMask
& (GLUT_OVERLAY_REDISPLAY_WORK
| GLUT_OVERLAY_REPAIR_WORK
)) {
1265 window
= __glutWindowList
[num
];
1266 if (window
&& window
->overlay
&&
1267 window
->overlay
->win
== xid
&& window
->overlay
->display
) {
1269 /* Render to overlay. */
1271 window
->renderDc
= window
->overlay
->hdc
;
1273 window
->renderWin
= window
->overlay
->win
;
1274 window
->renderCtx
= window
->overlay
->ctx
;
1275 __glutWindowDamaged
= (workMask
& GLUT_OVERLAY_REPAIR_WORK
);
1276 __glutSetWindow(window
);
1277 window
->overlay
->display();
1278 __glutWindowDamaged
= 0;
1280 /* Overlay may have since been destroyed or the
1281 overlay callback may have been disabled during
1282 normal display callback. */
1286 if (__glutMesaSwapHackSupport
) {
1287 if (!window
->overlay
&& window
->usedSwapBuffers
) {
1288 if ((workMask
& (GLUT_REPAIR_WORK
| GLUT_REDISPLAY_WORK
)) == GLUT_REPAIR_WORK
) {
1289 SWAP_BUFFERS_WINDOW(window
);
1290 goto skippedDisplayCallback2
;
1294 /* Render to normal plane (and possibly overlay). */
1295 __glutWindowDamaged
= (workMask
& (GLUT_OVERLAY_REPAIR_WORK
| GLUT_REPAIR_WORK
));
1296 __glutSetWindow(window
);
1297 window
->usedSwapBuffers
= 0;
1299 __glutWindowDamaged
= 0;
1301 skippedDisplayCallback2
:;
1304 /* Combine workMask with window->workMask to determine what
1305 finish and debug work there is. */
1306 workMask
|= window
->workMask
;
1308 if (workMask
& GLUT_FINISH_WORK
) {
1309 /* Finish work makes sure a glFinish gets done to indirect
1310 rendering contexts. Indirect contexts tend to have much
1311 longer latency because lots of OpenGL extension requests
1312 can queue up in the X protocol stream. __glutSetWindow
1313 is where the finish works gets queued for indirect
1315 __glutSetWindow(window
);
1318 if (workMask
& GLUT_DEBUG_WORK
) {
1319 __glutSetWindow(window
);
1322 /* Strip out dummy, finish, and debug work bits. */
1323 window
->workMask
&= ~(GLUT_DUMMY_WORK
| GLUT_FINISH_WORK
| GLUT_DEBUG_WORK
);
1324 if (window
->workMask
) {
1325 /* Leave on work list. */
1328 /* Remove current window from work list. */
1329 return window
->prevWorkWin
;
1334 static /* X11 implementations do not need this global. */
1337 __glutProcessWindowWorkLists(void)
1339 if (__glutWindowWorkList
) {
1340 GLUTwindow
*remainder
, *work
;
1342 work
= __glutWindowWorkList
;
1343 __glutWindowWorkList
= NULL
;
1345 remainder
= processWindowWorkList(work
);
1347 *beforeEnd
= __glutWindowWorkList
;
1348 __glutWindowWorkList
= remainder
;
1358 #if !defined(_WIN32)
1360 __glutFatalUsage("main loop entered with out proper initialization.");
1362 if (!__glutWindowListSize
)
1364 "main loop entered with no windows created.");
1366 __glutProcessWindowWorkLists();
1367 if (__glutIdleFunc
|| __glutWindowWorkList
) {
1370 if (__glutTimerList
) {
1373 processEventsAndTimeouts();