~gma500/+junk/gma500-maverick

« back to all changes in this revision

Viewing changes to xpsb-glx/mesa/src/glut/fbdev/menu.c

  • Committer: Luca Forina
  • Date: 2011-02-14 09:55:00 UTC
  • Revision ID: luca.forina@gmail.com-20110214095500-kq7o333fbjuoquqs
new commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Mesa 3-D graphics library
 
3
 * Version:  6.5
 
4
 * Copyright (C) 1995-2006  Brian Paul
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Library General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2 of the License, or (at your option) any later version.
 
10
 *
 
11
 * This library is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * Library General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Library General Public
 
17
 * License along with this library; if not, write to the Free
 
18
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 */
 
20
 
 
21
/*
 
22
 * Library for glut using mesa fbdev driver
 
23
 *
 
24
 * Written by Sean D'Epagnier (c) 2006
 
25
 */
 
26
 
 
27
#include <stdlib.h>
 
28
#include <stdio.h>
 
29
#include <string.h>
 
30
 
 
31
#include <linux/fb.h>
 
32
 
 
33
#include <GL/glut.h>
 
34
 
 
35
#include "internal.h"
 
36
 
 
37
#define MENU_FONT_WIDTH   9
 
38
#define MENU_FONT_HEIGHT 15 
 
39
#define MENU_FONT        GLUT_BITMAP_9_BY_15
 
40
#define SUBMENU_OFFSET   20
 
41
 
 
42
struct GlutMenu *Menus;
 
43
int ActiveMenu;
 
44
int CurrentMenu;
 
45
 
 
46
static double MenuProjection[16];
 
47
 
 
48
static int AttachedMenus[3];
 
49
static int NumMenus = 1;
 
50
static int SelectedMenu;
 
51
 
 
52
void InitializeMenus(void)
 
53
{
 
54
   glPushAttrib(GL_TRANSFORM_BIT);
 
55
   glMatrixMode(GL_PROJECTION);
 
56
   glPushMatrix();
 
57
   glLoadIdentity();
 
58
   gluOrtho2D(0.0, VarInfo.xres, VarInfo.yres, 0.0);
 
59
   glGetDoublev(GL_PROJECTION_MATRIX, MenuProjection);
 
60
 
 
61
   glPopMatrix();
 
62
   glPopAttrib();
 
63
}
 
64
 
 
65
void FreeMenus(void)
 
66
{
 
67
   int i, j;
 
68
        
 
69
   for(i = 1; i<NumMenus; i++) {
 
70
      for(j = 0; j<Menus[i].NumItems; j++)
 
71
         free(Menus[i].Items[j].name);
 
72
      free(Menus[i].Items);
 
73
   }
 
74
   
 
75
   free(Menus);
 
76
}
 
77
 
 
78
int TryMenu(int button, int pressed)
 
79
{
 
80
   if(ActiveMenu && !pressed) {
 
81
      ActiveMenu = 0;
 
82
      CloseMenu();
 
83
      Redisplay = 1;
 
84
      return 1;
 
85
   }
 
86
 
 
87
   if(AttachedMenus[button] && pressed) {
 
88
      ActiveMenu = AttachedMenus[button];
 
89
      OpenMenu();
 
90
      Redisplay = 1;
 
91
      return 1;
 
92
   }
 
93
   return 0;
 
94
}
 
95
 
 
96
static int DrawMenu(int menu, int x, int *y)
 
97
{
 
98
   int i;
 
99
   int ret = 1;
 
100
 
 
101
   for(i=0; i < Menus[menu].NumItems; i++) {
 
102
      char *s = Menus[menu].Items[i].name;
 
103
      int a = 0;
 
104
      if(MouseY >= *y && MouseY < *y + MENU_FONT_HEIGHT &&
 
105
         MouseX >= x && MouseX < x + Menus[menu].width) {
 
106
         a = 1;
 
107
         SelectedMenu = menu;
 
108
         ret = 0;
 
109
         Menus[menu].selected = i;
 
110
         glColor3f(1,0,0);
 
111
      } else
 
112
         glColor3f(1,1,1);
 
113
 
 
114
      *y += MENU_FONT_HEIGHT;
 
115
      glRasterPos2i(x, *y);
 
116
      for(; *s; s++)
 
117
         glutBitmapCharacter(MENU_FONT, *s);
 
118
 
 
119
      if(Menus[menu].selected == i)
 
120
         if(Menus[menu].Items[i].submenu) 
 
121
            if(DrawMenu(Menus[menu].Items[i].submenu, x 
 
122
                        + SUBMENU_OFFSET, y)) {
 
123
               if(!a)
 
124
                  Menus[menu].selected = -1;
 
125
            } else
 
126
               ret = 0;
 
127
   }
 
128
   return ret;
 
129
}
 
