summaryrefslogtreecommitdiffstats
path: root/x11vnc/v4l.c
blob: 516f5321b922f31287134a9a2299f570a8975407 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
/*
   Copyright (C) 2002-2009 Karl J. Runge <runge@karlrunge.com> 
   All rights reserved.

This file is part of x11vnc.

x11vnc is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

x11vnc is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with x11vnc; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA
or see <http://www.gnu.org/licenses/>.

In addition, as a special exception, Karl J. Runge
gives permission to link the code of its release of x11vnc with the
OpenSSL project's "OpenSSL" library (or with modified versions of it
that use the same license as the "OpenSSL" library), and distribute
the linked executables.  You must obey the GNU General Public License
in all respects for all of the code used other than "OpenSSL".  If you
modify this file, you may extend this exception to your version of the
file, but you are not obligated to do so.  If you do not wish to do
so, delete this exception statement from your version.
*/

/* -- v4l.c -- */

#include "x11vnc.h"
#include "cleanup.h"
#include "scan.h"
#include "xinerama.h"
#include "screen.h"
#include "connections.h"
#include "keyboard.h"
#include "allowed_input_t.h"

#if LIBVNCSERVER_HAVE_LINUX_VIDEODEV_H
#if LIBVNCSERVER_HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#define CONFIG_VIDEO_V4L1_COMPAT
#include <linux/videodev.h>
#define V4L_OK
#endif
#endif

char *v4l_guess(char *str, int *fd);
void v4l_key_command(rfbBool down, rfbKeySym keysym, rfbClientPtr client);
void v4l_pointer_command(int mask, int x, int y, rfbClientPtr client);

static int v4l1_val(int pct);
static int v4l1_width(int w);
static int v4l1_height(int h);
static int v4l1_resize(int fd, int w, int h);
static void v4l1_setfreq(int fd, unsigned long freq, int verb);
static void v4l1_set_input(int fd, int which);
static int v4l1_setfmt(int fd, char *fmt);
static void apply_settings(char *dev, char *settings, int *fd);
static int v4l1_dpct(int old, int d);
static void v4l_requery(void);
static void v4l_br(int b);
static void v4l_hu(int b);
static void v4l_co(int b);
static void v4l_cn(int b);
static void v4l_sz(int b);
static void v4l_sta(int sta);
static void v4l_inp(int inp);
static void v4l_fmt(char *fmt);
static int colon_n(char *line);
static char *colon_str(char *line);
static char *colon_tag(char *line);
static void lookup_rgb(char *g_fmt, int *g_b, int *mask_rev);
static char *v4l1_lu_palette(unsigned short palette);
static unsigned short v4l1_lu_palette_str(char *name, int *bits, int *rev);
static char *v4l2_lu_palette(unsigned int palette);
static unsigned int v4l2_lu_palette_str(char *name, int *bits, int *rev);
static int v4l1_query(int fd, int verbose);
static int v4l2_query(int fd, int verbose);
static int open_dev(char *dev);
static char *guess_via_v4l(char *dev, int *fd);
static char *guess_via_v4l_info(char *dev, int *fd);
static void parse_str(char *str, char **dev, char **settings, char **atparms);
static unsigned long lookup_freqtab(int sta);
static unsigned long lookup_freq(int sta);
static int lookup_station(unsigned long freq);
static void init_freqtab(char *file);
static void init_freqs(void);
static void init_ntsc_cable(void);

#define C_VIDEO_CAPTURE 1
#define C_PICTURE       2
#define C_WINDOW        3 

#ifdef V4L_OK
static struct video_capability  v4l1_capability;
static struct video_channel     v4l1_channel;
static struct video_tuner       v4l1_tuner;
static struct video_picture     v4l1_picture;
static struct video_window      v4l1_window;

#if HAVE_V4L2
static struct v4l2_capability   v4l2_capability;
static struct v4l2_input        v4l2_input;
static struct v4l2_tuner        v4l2_tuner;
static struct v4l2_fmtdesc      v4l2_fmtdesc;
static struct v4l2_format       v4l2_format;
/*static struct v4l2_framebuffer  v4l2_fbuf; */
/*static struct v4l2_queryctrl    v4l2_qctrl; */
#endif
#endif

static int v4l1_cap = -1;
static int v4l2_cap = -1;
#define V4L1_MAX 65535

#define CHANNEL_MAX 500
static unsigned long ntsc_cable[CHANNEL_MAX];
static unsigned long custom_freq[CHANNEL_MAX];

static unsigned long last_freq = 0;
static int last_channel = 0;

static int v4l1_val(int pct) {
	/* pct is % */
	int val, max = V4L1_MAX; 
	if (pct < 0) {
		return 0;
	} else if (pct > 100) {
		return max;
	}
	val = (pct * max)/100;

	return val;
}
static int v4l1_width(int w) {
#ifdef V4L_OK
	int min = v4l1_capability.minwidth;
	int max = v4l1_capability.maxwidth;
	if (w < min) {
		w = min;
	}
	if (w > max) {
		w = max;
	}
#endif
	return w;
}
static int v4l1_height(int h) {
#ifdef V4L_OK
	int min = v4l1_capability.minheight;
	int max = v4l1_capability.maxheight;
	if (h < min) {
		h = min;
	}
	if (h > max) {
		h = max;
	}
#endif
	return h;
}

static int v4l1_resize(int fd, int w, int h) {
#ifdef V4L_OK
	int dowin = 0;

	memset(&v4l1_window, 0, sizeof(v4l1_window));
	if (ioctl(fd, VIDIOCGWIN, &v4l1_window) == -1) {
		return 0;
	}

	if (w > 0) w = v4l1_width(w);

	if (w > 0 && w != (int) v4l1_window.width) {
		dowin = 1;
	}

	if (h > 0) h = v4l1_height(h);

	if (h > 0 && h != (int) v4l1_window.height) {
		dowin = 1;
	}

	if (dowin) {
		v4l1_window.x = 0;
		v4l1_window.y = 0;
		ioctl(fd, VIDIOCSWIN, &v4l1_window);
		if (w > 0) v4l1_window.width = w;
		if (h > 0) v4l1_window.height = h;
		fprintf(stderr, "calling V4L_1: VIDIOCSWIN\n");
		fprintf(stderr, "trying new size %dx%d\n",
		    v4l1_window.width, v4l1_window.height);
		if (ioctl(fd, VIDIOCSWIN, &v4l1_window) == -1) {
			perror("ioctl VIDIOCSWIN");
			return 0;
		}
	}
#else
	if (!fd || !w || !h) {}
#endif
	return 1;
}

static void v4l1_setfreq(int fd, unsigned long freq, int verb) {
#ifdef V4L_OK
	unsigned long f0, f1;
	f1 = (freq * 16) / 1000;
	ioctl(fd, VIDIOCGFREQ, &f0);
	if (verb) fprintf(stderr, "read freq: %d\n", (int) f0);
	if (freq > 0) {
		if (ioctl(fd, VIDIOCSFREQ, &f1) == -1) {
			perror("ioctl VIDIOCSFREQ");
		} else {
			ioctl(fd, VIDIOCGFREQ, &f0);
			if (verb) fprintf(stderr, "read freq: %d\n", (int) f0);
			last_freq = freq;
		}
	}
#else
	if (!fd || !freq || !verb) {}
#endif
}

