summaryrefslogtreecommitdiffstats
path: root/src/glut/dos/loop.c
blob: 36c3adc3ffe3906246a0fe19699c658a55ef1c18 (plain)
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
/*
 * DOS/DJGPP Mesa Utility Toolkit
 * Version:  1.0
 *
 * Copyright (C) 2005  Daniel Borca   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * DANIEL BORCA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


#include <string.h>

#include <GL/glut.h>
#include "GL/dmesa.h"

#include "PC_HW/pc_hw.h"
#include "internal.h"


static int looping = 0;


#define DO_REDISPLAY(w, ccin, ccout) \
   do {                                                            \
      if (w->redisplay && w->display) {                            \
         int rv = GL_TRUE;                                         \
                                                                   \
         idle         = GL_FALSE;                                  \
         w->redisplay = GL_FALSE;                                  \
                                                                   \
         /* test IN condition (whether we need to `MakeCurrent') */\
         if (ccin) {                                               \
            rv = DMesaMakeCurrent(w->context, w->buffer);          \
         }                                                         \
                                                                   \
         /* do the display only if `MakeCurrent' didn't failed */  \
         if (rv) {                                                 \
            if (w->show_mouse && !(_glut_default.mode & GLUT_DOUBLE)) {\
               /* XXX scare mouse */                               \
               w->display();                                       \
               /* XXX unscare mouse */                             \
            } else {                                               \
               w->display();                                       \
            }                                                      \
                                                                   \
            /* update OUT condition */                             \
            ccout;                                                 \
         }                                                         \
      }                                                            \
   } while (0)