130
 
 
131
void DrawMenus(void)
 
132
{
 
133
   int x, y;
 
134
 
 
135
   if(GameMode)
 
136
      return;
 
137
 
 
138
   x = Menus[ActiveMenu].x;
 
139
   y = Menus[ActiveMenu].y;
 
140
 
 
141
   /* save old settings */
 
142
   glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT
 
143
                | GL_ENABLE_BIT | GL_VIEWPORT_BIT);
 
144
 
 
145
   glMatrixMode(GL_MODELVIEW);
 
146
   glPushMatrix();
 
147
   glLoadIdentity();
 
148
 
 
149
   glMatrixMode(GL_PROJECTION);
 
150
   glPushMatrix();
 
151
   glLoadMatrixd(MenuProjection);
 
152
   glViewport(0, 0, VarInfo.xres, VarInfo.yres);
 
153
 
 
154
   glDisable(GL_DEPTH_TEST);
 
155
   glDisable(GL_ALPHA_TEST);
 
156
   glDisable(GL_LIGHTING);
 
157
   glDisable(GL_FOG);
 
158
   glDisable(GL_TEXTURE_2D);
 
159
   glEnable(GL_COLOR_LOGIC_OP);
 
160
   glLogicOp(GL_AND_REVERSE);
 
161
 
 
162
   if(DrawMenu(ActiveMenu, x, &y))
 
163
      Menus[ActiveMenu].selected = -1;
 
164
    
 
165
   /* restore settings */
 
166
   glPopMatrix();
 
167
   glMatrixMode(GL_MODELVIEW);
 
168
   glPopMatrix();
 
169
 
 
170
   glPopAttrib();
 
171
}
 
172
 
 
173
void OpenMenu(void)
 
174
{
 
175
   if(MenuStatusFunc)
 
176
      MenuStatusFunc(GLUT_MENU_IN_USE, MouseX, MouseY);
 
177
   if(MenuStateFunc)
 
178
      MenuStateFunc(GLUT_MENU_IN_USE);
 
179
   Menus[ActiveMenu].x = MouseX-Menus[ActiveMenu].width/2;
 
180
 
 
181
   if(Menus[ActiveMenu].x < 0)
 
182
      Menus[ActiveMenu].x = 0;
 
183
   if(Menus[ActiveMenu].x + Menus[ActiveMenu].width >= VarInfo.xres)
 
184
     Menus[ActiveMenu].x = VarInfo.xres - Menus[ActiveMenu].width - 1;
 
185
 
 
186
   Menus[ActiveMenu].y = MouseY-Menus[ActiveMenu].NumItems*MENU_FONT_HEIGHT/2;
 
187
   Menus[ActiveMenu].selected = -1;
 
188
}
 
189
 
 
190
void CloseMenu(void)
 
191
{
 
192
   if(MenuStatusFunc)
 
193
      MenuStatusFunc(GLUT_MENU_NOT_IN_USE, MouseX, MouseY);
 
194
   if(MenuStateFunc)
 
195
      MenuStateFunc(GLUT_MENU_NOT_IN_USE);
 
196
   if(SelectedMenu > 0) {
 
197
      int selected = Menus[SelectedMenu].selected;
 
198
      if(selected >= 0)
 
199
         if(Menus[SelectedMenu].Items[selected].submenu == 0)
 
200
            Menus[SelectedMenu].func(Menus[SelectedMenu].Items
 
201
                                     [selected].value);
 
202
   }
 
203
 
 
204
}
 
205
 
 
206
/* glut menu functions */
 
207
 
 
208
int glutCreateMenu(void (*func)(int value))
 
