summaryrefslogtreecommitdiffstats
path: root/output.c
blob: c12262c514f158f24ce49d3aa0548c563b951779 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include "project.h"

#include "output_map.h"

output_dev_t *output_devs;

static output_dev_t *
iface_to_dev (int iface)
{
  output_dev_t *ret;
  for (ret = output_devs; ret; ret = ret->next)
    {
      if (!(iface--))
        return ret;
    }
  return NULL;
}



static int
send_flush (output_dev_t * o)
{
  int ret;
  uint8_t rbuf[36];
  uint8_t xbuf[31] = {
    0x55, 0x53, 0x42, 0x43,
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x06, 0x00,
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00,
    0x00,
    0x00, 0x00
  };
  memcpy (&xbuf[4], &o->seq, 4);
  o->seq++;

/*Sends SCSI test unit ready*/

  if (usb_bulk_write (o->devh, 0x2, (char *) xbuf, 31, 10) != 31)
    return -1;
  ret = usb_bulk_read (o->devh, 0x81, (char *) rbuf, 36, 10);
  if (ret <= 0)
    return -1;


  return 0;
}

static int
send_msg (output_dev_t * o, uint8_t * msg)
{
  int ret;
  uint8_t xbuf[31] = {
    0x55, 0x53, 0x42, 0x43,
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x10, 0xd9,
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00,
    0x00,
    0x4f, 0x54
  };
  uint8_t rbuf[36];

  memcpy (&xbuf[16], msg, 13);
  memcpy (&xbuf[4], &o->seq, 4);
  o->seq++;

/* READ CD-DA MSF !*/

  if (usb_bulk_write (o->devh, 0x2, (char *) xbuf, 31, 10) != 31)
    return -1;
  ret = usb_bulk_read (o->devh, 0x81, (char *) rbuf, 36, 10);
  if (ret <= 0)
    return -1;


  return 0;
}

static int
mouse (output_dev_t * o, uint16_t x, uint16_t y, uint8_t s, int l,
       int m, int r)
{
  uint8_t bmask = (l ? 1 : 0) | (m ? 4 : 0) | (r ? 2 : 0);
  uint8_t msg[13] = { 0x33, bmask, s, x & 0xff,
    x >> 8, y & 0xff, y >> 8, 0x00,
    0x00, 0x00, 0x00, 0x00,
    0x00
  };

//  printf ("Mouse x=%4d y=%4d scroll=%2x bmask=%x\n", x, y, s, bmask);

  return send_msg (o, msg);
}

static int
send_key_list (output_dev_t * o, uint8_t modifiers)
{
  uint8_t msg[13] = { 0x34, modifiers, 0x00 };
  memcpy (&msg[3], o->keys, OUTPUT_KEY_LIST_LEN);

#if 0
  int i;
  printf ("Keybd modifiers mask=%x key list=", modifiers);

  for (i = 0; i < OUTPUT_KEY_LIST_LEN; ++i)
    {
      if (o->keys[i])
        printf ("%02x ", o->keys[i]);
    }
  printf ("\n");
#endif

  return send_msg (o, msg);
}



static int
key_down (output_dev_t * o, uint8_t key, uint8_t modifiers)
{
  int i;

//printf("Key %d down modifiers %x\n",key,modifiers);

  if (key)
    {
      do
        {
          for (i = 0; i < sizeof (o->keys); ++i)
            {
              if (o->keys[i] == key)
                break;
            }

          for (i = 0; i < sizeof (o->keys); ++i)
            {
              if (!o->keys[i])
                {
                  o->keys[i] = key;
                  break;
                }
            }
        }
      while (0);
    }

  return send_key_list (o, modifiers);
}


static int
key_up (output_dev_t * o, uint8_t key, uint8_t modifiers)
{
  int i;

//printf("Key %d up modifiers %x\n",key,modifiers);

  if (key)
    {
      for (i = 0; i < sizeof (o->keys); ++i)
        {
          if (o->keys[i] == key)
            o->keys[i] = 0;
        }
    }

  return send_key_list (o, modifiers);
}





struct map_ent *
lookup_map_ent (int keycode, uint8_t modifiers)
{
  int i;

  for (i = 0; i < MAP_LEN; ++i)
    {
      if (map[i].keycode == keycode)
        {
          if (map[i].hacks & (HACK_RCS | HACK_HS))
            {                   /*Do we require a shift key to match ? */
              if (modifiers & HACK_SHIFT_MASK)
                return &map[i];
            }
          else
            {
              return &map[i];
            }
        }
    }
  return NULL;
}