static void v4l1_set_input(int fd, int which) {
#ifdef V4L_OK
	if (which != -1) {
		memset(&v4l1_channel, 0, sizeof(v4l1_channel));
		v4l1_channel.channel = which;
		if (ioctl(fd, VIDIOCGCHAN, &v4l1_channel) != -1) {
			v4l1_channel.channel = which;
			fprintf(stderr, "setting input channel to %d: %s\n",
			    which, v4l1_channel.name);
			last_channel = which;
			ioctl(fd, VIDIOCSCHAN, &v4l1_channel);
		}
	}
#else
	if (!fd || !which) {}
#endif
}

static int v4l1_setfmt(int fd, char *fmt) {
#ifdef V4L_OK
	unsigned short fnew;
	int bnew, rnew;

	fnew = v4l1_lu_palette_str(fmt, &bnew, &rnew);
	if (fnew) {
		v4l1_picture.depth = bnew; 
		v4l1_picture.palette = fnew;
	}
	fprintf(stderr, "calling V4L_1: VIDIOCSPICT\n");
	if (ioctl(fd, VIDIOCSPICT, &v4l1_picture) == -1) {
		perror("ioctl VIDIOCSPICT");
		return 0;
	}
	if (raw_fb_pixfmt) {
		free(raw_fb_pixfmt);
	}
	raw_fb_pixfmt = strdup(fmt);
#else
	if (!fd || !fmt) {}
#endif
	return 1;
}

static int ignore_all = 0;

static void apply_settings(char *dev, char *settings, int *fd) {
#ifdef V4L_OK
	char *str, *p, *fmt = NULL, *tun = NULL, *inp = NULL;
	int br = -1, co = -1, cn = -1, hu = -1;
	int w = -1, h = -1, b = -1;
	int sta = -1;
	int setcnt = 0;
	if (! settings || settings[0] == '\0') {
		return;
	}
	str = strdup(settings);
	p = strtok(str, ",");
	while (p) {
		if (strstr(p, "br=") == p) {
			br = atoi(p+3);
			if (br >= 0) setcnt++;
		} else if (strstr(p, "co=") == p) {
			co = atoi(p+3);
			if (co >= 0) setcnt++;
		} else if (strstr(p, "cn=") == p) {
			cn = atoi(p+3);
			if (cn >= 0) setcnt++;
		} else if (strstr(p, "hu=") == p) {
			hu = atoi(p+3);
			if (hu >= 0) setcnt++;
		} else if (strstr(p, "w=") == p) {
			w = atoi(p+2);
			if (w > 0) setcnt++;
		} else if (strstr(p, "h=") == p) {
			h = atoi(p+2);
			if (h > 0) setcnt++;
		} else if (strstr(p, "bpp=") == p) {
			b = atoi(p+4);
			if (b > 0) setcnt++;
		} else if (strstr(p, "fmt=") == p) {
			fmt = strdup(p+4);
			setcnt++;
		} else if (strstr(p, "tun=") == p) {
			tun = strdup(p+4);
			setcnt++;
		} else if (strstr(p, "inp=") == p) {
			inp = strdup(p+4);
			setcnt++;
		} else if (strstr(p, "sta=") == p) {
			sta = atoi(p+4);
			setcnt++;
		}
		p = strtok(NULL, ",");
	}
	free(str);
	if (! setcnt) {
		return;
	}
	if (*fd < 0) {
		*fd = open_dev(dev);
	}
	if (*fd < 0) {
		return;
	}
	v4l1_cap = v4l1_query(*fd, 1);
	v4l2_cap = v4l2_query(*fd, 1);

	if (v4l1_cap && ! ignore_all) {
		if (br >= 0) v4l1_picture.brightness = v4l1_val(br);
		if (hu >= 0) v4l1_picture.hue        = v4l1_val(hu);
		if (co >= 0) v4l1_picture.colour     = v4l1_val(co);
		if (cn >= 0) v4l1_picture.contrast   = v4l1_val(cn);

		fprintf(stderr, "calling V4L_1: VIDIOCSPICT\n");
		if (ioctl(*fd, VIDIOCSPICT, &v4l1_picture) == -1) {
			perror("ioctl VIDIOCSPICT");
		}

		if (fmt) {
			v4l1_setfmt(*fd, fmt);
		} else if (b > 0 && b != v4l1_picture.depth) {
			if (b == 8) {
				v4l1_setfmt(*fd, "HI240");
			} else if (b == 16) {
				v4l1_setfmt(*fd, "RGB565");
			} else if (b == 24) {
				v4l1_setfmt(*fd, "RGB24");
			} else if (b == 32) {
				v4l1_setfmt(*fd, "RGB32");
			}
		}

		v4l1_resize(*fd, w, h);

		if (tun) {
			int mode = -1;
			if (!strcasecmp(tun, "PAL")) {
				mode = VIDEO_MODE_PAL;
			} else if (!strcasecmp(tun, "NTSC")) {
				mode = VIDEO_MODE_NTSC;
			} else if (!strcasecmp(tun, "SECAM")) {
				mode = VIDEO_MODE_SECAM;
			} else if (!strcasecmp(tun, "AUTO")) {
				mode = VIDEO_MODE_AUTO;
			}
			if (mode != -1) {
				int i;
				for (i=0; i< v4l1_capability.channels; i++) {
					memset(&v4l1_channel, 0, sizeof(v4l1_channel));
					v4l1_channel.channel = i;
					if (ioctl(*fd, VIDIOCGCHAN, &v4l1_channel) == -1) {
						continue;
					}
					if (! v4l1_channel.tuners) {
						continue;
					}
					if (v4l1_channel.norm == mode) {
						continue;
					}
					v4l1_channel.norm = mode;
					ioctl(*fd, VIDIOCSCHAN, &v4l1_channel);
				}
			}
		}
		if (inp) {
			char s[2];
			int i, chan = -1;

			s[0] = inp[0];
			s[1] = '\0';
			if (strstr("0123456789", s)) {
				chan = atoi(inp);
			} else {
				for (i=0; i< v4l1_capability.channels; i++) {
					memset(&v4l1_channel, 0, sizeof(v4l1_channel));
					v4l1_channel.channel = i;
					if (ioctl(*fd, VIDIOCGCHAN, &v4l1_channel) == -1) {
						continue;
					}
					if (!strcmp(v4l1_channel.name, inp)) {
						chan = i;
						break;
					}
				}
			}
			v4l1_set_input(*fd, chan);
		}
		if (sta >= 0) {
			unsigned long freq = lookup_freq(sta);
			v4l1_setfreq(*fd, freq, 1);
		}
	}
	v4l1_cap = v4l1_query(*fd, 1);
	v4l2_cap = v4l2_query(*fd, 1);
#else
	if (!dev || !settings || !fd) {}
	return;
#endif
}

static double dval = 0.05;

static int v4l1_dpct(int old, int d) {
	int new, max = V4L1_MAX; 
	
	/* -1 and 1 are special cases for "small increments" */
	if (d == -1) {
		new = old - (int) (dval * max);
	} else if (d == 1) {
		new = old + (int) (dval * max);
	} else {
		new = (d * max)/100;
	}
	if (new < 0) {
		new = 0;
	}
	if (new > max) {
		new = max;
	}
	return new;
}

static void v4l_requery(void) {
	if (raw_fb_fd < 0) {
		return;
	}
	v4l1_cap = v4l1_query(raw_fb_fd, 1);
	v4l2_cap = v4l2_query(raw_fb_fd, 1);
}
	
