src/video/wscons/SDL_wsconsevents.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 06 Jan 2006 13:20:10 +0000
changeset 1234 73676c1f56ee
parent 1187 19d8949b4584
child 1312 c9b51268668f
permissions -rw-r--r--
For sanity's sake, removed the '&' when passing copy_row array to asm.
icculus@1187
     1
/*
icculus@1187
     2
    SDL - Simple DirectMedia Layer
icculus@1187
     3
    Copyright (C) 1997-2004 Sam Lantinga
icculus@1187
     4
icculus@1187
     5
    This library is free software; you can redistribute it and/or
icculus@1187
     6
    modify it under the terms of the GNU Library General Public
icculus@1187
     7
    License as published by the Free Software Foundation; either
icculus@1187
     8
    version 2 of the License, or (at your option) any later version.
icculus@1187
     9
icculus@1187
    10
    This library is distributed in the hope that it will be useful,
icculus@1187
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
icculus@1187
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
icculus@1187
    13
    Library General Public License for more details.
icculus@1187
    14
icculus@1187
    15
    You should have received a copy of the GNU Library General Public
icculus@1187
    16
    License along with this library; if not, write to the Free
icculus@1187
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
icculus@1187
    18
icculus@1187
    19
    Sam Lantinga
icculus@1187
    20
    slouken@libsdl.org
icculus@1187
    21
*/
icculus@1187
    22
icculus@1187
    23
#ifdef SAVE_RCSID
icculus@1187
    24
static char rcsid =
icculus@1187
    25
 "@(#) $Id$";
icculus@1187
    26
#endif
icculus@1187
    27
icculus@1187
    28
#include <sys/types.h>
icculus@1187
    29
#include <dev/wscons/wsdisplay_usl_io.h>
icculus@1187
    30
#include <sys/ioctl.h>
icculus@1187
    31
#include <fcntl.h>    
icculus@1187
    32
#include <unistd.h>  
icculus@1187
    33
#include <termios.h>
icculus@1187
    34
#include <errno.h> 
icculus@1187
    35
#include <string.h>
icculus@1187
    36
icculus@1187
    37
#include "SDL.h"
icculus@1187
    38
#include "SDL_sysevents.h"
icculus@1187
    39
#include "SDL_events_c.h"
icculus@1187
    40
#include "SDL_wsconsvideo.h"
icculus@1187
    41
#include "SDL_wsconsevents_c.h"
icculus@1187
    42
icculus@1187
    43
static int posted = 0;
icculus@1187
    44
icculus@1187
    45
int WSCONS_InitKeyboard(_THIS)
icculus@1187
    46
{
icculus@1187
    47
  struct termios tty;
icculus@1187
    48
icculus@1187
    49
  if (ioctl(private->fd, WSKBDIO_GTYPE, &private->kbdType) == -1) {
icculus@1187
    50
    WSCONS_ReportError("cannot get keyboard type: %s", strerror(errno));
icculus@1187
    51
    return -1;
icculus@1187
    52
  }
icculus@1187
    53
icculus@1187
    54
  if (tcgetattr(private->fd, &private->saved_tty) == -1) {
icculus@1187
    55
    WSCONS_ReportError("cannot get terminal attributes: %s", strerror(errno));
icculus@1187
    56
    return -1;
icculus@1187
    57
  }
icculus@1187
    58
  private->did_save_tty = 1;
icculus@1187
    59
  tty = private->saved_tty;
icculus@1187
    60
  tty.c_iflag = IGNPAR | IGNBRK;
icculus@1187
    61
  tty.c_oflag = 0;
icculus@1187
    62
  tty.c_cflag = CREAD | CS8;
icculus@1187
    63
  tty.c_lflag = 0;
icculus@1187
    64
  tty.c_cc[VTIME] = 0;
icculus@1187
    65
  tty.c_cc[VMIN] = 1;
icculus@1187
    66
  cfsetispeed(&tty, 9600);
icculus@1187
    67
  cfsetospeed(&tty, 9600);
icculus@1187
    68
  if (tcsetattr(private->fd, TCSANOW, &tty) < 0) {
icculus@1187
    69
    WSCONS_ReportError("cannot set terminal attributes: %s", strerror(errno));
icculus@1187
    70
    return -1;
icculus@1187
    71
  }
icculus@1187
    72
  if (ioctl(private->fd, KDSKBMODE, K_RAW) == -1) {
icculus@1187
    73
    WSCONS_ReportError("cannot set raw keyboard mode: %s", strerror(errno));
icculus@1187
    74
    return -1;
icculus@1187
    75
  }
icculus@1187
    76
icculus@1187
    77
  return 0;
icculus@1187
    78
}
icculus@1187
    79
