Changeset 213
- Timestamp:
- Nov 23, 2003, 4:44:59 AM (17 years ago)
- Location:
- libcaca/trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
libcaca/trunk/examples/demo.c
r206 r213 123 123 { 124 124 handle_key: 125 switch(event & 0xff )125 switch(event & 0xffff) 126 126 { 127 127 case 'q': -
libcaca/trunk/src/caca.c
r205 r213 61 61 62 62 #if defined(USE_NCURSES) 63 static mmask_t oldmask; 63 64 int _caca_attr[16]; 64 65 #endif … … 125 126 #elif defined(USE_NCURSES) 126 127 int i; 128 mmask_t newmask; 127 129 128 130 initscr(); … … 133 135 nodelay(stdscr, TRUE); 134 136 curs_set(0); 137 138 newmask = ALL_MOUSE_EVENTS; 139 mousemask(newmask, &oldmask); 135 140 136 141 start_color(); … … 307 312 SLsmg_reset_smg(); 308 313 #elif defined(USE_NCURSES) 314 mousemask(oldmask, NULL); 309 315 curs_set(1); 310 316 endwin(); -
libcaca/trunk/src/caca.h
r205 r213 146 146 * Events 147 147 */ 148 int caca_get_event(void);148 unsigned int caca_get_event(void); 149 149 150 150 /* -
libcaca/trunk/src/io.c
r205 r213 43 43 #include "caca_internals.h" 44 44 45 static void _push_key(unsigned char); 46 static unsigned char _pop_key(void); 47 static unsigned char _read_key(void); 48 49 static unsigned char back[5] = {0, 0, 0, 0, 0}; 50 51 int caca_get_event(void) 52 { 53 unsigned char key[6]; 54 55 /* If there were legacy keys, pop them */ 56 key[0] = _pop_key(); 57 if(key[0]) 58 return CACA_EVENT_KEY_PRESS | key[0]; 59 60 key[0] = _read_key(); 61 if(!key[0]) 45 static void _push_key(unsigned int); 46 static unsigned int _pop_key(void); 47 static unsigned int _read_key(void); 48 49 #define KEY_BUFLEN 10 50 static unsigned int keybuf[KEY_BUFLEN + 1]; /* zero-terminated */ 51 static int keys = 0; 52 53 unsigned int caca_get_event(void) 54 { 55 unsigned int event = 0; 56 57 /* Read all available key events */ 58 while(keys < KEY_BUFLEN) 59 { 60 unsigned int key = _read_key(); 61 if(!key) 62 break; 63 _push_key(key); 64 } 65 66 if(!keys) 62 67 return 0; 63 68 64 if(key[0] != 0x1b) 65 return CACA_EVENT_KEY_PRESS | key[0]; 69 #if defined(USE_NCURSES) 70 if(keybuf[0] == KEY_MOUSE) 71 { 72 MEVENT mevent; 73 _pop_key(); 74 getmouse(&mevent); 75 76 event |= (1) << 16; 77 event |= (mevent.x) << 8; 78 event |= (mevent.y) << 0; 79 80 return CACA_EVENT_MOUSE_CLICK | event; 81 } 82 83 switch(keybuf[0]) 84 { 85 case KEY_UP: event = CACA_EVENT_KEY_PRESS | CACA_KEY_UP; break; 86 case KEY_DOWN: event = CACA_EVENT_KEY_PRESS | CACA_KEY_DOWN; break; 87 case KEY_LEFT: event = CACA_EVENT_KEY_PRESS | CACA_KEY_LEFT; break; 88 case KEY_RIGHT: event = CACA_EVENT_KEY_PRESS | CACA_KEY_RIGHT; break; 89 90 case KEY_F(1): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F1; break; 91 case KEY_F(2): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F2; break; 92 case KEY_F(3): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F3; break; 93 case KEY_F(4): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F4; break; 94 case KEY_F(5): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F5; break; 95 case KEY_F(6): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F6; break; 96 case KEY_F(7): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F7; break; 97 case KEY_F(8): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F8; break; 98 case KEY_F(9): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F9; break; 99 case KEY_F(10): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F10; break; 100 case KEY_F(11): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F11; break; 101 case KEY_F(12): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F12; break; 102 } 103 104 if(event) 105 { 106 _pop_key(); 107 return event; 108 } 109 #endif 110 111 if(keybuf[0] != '\x1b') 112 return CACA_EVENT_KEY_PRESS | _pop_key(); 66 113 67 114 /* 68 * Handle escape sequences115 * Handle known escape sequences 69 116 */ 70 117 71 key[1] = _read_key(); 72 if(!key[1]) 73 return CACA_EVENT_KEY_PRESS | key[0]; 74 75 key[2] = _read_key(); 76 if(!key[2]) 77 { 78 _push_key(key[1]); 79 return CACA_EVENT_KEY_PRESS | key[0]; 80 } 81 82 if(key[1] == 'O') 118 _pop_key(); 119 120 if(keybuf[0] == 'O' && keybuf[1] >= 'P' && keybuf[1] <= 'S') 83 121 { 84 122 /* ^[OP ^[OQ ^[OR ^[OS */ 85 switch(key[2]) 86 { 87 case 'P': return CACA_EVENT_KEY_PRESS | CACA_KEY_F1; 88 case 'Q': return CACA_EVENT_KEY_PRESS | CACA_KEY_F2; 89 case 'R': return CACA_EVENT_KEY_PRESS | CACA_KEY_F3; 90 case 'S': return CACA_EVENT_KEY_PRESS | CACA_KEY_F4; 91 } 92 } 93 else if(key[1] == '[') 123 static unsigned int keylist[] = 124 { CACA_KEY_F1, CACA_KEY_F2, CACA_KEY_F3, CACA_KEY_F4 }; 125 _pop_key(); 126 return CACA_EVENT_KEY_PRESS | keylist[_pop_key() - 'P']; 127 } 128 else if(keybuf[0] == '[' && keybuf[1] >= 'A' && keybuf[1] <= 'D') 94 129 { 95 130 /* ^[[A ^[[B ^[[C ^[[D */ 96 switch(key[2]) 97 { 98 case 'A': return CACA_EVENT_KEY_PRESS | CACA_KEY_UP; 99 case 'B': return CACA_EVENT_KEY_PRESS | CACA_KEY_DOWN; 100 case 'C': return CACA_EVENT_KEY_PRESS | CACA_KEY_RIGHT; 101 case 'D': return CACA_EVENT_KEY_PRESS | CACA_KEY_LEFT; 102 } 103 104 key[3] = _read_key(); 105 key[4] = _read_key(); 106 131 static unsigned int keylist[] = 132 { CACA_KEY_UP, CACA_KEY_DOWN, CACA_KEY_RIGHT, CACA_KEY_LEFT }; 133 _pop_key(); 134 return CACA_EVENT_KEY_PRESS | keylist[_pop_key() - 'A']; 135 } 136 else if(keybuf[0] == '[' && keybuf[1] == 'M' && 137 keybuf[2] && keybuf[3] && keybuf[3]) 138 { 107 139 /* ^[[Mxxx */ 108 if(key[2] == 'M') 109 { 110 int event = CACA_EVENT_MOUSE_CLICK; 111 112 key[5] = _read_key(); 113 114 event |= (int)(key[3] - ' ') << 16; 115 event |= (int)(key[4] - '!') << 8; 116 event |= (int)(key[5] - '!') << 0; 117 118 return event; 119 } 120 140 _pop_key(); 141 _pop_key(); 142 event |= (_pop_key() - ' ') << 16; 143 event |= (_pop_key() - '!') << 8; 144 event |= (_pop_key() - '!') << 0; 145 146 return CACA_EVENT_MOUSE_CLICK | event; 147 } 148 else if(keybuf[0] == '[' && keybuf[1] == '1' && keybuf[3] == '~' && 149 keybuf[2] >= '5' && keybuf[2] != '6' && keybuf[2] <= '9') 150 { 121 151 /* ^[[15~ ^[[17~ ^[[18~ ^[[19~ */ 122 if(key[2] == '1' && key[4] == '~') 123 switch(key[3]) 124 { 125 case '5': return CACA_EVENT_KEY_PRESS | CACA_KEY_F5; 126 case '7': return CACA_EVENT_KEY_PRESS | CACA_KEY_F6; 127 case '8': return CACA_EVENT_KEY_PRESS | CACA_KEY_F7; 128 case '9': return CACA_EVENT_KEY_PRESS | CACA_KEY_F8; 129 } 130 152 static unsigned int keylist[] = 153 { CACA_KEY_F5, 0, CACA_KEY_F6, CACA_KEY_F7, CACA_KEY_F8 }; 154 _pop_key(); 155 _pop_key(); 156 event = CACA_EVENT_KEY_PRESS | keylist[_pop_key() - '5']; 157 _pop_key(); 158 return event; 159 } 160 else if(keybuf[0] == '[' && keybuf[1] == '2' && keybuf[3] == '~' && 161 keybuf[2] >= '0' && keybuf[2] != '2' && keybuf[2] <= '4') 162 { 131 163 /* ^[[20~ ^[[21~ ^[[23~ ^[[24~ */ 132 if(key[2] == '2' && key[4] == '~') 133 switch(key[3]) 134 { 135 case '0': return CACA_EVENT_KEY_PRESS | CACA_KEY_F9; 136 case '1': return CACA_EVENT_KEY_PRESS | CACA_KEY_F10; 137 case '3': return CACA_EVENT_KEY_PRESS | CACA_KEY_F11; 138 case '4': return CACA_EVENT_KEY_PRESS | CACA_KEY_F12; 139 } 140 141 _push_key(key[4]); 142 _push_key(key[3]); 143 } 144 145 /* Unknown escape sequence: return each key one after the other */ 146 _push_key(key[2]); 147 _push_key(key[1]); 148 return CACA_EVENT_KEY_PRESS | key[0]; 149 } 150 151 static void _push_key(unsigned char key) 152 { 153 back[4] = back[3]; 154 back[3] = back[2]; 155 back[2] = back[1]; 156 back[1] = back[0]; 157 back[0] = key; 158 } 159 160 static unsigned char _pop_key(void) 161 { 162 unsigned char key = back[0]; 163 back[0] = back[1]; 164 back[1] = back[2]; 165 back[2] = back[3]; 166 back[3] = back[4]; 164 static unsigned int keylist[] = 165 { CACA_KEY_F9, CACA_KEY_F10, 0, CACA_KEY_F11, CACA_KEY_F12 }; 166 _pop_key(); 167 _pop_key(); 168 event = CACA_EVENT_KEY_PRESS | keylist[_pop_key() - '0']; 169 _pop_key(); 170 return event; 171 } 172 173 caca_printf(0,0, "unknown esc sequence %2x %2x %2x %2x %2x\n", '\x1b', keybuf[0], keybuf[1], keybuf[2], keybuf[3]); 174 /* Unknown escape sequence: return the ESC key */ 175 return CACA_EVENT_KEY_PRESS | '\x1b'; 176 } 177 178 static void _push_key(unsigned int key) 179 { 180 if(keys == KEY_BUFLEN) 181 return; 182 keybuf[keys] = key; 183 keys++; 184 keybuf[keys] = 0; 185 } 186 187 static unsigned int _pop_key(void) 188 { 189 int i; 190 unsigned int key = keybuf[0]; 191 keys--; 192 for(i = 0; i < keys; i++) 193 keybuf[i] = keybuf[i + 1]; 194 keybuf[keys] = 0; 195 167 196 return key; 168 197 } 169 198 170 static unsigned char_read_key(void)199 static unsigned int _read_key(void) 171 200 { 172 201 #if defined(USE_SLANG) 173 202 return SLang_input_pending(0) ? SLang_getkey() : 0; 174 203 #elif defined(USE_NCURSES) 175 unsigned charkey = getch();204 int key = getch(); 176 205 return (key == ERR) ? 0 : key; 177 206 #elif defined(USE_CONIO)
Note: See TracChangeset
for help on using the changeset viewer.