static void v4l_br(int b) {
#ifdef V4L_OK
	int old = v4l1_picture.brightness;

	v4l1_picture.brightness = v4l1_dpct(old, b);
	ioctl(raw_fb_fd, VIDIOCSPICT, &v4l1_picture);
	v4l_requery();
#else
	if (!b) {}
#endif
}

static void v4l_hu(int b) {
#ifdef V4L_OK
	int old = v4l1_picture.hue;

	v4l1_picture.hue = v4l1_dpct(old, b);
	ioctl(raw_fb_fd, VIDIOCSPICT, &v4l1_picture);
	v4l_requery();
#else
	if (!b) {}
#endif
}

static void v4l_co(int b) {
#ifdef V4L_OK
	int old = v4l1_picture.colour;

	v4l1_picture.colour = v4l1_dpct(old, b);
	ioctl(raw_fb_fd, VIDIOCSPICT, &v4l1_picture);
	v4l_requery();
#else
	if (!b) {}
#endif
}

static void v4l_cn(int b) {
#ifdef V4L_OK
	int old = v4l1_picture.contrast;

	v4l1_picture.contrast = v4l1_dpct(old, b);
	ioctl(raw_fb_fd, VIDIOCSPICT, &v4l1_picture);
	v4l_requery();
#else
	if (!b) {}
#endif
}

static void v4l_sz(int b) {
#ifdef V4L_OK
	int w_old = v4l1_window.width;
	int h_old = v4l1_window.height;
	int w, h;

	if (w_old == 0) {
		w_old = 160;
	}
	if (h_old == 0) {
		h_old = 120;
	}

	if (b == 1) {
		w = w_old + (int) (0.15 * w_old); 
		h = h_old + (int) (0.15 * h_old); 
	} else if (b == -1) {
		w = w_old - (int) (0.15 * w_old); 
		h = h_old - (int) (0.15 * h_old); 
	} else {
		return;
	}

	if (! v4l1_resize(raw_fb_fd, w, h)) {
		return;
	}

	v4l_requery();

	push_black_screen(4);

	ignore_all = 1;
	do_new_fb(1);
	ignore_all = 0;
#else
	if (!b) {}
#endif
}

static void v4l_sta(int sta) {
#ifdef V4L_OK
	unsigned long freq = 0;
	int cur = lookup_station(last_freq);

	if (! last_freq) {
		if (sta == 0 || sta == -1) {
			sta = 11;
		}
	}

	if (sta == -1) {
		while (cur > 0) {
			freq = lookup_freq(--cur);
			if (freq) {
				break;
			}
		}
	} else if (sta == 0) {
		while (cur < CHANNEL_MAX - 1) {
			freq = lookup_freq(++cur);
			if (freq) {
				break;
			}
		}
	} else {
		freq = lookup_freq(sta);
		cur = sta;
	}
	fprintf(stderr, "to station %d / %d\n", cur, (int) freq);
	v4l1_setfreq(raw_fb_fd, freq, 0);
#else
	if (!sta) {}
#endif
}

static void v4l_inp(int inp) {
#ifdef V4L_OK
	int next = -1;
	if (inp == -1) {
		inp = last_channel + 1;
		if (inp >= v4l1_capability.channels) {
			inp = 0;
		}
		next = inp;
	} else if (inp == -2) {
		inp = last_channel - 1;
		if (inp < 0) {
			inp = v4l1_capability.channels - 1;
		}
		next = inp;
	} else {
		next = inp;
	}
	v4l1_set_input(raw_fb_fd, next);
#else
	if (!inp) {}
#endif
}

static void v4l_fmt(char *fmt) {
	if (v4l1_setfmt(raw_fb_fd, fmt)) {
		v4l_requery();

		ignore_all = 1;
		do_new_fb(1);
		ignore_all = 0;
	}
}

void v4l_key_command(rfbBool down, rfbKeySym keysym, rfbClientPtr client) {
	allowed_input_t input;

	if (raw_fb_fd < 0) {
		return;		
	}
	if (! down) {
		return;
	}
	if (view_only) {
		return;
	}
	get_allowed_input(client, &input);
	if (! input.keystroke) {
		return;
	}

	if (keysym == XK_b) {
		v4l_br(-1);
	} else if (keysym == XK_B) {
		v4l_br(+1);
	} else if (keysym == XK_h) {
		v4l_hu(-1);
	} else if (keysym == XK_H) {
		v4l_hu(+1);
	} else if (keysym == XK_c) {
		v4l_co(-1);
	} else if (keysym == XK_C) {
		v4l_co(+1);
	} else if (keysym == XK_n) {
		v4l_cn(-1);
	} else if (keysym == XK_N) {
		v4l_cn(+1);
	} else if (keysym == XK_s) {
		v4l_sz(-1);
	} else if (keysym == XK_S) {
		v4l_sz(+1);
	} else if (keysym == XK_i) {
		v4l_inp(-1);
	} else if (keysym == XK_I) {
		v4l_inp(-2);
	} else if (keysym == XK_Up) {
		v4l_sta(+0);
	} else if (keysym == XK_Down) {
		v4l_sta(-1);
	} else if (keysym == XK_F1) {
		v4l_fmt("HI240");
	} else if (keysym == XK_F2) {
		v4l_fmt("RGB565");
	} else if (keysym == XK_F3) {
		v4l_fmt("RGB24");
	} else if (keysym == XK_F4) {
		v4l_fmt("RGB32");
	} else if (keysym == XK_F5) {
		v4l_fmt("RGB555");
	} else if (keysym == XK_F6) {
		v4l_fmt("GREY");
	}
	if (client) {}
}


void v4l_pointer_command(int mask, int x, int y, rfbClientPtr client) {
	/* do not forget viewonly perms */
	if (mask || x || y || client) {}
}

static int colon_n(char *line) {
	char *q;
	int n;
	q = strrchr(line, ':');
	if (! q) {
		return 0;
	}
	q = lblanks(q+1);
	if (sscanf(q, "%d", &n) == 1) {
		return n;
	}
	return 0;
}

static char *colon_str(char *line) {
	char *q, *p, *t;
	q = strrchr(line, ':');
	if (! q) {
		return strdup("");
	}
	q = lblanks(q+1);
	p = strpbrk(q, " \t\n");
	if (p) {
		*p = '\0';
	}
	t = strdup(q);
	*p = '\n';
	return t;
}

static char *colon_tag(char *line) {
	char *q, *p, *t;
	q = strrchr(line, '[');
	if (! q) {
		return strdup("");
	}
	q++;
	p = strrchr(q, ']');
	if (! p) {
		return strdup("");
	}
	*p = '\0';
	t = strdup(q);
	*p = ']';
	return t;
}

static void lookup_rgb(char *fmt, int *bits, int *rev) {
	int tb, tr;

	if (v4l2_lu_palette_str(fmt, &tb, &tr)) {
		*bits = tb;
		*rev  = tr;
		return;
	}
	if (v4l1_lu_palette_str(fmt, &tb, &tr)) {
		*bits = tb;
		*rev  = tr;
		return;
	}
}

