| Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: |
| 2 | * |
| Rob Landley | 5c4a217 | 2008-01-26 23:18:08 -0600 | [diff] [blame] | 3 | * chvt.c - switch virtual terminals |
| 4 | * |
| Rob Landley | daa7fe6 | 2008-01-06 16:01:11 -0600 | [diff] [blame] | 5 | * Copyright (C) 2008 David Anders <danders@amltd.com> |
| 6 | * |
| Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 7 | * Not in SUSv3. |
| 8 | |
| Rob Landley | 55928b1 | 2008-01-19 17:43:27 -0600 | [diff] [blame] | 9 | USE_CHVT(NEWTOY(chvt, "<1", TOYFLAG_USR|TOYFLAG_SBIN)) |
| 10 | |
| Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 11 | config CHVT |
| 12 | bool "chvt" |
| 13 | default y |
| 14 | help |
| 15 | usage: chvt N |
| 16 | |
| 17 | Change to virtual terminal number N. (This only works in text mode.) |
| 18 | |
| 19 | Virtual terminals are the Linux VGA text mode displays, ordinarily |
| 20 | switched between via alt-F1, alt-F2, etc. Use ctrl-alt-F1 to switch |
| 21 | from X to a virtual terminal, and alt-F6 (or F7, or F8) to get back. |
| 22 | */ |
| Rob Landley | daa7fe6 | 2008-01-06 16:01:11 -0600 | [diff] [blame] | 23 | |
| 24 | #include "toys.h" |
| 25 | |
| Rob Landley | 5c4a217 | 2008-01-26 23:18:08 -0600 | [diff] [blame] | 26 | /* Note: get_console_fb() will need to be moved into a seperate lib section */ |
| Rob Landley | daa7fe6 | 2008-01-06 16:01:11 -0600 | [diff] [blame] | 27 | int get_console_fd() |
| 28 | { |
| Rob Landley | 5c4a217 | 2008-01-26 23:18:08 -0600 | [diff] [blame] | 29 | int fd; |
| Rob Landley | 839fb0b | 2008-10-24 23:19:38 -0500 | [diff] [blame] | 30 | char *consoles[]={"/dev/console", "/dev/vc/0", "/dev/tty", NULL}, **cc; |
| Rob Landley | daa7fe6 | 2008-01-06 16:01:11 -0600 | [diff] [blame] | 31 | |
| Rob Landley | 839fb0b | 2008-10-24 23:19:38 -0500 | [diff] [blame] | 32 | cc = consoles; |
| 33 | while (*cc) { |
| 34 | fd = open(*cc++, O_RDWR); |
| 35 | if (fd >= 0) return fd; |
| 36 | } |
| Rob Landley | daa7fe6 | 2008-01-06 16:01:11 -0600 | [diff] [blame] | 37 | |
| Rob Landley | 5c4a217 | 2008-01-26 23:18:08 -0600 | [diff] [blame] | 38 | return -1; |
| Rob Landley | daa7fe6 | 2008-01-06 16:01:11 -0600 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void chvt_main(void) |
| 42 | { |
| Rob Landley | 5c4a217 | 2008-01-26 23:18:08 -0600 | [diff] [blame] | 43 | int vtnum, fd; |
| Rob Landley | daa7fe6 | 2008-01-06 16:01:11 -0600 | [diff] [blame] | 44 | |
| Rob Landley | 5c4a217 | 2008-01-26 23:18:08 -0600 | [diff] [blame] | 45 | vtnum=atoi(*toys.optargs); |
| Rob Landley | daa7fe6 | 2008-01-06 16:01:11 -0600 | [diff] [blame] | 46 | |
| Rob Landley | 5c4a217 | 2008-01-26 23:18:08 -0600 | [diff] [blame] | 47 | fd=get_console_fd(); |
| Rob Landley | 839fb0b | 2008-10-24 23:19:38 -0500 | [diff] [blame] | 48 | // These numbers are VT_ACTIVATE and VT_WAITACTIVE from linux/vt.h |
| 49 | if (fd < 0 || ioctl(fd, 0x5606, vtnum) || ioctl(fd, 0x5607, vtnum)) |
| Rob Landley | 651e253 | 2008-01-27 15:26:32 -0600 | [diff] [blame] | 50 | perror_exit(NULL); |
| Rob Landley | daa7fe6 | 2008-01-06 16:01:11 -0600 | [diff] [blame] | 51 | } |