Package: ofbis
Version: 0.1.1
Author: Guillem Jover <guillem@hadrons.org>
Status: applied
Description:
 Allow non-root users to use the lib.

Index: src/mouse.c
===================================================================
RCS file: /usr/local/cvsroot/ofbis/src/mouse.c,v
retrieving revision 1.4
diff -u -r1.4 mouse.c
--- src/mouse.c	13 Mar 2001 20:09:16 -0000	1.4
+++ src/mouse.c	26 Jun 2003 04:04:44 -0000
@@ -40,7 +40,7 @@
   {
     calls++;
 
-    if((msefd = open(GPMDATADEVICE, O_RDWR | O_NDELAY )) != -1)
+    if((msefd = open(GPMDATADEVICE, O_RDONLY | O_NDELAY )) != -1)
     {
       devtype = DEV_TYPE_GPMDATA;
       
@@ -54,7 +54,7 @@
       /* Flush input */
       tcflush(msefd, TCIFLUSH);
     }
-    else if((msefd = open(MOUSEDEVICE, O_RDWR | O_NDELAY )) != -1)
+    else if((msefd = open(MOUSEDEVICE, O_RDONLY | O_NDELAY )) != -1)
     {
       devtype = DEV_TYPE_MOUSE;
       
Index: src/open.c
===================================================================
RCS file: /usr/local/cvsroot/ofbis/src/open.c,v
retrieving revision 1.2
diff -u -r1.2 open.c
--- src/open.c	17 Feb 2000 14:39:36 -0000	1.2
+++ src/open.c	26 Jun 2003 04:04:44 -0000
@@ -32,7 +32,10 @@
     
     fbname = getenv("FRAMEBUFFER");
     if ( !fbname ) {
-      fbname = "/dev/fb0";
+      if ( access("/dev/.devfsd", F_OK) == 0 )  /* devfs detected */
+	fbname = "/dev/fb/0";
+      else
+	fbname = "/dev/fb0";
     }
   }
 
Index: src/vt.c
===================================================================
RCS file: /usr/local/cvsroot/ofbis/src/vt.c,v
retrieving revision 1.2
diff -u -r1.2 vt.c
--- src/vt.c	17 Feb 2000 14:39:36 -0000	1.2
+++ src/vt.c	26 Jun 2003 04:04:44 -0000
@@ -19,14 +19,17 @@
 
 #include "list.h"
 
-static  char            *ttynames[]={"/dev/tty0", "/dev/tty", NULL};
-static  char            *ttyfmts[]={"/dev/tty%d", "/dev/tty%02x", "/dev/tty%x", "/dev/tty%02d", NULL};
+static  char            *ttynames[]={"/dev/vc/0", "/dev/tty0", "/dev/tty", NULL};
+static  char            *ttyfmts[]={"/dev/vc/%d", "/dev/tty%d", "/dev/tty%02x", "/dev/tty%x", "/dev/tty%02d", NULL};
 static  int             cttyname = 0;
+static  int             ctty;
 static  int             originaltty;
-static  struct vt_mode  vtm;
+static  int             kd_mode;
+static  struct vt_mode  vt_mode;
 /*static*/      unsigned short  switching;              /* Flag to prevent reentrancy */
 
 /*static*/      void FBVTswitch(int s);
+static void FBVTdetach(FB *f);
 
 static  FBList          ttylist;
 static  unsigned short  listinit = FALSE;
@@ -37,10 +40,11 @@
   char            ttynam[11];
   int             i = 0;
   struct vt_stat  vts;
+  struct vt_mode  vtm;
 
   /* Open current console */
 
-  while ( ( f->tty = open( ttynames[cttyname], O_WRONLY ) ) == -1 )
+  while ( ( ctty = open( ttynames[cttyname], O_RDWR ) ) == -1 )
   {
     FBerror( WARNING | SYSERR, "FBVTopen: open failed on %s",
              ttynames[cttyname]);
@@ -55,102 +59,88 @@
     }
   }
 
