3

I'm wondering how to programmatically change to one of the available terminals from an embedded Qt application.

The application is invoked via application -qws. Graphically it is configured with the Linux framebuffer (support compiled into the kernel) with the tty drivers enabled.

The reason for doing this programmatically is that Ctrl+Alt+FX does not work to switch ttys.

Firstly, I tried the obvious cheating mechanism - overriding keyPressEvent I added system("chvt 2"); Unfortunately, this did not work.

Instead, I looked directly at the source of chvt.c in busybox. Essentially, that boiled down to doing:

fd = open("/dev/console", O_RDRW);
ioctl(fd, VT_ACTIVATE, vt_num);
ioctl(fd, VT_WAITACTIVE, vt_num);

Ignoring the use of xioctl and detecting the correct tty - I know /dev/console exists and I can get a RW file handle on it.

Again, no luck.

So taking my cue from chvt.c I decided to iterate over all the framebuffer devices present until I found one that was receiving keyboard input; then I ran

fd = open("/dev/fb1", O_RDRW);
ioctl(fd, VT_ACTIVATE, vt_num);
ioctl(fd, VT_WAITACTIVE, vt_num);

That caused a black screen as per any normal tty change, except that the framebuffer device returned afterwards, bringing my app back with it.

So - my question - without exiting my application, I'd like to be able to programmatically switch to a tty of my choice, and then be able to switch back using the usual ctrl+alt+fX mechanism.

Environment:

  • Qt 4.7.4
  • Linux 3.3.3
  • Buildroot 2011.11
  • Busybox 1.19
  • uClibc 2.21

I suspect, but am not certain, that the cause of ctrl+alt+fX not working in the first place may be the cause of the tty change not working programmatically as per chvt.

I should point out at this stage that, confusingly, the zap keys (ctrl+alt+backspace) do work as expected.

  • Have you seen [this question](http://superuser.com/questions/69807/why-does-chvt-work-fine-as-a-regular-user-on-a-virtual-terminal-but-not-under-x)? I take it you are not running in a X session, but still, could be related. – Michael Wild Apr 25 '12 at 15:26
  • I'm not running X at all; also, I can open a file descriptor to `/dev/console` - it's just that ioctl'ing them doesn't appear to *do* anything. uClibc includes several tricks for circumnavigating this in `console_make_active` - I've skipped all that because it works without needing it - because I don't have X. Also, this should be a comment. –  Apr 25 '12 at 15:41

0 Answers0