summaryrefslogtreecommitdiffstats
path: root/dispatch.c
blob: 230d430c02ae7197c042e0355d66a6713fa8a811 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "project.h"

#define N_SCREENS 4
#define GRACE 10

#define W 2048
#define H 2048

static int current_computer = 0;
static int keys_down;
static int current_x, current_y;
static int mouse_button[0x10];


void
dispatch_key (int key, int ud)
{
  static int keys[KEY_MAX];

// ignore autorepeat
  if (ud == 2)
    return;

  if (keys[key] != ud)
    {
      keys[key] = ud;
      if (ud)
        keys_down++;
      else
        keys_down--;
    }

  if (ud && keys[KEY_RIGHTALT] && (key == KEY_RIGHTMETA))
    {
      lcd_off ();
      output_reset ();
      status_reset ();
    }

  if (ud && keys[KEY_LEFTALT] && (key == KEY_LEFTMETA))
    layout_toggle_lock (current_computer);

  send_keyboard_event (current_computer, key, ud);
}

void
dispatch_mouse_button (int button, int ud)
{

  if (mouse_button[button] != ud)
    {
      mouse_button[button] = ud;
      if (ud)
        keys_down++;
      else
        keys_down--;
    }

  send_mouse_event (current_computer, current_x, current_y, 0,
                    mouse_button[0], mouse_button[2], mouse_button[1]);
}


void
dispatch_mouse_wheel (int d)
{
  send_mouse_event (current_computer, current_x, current_y, d,
                    mouse_button[0], mouse_button[2], mouse_button[1]);
}

static int
accell (int a)
{

  int m = a / 10;

  if (m < 0)
    m = -m;
  if (m == 0)
    m = 1;
  if (m > 4)
    m = 4;

  return m * a;
}


void
dispatch_mouse_motion (int rx, int ry)
{

  ry += ry;

  rx = accell (rx);
  ry = accell (ry);

  current_x += rx;
  current_y += ry;

  if (current_y < 0)
    current_y = 0;
  if (current_y >= H)
    current_y = H - 1;

  if (current_x < 0)
    {
      if (map_grace_left (current_computer))
        {
          if (keys_down)
            {
              current_x = 0;
            }
          else if (current_x < -GRACE)
            {
              if (map_switch_left (&current_computer))
                {
                  current_x = W - 1;
                }
              else
                {
                  current_x = -GRACE;
                }
            }
        }
      else
        {
          if (map_switch_left (&current_computer))
            {
              current_x = W - 1;
            }
          else
            {
              current_x = 0;
            }
        }
    }

  if (current_x >= W)
    {
      if (map_grace_right (current_computer))
        {
          if (keys_down)
            {
              current_x = W - 1;
            }
          else if (current_x > (W + GRACE))
            {
              if (map_switch_right (&current_computer))
                {
                  current_x = 0;
                }
              else
                {
                  current_x = W + GRACE;
                }
            }
        }
      else
        {
          if (map_switch_right (&current_computer))
            {
              current_x = 0;
            }
          else
            {
              current_x = W - 1;
            }
        }
    }

  send_mouse_event (current_computer, current_x, current_y, 0,
                    mouse_button[0], mouse_button[2], mouse_button[1]);
}


void
dispatch_event (struct input_event *ev)
{
  static int rel_x, rel_y, rel_wheel;

  if (ev->type == EV_REL)
    {

      if (ev->code == REL_X)
        rel_x = ev->value;
      if (ev->code == REL_Y)
        rel_y = ev->value;
      if (ev->code == REL_WHEEL)
        rel_wheel = ev->value;
    }
  else if (ev->type == EV_KEY)
    {
      switch (ev->code)
        {
        case BTN_LEFT:
        case BTN_RIGHT:
        case BTN_MIDDLE:
        case BTN_SIDE:
        case BTN_EXTRA:
        case BTN_FORWARD:
        case BTN_BACK:
        case BTN_TASK:
          dispatch_mouse_button (ev->code - BTN_LEFT, ev->value);
          break;
        default:
          dispatch_key (ev->code, ev->value);
        }
    }
  else if (ev->type == EV_SYN)
    {
      if (rel_x || rel_y)
        {
          dispatch_mouse_motion (rel_x, rel_y);
          rel_x = 0;
          rel_y = 0;
        }
      if (rel_wheel)
        {
          dispatch_mouse_wheel (rel_wheel);
          rel_wheel = 0;
        }

    }

//print_event(ev);

}

void
dispatch_init (void)
{
  current_computer = map_init ();
}