static char *v4l1_lu_palette(unsigned short palette) {
	switch(palette) {
#ifdef V4L_OK
		case VIDEO_PALETTE_GREY:	return "GREY";
		case VIDEO_PALETTE_HI240:	return "HI240";
		case VIDEO_PALETTE_RGB565:	return "RGB565";
		case VIDEO_PALETTE_RGB24:	return "RGB24";
		case VIDEO_PALETTE_RGB32:	return "RGB32";
		case VIDEO_PALETTE_RGB555:	return "RGB555";
		case VIDEO_PALETTE_YUV422:	return "YUV422";
		case VIDEO_PALETTE_YUYV:	return "YUYV";
		case VIDEO_PALETTE_UYVY:	return "UYVY";
		case VIDEO_PALETTE_YUV420:	return "YUV420";
		case VIDEO_PALETTE_YUV411:	return "YUV411";
		case VIDEO_PALETTE_RAW:		return "RAW";
		case VIDEO_PALETTE_YUV422P:	return "YUV422P";
		case VIDEO_PALETTE_YUV411P:	return "YUV411P";
		case VIDEO_PALETTE_YUV420P:	return "YUV420P";
		case VIDEO_PALETTE_YUV410P:	return "YUV410P";
#endif
		default:			return "unknown";
	}
}

static unsigned short v4l1_lu_palette_str(char *name, int *bits, int *rev) {
#ifdef V4L_OK
	*rev = 0;
	if (!strcmp(name, "RGB555")) {
		*bits = 16;
		return VIDEO_PALETTE_RGB555;
	} else if (!strcmp(name, "RGB565")) {
		*bits = 16;
		return VIDEO_PALETTE_RGB565;
	} else if (!strcmp(name, "RGB24")) {
		*bits = 24;
		return VIDEO_PALETTE_RGB24;
	} else if (!strcmp(name, "RGB32")) {
		*bits = 32;
		return VIDEO_PALETTE_RGB32;
	} else if (!strcmp(name, "HI240")) {
		*bits = 8;
		return VIDEO_PALETTE_HI240;
	} else if (!strcmp(name, "GREY")) {
		*bits = 8;
		return VIDEO_PALETTE_GREY;
	}
#else
	if (!name || !bits || !rev) {}
#endif
	return 0;
}

static char *v4l2_lu_palette(unsigned int fmt) {
	switch(fmt) {
#if defined(V4L_OK) && HAVE_V4L2
		case V4L2_PIX_FMT_RGB332:	return "RGB332";
		case V4L2_PIX_FMT_RGB555:	return "RGB555";
		case V4L2_PIX_FMT_RGB565:	return "RGB565";
		case V4L2_PIX_FMT_RGB555X:	return "RGB555X";
		case V4L2_PIX_FMT_RGB565X:	return "RGB565X";
		case V4L2_PIX_FMT_BGR24:	return "BGR24";
		case V4L2_PIX_FMT_RGB24:	return "RGB24";
		case V4L2_PIX_FMT_BGR32:	return "BGR32";
		case V4L2_PIX_FMT_RGB32:	return "RGB32";
		case V4L2_PIX_FMT_GREY:		return "GREY";
		case V4L2_PIX_FMT_YVU410:	return "YVU410";
		case V4L2_PIX_FMT_YVU420:	return "YVU420";
		case V4L2_PIX_FMT_YUYV:		return "YUYV";
		case V4L2_PIX_FMT_UYVY:		return "UYVY";
		case V4L2_PIX_FMT_YUV422P:	return "YUV422P";
		case V4L2_PIX_FMT_YUV411P:	return "YUV411P";
		case V4L2_PIX_FMT_Y41P:		return "Y41P";
		case V4L2_PIX_FMT_NV12:		return "NV12";
		case V4L2_PIX_FMT_NV21:		return "NV21";
		case V4L2_PIX_FMT_YUV410:	return "YUV410";
		case V4L2_PIX_FMT_YUV420:	return "YUV420";
		case V4L2_PIX_FMT_YYUV:		return "YYUV";
		case V4L2_PIX_FMT_HI240:	return "HI240";
		case V4L2_PIX_FMT_MJPEG:	return "MJPEG";
		case V4L2_PIX_FMT_JPEG:		return "JPEG";
		case V4L2_PIX_FMT_DV:		return "DV";
		case V4L2_PIX_FMT_MPEG:		return "MPEG";
#endif
		default:			return "unknown";
	}
}

static unsigned int v4l2_lu_palette_str(char *name, int *bits, int *rev) {
#if defined(V4L_OK) && HAVE_V4L2
	if (!strcmp(name, "RGB1") || !strcmp(name, "RGB332")) {
		*bits = 8;
		*rev = 0;
		return V4L2_PIX_FMT_RGB332;
	} else if (!strcmp(name, "RGBO") || !strcmp(name, "RGB555")) {
		*bits = 16;
		*rev = 0;
		return V4L2_PIX_FMT_RGB555;
	} else if (!strcmp(name, "RGBP") || !strcmp(name, "RGB565")) {
		*bits = 16;
		*rev = 0;
		return V4L2_PIX_FMT_RGB565;
	} else if (!strcmp(name, "RGBQ") || !strcmp(name, "RGB555X")) {
		*bits = 16;
		*rev = 1;
		return V4L2_PIX_FMT_RGB555X;
	} else if (!strcmp(name, "RGBR") || !strcmp(name, "RGB565X")) {
		*bits = 16;
		*rev = 1;
		return V4L2_PIX_FMT_RGB565X;
	} else if (!strcmp(name, "BGR3") || !strcmp(name, "BGR24")) {
		*bits = 24;
		*rev = 1;
		return V4L2_PIX_FMT_BGR24;
	} else if (!strcmp(name, "RGB3") || !strcmp(name, "RGB24")) {
		*bits = 24;
		*rev = 0;
		return V4L2_PIX_FMT_RGB24;
	} else if (!strcmp(name, "BGR4") || !strcmp(name, "BGR32")) {
		*bits = 32;
		*rev = 1;
		return V4L2_PIX_FMT_BGR32;
	} else if (!strcmp(name, "RGB4") || !strcmp(name, "RGB32")) {
		*bits = 32;
		*rev = 0;
		return V4L2_PIX_FMT_RGB32;
	} else if (!strcmp(name, "GREY")) {
		*bits = 8;
		*rev = 0;
		return V4L2_PIX_FMT_GREY;
	}
#else
	if (!name || !bits || !rev) {}
#endif
	return 0;
}

