Skip to content

Commit de1fd8a

Browse files
committed
wip
1 parent 4c3f721 commit de1fd8a

File tree

6 files changed

+57
-29
lines changed

6 files changed

+57
-29
lines changed

linux/server/jsmn-cfg.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ int jsmn_get_bool(jsmntok_t token) {
4848
return (strcmp(str,"true") == 0) ? 1:0;
4949
}
5050

51+
int jsmn_get_int(jsmntok_t token) {
52+
char *str = em_malloc(token.end - token.start + 1);
53+
memcpy(str, &jsmn_cfg[token.start], token.end - token.start);
54+
str[token.end - token.start] = '\0';
55+
return atoi(str);
56+
}
57+
5158
int jsmn_skip(jsmntok_t *tokens, int t) {
5259
int i = 0;
5360
t++;
@@ -174,6 +181,10 @@ void jsmn_cfg_parse(char *fname, em_device **devices_p, em_mapping **mappings_p,
174181
scalar_tnum = jsmn_object_key_value(tokens, mapping_tnum, "release_pressed", JSMN_PRIMITIVE);
175182
if (scalar_tnum > 0) mapping->release_pressed = jsmn_get_bool(tokens[scalar_tnum]);
176183

184+
scalar_tnum = jsmn_object_key_value(tokens, mapping_tnum, "always_client", JSMN_PRIMITIVE);
185+
if (scalar_tnum > 0)
186+
mapping->always_client = jsmn_get_int(tokens[scalar_tnum]);
187+
177188
int combo_tnum = jsmn_object_key_value(tokens, mapping_tnum, "combo", JSMN_ARRAY);
178189
if (combo_tnum < 0) em_fatal("Config: 'combo' is mandatory.");
179190
for (int event_num = 0; (event_num < tokens[combo_tnum].size && event_num < EM_MAX_COMBO); event_num++) {

linux/server/octopus-devices

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ do
2121
echo " vendor_id: $V_ID"
2222
echo " product_id: $P_ID"
2323
echo "capabilities:$CAPS"
24+
echo " device: /dev/input/$DEV"
2425
echo
2526
done

linux/server/octopus-server

-39.3 KB
Binary file not shown.

linux/server/octopus-server.c

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ const char * em_event_code_get_name(unsigned int code) {
5858
return libevdev_event_code_get_name(EV_KEY, code);
5959
}
6060

61+
em_client *em_client_by_idx(em_client *item, int idx) {
62+
while (item) {
63+
if (item->idx == idx) return item;
64+
item = item->next;
65+
}
66+
return NULL;
67+
}
68+
6169
void em_grab_devices(em_device *devices) {
6270
struct dirent **event_dev_list;
6371

@@ -253,18 +261,18 @@ int main(int argc, char* argv[]) {
253261
// Currently active client
254262
em_client *active_client = clients;
255263

256-
void send_remote_event(uint16_t type, uint16_t code, int32_t value) {
264+
void send_remote_event(em_client *client, uint16_t type, uint16_t code, int32_t value) {
257265
struct em_packet packet;
258-
packet.clientIdx = (uint8_t) active_client->idx;
266+
packet.clientIdx = (uint8_t) client->idx;
259267
packet.type = type;
260268
packet.code = code;
261269
packet.value = value;
262270
if (sendto(sock, &packet, sizeof(em_packet), 0, (struct sockaddr *)&addr, sizeof(addr)) < 0)
263271
em_fatal("Sending UDP packet failed.");
264272
}
265273

266-
void send_event(em_device *dev, uint16_t type, uint16_t code, int32_t value) {
267-
if (active_client->local) {
274+
void send_event(em_client *client, em_device *dev, uint16_t type, uint16_t code, int32_t value) {
275+
if (client->local) {
268276
if (dev) {
269277
int rc = libevdev_uinput_write_event(dev->uidev, type, code, value);
270278
if (rc != 0) {
@@ -277,12 +285,12 @@ int main(int argc, char* argv[]) {
277285
if (rc != 0) em_fatal("Sending event failed with rc %d on uinput device.", rc);
278286
}
279287
}
280-
else send_remote_event(type, code, value);
288+
else send_remote_event(client, type, code, value);
281289
}
282290

283-
void release_pressed() {
291+
void release_pressed(em_client *client) {
284292
// Send release events for pressed keys on all devices
285-
if (active_client->local) {
293+
if (client->local) {
286294
em_device *dev = devices;
287295
while (dev) {
288296
for (int k = 0; k < EM_MAX_COMBO; k++) {
@@ -296,7 +304,7 @@ int main(int argc, char* argv[]) {
296304
else {
297305
for (int k = 0; k < EM_MAX_COMBO; k++) {
298306
if (active_keys[k])
299-
send_remote_event(EV_KEY, active_keys[k], 0);
307+
send_remote_event(client, EV_KEY, active_keys[k], 0);
300308
}
301309
}
302310
for (int k = 0; k < EM_MAX_COMBO; k++) { active_keys[k] = 0; };
@@ -374,22 +382,23 @@ int main(int argc, char* argv[]) {
374382

375383
int check_combos = 0;
376384
int filter = 0;
385+
struct input_event aie = ie;
377386

378387
if (ie.type == EV_REL && ie.code == REL_WHEEL) {
379-
if (ie.value > 0) { ie.type = EV_KEY; ie.code = 0x400; ie.value = 1; };
380-
if (ie.value < 0) { ie.type = EV_KEY; ie.code = 0x402; ie.value = 1; };
388+
if (ie.value > 0) { aie.type = EV_KEY; aie.code = 0x400; aie.value = 1; };
389+
if (ie.value < 0) { aie.type = EV_KEY; aie.code = 0x402; aie.value = 1; };
381390
}
382391
if (ie.type == EV_REL && ie.code == REL_HWHEEL) {
383-
if (ie.value > 0) { ie.type = EV_KEY; ie.code = 0x401; ie.value = 1; };
384-
if (ie.value < 0) { ie.type = EV_KEY; ie.code = 0x403; ie.value = 1; };
392+
if (ie.value > 0) { aie.type = EV_KEY; aie.code = 0x401; aie.value = 1; };
393+
if (ie.value < 0) { aie.type = EV_KEY; aie.code = 0x403; aie.value = 1; };
385394
}
386395

387-
if (ie.type == EV_KEY && ie.value < 2) {
388-
if (ie.value) {
396+
if (aie.type == EV_KEY && aie.value < 2) {
397+
if (aie.value) {
389398
// Key pressed
390399
for (int k = 0; k < EM_MAX_COMBO; k++) {
391-
if (!active_keys[k] || active_keys[k] == ie.code) {
392-
active_keys[k] = ie.code;
400+
if (!active_keys[k] || active_keys[k] == aie.code) {
401+
active_keys[k] = aie.code;
393402
check_combos = 1;
394403
break;
395404
};
@@ -398,7 +407,7 @@ int main(int argc, char* argv[]) {
398407
else {
399408
// Key released
400409
for (int k = 0; k < EM_MAX_COMBO; k++) {
401-
if (active_keys[k] == ie.code) active_keys[k] = 0;
410+
if (active_keys[k] == aie.code) active_keys[k] = 0;
402411
}
403412
}
404413
}
@@ -455,7 +464,7 @@ int main(int argc, char* argv[]) {
455464

456465
// Forward event to output if we don't want it filtered
457466
if (!filter) {
458-
send_event(dev, ie.type, ie.code, ie.value);
467+
send_event(active_client, dev, ie.type, ie.code, ie.value);
459468
}
460469
}
461470

@@ -472,28 +481,35 @@ int main(int argc, char* argv[]) {
472481
em_mapping *mapping = mappings;
473482
while (mapping) {
474483
if (mapping->send_output) {
475-
if (mapping->release_pressed) release_pressed();
484+
em_client *which_client = active_client;
485+
486+
if (mapping->always_client) {
487+
em_client *c = em_client_by_idx(clients, mapping->always_client - 1);
488+
if (c) which_client = c;
489+
}
490+
491+
if (mapping->release_pressed) release_pressed(which_client);
476492
for (int k = 0; k < EM_MAX_OUTPUT_EVENTS; k++) {
477493
if (!mapping->output[k]) break;
478494
if (mapping->output[k]->code >= 0x400) {
479495
switch (mapping->output[k]->code) {
480496
case 0x400:
481-
send_event(NULL, EV_REL, REL_WHEEL, 1);
497+
send_event(which_client, NULL, EV_REL, REL_WHEEL, 1);
482498
break;
483499
case 0x401:
484-
send_event(NULL, EV_REL, REL_HWHEEL, 1);
500+
send_event(which_client, NULL, EV_REL, REL_HWHEEL, 1);
485501
break;
486502
case 0x402:
487-
send_event(NULL, EV_REL, REL_WHEEL, -1);
503+
send_event(which_client, NULL, EV_REL, REL_WHEEL, -1);
488504
break;
489505
case 0x403:
490-
send_event(NULL, EV_REL, REL_HWHEEL, -1);
506+
send_event(which_client, NULL, EV_REL, REL_HWHEEL, -1);
491507
break;
492508
}
493509
}
494-
else send_event(NULL, mapping->output[k]->type, mapping->output[k]->code, mapping->output[k]->value);
510+
else send_event(which_client, NULL, mapping->output[k]->type, mapping->output[k]->code, mapping->output[k]->value);
495511
}
496-
send_event(NULL, EV_SYN, SYN_REPORT, 0);
512+
send_event(which_client, NULL, EV_SYN, SYN_REPORT, 0);
497513
mapping->send_output = 0;
498514
}
499515

@@ -503,7 +519,7 @@ int main(int argc, char* argv[]) {
503519
// Switch clients, if requested
504520
if (switch_client) {
505521
if (switch_client != active_client) {
506-
release_pressed();
522+
release_pressed(active_client);
507523
printf("Switching to client #%u\n", switch_client->idx);
508524
active_client = switch_client;
509525
}

linux/server/octopus-server.cfg

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
"combo" : [ "BTN_EXTRA" ],
1818
"output" : [ "+KEY_VOLUMEUP", "-KEY_VOLUMEUP" ],
1919
"filter_last": true,
20-
"only_client": 1
20+
"always_client": 1
2121
},
2222
{
2323
"combo" : [ "BTN_SIDE" ],
2424
"output" : [ "+KEY_VOLUMEDOWN", "-KEY_VOLUMEDOWN" ],
2525
"filter_last": true,
26-
"only_client": 1
26+
"always_client": 1
2727
}
2828
],
2929
"clients": [
@@ -36,4 +36,3 @@
3636
}
3737
]
3838
}
39-

linux/server/octopus-server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ typedef struct em_mapping_type {
3434
struct input_event *output[EM_MAX_OUTPUT_EVENTS];
3535
int filter_last;
3636
int release_pressed;
37+
int always_client;
3738

3839
int send_output;
3940

0 commit comments

Comments
 (0)