-
Notifications
You must be signed in to change notification settings - Fork 0
/
irtoy_tool.c
2041 lines (1853 loc) · 49.6 KB
/
irtoy_tool.c
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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* IRToy controller
* Connections:
* - IR port (receive/transmit)
* - MythRemote port
* - Command port
*/
#if __linux__
#define USE_UINPUT
#endif
/*
* TODO: More efficient matching?
* TODO: sort packets to find the best packet to transmit?
* TODO: Move gap/jitter to IRState.
* TODO: Don't reconstruct the fd_set every select().
* TODO: better recording of samples? Because as it stands... this is gross.
*
* Oct 2016 to-do
* - command to set 'UNKNOWN' key name in output
* - refactor analysis to take one file, and clean it out.
* - globs in device names
* - decode packets to binary?
* - use osascript in interactive mode with a pipe to cut out start overheads.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdbool.h>
#include <termios.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <time.h>
#ifdef USE_UINPUT
#include <linux/uinput.h>
#endif
#include "dict.h"
#include "irtoy.h"
#include "error.h"
#include "keywords.h"
#include "mac_actions.h"
#include "server.h"
int ir_packet_timeout = 100000;
int ir_debounce_time = 250000; /* initial repeat delay */
int ir_repeat_delay = 0; /* current repeat delay */
/* ------------------------------------------------------------
* Testing stuff
*/
typedef struct PacketFile PacketFile;
struct PacketFile
{
char *fname;
int n_packets;
IRPacket **packets;
};
PacketFile *
read_packets (FILE * in)
{
PacketFile *pf;
int n_packets_allocated = 1;
pf = malloc (sizeof *pf);
pf->packets = malloc (sizeof *pf->packets);
pf->n_packets = 0;
while (!feof (in))
{
IRPacket *k;
k = irpacket_scanf (in);
if (!k)
break;
if (pf->n_packets + 1 >= n_packets_allocated)
{
n_packets_allocated *= 2;
pf->packets = realloc (pf->packets,
n_packets_allocated * sizeof *pf->packets);
}
pf->packets[pf->n_packets++] = k;
}
pf->packets[pf->n_packets] = NULL;
return pf;
}
void
analyse_packet_files (int n, char **name)
{
int i, x, y, j;
const int jitter_max = 100;
int max_useful_jitter = 0;
FILE *in;
PacketFile **files = malloc (n * sizeof *files);
int n_packets = 0;
IRPacket **packets;
const char **names;
int *m;
int best_jitter;
int best_jitter_score;
bool *keep;
bool changed;
int *scores;
int *kept_packets;
for (i = 0; i < n; i++)
{
char *c;
in = fopen (name[i], "r");
if (!in)
fatal (0, "Can't open input file '%s'", name[i]);
files[i] = read_packets (in);
files[i]->fname = strdup(name[i]);
for (c = files[i]->fname; *c; c++)
if (*c == '/')
*c = '_';
printf ("Read %d packets from '%s'\n", files[i]->n_packets,
files[i]->fname);
n_packets += files[i]->n_packets;
}
/* Build the big list */
packets = calloc (n_packets + 1, sizeof *packets);
names = calloc (n_packets + 1, sizeof *names);
n_packets = 0;
for (i = 0; i < n; i++)
{
int j;
for (j = 0; j < files[i]->n_packets; j++)
{
packets[n_packets] = files[i]->packets[j];
names[n_packets] = files[i]->fname;
n_packets++;
}
}
printf ("All packets\n");
for (i = 0; i < n_packets; i++)
{
printf ("%s ", names[i]);
irpacket_render (stdout, packets[i]);
printf ("\n");
}
printf ("%d packets\n", n_packets);
m = calloc (n_packets * n_packets, sizeof *m);
printf ("Pulse width distribution\n");
{
int max_pulse_width = 0;
int max_count = 0;
int *counts;
for (i = 0; i < n_packets; i++)
for (j = 0; j < packets[i]->n_pulses; j++)
if (max_pulse_width < packets[i]->pulses[j].width)
max_pulse_width = packets[i]->pulses[j].width;
printf ("Max pulse width is %d\n", max_pulse_width);
counts = calloc (max_pulse_width + 1, sizeof *counts);
for (i = 0; i < n_packets; i++)
for (j = 0; j < packets[i]->n_pulses; j++)
{
int c = ++counts[packets[i]->pulses[j].width];
if (c > max_count)
max_count = c;
}
for (i = 0; i < max_pulse_width; i++)
{
printf ("%5d %-6d", i, counts[i]);
for (j = 0; j < (79 - 12) * counts[i] / max_count; j++)
printf ("*");
printf ("\n");
}
free (counts);
}
/* Jitter Matrix.
This holds, for each combination of X and Y, minimum jitter
necessary to allow them to match, so is a measure of the
proximity of the packets in the structural space.
*/
printf ("Computing jitter matrix\n");
for (y = 0; y < n_packets; y++)
for (x = y; x < n_packets; x++)
{
int i;
for (i = 0; i < jitter_max; i++)
{
if (irpacket_match (packets[x], packets[y], i))
break;
}
m[x + y * n_packets] = i;
m[y + x * n_packets] = i;
if (i > max_useful_jitter && i != jitter_max)
max_useful_jitter = i;
}
printf ("Jitter matrix:\n");
for (y = 0; y < n_packets; y++)
{
for (x = 0; x < n_packets; x++)
printf ("%4d", m[x + y * n_packets]);
printf ("\n");
}
best_jitter = jitter_max;
for (j = 0; j <= max_useful_jitter; j++)
{
int mismatches = 0, /* false mismatches */
ambiguous = 0; /* false matches */
for (y = 0; y < n_packets; y++)
for (x = y; x < n_packets; x++)
{
bool match = m[x + y * n_packets] <= j;
bool same = (names[x] == names[y]);
if (match && !same)
{
if (j == 0)
{
printf ("Warning: entirely ambiguous data:\n");
printf ("%s ", names[x]);
irpacket_printf (stdout, packets[x]);
printf (" matches\n%s ", names[y]);
irpacket_printf (stdout, packets[x]);
printf ("\n");
}
ambiguous++;
}
else if (!match && same)
mismatches++;
}
printf ("At jitter=%d, false mismatches=%d, ambiguous=%d\n",
j, mismatches, ambiguous);
if (j == 0 || ambiguous + mismatches < best_jitter_score)
{
best_jitter_score = ambiguous + mismatches;
best_jitter = j;
}
if (mismatches == 0)
{
printf ("No mismatches, abandoning search\n");
break;
}
}
printf ("Best jitter is %d with a badness of %d\n",
best_jitter, best_jitter_score);
printf ("Optimising key codes\n");
keep = calloc (n_packets, sizeof *keep);
for (x = 0; x < n_packets; x++)
keep[x] = true;
scores = calloc (n_packets, sizeof *scores);
kept_packets = calloc (n_packets, sizeof *kept_packets);
do
{
int best_packet;
int best_packet_score;
int n_kept_packets;
changed = false;
for (i = 0; i < n; i++)
{
best_packet_score = -1;
for (x = 0; x < n_packets; x++)
{
int x_score = 0;
if (!keep[x])
continue;
if (names[x] != files[i]->fname)
continue;
for (y = 0; y < n_packets; y++)
{
bool match, same;
if (x == y)
continue;
match = m[x + y * n_packets] <= best_jitter;
same = names[x] == names[y];
if (match && same && keep[y])
/* In this round, only score it if it helps us
narrow the field */
x_score++;
else if (match && !same)
x_score -= 1; /* penalise false + */
else if (!match && same)
; /* Probably two distinct patterns
from one key. Don't penalise. */
else if (!match && !same)
x_score++;
}
printf ("%s ", names[x]);
irpacket_printf (stdout, packets[x]);
printf (" score=%d\n", x_score);
scores[x] = x_score;
if (x_score > best_packet_score || best_packet_score < 0)
{
best_packet_score = x_score;
best_packet = x;
}
}
printf ("Best packet is: p%d %s ", best_packet, names[best_packet]);
irpacket_printf (stdout, packets[best_packet]);
printf (" with score %d\n", best_packet_score);
n_kept_packets = 1;
kept_packets[0] = best_packet;
/* Trim redundant packets */
for (x = 0; x < n_packets; x++)
{
int k;
if (!keep[x] || x == best_packet
|| names[x] != names[best_packet])
continue;
/* Can we find a kept packet that makes this packet X
unnecessary? */
printf ("Do we need packet %d?\n", x);
for (k = 0; k < n_kept_packets; k++)
if (m[kept_packets[k] + x * n_packets] <= best_jitter)
{
keep[x] = false;
changed = true;
printf ("Don't need to keep p%d because of packet p%d", x, kept_packets[k]);
irpacket_printf (stdout, packets[x]);
printf ("\n");
break;
}
if (keep[x])
{
kept_packets[n_kept_packets++] = x;
}
}
printf ("Kept %d packets for '%s'\n", n_kept_packets, names[i]);
}
}
while (changed);
/* Don't keep duplicates. Only do this at this stage since in the
above code, the duplicates serve to weight the scores. */
for (x = 0; x < n_packets; x++)
if (keep[x] && scores[x] > 0)
for (y = x + 1; y < n_packets; y++)
if (irpacket_match (packets[x], packets[y], 0))
keep[y] = false;
/* Dump out 'canonical' packets */
{
int kept = 0;
printf ("# Good packet list:\n");
for (x = 0; x < n_packets; x++)
{
if (keep[x] && scores[x] > 0)
{
kept++;
printf ("keycode %s ", names[x]);
irpacket_printf (stdout, packets[x]);
printf (" # p%d score %d\n", x, scores[x]);
}
}
printf ("# kept %d of %d packets\n", kept, n_packets);
}
}
unsigned char test_chars[] = {
0, 5, /* 1 */
0, 5, /* 0 */
0, 10, /* 1 */
0, 100, /* 0 */
0, 1,
0, 2,
0, 3,
0, 40,
255, 255,
255, 255,
255, 255,
0, 1,
0, 2,
0, 3,
0, 40
};
void
log_packet (IRPacket * k)
{
static int counter = 1;
static IRDict *d;
const char *name;
if (!d)
d = new_irdict ();
name = irdict_lookup_packet (d, k);
if (name)
fprintf (stdout, "(Duplicate packet '%s')\n", name);
else
{
char buffer[BUFSIZ];
sprintf (buffer, "packet_%d", counter++);
fprintf (stdout, "(New unique packet '%s')\n", buffer);
irdict_insert (d, strdup (buffer), k);
}
}
int
test_main (int argc, char *argv[])
{
IRState *ir = new_irstate ();
IRPacket *k;
IRPacket *out_packets[1];
int n, i;
n = irstate_rxbytes (ir, sizeof (test_chars), test_chars, out_packets);
fprintf (stdout, "Test sequence has %d packets:\n", n);
for (i = 0; i < n; i++)
{
irpacket_printf (stdout, out_packets[i]);
fprintf (stdout, "\n");
irpacket_render (stdout, out_packets[i]);
fprintf (stdout, "\n");
log_packet (out_packets[i]);
}
while (!feof (stdin))
{
fprintf (stdout, "Gimme a packet: ");
k = irpacket_scanf (stdin);
if (k)
{
fprintf (stdout, "Got packet:\n");
irpacket_printf (stdout, k);
fprintf (stdout, "\n");
log_packet (k);
}
else
{
fprintf (stdout, "Couldn't get packet\n");
}
}
return 0;
}
/* ------------------------------------------------------------
* Server/Connection management
* XXX TODO: handle timeouts better. Set time?
*/
typedef struct IRConnectionInfo IRConnectionInfo;
typedef struct IRServerInfo IRServerInfo;
struct IRConnectionInfo {
IRServerInfo *si;
char *buffer;
char *end;
};
struct IRServerInfo {
Server *server;
Dict *keymaps; /* name -> Keymap* */
Keymap *current_keymap;
IRDict *buttondict;
IRState *ir;
Connection *mythremote;
Connection *irdev;
Connection *vlc;
Connection *uinput;
FILE *out_file;
/* Key debouncing */
const char *last_button;
struct timeval last_button_time;
struct timeval next_repeat_time; /* earliest time of next repeat button */
bool verbose;
char *unknown_key;
};
bool handle_button (IRServerInfo *si, const char *button);
IRPacket *transmit_button (IRServerInfo *si, const char *button);
bool mythremote_command (IRServerInfo *si, const char *command);
bool vlc_command (IRServerInfo *si, const char *command);
bool send_myth_command (IRServerInfo *si, const char *command);
bool send_keypress (IRServerInfo *si, int key);
bool send_key (IRServerInfo *si, int key, int value);
IRServerInfo *
new_irserverinfo (void)
{
IRServerInfo *si = malloc (sizeof *si);
/* IR specific stuff */
si->buttondict = NULL;
si->keymaps = dict_new (NULL);
si->current_keymap = NULL;
si->ir = NULL;
si->mythremote = NULL;
si->out_file = NULL;
si->last_button = NULL;
si->uinput = NULL;
si->verbose = false;
si->unknown_key = NULL;
return si;
}
IRConnectionInfo *new_irconnectioninfo (IRServerInfo *si) {
IRConnectionInfo *ci = malloc (sizeof *ci);
ci->si = si;
ci->buffer = malloc (sizeof (char) * BUFSIZ);
ci->end = ci->buffer;
return ci;
}
Connection *
new_cmdconnection (IRServerInfo *si, int fd, IRConnectionInfo *ci)
{
Connection *n;
char buffer[BUFSIZ];
sprintf (buffer, "cmd connection %d", fd);
n = new_connection (si->server, fd, buffer, ci);
return n;
}
/* ------------------------------------------------------------
* Command server
*/
void
can_read_command (Connection * n, void *h)
{
/* Dynamic buffer for this transfer */
char buffer[BUFSIZ];
int fd = connection_fd (n);
int count = read (fd, buffer, BUFSIZ);
int i;
IRConnectionInfo *ci = (IRConnectionInfo *)h;
for (i = 0; i < count; i++)
{
if (buffer[i] != '\n' && buffer[i] != '\r')
{
if (ci->end - ci->buffer < BUFSIZ - 1)
{
*ci->end++ = buffer[i];
}
else
{
char str[] = "Error: command buffer overrun\n\n";
write (fd, str, sizeof (str) - 1);
count = 0; /* => close connection */
break;
}
}
else if (ci->buffer != ci->end)
{
IRPacket *k;
*ci->end++ = '\0';
/* ">symname" to transmit 'symname' */
if (ci->buffer[0] == '>')
{
k = transmit_button (ci->si, ci->buffer+1);
if (k)
{
write (fd, "ok\n", 3);
if (ci->si->verbose)
{
fprintf (stdout, "Command '%s' gets packet: ", ci->buffer);
irpacket_printf (stdout, k);
fprintf (stdout, "\n");
irpacket_render (stdout, k);
fprintf (stdout, "\n");
}
}
else
{
char b2[BUFSIZ];
sprintf (b2, "Unknown button '%s'\n", ci->buffer);
write (fd, b2, strlen (b2));
}
}
/* "=symname" to set the symbol for unknown packets to 'symname' */
else if (ci->buffer[0] == '=')
{
if (ci->si->verbose)
fprintf (stdout, "Setting UNKWOWN key to '%s'\n", &ci->buffer[1]);
if (ci->si->unknown_key)
free (ci->si->unknown_key);
ci->si->unknown_key = strdup (&ci->buffer[1]);
}
else
{
if (ci->si->verbose)
fprintf (stdout, "Command port gets '%s'\n", ci->buffer);
if (handle_button(ci->si, ci->buffer))
write (fd, "ok\n", 3);
}
ci->end = ci->buffer;
}
}
if (count == 0)
{
free (ci->buffer);
close (fd);
connection_remove (n);
}
}
void
can_read_cmdport (Connection * n, void *h)
{
int nfd = accept (connection_fd (n), NULL, NULL);
IRServerInfo *si = (IRServerInfo *)h;
Connection *n2;
/* Set non-blocking */
fcntl (nfd, F_SETFL, fcntl (nfd, F_GETFL) | O_NONBLOCK);
n2 = new_cmdconnection (si, nfd,
new_irconnectioninfo (si));
connection_set_can_read(n2, can_read_command);
}
/* ------------------------------------------------------------
* Keymaps and Actions
*/
typedef enum ActionID {
action_keypress, action_multitap, action_mythtv, action_transmit,
action_set_keymap, action_vlc, action_applescript, action_key_action
} ActionID;
struct Action
{
ActionID id;
const char *operand;
Action *next;
};
Action *
new_action (ActionID id, const char *operand) {
Action *a = malloc (sizeof *a);
a->id = id;
a->operand = operand;
a->next = NULL;
return a;
}
typedef struct InheritedKeymap InheritedKeymap;
struct InheritedKeymap {
const char *mapname;
Keymap *map;
InheritedKeymap *next;
};
struct Keymap {
const char *name;
InheritedKeymap *inherit;
Dict *mapping; /* char* -> Action* */
};
Keymap *
new_keymap (const char *name) {
Keymap *km = malloc (sizeof *km);
km->name = strdup (name);
km->mapping = dict_new (NULL);
km->inherit = NULL;
return km;
}
void
keymap_add_action (Keymap *m, const char *k, Action *a) {
dict_set (m->mapping, k, a);
}
/* ------------------------------------------------------------
* Options and Config File
*/
typedef struct ServerOpts ServerOpts;
struct ServerOpts
{
char *config_file;
char *irdev;
int cmdport;
char *frontend_host;
int frontend_port;
char *vlc_host;
int vlc_port;
char *out_file;
bool verbose;
char *uinput_dev;
char *buttondict_fname;
bool daemon;
};
/* Config file format is:
* file ::= entry*
* entry ::= '#' comment
* | "irdev" string
* | "frontend" string
* | "frontend_port" integer
* | "keycode" string packet
* | "cmdport" integer
* | "include" string
* | "out_file" string
* XXX out of date....
* packet ::= " { " integer* " } "
*/
char *
read_string (FILE * in)
{
char buffer[BUFSIZ], *b = buffer;
char c;
char end = ' ';
if (feof (in))
return NULL;
c = fgetc (in);
while (isspace (c))
c = fgetc (in);
if (c == '#')
{
/* Comment until end of line. */
for (;;)
{
c = fgetc (in);
if (c == '\n' || feof (in))
/* Now read the next string. */
return read_string (in);
}
}
if (c == '\"' || c == '\'')
end = c;
else
{
/* c is first char of string */
end = ' ';
*b++ = c;
}
while (!feof (in))
{
c = fgetc (in);
if (c == end || (end == ' ' && isspace (c)))
break;
*b++ = c;
}
*b++ = '\0';
return strdup (buffer);
}
int
read_integer (FILE * in)
{
char *str = read_string (in);
int rv;
if (!str)
return 0;
rv = atoi (str);
free (str);
return rv;
}
/* Button dictionary IO */
void
read_buttondict (ServerOpts *opts, IRServerInfo *si, const char *file)
{
FILE *in;
if (opts->verbose)
fprintf (stdout, "Reading button dictionary file '%s'\n", file);
in = fopen(file, "r");
if (!in)
fatal(0, "Can't read button dictionary file '%s'\n", file);
while (!feof (in))
{
char buffer[BUFSIZ];
if (!fscanf (in, "%s", buffer) || feof (in))
break;
if (!strcmp (buffer, "keycode")) {
IRPacket *k;
char *id;
id = read_string (in);
k = irpacket_scanf (in);
irdict_insert (si->buttondict, id, k);
} else if (buffer[0] == '#') {
char c;
for (;;)
{
c = fgetc (in);
if (c == '\n' || feof (in))
break;
}
} else {
fatal(0, "Unknown entry '%s' in button dictionary '%s'\n",
buffer, file);
}
}
fclose (in);
}
void write_buttondict (ServerOpts *opts, IRServerInfo *si, const char *file)
{
FILE *out;
IRSymbol *s;
if (opts->verbose)
fprintf (stdout, "Writting button dictionary to '%s'\n", file);
out = fopen (file, "w");
if (!out)
fatal(0, "Can't write button dictionary file '%s'\n", file);
for (s = si->buttondict->first; s; s = s->next)
{
fprintf (out, "keycode %s ", s->name);
irpacket_printf (out, s->packet);
fprintf (out, "\n");
}
fclose (out);
}
/* Action IO */
Action *
read_action (FILE *in)
{
char *id = read_string (in);
switch (decode_keyword(id)) {
case k_keypress:
return new_action(action_keypress, read_string (in));
case k_multitap:
return new_action(action_multitap, read_string (in));
case k_transmit:
return new_action(action_transmit, read_string (in));
case k_set_keymap:
return new_action(action_set_keymap, read_string (in));
case k_vlc:
return new_action(action_vlc, read_string (in));
case k_key_action:
return new_action(action_key_action, read_string (in));
case k_begin: {
/* Read an action sequence */
Action *a, *last_a = NULL, *first_a = NULL;
while (!feof (in)) {
a = read_action (in);
if (!first_a)
first_a = a;
if (last_a)
last_a->next = a;
if (!a)
break;
last_a = a;
}
return first_a;
}
case k_end:
return NULL;
case k_applescript:
return new_action (action_applescript, read_string (in));
default:
fatal (0, "Unknown action '%s'\n", id);
}
return NULL;
}
/*
keymap "vlc"
key "up" keypress up
key "down" keypress down
end
*/
Keymap *
read_keymap (FILE *in)
{
char *id = read_string (in);
Keymap *km = new_keymap (id);
char *key_id;
Action *action;
free (id);
while (!feof (in))
{
id = read_string (in);
switch (decode_keyword (id))
{
case k_key:
free (id);
key_id = read_string (in);
action = read_action (in);
keymap_add_action (km, key_id, action);
free (key_id);
break;
case k_end:
/* Finish */
free (id);
return km;
break;
case k_inherit:
free (id);
fprintf (stderr, "Adding inherit in keymap '%s'\n", km->name);
InheritedKeymap **tail = &(km->inherit);
while (*tail) {
fprintf (stderr, " skip past map '%s'\n", (*tail)->mapname);
tail = &( (*tail)->next );
}
*tail = malloc (sizeof **tail);
(*tail)->mapname = read_string (in);
(*tail)->next = NULL;
(*tail)->map = NULL;
break;
default:
fatal (1, "Unknown keyword '%s' in keymap '%s'", id, km->name);
return NULL;
}
}
return NULL;
}
/* Config file IO */
void
read_config (ServerOpts *opts, IRServerInfo *si, const char *file)
{
FILE *in;
if (opts->verbose)
fprintf (stdout, "Reading config file '%s'\n", file);
in = fopen (file, "r");
if (!in)
fatal (0, "Can't read config file '%s'", file);
while (!feof (in))
{
char buffer[BUFSIZ];
if (!fscanf (in, "%s", buffer) || feof (in))
break;
switch (decode_keyword(buffer))
{
case k_keycode:
{
IRPacket *k;
char *id;
id = read_string (in);
k = irpacket_scanf (in);
irdict_insert (si->buttondict, id, k);
break;
}
case k_irdev:
opts->irdev = read_string (in);
break;
case k_frontend:
opts->frontend_host = read_string (in);
break;
case k_frontend_port:
opts->frontend_port = read_integer (in);
break;
case k_cmdport:
opts->cmdport = read_integer (in);
break;
case k_include:
{
char *f = read_string (in);
read_config (opts, si, f);
free (f);
break;
}
case k_jitter:
irtoy_jitter = read_integer (in);
break;
case k_gap:
irtoy_gap = read_integer (in);
break;
case k_packet_timeout:
ir_packet_timeout = read_integer (in);
break;
case k_debounce_time:
ir_debounce_time = read_integer (in);
break;
case k_out_file:
opts->out_file = read_string (in);
break;
case k_vlc_host:
opts->vlc_host = read_string (in);
break;