static int v4l1_query(int fd, int v) {
#ifdef V4L_OK
	unsigned int i;

	memset(&v4l1_capability, 0, sizeof(v4l1_capability));
	memset(&v4l1_channel,    0, sizeof(v4l1_channel));
	memset(&v4l1_tuner,      0, sizeof(v4l1_tuner));
	memset(&v4l1_picture,    0, sizeof(v4l1_picture));
	memset(&v4l1_window,     0, sizeof(v4l1_window));

	if (v) fprintf(stderr, "\nV4L_1 query:\n");
#ifdef VIDIOCGCAP
	if (ioctl(fd, VIDIOCGCAP, &v4l1_capability) == -1) {
		perror("ioctl VIDIOCGCAP");
		fprintf(stderr, "\n");
		return 0;
	}
#else
	return 0;
#endif
	if (v) fprintf(stderr, "v4l-1 capability:\n");
	if (v) fprintf(stderr, "     name:      %s\n", v4l1_capability.name);
	if (v) fprintf(stderr, "     channels:  %d\n", v4l1_capability.channels);
	if (v) fprintf(stderr, "     audios:    %d\n", v4l1_capability.audios);
	if (v) fprintf(stderr, "     maxwidth:  %d\n", v4l1_capability.maxwidth);
	if (v) fprintf(stderr, "     maxheight: %d\n", v4l1_capability.maxheight);
	if (v) fprintf(stderr, "     minwidth:  %d\n", v4l1_capability.minwidth);
	if (v) fprintf(stderr, "     minheight: %d\n", v4l1_capability.minheight);

	for (i=0; (int) i < v4l1_capability.channels; i++) {
		char *type = "unknown";
		memset(&v4l1_channel, 0, sizeof(v4l1_channel));
		v4l1_channel.channel = i;
		if (ioctl(fd, VIDIOCGCHAN, &v4l1_channel) == -1) {
			perror("ioctl VIDIOCGCHAN");
			continue;
		}
		if (v4l1_channel.type == VIDEO_TYPE_TV) {
			type = "TV";
		} else if (v4l1_channel.type == VIDEO_TYPE_CAMERA) {
			type = "CAMERA";
		}
		if (v) fprintf(stderr, "     channel[%d]: %s\ttuners: %d norm: %d type: %d  %s\n",
		    i, v4l1_channel.name, v4l1_channel.tuners, v4l1_channel.norm,
		    v4l1_channel.type, type);
	}

	memset(&v4l1_tuner, 0, sizeof(v4l1_tuner));
	if (ioctl(fd, VIDIOCGTUNER, &v4l1_tuner) != -1) {
		char *mode = "unknown";
		if (v4l1_tuner.mode == VIDEO_MODE_PAL) {
			mode = "PAL";
		} else if (v4l1_tuner.mode == VIDEO_MODE_NTSC) {
			mode = "NTSC";
		} else if (v4l1_tuner.mode == VIDEO_MODE_SECAM) {
			mode = "SECAM";
		} else if (v4l1_tuner.mode == VIDEO_MODE_AUTO) {
			mode = "AUTO";
		}

		if (v) fprintf(stderr, "     tuner[%d]:   %s\tflags: 0x%x mode: %s\n",
		    v4l1_tuner.tuner, v4l1_tuner.name, v4l1_tuner.flags, mode);
		
	}

	if (ioctl(fd, VIDIOCGPICT, &v4l1_picture) == -1) {
		perror("ioctl VIDIOCGCHAN");
		return 0;
	}
	if (v) fprintf(stderr, "v4l-1 picture:\n");
	if (v) fprintf(stderr, "     brightness:  %d\n", v4l1_picture.brightness);
	if (v) fprintf(stderr, "     hue:         %d\n", v4l1_picture.hue);
	if (v) fprintf(stderr, "     colour:      %d\n", v4l1_picture.colour);
	if (v) fprintf(stderr, "     contrast:    %d\n", v4l1_picture.contrast);
	if (v) fprintf(stderr, "     whiteness:   %d\n", v4l1_picture.whiteness);
	if (v) fprintf(stderr, "     depth:       %d\n", v4l1_picture.depth);
	if (v) fprintf(stderr, "     palette:     %d  %s\n", v4l1_picture.palette,
	    v4l1_lu_palette(v4l1_picture.palette));
	
	if (ioctl(fd, VIDIOCGWIN, &v4l1_window) == -1) {
		perror("ioctl VIDIOCGWIN");
		if (v) fprintf(stderr, "\n");
		return 0;
	}
	if (v) fprintf(stderr, "v4l-1 window:\n");
	if (v) fprintf(stderr, "     x:           %d\n", v4l1_window.x);
	if (v) fprintf(stderr, "     y:           %d\n", v4l1_window.y);
	if (v) fprintf(stderr, "     width:       %d\n", v4l1_window.width);
	if (v) fprintf(stderr, "     height:      %d\n", v4l1_window.height);
	if (v) fprintf(stderr, "     chromakey:   %d\n", v4l1_window.chromakey);
	if (v) fprintf(stderr, "\n");

	return 1;
#else
	if (!fd || !v) {}
	return 0;
#endif	/* V4L_OK */

}
static int v4l2_query(int fd, int v) {
#if defined(V4L_OK) && HAVE_V4L2
	unsigned int i;

	memset(&v4l2_capability, 0, sizeof(v4l2_capability));
	memset(&v4l2_input,      0, sizeof(v4l2_input));
	memset(&v4l2_tuner,      0, sizeof(v4l2_tuner));
	memset(&v4l2_fmtdesc,    0, sizeof(v4l2_fmtdesc));
	memset(&v4l2_format,     0, sizeof(v4l2_format));

	if (v) fprintf(stderr, "\nV4L_2 query:\n");
#ifdef VIDIOC_QUERYCAP
	if (ioctl(fd, VIDIOC_QUERYCAP, &v4l2_capability) == -1) {
		perror("ioctl VIDIOC_QUERYCAP");
		if (v) fprintf(stderr, "\n");
		return 0;
	}
#else
	return 0;
#endif

	if (v) fprintf(stderr, "v4l-2 capability:\n");
	if (v) fprintf(stderr, "    driver:       %s\n", v4l2_capability.driver);
	if (v) fprintf(stderr, "    card:         %s\n", v4l2_capability.card);
	if (v) fprintf(stderr, "    bus_info:     %s\n", v4l2_capability.bus_info);
	if (v) fprintf(stderr, "    version:      %d\n", v4l2_capability.version);
	if (v) fprintf(stderr, "    capabilities: %u\n", v4l2_capability.capabilities);

	for (i=0; ; i++) {
		memset(&v4l2_input, 0, sizeof(v4l2_input));
		v4l2_input.index = i;
		if (ioctl(fd, VIDIOC_ENUMINPUT, &v4l2_input) == -1) {
			break;
		}
		if (v) fprintf(stderr, "    input[%d]: %s\ttype: %d tuner: %d\n",
		    i, v4l2_input.name, v4l2_input.type, v4l2_input.tuner);
	}
	if (v4l2_capability.capabilities & V4L2_CAP_TUNER) {
		for (i=0; ; i++) {
			memset(&v4l2_tuner, 0, sizeof(v4l2_tuner));
			v4l2_tuner.index = i;
			if (ioctl(fd, VIDIOC_G_TUNER, &v4l2_tuner) == -1) {
				break;
			}
			if (v) fprintf(stderr, "    tuner[%d]: %s\ttype: %d\n",
			    i, v4l2_tuner.name, v4l2_tuner.type);
		}
	}
	if (v4l2_capability.capabilities & V4L2_CAP_VIDEO_CAPTURE) {
		for (i=0; ; i++) {
			memset(&v4l2_fmtdesc, 0, sizeof(v4l2_fmtdesc));
			v4l2_fmtdesc.index = i;
			v4l2_fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
			
			if (ioctl(fd, VIDIOC_ENUM_FMT, &v4l2_fmtdesc) == -1) {
				break;
			}
			if (v) fprintf(stderr, "    fmtdesc[%d]: %s\ttype: %d"
			    " pixelformat: %d\n",
			    i, v4l2_fmtdesc.description, v4l2_fmtdesc.type,
			    v4l2_fmtdesc.pixelformat);
		}
		v4l2_format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
		if (ioctl(fd, VIDIOC_G_FMT, &v4l2_format) == -1) {
			perror("ioctl VIDIOC_G_FMT");
		} else {
			if (v) fprintf(stderr, "    width:  %d\n", v4l2_format.fmt.pix.width);
			if (v) fprintf(stderr, "    height: %d\n", v4l2_format.fmt.pix.height);
			if (v) fprintf(stderr, "    format: %u %s\n",
			    v4l2_format.fmt.pix.pixelformat,
			    v4l2_lu_palette(v4l2_format.fmt.pix.pixelformat));
		}
	}

	return 1;
#else
	if (!fd || !v) {}
	return 0;
#endif	/* V4L_OK && HAVE_V4L2 */

}