209
{
 
210
   CurrentMenu = NumMenus;
 
211
   NumMenus++;
 
212
   Menus = realloc(Menus, sizeof(*Menus) * NumMenus);
 
213
   Menus[CurrentMenu].NumItems = 0;
 
214
   Menus[CurrentMenu].Items = NULL;
 
215
   Menus[CurrentMenu].func = func;
 
216
   Menus[CurrentMenu].width = 0;
 
217
   return CurrentMenu;
 
218
}
 
219
 
 
220
void glutSetMenu(int menu)
 
221
{
 
222
   CurrentMenu = menu;
 
223
}
 
224
 
 
225
int glutGetMenu(void)
 
226
{
 
227
   return CurrentMenu;
 
228
}
 
229
 
 
230
void glutDestroyMenu(int menu)
 
231
{
 
232
   if(menu == CurrentMenu)
 
233
      CurrentMenu = 0;
 
234
}
 
235
 
 
236
static void NameMenuEntry(int entry, const char *name)
 
237
{
 
238
   int cm = CurrentMenu;
 
239
   if(!(Menus[cm].Items[entry-1].name = realloc(Menus[cm].Items[entry-1].name,
 
240
                                                strlen(name) + 1))) {
 
241
      sprintf(exiterror, "realloc failed in NameMenuEntry\n");
 
242
      exit(0);
 
243
   }
 
244
   strcpy(Menus[cm].Items[entry-1].name, name);
 
245
   if(strlen(name) * MENU_FONT_WIDTH > Menus[cm].width)
 
246
      Menus[cm].width = strlen(name) * MENU_FONT_WIDTH;
 
247
}
 
248
 
 
249
static int AddMenuItem(const char *name)
 
250
{
 
251
   int cm = CurrentMenu;
 
252
   int item = Menus[cm].NumItems++;
 
253
   if(!(Menus[cm].Items = realloc(Menus[cm].Items,
 
254
                                  Menus[cm].NumItems * sizeof(*Menus[0].Items)))) {
 
255
      sprintf(exiterror, "realloc failed in AddMenuItem\n");
 
256
      exit(0);
 
257
   }
 
258
   Menus[cm].Items[item].name = NULL;
 
259
   NameMenuEntry(item+1, name);
 
260
   return item;
 
261
}
 
262
 
 
263
void glutAddMenuEntry(const char *name, int value)
 
264
{
 
265
   int item = AddMenuItem(name);
 
266
   Menus[CurrentMenu].Items[item].value = value;
 
267
   Menus[CurrentMenu].Items[item].submenu = 0;
 
268
}
 
269
 
 
270
void glutAddSubMenu(const char *name, int menu)
 
271
{
 
272
   int item = AddMenuItem(name);
 
273
   if(menu == CurrentMenu) {
 
274
      sprintf(exiterror, "Recursive menus not supported\n");
 
275
      exit(0);
 
276
   }
 
277
   Menus[CurrentMenu].Items[item].submenu = menu;
 
278
}
 
279
 
 
280
void glutChangeToMenuEntry(int entry, const char *name, int value)
 
281
{
 
282
   NameMenuEntry(entry, name);
 
283
   Menus[CurrentMenu].Items[entry-1].value = value;
 
284
   Menus[CurrentMenu].Items[entry-1].submenu = 0;
 
285
}
 
286
 
 
287
void glutChangeToSubMenu(int entry, const char *name, int menu)
 
288
{
 
289
   NameMenuEntry(entry, name);
 
290
   Menus[CurrentMenu].Items[entry-1].submenu = menu;
 
291
}
 
292
 
 
293
void glutRemoveMenuItem(int entry)
 
294
{
 
295
   memmove(Menus[CurrentMenu].Items + entry - 1,
 
296
           Menus[CurrentMenu].Items + entry,
 
297
           sizeof(*Menus[0].Items) * (Menus[CurrentMenu].NumItems - entry));
 
298
   Menus[CurrentMenu].NumItems--;
 
299
}
 
300
 
 
301
void glutAttachMenu(int button)
 
302
{
 
303
   AttachedMenus[button] = CurrentMenu;
 
304
}
 
305
 
 
306
void glutDetachMenu(int button)
 
307
{
 
308
   AttachedMenus[button] = 0;
 
309
}