Subject: Re: Fw: Command for switching wscons/resume from hibernation w/X help?
To: Daishi Harada <daishi@CS.Berkeley.EDU>
From: Tero Kivinen <kivinen@ssh.fi>
List: port-i386
Date: 05/02/2002 18:33:45
daishi@CS.Berkeley.EDU (Daishi Harada) writes:
> The simple but tedious fix when resuming is to always switch to a
> console, and switch back to X. I was hoping to automate this process
> via the /etc/apm scripts, but I can't seem to find a command which
> will perform this switch for me. The Linux command for what I want is
> chvt - is there a corresponding command in NetBSD?

I had the similar problem in my laptop (except my laptop would
immediately crash if I tried to suspend it while the X11 was running).
I created following short programs to do the thing:

switchtoscreen.c:
----------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <dev/wscons/wsdisplay_usl_io.h>

int main(int argc, char **argv)
{
  int f, data;
  char *file;

  if (argc == 1)
    data = 1;
  else
    data = atoi(argv[1]);
  if (argc <= 2)
    file = "/dev/ttyEcfg";
  else
    file = argv[2];
  if (data == 0)
    exit(3);
  f = open(file, O_RDWR, 0000);
  if (f < 0)
    {
      perror("File open failed");
      exit(1);
    }
  if (ioctl(f, VT_ACTIVATE, data) < 0)
    {
      perror("Ioctl failed");
      exit(2);
    }
  close(f);
  exit(0);
}
----------------------------------------------------------------------

currentscreen.c:
----------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <dev/wscons/wsdisplay_usl_io.h>

int main(int argc, char **argv)
{
  int f, data;
  char *file;

  if (argc == 1)
    file = "/dev/ttyEcfg";
  else
    file = argv[1];
  f = open(file, O_RDWR, 0000);
  if (f < 0)
    {
      perror("File open failed");
      exit(1);
    }
  if (ioctl(f, VT_GETACTIVE, &data) < 0)
    {
      perror("Ioctl failed");
      exit(2);
    }
  printf("%d\n", data);
  close(f);
  exit(0);
}
----------------------------------------------------------------------

The first one will switch to screen given on command line argument,
i.e usage is like "switchscreen 1" and "switchscreen 5". The second
one will return the current screen number. I used that to store the
screen I was using when suspending and then resuming to the same
screen when waking up.

I.e my apm script has following functions:
----------------------------------------------------------------------
SCREENNO=/var/run/apm-screenno

switch_to_vt0() {
    /usr/sbin/switchtoscreen 1
    while [ `/usr/sbin/currentscreen` != 1 ]
    do
	# Wait until the switchtoscreen has actually changed the screen
	sleep 1
    done
}

switch_to_back() {
    /usr/sbin/switchtoscreen `cat $SCREENNO`  
    rm -f $SCREENNO
}

store_current_screen() {
    if [ ! -f $SCREENNO ]; then
	/usr/sbin/currentscreen > $SCREENNO
    fi
}

...
case $0 in
*suspend)	
...
	store_current_screen
	switch_to_vt0
...
	;;

*resume)
...
	switch_to_back
...
	;;
----------------------------------------------------------------------

On loaded system the switchtoscreen can take few seconds before the
virtual screen has actually changed thus I wanted to make sure in the
switch_to_vt0 that we are in the vt0 (screen 1) before continuing.

Both of those should simply compile with "cc -o switchscreen
switchscreen.c" commands.
-- 
kivinen@ssh.fi
SSH Communications Security                  http://www.ssh.fi/
SSH IPSEC Toolkit                            http://www.ssh.fi/ipsec/