static int open_dev(char *dev) {
	int dfd = -1;
	if (! dev) {
		return dfd;
	}
	dfd = open(dev, O_RDWR);
	if (dfd < 0) {
		rfbLog("failed to rawfb file: %s O_RDWR\n", dev);
		rfbLogPerror("open");
		dfd = open(dev, O_RDONLY);
	}
	if (dfd < 0) {
		rfbLog("failed to rawfb file: %s\n", dev);
		rfbLog("failed to rawfb file: %s O_RDONLY\n", dev);
		rfbLogPerror("open");
	}
	return dfd;
}

static char *guess_via_v4l(char *dev, int *fd) {
#ifdef V4L_OK
	int dfd;
	
	if (*fd < 0) {
		dfd = open_dev(dev);
		*fd = dfd;
	}
	dfd = *fd;
	if (dfd < 0) {
		return NULL;
	}
	if (v4l1_cap < 0) {
		v4l1_cap = v4l1_query(dfd, 1);
	}
	if (v4l2_cap < 0) {
		v4l2_cap = v4l2_query(dfd, 1);
	}

	if (v4l2_cap) {
#if HAVE_V4L2
		int g_w = v4l2_format.fmt.pix.width;
		int g_h = v4l2_format.fmt.pix.height;
		int g_d = 0, g_rev;

		if (v4l2_format.fmt.pix.pixelformat) {
			char *str = v4l2_lu_palette(v4l2_format.fmt.pix.pixelformat);
			if (strcmp(str, "unknown")) {
				v4l2_lu_palette_str(str, &g_d, &g_rev);
			}
		}
		
		if (g_w > 0 && g_h > 0 && g_d > 0) {
			char *atparms = (char *) malloc(200);
			char *pal = v4l2_lu_palette(v4l2_format.fmt.pix.pixelformat);
			sprintf(atparms, "%dx%dx%d", g_w, g_h, g_d);
			if (strstr(pal, "RGB555")) {
				strcat(atparms, ":7c00/3e0/1f");
			}
			*fd = dfd;
			return atparms;
		}
#endif
	}
	if (v4l1_cap) {
		int g_w = v4l1_window.width;
		int g_h = v4l1_window.height;
		int g_d = v4l1_picture.depth;
		int g_rev;
		if (g_d == 0) {
			char *str = v4l1_lu_palette(v4l1_picture.palette);
			if (strcmp(str, "unknown")) {
				v4l1_lu_palette_str(str, &g_d, &g_rev);
			}
		}
if (0) fprintf(stderr, "v4l1: %d %d %d\n", g_w, g_h, g_d);
		if (g_w > 0 && g_h > 0 && g_d > 0) {
			char *atparms = (char *) malloc(200);
			char *pal = v4l1_lu_palette(v4l1_picture.palette);
			fprintf(stderr, "palette: %s\n", pal);
			sprintf(atparms, "%dx%dx%d", g_w, g_h, g_d);
			if (strstr(pal, "RGB555")) {
				strcat(atparms, ":7c00/3e0/1f");
			}
			*fd = dfd;
			return atparms;
		}
	}

	/* failure */
	close(dfd);
	return NULL;
#else
	if (!dev || !fd) {}
	return NULL;
#endif
}

static char *guess_via_v4l_info(char *dev, int *fd) {
	char *atparms, *cmd;
	char line[1024], tmp[] = "/tmp/x11vnc-tmp.XXXXXX";
	FILE *out;
	int tmp_fd, len, rc, curr = 0;
	int g_w = 0, g_h = 0, g_b = 0, mask_rev = 0;
	char *g_fmt = NULL;

	if (*fd) {}

	/* v4l-info */
	if (no_external_cmds || !cmd_ok("v4l-info")) {
		rfbLog("guess_via_v4l_info: cannot run external "
		    "command: v4l-info\n");
		return NULL;
	}

	if (strchr(dev, '\'')) {
		rfbLog("guess_via_v4l_info: bad dev string: %s\n", dev);
		return NULL;
	}

	tmp_fd = mkstemp(tmp);
	if (tmp_fd < 0) {
		return NULL;
	}

	len =  strlen("v4l-info")+1+1+strlen(dev)+1+1+1+1+strlen(tmp)+1; 
	cmd = (char *) malloc(len);
	rfbLog("guess_via_v4l_info running: v4l-info '%s'\n", dev);
	sprintf(cmd, "v4l-info '%s' > %s", dev, tmp);

	close(tmp_fd);
	close_exec_fds();
	rc = system(cmd);
	if (rc != 0) {
		unlink(tmp);
		return NULL;
	}

	out = fopen(tmp, "r");
	if (out == NULL) {
		unlink(tmp);
		return NULL;
	}
	
	curr = 0;
	while (fgets(line, 1024, out) != NULL) {
		char *lb = lblanks(line);
		if (strstr(line, "video capture") == line) {
			curr = C_VIDEO_CAPTURE;
		} else if (strstr(line, "picture") == line) {
			curr = C_PICTURE;
		} else if (strstr(line, "window") == line) {
			curr = C_WINDOW;
		}

if (0) fprintf(stderr, "lb: %s", lb);

		if (curr == C_VIDEO_CAPTURE) {
			if (strstr(lb, "pixelformat ") == lb) {
				fprintf(stderr, "%s", line);
			} else if (strstr(lb, "fmt.pix.width ") == lb) {
				if (! g_w) {
					g_w = colon_n(line);
				}
			} else if (strstr(lb, "fmt.pix.height ") == lb) {
				if (! g_h) {
					g_h = colon_n(line);
				}
			} else if (strstr(lb, "fmt.pix.pixelformat ") == lb) {
				if (! g_fmt) {
					g_fmt = colon_tag(line);
				}
			}
		} else if (curr == C_PICTURE) {
			if (strstr(lb, "depth ") == lb) {
				if (! g_b) {
					g_b = colon_n(line);
				}
			} else if (strstr(lb, "palette ") == lb) {
				if (! g_fmt) {
					g_fmt = colon_str(line);
				}
			}
		} else if (curr == C_WINDOW) {
			if (strstr(lb, "width ") == lb) {
				if (! g_w) {
					g_w = colon_n(line);
				}
			} else if (strstr(lb, "height ") == lb) {
				if (! g_h) {
					g_h = colon_n(line);
				}
			}
		}
	}
	fclose(out);
	unlink(tmp);

	if (! g_w) {
		rfbLog("could not guess device width.\n");
		return NULL;
	}
	rfbLog("guessed device width:  %d\n", g_w);

	if (! g_h) {
		rfbLog("could not guess device height.\n");
		return NULL;
	}
	rfbLog("guessed device height: %d\n", g_h);

	if (g_fmt) {
		rfbLog("guessed pixel fmt:     %s\n", g_fmt);
		lookup_rgb(g_fmt, &g_b, &mask_rev);
	}
	if (! g_b) {
		rfbLog("could not guess device bpp.\n");
		return NULL;
	}
	rfbLog("guessed device bpp:    %d\n", g_b);

	atparms = (char *) malloc(100);
	sprintf(atparms, "%dx%dx%d", g_w, g_h, g_b);
	return atparms;
}