icculus@1187
    80
void WSCONS_ReleaseKeyboard(_THIS)
icculus@1187
    81
{
icculus@1187
    82
  if (private->fd != -1) {
icculus@1187
    83
    if (ioctl(private->fd, KDSKBMODE, K_XLATE) == -1) {
icculus@1187
    84
      WSCONS_ReportError("cannot restore keyboard to translated mode: %s",
icculus@1187
    85
			 strerror(errno));
icculus@1187
    86
    }
icculus@1187
    87
    if (private->did_save_tty) {
icculus@1187
    88
      if (tcsetattr(private->fd, TCSANOW, &private->saved_tty) < 0) {
icculus@1187
    89
	WSCONS_ReportError("cannot restore keynoard attributes: %s",
icculus@1187
    90
			   strerror(errno));
icculus@1187
    91
      }
icculus@1187
    92
    }
icculus@1187
    93
  }
icculus@1187
    94
}
icculus@1187
    95
icculus@1187
    96
static void updateMouse()
icculus@1187
    97
{
icculus@1187
    98
}
icculus@1187
    99
icculus@1187
   100
static SDLKey keymap[128];
icculus@1187
   101
icculus@1187
   102
static SDL_keysym *TranslateKey(int scancode, SDL_keysym *keysym)
icculus@1187
   103
{
icculus@1187
   104
  keysym->scancode = scancode;
icculus@1187
   105
  keysym->sym = SDLK_UNKNOWN;
icculus@1187
   106
  keysym->mod = KMOD_NONE;
icculus@1187
   107
icculus@1187
   108
  if (scancode < SDL_TABLESIZE(keymap))
icculus@1187
   109
    keysym->sym = keymap[scancode];
icculus@1187
   110
icculus@1187
   111
  if (keysym->sym == SDLK_UNKNOWN)
icculus@1187
   112
    printf("Unknown mapping for scancode %d\n", scancode);
icculus@1187
   113
icculus@1187
   114
  return keysym;
icculus@1187
   115
}
icculus@1187
   116
icculus@1187
   117
static void updateKeyboard(_THIS)
icculus@1187
   118
{
icculus@1187
   119
  unsigned char buf[100];
icculus@1187
   120
  SDL_keysym keysym;
icculus@1187
   121
  int n, i;
icculus@1187
   122
icculus@1187
   123
  if ((n = read(private->fd, buf, sizeof(buf))) > 0) {
icculus@1187
   124
    for (i = 0; i < n; i++) {
icculus@1187
   125
      char c = buf[i] & 0x7f;
icculus@1187
   126
      if (c == 224) // special key prefix -- what should we do with it?
icculus@1187
   127
	continue;
icculus@1187
   128
      int release = (buf[i] & 0x80) != 0;
icculus@1187
   129
      posted += SDL_PrivateKeyboard(release ? SDL_RELEASED : SDL_PRESSED,
icculus@1187
   130
				    TranslateKey(c, &keysym));
icculus@1187
   131
    }
icculus@1187
   132
  }
icculus@1187
   133
}
icculus@1187
   134
icculus@1187
   135
void WSCONS_PumpEvents(_THIS)
icculus@1187
   136
{
icculus@1187
   137
  do {
icculus@1187
   138
    posted = 0;
icculus@1187
   139
    updateMouse();
icculus@1187
   140
    updateKeyboard(this);
icculus@1187
   141
  } while (posted);
icculus@1187
   142
}
icculus@1187
   143
icculus@1187
   144
