summaryrefslogtreecommitdiffstats
path: root/src/glut/fbdev/gamemode.c
blob: 30be36af7af802f6f3a30831be9ee3e2e9cbd32b (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
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
/*
 * Mesa 3-D graphics library
 * Version:  6.5
 * Copyright (C) 1995-2006  Brian Paul
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * Library for glut using mesa fbdev driver
 *
 * Written by Sean D'Epagnier (c) 2006
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <linux/fb.h>

#include <GL/glut.h>

#include "internal.h"

int GameMode;

static int ModePossible, DispChanged;
static struct fb_var_screeninfo NormVarInfo, GameVarInfo;

static GLFBDevContextPtr GameContext;
static GLFBDevVisualPtr NormVisual;

/* storage for non-gamemode callbacks */
void (*KeyFuncs[2])(unsigned char key, int x, int y);
static void (*NormFuncs[8])();

static const char*SetOpers(const char *p, unsigned int *min, unsigned int *max)
{
   char *endptr;
   int comp = *p, val, neq = 0;

   if(p[1] == '=') {
      neq = 0;
      p++;
   }

   val = strtol(p+1, &endptr, 10);
   if(endptr == p+1)
      return p;

   switch(comp) {
   case '=':
      *min = *max = val;
      break;
   case '!':
      *min = val + 1;
      *max = val - 1;
      break;
   case '<':
      *max = val - neq;
      break;
   case '>':
      *min = val + neq;
      break;
   }
   return endptr;
}

void glutGameModeString(const char *string)
{
   const char *p = string;
   unsigned int minb = 15, maxb = 32;
   unsigned int minw = 0, maxw = -1;
   unsigned int minh, maxh = -1;
   unsigned int minf = 0, maxf = MAX_VSYNC;
   char *endptr;
   int count = -1, val;

   ModePossible = 0;

   if(DisplayMode & GLUT_INDEX)
      minb = maxb = 8;

 again:
   count++;
   if((val = strtol(p, &endptr, 10)) && *endptr=='x') {
      maxw = minw = val;
      p = endptr + 1;
      maxh = minh = strtol(p, &endptr, 10);
      p = endptr;
      goto again;
   }

   if(*p == ':') {
      minb = strtol(p+1, &endptr, 10);
      p = endptr;
      if(DisplayMode & GLUT_INDEX) {
	 if(minb != 8)
	    return;
      } else
	 if(minb != 15 && minb != 16 && minb != 24 && minb != 32)
	    return;
      maxb = minb;
      goto again;
   }

   if(*p == '@') {
      minf = strtol(p+1, &endptr, 10) - 5;
      maxf = minf + 10;
      p = endptr;
      goto again;
   }

   if(count == 0)
      while(*p) {
	 if(*p == ' ')
	    p++;
	 else
	 if(memcmp(p, "bpp", 3) == 0)
	    p = SetOpers(p+3, &minb, &maxb);
	 else
	 if(memcmp(p, "height", 6) == 0)
	    p = SetOpers(p+6, &minh, &maxh);
	 else
	 if(memcmp(p, "hertz", 5) == 0)
	    p = SetOpers(p+5, &minf, &maxf);
	 else
	 if(memcmp(p, "width", 5) == 0)
	    p = SetOpers(p+5, &minw, &maxw);
	 else
	 if(p = strchr(p, ' '))
	    p++;
	 else
	    break;
   }

   NormVarInfo = VarInfo;
   if(!ParseFBModes(minw, maxw, minh, maxh, minf, maxf))
      return;

   GameVarInfo = VarInfo;
   VarInfo = NormVarInfo;

   /* determine optimal bitdepth, make sure we have enough video memory */
   if(VarInfo.bits_per_pixel && VarInfo.bits_per_pixel <= maxb)
      GameVarInfo.bits_per_pixel = VarInfo.bits_per_pixel;
   else
      GameVarInfo.bits_per_pixel = maxb;

   while(FixedInfo.smem_len < GameVarInfo.xres * GameVarInfo.yres
	 * GameVarInfo.bits_per_pixel / 8) {
      if(GameVarInfo.bits_per_pixel < minb)
	 return;
      GameVarInfo.bits_per_pixel = ((GameVarInfo.bits_per_pixel+1)/8)*8-8;
   }
  
   ModePossible = 1;
}
   
int glutEnterGameMode(void)
{
   if(ActiveMenu)
      return 0;

   if(!ModePossible)
      return 0;

   if(GameMode) {
      if(!memcmp(&GameVarInfo, &VarInfo, sizeof VarInfo)) {
	 DispChanged = 0;
	 return 1;
      }
      glutLeaveGameMode();
   }

   if (ioctl(FrameBufferFD, FBIOPUT_VSCREENINFO, &GameVarInfo))
      return 0;

   NormVarInfo = VarInfo;
   VarInfo = GameVarInfo;

   NormVisual = Visual;
   SetVideoMode();
   CreateVisual();
   CreateBuffer();

   if(!(GameContext = glFBDevCreateContext(Visual, NULL))) {
      sprintf(exiterror, "Failure to create Context\n");
      exit(0);
   }

   if(!glFBDevMakeCurrent( GameContext, Buffer, Buffer )) {
      sprintf(exiterror, "Failure to Make Game Current\n");
      exit(0);
   }

   InitializeCursor();

   KeyFuncs[0] = KeyboardFunc;
   KeyFuncs[1] = KeyboardUpFunc;

   NormFuncs[0] = DisplayFunc;
   NormFuncs[1] = ReshapeFunc;
   NormFuncs[2] = MouseFunc;
   NormFuncs[3] = MotionFunc;
   NormFuncs[4] = PassiveMotionFunc;
   NormFuncs[5] = VisibilityFunc;
   NormFuncs[6] = SpecialFunc;
   NormFuncs[7] = SpecialUpFunc;

   DisplayFunc = NULL;
   ReshapeFunc = NULL;
   KeyboardFunc = NULL;
   KeyboardUpFunc = NULL;
   MouseFunc = NULL;
   MotionFunc = NULL;
   PassiveMotionFunc = NULL;
   VisibilityFunc = NULL;
   SpecialFunc = SpecialUpFunc = NULL;

   DispChanged = 1;
   GameMode = 1;
   Visible = 1;
   VisibleSwitch = 1;
   Redisplay = 1;
   return 1;
}

void glutLeaveGameMode(void)
{
   if(!GameMode)
      return;

   glFBDevDestroyContext(GameContext);
   glFBDevDestroyVisual(Visual);

   VarInfo = NormVarInfo;
   Visual = NormVisual;
   
   if(Visual) {
      SetVideoMode();
      CreateBuffer();
   
      if(!glFBDevMakeCurrent( Context, Buffer, Buffer )) {
	 sprintf(exiterror, "Failure to Make Current\n");
	 exit(0);
      }
      
      Redisplay = 1;
   }

   KeyboardFunc = KeyFuncs[0];
   KeyboardUpFunc = KeyFuncs[1];

   DisplayFunc = NormFuncs[0];
   ReshapeFunc = NormFuncs[1];
   MouseFunc = NormFuncs[2];
   MotionFunc = NormFuncs[3];
   PassiveMotionFunc = NormFuncs[4];
   VisibilityFunc = NormFuncs[5];
   SpecialFunc = NormFuncs[6];
   SpecialUpFunc = NormFuncs[7];

   GameMode = 0;
}

int glutGameModeGet(GLenum mode) {
   switch(mode) {
   case GLUT_GAME_MODE_ACTIVE:
      return GameMode;
   case GLUT_GAME_MODE_POSSIBLE:
      return ModePossible;
   case GLUT_GAME_MODE_DISPLAY_CHANGED:
      return DispChanged;
   }

   if(!ModePossible)
      return -1;
   
   switch(mode) {
   case GLUT_GAME_MODE_WIDTH:
      return GameVarInfo.xres;
   case GLUT_GAME_MODE_HEIGHT:
      return GameVarInfo.yres;
   case GLUT_GAME_MODE_PIXEL_DEPTH:
      return GameVarInfo.bits_per_pixel;
   case GLUT_GAME_MODE_REFRESH_RATE:
      return 1E12/GameVarInfo.pixclock
	 / (GameVarInfo.left_margin + GameVarInfo.xres
	   + GameVarInfo.right_margin + GameVarInfo.hsync_len)
	 / (GameVarInfo.upper_margin + GameVarInfo.yres 
	   + GameVarInfo.lower_margin + GameVarInfo.vsync_len);
   }
}