summaryrefslogtreecommitdiff
path: root/src/keys.c
blob: b87a29505149f10fb1821a68bd7b4ee5e997d204 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 *  LiberTI - TI-like calculator designed for LibreCalc
 *  Copyright (C) 2016 Delwink, LLC
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, version 3 only.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "keys.h"
#include "tibchar.h"

static const SDL_Keycode UNCHANGED_RANGES[] =
  {
    SDLK_0, SDLK_9
  };

static const SDL_Keycode UNCHANGED[] =
  {
    SDLK_PERIOD,
    SDLK_MINUS,
    SDLK_SLASH,

    SDLK_e,
    SDLK_i
  };

int
normalize_keycode (SDL_Keycode code, SDL_Keymod mod)
{
  if (mod & KMOD_CTRL)
    {
      switch (code)
        {
        case SDLK_a:
          return TIB_CHAR_ANS;

        case SDLK_d:
          return TIB_CHAR_DEGREE;

        case SDLK_e:
          return TIB_CHAR_EPOW10;

        case SDLK_p:
          return TIB_CHAR_PI;
        }
    }
  else if (mod & KMOD_SHIFT)
    {
      switch (code)
        {
        case SDLK_EQUALS:
          return '+';

        case SDLK_1:
          return '!';

        case SDLK_4:
          return TIB_CHAR_STO;

        case SDLK_6:
          return '^';

        case SDLK_8:
          return '*';

        case SDLK_9:
          return '(';

        case SDLK_0:
          return ')';
        }
    }
  else
    {
      switch (code)
        {
        case SDLK_KP_0:
          return '0';

        case SDLK_KP_PERIOD:
          return '.';

        case SDLK_KP_PLUS:
          return '+';

        case SDLK_KP_MINUS:
          return '-';

        case SDLK_KP_MULTIPLY:
          return '*';

        case SDLK_KP_DIVIDE:
          return '/';

        case SDLK_c:
          return TIB_CHAR_COS;

        case SDLK_s:
          return TIB_CHAR_SIN;

        case SDLK_t:
          return TIB_CHAR_TAN;
        }
    }

  for (unsigned int i = 0; i < (sizeof UNCHANGED_RANGES / sizeof (int)); ++i)
    if (code >= UNCHANGED_RANGES[i++] && code <= UNCHANGED_RANGES[i])
      return code;

  for (unsigned int i = 0; i < (sizeof UNCHANGED / sizeof (int)); ++i)
    if (code == UNCHANGED[i])
      return code;

  if (code >= SDLK_KP_1 && code <= SDLK_KP_9)
    return code - SDLK_KP_1 + '1';

  if (code >= SDLK_a && code <= SDLK_z)
    return code - SDLK_a + 'A';

  return 0;
}