static void parse_str(char *str, char **dev, char **settings, char **atparms) {
	char *p, *q, *s = NULL;

	q = strchr(str, '@');
	if (q && strlen(q+1) > 0) {
		/* ends @WxHXB... */
		*atparms = strdup(q+1);
		*q = '\0';
	}

	q = strchr(str, ':');
	if (q && strlen(q+1) > 0) {
		/* ends :br=N,w=N... */
		s = strdup(q+1);
		*settings = s;
		*q = '\0';
	}

	if (s != NULL) {
		/* see if fn=filename */
		q = strstr(s, "fn=");
		if (q) {
			q += strlen("fn=");
			p = strchr(q, ',');
			if (p) {
				*p = '\0';
				*dev = strdup(q); 
				*p = ',';
			} else {
				*dev = strdup(q); 
			}
			rfbLog("set video device to: '%s'\n", *dev);
		}
	}

	if (*dev == NULL) {
		struct stat sbuf;
		s = (char *) malloc(strlen("/dev/") + strlen(str) + 2);
		if (strstr(str, "/dev/") == str) {
			sprintf(s, "%s", str);
		} else {
			sprintf(s, "/dev/%s", str);
		} 
		rfbLog("Checking existence of '%s'\n", s);
                if (stat(s, &sbuf) != 0) {
			rfbLogPerror("stat");
			strcat(s, "0");
			rfbLog("switching to '%s'\n", s);
		}
                if (stat(s, &sbuf) != 0) {
			rfbLogPerror("stat");
			rfbLog("You will need to specify the video device more explicity.\n");
		}

		*dev = s;
		rfbLog("set video device to: '%s'\n", *dev);
	}
}

char *v4l_guess(char *str, int *fd) {
	char *dev = NULL, *settings = NULL, *atparms = NULL;

	parse_str(str, &dev, &settings, &atparms);

	init_freqs();

	v4l1_cap = -1;
	v4l2_cap = -1;
	*fd = -1;

	if (dev == NULL) {
		rfbLog("v4l_guess: could not find device in: %s\n", str);
		return NULL;
	}

	if (settings) {
		apply_settings(dev, settings, fd);
	}

	if (atparms) {
		/* use user's parameters. */
		char *t = (char *) malloc(5+strlen(dev)+1+strlen(atparms)+1);
		sprintf(t, "snap:%s@%s", dev, atparms);
		return t;
	}

	/* try to query the device for parameters. */
	atparms = guess_via_v4l(dev, fd);
	if (atparms == NULL) {
		/* try again with v4l-info(1) */
		atparms = guess_via_v4l_info(dev, fd);
	}

	if (atparms == NULL) {
		/* bad news */
		if (*fd >= 0) {
			close(*fd);
		}
		*fd = -1;
		return NULL;
	} else {
		char *t = (char *) malloc(5+strlen(dev)+1+strlen(atparms)+1);
		sprintf(t, "snap:%s@%s", dev, atparms);
		return t;
	}
}

static unsigned long lookup_freqtab(int sta) {

	if (sta >= CHANNEL_MAX) {
		return (unsigned long) sta;
	}
	if (sta < 0 || sta >= CHANNEL_MAX) {
		return 0;
	}
	return custom_freq[sta];
}

static unsigned long lookup_freq(int sta) {
	if (freqtab) {
		return lookup_freqtab(sta);
	}
	if (sta >= CHANNEL_MAX) {
		return (unsigned long) sta;
	}
	if (sta < 1 || sta > 125) {
		return 0;
	}
	return ntsc_cable[sta];
}

static int lookup_station(unsigned long freq) {
	int i;
	if (freqtab) {
		for (i = 0; i < CHANNEL_MAX; i++) {
if (0) fprintf(stderr, "%lu %lu\n", freq, custom_freq[i]);
			if (freq == custom_freq[i]) {
				return i;
			}
		}
	} else {
		for (i = 1; i <= 125; i++) {
			if (freq == ntsc_cable[i]) {
				return i;
			}
		}
	}
	return 0;
}

static void init_freqtab(char *file) {
	char *p, *q, *dir, *file2;
	char line[1024], inc[1024];
	char *text, *str;
	int size = 0, maxn, extra, currn;
	FILE *in1, *in2;
	static int v = 1;
	if (quiet) {
		v = 0;
	}

	/* YUCK */

	dir = strdup(file);
	q = strrchr(dir, '/');
	if (q) {
		*(q+1) = '\0';
	} else {
		free(dir);
		dir = strdup("./");
	}
	file2 = (char *) malloc(strlen(dir) + 1024 + 1);
	in1 = fopen(file, "r");
	if (in1 == NULL) {
		rfbLog("error opening freqtab: %s\n", file);
		clean_up_exit(1);
	}
	if (v) fprintf(stderr, "loading frequencies from: %s\n", file);
	while (fgets(line, 1024, in1) != NULL) {
		char *lb;
		char line2[1024];
		size += strlen(line);
		lb = lblanks(line);
		if (strstr(lb, "#include") == lb && 
		    sscanf(lb, "#include %s", inc) == 1) {
			char *q, *s = inc;
			if (s[0] == '"') {
				s++;
			}
			q = strrchr(s, '"');
			if (q) {
				*q = '\0';
			}
			sprintf(file2, "%s%s", dir, s);
			in2 = fopen(file2, "r");
			if (in2 == NULL) {
				rfbLog("error opening freqtab include: %s %s\n", line, file2);
				clean_up_exit(1);
			}
			if (v) fprintf(stderr, "loading frequencies from: %s\n", file2);
			while (fgets(line2, 1024, in2) != NULL) {
				size += strlen(line2);
			}
			fclose(in2);
		}
	}
	fclose(in1);

	size = 4*(size + 10000);

	text = (char *) malloc(size);

	text[0] = '\0';

	in1 = fopen(file, "r");
	if (in1 == NULL) {
		rfbLog("error opening freqtab: %s\n", file);
		clean_up_exit(1);
	}
	while (fgets(line, 1024, in1) != NULL) {
		char *lb;
		char line2[1024];
		lb = lblanks(line);
		if (lb[0] == '[') {
			strcat(text, lb);
		} else if (strstr(lb, "freq")) {
			strcat(text, lb);
		} else if (strstr(lb, "#include") == lb && 
		    sscanf(lb, "#include %s", inc) == 1) {
			char *lb2;
			char *q, *s = inc;
			if (s[0] == '"') {
				s++;
			}
			q = strrchr(s, '"');
			if (q) {
				*q = '\0';
			}
			sprintf(file2, "%s%s", dir, s);
			in2 = fopen(file2, "r");
			if (in2 == NULL) {
				rfbLog("error opening freqtab include: %s %s\n", line, file2);
				clean_up_exit(1);
			}
			while (fgets(line2, 1024, in2) != NULL) {
				lb2 = lblanks(line2);
				if (lb2[0] == '[') {
					strcat(text, lb2);
				} else if (strstr(lb2, "freq")) {
					strcat(text, lb2);
				}
				if ((int) strlen(text) > size/2) {
					break;
				}
			}
			fclose(in2);
		}
		if ((int) strlen(text) > size/2) {
			break;
		}
	}
	fclose(in1);

	if (0) fprintf(stderr, "%s", text);

	str = strdup(text);
	p = strtok(str, "\n");
	maxn = -1;
	extra = 0;
	while (p) {
		if (p[0] == '[') {
			int ok = 1;
			q = p+1;	
			while (*q) {
				if (*q == ']') {
					break;
				}
				if (! isdigit((unsigned char) (*q))) {
					if (0) fprintf(stderr, "extra: %s\n", p);
					extra++;
					ok = 0;
					break;
				}
				q++;
			}
			if (ok) {
				int n;
				if (sscanf(p, "[%d]", &n) == 1)  {
					if (n > maxn) {
						maxn = n;
					}
					if (0) fprintf(stderr, "maxn:  %d %d\n", maxn, n);
				}
			}
			
		}
		p = strtok(NULL, "\n");
	}
	free(str);

	str = strdup(text);
	p = strtok(str, "\n");
	extra = 0;
	currn = 0;
	if (v) fprintf(stderr, "\nname\tstation\tfreq (KHz)\n");
	while (p) {
		if (p[0] == '[') {
			int ok = 1;
			strncpy(line, p, 100);
			q = p+1;	
			while (*q) {
				if (*q == ']') {
					break;
				}
				if (! isdigit((unsigned char) (*q))) {
					extra++;
					currn = maxn + extra;
					ok = 0;
					break;
				}
				q++;
			}
			if (ok) {
				int n;
				if (sscanf(p, "[%d]", &n) == 1)  {
					currn = n;
				}
			}
		}
		if (strstr(p, "freq") && (q = strchr(p, '=')) != NULL) {
			int n;
			q = lblanks(q+1);
			if (sscanf(q, "%d", &n) == 1) {
				if (currn >= 0 && currn < CHANNEL_MAX) {
					if (v) fprintf(stderr, "%s\t%d\t%d\n", line, currn, n);
					custom_freq[currn] = (unsigned long) n;
					if (last_freq == 0) {
						last_freq = custom_freq[currn];
					}
				}
			}
		}
		p = strtok(NULL, "\n");
	}
	if (v) fprintf(stderr, "\n");
	v = 0;
	free(str);
	free(text);
	free(dir);
	free(file2);
}

