summaryrefslogtreecommitdiff
path: root/libcons/cons-switch.c
blob: f2b72daf9ae26d77461f137742b63ed4106462d6 (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
/* cons-switch.c - Switch to another virtual console.
   Copyright (C) 2002 Free Software Foundation, Inc.
   Written by Marcus Brinkmann.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   The GNU Hurd 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
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA. */

#include <errno.h>
#include <error.h>
#include <assert.h>

#include "cons.h"

/* Switch the active console in CONS to ID or the current one plus
   DELTA.  This will call back into the user code by doing a
   cons_vcons_activate.  */
error_t
cons_switch (cons_t cons, int id, int delta)
{
  vcons_t vcons = NULL;
  vcons_t active;

  if (!id && !delta)
    return 0;

  mutex_lock (&cons->lock);
  active = cons->active;

  if (!id && !active)
    {
      mutex_unlock (&cons->lock);
      return EINVAL;
    }

  if (id)
    {
      vcons = cons->vcons_list;
      while (vcons && vcons->id != id)
	vcons = vcons->next;
    }
  else if (delta > 0)
    {
      vcons = cons->active;
      while (delta-- > 0)
	{
	  vcons = vcons->next;
	  if (!vcons)
	    vcons = cons->vcons_list;
	}
    }
  else
    {
      assert (delta < 0);
      while (delta++ < 0)
	{
	  vcons = vcons->prev;
	  if (!vcons)
	    vcons = cons->vcons_last;
	}
    }

  if (!vcons)
    {
      mutex_unlock (&cons->lock);
      return ESRCH;
    }

  if (vcons != active)
    {
      error_t err = cons_vcons_activate (vcons);
      if (err)
	{
	  mutex_unlock (&cons->lock);
	  return err;
	}

      cons->active = vcons;
      cons_vcons_refresh (vcons);
    }
  mutex_unlock (&cons->lock);
  return 0;
}