Home | History | Annotate | Download | only in dri
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "ui/ozone/platform/dri/virtual_terminal_manager.h"
      6 
      7 #include <errno.h>
      8 #include <fcntl.h>
      9 #include <linux/kd.h>
     10 #include <linux/vt.h>
     11 #include <sys/ioctl.h>
     12 #include <unistd.h>
     13 
     14 #include "base/logging.h"
     15 
     16 namespace ui {
     17 
     18 namespace {
     19 
     20 const char kTTYDevice[] = "/dev/tty1";
     21 
     22 const int kVT = 1;
     23 
     24 }  // namespace
     25 
     26 VirtualTerminalManager::VirtualTerminalManager() {
     27   // Use the current console.
     28   fd_ = open(kTTYDevice, O_RDWR | O_CLOEXEC, 0);
     29   if (fd_ < 0)
     30     LOG(ERROR) << "Failed to open '" << kTTYDevice << "' " << strerror(errno);
     31 
     32   if (ioctl(fd_, VT_ACTIVATE, kVT) || ioctl(fd_, VT_WAITACTIVE, kVT))
     33     LOG(ERROR) << "Failed to switch to VT: " << kVT
     34                << " error: " << strerror(errno);;
     35 
     36   if (ioctl(fd_, KDGETMODE, &vt_mode_))
     37     LOG(ERROR) << "Failed to get VT mode: " << strerror(errno);
     38 
     39   if (ioctl(fd_, KDSETMODE, KD_GRAPHICS))
     40     LOG(ERROR) << "Failed to set graphics mode: " << strerror(errno);
     41 
     42   if (tcgetattr(fd_, &terminal_attributes_))
     43     LOG(ERROR) << "Failed to get terminal attributes";
     44 
     45   // Stop the TTY from processing keys and echo-ing them to the terminal.
     46   struct termios raw_attributes = terminal_attributes_;
     47   cfmakeraw(&raw_attributes);
     48   raw_attributes.c_oflag |= OPOST;
     49   if (tcsetattr(fd_, TCSANOW, &raw_attributes))
     50     LOG(ERROR) << "Failed to set raw attributes";
     51 
     52   if (ioctl(fd_, KDGKBMODE, &previous_keyboard_mode_))
     53     LOG(ERROR) << "Failed to get keyboard mode";
     54 
     55   if (ioctl(fd_, KDSKBMODE, K_OFF) && ioctl(fd_, KDSKBMODE, K_RAW))
     56     LOG(ERROR) << "Failed to set keyboard mode";
     57 }
     58 
     59 VirtualTerminalManager::~VirtualTerminalManager() {
     60   if (fd_ < 0)
     61     return;
     62 
     63   if (ioctl(fd_, KDSETMODE, &vt_mode_))
     64     LOG(ERROR) << "Failed to restore VT mode";
     65 
     66   if (ioctl(fd_, KDSKBMODE, previous_keyboard_mode_))
     67     LOG(ERROR) << "Failed to restore keyboard mode";
     68 
     69   if (tcsetattr(fd_, TCSANOW, &terminal_attributes_))
     70     LOG(ERROR) << "Failed to restore terminal attributes";
     71 
     72   close(fd_);
     73 }
     74 
     75 }  // namespace ui
     76