+  /* Get current VT number so we can switch back to it later */
+
+  if ( ioctl( ctty, VT_GETSTATE, &vts ) == -1 )
+  {
+    FBerror( FATAL | SYSERR, "FBVTopen: couldn't get VT state");
+  }
+  originaltty = vts.v_active;
+
   if ( f->vtchoice == FB_KEEP_CURRENT_VC )
   {
-    /* Get current VT number so we can switch back to it later */
+    f->tty = 0;
+    f->ttyno = originaltty;
+    f->keeptty = TRUE;
 
-    if ( ioctl( f->tty, VT_GETSTATE, &vts ) == -1 )
+    if ( close( ctty ) == -1 )
     {
-      FBerror( FATAL | SYSERR, "FBVTopen: couldn't get VT state");
+      FBerror( FATAL | SYSERR, "FBVTopen: failed to close controlling VT");
     }
-    f->ttyno = vts.v_active;
-    f->keeptty = TRUE;
-  }
+ }
   else if ( f->vtchoice == FB_OPEN_NEW_VC )
   {
     /* Get number of free VT */
 
-    if ( ioctl( f->tty, VT_OPENQRY, &f->ttyno ) == -1 )
+    if ( ioctl( ctty, VT_OPENQRY, &f->ttyno) == -1 )
     {
       FBerror( FATAL | SYSERR, "FBVTopen: no free ttys");
     }
     f->keeptty = FALSE;
-  }
-  else
-  {
-    FBerror( FATAL | SYSERR, "FBVTopen: please pass correct options!" );
-  }
 
-  /* Close current console */
+    /* Find an accessible VT */
 
-  if ( close( f->tty ) == -1 )
-  {
-    FBerror( WARNING | SYSERR, "FBVTopen: failed to close %s",
-             ttynames[cttyname]);
-  }
+    while ( ttyfmts[i] != NULL )
+    {
+      (void) sprintf( ttynam, ttyfmts[i++], f->ttyno );
+      chown( ttynam, getuid(), getgid() );
+      if ( access( ttynam, R_OK | W_OK ) != -1 )
+      {
+        break;
+      }
+    }
 
-  /* Open new VT */
+    if ( close( ctty ) == -1 )
+    {
+      FBerror( FATAL | SYSERR, "FBVTopen: failed to close controlling VT");
+    }
 
-  do
-  {
-    (void) sprintf( ttynam, ttyfmts[i++], f->ttyno );
-  }
-  while ( ( ttyfmts[i] != NULL ) &&
-          ( f->tty = open( ttynam, O_RDONLY )) == -1 );
+    /* Detach from our controlling terminal */
 
-  if ( f->tty == -1 )
-  {
-    FBerror( FATAL | SYSERR, "FBVTopen: failed to open %s", ttynam);
-  }
+    FBVTdetach( f );
+
+    /* Open new VT */
 
-  /* junk from Xserver... clean up later */
+    close( 0 );
+    close( 1 );
+    close( 2 );
 
-  /*if (!f->keeptty)
+    f->tty = open( ttynam, O_RDWR );
+    if ( f->tty == -1 )
     {
-    setpgrp();
+      FBerror( FATAL | SYSERR, "FBVTopen: failed to open %s", ttynam);
     }
-    chown(ttynam,getuid(),getgid());
-    chown("/dev/console",getuid(),getgid());
-    chown("/dev/tty0",getuid(),getgid());*/
+    dup( 0 );
+    dup( 0 );
 
-  /* This seems necessary for us to do, but I'm still not sure why. */
-#if 0
-  if (f->keeptty)
-#endif
+    /* Switch to new VT */
+
+    if ( ioctl( f->tty, VT_ACTIVATE, f->ttyno ) == -1 )
     {
-      int i;
-      if ((i=open("/dev/tty",O_RDWR))>=0)
-	{
-	  ioctl(i,TIOCNOTTY,0);
-	  close(i);
-	}
-      /*setsid();*/
-      /*ioctl(f->tty,TIOCSCTTY);*/
+      FBerror( FATAL | SYSERR, "FBVTopen: couldn't switch to VT %d", f->ttyno);
     }
 
