summaryrefslogtreecommitdiffstats
path: root/src/glut/fbdev/fbdev.c
blob: 3b63cd70ea18e201d0ef8c541440f20c3cdfaf03 (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
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
/*
 * 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
 * 
 * To improve on this library, maybe support subwindows or overlays,
 * I (sean at depagnier dot com) will do my best to help.
 */

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <inttypes.h>

#include <sys/mman.h>
#include <sys/time.h>
#include <sys/kd.h>

#include <linux/fb.h>
#include <linux/vt.h>

#include <GL/gl.h>
#include <GL/glut.h>

#include "internal.h"

#define FBMODES "/etc/fb.modes"

struct fb_fix_screeninfo FixedInfo;
struct fb_var_screeninfo VarInfo;
static struct fb_var_screeninfo OrigVarInfo;

static int DesiredDepth = 0;

int FrameBufferFD = -1;
unsigned char *FrameBuffer;
unsigned char *BackBuffer = NULL;
int DisplayMode;

struct GlutTimer *GlutTimers = NULL;

struct timeval StartTime;

/* per window data */
GLFBDevContextPtr Context;
GLFBDevBufferPtr Buffer;
GLFBDevVisualPtr Visual;

int Redisplay;
int Visible;
int VisibleSwitch;
int Active;
static int Resized;
/* we have to poll to see if we are visible
   on a framebuffer that is not active */
int VisiblePoll;
int Swapping, VTSwitch;
static int FramebufferIndex;

static int Initialized;

char exiterror[256];

/* test if the active console is attached to the same framebuffer */
void TestVisible(void) {
   struct fb_con2fbmap confb;
   struct vt_stat st;
   int ret;
   ioctl(ConsoleFD, VT_GETSTATE, &st);
   confb.console = st.v_active;

   ret = ioctl(FrameBufferFD, FBIOGET_CON2FBMAP, &confb);

   if(ret == -1 || confb.framebuffer == FramebufferIndex) {
      VisibleSwitch = 1;
      Visible = 0;
      VisiblePoll = 0;
   }
}

static void Cleanup(void)
{
   /* do not handle this signal when cleaning up */
   signal(SIGWINCH, SIG_IGN);

   if(GameMode)
      glutLeaveGameMode();

   if(ConsoleFD != -1)
      RestoreVT();

   /* close mouse */
   CloseMouse();

   if(Visual)
      glutDestroyWindow(1);

   /* restore original variable screen info */
   if(FrameBufferFD != -1) {
      OrigVarInfo.xoffset = 0;
      OrigVarInfo.yoffset = 0;

      if (ioctl(FrameBufferFD, FBIOPUT_VSCREENINFO, &OrigVarInfo))
	 fprintf(stderr, "ioctl(FBIOPUT_VSCREENINFO failed): %s\n",
		 strerror(errno));

      if(FrameBuffer)
         munmap(FrameBuffer, FixedInfo.smem_len);
      close(FrameBufferFD);

   }

   /* free allocated back buffer */
   if(DisplayMode & GLUT_DOUBLE)
      free(BackBuffer);

   /* free menu items */
   FreeMenus();

   if(exiterror[0])
      fprintf(stderr, "[glfbdev glut] %s", exiterror);
 }

static void CrashHandler(int sig)
{
   sprintf(exiterror, "Caught signal %d, cleaning up\n", sig);
   exit(0);
}

static void removeArgs(int *argcp, char **argv, int num)
{
   int i;
   for (i = 0; argv[i+num]; i++)
      argv[i] = argv[i+num];

   argv[i] = NULL;
   *argcp -= num;
}

#define REQPARAM(PARAM)  \
    if (i >= *argcp - 1) { \
	fprintf(stderr, PARAM" requires a parameter\n"); \
	exit(0); \
    }

void glutInit (int *argcp, char **argv)
{
   int i, nomouse = 0, nokeyboard = 0, usestdin = 0;
   int RequiredWidth = 0, RequiredHeight;
   char *fbdev;

   /* parse out args */
   for (i = 1; i < *argcp;) {
      if (!strcmp(argv[i], "-geometry")) {
	 REQPARAM("geometry");
	 if(sscanf(argv[i+1], "%dx%d", &RequiredWidth,
		   &RequiredHeight) != 2) {
	    fprintf(stderr,"Please specify geometry as widthxheight\n");
	    exit(0);
	 }
	 removeArgs(argcp, &argv[i], 2);
      } else
      if (!strcmp(argv[i], "-bpp")) {
	 REQPARAM("bpp");
	 if(sscanf(argv[i+1], "%d", &DesiredDepth) != 1) {
	    fprintf(stderr, "Please specify a parameter for bpp\n");
	    exit(0);
	 }
	 removeArgs(argcp, &argv[i], 2);
      } else 
      if (!strcmp(argv[i], "-vt")) {
	 REQPARAM("vt");
	 if(sscanf(argv[i+1], "%d", &CurrentVT) != 1) {
	    fprintf(stderr, "Please specify a parameter for vt\n");
	    exit(0);
	 }
	 removeArgs(argcp, &argv[i], 2);
      } else 
      if (!strcmp(argv[i], "-mousespeed")) {
	 REQPARAM("mousespeed");
	 if(sscanf(argv[i+1], "%lf", &MouseSpeed) != 1) {
	    fprintf(stderr, "Please specify a mouse speed, eg: 2.5\n");
	    exit(0);
	 }
	 removeArgs(argcp, &argv[i], 2);
      } else 
      if (!strcmp(argv[i], "-nomouse")) {
	 nomouse = 1;
	 removeArgs(argcp, &argv[i], 1);
      } else 
      if (!strcmp(argv[i], "-nokeyboard")) {
	    nokeyboard = 1;
	    removeArgs(argcp, &argv[i], 1);
	 } else 
      if (!strcmp(argv[i], "-stdin")) {
	 usestdin = 1;
	 removeArgs(argcp, &argv[i], 1);
      } else 
      if (!strcmp(argv[i], "-gpmmouse")) {
#ifdef HAVE_GPM
	 GpmMouse = 1;
#else
	 fprintf(stderr, "gpm support not compiled\n");
	 exit(0);
#endif
	 removeArgs(argcp, &argv[i], 1);
      } else 
      if (!strcmp(argv[i], "--")) {
	 removeArgs(argcp, &argv[i], 1);
	 break;
      } else 
	 i++;
   }

   gettimeofday(&StartTime, 0);
   atexit(Cleanup);

   signal(SIGSEGV, CrashHandler);
   signal(SIGINT, CrashHandler);
   signal(SIGTERM, CrashHandler);
   signal(SIGABRT, CrashHandler);

   if(nomouse == 0)
      InitializeMouse();
   if(nokeyboard == 0)
      InitializeVT(usestdin);

   fbdev = getenv("FRAMEBUFFER");
   if(fbdev) {
#ifdef MULTIHEAD
      if(!sscanf(fbdev, "/dev/fb%d", &FramebufferIndex))
	 if(!sscanf(fbdev, "/dev/fb/%d", &FramebufferIndex))
	    sprintf(exiterror, "Could not determine Framebuffer index!\n");
#endif
   } else {
      static char fb[128];
      struct fb_con2fbmap confb;
      int fd = open("/dev/fb0", O_RDWR);

      FramebufferIndex = 0;

      confb.console = CurrentVT;
      if(ioctl(fd, FBIOGET_CON2FBMAP, &confb) != -1)
	 FramebufferIndex = confb.framebuffer;
      sprintf(fb, "/dev/fb%d", FramebufferIndex);
      fbdev = fb;
      close(fd);
   }

   /* open the framebuffer device */
   FrameBufferFD = open(fbdev, O_RDWR);
   if (FrameBufferFD < 0) {
      sprintf(exiterror, "Error opening %s: %s\n", fbdev, strerror(errno));
      exit(0);
   }

   /* get the fixed screen info */
   if (ioctl(FrameBufferFD, FBIOGET_FSCREENINFO, &FixedInfo)) {
      sprintf(exiterror, "error: ioctl(FBIOGET_FSCREENINFO) failed: %s\n",
	      strerror(errno));
      exit(0);
   }

   /* get the variable screen info */
   if (ioctl(FrameBufferFD, FBIOGET_VSCREENINFO, &OrigVarInfo)) {
      sprintf(exiterror, "error: ioctl(FBIOGET_VSCREENINFO) failed: %s\n",
	      strerror(errno));
      exit(0);
   }

   /* operate on a copy */
   VarInfo = OrigVarInfo;

   /* set the depth, resolution, etc */
   if(RequiredWidth)
      if(!ParseFBModes(RequiredWidth, RequiredWidth, RequiredHeight,
		       RequiredHeight, 0, MAX_VSYNC)) {
	 sprintf(exiterror, "No mode (%dx%d) found in "FBMODES"\n",
		 RequiredWidth, RequiredHeight);
	 exit(0);
      }

   Initialized = 1;
}

void glutInitDisplayMode (unsigned int mode)
{
   DisplayMode = mode;
}

static const char *GetStrVal(const char *p, int *set, int min, int max)
{
   char *endptr;
   int comp = *p, val;

   if(p[1] == '=')
      p++;

   if(*p == '\0')
      return p;

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

   if(endptr == p+1)
      return p;

   switch(comp) {
   case '!':
      if(val == min)
	 val = max;
      else
	 val = min;
      break;
   case '<':
      val = min;
      break;
   case '>':
      val = max;
      break;
   }

   if(val < min || val > max) {
      sprintf(exiterror, "display string value out of range\n");
      exit(0);
   }

   *set = val;

   return endptr;
}

static void SetAttrib(int val, int attr)
{
   if(val)
      DisplayMode |= attr;
   else
      DisplayMode &= ~attr;
}

void glutInitDisplayString(const char *string)
{
   const char *p = string;
   int val;
   while(*p) {
      if(*p == ' ')
	 p++;
      else
      if(memcmp(p, "acca", 4) == 0) {
	 p = GetStrVal(p+4, &AccumSize, 1, 32);
	 SetAttrib(AccumSize, GLUT_ACCUM);
      } else
      if(memcmp(p, "acc", 3) == 0) {
	 p = GetStrVal(p+3, &AccumSize, 1, 32);
	 SetAttrib(AccumSize, GLUT_ACCUM);
      } else
      if(memcmp(p, "depth", 5) == 0) {
	 p = GetStrVal(p+5, &DepthSize, 12, 32);
	 SetAttrib(DepthSize, GLUT_DEPTH);
      } else
      if(memcmp(p, "double", 6) == 0) {
	 val = 1;
	 p = GetStrVal(p+6, &val, 0, 1);
	 SetAttrib(val, GLUT_DOUBLE);
      } else
      if(memcmp(p, "index", 5) == 0) {
	 val = 1;
	 p = GetStrVal(p+5, &val, 0, 1);
	 SetAttrib(val, GLUT_INDEX);
      } else
      if(memcmp(p, "stencil", 7) == 0) {
	 p = GetStrVal(p+7, &StencilSize, 0, 1);
	 SetAttrib(StencilSize, GLUT_STENCIL);
      } else
      if(memcmp(p, "samples", 7) == 0) {
	 NumSamples = 1;
	 p = GetStrVal(p+7, &NumSamples, 0, 16);
	 SetAttrib(NumSamples, GLUT_MULTISAMPLE);
      } else
      if(p = strchr(p, ' '))
         p++;
      else
	 break;
   }
}

void glutInitWindowPosition (int x, int y)
{
}

void glutInitWindowSize (int width, int height)
{
}

static void ProcessTimers(void)
{
   while(GlutTimers && GlutTimers->time <= glutGet(GLUT_ELAPSED_TIME)) {
      struct GlutTimer *timer = GlutTimers;
      GlutTimers = timer->next;
      timer->func(timer->value);
      free(timer);
   }
}

void glutMainLoop(void)
{
   int idleiters;

   if(ReshapeFunc)
      ReshapeFunc(VarInfo.xres, VarInfo.yres);

   if(!DisplayFunc) {
      sprintf(exiterror, "Fatal Error: No Display Function registered\n");
      exit(0);
   }   

   for(;;) {
      ProcessTimers();

      if(Active)
	 ReceiveInput();
      else
	 if(VisiblePoll)
	    TestVisible();

      if(IdleFunc)
	 IdleFunc();
      
      if(VisibleSwitch) {
	 VisibleSwitch = 0;
	 if(VisibilityFunc)
	    VisibilityFunc(Visible ? GLUT_VISIBLE : GLUT_NOT_VISIBLE);
      }

      if(Resized) {
         SetVideoMode();
         CreateBuffer();

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

         InitializeMenus();

         if(ReshapeFunc)
            ReshapeFunc(VarInfo.xres, VarInfo.yres);

         Redisplay = 1;
         Resized = 0;
      }

      if(Visible && Redisplay) {
	 Redisplay = 0;
         EraseCursor();
	 DisplayFunc();
	 if(!(DisplayMode & GLUT_DOUBLE)) {
	    if(ActiveMenu)
	       DrawMenus();
            DrawCursor();
	 }
         idleiters = 0;
      } else {
         /* we sleep if not receiving redisplays, and
            the main loop is running faster than 2khz */

         static int lasttime;
         int time = glutGet(GLUT_ELAPSED_TIME);
         if(time > lasttime) {
            if(idleiters >= 2)
               usleep(100);

            idleiters = 0;
            lasttime = time;
         }
         idleiters++;         
      }
   }
}

int ParseFBModes(int minw, int maxw, int minh, int maxh, int minf, int maxf)
{
   char buf[1024];
   struct fb_var_screeninfo vi = VarInfo;

   FILE *fbmodes = fopen(FBMODES, "r");

   if(!fbmodes) {
      sprintf(exiterror, "Warning: could not open "FBMODES"\n");
      return 0;
   }

   while(fgets(buf, sizeof buf, fbmodes)) {
      char *c;
      int v, bpp, freq;

      if(!(c = strstr(buf, "geometry")))
	 continue;
      v = sscanf(c, "geometry %d %d %d %d %d", &vi.xres, &vi.yres,
		 &vi.xres_virtual, &vi.yres_virtual, &bpp);
      if(v != 5)
	 continue;

      if(maxw < minw) {
	 if(maxw < vi.xres && minw > vi.xres)
	    continue;
      } else
	 if(maxw < vi.xres || minw > vi.xres)
	    continue;

      if(maxh < minh) {
	 if(maxh < vi.yres && minh > vi.yres)
	    continue;
      } else
	 if(maxh < vi.yres || minh > vi.yres)
	    continue;

      fgets(buf, sizeof buf, fbmodes);
      if(!(c = strstr(buf, "timings")))
	 continue;

      v = sscanf(c, "timings %d %d %d %d %d %d %d", &vi.pixclock,
		 &vi.left_margin, &vi.right_margin, &vi.upper_margin,
		 &vi.lower_margin, &vi.hsync_len, &vi.vsync_len);

      if(v != 7)
	 continue;

      freq = 1E12/vi.pixclock
	 /(vi.left_margin + vi.xres + vi.right_margin + vi.hsync_len)
	 /(vi.upper_margin + vi.yres + vi.lower_margin + vi.vsync_len);

      if(maxf < minf) {
	 if(maxf < freq && minf > freq)
	    continue;
      } else
	 if(maxf < freq || minf > freq)
	    continue;

      VarInfo = vi;
      fclose(fbmodes);
      return 1;
   }

   fclose(fbmodes);

   return 0;
}

void SetVideoMode(void)
{
   /* set new variable screen info */
   if (ioctl(FrameBufferFD, FBIOPUT_VSCREENINFO, &VarInfo)) {
      sprintf(exiterror, "FBIOPUT_VSCREENINFO failed: %s\n", strerror(errno));
      strcat(exiterror, "Perhaps the device does not support the selected mode\n");
      exit(0);
   }

   /* reload the screen info to update rgb bits */
   if (ioctl(FrameBufferFD, FBIOGET_VSCREENINFO, &VarInfo)) {
      sprintf(exiterror, "error: ioctl(FBIOGET_VSCREENINFO) failed: %s\n",
	      strerror(errno));
      exit(0);
   }

   /* reload the fixed info to update color mode */
   if (ioctl(FrameBufferFD, FBIOGET_FSCREENINFO, &FixedInfo)) {
      sprintf(exiterror, "error: ioctl(FBIOGET_FSCREENINFO) failed: %s\n",
	      strerror(errno));
      exit(0);
   }

   if (DesiredDepth && DesiredDepth !=  VarInfo.bits_per_pixel) {
      sprintf(exiterror, "error: Could not set set %d bpp\n", DesiredDepth);
      exit(0);
   }

   if(DisplayMode & GLUT_INDEX && FixedInfo.visual == FB_VISUAL_DIRECTCOLOR) {
      sprintf(exiterror, "error: Could not set 8 bit color mode\n");
      exit(0);
   }

   /* initialize colormap */
   LoadColorMap();
}

void CreateBuffer(void)
{
   int size = VarInfo.xres_virtual * VarInfo.yres_virtual
                              * VarInfo.bits_per_pixel / 8;

   /* mmap the framebuffer into our address space */
   if(FrameBuffer)
      munmap(FrameBuffer, FixedInfo.smem_len);
   FrameBuffer = mmap(0, FixedInfo.smem_len, PROT_READ | PROT_WRITE, 
		      MAP_SHARED, FrameBufferFD, 0);
   if (FrameBuffer == MAP_FAILED) {
      sprintf(exiterror, "error: unable to mmap framebuffer: %s\n",
	      strerror(errno));
      exit(0);
   }

   if(DisplayMode & GLUT_DOUBLE) {
      free(BackBuffer);
      if(!(BackBuffer = malloc(size))) {
	 sprintf(exiterror, "Failed to allocate double buffer\n");
	 exit(0);
      }
   } else
      BackBuffer = FrameBuffer;

   if(Buffer)
      glFBDevDestroyBuffer(Buffer);

   if(!(Buffer = glFBDevCreateBuffer( &FixedInfo, &VarInfo, Visual,
				      FrameBuffer, BackBuffer, size))) {
      sprintf(exiterror, "Failure to create Buffer\n");
      exit(0);
   }
}

void CreateVisual(void)
{
   int i, mask = DisplayMode;
   int attribs[20];
   for(i=0; i<sizeof(attribs)/sizeof(*attribs) && mask; i++) {
      if(mask & GLUT_DOUBLE) {
	 attribs[i] = GLFBDEV_DOUBLE_BUFFER;
	 mask &= ~GLUT_DOUBLE;
	 continue;
      }

      if(mask & GLUT_INDEX) {
	 attribs[i] = GLFBDEV_COLOR_INDEX;
	 mask &= ~GLUT_INDEX;
	 continue;
      }

      if(mask & GLUT_DEPTH) {
	 attribs[i] = GLFBDEV_DEPTH_SIZE;
	 attribs[++i] = DepthSize;
	 mask &= ~GLUT_DEPTH;
	 continue;
      }

      if(mask & GLUT_STENCIL) {
	 attribs[i] = GLFBDEV_STENCIL_SIZE;
	 attribs[++i] = StencilSize;
	 mask &= ~GLUT_STENCIL;
	 continue;
      }

      if(mask & GLUT_ACCUM) {
	 attribs[i] = GLFBDEV_ACCUM_SIZE;
	 attribs[++i] = AccumSize;
	 mask &= ~GLUT_ACCUM;
	 continue;
      }

      if(mask & GLUT_ALPHA)
	 if(!(DisplayMode & GLUT_INDEX)) {
	    mask &= ~GLUT_ALPHA;
	    i--;
	    continue;
	 }

      if(mask & GLUT_MULTISAMPLE) {
	 attribs[i] = GLFBDEV_MULTISAMPLE;
	 attribs[++i] = NumSamples;
	 mask &= ~GLUT_MULTISAMPLE;
	 continue;
      }
       
      sprintf(exiterror, "Invalid mode from glutInitDisplayMode\n");
      exit(0);
   }       

   attribs[i] = GLFBDEV_NONE;
   
   if(!(Visual = glFBDevCreateVisual( &FixedInfo, &VarInfo, attribs ))) {
      sprintf(exiterror, "Failure to create Visual\n");
      exit(0);
   }
}

static void SignalWinch(int arg)
{
   /* we can't change bitdepth without destroying the visual */
   int bits_per_pixel = VarInfo.bits_per_pixel;
   struct fb_bitfield red = VarInfo.red, green = VarInfo.green,
                      blue = VarInfo.blue, transp = VarInfo.transp;

   /* get the variable screen info */
   if (ioctl(FrameBufferFD, FBIOGET_VSCREENINFO, &VarInfo)) {
      sprintf(exiterror, "error: ioctl(FBIOGET_VSCREENINFO) failed: %s\n",
	      strerror(errno));
      exit(0);
   }

   /* restore bitdepth and color masks only */
   VarInfo.bits_per_pixel = bits_per_pixel;
   VarInfo.red = red;
   VarInfo.green = green;
   VarInfo.blue = blue;
   VarInfo.transp = transp;

   Resized = 1;
}

int glutCreateWindow (const char *title)
{
   if(Initialized == 0) {
      int argc = 0;
      char *argv[] = {NULL};
      glutInit(&argc, argv);
   }

   if(Context)
      return 0;

   if(DisplayMode & GLUT_INDEX)
      VarInfo.bits_per_pixel = 8;
   else
      if(VarInfo.bits_per_pixel == 8)
	 VarInfo.bits_per_pixel = 32;
    
   if (DesiredDepth)
      VarInfo.bits_per_pixel = DesiredDepth;

   VarInfo.xoffset = 0;
   VarInfo.yoffset = 0;
   VarInfo.nonstd = 0;
   VarInfo.vmode &= ~FB_VMODE_YWRAP; /* turn off scrolling */

   SetVideoMode();
   CreateVisual();
   CreateBuffer();

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

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

   InitializeCursor();
   InitializeMenus();

   glutSetWindowTitle(title);

   signal(SIGWINCH, SignalWinch);

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

int glutCreateSubWindow(int win, int x, int y, int width, int height)
{
   return 0;
}

void glutSetWindow(int win)
{
}

int glutGetWindow(void)
{
   return 1;
}

void glutDestroyWindow(int win)
{
   glFBDevMakeCurrent( NULL, NULL, NULL);
   glFBDevDestroyContext(Context);
   glFBDevDestroyBuffer(Buffer);
   glFBDevDestroyVisual(Visual);
  
   Visual = NULL;
}

void glutPostRedisplay(void)
{
   Redisplay = 1;
}

void glutPostWindowRedisplay(int win)
{
   Redisplay = 1;
}

void glutSwapBuffers(void)
{
   glFlush();

   if(!(DisplayMode & GLUT_DOUBLE))
      return;

   if(ActiveMenu)
      DrawMenus();
   DrawCursor();

   if(Visible) {
      Swapping = 1;
      glFBDevSwapBuffers(Buffer);
      Swapping = 0;
   }

   /* if there was a vt switch while swapping, switch now */
   if(VTSwitch) {
      if(ioctl(ConsoleFD, VT_ACTIVATE, VTSwitch) < 0)
	 sprintf(exiterror, "Error switching console\n");
      VTSwitch = 0;
   }
}

void glutPositionWindow(int x, int y) 
{
}

void glutReshapeWindow(int width, int height)
{
   if(GameMode)
      return;

   if(!ParseFBModes(width, width, height, height, 0, MAX_VSYNC))
      return;

   signal(SIGWINCH, SIG_IGN);

   SetVideoMode();
   signal(SIGWINCH, SignalWinch);
   Resized = 1;
}

void glutFullScreen(void)
{
}

void glutPopWindow(void)
{
}

void glutPushWindow(void)
{
}

void glutShowWindow(void)
{
   Visible = 1;
}

void glutHideWindow(void)
{
   Visible = 0;
}

static void UnIconifyWindow(int sig)
{
   if(ConsoleFD == 0)
      InitializeVT(1);
   else
      if(ConsoleFD > 0)
	 InitializeVT(0);
   if (ioctl(FrameBufferFD, FBIOPUT_VSCREENINFO, &VarInfo)) {
      sprintf(exiterror, "ioctl(FBIOPUT_VSCREENINFO failed): %s\n",
	      strerror(errno));
      exit(0);
   }

   RestoreColorMap();

   Redisplay = 1;
   VisibleSwitch = 1;
   Visible = 1;
}

void glutIconifyWindow(void)
{
   RestoreVT();
   signal(SIGCONT, UnIconifyWindow);
   if (ioctl(FrameBufferFD, FBIOPUT_VSCREENINFO, &OrigVarInfo))
      fprintf(stderr, "ioctl(FBIOPUT_VSCREENINFO failed): %s\n",
	      strerror(errno));

   raise(SIGSTOP);
}

void glutSetWindowTitle(const char *name)
{
   /* escape code to set title in screen */
   if(getenv("TERM") && memcmp(getenv("TERM"), "screen", 6) == 0)
      printf("\033k%s\033\\", name);
}

void glutSetIconTitle(const char *name)
{
}