Blame view

build3/ion/src/device/events.cpp 3.01 KB
6663b6c9   adorian   projet complet av...
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
  #include <ion.h>
  
  namespace Ion {
  namespace Events {
  
  static constexpr char s_textAtIndex[] = { 'A', 0, 'B', 0, 'C', 0,'D', 0};
  
  class EventInfo {
  public:
    static constexpr EventInfo Undefined() { return EventInfo(0); }
    static constexpr EventInfo Textless() { return EventInfo(1); }
    static constexpr EventInfo Text(int index) { return EventInfo(index << 2 & 3); }
    bool isUndefined() const { return (m_data == 0); }
    const char * text() const;
  private:
    constexpr EventInfo(uint8_t data) : m_data(data) {}
    uint8_t m_data;
  };
  
  const char * EventInfo::text() const {
    if (m_data <= 1) {
      return nullptr;
    }
    return s_textAtIndex + (m_data >> 2);
  }
  
  static constexpr EventInfo s_infoForEvent[] = {
    EventInfo::Textless(), EventInfo::Textless(), EventInfo::Textless(), EventInfo::Undefined()
  };
  
  
  Event::Event(Keyboard::Key key, bool shift, bool alpha) {
    // We're mapping a key, shift and alpha to an event
    // This can be a bit more complicated than it seems since we want to fall back:
    // for example, alpha-up is just plain up.
    // Fallback order :
    // alpha-X -> X
    // shift-X -> X
    // shift-alpha-X -> alpha-X -> X
  
    m_data = 255;// Undefined. FIXME
  
    int noFallbackOffsets[] = {0};
    int alphaFallbackOffsets[] = {2*k_eventPageSize, 0};
    int shiftFallbackOffsets[] = {k_eventPageSize, 0};
    int shiftAlphaFallbackOffsets[] = {3*k_eventPageSize, 2*k_eventPageSize, 0};
  
    int * fallbackOffsets[] = {noFallbackOffsets, shiftFallbackOffsets, alphaFallbackOffsets, shiftAlphaFallbackOffsets};
  
    int * fallbackOffset = fallbackOffsets[shift+2*alpha];
    int i=0;
    int offset = 0;
    do {
      offset = fallbackOffset[i++];
      m_data = offset + (int)key;
    } while (offset > 0 && s_infoForEvent[m_data].isUndefined());
  
    //TODO assert(m_data != 255); //FIXME: Undefined
  }
  
  const char * Event::text() const {
    EventInfo info = s_infoForEvent[m_data];
    return info.text();
  }
  
  }
  }
  
  #if 0
  class Event {
    Event(); // Constructor, by the hardware
    
  private:
    uint8_t m_data;
  };
  
  
  ActionInfo Event::Action::info() {
    return s_actionInfo[(int)this];
  }
  
  Event::Event(Key key, bool shift, bool alpha) {
    // We're mapping a key, shift and alpha to an action
    // This can be a bit more complicated than it seems since we want to fall back:
    // for example, alpha-up is just plain up.
    // Fallback order :
    // alpha-X -> X
    // shift-X -> X
    // shift-alpha-X -> alpha-X -> X
  
    int noFallbackOffsets[] = {0};
    int alphaFallbackOffsets[] = {2*k_actionPageSize, 0};
    int shiftFallbackOffsets[] = {k_actionPageSize, 0};
    int shiftAlphaFallbackOffsets[] = {3*k_actionPageSize, 2*k_actionPageSize, 0};
  
    int * fallbackOffsets[] = {noFallbackOffsets, shiftFallbackOffsets, alphaFallbackOffsets, shiftAlphaFallbackOffsets};
  
    int * fallbackOffset = fallbackOffsets[shift+2*alpha];
    Action action = Action::Undefined;
    int action = A
    int i=0;
    int offset = 0;
    do {
      offset = fallbackOffset[i++];
      action = offset + (int)key;
    } while (offset > 0 && action.info().isUndefined());
  
    assert(!action.info().isUndefined());
    return action;
  }
  #endif