void WSCONS_InitOSKeymap(_THIS)
icculus@1187
   145
{
icculus@1187
   146
  int i;
icculus@1187
   147
icculus@1187
   148
  /* Make sure unknown keys are mapped correctly */
icculus@1187
   149
  for (i=0; i < SDL_TABLESIZE(keymap); i++) {
icculus@1187
   150
    keymap[i] = SDLK_UNKNOWN;
icculus@1187
   151
  }
icculus@1187
   152
icculus@1187
   153
  switch (private->kbdType) {
icculus@1187
   154
  case WSKBD_TYPE_ZAURUS:
icculus@1187
   155
    /* top row */
icculus@1187
   156
    keymap[2] = SDLK_1;
icculus@1187
   157
    keymap[3] = SDLK_2;
icculus@1187
   158
    keymap[4] = SDLK_3;
icculus@1187
   159
    keymap[5] = SDLK_4;
icculus@1187
   160
    keymap[6] = SDLK_5;
icculus@1187
   161
    keymap[7] = SDLK_6;
icculus@1187
   162
    keymap[8] = SDLK_7;
icculus@1187
   163
    keymap[9] = SDLK_8;
icculus@1187
   164
    keymap[10] = SDLK_9;
icculus@1187
   165
    keymap[11] = SDLK_0;
icculus@1187
   166
    keymap[14] = SDLK_BACKSPACE;
icculus@1187
   167
    
icculus@1187
   168
    /* second row */
icculus@1187
   169
    keymap[16] = SDLK_q;
icculus@1187
   170
    keymap[17] = SDLK_w;
icculus@1187
   171
    keymap[18] = SDLK_e;
icculus@1187
   172
    keymap[19] = SDLK_r;
icculus@1187
   173
    keymap[20] = SDLK_t;
icculus@1187
   174
    keymap[21] = SDLK_y;
icculus@1187
   175
    keymap[22] = SDLK_u;
icculus@1187
   176
    keymap[23] = SDLK_i;
icculus@1187
   177
    keymap[24] = SDLK_o;
icculus@1187
   178
    keymap[25] = SDLK_p;
icculus@1187
   179
icculus@1187
   180
    /* third row */
icculus@1187
   181
    keymap[15] = SDLK_TAB;
icculus@1187
   182
    keymap[30] = SDLK_a;
icculus@1187
   183
    keymap[31] = SDLK_s;
icculus@1187
   184
    keymap[32] = SDLK_d;
icculus@1187
   185
    keymap[33] = SDLK_f;
icculus@1187
   186
    keymap[34] = SDLK_g;
icculus@1187
   187
    keymap[35] = SDLK_h;
icculus@1187
   188
    keymap[36] = SDLK_j;
icculus@1187
   189
    keymap[37] = SDLK_k;
icculus@1187
   190
    keymap[38] = SDLK_l;
icculus@1187
   191
icculus@1187
   192
    /* fourth row */
icculus@1187
   193
    keymap[42] = SDLK_LSHIFT;
icculus@1187
   194
    keymap[44] = SDLK_z;
icculus@1187
   195
    keymap[45] = SDLK_x;
icculus@1187
   196
    keymap[46] = SDLK_c;
icculus@1187
   197
    keymap[47] = SDLK_v;
icculus@1187
   198
    keymap[48] = SDLK_b;
icculus@1187
   199
    keymap[49] = SDLK_n;
icculus@1187
   200
    keymap[50] = SDLK_m;
icculus@1187
   201
    keymap[54] = SDLK_RSHIFT;
icculus@1187
   202
    keymap[28] = SDLK_RETURN;
icculus@1187
   203
icculus@1187
   204
    /* fifth row */
icculus@1187
   205
    keymap[56] = SDLK_LALT;
icculus@1187
   206
    keymap[29] = SDLK_LCTRL;
icculus@1187
   207
    /* keymap[56] = ; */
icculus@1187
   208
    keymap[0] = SDLK_LSUPER;
icculus@1187
   209
    keymap[12] = SDLK_MINUS;
icculus@1187
   210
    keymap[57] = SDLK_SPACE;
icculus@1187
   211
    keymap[51] = SDLK_COMMA;
icculus@1187
   212
    keymap[52] = SDLK_PERIOD;
icculus@1187
   213
icculus@1187
   214
    /* misc */
icculus@1187
   215
    keymap[59] = SDLK_F1;
icculus@1187
   216
    keymap[60] = SDLK_F2;
icculus@1187
   217
    keymap[61] = SDLK_F3;
icculus@1187
   218
    keymap[62] = SDLK_F4;
icculus@1187
   219
    keymap[63] = SDLK_F5;
icculus@1187
   220
    keymap[1] = SDLK_ESCAPE;
icculus@1187
   221
    /* keymap[28] = SDLK_KP_ENTER; */
icculus@1187
   222
    keymap[72] = SDLK_UP;
icculus@1187
   223
    keymap[75] = SDLK_LEFT;
icculus@1187
   224
    keymap[77] = SDLK_RIGHT;
icculus@1187
   225
    keymap[80] = SDLK_DOWN;
icculus@1187
   226
    break;
icculus@1187
   227
icculus@1187
   228
  default:
icculus@1187
   229
    WSCONS_ReportError("Unable to map keys for keyboard type %u", 
icculus@1187
   230
		       private->kbdType);
icculus@1187
   231
    break;
icculus@1187
   232
  }
icculus@1187
   233
}
icculus@1187
   234
icculus@1187
   235
/* end of SDL_wsconsevents.c ... */
icculus@1187
   236