void
send_keyboard_event (int computer, int k, int ud)
{
  static uint8_t modifiers;
  uint8_t mods;
  struct map_ent *e = lookup_map_ent (k, modifiers);
  output_dev_t *o;

  map_output (&computer, NULL, NULL);

  o = iface_to_dev (computer);

  if (!o)
    return;

  if (!e)
    return;

  if (e->hacks & HACK_MODIFIER_MASK)
    {
      if (ud)
        {
          modifiers |= e->hacks & HACK_MODIFIER_MASK;
        }
      else
        {
          modifiers &= ~(e->hacks & HACK_MODIFIER_MASK);
        }
    }

  if (e->hacks & HACK_HS)
    {
      mods = modifiers & ~HACK_SHIFT_MASK;

      if (ud)
        {
          if (modifiers & HACK_SHIFT_L)
            key_up (o, 0xe1, mods);
          if (modifiers & HACK_SHIFT_R)
            key_up (o, 0xe5, mods);
        }


    }
  else
    {
      mods = modifiers;
    }

#if 0
  printf
    ("Hacks %8x, modifiers=%4x, mods=%4x, e->keysym=%4x, e->keycode=%4x, e->code=%2x\n",
     e->hacks, modifiers, mods, e->keysym, e->keycode, e->code);
#endif

  if (ud)
    key_down (o, e->code, mods);
  else
    key_up (o, e->code, mods);

  if (e->hacks & HACK_HS)
    {
      if (!ud)
        {
          if (modifiers & HACK_SHIFT_L)
            key_down (o, 0xe1, modifiers);
          if (modifiers & HACK_SHIFT_R)
            key_down (o, 0xe5, modifiers);
        }
    }


  send_flush (o);
}



void
send_mouse_event (int computer, int x, int y, int s, int l, int m, int r)
{
  output_dev_t *o;


  map_output (&computer, &x, &y);

  o = iface_to_dev (computer);


  if (!o)
    return;

  if (s < 0)
    s += 256;

  if (x < 0)
    x = 0;
  if (y < 0)
    y = 0;
  if (x > 2047)
    x = 2047;
  if (y > 2047)
    y = 2047;

  mouse (o, x, y, s, l, m, r);

  if (s)
    send_flush (o);
}



static output_dev_t *
get_output_dev (struct usb_device *dev)
{
  struct usb_dev_handle *devh;

  output_dev_t *o;


  for (o = output_devs; o; o = o->next)
    if (!strcmp (o->filename, dev->filename))
      return o;


  printf ("New output: %s\n", dev->filename);

  devh = usb_open (dev);
  if (!devh)
    return NULL;

  usb_reset (devh);
  usb_close (devh);
  usleep (100000);

  devh = usb_open (dev);

  if (!devh)
    return NULL;



  usb_detach_kernel_driver_np (devh, 0);
  usb_detach_kernel_driver_np (devh, 1);
  usb_detach_kernel_driver_np (devh, 2);
  usb_claim_interface (devh, 0);


  usb_clear_halt (devh, 0x81);
  usb_clear_halt (devh, 0x2);

  o = malloc (sizeof (*o));
  bzero (o, sizeof (*o));

  strcpy (o->filename, dev->filename);
  o->devh = devh;

  o->next = output_devs;

  output_devs = o;

  return o;
}

static void
free_output_dev (output_dev_t * o)
{
  printf ("Lost output: %s\n", o->filename);
  if (o->devh)
    usb_close (o->devh);
  free (o);
}


void
scan_output_devs (int init)
{
  struct usb_bus *bus;
  struct usb_device *dev;
  output_dev_t *od, **odp;

  usb_find_busses ();
  if (!init && !usb_find_devices ())
    return;

  for (od = output_devs; od; od = od->next)
    od->present = 0;

  for (bus = usb_get_busses (); bus; bus = bus->next)
    {
      for (dev = bus->devices; dev; dev = dev->next)
        {
          if ((dev->descriptor.idVendor == 0x0ea0) &&
              (dev->descriptor.idProduct == 0x2211))
            {

              od = get_output_dev (dev);
              if (od)
                od->present = 1;
            }
        }
    }



  for (odp = &output_devs; (od = *odp);)
    {
      if (!od->present)
        {
          *odp = od->next;
          free_output_dev (od);
        }
      else
        {
          odp = &od->next;
        }
    }


}

void
output_reset (void)
{
  output_dev_t *od, **odp;

  for (odp = &output_devs; (od = *odp);)
    {
      *odp = od->next;
      free_output_dev (od);
    }

  scan_output_devs (1);


}