"Fossies" - the Fresh Open Source Software Archive

Member "PDCurses-3.9/sdl1/pdckbd.c" (4 Sep 2019, 11740 Bytes) of package /linux/misc/PDCurses-3.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "pdckbd.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.8_vs_3.9.

    1 /* PDCurses */
    2 
    3 #include "pdcsdl.h"
    4 
    5 /*man-start**************************************************************
    6 
    7 pdckbd
    8 ------
    9 
   10 ### Synopsis
   11 
   12     unsigned long PDC_get_input_fd(void);
   13 
   14 ### Description
   15 
   16    PDC_get_input_fd() returns the file descriptor that PDCurses reads
   17    its input from. It can be used for select().
   18 
   19 ### Portability
   20                              X/Open  ncurses  NetBSD
   21     PDC_get_input_fd            -       -       -
   22 
   23 **man-end****************************************************************/
   24 
   25 #include <string.h>
   26 
   27 static SDL_Event event;
   28 static SDLKey oldkey;
   29 static MOUSE_STATUS old_mouse_status;
   30 
   31 static struct
   32 {
   33     SDLKey keycode;
   34     bool numkeypad;
   35     unsigned short normal;
   36     unsigned short shifted;
   37     unsigned short control;
   38     unsigned short alt;
   39 } key_table[] =
   40 {
   41 /* keycode  keypad  normal       shifted       control  alt*/
   42  {SDLK_LEFT,    FALSE,  KEY_LEFT,    KEY_SLEFT,    CTL_LEFT,    ALT_LEFT},
   43  {SDLK_RIGHT,   FALSE,  KEY_RIGHT,   KEY_SRIGHT,   CTL_RIGHT,   ALT_RIGHT},
   44  {SDLK_UP,  FALSE,  KEY_UP,      KEY_SUP,      CTL_UP,  ALT_UP},
   45  {SDLK_DOWN,    FALSE,  KEY_DOWN,    KEY_SDOWN,    CTL_DOWN,    ALT_DOWN},
   46  {SDLK_HOME,    FALSE,  KEY_HOME,    KEY_SHOME,    CTL_HOME,    ALT_HOME},
   47  {SDLK_END, FALSE,  KEY_END,     KEY_SEND,     CTL_END, ALT_END},
   48  {SDLK_PAGEUP,  FALSE,  KEY_PPAGE,   KEY_SPREVIOUS,CTL_PGUP,    ALT_PGUP},
   49  {SDLK_PAGEDOWN,FALSE,  KEY_NPAGE,   KEY_SNEXT,    CTL_PGDN,    ALT_PGDN},
   50  {SDLK_INSERT,  FALSE,  KEY_IC,      KEY_SIC,      CTL_INS, ALT_INS},
   51  {SDLK_DELETE,  FALSE,  KEY_DC,      KEY_SDC,      CTL_DEL, ALT_DEL},
   52  {SDLK_F1,  FALSE,  KEY_F(1),    KEY_F(13),    KEY_F(25),   KEY_F(37)},
   53  {SDLK_F2,  FALSE,  KEY_F(2),    KEY_F(14),    KEY_F(26),   KEY_F(38)},
   54  {SDLK_F3,  FALSE,  KEY_F(3),    KEY_F(15),    KEY_F(27),   KEY_F(39)},
   55  {SDLK_F4,  FALSE,  KEY_F(4),    KEY_F(16),    KEY_F(28),   KEY_F(40)},
   56  {SDLK_F5,  FALSE,  KEY_F(5),    KEY_F(17),    KEY_F(29),   KEY_F(41)},
   57  {SDLK_F6,  FALSE,  KEY_F(6),    KEY_F(18),    KEY_F(30),   KEY_F(42)},
   58  {SDLK_F7,  FALSE,  KEY_F(7),    KEY_F(19),    KEY_F(31),   KEY_F(43)},
   59  {SDLK_F8,  FALSE,  KEY_F(8),    KEY_F(20),    KEY_F(32),   KEY_F(44)},
   60  {SDLK_F9,  FALSE,  KEY_F(9),    KEY_F(21),    KEY_F(33),   KEY_F(45)},
   61  {SDLK_F10, FALSE,  KEY_F(10),   KEY_F(22),    KEY_F(34),   KEY_F(46)},
   62  {SDLK_F11, FALSE,  KEY_F(11),   KEY_F(23),    KEY_F(35),   KEY_F(47)},
   63  {SDLK_F12, FALSE,  KEY_F(12),   KEY_F(24),    KEY_F(36),   KEY_F(48)},
   64  {SDLK_F13, FALSE,  KEY_F(13),   KEY_F(25),    KEY_F(37),   KEY_F(49)},
   65  {SDLK_F14, FALSE,  KEY_F(14),   KEY_F(26),    KEY_F(38),   KEY_F(50)},
   66  {SDLK_F15, FALSE,  KEY_F(15),   KEY_F(27),    KEY_F(39),   KEY_F(51)},
   67  {SDLK_BACKSPACE,FALSE, 0x08,        0x08,     CTL_BKSP,    ALT_BKSP},
   68  {SDLK_TAB, FALSE,  0x09,        KEY_BTAB,     CTL_TAB, ALT_TAB},
   69  {SDLK_PRINT,   FALSE,  KEY_PRINT,   KEY_SPRINT,   KEY_PRINT,   KEY_PRINT},
   70  {SDLK_PAUSE,   FALSE,  KEY_SUSPEND, KEY_SSUSPEND, KEY_SUSPEND, KEY_SUSPEND},
   71  {SDLK_CLEAR,   FALSE,  KEY_CLEAR,   KEY_CLEAR,    KEY_CLEAR,   KEY_CLEAR},
   72  {SDLK_BREAK,   FALSE,  KEY_BREAK,   KEY_BREAK,    KEY_BREAK,   KEY_BREAK},
   73  {SDLK_HELP,    FALSE,  KEY_HELP,    KEY_SHELP,    KEY_LHELP,   KEY_HELP},
   74  {SDLK_MENU,    FALSE,  KEY_OPTIONS, KEY_SOPTIONS, KEY_OPTIONS, KEY_OPTIONS},
   75  {SDLK_ESCAPE,  FALSE,  0x1B,        0x1B,     0x1B,    ALT_ESC},
   76  {SDLK_KP_ENTER,TRUE,   PADENTER,    PADENTER,     CTL_PADENTER,ALT_PADENTER},
   77  {SDLK_KP_PLUS, TRUE,   PADPLUS,     '+',      CTL_PADPLUS, ALT_PADPLUS},
   78  {SDLK_KP_MINUS,TRUE,   PADMINUS,    '-',      CTL_PADMINUS,ALT_PADMINUS},
   79  {SDLK_KP_MULTIPLY,TRUE,PADSTAR,     '*',      CTL_PADSTAR, ALT_PADSTAR},
   80  {SDLK_KP_DIVIDE,TRUE,  PADSLASH,    '/',      CTL_PADSLASH,ALT_PADSLASH},
   81  {SDLK_KP_PERIOD,TRUE,  PADSTOP,     '.',      CTL_PADSTOP, ALT_PADSTOP},
   82  {SDLK_KP0, TRUE,   PAD0,        '0',      CTL_PAD0,    ALT_PAD0},
   83  {SDLK_KP1, TRUE,   KEY_C1,      '1',      CTL_PAD1,    ALT_PAD1},
   84  {SDLK_KP2, TRUE,   KEY_C2,      '2',      CTL_PAD2,    ALT_PAD2},
   85  {SDLK_KP3, TRUE,   KEY_C3,      '3',      CTL_PAD3,    ALT_PAD3},
   86  {SDLK_KP4, TRUE,   KEY_B1,      '4',      CTL_PAD4,    ALT_PAD4},
   87  {SDLK_KP5, TRUE,   KEY_B2,      '5',      CTL_PAD5,    ALT_PAD5},
   88  {SDLK_KP6, TRUE,   KEY_B3,      '6',      CTL_PAD6,    ALT_PAD6},
   89  {SDLK_KP7, TRUE,   KEY_A1,      '7',      CTL_PAD7,    ALT_PAD7},
   90  {SDLK_KP8, TRUE,   KEY_A2,      '8',      CTL_PAD8,    ALT_PAD8},
   91  {SDLK_KP9, TRUE,   KEY_A3,      '9',      CTL_PAD9,    ALT_PAD9},
   92  {0,        0,  0,       0,        0,       0}
   93 };
   94 
   95 unsigned long PDC_get_input_fd(void)
   96 {
   97     PDC_LOG(("PDC_get_input_fd() - called\n"));
   98 
   99     return 0L;  /* test this */
  100 }
  101 
  102 void PDC_set_keyboard_binary(bool on)
  103 {
  104     PDC_LOG(("PDC_set_keyboard_binary() - called\n"));
  105 }
  106 
  107 /* check if a key or mouse event is waiting */
  108 
  109 bool PDC_check_key(void)
  110 {
  111     Uint32 current = SDL_GetTicks();
  112     int haveevent = SDL_PollEvent(&event);
  113 
  114     /* if we have an event, or 30 ms have passed without a screen
  115        update, or the timer has wrapped, update now */
  116 
  117     if (haveevent ||
  118         current < pdc_lastupdate || ((current - pdc_lastupdate) > 30))
  119         PDC_update_rects();
  120 
  121     return haveevent;
  122 }
  123 
  124 static int _process_key_event(void)
  125 {
  126     int i, key = 0;
  127 
  128     SP->key_modifiers = 0L;
  129     SP->key_code = FALSE;
  130 
  131     if (event.type == SDL_KEYUP)
  132     {
  133         if (SP->return_key_modifiers && event.key.keysym.sym == oldkey)
  134         {
  135             SP->key_code = TRUE;
  136 
  137             switch (oldkey)
  138             {
  139             case SDLK_RSHIFT:
  140                 return KEY_SHIFT_R;
  141             case SDLK_LSHIFT:
  142                 return KEY_SHIFT_L;
  143             case SDLK_RCTRL:
  144                 return KEY_CONTROL_R;
  145             case SDLK_LCTRL:
  146                 return KEY_CONTROL_L;
  147             case SDLK_RALT:
  148                 return KEY_ALT_R;
  149             case SDLK_LALT:
  150                 return KEY_ALT_L;
  151             default:
  152                 break;
  153             }
  154 
  155             SP->key_code = FALSE;
  156         }
  157 
  158         return -1;
  159     }
  160 
  161     oldkey = event.key.keysym.sym;
  162 
  163     if (event.key.keysym.mod & KMOD_NUM)
  164         SP->key_modifiers |= PDC_KEY_MODIFIER_NUMLOCK;
  165 
  166     if (event.key.keysym.mod & KMOD_SHIFT)
  167         SP->key_modifiers |= PDC_KEY_MODIFIER_SHIFT;
  168 
  169     if (event.key.keysym.mod & KMOD_CTRL)
  170         SP->key_modifiers |= PDC_KEY_MODIFIER_CONTROL;
  171 
  172     if (event.key.keysym.mod & KMOD_ALT)
  173         SP->key_modifiers |= PDC_KEY_MODIFIER_ALT;
  174 
  175     for (i = 0; key_table[i].keycode; i++)
  176     {
  177         if (key_table[i].keycode == event.key.keysym.sym)
  178         {
  179             if ((event.key.keysym.mod & KMOD_SHIFT) ||
  180                 (key_table[i].numkeypad && (event.key.keysym.mod & KMOD_NUM)))
  181             {
  182                 key = key_table[i].shifted;
  183             }
  184             else if (event.key.keysym.mod & KMOD_CTRL)
  185             {
  186                 key = key_table[i].control;
  187             }
  188             else if (event.key.keysym.mod & KMOD_ALT)
  189             {
  190                 key = key_table[i].alt;
  191             }
  192 
  193             /* To get here, we ignore all other modifiers */
  194 
  195             else
  196                 key = key_table[i].normal;
  197 
  198             SP->key_code = (key > 0x100);
  199             break;
  200         }
  201     }
  202 
  203     if (!key)
  204     {
  205         key = event.key.keysym.unicode;
  206 
  207         if (key > 0x7f)
  208             key = 0;
  209     }
  210 
  211     /* Handle ALT letters and numbers */
  212 
  213     if (event.key.keysym.mod & KMOD_ALT)
  214     {
  215         if (key >= 'A' && key <= 'Z')
  216         {
  217             key += ALT_A - 'A';
  218             SP->key_code = TRUE;
  219         }
  220 
  221         if (key >= 'a' && key <= 'z')
  222         {
  223             key += ALT_A - 'a';
  224             SP->key_code = TRUE;
  225         }
  226 
  227         if (key >= '0' && key <= '9')
  228         {
  229             key += ALT_0 - '0';
  230             SP->key_code = TRUE;
  231         }
  232     }
  233 
  234     return key ? key : -1;
  235 }
  236 
  237 static int _process_mouse_event(void)
  238 {
  239     SDLMod keymods;
  240     short shift_flags = 0;
  241 
  242     memset(&SP->mouse_status, 0, sizeof(MOUSE_STATUS));
  243 
  244     keymods = SDL_GetModState();
  245 
  246     if (keymods & KMOD_SHIFT)
  247         shift_flags |= BUTTON_SHIFT;
  248 
  249     if (keymods & KMOD_CTRL)
  250         shift_flags |= BUTTON_CONTROL;
  251 
  252     if (keymods & KMOD_ALT)
  253         shift_flags |= BUTTON_ALT;
  254 
  255     if (event.type == SDL_MOUSEMOTION)
  256     {
  257         int i;
  258 
  259         SP->mouse_status.x = (event.motion.x - pdc_xoffset) / pdc_fwidth;
  260         SP->mouse_status.y = (event.motion.y - pdc_yoffset) / pdc_fheight;
  261 
  262         if (!event.motion.state ||
  263            (SP->mouse_status.x == old_mouse_status.x &&
  264             SP->mouse_status.y == old_mouse_status.y))
  265             return -1;
  266 
  267         SP->mouse_status.changes = PDC_MOUSE_MOVED;
  268 
  269         for (i = 0; i < 3; i++)
  270         {
  271             if (event.motion.state & SDL_BUTTON(i + 1))
  272             {
  273                 SP->mouse_status.button[i] = BUTTON_MOVED | shift_flags;
  274                 SP->mouse_status.changes |= (1 << i);
  275             }
  276         }
  277     }
  278     else
  279     {
  280         short action = (event.button.state == SDL_PRESSED) ?
  281                        BUTTON_PRESSED : BUTTON_RELEASED;
  282         Uint8 btn = event.button.button;
  283 
  284         /* handle scroll wheel */
  285 
  286         if ((btn >= 4 && btn <= 7) && action == BUTTON_RELEASED)
  287         {
  288             SP->mouse_status.x = SP->mouse_status.y = -1;
  289 
  290             switch (btn)
  291             {
  292             case 4:
  293                 SP->mouse_status.changes = PDC_MOUSE_WHEEL_UP;
  294                 break;
  295             case 5:
  296                 SP->mouse_status.changes = PDC_MOUSE_WHEEL_DOWN;
  297                 break;
  298             case 6:
  299                 SP->mouse_status.changes = PDC_MOUSE_WHEEL_LEFT;
  300                 break;
  301             case 7:
  302                 SP->mouse_status.changes = PDC_MOUSE_WHEEL_RIGHT;
  303             }
  304 
  305             SP->key_code = TRUE;
  306             return KEY_MOUSE;
  307         }
  308 
  309         if (btn < 1 || btn > 3)
  310             return -1;
  311 
  312         /* check for a click -- a press followed immediately by a release */
  313 
  314         if (action == BUTTON_PRESSED && SP->mouse_wait)
  315         {
  316             SDL_Event rel;
  317 
  318             napms(SP->mouse_wait);
  319 
  320             if (SDL_PollEvent(&rel))
  321             {
  322                 if (rel.type == SDL_MOUSEBUTTONUP && rel.button.button == btn)
  323                     action = BUTTON_CLICKED;
  324                 else
  325                     SDL_PushEvent(&rel);
  326             }
  327         }
  328 
  329         SP->mouse_status.x = (event.button.x - pdc_xoffset) / pdc_fwidth;
  330         SP->mouse_status.y = (event.button.y - pdc_yoffset) / pdc_fheight;
  331 
  332         btn--;
  333 
  334         SP->mouse_status.button[btn] = action | shift_flags;
  335         SP->mouse_status.changes = (1 << btn);
  336     }
  337 
  338     old_mouse_status = SP->mouse_status;
  339 
  340     SP->key_code = TRUE;
  341     return KEY_MOUSE;
  342 }
  343 
  344 /* return the next available key or mouse event */
  345 
  346 int PDC_get_key(void)
  347 {
  348     switch (event.type)
  349     {
  350     case SDL_QUIT:
  351         exit(1);
  352     case SDL_VIDEORESIZE:
  353         if (pdc_own_screen &&
  354            (event.resize.h / pdc_fheight != LINES ||
  355             event.resize.w / pdc_fwidth != COLS))
  356         {
  357             pdc_sheight = event.resize.h;
  358             pdc_swidth = event.resize.w;
  359 
  360             if (!SP->resized)
  361             {
  362                 SP->resized = TRUE;
  363                 SP->key_code = TRUE;
  364                 return KEY_RESIZE;
  365             }
  366         }
  367         break;
  368     case SDL_MOUSEMOTION:
  369         SDL_ShowCursor(SDL_ENABLE);
  370     case SDL_MOUSEBUTTONUP:
  371     case SDL_MOUSEBUTTONDOWN:
  372         oldkey = SDLK_SPACE;
  373         return _process_mouse_event();
  374     case SDL_KEYUP:
  375     case SDL_KEYDOWN:
  376         PDC_mouse_set();
  377         return _process_key_event();
  378     case SDL_USEREVENT:
  379         PDC_blink_text();
  380     }
  381 
  382     return -1;
  383 }
  384 
  385 /* discard any pending keyboard or mouse input -- this is the core
  386    routine for flushinp() */
  387 
  388 void PDC_flushinp(void)
  389 {
  390     PDC_LOG(("PDC_flushinp() - called\n"));
  391 
  392     while (PDC_check_key());
  393 }
  394 
  395 bool PDC_has_mouse(void)
  396 {
  397     return TRUE;
  398 }
  399 
  400 int PDC_mouse_set(void)
  401 {
  402     SDL_ShowCursor(SP->_trap_mbe ? SDL_ENABLE : SDL_DISABLE);
  403 
  404     return OK;
  405 }
  406 
  407 int PDC_modifiers_set(void)
  408 {
  409     return OK;
  410 }