-  /* Get current VT number so we can switch back to it later */
-
-  if ( ioctl( f->tty, VT_GETSTATE, &vts ) == -1 )
-  {
-    FBerror( FATAL | SYSERR, "FBVTopen: couldn't get VT state");
-  }
-  originaltty = vts.v_active;
+    /* Wait for new VT to become active */
 
-  /* Switch to new VT */
-        
-  if ( ioctl( f->tty, VT_ACTIVATE, f->ttyno ) == -1 )
-  {
-    FBerror( FATAL | SYSERR, "FBVTopen: couldn't switch to VT %d", f->ttyno);
+    if ( ioctl( f->tty, VT_WAITACTIVE, f->ttyno ) == -1 )
+    {
+      FBerror( FATAL | SYSERR, "FBVTopen: VT %d didn't become active", f->ttyno);
+    }
   }
-
-  /* Wait for new VT to become active */
-
-  if ( ioctl( f->tty, VT_WAITACTIVE, f->ttyno ) == -1 )
+  else
   {
-    FBerror( FATAL | SYSERR, "FBVTopen: VT %d didn't become active", f->ttyno);
+    FBerror( FATAL | SYSERR, "FBVTopen: please pass correct options!" );
   }
-  f->visible=TRUE;
-  f->drawing=FALSE;
 
   /* Get mode of new VT */
 
@@ -176,9 +166,15 @@
     FBerror( FATAL | SYSERR, "FBVTopen: Couldn't set mode of VT %d", f->ttyno);
   }
 
+  /* Save current keybard mode */
+
+  if ( ioctl( f->tty, KDGETMODE, &kd_mode ) == -1 )
+  {
+    FBerror( FATAL | SYSERR, "FBVTopen: Couldn't get keyboard mode on VT %d", f->ttyno);
+  }
+
   /* Disable cursor */
 
-        
   if ( ioctl( f->tty, KDSETMODE, KD_GRAPHICS ) == -1 )
   {
     FBerror( FATAL | SYSERR, "FBVTopen: Couldn't set keyboard graphics mode on VT %d", f->ttyno);
@@ -200,6 +196,56 @@
 
     FBkbdopen( f );
   }
+
+  f->visible=TRUE;
+  f->drawing=FALSE;
+
+}
+
+static void
+FBVTdetach(FB *f)
+{
+#if 1
+  int child;
+
+  /* Fork to background */
+
+  child = fork();
+
+  if ( child < 0 )
+  {
+    FBerror( FATAL | SYSERR, "FBVTdetach: Failed to fork" );
+  }
+  else if ( child )
+  {
+    exit( 0 );
+  }
+
+  setsid();
+#else
+  int ctty;
+  struct vt_stat vts;
+
+  ctty = open( "/dev/tty", O_RDONLY );
+  if ( ctty < 0 )
+  {
+    FBerror( FATAL | SYSERR, "FBVTdetach: Couldn't open /dev/tty");
+  }
+
+  if ( ioctl( ctty, VT_GETSTATE, &vts ) == -1 )
+  {
+    return;
+  }
+
+  /* Detach from our controlling terminal, and start a new session */
+
+  if ( ioctl( ctty, TIOCNOTTY, 0) == -1 )
+  {
+    FBerror( FATAL | SYSERR, "FBVTdetach: Couldn't detach from controlling terminal");
+  }
+
+  close( ctty );
+#endif
 }
 
 FB *
@@ -316,7 +362,7 @@
 
     if ( ioctl(f->tty, VT_RELDISP, 1) == -1 )
     {
-      FBerror( FATAL | SYSERR, "FBVTswitch: switch away from VT %d denied", f->tty);
+      FBerror( FATAL | SYSERR, "FBVTswitch: switch away from VT %d denied", f->ttyno);
     }
   }
   else
