Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

htkeyb.cc

Go to the documentation of this file.
00001 /*
00002  *      HT Editor
00003  *      syskeyb.cc - keyboard access functions for POSIX
00004  *
00005  *      Copyright (C) 1999-2002 Stefan Weyergraf (stefan@weyergraf.de)
00006  *
00007  *      This program is free software; you can redistribute it and/or modify
00008  *      it under the terms of the GNU General Public License version 2 as
00009  *      published by the Free Software Foundation.
00010  *
00011  *      This program is distributed in the hope that it will be useful,
00012  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *      GNU General Public License for more details.
00015  *
00016  *      You should have received a copy of the GNU General Public License
00017  *      along with this program; if not, write to the Free Software
00018  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 #include "config.h"
00022 #include CURSES_HDR
00023 #include "htkeyb.h"
00024 
00025 #ifdef HAVE_TEXTMODE_X11
00026 #define META_KEY(c)    ((c) |   0x80000000)
00027 #define UNMETA_KEY(c)  ((c) & (~0x80000000))
00028 #define SHIFT_KEY(c)   ((c) |   0x40000000)
00029 #define UNSHIFT_KEY(c) ((c) & (~0x40000000))
00030 #define CTRL_KEY(c)    ((c) |   0x20000000)
00031 #define UNCTRL_KEY(c)  ((c) & (~0x20000000))
00032 #else
00033 #define META_KEY(c)    ((c) |   0x80000000)
00034 #define UNMETA_KEY(c)  ((c) & (~0x80000000))
00035 #define SHIFT_KEY(c)   ((c) |   0x80000000)
00036 #define UNSHIFT_KEY(c) ((c) & (~0x80000000))
00037 #define CTRL_KEY(c)    ((((c)>='a') && ((c)<='z')) ? (c-'a'+1) : ((c) | 0x80000000))
00038 #define UNCTRL_KEY(c)  ((c) & (~0x80000000))
00039 #endif
00040 
00041 #define CTRL_ALPHA_KEY(c) ((c)-'a'+1)
00042 
00043 #ifdef HAVE_TEXTMODE_X11
00044 #include <X11/Xlib.h>
00045 
00046 static Display *Xdisplay;
00047 static Window Xwindow;
00048 #endif /* HAVE_TEXTMODE_X11 */
00049 
00050 void sys_get_driver_desc(char *buf)
00051 {
00052 #ifdef HAVE_TEXTMODE_X11
00053         sprintf(buf, "POSIX/NCurses/X11 (X server %sconnected)", Xdisplay ? "" : "NOT ");
00054 #else
00055         sprintf(buf, "POSIX/NCurses");
00056 #endif  
00057 }
00058 
00059 static int get_modifier(int key)
00060 {
00061 #ifdef HAVE_TEXTMODE_X11
00062         if (Xdisplay) {
00063                 Window root, child;
00064                 int root_x, root_y;
00065                 int win_x, win_y;
00066                 unsigned int mask;
00067                 Bool b;
00068                 int result = key;
00069 
00070                 b = XQueryPointer(Xdisplay, Xwindow, &root, &child,
00071                                     &root_x, &root_y,
00072                                     &win_x, &win_y,
00073                                     &mask);
00074 
00075                 if (mask & ShiftMask) result = SHIFT_KEY(result);
00076                 if (mask & ControlMask) result = CTRL_KEY(result);
00077                 return result;
00078         }
00079 #endif
00080         return key;
00081 }
00082 
00083 static UINT escseq2rawkey(UINT r)
00084 {
00085         switch (r) {
00086                 case 'H': return KEY_HOME;
00087                 case 'F': return KEY_END;
00088                 case 'P': return KEY_F(1);
00089                 case 'Q': return KEY_F(2);
00090                 case 'R': return KEY_F(3);
00091                 case 'S': return KEY_F(4);
00092         }
00093         return K_INVALID;
00094 }
00095 
00096 ht_key ht_getkey()
00097 {
00098         int r = ht_raw_getkey();
00099         ht_key k = K_INVALID;
00100         int r2 = UNMETA_KEY(UNCTRL_KEY(UNSHIFT_KEY(r)));
00101         if ((META_KEY(r) == r) && ((r2 == '[') || (r2 == 'O'))) {/* escape seq */
00102                 r2 = r;
00103                 if (ht_keypressed()) {
00104                         r = ht_raw_getkey();
00105                         r = escseq2rawkey(r);
00106 #ifdef HAVE_TEXTMODE_X11
00107                         if (CTRL_KEY(r2) == r2) r = CTRL_KEY(r);
00108                         if (SHIFT_KEY(r2) == r2) r = SHIFT_KEY(r);
00109 #endif
00110                 }
00111         }
00112         k = ht_rawkey2key(r);
00113         if ((k == K_INVALID) && ((unsigned int)r <= 255)) return (ht_key)r;
00114 //debug:        if (k == K_INVALID) return (ht_key)r;
00115         return k;
00116 }
00117 
00118 struct key_keycode {
00119         ht_key key;
00120         int keycode;
00121 };
00122 
00123 bool ht_keypressed()
00124 {
00125         int i = getch();
00126         if (i != -1) ungetch(i);
00127         return (i != -1);
00128 }
00129 
00130 int ht_raw_getkey()
00131 {
00132         int c = getch();
00133         if (c == '\e') {
00134                 c = getch();
00135                 if ((c == -1) || (c == '\e')) c = '\e'; else c = META_KEY(c);
00136         }
00137         if ((unsigned int)c>=0x100) c = get_modifier(c);
00138 //      fprintf(stderr, "getrawkey() %d/0x%x\n", c, c);
00139         return c;
00140 }
00141 
00142 static key_keycode curses_key_defs[] = {
00143         {K_Return,              '\n'},
00144         {K_Delete,              KEY_DC},
00145         {K_Insert,              KEY_IC},
00146         {K_Backspace,           KEY_BACKSPACE},
00147         {K_Backspace,           8},
00148         {K_Backspace,           127},
00149         {K_Alt_Backspace,       META_KEY(KEY_BACKSPACE)},
00150         {K_Alt_Backspace,       META_KEY(8)},
00151         {K_Alt_Backspace,       META_KEY(127)},
00152 
00153         {K_F1,                  KEY_F(1)},
00154         {K_F2,                  KEY_F(2)},
00155         {K_F3,                  KEY_F(3)},
00156         {K_F4,                  KEY_F(4)},
00157         {K_F5,                  KEY_F(5)},
00158         {K_F6,                  KEY_F(6)},
00159         {K_F7,                  KEY_F(7)},
00160         {K_F8,                  KEY_F(8)},
00161         {K_F9,                  KEY_F(9)},
00162         {K_F10,                 KEY_F(10)},
00163         {K_F11,                 KEY_F(11)},
00164         {K_F12,                 KEY_F(12)},
00165         {K_Home,                KEY_HOME},
00166         {K_End,                 KEY_END},
00167         {K_Up,                  KEY_UP},
00168         {K_Down,                KEY_DOWN},
00169         {K_Left,                KEY_LEFT},
00170         {K_Right,               KEY_RIGHT},
00171         {K_PageUp,              KEY_PPAGE},
00172         {K_PageDown,            KEY_NPAGE},
00173         {K_Tab,                 '\t'},
00174 //      {K_Shift_Tab,           SHIFT_KEY('\t')},
00175         {K_Control_PageUp,      CTRL_KEY(KEY_PPAGE)},
00176         {K_Control_PageDown,    CTRL_KEY(KEY_NPAGE)},
00177         {K_Control_Left,        CTRL_KEY(KEY_LEFT)},
00178         {K_Control_Right,       CTRL_KEY(KEY_RIGHT)},
00179         {K_Control_Up,          CTRL_KEY(KEY_UP)},
00180         {K_Control_Down,        CTRL_KEY(KEY_DOWN)},
00181         {K_Control_Insert,      CTRL_KEY(KEY_IC)},
00182         {K_Control_Delete,      CTRL_KEY(KEY_DC)},
00183         {K_Control_Home,        CTRL_KEY(KEY_HOME)},
00184         {K_Control_End,         CTRL_KEY(KEY_END)},
00185         {K_Shift_Up,            SHIFT_KEY(KEY_UP)},
00186         {K_Shift_Down,          SHIFT_KEY(KEY_DOWN)},
00187         {K_Shift_Left,          SHIFT_KEY(KEY_LEFT)},
00188         {K_Shift_Right,         SHIFT_KEY(KEY_RIGHT)},
00189         {K_Shift_PageUp,        SHIFT_KEY(KEY_PPAGE)},
00190         {K_Shift_PageDown,      SHIFT_KEY(KEY_NPAGE)},
00191         {K_Shift_Delete,        SHIFT_KEY(KEY_DC)},
00192         {K_Shift_Insert,        SHIFT_KEY(KEY_IC)},
00193         {K_Shift_Home,          SHIFT_KEY(KEY_HOME)},
00194         {K_Shift_End,           SHIFT_KEY(KEY_END)},
00195 
00196         {K_Alt_1,               META_KEY('1')},
00197         {K_Alt_2,               META_KEY('2')},
00198         {K_Alt_3,               META_KEY('3')},
00199         {K_Alt_4,               META_KEY('4')},
00200         {K_Alt_5,               META_KEY('5')},
00201         {K_Alt_6,               META_KEY('6')},
00202         {K_Alt_7,               META_KEY('7')},
00203         {K_Alt_8,               META_KEY('8')},
00204         {K_Alt_9,               META_KEY('9')},
00205         {K_Alt_0,               META_KEY('0')},
00206 
00207         {K_Control_A,           CTRL_ALPHA_KEY('a')},
00208         {K_Control_B,           CTRL_ALPHA_KEY('b')},
00209         {K_Control_C,           CTRL_ALPHA_KEY('c')},
00210         {K_Control_D,           CTRL_ALPHA_KEY('d')},
00211         {K_Control_E,           CTRL_ALPHA_KEY('e')},
00212         {K_Control_F,           CTRL_ALPHA_KEY('f')},
00213         {K_Control_G,           CTRL_ALPHA_KEY('g')},
00214         {K_Control_H,           CTRL_ALPHA_KEY('h')},
00215         {K_Control_I,           CTRL_ALPHA_KEY('i')},
00216         {K_Control_J,           CTRL_ALPHA_KEY('j')},
00217         {K_Control_K,           CTRL_ALPHA_KEY('k')},
00218         {K_Control_L,           CTRL_ALPHA_KEY('l')},
00219         {K_Control_M,           CTRL_ALPHA_KEY('m')},
00220         {K_Control_N,           CTRL_ALPHA_KEY('n')},
00221         {K_Control_O,           CTRL_ALPHA_KEY('o')},
00222         {K_Control_P,           CTRL_ALPHA_KEY('p')},
00223         {K_Control_Q,           CTRL_ALPHA_KEY('q')},
00224         {K_Control_R,           CTRL_ALPHA_KEY('r')},
00225         {K_Control_S,           CTRL_ALPHA_KEY('s')},
00226         {K_Control_T,           CTRL_ALPHA_KEY('t')},
00227         {K_Control_U,           CTRL_ALPHA_KEY('u')},
00228         {K_Control_V,           CTRL_ALPHA_KEY('v')},
00229         {K_Control_W,           CTRL_ALPHA_KEY('w')},
00230         {K_Control_X,           CTRL_ALPHA_KEY('x')},
00231         {K_Control_Y,           CTRL_ALPHA_KEY('y')},
00232         {K_Control_Z,           CTRL_ALPHA_KEY('z')},
00233 
00234         {K_Alt_A,               META_KEY('a')},
00235         {K_Alt_B,               META_KEY('b')},
00236         {K_Alt_C,               META_KEY('c')},
00237         {K_Alt_D,               META_KEY('d')},
00238         {K_Alt_E,               META_KEY('e')},
00239         {K_Alt_F,               META_KEY('f')},
00240         {K_Alt_G,               META_KEY('g')},
00241         {K_Alt_H,               META_KEY('h')},
00242         {K_Alt_I,               META_KEY('i')},
00243         {K_Alt_J,               META_KEY('j')},
00244         {K_Alt_K,               META_KEY('k')},
00245         {K_Alt_L,               META_KEY('l')},
00246         {K_Alt_M,               META_KEY('m')},
00247         {K_Alt_N,               META_KEY('n')},
00248         {K_Alt_O,               META_KEY('o')},
00249         {K_Alt_P,               META_KEY('p')},
00250         {K_Alt_Q,               META_KEY('q')},
00251         {K_Alt_R,               META_KEY('r')},
00252         {K_Alt_S,               META_KEY('s')},
00253         {K_Alt_T,               META_KEY('t')},
00254         {K_Alt_U,               META_KEY('u')},
00255         {K_Alt_V,               META_KEY('v')},
00256         {K_Alt_W,               META_KEY('w')},
00257         {K_Alt_X,               META_KEY('x')},
00258         {K_Alt_Y,               META_KEY('y')},
00259         {K_Alt_Z,               META_KEY('z')},
00260 
00261 /*      {K_Shift_F1,            SHIFT_KEY(KEY_F(1))},
00262         {K_Shift_F2,            SHIFT_KEY(KEY_F(2))},
00263         {K_Shift_F3,            SHIFT_KEY(KEY_F(3))},
00264         {K_Shift_F4,            SHIFT_KEY(KEY_F(4))},
00265         {K_Shift_F5,            SHIFT_KEY(KEY_F(5))},
00266         {K_Shift_F6,            SHIFT_KEY(KEY_F(6))},
00267         {K_Shift_F7,            SHIFT_KEY(KEY_F(7))},
00268         {K_Shift_F8,            SHIFT_KEY(KEY_F(8))},
00269         {K_Shift_F9,            SHIFT_KEY(KEY_F(9))},
00270         {K_Shift_F10,           SHIFT_KEY(KEY_F(10))},
00271         {K_Shift_F11,           SHIFT_KEY(KEY_F(11))},
00272         {K_Shift_F12,           SHIFT_KEY(KEY_F(12))},*/
00273         {K_Shift_F1,            SHIFT_KEY(KEY_F(1+12))},
00274         {K_Shift_F2,            SHIFT_KEY(KEY_F(2+12))},
00275         {K_Shift_F3,            SHIFT_KEY(KEY_F(3+12))},
00276         {K_Shift_F4,            SHIFT_KEY(KEY_F(4+12))},
00277         {K_Shift_F5,            SHIFT_KEY(KEY_F(5+12))},
00278         {K_Shift_F6,            SHIFT_KEY(KEY_F(6+12))},
00279         {K_Shift_F7,            SHIFT_KEY(KEY_F(7+12))},
00280         {K_Shift_F8,            SHIFT_KEY(KEY_F(8+12))},
00281         {K_Shift_F9,            SHIFT_KEY(KEY_F(9+12))},
00282         {K_Shift_F10,           SHIFT_KEY(KEY_F(10+12))},
00283         {K_Shift_F11,           SHIFT_KEY(KEY_F(11+12))},
00284         {K_Shift_F12,           SHIFT_KEY(KEY_F(12+12))},
00285 
00286         {K_Control_F1,          CTRL_KEY(KEY_F(1))},
00287         {K_Control_F2,          CTRL_KEY(KEY_F(2))},
00288         {K_Control_F3,          CTRL_KEY(KEY_F(3))},
00289         {K_Control_F4,          CTRL_KEY(KEY_F(4))},
00290         {K_Control_F5,          CTRL_KEY(KEY_F(5))},
00291         {K_Control_F6,          CTRL_KEY(KEY_F(6))},
00292         {K_Control_F7,          CTRL_KEY(KEY_F(7))},
00293         {K_Control_F8,          CTRL_KEY(KEY_F(8))},
00294         {K_Control_F9,          CTRL_KEY(KEY_F(9))},
00295         {K_Control_F10,         CTRL_KEY(KEY_F(10))},
00296         {K_Control_F11,         CTRL_KEY(KEY_F(11))},
00297         {K_Control_F12,         CTRL_KEY(KEY_F(12))}
00298 };
00299 
00300 bool init_keyb()
00301 {
00302         UINT i;
00303 
00304 #ifdef HAVE_TEXTMODE_X11
00305         Xdisplay = XOpenDisplay(0);
00306         if (Xdisplay) Xwindow = DefaultRootWindow(Xdisplay);
00307 #endif /* HAVE_TEXTMODE_X11 */
00308 
00309         for (i=0; i<K_COUNT; i++) {
00310                 ht_set_key((ht_key)i, -1);
00311         }
00312 
00313         for (i=0; i<sizeof curses_key_defs/ sizeof curses_key_defs[0]; i++) {
00314                 int kc = curses_key_defs[i].keycode;
00315 #ifdef HAVE_TEXTMODE_X11
00316                 if (!Xdisplay) {
00317                         if (kc & 0x40000000) kc = kc & (~0x40000000) | 0x80000000;
00318                         if (kc & 0x20000000) kc = kc & (~0x20000000) | 0x80000000;
00319                 }
00320 #endif /* HAVE_TEXTMODE_X11 */
00321                 ht_set_key(curses_key_defs[i].key, kc);
00322 //              fprintf(stderr, "%x - %x\n", curses_key_defs[i].key, kc);
00323         }
00324         
00325 //      fprintf(stderr, "%d/%x\n", KEY_F(8), KEY_F(8));
00326         return true;
00327 }
00328 
00329 void done_keyb()
00330 {
00331 #ifdef HAVE_TEXTMODE_X11
00332         if (Xdisplay) XCloseDisplay(Xdisplay);
00333 #endif /* HAVE_TEXTMODE_X11 */
00334 }

Generated on Fri May 7 21:15:34 2004 by doxygen 1.3.5