2 winnie - an experimental window system
4 Copyright (C) 2013 Eleni Maria Stea
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 Author: Eleni Maria Stea <elene.mst@gmail.com>
29 #include <sys/ioctl.h>
40 enum {RAW, CANONICAL} ttystate;
43 static Keyboard *keyboard;
47 if(!(keyboard = (Keyboard*)sh_malloc(sizeof *keyboard))) {
51 keyboard->ttystate = keyboard->CANONICAL;
52 keyboard->dev_fd = -1;
54 if((keyboard->dev_fd = open("/dev/tty", O_RDWR)) == -1) {
55 fprintf(stderr, "Cannot open /dev/tty : %s\n", strerror(errno));
61 if(tcgetattr(keyboard->dev_fd, &buf) < 0) {
62 fprintf(stderr, "Cannot get the tty parameters : %s\n", strerror(errno));
66 buf.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
67 buf.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
68 buf.c_cflag &= ~(CSIZE | PARENB);
70 buf.c_oflag &= ~(OPOST);
72 if(tcsetattr(keyboard->dev_fd, TCSAFLUSH, &buf) < 0) {
76 keyboard->ttystate = keyboard->RAW;
80 void destroy_keyboard()
84 if(tcgetattr(keyboard->dev_fd, &buf) < 0) {
85 fprintf(stderr, "Cannot get the tty parameters : %s\n", strerror(errno));
88 buf.c_lflag |= (ECHO | ICANON | IEXTEN | ISIG);
89 buf.c_iflag |= (BRKINT | ICRNL | INPCK | ISTRIP | IXON);
90 buf.c_cflag |= (CSIZE | PARENB);
92 buf.c_oflag |= (OPOST);
94 if(tcsetattr(keyboard->dev_fd, TCSAFLUSH, &buf) < 0) {
95 fprintf(stderr, "Cannot set the tty parameters : %s\n", strerror(errno));
98 keyboard->ttystate = keyboard->CANONICAL;
100 if(keyboard->dev_fd != -1) {
101 close(keyboard->dev_fd);
102 keyboard->dev_fd = -1;
108 int get_keyboard_fd()
110 return keyboard->dev_fd;
113 void process_keyboard_event()
116 if(read(keyboard->dev_fd, &key, 1) < 1) {
124 Window *focused_win = wm->get_focused_window();
126 KeyboardFuncType keyb_callback = focused_win->get_keyboard_callback();
128 keyb_callback(focused_win, key, true); //TODO: true??
133 * - handle system-wide key combinations (alt-tab?)
134 * - otherwise send keypress/release to focused window
137 #endif // WINNIE_FBDEV