@@ -324,7 +370,7 @@
     /* Acknowledge switch back to this VT */
     if ( ioctl(f->tty, VT_RELDISP, VT_ACKACQ) == -1 )
     {
-      FBerror( FATAL | SYSERR, "FBVTswitch: switch to VT %d denied", f->tty);
+      FBerror( FATAL | SYSERR, "FBVTswitch: switch to VT %d denied", f->ttyno);
     }
 
     /* Map framebuffer back into memory */
@@ -372,67 +418,56 @@
 void
 FBVTclose(FB *f)
 {
-  if (f->handle_kbd) {
-    /* Close keyboard */
-
-    FBkbdclose( f );
-  }
-
-  /* If backing store allocated, free it */
-
-  if (f->sbak)
-  {
-    FBfree(f->sbak);
-  }
-
   /* Enable cursor */
 
-  if ( ioctl( f->tty, KDSETMODE, KD_TEXT ) == -1 )
+  if ( ioctl( f->tty, KDSETMODE, kd_mode ) == -1 )
   {
-    FBerror( FATAL | SYSERR, "FBVTclose: Couldn't set keyboard graphics mode on VT %d", f->ttyno);
+    FBerror( FATAL | SYSERR, "FBVTclose: Couldn't restore keyboard mode on VT %d", f->ttyno);
   }
 
   /* Restore mode parameters */
 
-  vtm.mode = VT_AUTO;
+  if ( ioctl( f->tty, VT_SETMODE, &vt_mode ) == -1 )
+  {
+    FBerror( FATAL | SYSERR, "FBVTclose: Couldn't restore mode of VT %d", f->ttyno);
+  }
 
-  /* Set new mode parameters */
+  if (f->handle_kbd) {
+    /* Close keyboard */
 
-  if ( ioctl( f->tty, VT_SETMODE, &vtm ) == -1 )
-  {
-    FBerror( FATAL | SYSERR, "FBVTclose: Couldn't set mode of VT %d", f->ttyno);
+    FBkbdclose( f );
   }
 
-  /* Switch back to original VT */
+  /* If backing store allocated, free it */
 
-  if ( ioctl( f->tty, VT_ACTIVATE, originaltty ) == -1 )
+  if (f->sbak)
   {
-    FBerror( FATAL | SYSERR, "FBVTclose: couldn't switch to VT %d", originaltty);
+    FBfree(f->sbak);
   }
-  if ( ioctl( f->tty, VT_WAITACTIVE, originaltty ) == -1 )
+
+  if ( !f->keeptty )
   {
-    FBerror( FATAL | SYSERR, "FBVTclose: couldn't wait for VT %d", originaltty);
-  }
+    /* Switch back to original VT */
 
-#if 0
-  /* Deallocate VT if we opened a new one */
-  if(f->vtchoice == FB_OPEN_NEW_VC)
-    {  
-      if(ioctl(f->tty, VT_DISALLOCATE, f->ttyno) == -1 )
-	{
-	  FBerror( WARNING | SYSERR, "FBVTclose: couldn't deallocate VT %d", f->ttyno);
-	}
+    if ( ioctl( f->tty, VT_ACTIVATE, originaltty ) == -1 )
+    {
+      FBerror( FATAL | SYSERR, "FBVTclose: couldn't switch to VT %d", originaltty);
+    }
+    if ( ioctl( f->tty, VT_WAITACTIVE, originaltty ) == -1 )
+    {
+      FBerror( FATAL | SYSERR, "FBVTclose: couldn't wait for VT %d", originaltty);
     }
-#endif
 
-  /* Close VT */
+    /* Close VT */
 
-  if ( close( f->tty ) == -1 )
-  {
-    FBerror( FATAL | SYSERR, "FBVTclose: failed to close VT");
+    if ( close( f->tty ) == -1 )
+    {
+      FBerror( FATAL | SYSERR, "FBVTclose: failed to close VT");
+    }
   }
 
   /* Remove from ttylist */
 
   ttylist = FBListRemoveKey( ttylist, f->ttyno );
 }
+