static void init_freqs(void) {
	int i;
	for (i=0; i<CHANNEL_MAX; i++) {
		ntsc_cable[i] = 0;
		custom_freq[i] = 0;
	}

	init_ntsc_cable();
	last_freq = ntsc_cable[1];

	if (freqtab) {
		init_freqtab(freqtab);
	}
}

static void init_ntsc_cable(void) {
	ntsc_cable[1] = 73250;
	ntsc_cable[2] = 55250;
	ntsc_cable[3] = 61250;
	ntsc_cable[4] = 67250;
	ntsc_cable[5] = 77250;
	ntsc_cable[6] = 83250;
	ntsc_cable[7] = 175250;
	ntsc_cable[8] = 181250;
	ntsc_cable[9] = 187250;
	ntsc_cable[10] = 193250;
	ntsc_cable[11] = 199250;
	ntsc_cable[12] = 205250;
	ntsc_cable[13] = 211250;
	ntsc_cable[14] = 121250;
	ntsc_cable[15] = 127250;
	ntsc_cable[16] = 133250;
	ntsc_cable[17] = 139250;
	ntsc_cable[18] = 145250;
	ntsc_cable[19] = 151250;
	ntsc_cable[20] = 157250;
	ntsc_cable[21] = 163250;
	ntsc_cable[22] = 169250;
	ntsc_cable[23] = 217250;
	ntsc_cable[24] = 223250;
	ntsc_cable[25] = 229250;
	ntsc_cable[26] = 235250;
	ntsc_cable[27] = 241250;
	ntsc_cable[28] = 247250;
	ntsc_cable[29] = 253250;
	ntsc_cable[30] = 259250;
	ntsc_cable[31] = 265250;
	ntsc_cable[32] = 271250;
	ntsc_cable[33] = 277250;
	ntsc_cable[34] = 283250;
	ntsc_cable[35] = 289250;
	ntsc_cable[36] = 295250;
	ntsc_cable[37] = 301250;
	ntsc_cable[38] = 307250;
	ntsc_cable[39] = 313250;
	ntsc_cable[40] = 319250;
	ntsc_cable[41] = 325250;
	ntsc_cable[42] = 331250;
	ntsc_cable[43] = 337250;
	ntsc_cable[44] = 343250;
	ntsc_cable[45] = 349250;
	ntsc_cable[46] = 355250;
	ntsc_cable[47] = 361250;
	ntsc_cable[48] = 367250;
	ntsc_cable[49] = 373250;
	ntsc_cable[50] = 379250;
	ntsc_cable[51] = 385250;
	ntsc_cable[52] = 391250;
	ntsc_cable[53] = 397250;
	ntsc_cable[54] = 403250;
	ntsc_cable[55] = 409250;
	ntsc_cable[56] = 415250;
	ntsc_cable[57] = 421250;
	ntsc_cable[58] = 427250;
	ntsc_cable[59] = 433250;
	ntsc_cable[60] = 439250;
	ntsc_cable[61] = 445250;
	ntsc_cable[62] = 451250;
	ntsc_cable[63] = 457250;
	ntsc_cable[64] = 463250;
	ntsc_cable[65] = 469250;
	ntsc_cable[66] = 475250;
	ntsc_cable[67] = 481250;
	ntsc_cable[68] = 487250;
	ntsc_cable[69] = 493250;
	ntsc_cable[70] = 499250;
	ntsc_cable[71] = 505250;
	ntsc_cable[72] = 511250;
	ntsc_cable[73] = 517250;
	ntsc_cable[74] = 523250;
	ntsc_cable[75] = 529250;
	ntsc_cable[76] = 535250;
	ntsc_cable[77] = 541250;
	ntsc_cable[78] = 547250;
	ntsc_cable[79] = 553250;
	ntsc_cable[80] = 559250;
	ntsc_cable[81] = 565250;
	ntsc_cable[82] = 571250;
	ntsc_cable[83] = 577250;
	ntsc_cable[84] = 583250;
	ntsc_cable[85] = 589250;
	ntsc_cable[86] = 595250;
	ntsc_cable[87] = 601250;
	ntsc_cable[88] = 607250;
	ntsc_cable[89] = 613250;
	ntsc_cable[90] = 619250;
	ntsc_cable[91] = 625250;
	ntsc_cable[92] = 631250;
	ntsc_cable[93] = 637250;
	ntsc_cable[94] = 643250;
	ntsc_cable[95] = 91250;
	ntsc_cable[96] = 97250;
	ntsc_cable[97] = 103250;
	ntsc_cable[98] = 109250;
	ntsc_cable[99] = 115250;
	ntsc_cable[100] = 649250;
	ntsc_cable[101] = 655250;
	ntsc_cable[102] = 661250;
	ntsc_cable[103] = 667250;
	ntsc_cable[104] = 673250;
	ntsc_cable[105] = 679250;
	ntsc_cable[106] = 685250;
	ntsc_cable[107] = 691250;
	ntsc_cable[108] = 697250;
	ntsc_cable[109] = 703250;
	ntsc_cable[110] = 709250;
	ntsc_cable[111] = 715250;
	ntsc_cable[112] = 721250;
	ntsc_cable[113] = 727250;
	ntsc_cable[114] = 733250;
	ntsc_cable[115] = 739250;
	ntsc_cable[116] = 745250;
	ntsc_cable[117] = 751250;
	ntsc_cable[118] = 757250;
	ntsc_cable[119] = 763250;
	ntsc_cable[120] = 769250;
	ntsc_cable[121] = 775250;
	ntsc_cable[122] = 781250;
	ntsc_cable[123] = 787250;
	ntsc_cable[124] = 793250;
	ntsc_cable[125] = 799250;
}

OpenPOWER on IntegriCloud