Numworks Epsilon  1.4.1
Graphing Calculator Operating System
events.cpp
Go to the documentation of this file.
1 #include <ion/events.h>
2 #include <ion/charset.h>
3 
4 extern "C" {
5 #include <assert.h>
6 }
7 
8 namespace Ion {
9 namespace Events {
10 
11 class EventData {
12 public:
13  static constexpr EventData Undefined() { return EventData(nullptr); }
14  static constexpr EventData Textless() { return EventData(k_textless); }
15  static constexpr EventData Text(const char * text) { return EventData(text); }
16  bool isDefined() const { return (m_data != nullptr); }
17  const char * text() const;
18 private:
19  static constexpr const char * k_textless = "";
20  constexpr EventData(const char * data) : m_data(data) {}
21  const char * m_data;
22 };
23 
24 #define TL() EventData::Textless()
25 #define U() EventData::Undefined()
26 #define T(x) EventData::Text(x)
27 
28 static constexpr const char k_pi[2] = {Ion::Charset::SmallPi, 0};
29 static constexpr const char k_root[4] = {Ion::Charset::Root, '(', ')', 0};
30 static constexpr const char k_complexI[2] = {Ion::Charset::IComplex, 0};
31 static constexpr const char k_exponential[5] = {Ion::Charset::Exponential, '^', '(', ')', 0};
32 static constexpr const char k_sto[2] = {Ion::Charset::Sto, 0};
33 static constexpr const char k_exponent[2] = {Ion::Charset::Exponent, 0};
34 static constexpr const char k_multiplicationSign[2] = {Ion::Charset::MultiplicationSign, 0};
35 
36 static constexpr EventData s_dataForEvent[4*Event::PageSize] = {
37 // Plain
38  TL(), TL(), TL(), TL(), TL(), TL(),
39  TL(), TL(), U(), U(), U(), U(),
40  TL(), TL(), TL(), TL(), TL(), TL(),
41  T(k_exponential), T("ln()"), T("log()"), T(k_complexI), T(","), T("^"),
42  T("sin()"), T("cos()"), T("tan()"), T(k_pi), T(k_root), T("^2"),
43  T("7"), T("8"), T("9"), T("("), T(")"), U(),
44  T("4"), T("5"), T("6"), T(k_multiplicationSign), T("/"), U(),
45  T("1"), T("2"), T("3"), T("+"), T("-"), U(),
46  T("0"), T("."), T(k_exponent), TL(), TL(), U(),
47 // Shift
48  TL(), U(), U(), TL(), U(), U(),
49  U(), U(), U(), U(), U(), U(),
50  U(), U(), TL(), TL(), TL(), TL(),
51  T("["), T("]"), T("{"), T("}"), T("_"), T(k_sto),
52  T("asin()"), T("acos()"), T("atan()"), T("="), T("<"), T(">"),
53  U(), U(), U(), U(), U(), U(),
54  U(), U(), U(), U(), U(), U(),
55  U(), U(), U(), U(), U(), U(),
56  U(), U(), U(), U(), U(), U(),
57 // Alpha
58  U(), U(), U(), U(), U(), U(),
59  U(), U(), U(), U(), U(), U(),
60  U(), U(), T(":"), T(";"), T("\""), U(),
61  T("a"), T("b"), T("c"), T("d"), T("e"), T("f"),
62  T("g"), T("h"), T("i"), T("j"), T("k"), T("l"),
63  T("m"), T("n"), T("o"), T("p"), T("q"), U(),
64  T("r"), T("s"), T("t"), T("u"), T("v"), U(),
65  T("w"), T("x"), T("y"), T("z"), T(" "), U(),
66  T("?"), T("!"), U(), U(), U(), U(),
67 // Shift+Alpha
68  U(), U(), U(), U(), U(), U(),
69  U(), U(), U(), U(), U(), U(),
70  U(), U(), U(), U(), U(), U(),
71  T("A"), T("B"), T("C"), T("D"), T("E"), T("F"),
72  T("G"), T("H"), T("I"), T("J"), T("K"), T("L"),
73  T("M"), T("N"), T("O"), T("P"), T("Q"), U(),
74  T("R"), T("S"), T("T"), T("U"), T("V"), U(),
75  T("W"), T("X"), T("Y"), T("Z"), U(), U(),
76  U(), U(), U(), U(), U(), U(),
77 };
78 
79 const char * EventData::text() const {
80  if (m_data == nullptr || m_data == k_textless) {
81  return nullptr;
82  }
83  return m_data;
84 }
85 
86 Event::Event(Keyboard::Key key, bool shift, bool alpha) {
87  // We're mapping a key, shift and alpha to an event
88  // This can be a bit more complicated than it seems since we want to fall back:
89  // for example, alpha-up is just plain up.
90  // Fallback order :
91  // shift-X -> X
92  // alpha-X -> X
93  // shift-alpha-X -> alpha-X -> X
94  assert((int)key >= 0 && (int)key < Keyboard::NumberOfKeys);
95  m_id = Events::None.m_id;
96 
97  int noFallbackOffsets[] = {0};
98  int shiftFallbackOffsets[] = {PageSize, 0};
99  int alphaFallbackOffsets[] = {2*PageSize, 0};
100  int shiftAlphaFallbackOffsets[] = {3*PageSize, 2*PageSize, 0};
101 
102  int * fallbackOffsets[] = {noFallbackOffsets, shiftFallbackOffsets, alphaFallbackOffsets, shiftAlphaFallbackOffsets};
103 
104  int * fallbackOffset = fallbackOffsets[shift+2*alpha];
105  int i=0;
106  int offset = 0;
107  do {
108  offset = fallbackOffset[i++];
109  m_id = offset + (int)key;
110  } while (offset > 0 && !s_dataForEvent[m_id].isDefined() && m_id < 4*PageSize);
111 
112  assert(m_id != Events::None.m_id);
113 }
114 
115 const char * Event::text() const {
116  if (m_id >= 4*PageSize) {
117  return nullptr;
118  }
119  return s_dataForEvent[m_id].text();
120 }
121 
122 bool Event::hasText() const {
123  return text() != nullptr;
124 }
125 
126 bool Event::isDefined() const {
127  if (isKeyboardEvent()) {
128  return s_dataForEvent[m_id].isDefined();
129  } else {
130  return (*this == None || *this == Termination || *this == USBEnumeration || *this == USBPlug);
131  }
132 }
133 
134 #if DEBUG
135 
136 static constexpr const char * s_nameForEvent[255] = {
137  // Plain
138  "Left", "Up", "Down", "Right", "OK", "Back",
139  "Home", "OnOff", nullptr, nullptr, nullptr, nullptr,
140  "Shift", "Alpha", "XNT", "Var", "Toolbox", "Backspace",
141  "Exp", "Ln", "Log", "Imaginary", "Comma", "Power",
142  "Sine", "Cosine", "Tangent", "Pi", "Sqrt", "Square",
143  "Seven", "Eight", "Nine", "LeftParenthesis", "RightParenthesis", nullptr,
144  "Four", "Five", "Six", "Multiplication", "Division", nullptr,
145  "One", "Two", "Three", "Plus", "Minus", nullptr,
146  "Zero", "Dot", "EE", "Ans", "EXE", nullptr,
147  //Shift,
148  nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
149  nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
150  nullptr, "AlphaLock", "Cut", "Copy", "Paste", "Clear",
151  "LeftBracket", "RightBracket", "LeftBrace", "RightBrace", "Underscore", "Sto",
152  "Arcsine", "Arccosine", "Arctangent", "Equal", "Lower", "Greater",
153  nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
154  nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
155  nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
156  nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
157  //Alpha,
158  nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
159  nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
160  nullptr, nullptr, "Colon", "SemiColon", "DoubleQuotes", nullptr,
161  "LowerA", "LowerB", "LowerC", "LowerD", "LowerE", "LowerF",
162  "LowerG", "LowerH", "LowerI", "LowerJ", "LowerK", "LowerL",
163  "LowerM", "LowerN", "LowerO", "LowerP", "LowerQ", nullptr,
164  "LowerR", "LowerS", "LowerT", "LowerU", "LowerV", nullptr,
165  "LowerW", "LowerX", "LowerY", "LowerZ", "Space", nullptr,
166  "Question", "Exclamation", nullptr, nullptr, nullptr, nullptr,
167  //Shift+Alpha,
168  nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
169  nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
170  nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
171  "UpperA", "UpperB", "UpperC", "UpperD", "UpperE", "UpperF",
172  "UpperG", "UpperH", "UpperI", "UpperJ", "UpperK", "UpperL",
173  "UpperM", "UpperN", "UpperO", "UpperP", "UpperQ", nullptr,
174  "UpperR", "UpperS", "UpperT", "UpperU", "UpperV", nullptr,
175  "UpperW", "UpperX", "UpperY", "UpperZ", nullptr, nullptr,
176  // Special
177  "None", "Termination", nullptr, nullptr, nullptr, nullptr,
178 };
179 
180 const char * Event::name() const {
181  return s_nameForEvent[m_id];
182 }
183 
184 #endif
185 
186 }
187 }
#define assert(e)
Definition: assert.h:9
def data
Definition: i18n.py:176
bool isDefined() const
Definition: events.cpp:126
#define U()
Definition: events.cpp:25
#define T(x)
Definition: events.cpp:26
constexpr Event None
Definition: events.h:215
static constexpr EventData Text(const char *text)
Definition: events.cpp:15
bool isDefined() const
Definition: events.cpp:16
const char * text() const
Definition: events.cpp:79
constexpr int NumberOfKeys
Definition: keyboard.h:33
bool hasText() const
Definition: events.cpp:122
#define TL()
Definition: events.cpp:24
const char * text() const
Definition: events.cpp:61
constexpr Event USBPlug
Definition: events.h:219
constexpr Event()
Definition: events.h:17
bool isKeyboardEvent() const
Definition: events.h:33
static constexpr EventData Undefined()
Definition: events.cpp:13
static constexpr EventData Textless()
Definition: events.cpp:14
static constexpr int PageSize
Definition: events.h:36
constexpr Event USBEnumeration
Definition: events.h:218
Definition: backlight.h:6
constexpr Event Termination
Definition: events.h:216