home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / mc454src.zip / mc-4.5.4.src / os2emx / src / key_os2.c < prev    next >
C/C++ Source or Header  |  1999-01-04  |  4KB  |  150 lines

  1. /* Keyboard support routines for OS/2 system.
  2.  
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2 of the License, or
  7.    (at your option) any later version.
  8.    
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
  17.  
  18. */
  19.  
  20. #include <config.h>
  21.  
  22. #define INCL_KBD
  23. #include <os2.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <sys/select.h>
  27. #include <sys/time.h>
  28. #include <errno.h>
  29. #include "src/mouse.h"
  30. #include "src/global.h"
  31. #include "src/main.h"
  32. #include "src/key.h"
  33. #include "src/main.h"
  34. #include "src/tty.h"
  35.  
  36.  
  37. extern int  (*os_get_key_code)(int no_delay);
  38. extern int  (*os_get_event)(Gpm_Event *event, int redo_event, int block);
  39. extern int  (*os_get_modifier)(void);
  40.  
  41. int os2_get_key_code (int no_delay)
  42. {
  43.     unsigned int        inp_ch;
  44.  
  45.     if (no_delay) {
  46.         /* Check if any input pending, otherwise return */
  47.     nodelay (stdscr, TRUE);
  48.         inp_ch = SLang_input_pending(0);
  49.         if (inp_ch == 0) {
  50.            return 0;
  51.         }
  52.     } 
  53.  
  54.     if (no_delay) {
  55.        return (VKtoCurses(inp_ch));
  56.     }
  57.  
  58.     do {
  59.       if(SLang_input_pending(0)>0){
  60.          inp_ch = SLang_getkey();
  61.          if (!inp_ch)
  62.             inp_ch = (SLang_getkey() << 8);
  63.          if (inp_ch) return (VKtoCurses(inp_ch));
  64.       } else 
  65.          if(mouse_has_data())return 0;
  66.       _sleep2(10);
  67.     } while (!no_delay);
  68.     return 0;
  69. }
  70.  
  71. static int getch_with_delay (void)
  72. {
  73.     int c=0;
  74.  
  75.     while (1) {
  76.     /* Try to get a character */
  77.     c = os2_get_key_code (0);
  78.     if (c != ERR)
  79.         break;
  80.     }
  81.     /* Success -> return the character */
  82.     return c;
  83. }
  84.  
  85. int os2_get_event (Gpm_Event *event, int redo_event, int block)
  86. {
  87.     int c;    
  88.     static Gpm_Event ev;  /* Mouse event */
  89.  
  90.     refresh ();
  91.     doupdate ();
  92.     vfs_timeout_handler ();
  93.  
  94. /*-----------------------------------------------------------------------*/
  95.     /* Repeat if using mouse */
  96.     if(redo_event)
  97.      { if(os2_mouse_get_event(&ev))_sleep2(mou_auto_repeat);      
  98.        *event=ev;
  99.        return EV_MOUSE;
  100.      }
  101. /*-----------------------------------------------------------------------*/
  102.     c = block ? getch_with_delay () : os2_get_key_code (1);
  103.     if (!c) {
  104.         if(os2_mouse_get_event(&ev)) {       
  105.            /* Code is -1, no mouse event */
  106.             return EV_NONE; 
  107.         } else {
  108.             *event = ev; 
  109.             return EV_MOUSE;
  110.         }
  111.     }
  112.  
  113.     return c;
  114. }
  115.  
  116. int os2_get_modifier()
  117. { int   rc=0,f;
  118.   KBDINFO I;
  119.   f=KbdGetStatus(&I,0);
  120.   if(!f)
  121.    { f=I.fsState;
  122.      if(f&0x0003)rc|=SHIFT_PRESSED;
  123.      if(f&0x0004)rc|=CONTROL_PRESSED;
  124.      if(f&0x0200)rc|=ALTL_PRESSED;
  125.      if(f&0x0800)rc|=ALTR_PRESSED;
  126.    }
  127.   return rc;
  128. }
  129.  
  130. int os2_init_key (char *term)
  131. {  
  132.    os_get_key_code=os2_get_key_code;
  133.    os_get_event=os2_get_event;
  134.    os_get_modifier=os2_get_modifier;
  135.    return 1;
  136. }
  137.  
  138.  
  139. void OS_Setup_Console(char *term, int kbd, int mouse)
  140. { static char *lines,*columns; 
  141.   if(term&&strncmp(term,"xterm",5)) 
  142.      { int c=GetScrCols(),r=GetScrRows();  /* VIO */
  143.        char buf[256];
  144.        sprintf(buf,"LINES=%i",r);   lines=strdup(buf);   putenv(lines);
  145.        sprintf(buf,"COLUMNS=%i",c); columns=strdup(buf); putenv(columns);
  146.        if(kbd&&mouse)os2_create_mouse_thread();
  147.        if(kbd)os_init_key=os2_init_key;
  148.      }
  149. }
  150.