forked from farva/synergy-haiku
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhaiku-usynergy.cpp
796 lines (643 loc) · 20.3 KB
/
haiku-usynergy.cpp
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
/*
* Copyright (c)
* 2014 Ed Robbins <edd.robbins@gmail.com>
* 2014 Jessica Hamilton <jessica.l.hamilton@gmail.com>
* 2015-2016 Alexander von Gluck IV <kallisti5@unixzen.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <Application.h>
#include <Autolock.h>
#include <Bitmap.h>
#include <Clipboard.h>
#include <FindDirectory.h>
#include <IconUtils.h>
#include <Mime.h>
#include <NodeMonitor.h>
#include <Notification.h>
#include <OS.h>
#include <Path.h>
#include <PathFinder.h>
#include <PathMonitor.h>
#include <Resources.h>
#include <Screen.h>
#include <TranslationUtils.h>
#include <stdio.h>
#include <cstdlib>
#include <strings.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <driver_settings.h>
#include <keyboard_mouse_driver.h>
#include "ATKeymap.h"
#include "haiku-usynergy.h"
#define TRACE_SYNERGY_DEVICE
#ifdef TRACE_SYNERGY_DEVICE
# define TRACE(x...) \
do { debug_printf(x); } while (0)
#else
# define TRACE(x...) do {} while (0)
#endif
#define FILE_UPDATED 'fiUp'
static status_t our_image(image_info& image)
{
int32 cookie = 0;
while (get_next_image_info(B_CURRENT_TEAM, &cookie, &image) == B_OK) {
if ((char *)our_image >= (char *)image.text
&& (char *)our_image <= (char *)image.text + image.text_size)
return B_OK;
}
return B_ERROR;
}
const static uint32 kSynergyThreadPriority = B_FIRST_REAL_TIME_PRIORITY + 4;
// Static hook functions for uSynergy
static uSynergyBool uConnect(uSynergyCookie cookie)
{
uSynergyInputServerDevice* device = (uSynergyInputServerDevice*)cookie;
return device->Connect();
}
static uSynergyBool uSend(uSynergyCookie cookie, const uint8* buffer, int length)
{
uSynergyInputServerDevice* device = (uSynergyInputServerDevice*)cookie;
return device->Send(buffer, length);
}
static uSynergyBool uReceive(uSynergyCookie cookie, uint8* buffer, int maxLength, int* outLength) {
uSynergyInputServerDevice* device = (uSynergyInputServerDevice*)cookie;
if ((uSynergyInputServerDevice*)device->Receive(buffer, maxLength, outLength))
return USYNERGY_TRUE;
return USYNERGY_FALSE;
}
static void uSleep(uSynergyCookie cookie, int milliseconds)
{
snooze(milliseconds * 1000);
}
static uint32_t uGetTime()
{
return system_time() / 1000;
}
static void uTrace(uSynergyCookie cookie, const char* text) {
uSynergyInputServerDevice* device = (uSynergyInputServerDevice*)cookie;
device->Trace(text);
}
static void uScreenActive(uSynergyCookie cookie, uSynergyBool active) {
uSynergyInputServerDevice* device = (uSynergyInputServerDevice*)cookie;
device->ScreenActive(active);
}
static void uMouseCallback(uSynergyCookie cookie, uint16 x, uint16 y, int16 wheelX, int16 wheelY, uSynergyBool buttonLeft,
uSynergyBool buttonRight, uSynergyBool buttonMiddle)
{
uSynergyInputServerDevice* device = (uSynergyInputServerDevice*)cookie;
device->MouseCallback(x, y, wheelX, wheelY, buttonLeft,
buttonRight, buttonMiddle);
}
static void uKeyboardCallback(uSynergyCookie cookie, uint16 key, uint16 modifiers, uSynergyBool isKeyDown, uSynergyBool isKeyRepeat)
{
uSynergyInputServerDevice* device = (uSynergyInputServerDevice*)cookie;
device->KeyboardCallback(key, modifiers, isKeyDown, isKeyRepeat);
}
static void uJoystickCallback(uSynergyCookie cookie, uint8_t joyNum, uint16_t buttons,
int8_t leftStickX, int8_t leftStickY, int8_t rightStickX, int8_t rightStickY)
{
uSynergyInputServerDevice* device = (uSynergyInputServerDevice*)cookie;
device->JoystickCallback(joyNum, buttons, leftStickX, leftStickY,
rightStickX, rightStickY);
}
static void uClipboardCallback(uSynergyCookie cookie, enum uSynergyClipboardFormat format,
const uint8_t* data, uint32_t size)
{
uSynergyInputServerDevice* device = (uSynergyInputServerDevice*)cookie;
device->ClipboardCallback(format, data, size);
}
uSynergyInputServerDevice::uSynergyInputServerDevice()
:
BHandler("uSynergy Handler"),
threadActive(false),
fContext(NULL),
fSocket(-1),
fEnableSynergy(false),
fServerAddress(NULL),
fServerKeymap(NULL),
fClientName(DEFAULT_NAME),
fEnableCLipboard(false),
fUpdateSettings(false),
fKeymapLock("synergy keymap lock")
{
fContext = (uSynergyContext*)malloc(sizeof(uSynergyContext));
uSynergyInit(fContext);
fContext->m_connectFunc = uConnect;
fContext->m_receiveFunc = uReceive;
fContext->m_sendFunc = uSend;
fContext->m_getTimeFunc = uGetTime;
fContext->m_screenActiveCallback = uScreenActive;
fContext->m_mouseCallback = uMouseCallback;
fContext->m_keyboardCallback = uKeyboardCallback;
fContext->m_sleepFunc = uSleep;
fContext->m_traceFunc = uTrace;
fContext->m_joystickCallback = uJoystickCallback;
fContext->m_clipboardCallback = uClipboardCallback;
fContext->m_clientName = fClientName;
fContext->m_messageName = fMessageName;
fContext->m_cookie = (uSynergyCookie)this;
BRect screenRect = BScreen().Frame();
fContext->m_clientWidth = (uint16_t)screenRect.Width() + 1;
fContext->m_clientHeight = (uint16_t)screenRect.Height() + 1;
if (be_app->Lock()) {
be_app->AddHandler(this);
be_app->Unlock();
}
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK)
path.Append("synergy");
fFilename = new char[strlen(path.Path()) + 1];
strcpy(fFilename, path.Path());
BEntry entry(fFilename);
BPrivate::BPathMonitor::StartWatching(fFilename,
B_WATCH_STAT | B_WATCH_FILES_ONLY, this);
_UpdateSettings();
}
uSynergyInputServerDevice::~uSynergyInputServerDevice()
{
if (be_app->Lock()) {
be_app->RemoveHandler(this);
be_app->Unlock();
}
free(fContext);
}
status_t uSynergyInputServerDevice::InitCheck()
{
input_device_ref *devices[3];
input_device_ref mouse = { (char*)"uSynergy Mouse", B_POINTING_DEVICE,
(void *)this };
input_device_ref keyboard = { (char*)"uSynergy Keyboard", B_KEYBOARD_DEVICE,
(void *)this };
devices[0] = &mouse;
devices[1] = &keyboard;
devices[2] = NULL;
RegisterDevices(devices);
return B_OK;
}
void uSynergyInputServerDevice::MessageReceived(BMessage* message)
{
switch (message->what) {
case B_PATH_MONITOR:
{
const char* path = "";
// only fall through for appropriate file
if (!(message->FindString("path", &path) == B_OK && strcmp(path, fFilename) == 0)) {
break; // not the file we're looking for
}
}
// fall-through
case FILE_UPDATED:
{
_UpdateSettings();
if (threadActive)
Stop(NULL, NULL);
Start(NULL, NULL);
break;
}
case B_CLIPBOARD_CHANGED:
{
// BUG #4735, in old synergy servers the feature returns "ERROR: invalid message from client: DCLP".
// Disabled by default.
if(fEnableCLipboard == true)
{
const char *text = NULL;
ssize_t len = 0;
BMessage *clip = NULL;
if (be_clipboard->Lock()) {
clip = be_clipboard->Data();
if (clip != NULL) {
clip->FindData("text/plain", B_MIME_TYPE, (const void **)&text, &len);
//Trace(text);
}
be_clipboard->Unlock();
}
if (len > 0 && text != NULL) {
uSynergySendClipboard(fContext, text);
TRACE("synergy: data added to clipboard\n");
} else
TRACE("synergy: couldn't add data to clipboard\n");
}
}
default:
BHandler::MessageReceived(message);
}
}
status_t uSynergyInputServerDevice::Start(const char* name, void* cookie)
{
if (fServerAddress.Length() == 0 || fEnableSynergy == false) {
TRACE("synergy: not enabled, or no server specified\n");
return B_NO_ERROR;
}
status_t status = B_OK;
char threadName[B_OS_NAME_LENGTH];
snprintf(threadName, B_OS_NAME_LENGTH, "uSynergy haiku");
TRACE("synergy: thread active = %d\n", threadActive);
if ((atomic_get_and_set((int32*)&threadActive, true) & true) == true) {
TRACE("synergy: main thread already running\n");
return B_OK;
}
uSynergyThread = spawn_thread(_MainLoop, threadName, kSynergyThreadPriority,
(void*)this);
if (uSynergyThread < 0) {
threadActive = false;
status = uSynergyThread;
TRACE("synergy: spawn thread failed: %" B_PRIx32 "\n", status);
} else {
be_clipboard->StartWatching(this);
status = resume_thread(uSynergyThread);
}
return status;
}
status_t uSynergyInputServerDevice::Stop(const char* name, void* cookie)
{
threadActive = false;
// this will stop the thread as soon as it reads the next packet
be_clipboard->StopWatching(this);
if (uSynergyThread >= 0) {
// unblock the thread, which might wait on a semaphore.
suspend_thread(uSynergyThread);
resume_thread(uSynergyThread);
status_t dummy;
wait_for_thread(uSynergyThread, &dummy);
}
return B_OK;
}
status_t uSynergyInputServerDevice::SystemShuttingDown()
{
threadActive = false;
return B_OK;
}
status_t uSynergyInputServerDevice::Control(const char* name, void* cookie,
uint32 command, BMessage* message)
{
if (command == B_KEY_MAP_CHANGED) {
fUpdateSettings = true;
return B_OK;
}
return B_BAD_VALUE;
}
status_t uSynergyInputServerDevice::_MainLoop(void* arg)
{
uSynergyInputServerDevice *inputDevice = (uSynergyInputServerDevice*)arg;
while (inputDevice->threadActive) {
uSynergyUpdate(inputDevice->fContext);
if (inputDevice->fUpdateSettings) {
inputDevice->_UpdateSettings();
inputDevice->fUpdateSettings = false;
}
}
close(inputDevice->fSocket);
inputDevice->fSocket = -1;
return B_OK;
}
void uSynergyInputServerDevice::_UpdateSettings()
{
BAutolock lock(fKeymapLock);
fKeymap.RetrieveCurrent();
fModifiers = fKeymap.Map().lock_settings;
fControlKey = fKeymap.KeyForModifier(B_LEFT_CONTROL_KEY);
fCommandKey = fKeymap.KeyForModifier(B_LEFT_COMMAND_KEY);
void* handle = load_driver_settings(fFilename);
if (handle == NULL)
return;
fEnableSynergy = get_driver_boolean_parameter(handle, "enable", false, false);
fUseBarrier = get_driver_boolean_parameter(handle, "use_barrier", false, false);
fServerKeymap = get_driver_parameter(handle, "server_keymap", NULL, NULL);
fServerAddress = get_driver_parameter(handle, "server", NULL, NULL);
fClientName = get_driver_parameter(handle, "client_name", DEFAULT_NAME, DEFAULT_NAME);
fEnableCLipboard = get_driver_boolean_parameter(handle, "enableCLipboard", false, false); //Get the config variable and set the value. Default is false!
if (fUseBarrier) {
fMessageName = "Barrier";
} else {
fMessageName = "Synergy";
}
fContext->m_messageName = fMessageName;
fContext->m_clientName = fClientName;
unload_driver_settings(handle);
}
bool uSynergyInputServerDevice::Connect()
{
if (fServerAddress.Length() == 0 || fEnableSynergy == false)
goto exit;
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(24800);
inet_aton(fServerAddress.String(), &server.sin_addr);
TRACE("synergy: connecting to %s:%d\n", fServerAddress.String(), 24800);
fSocket = socket(PF_INET, SOCK_STREAM, 0);
if (fSocket < 0) {
TRACE("synergy: socket couldn't be created\n");
goto exit;
}
if (connect(fSocket, (struct sockaddr*)&server,
sizeof(struct sockaddr)) < 0 ) {
TRACE("synergy: %s: %x\n", "failed to connect to remote host", errno);
close(fSocket);
fSocket = -1;
goto exit;
} else
return true;
exit:
snooze(1000000);
return false;
}
bool
uSynergyInputServerDevice::Send(const uint8_t* buffer, int32_t length)
{
if (send(fSocket, buffer, length, 0) != length)
return false;
return true;
}
bool uSynergyInputServerDevice::Receive(uint8_t *buffer, int maxLength, int* outLength)
{
if ((*outLength = recv(fSocket, buffer, maxLength, 0)) == -1)
return false;
return true;
}
void uSynergyInputServerDevice::Trace(const char *text)
{
BNotification notify(B_INFORMATION_NOTIFICATION);
BString group(fMessageName);
BString content(text);
notify.SetGroup(group);
notify.SetContent(content);
image_info info;
if (our_image(info) != B_OK)
return;
BFile file(info.name, B_READ_ONLY);
if (file.InitCheck() < B_OK)
return;
BResources res(&file);
size_t size;
const void* data = res.LoadResource(B_VECTOR_ICON_TYPE, "icon", &size);
if (!data) {
TRACE("Unable to load resource for notification!");
notify.Send();
return;
}
BBitmap* bitmap = new BBitmap(BRect(0, 0, 32, 32), B_RGBA32);
status_t status = BIconUtils::GetVectorIcon((uint8*)data, size, bitmap);
if (status == B_OK)
notify.SetIcon(bitmap);
notify.Send();
delete bitmap;
}
void uSynergyInputServerDevice::ScreenActive(bool active)
{
}
BMessage* uSynergyInputServerDevice::_BuildMouseMessage(uint32 what, uint64 when, uint32 buttons, float x, float y) const
{
BMessage* message = new BMessage(what);
if (message == NULL)
return NULL;
if (message->AddInt64("when", when) < B_OK
|| message->AddInt32("buttons", buttons) < B_OK
|| message->AddFloat("x", x) < B_OK
|| message->AddFloat("y", y) < B_OK) {
delete message;
return NULL;
}
return message;
}
void uSynergyInputServerDevice::MouseCallback(uint16_t x, uint16_t y, int16_t wheelX,
int16_t wheelY, uSynergyBool buttonLeft, uSynergyBool buttonRight,
uSynergyBool buttonMiddle)
{
static uint32_t oldButtons = 0, oldPressedButtons = 0;
uint32_t buttons = 0;
static uint16_t oldX = 0, oldY = 0, clicks = 0;
static int16_t oldWheelX = 0, oldWheelY = 0;
static uint64 oldWhen = system_time();
float xVal = (float)x / (float)fContext->m_clientWidth;
float yVal = (float)y / (float)fContext->m_clientHeight;
int64 timestamp = system_time();
if (buttonLeft == USYNERGY_TRUE) {
buttons |= 1 << 0;
}
if (buttonRight == USYNERGY_TRUE) {
buttons |= 1 << 1;
}
if (buttonMiddle == USYNERGY_TRUE) {
buttons |= 1 << 2;
}
if (buttons != oldButtons) {
bool pressedButton = buttons > 0;
BMessage* message = _BuildMouseMessage(pressedButton ? B_MOUSE_DOWN : B_MOUSE_UP, timestamp, buttons, xVal, yVal);
if (pressedButton) {
if ((buttons == oldPressedButtons) && ((timestamp - oldWhen) < 500000))
clicks++;
else
clicks = 1;
message->AddInt32("clicks", clicks);
oldWhen = timestamp;
oldPressedButtons = buttons;
} else
clicks = 1;
if (message != NULL)
EnqueueMessage(message);
oldButtons = buttons;
}
if ((x != oldX) || (y != oldY)) {
BMessage* message = _BuildMouseMessage(B_MOUSE_MOVED, timestamp,
buttons, xVal, yVal);
if (message != NULL)
EnqueueMessage(message);
oldX = x;
oldY = y;
}
if (wheelX != 0 || wheelY != 0) {
BMessage* message = new BMessage(B_MOUSE_WHEEL_CHANGED);
if (message != NULL) {
if (message->AddInt64("when", timestamp) == B_OK
&& message->AddFloat("be:wheel_delta_x",
(oldWheelX - wheelX) / 120) == B_OK
&& message->AddFloat("be:wheel_delta_y",
(oldWheelY - wheelY) / 120) == B_OK)
EnqueueMessage(message);
else
delete message;
}
oldWheelX = wheelX;
oldWheelY = wheelY;
}
}
void uSynergyInputServerDevice::KeyboardCallback(uint16_t scancode, uint16_t _modifiers, bool isKeyDown, bool isKeyRepeat)
{
static uint32 lastScanCode = 0;
static uint32 repeatCount = 1;
static uint8 states[16];
int64 timestamp = system_time();
uint32_t keycode = 0;
// XXX: This is a dirty hack.
// See https://github.com/synergy/synergy/issues/4640
if (fServerKeymap == "X11") {
if (scancode > 0 && scancode < sizeof(kXKeycodeMap)/sizeof(uint32))
keycode = kXKeycodeMap[scancode - 1];
else {
scancode = (uint8)(scancode | 0x80);
if (scancode > 0 && scancode < sizeof(kXKeycodeMap)/sizeof(uint32))
keycode = kXKeycodeMap[scancode - 1];
}
} else {
if (scancode > 0 && scancode < sizeof(kATKeycodeMap)/sizeof(uint32))
keycode = kATKeycodeMap[scancode - 1];
else {
scancode = (uint8)(scancode | 0x80);
if (scancode > 0 && scancode < sizeof(kATKeycodeMap)/sizeof(uint32))
keycode = kATKeycodeMap[scancode - 1];
}
}
TRACE("synergy: scancode = 0x%02x, keycode = 0x%x\n", scancode, keycode);
if (keycode < 256) {
if (isKeyDown)
states[(keycode) >> 3] |= (1 << (7 - (keycode & 0x7)));
else
states[(keycode) >> 3] &= (!(1 << (7 - (keycode & 0x7))));
}
else
{
Trace("keycode invalid");
}
#if false
if (isKeyDown && keycode == 0x34 // DELETE KEY
&& (states[fCommandKey >> 3] & (1 << (7 - (fCommandKey & 0x7))))
&& (states[fControlKey >> 3] & (1 << (7 - (fControlKey & 0x7))))) {
TRACE("synergy: TeamMonitor called\n");
}
#endif
uint32 modifiers = 0;
if (_modifiers & USYNERGY_MODIFIER_SHIFT)
modifiers |= B_SHIFT_KEY | B_LEFT_SHIFT_KEY;
if (_modifiers & USYNERGY_MODIFIER_CTRL)
modifiers |= B_CONTROL_KEY | B_LEFT_CONTROL_KEY;
if (_modifiers & USYNERGY_MODIFIER_ALT)
modifiers |= B_COMMAND_KEY | B_LEFT_COMMAND_KEY;
if (_modifiers & USYNERGY_MODIFIER_META)
modifiers |= B_MENU_KEY;
if (_modifiers & USYNERGY_MODIFIER_WIN)
modifiers |= B_OPTION_KEY | B_LEFT_OPTION_KEY;
if (_modifiers & USYNERGY_MODIFIER_ALT_GR)
modifiers |= B_RIGHT_OPTION_KEY | B_OPTION_KEY;
if (_modifiers & USYNERGY_MODIFIER_CAPSLOCK)
modifiers |= B_CAPS_LOCK;
if (_modifiers & USYNERGY_MODIFIER_NUMLOCK)
modifiers |= B_NUM_LOCK;
if (_modifiers & USYNERGY_MODIFIER_SCROLLOCK)
modifiers |= B_SCROLL_LOCK;
//bool isLock = (modifiers & (B_CAPS_LOCK | B_NUM_LOCK | B_SCROLL_LOCK)) != 0;
if (!isKeyRepeat) {
if (fModifiers != modifiers) {
BMessage* message = new BMessage(B_MODIFIERS_CHANGED);
if (message == NULL)
return;
TRACE("synergy: modifiers: 0x%04" B_PRIx32 " & 0x%04" B_PRIx32 "\n",
modifiers, fModifiers);
if (isKeyDown)
{
modifiers |= fModifiers;
}
else
{
modifiers &= ~fModifiers;
}
TRACE("synergy: modifiers changed: 0x%04" B_PRIx32 " => 0x%04"
B_PRIx32 "\n", fModifiers, modifiers);
message->AddInt64("when", timestamp);
message->AddInt32("be:old_modifiers", fModifiers);
message->AddInt32("modifiers", modifiers);
message->AddData("states", B_UINT8_TYPE, states, 16);
fModifiers = modifiers;
if (EnqueueMessage(message) != B_OK)
delete message;
}
}
if (scancode == 0)
return;
BMessage* msg = new BMessage;
if (msg == NULL)
return;
char* string = NULL;
char* rawString = NULL;
int32 numBytes = 0, rawNumBytes = 0;
fKeymap.GetChars(keycode, fModifiers, 0, &string, &numBytes);
if (numBytes > 0)
msg->what = isKeyDown ? B_KEY_DOWN : B_KEY_UP;
else
msg->what = isKeyDown ? B_UNMAPPED_KEY_DOWN : B_UNMAPPED_KEY_UP;
fKeymap.GetChars(keycode, 0, 0, &rawString, &rawNumBytes);
msg->AddInt64("when", timestamp);
msg->AddInt32("key", keycode);
msg->AddInt32("modifiers", fModifiers);
msg->AddData("states", B_UINT8_TYPE, states, 16);
if (numBytes > 0) {
for (int i = 0; i < numBytes; i++) {
TRACE("%02x:", (int8)string[i]);
msg->AddInt8("byte", (int8)string[i]);
}
TRACE("\n");
msg->AddData("bytes", B_STRING_TYPE, string, numBytes + 1);
if (rawNumBytes <= 0) {
rawNumBytes = 1;
delete[] rawString;
rawString = string;
} else
delete[] string;
if (isKeyDown && isKeyRepeat) {
repeatCount++;
msg->AddInt32("be:key_repeat", repeatCount);
} else
repeatCount = 1;
} else
delete[] string;
if (rawNumBytes > 0)
msg->AddInt32("raw_char", (uint32)((uint8)rawString[0] & 0x7f));
delete[] rawString;
if (msg != NULL && EnqueueMessage(msg) != B_OK)
delete msg;
lastScanCode = isKeyDown ? scancode : 0;
}
void
uSynergyInputServerDevice::JoystickCallback(uint8_t joyNum, uint16_t buttons,
int8_t leftStickX, int8_t leftStickY,
int8_t rightStickX, int8_t rightStickY)
{
}
void
uSynergyInputServerDevice::ClipboardCallback(enum uSynergyClipboardFormat format,
const uint8_t *data, uint32_t size)
{
if (format != USYNERGY_CLIPBOARD_FORMAT_TEXT)
return;
if (be_clipboard->Lock()) {
be_clipboard->Clear();
BMessage *clip = be_clipboard->Data();
clip->AddData("text/plain", B_MIME_TYPE, data, size);
status_t result = be_clipboard->Commit();
if (result != B_OK)
TRACE("synergy: failed to commit data to clipboard\n");
be_clipboard->Unlock();
} else {
TRACE("synergy: could not lock clipboard\n");
}
}
extern "C" BInputServerDevice*
instantiate_input_device()
{
return new (std::nothrow)uSynergyInputServerDevice;
}