void APIENTRY
glutMainLoopEvent (void)
{
   int i, n;
   GLUTwindow *w;
   GLboolean idle;
   static int old_mouse_x = 0;
   static int old_mouse_y = 0;
   static int old_mouse_b = 0;

   static GLboolean virgin = GL_TRUE;
   if (virgin) {
      pc_install_keyb();
      _glut_mouse_init();

      for (i = 0; i < MAX_WINDOWS; i++) {
         w = _glut_windows[i];
         if (w != NULL) {
            glutSetWindow(w->num);
            glutPostRedisplay();
            if (w->reshape) {
               w->reshape(w->width, w->height);
            }
            if (w->visibility) {
               w->visibility(GLUT_VISIBLE);
            }
         }
      }
      virgin = GL_FALSE;
   }

   idle = GL_TRUE;

   n = 0;
   for (i = 0; i < MAX_WINDOWS; i++) {
      w = _glut_windows[i];
      if ((w != NULL) && (w != _glut_current)) {
         /* 1) redisplay `w'
          * 2) `MakeCurrent' always
          * 3) update number of non-default windows
          */
         DO_REDISPLAY(w, GL_TRUE, n++);
      }
   }
   /* 1) redisplay `_glut_current'
    * 2) `MakeCurrent' only if we previously did non-default windows
    * 3) don't update anything
    */
   DO_REDISPLAY(_glut_current, n, n);

   if (_glut_mouse) {
      int mouse_x;
      int mouse_y;
      int mouse_z;
      int mouse_b;

      /* query mouse */
      mouse_b = pc_query_mouse(&mouse_x, &mouse_y, &mouse_z);

      /* relative to window coordinates */
      _glut_mouse_x = mouse_x - _glut_current->xpos;
      _glut_mouse_y = mouse_y - _glut_current->ypos;

      /* mouse was moved? */
      if ((mouse_x != old_mouse_x) || (mouse_y != old_mouse_y)) {
         idle        = GL_FALSE;
         old_mouse_x = mouse_x;
         old_mouse_y = mouse_y;

         if (mouse_b) {
            /* any button pressed */
            if (_glut_current->motion) {
               _glut_current->motion(_glut_mouse_x, _glut_mouse_y);
            }
         } else {
            /* no button pressed */
            if (_glut_current->passive) {
               _glut_current->passive(_glut_mouse_x, _glut_mouse_y);
            }
         }
      }

      /* button state changed? */
      if (mouse_b != old_mouse_b) {
         GLUTmouseCB mouse_func;

         if ((mouse_func = _glut_current->mouse)) {
            if ((old_mouse_b & 1) && !(mouse_b & 1))
               mouse_func(GLUT_LEFT_BUTTON, GLUT_UP,     _glut_mouse_x, _glut_mouse_y);
            else if (!(old_mouse_b & 1) && (mouse_b & 1))
               mouse_func(GLUT_LEFT_BUTTON, GLUT_DOWN,   _glut_mouse_x, _glut_mouse_y);

            if ((old_mouse_b & 2) && !(mouse_b & 2))
               mouse_func(GLUT_RIGHT_BUTTON, GLUT_UP,    _glut_mouse_x, _glut_mouse_y);
            else if (!(old_mouse_b & 2) && (mouse_b & 2))
               mouse_func(GLUT_RIGHT_BUTTON, GLUT_DOWN,  _glut_mouse_x, _glut_mouse_y);

            if ((old_mouse_b & 4) && !(mouse_b & 4))
               mouse_func(GLUT_MIDDLE_BUTTON, GLUT_UP,   _glut_mouse_x, _glut_mouse_y);
            else if (!(old_mouse_b & 3) && (mouse_b & 4))
               mouse_func(GLUT_MIDDLE_BUTTON, GLUT_DOWN, _glut_mouse_x, _glut_mouse_y);
         }

         idle        = GL_FALSE;
         old_mouse_b = mouse_b;
      }
   }

   if (pc_keypressed()) {
      int key;
      int glut_key;

      idle = GL_FALSE;
      key  = pc_readkey();

      switch (key>>16) {
         case KEY_F1:     glut_key = GLUT_KEY_F1;        goto special;
         case KEY_F2:     glut_key = GLUT_KEY_F2;        goto special;
         case KEY_F3:     glut_key = GLUT_KEY_F3;        goto special;
         case KEY_F4:     glut_key = GLUT_KEY_F4;        goto special;
         case KEY_F5:     glut_key = GLUT_KEY_F5;        goto special;
         case KEY_F6:     glut_key = GLUT_KEY_F6;        goto special;
         case KEY_F7:     glut_key = GLUT_KEY_F7;        goto special;
         case KEY_F8:     glut_key = GLUT_KEY_F8;        goto special;
         case KEY_F9:     glut_key = GLUT_KEY_F9;        goto special;
         case KEY_F10:    glut_key = GLUT_KEY_F10;       goto special;
         case KEY_F11:    glut_key = GLUT_KEY_F11;       goto special;
         case KEY_F12:    glut_key = GLUT_KEY_F12;       goto special;
         case KEY_LEFT:   glut_key = GLUT_KEY_LEFT;      goto special;
         case KEY_UP:     glut_key = GLUT_KEY_UP;        goto special;
         case KEY_RIGHT:  glut_key = GLUT_KEY_RIGHT;     goto special;
         case KEY_DOWN:   glut_key = GLUT_KEY_DOWN;      goto special;
         case KEY_PGUP:   glut_key = GLUT_KEY_PAGE_UP;   goto special;
         case KEY_PGDN:   glut_key = GLUT_KEY_PAGE_DOWN; goto special;
         case KEY_HOME:   glut_key = GLUT_KEY_HOME;      goto special;
         case KEY_END:    glut_key = GLUT_KEY_END;       goto special;
         case KEY_INSERT: glut_key = GLUT_KEY_INSERT;    goto special;
         special:
            if (_glut_current->special) {
               _glut_current->special(glut_key, _glut_mouse_x, _glut_mouse_y);
            }
            break;
         default:
            if (_glut_current->keyboard) {
               _glut_current->keyboard(key & 0xFF, _glut_mouse_x, _glut_mouse_y);
            }
      }
   }

   if (idle && _glut_idle_func)
      _glut_idle_func();

   for (i = 0; i < MAX_TIMER_CB; i++) {
      int time = glutGet(GLUT_ELAPSED_TIME);
      GLUTSShotCB *cb = &_glut_timer_cb[i];
      if (cb->func && (time >= cb->time)) {
         cb->func(cb->value);
         cb->func = NULL;
      }
   }
}


void APIENTRY
glutMainLoop (void)
{
   looping++;
   while (looping) {
      glutMainLoopEvent();
   }
}


void APIENTRY
glutLeaveMainLoop (void)
{
   looping--;
}