File: jack-keyboard.c

package info (click to toggle)
jack-keyboard 2.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch, wheezy
  • size: 340 kB
  • ctags: 454
  • sloc: ansic: 2,026; makefile: 39
file content (1877 lines) | stat: -rw-r--r-- 47,825 bytes parent folder | download
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
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
/*-
 * Copyright (c) 2007, 2008 Edward Tomasz NapieraƂa <trasz@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * jack-keyboard is a virtual keyboard for JACK MIDI.
 *
 * For questions and comments, you can contact:
 * - Edward Tomasz Napierala <trasz@FreeBSD.org>
 * - Hans Petter Selasky <hselasky@FreeBSD.org>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <sysexits.h>
#include <errno.h>
#include <jack/jack.h>
#include <jack/midiport.h>
#include <jack/ringbuffer.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "pianokeyboard.h"

#ifdef HAVE_LASH
#include <lash/lash.h>
#endif

#ifdef HAVE_X11
#include <gdk/gdkx.h>
#endif

#define VELOCITY_MAX		127
#define VELOCITY_HIGH		100
#define VELOCITY_NORMAL		64
#define VELOCITY_LOW		32
#define VELOCITY_MIN		1

#define OUTPUT_PORT_NAME	"midi_out"
#define INPUT_PORT_NAME		"midi_in"
#define PACKAGE_NAME		"jack-keyboard"
#define PACKAGE_VERSION		"2.7.1"

jack_port_t	*output_port;
jack_port_t	*input_port;
int		entered_number = -1;
int		allow_connecting_to_own_kind = 0;
int		enable_gui = 1;
int		grab_keyboard_at_startup = 0;
volatile int	keyboard_grabbed = 0;
int		enable_window_title = 0;
int		time_offsets_are_zero = 0;
int		send_program_change_at_reconnect = 0;
int		send_program_change_once = 0;
int		program_change_was_sent = 0;
int		velocity_high = VELOCITY_HIGH;
int		velocity_normal = VELOCITY_NORMAL;
int		velocity_low = VELOCITY_LOW;
int		*current_velocity = &velocity_normal;
int		octave = 4;
double		rate_limit = 0.0;
jack_client_t	*jack_client = NULL;

#ifdef HAVE_LASH
lash_client_t	*lash_client;
#endif

#define MIDI_NOTE_ON		0x90
#define MIDI_NOTE_OFF		0x80
#define MIDI_PROGRAM_CHANGE	0xC0
#define MIDI_CONTROLLER		0xB0
#define MIDI_RESET		0xFF
#define MIDI_HOLD_PEDAL		64
#define MIDI_ALL_SOUND_OFF	120
#define MIDI_ALL_MIDI_CONTROLLERS_OFF	121
#define MIDI_ALL_NOTES_OFF	123
#define MIDI_BANK_SELECT_MSB	0
#define MIDI_BANK_SELECT_LSB	32

#define BANK_MIN		0
#define BANK_MAX		127
#define PROGRAM_MIN		0
#define PROGRAM_MAX		127
#define CHANNEL_MIN		1
#define CHANNEL_MAX		16

GtkWidget	*window, *sustain_button, *channel_spin, *bank_spin, *program_spin, *connected_to_combo,
		*velocity_hscale, *grab_keyboard_checkbutton, *octave_spin;
PianoKeyboard	*keyboard;
GtkListStore	*connected_to_store;

#ifdef HAVE_X11
Display		*dpy;
#endif

struct MidiMessage {
	jack_nframes_t	time;
	int		len;	/* Length of MIDI message, in bytes. */
	unsigned char	data[3];
};

#define RINGBUFFER_SIZE		1024*sizeof(struct MidiMessage)

/* Will emit a warning if time between jack callbacks is longer than this. */
#define MAX_TIME_BETWEEN_CALLBACKS	0.1

/* Will emit a warning if execution of jack callback takes longer than this. */
#define MAX_PROCESSING_TIME	0.01

jack_ringbuffer_t *ringbuffer;

/* Number of currently used program. */
int		program = 0;

/* Number of currently selected bank. */
int		bank = 0;

/* Number of currently selected channel (0..15). */
int		channel = 0;

void draw_note(int key);
void queue_message(struct MidiMessage *ev);

double 
get_time(void)
{
	double seconds;
	int ret;
	struct timeval tv;

	ret = gettimeofday(&tv, NULL);

	if (ret) {
		perror("gettimeofday");
		exit(EX_OSERR);
	}

	seconds = tv.tv_sec + tv.tv_usec / 1000000.0;

	return (seconds);
}

double
get_delta_time(void)
{
	static double previously = -1.0;
	double now;
	double delta;

	now = get_time();

	if (previously == -1.0) {
		previously = now;

		return (0);
	}

	delta = now - previously;
	previously = now;

	assert(delta >= 0.0);

	return (delta);
}

gboolean
process_received_message_async(gpointer evp)
{
	int i;
	struct MidiMessage *ev = (struct MidiMessage *)evp;
	int b0 = ev->data[0];
	int b1 = ev->data[1];

	/* Strip channel from channel messages */
	if (b0 >= 0x80 && b0 <= 0xEF)
		b0 = b0 & 0xF0;

	if (b0 == MIDI_RESET || (b0 == MIDI_CONTROLLER &&
		(b1 == MIDI_ALL_NOTES_OFF || b1 == MIDI_ALL_SOUND_OFF))) {

		for (i = 0; i < NNOTES; i++) {
			piano_keyboard_set_note_off(keyboard, i);
		}
	}

	if (b0 == MIDI_NOTE_ON) {
		if (ev->data[2] == 0)
			piano_keyboard_set_note_off(keyboard, ev->data[1]);
		else
			piano_keyboard_set_note_on(keyboard, ev->data[1]);
	}

	if (b0 == MIDI_NOTE_OFF) {
		piano_keyboard_set_note_off(keyboard, ev->data[1]);
	}

	ev->data [0] = b0 | channel;
	queue_message(ev);

	return (FALSE);
}

struct MidiMessage *
midi_message_from_midi_event(jack_midi_event_t event)
{
	struct MidiMessage *ev = malloc(sizeof(*ev));

	if (ev == NULL) {
		perror("malloc");
		return (NULL);
	}

	assert(event.size >= 1 && event.size <= 3);

	ev->len = event.size;
	ev->time = event.time;

	memcpy(ev->data, event.buffer, ev->len);

	return (ev);
}

gboolean
warning_async(gpointer s)
{
	const char *str = (const char *)s;

	g_warning("%s", str);

	return (FALSE);
}

void
warn_from_jack_thread_context(const char *str)
{
	g_idle_add(warning_async, (gpointer)str);
}

void
process_midi_input(jack_nframes_t nframes)
{
	int read, events, i;
	void *port_buffer;
	jack_midi_event_t event;

	port_buffer = jack_port_get_buffer(input_port, nframes);
	if (port_buffer == NULL) {
		warn_from_jack_thread_context("jack_port_get_buffer failed, cannot receive anything.");
		return;
	}

#ifdef JACK_MIDI_NEEDS_NFRAMES
	events = jack_midi_get_event_count(port_buffer, nframes);
#else
	events = jack_midi_get_event_count(port_buffer);
#endif

	for (i = 0; i < events; i++) {
		struct MidiMessage *rev;

#ifdef JACK_MIDI_NEEDS_NFRAMES
		read = jack_midi_event_get(&event, port_buffer, i, nframes);
#else
		read = jack_midi_event_get(&event, port_buffer, i);
#endif
		if (read) {
			warn_from_jack_thread_context("jack_midi_event_get failed, RECEIVED NOTE LOST.");
			continue;
		}

		if (event.size > 3) {
			warn_from_jack_thread_context("Ignoring MIDI message longer than three bytes, probably a SysEx.");
			continue;
		}

		rev = midi_message_from_midi_event(event);
		if (rev == NULL) {
			warn_from_jack_thread_context("midi_message_from_midi_event failed, RECEIVED NOTE LOST.");
			continue;
		}

		g_idle_add(process_received_message_async, rev);
	}
}

double
nframes_to_ms(jack_nframes_t nframes)
{
	jack_nframes_t sr;

	sr = jack_get_sample_rate(jack_client);

	assert(sr > 0);

	return ((nframes * 1000.0) / (double)sr);
}

void
process_midi_output(jack_nframes_t nframes)
{
	int read, t, bytes_remaining;
	unsigned char *buffer;
	void *port_buffer;
	jack_nframes_t last_frame_time;
	struct MidiMessage ev;

	last_frame_time = jack_last_frame_time(jack_client);

	port_buffer = jack_port_get_buffer(output_port, nframes);
	if (port_buffer == NULL) {
		warn_from_jack_thread_context("jack_port_get_buffer failed, cannot send anything.");
		return;
	}

#ifdef JACK_MIDI_NEEDS_NFRAMES
	jack_midi_clear_buffer(port_buffer, nframes);
#else
	jack_midi_clear_buffer(port_buffer);
#endif

	/* We may push at most one byte per 0.32ms to stay below 31.25 Kbaud limit. */
	bytes_remaining = nframes_to_ms(nframes) * rate_limit;

	while (jack_ringbuffer_read_space(ringbuffer)) {
		read = jack_ringbuffer_peek(ringbuffer, (char *)&ev, sizeof(ev));

		if (read != sizeof(ev)) {
			warn_from_jack_thread_context("Short read from the ringbuffer, possible note loss.");
			jack_ringbuffer_read_advance(ringbuffer, read);
			continue;
		}

		bytes_remaining -= ev.len;

		if (rate_limit > 0.0 && bytes_remaining <= 0) {
			warn_from_jack_thread_context("Rate limiting in effect.");
			break;
		}

		t = ev.time + nframes - last_frame_time;

		/* If computed time is too much into the future, we'll need
		   to send it later. */
		if (t >= (int)nframes)
			break;

		/* If computed time is < 0, we missed a cycle because of xrun. */
		if (t < 0)
			t = 0;

		if (time_offsets_are_zero)
			t = 0;

		jack_ringbuffer_read_advance(ringbuffer, sizeof(ev));

#ifdef JACK_MIDI_NEEDS_NFRAMES
		buffer = jack_midi_event_reserve(port_buffer, t, ev.len, nframes);
#else
		buffer = jack_midi_event_reserve(port_buffer, t, ev.len);
#endif

		if (buffer == NULL) {
			warn_from_jack_thread_context("jack_midi_event_reserve failed, NOTE LOST.");
			break;
		}

		memcpy(buffer, ev.data, ev.len);
	}
}

int 
process_callback(jack_nframes_t nframes, void *notused)
{
#ifdef MEASURE_TIME
	if (get_delta_time() > MAX_TIME_BETWEEN_CALLBACKS)
		warn_from_jack_thread_context("Had to wait too long for JACK callback; scheduling problem?");
#endif

	/* Check for impossible condition that actually happened to me, caused by some problem between jackd and OSS4. */
	if (nframes <= 0) {
		warn_from_jack_thread_context("Process callback called with nframes = 0; bug in JACK?");
		return 0;
	}

	process_midi_input(nframes);
	process_midi_output(nframes);

#ifdef MEASURE_TIME
	if (get_delta_time() > MAX_PROCESSING_TIME)
		warn_from_jack_thread_context("Processing took too long; scheduling problem?");
#endif

	return (0);
}

void
queue_message(struct MidiMessage *ev)
{
	int written;

	if (jack_ringbuffer_write_space(ringbuffer) < sizeof(*ev)) {
		g_critical("Not enough space in the ringbuffer, NOTE LOST.");
		return;
	}

	written = jack_ringbuffer_write(ringbuffer, (char *)ev, sizeof(*ev));

	if (written != sizeof(*ev))
		g_warning("jack_ringbuffer_write failed, NOTE LOST.");
}

void 
queue_new_message(int b0, int b1, int b2)
{
	struct MidiMessage ev;

	/* For MIDI messages that specify a channel number, filter the original
	   channel number out and add our own. */
	if (b0 >= 0x80 && b0 <= 0xEF) {
		b0 &= 0xF0;
		b0 += channel;
	}

	if (b1 == -1) {
		ev.len = 1;
		ev.data[0] = b0;

	} else if (b2 == -1) {
		ev.len = 2;
		ev.data[0] = b0;
		ev.data[1] = b1;

	} else {
		ev.len = 3;
		ev.data[0] = b0;
		ev.data[1] = b1;
		ev.data[2] = b2;
	}

	ev.time = jack_frame_time(jack_client);

	queue_message(&ev);
}

gboolean
update_connected_to_combo_async(gpointer notused)
{
	int i, count = 0;
	const char **connected, **available, *my_name;
	GtkTreeIter iter;

	if (jack_client == NULL || output_port == NULL)
		return (FALSE);

	connected = jack_port_get_connections(output_port);
	available = jack_get_ports(jack_client, NULL, JACK_DEFAULT_MIDI_TYPE, JackPortIsInput);
	my_name = jack_port_name(input_port);

	assert(my_name);

	/* There will be at least one listening MIDI port - the one we create. */
	assert(available);

	if (available != NULL) {
		gtk_list_store_clear(connected_to_store);

		for (i = 0; available[i] != NULL; i++) {
			if (!strcmp(available[i], my_name) || (!allow_connecting_to_own_kind &&
			    !strncmp(available[i], my_name, strlen(PACKAGE_NAME))))

				continue;

			count++;

			gtk_list_store_append(connected_to_store, &iter);
			gtk_list_store_set(connected_to_store, &iter, 0, available[i], -1);

			if (connected != NULL && connected[0] != NULL && !strcmp(available[i], connected[0]))
				gtk_combo_box_set_active_iter(GTK_COMBO_BOX(connected_to_combo), &iter);
		}
	}

	if (count > 0)
		gtk_widget_set_sensitive(connected_to_combo, TRUE);
	else
		gtk_widget_set_sensitive(connected_to_combo, FALSE);

	if (connected != NULL)
		free(connected);

	free(available);

	return (FALSE);
}

void 
draw_window_title(void)
{
	int i, off = 0;
	char title[256];
	const char **connected_ports;

	if (window == NULL)
		return;

	if (enable_window_title && jack_client != NULL) {
		connected_ports = jack_port_get_connections(output_port);

		off += snprintf(title, sizeof(title) - off, "%s: channel %d, bank %d, program %d",
		    jack_get_client_name(jack_client), channel + 1, bank, program);

		if (!program_change_was_sent)
			off += snprintf(title + off, sizeof(title) - off, " (bank/program change not sent)");

		if (connected_ports == NULL || connected_ports[0] == NULL) {
			off += snprintf(title + off, sizeof(title) - off, ", NOT CONNECTED");

		} else {
			off += snprintf(title + off, sizeof(title) - off, ", connected to ");

			for (i = 0; connected_ports[i] != NULL; i++) {
				off += snprintf(title + off, sizeof(title) - off, "%s%s",
				    i == 0 ? "" : ", ", connected_ports[i]);
			}
		}

		if (connected_ports != NULL)
			free(connected_ports);

		gtk_window_set_title(GTK_WINDOW(window), title);
	} else {
		/* May be null if JACK is not initialized yet. */
		if (jack_client != NULL)
			gtk_window_set_title(GTK_WINDOW(window), jack_get_client_name(jack_client));
		else
			gtk_window_set_title(GTK_WINDOW(window), PACKAGE_NAME);
	}
}

gboolean
update_window_title_async(gpointer notused)
{
	if (window != NULL)
		draw_window_title();

	return (FALSE);
}

int 
graph_order_callback(void *notused)
{
	g_idle_add(update_window_title_async, NULL);
	g_idle_add(update_connected_to_combo_async, NULL);

	return (0);
}

void
send_program_change(void)
{
	if (jack_port_connected(output_port) == 0)
		return;

	queue_new_message(MIDI_CONTROLLER, MIDI_BANK_SELECT_LSB, bank % 128);
	queue_new_message(MIDI_CONTROLLER, MIDI_BANK_SELECT_MSB, bank / 128);
	queue_new_message(MIDI_PROGRAM_CHANGE, program, -1);

	program_change_was_sent = 1;
}

/* Connects to the specified input port, disconnecting already connected ports. */
int
connect_to_input_port(const char *port)
{
        int ret;

	if (!strcmp(port, jack_port_name(input_port)))
		return (-1);

	if (!allow_connecting_to_own_kind) {
		if (!strncmp(port, jack_port_name(input_port), strlen(PACKAGE_NAME)))
			return (-2);
	}

        ret = jack_port_disconnect(jack_client, output_port);
        if (ret) {
                g_warning("Cannot disconnect MIDI port.");

                return (-3);
        }

        ret = jack_connect(jack_client, jack_port_name(output_port), port);
        if (ret) {
                g_warning("Cannot connect to %s.", port);

                return (-4);
        } 

	g_warning("Connected to %s.", port);

	program_change_was_sent = 0;

	if (send_program_change_at_reconnect || send_program_change_once) {
		send_program_change();
		draw_window_title();
	}

	send_program_change_once = 0;

	return (0);
}

void
connect_to_another_input_port(int up_not_down)
{
	const char **available_midi_ports;
	const char **connected_ports;
	const char *current = NULL;
	int i, max, current_index;

	available_midi_ports = jack_get_ports(jack_client, NULL, JACK_DEFAULT_MIDI_TYPE, JackPortIsInput);

	/* There will be at least one listening MIDI port - the one we create. */
	assert(available_midi_ports);

	/*
	 * max is the highest possible index into available_midi_ports[],
	 * i.e. number of elements - 1.
	 */
	for (max = 0; available_midi_ports[max + 1] != NULL; max++);

	/* Only one input port - our own. */
	if (max == 0) {
		g_warning("No listening JACK MIDI input ports found.  "
		    "Run some softsynth and press Insert or Delete key.");

		return;
	}

	connected_ports = jack_port_get_connections(output_port);

	if (connected_ports != NULL && connected_ports[0] != NULL)
		current = connected_ports[0];
	else
		current = available_midi_ports[0];

	/*
	 * current is the index of currently connected port into available_midi_ports[].
	 */
	for (i = 0; i <= max && strcmp(available_midi_ports[i], current); i++);

	current_index = i;

	assert(!strcmp(available_midi_ports[current_index], current));

	/* XXX: rewrite. */
	if (up_not_down) {
		for (i = current_index + 1; i <= max; i++) {
			assert(available_midi_ports[i] != NULL);

			if (connect_to_input_port(available_midi_ports[i]) == 0)
				goto connected;
		}

		for (i = 0; i <= current_index; i++) {
			assert(available_midi_ports[i] != NULL);

			if (connect_to_input_port(available_midi_ports[i]) == 0)
				goto connected;
		}

	} else {
		for (i = current_index - 1; i >= 0; i--) {
			assert(available_midi_ports[i] != NULL);

			if (connect_to_input_port(available_midi_ports[i]) == 0)
				goto connected;
		}

		for (i = max; i >= current_index; i--) {
			assert(available_midi_ports[i] != NULL);

			if (connect_to_input_port(available_midi_ports[i]) == 0)
				goto connected;
		}
	}

	g_warning("Cannot connect to any of the input ports.");

connected:
	free(available_midi_ports);

	if (connected_ports != NULL)
		free(connected_ports);
}

void 
connect_to_next_input_port(void)
{
	connect_to_another_input_port(1);
}

void 
connect_to_prev_input_port(void)
{
	connect_to_another_input_port(0);
}

void 
init_jack(void)
{
	int err;

#ifdef HAVE_LASH
	lash_event_t *event;
#endif

	jack_client = jack_client_open(PACKAGE_NAME, JackNoStartServer, NULL);

	if (jack_client == NULL) {
		g_critical("Could not connect to the JACK server; run jackd first?");
		exit(EX_UNAVAILABLE);
	}

	ringbuffer = jack_ringbuffer_create(RINGBUFFER_SIZE);

	if (ringbuffer == NULL) {
		g_critical("Cannot create JACK ringbuffer.");
		exit(EX_SOFTWARE);
	}

	jack_ringbuffer_mlock(ringbuffer);

#ifdef HAVE_LASH
	event = lash_event_new_with_type(LASH_Client_Name);
	assert (event); /* Documentation does not say anything about return value. */
	lash_event_set_string(event, jack_get_client_name(jack_client));
	lash_send_event(lash_client, event);

	lash_jack_client_name(lash_client, jack_get_client_name(jack_client));
#endif

	err = jack_set_process_callback(jack_client, process_callback, 0);
	if (err) {
		g_critical("Could not register JACK process callback.");
		exit(EX_UNAVAILABLE);
	}

	err = jack_set_graph_order_callback(jack_client, graph_order_callback, 0);
	if (err) {
		g_critical("Could not register JACK graph order callback.");
		exit(EX_UNAVAILABLE);
	}

	output_port = jack_port_register(jack_client, OUTPUT_PORT_NAME, JACK_DEFAULT_MIDI_TYPE,
		JackPortIsOutput, 0);

	if (output_port == NULL) {
		g_critical("Could not register JACK output port.");
		exit(EX_UNAVAILABLE);
	}

	input_port = jack_port_register(jack_client, INPUT_PORT_NAME, JACK_DEFAULT_MIDI_TYPE,
		JackPortIsInput, 0);

	if (input_port == NULL) {
		g_critical("Could not register JACK input port.");
		exit(EX_UNAVAILABLE);
	}

	if (jack_activate(jack_client)) {
		g_critical("Cannot activate JACK client.");
		exit(EX_UNAVAILABLE);
	}
}

#ifdef HAVE_LASH

void
load_config_from_lash(void)
{
	lash_config_t *config;
	const char *key;
	int value;

	while ((config = lash_get_config(lash_client))) {
		
		key = lash_config_get_key(config);
		value = lash_config_get_value_int(config);

		if (!strcmp(key, "channel")) {
			if (value < CHANNEL_MIN || value > CHANNEL_MAX) {
				g_warning("Bad value '%d' for 'channel' property received from LASH.", value);
			} else {
				gtk_spin_button_set_value(GTK_SPIN_BUTTON(channel_spin), value);
			}

		} else if (!strcmp(key, "bank")) {
			if (value < BANK_MIN || value > BANK_MAX) {
				g_warning("Bad value '%d' for 'bank' property received from LASH.", value);
			} else {
				gtk_spin_button_set_value(GTK_SPIN_BUTTON(bank_spin), value);
			}

		} else if (!strcmp(key, "program")) {
			if (value < PROGRAM_MIN || value > PROGRAM_MAX) {
				g_warning("Bad value '%d' for 'program' property received from LASH.", value);
			} else {
				gtk_spin_button_set_value(GTK_SPIN_BUTTON(program_spin), value);
			}

		} else if (!strcmp(key, "keyboard_grabbed")) {
			if (value < 0 || value > 1) {
				g_warning("Bad value '%d' for 'keyboard_grabbed' property received from LASH.", value);
			} else {
				gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(grab_keyboard_checkbutton), value);
			}

		} else if (!strcmp(key, "octave")) {
			if (value < OCTAVE_MIN || value > OCTAVE_MAX) {
				g_warning("Bad value '%d' for 'octave' property received from LASH.", value);
			} else {
				gtk_spin_button_set_value(GTK_SPIN_BUTTON(octave_spin), value);
			}

		} else if (!strcmp(key, "velocity_normal")) {
			if (value < VELOCITY_MIN || value > VELOCITY_MAX) {
				g_warning("Bad value '%d' for 'velocity_normal' property received from LASH.", value);
			} else {
		                velocity_normal = value;
				gtk_range_set_value(GTK_RANGE(velocity_hscale), *current_velocity);
			}

		} else if (!strcmp(key, "velocity_high")) {
			if (value < VELOCITY_MIN || value > VELOCITY_MAX) {
				g_warning("Bad value '%d' for 'velocity_high' property received from LASH.", value);
			} else {
		                velocity_high = value;
				gtk_range_set_value(GTK_RANGE(velocity_hscale), *current_velocity);
			}

		} else if (!strcmp(key, "velocity_low")) {
			if (value < VELOCITY_MIN || value > VELOCITY_MAX) {
				g_warning("Bad value '%d' for 'velocity_low' property received from LASH.", value);
			} else {
		                velocity_low = value;
				gtk_range_set_value(GTK_RANGE(velocity_hscale), *current_velocity);
			}

		} else {
			g_warning("Received unknown config key '%s' (value '%d') from LASH.", key, value);
		}

		lash_config_destroy(config);
	}
}

void
save_config_int(const char *name, int value)
{
	lash_config_t *config;

	config = lash_config_new_with_key(name);
	lash_config_set_value_int(config, value);
	lash_send_config(lash_client, config);
}

void
save_config_into_lash(void)
{
	save_config_int("channel", channel + 1);
	save_config_int("bank", bank);
	save_config_int("program", program);
	save_config_int("keyboard_grabbed", keyboard_grabbed);
	save_config_int("octave", octave);
	save_config_int("velocity_normal", velocity_normal);
	save_config_int("velocity_high", velocity_high);
	save_config_int("velocity_low", velocity_low);
}

gboolean
lash_callback(gpointer notused)
{
	lash_event_t *event;

	while ((event = lash_get_event(lash_client))) {
		switch (lash_event_get_type(event)) {
		case LASH_Restore_Data_Set:
			load_config_from_lash();
			lash_send_event(lash_client, event);
			break;

		case LASH_Save_Data_Set:
			save_config_into_lash();
			lash_send_event(lash_client, event);
			break;

		case LASH_Quit:
			g_warning("Exiting due to LASH request.");
			exit(EX_OK);
			break;

		default:
			g_warning("Receieved unknown LASH event of type %d.", lash_event_get_type(event));
			lash_event_destroy(event);
		}
	}

	return (TRUE);
}

void
init_lash(lash_args_t *args)
{
	/* XXX: Am I doing the right thing wrt protocol version? */
	lash_client = lash_init(args, PACKAGE_NAME, LASH_Config_Data_Set, LASH_PROTOCOL(2, 0));

	if (!lash_server_connected(lash_client)) {
		fprintf(stderr, "jack-keyboard: Cannot initialize LASH.  Continuing anyway.\n");
		/* exit(EX_UNAVAILABLE); */

		return;
	}

	/* Schedule a function to process LASH events, ten times per second. */
	g_timeout_add(100, lash_callback, NULL);
}

#endif /* HAVE_LASH */

gboolean
sustain_event_handler(GtkToggleButton *widget, gpointer pressed)
{
	if (pressed) {
		gtk_toggle_button_set_active(widget, TRUE);
		piano_keyboard_sustain_press(keyboard);
	} else {
		gtk_toggle_button_set_active(widget, FALSE);
		piano_keyboard_sustain_release(keyboard);
	}

	return (FALSE);
}

void
channel_event_handler(GtkSpinButton *spinbutton, gpointer notused)
{
	channel = gtk_spin_button_get_value(spinbutton) - 1;

	draw_window_title();
}

void
bank_event_handler(GtkSpinButton *spinbutton, gpointer notused)
{
	bank = gtk_spin_button_get_value(spinbutton);

	send_program_change();
	draw_window_title();
}

void
program_event_handler(GtkSpinButton *spinbutton, gpointer notused)
{
	program = gtk_spin_button_get_value(spinbutton);

	send_program_change();
	draw_window_title();
}

void
connected_to_event_handler(GtkComboBox *widget, gpointer notused)
{
	GtkTreeIter iter;
	gchar *connect_to;
	const char **connected_ports;

	if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(connected_to_combo), &iter) == FALSE)
		return;

	gtk_tree_model_get(GTK_TREE_MODEL(connected_to_store), &iter, 0, &connect_to, -1);

	connected_ports = jack_port_get_connections(output_port);
	if (connected_ports != NULL && connected_ports[0] != NULL && !strcmp(connect_to, connected_ports[0])) {
		free(connected_ports);
		return;
	}

	connect_to_input_port(connect_to);

	free(connected_ports);
}

void
velocity_event_handler(GtkRange *range, gpointer notused)
{
	assert(current_velocity);

	*current_velocity = gtk_range_get_value(range);
}

#ifdef HAVE_X11

int grab_x_error_handler(Display *dpy, XErrorEvent *notused) 
{
	keyboard_grabbed = 0;

	return (42);  /* Returned value is ignored. */
}

GdkFilterReturn
keyboard_grab_filter(GdkXEvent *xevent, GdkEvent *event, gpointer notused)
{
	XEvent *xe;
	XKeyEvent *xke;

	xe = (XEvent *)xevent;

	if (xe->type != KeyPress && xe->type != KeyRelease)
		return (GDK_FILTER_CONTINUE);

	xke = (XKeyEvent *)xevent;

	/* Lie to GDK, pretending we are the proper recipient of this XEvent.  Without it,
	   GDK would discard it. */
	xke->window = GDK_WINDOW_XWINDOW(window->window);

	return (GDK_FILTER_CONTINUE);
}

void
ungrab_keyboard(void)
{
	static int (*standard_x_error_handler)(Display *dpy, XErrorEvent *notused);

	standard_x_error_handler = XSetErrorHandler(grab_x_error_handler);

	XUngrabKey(dpy, AnyKey, AnyModifier, GDK_ROOT_WINDOW());
	XSync(dpy, FALSE);

	XSetErrorHandler(standard_x_error_handler);

	keyboard_grabbed = 0;
}

void
grab_keyboard(void)
{
	int i;
	static int (*standard_x_error_handler)(Display *dpy, XErrorEvent *notused);

	KeySym keys_to_grab[] = {
		XK_1, XK_2, XK_3, XK_4, XK_5, XK_6, XK_7, XK_8, XK_9, XK_0, XK_minus, XK_equal,
		XK_q, XK_w, XK_e, XK_r, XK_t, XK_y, XK_u, XK_i, XK_o, XK_p,
		XK_a, XK_s, XK_d, XK_f, XK_g, XK_h, XK_j, XK_k, XK_l,
		XK_z, XK_x, XK_c, XK_v, XK_b, XK_n, XK_m,
		XK_Shift_L, XK_Shift_R, XK_Control_L, XK_Control_R, XK_space, 
		XK_Insert, XK_Delete, XK_Home, XK_End, XK_Page_Up, XK_Page_Down,
		XK_KP_1, XK_KP_2, XK_KP_3, XK_KP_4, XK_KP_5, XK_KP_6, XK_KP_7, XK_KP_8, XK_KP_9, XK_KP_0,
		XK_Num_Lock, XK_KP_Multiply, XK_KP_Divide, XK_KP_Subtract, XK_KP_Add, XK_KP_Enter,
		XK_Escape,

		/* End of list. */
		XK_VoidSymbol};

	if (keyboard_grabbed)
		return;

	dpy = GDK_WINDOW_XDISPLAY(window->window);

	keyboard_grabbed = 1;

	standard_x_error_handler = XSetErrorHandler(grab_x_error_handler);

	for (i = 0; keys_to_grab[i] != XK_VoidSymbol; i++) {
		KeyCode kc = XKeysymToKeycode(dpy, keys_to_grab[i]);

		if (kc == 0) {
			g_critical("XKeysymToKeycode failed.  Please report this to the author.");
			continue;
		}

		XGrabKey(dpy, kc, 0, GDK_ROOT_WINDOW(), True, GrabModeAsync, GrabModeAsync);
		XGrabKey(dpy, kc, ShiftMask, GDK_ROOT_WINDOW(), True, GrabModeAsync, GrabModeAsync);
		XGrabKey(dpy, kc, ControlMask, GDK_ROOT_WINDOW(), True, GrabModeAsync, GrabModeAsync);
		XGrabKey(dpy, kc, ShiftMask | ControlMask, GDK_ROOT_WINDOW(), True, GrabModeAsync, GrabModeAsync);
	}

	XSync(dpy, FALSE);

	XSetErrorHandler(standard_x_error_handler);

	if (keyboard_grabbed == 0) {
                g_critical("XGrabKey() failed; keyboard grabbing not possible.  "
				"Maybe some other application grabbed the keyboard?");
		/* Make sure we don't keep any keys grabbed.  Only one of XGrabKey invocations failed, others might have been successfull. */
		ungrab_keyboard();
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(grab_keyboard_checkbutton), keyboard_grabbed);

		return;
	}

	g_atexit(ungrab_keyboard);

	gdk_window_add_filter(NULL, keyboard_grab_filter, NULL);
}

#else /* ! HAVE_X11 */

void
ungrab_keyboard(void)
{
}

void
grab_keyboard(void)
{
	g_critical("Compiled without XGrabKey support; keyboard grabbing not possible.");
}

#endif /* ! HAVE_X11 */

void
grab_keyboard_handler(GtkToggleButton *togglebutton, gpointer notused)
{
	gboolean active = gtk_toggle_button_get_active(togglebutton);

	if (active)
		grab_keyboard();
	else
		ungrab_keyboard();
}

void
octave_event_handler(GtkSpinButton *spinbutton, gpointer notused)
{
	octave = gtk_spin_button_get_value(spinbutton);
	piano_keyboard_set_octave(keyboard, octave);
}

void 
panic(void)
{
	int i;

	/*
	 * These two have to be sent first, in case we have no room in the
	 * ringbuffer for all these MIDI_NOTE_OFF messages sent five lines below.
	 */
	queue_new_message(MIDI_CONTROLLER, MIDI_ALL_NOTES_OFF, 0);
	queue_new_message(MIDI_CONTROLLER, MIDI_ALL_SOUND_OFF, 0);

	for (i = 0; i < NNOTES; i++) {
		queue_new_message(MIDI_NOTE_OFF, i, 0);
		piano_keyboard_set_note_off(keyboard, i);
		usleep(100);
	}

	queue_new_message(MIDI_CONTROLLER, MIDI_HOLD_PEDAL, 0);
	queue_new_message(MIDI_CONTROLLER, MIDI_ALL_MIDI_CONTROLLERS_OFF, 0);
	queue_new_message(MIDI_CONTROLLER, MIDI_ALL_NOTES_OFF, 0);
	queue_new_message(MIDI_CONTROLLER, MIDI_ALL_SOUND_OFF, 0);
	queue_new_message(MIDI_RESET, -1, -1);
}

void
add_digit(int digit)
{
	if (entered_number == -1)
		entered_number = 0;
	else
		entered_number *= 10;

	entered_number += digit;
}

int
maybe_add_digit(GdkEventKey *event)
{
	/*
	 * User can enter a number from the keypad; after that, pressing
	 * '*'/'/', Page Up/Page Down etc will set program/bank/whatever
	 * to the number that was entered.
	 */
	/*
	 * XXX: This is silly.  Find a way to enter the number without
	 * all these contitional instructions.
	 */
	if (event->keyval == GDK_KP_0 || event->keyval == GDK_KP_Insert) {
		if (event->type == GDK_KEY_PRESS)
			add_digit(0);

		return (TRUE);
	}

	if (event->keyval == GDK_KP_1 || event->keyval == GDK_KP_End) {
		if (event->type == GDK_KEY_PRESS)
			add_digit(1);

		return (TRUE);
	}

	if (event->keyval == GDK_KP_2 || event->keyval == GDK_KP_Down) {
		if (event->type == GDK_KEY_PRESS)
			add_digit(2);

		return (TRUE);
	}

	if (event->keyval == GDK_KP_3 || event->keyval == GDK_KP_Page_Down) {
		if (event->type == GDK_KEY_PRESS)
			add_digit(3);

		return (TRUE);
	}

	if (event->keyval == GDK_KP_4 || event->keyval == GDK_KP_Left) {
		if (event->type == GDK_KEY_PRESS)
			add_digit(4);

		return (TRUE);
	}

	if (event->keyval == GDK_KP_5 || event->keyval == GDK_KP_Begin) {
		if (event->type == GDK_KEY_PRESS)
			add_digit(5);

		return (TRUE);
	}

	if (event->keyval == GDK_KP_6 || event->keyval == GDK_KP_Right) {
		if (event->type == GDK_KEY_PRESS)
			add_digit(6);

		return (TRUE);
	}

	if (event->keyval == GDK_KP_7 || event->keyval == GDK_KP_Home) {
		if (event->type == GDK_KEY_PRESS)
			add_digit(7);

		return (TRUE);
	}

	if (event->keyval == GDK_KP_8 || event->keyval == GDK_KP_Up) {
		if (event->type == GDK_KEY_PRESS)
			add_digit(8);

		return (TRUE);
	}

	if (event->keyval == GDK_KP_9 || event->keyval == GDK_KP_Page_Up) {
		if (event->type == GDK_KEY_PRESS)
			add_digit(9);

		return (TRUE);
	}

	return (FALSE);
}

int
get_entered_number(void)
{
	int tmp;

	tmp = entered_number;

	entered_number = -1;

	return (tmp);
}

int
clip(int val, int lo, int hi)
{
	if (val < lo)
		val = lo;

	if (val > hi)
		val = hi;

	return (val);
}

gint 
keyboard_event_handler(GtkWidget *widget, GdkEventKey *event, gpointer notused)
{
	int tmp;
	gboolean retval = FALSE;

	/* Pass signal to piano_keyboard widget.  Is there a better way to do this? */
	if (event->type == GDK_KEY_PRESS)
		g_signal_emit_by_name(keyboard, "key-press-event", event, &retval);
	else
		g_signal_emit_by_name(keyboard, "key-release-event", event, &retval);

	if (retval)
		return (TRUE);


	if (maybe_add_digit(event))
		return (TRUE);

	/*
	 * '+' key shifts octave up. '-' key shifts octave down.
	 */
	if (event->keyval == GDK_KP_Add || event->keyval  == GDK_equal) {
		if (event->type == GDK_KEY_PRESS && octave < OCTAVE_MAX)
			gtk_spin_button_set_value(GTK_SPIN_BUTTON(octave_spin), octave + 1);

		return (TRUE);
	}

	if (event->keyval == GDK_KP_Subtract || event->keyval == GDK_minus) {
		if (event->type == GDK_KEY_PRESS && octave > OCTAVE_MIN)
			gtk_spin_button_set_value(GTK_SPIN_BUTTON(octave_spin), octave - 1);

		return (TRUE);
	}

	/*
	 * '*' character increases program number. '/' character decreases it.
	 */
	if (event->keyval == GDK_KP_Multiply) {
		if (event->type == GDK_KEY_PRESS) {

			tmp = get_entered_number();

			if (tmp < 0)
				tmp = gtk_spin_button_get_value(GTK_SPIN_BUTTON(program_spin)) + 1;

			gtk_spin_button_set_value(GTK_SPIN_BUTTON(program_spin), clip(tmp, PROGRAM_MIN, PROGRAM_MAX));
		}

		return (TRUE);
	}

	if (event->keyval == GDK_KP_Divide) {
		if (event->type == GDK_KEY_PRESS) {

			tmp = get_entered_number();

			if (tmp < 0)
				tmp = gtk_spin_button_get_value(GTK_SPIN_BUTTON(program_spin)) - 1;

			gtk_spin_button_set_value(GTK_SPIN_BUTTON(program_spin), clip(tmp, PROGRAM_MIN, PROGRAM_MAX));
		}

		return (TRUE);
	}

	/*
	 * PgUp key increases bank number, PgDown decreases it.
	 */
	if (event->keyval == GDK_Page_Up) {
		if (event->type == GDK_KEY_PRESS) {

			tmp = get_entered_number();

			if (tmp < 0)
				tmp = gtk_spin_button_get_value(GTK_SPIN_BUTTON(bank_spin)) + 1;

			gtk_spin_button_set_value(GTK_SPIN_BUTTON(bank_spin), clip(tmp, BANK_MIN, BANK_MAX));
		}

		return (TRUE);
	}

	if (event->keyval == GDK_Page_Down) {
		if (event->type == GDK_KEY_PRESS) {

			tmp = get_entered_number();

			if (tmp < 0)
				tmp = gtk_spin_button_get_value(GTK_SPIN_BUTTON(bank_spin)) - 1;

			gtk_spin_button_set_value(GTK_SPIN_BUTTON(bank_spin), clip(tmp, BANK_MIN, BANK_MAX));
		}

		return (TRUE);
	}

	/*
	 * Home key increases channel number, End decreases it.
	 */
	if (event->keyval == GDK_Home) {
		if (event->type == GDK_KEY_PRESS) {

			tmp = get_entered_number();

			if (tmp < 0)
				tmp = gtk_spin_button_get_value(GTK_SPIN_BUTTON(channel_spin)) + 1;

			gtk_spin_button_set_value(GTK_SPIN_BUTTON(channel_spin), clip(tmp, CHANNEL_MIN, CHANNEL_MAX));
		}

		return (TRUE);
	}

	if (event->keyval == GDK_End) {
		if (event->type == GDK_KEY_PRESS) {

			tmp = get_entered_number();

			if (tmp < 0)
				tmp = gtk_spin_button_get_value(GTK_SPIN_BUTTON(channel_spin)) - 1;

			gtk_spin_button_set_value(GTK_SPIN_BUTTON(channel_spin), clip(tmp, CHANNEL_MIN, CHANNEL_MAX));
		}

		return (TRUE);
	}

	/*
	 * Insert key connects to the next input port.  Delete connects to the previous one.
	 */
	if (event->keyval == GDK_Insert) {
		if (event->type == GDK_KEY_PRESS)
			connect_to_next_input_port();

		return (TRUE);
	}

	if (event->keyval == GDK_Delete) {
		if (event->type == GDK_KEY_PRESS)
			connect_to_prev_input_port();

		return (TRUE);
	}

	if (event->keyval == GDK_Escape) {
		if (event->type == GDK_KEY_PRESS)
			panic();

		return (TRUE);
	}

	/*
	 * Spacebar works as a 'sustain' key.  Holding spacebar while
	 * releasing note will cause the note to continue. Pressing and
	 * releasing spacebar without pressing any note keys will make all
	 * the sustained notes end (it will send 'note off' midi messages for
	 * all the sustained notes).
	 */
	if (event->keyval == GDK_space) {
		if (event->type == GDK_KEY_PRESS)
			gtk_button_pressed(GTK_BUTTON(sustain_button));
		else
			gtk_button_released(GTK_BUTTON(sustain_button));

		return (TRUE);
	}

	/*
	 * Shift increases velocity, i.e. holding it while pressing note key
	 * will make the sound louder. Ctrl decreases velocity.
	 */
	if (event->keyval == GDK_Shift_L || event->keyval == GDK_Shift_R) {
		if (event->type == GDK_KEY_PRESS)
			current_velocity = &velocity_high;
		else
			current_velocity = &velocity_normal;

		gtk_range_set_value(GTK_RANGE(velocity_hscale), *current_velocity);

		return (TRUE);

	}

	if (event->keyval == GDK_Control_L || event->keyval == GDK_Control_R) {
		if (event->type == GDK_KEY_PRESS)
			current_velocity = &velocity_low;
		else
			current_velocity = &velocity_normal;

		gtk_range_set_value(GTK_RANGE(velocity_hscale), *current_velocity);

		return (TRUE);
	}

	return (FALSE);
}

void
note_on_event_handler(GtkWidget *widget, int note)
{
	assert(current_velocity);

	queue_new_message(MIDI_NOTE_ON, note, *current_velocity);
}

void
note_off_event_handler(GtkWidget *widget, int note)
{
	assert(current_velocity);

	queue_new_message(MIDI_NOTE_OFF, note, *current_velocity);
}

void
init_gtk_1(int *argc, char ***argv)
{
	GdkPixbuf *icon = NULL;
	GError *error = NULL;

	gtk_init(argc, argv);

	icon = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(), "jack-keyboard", 48, 0, &error);

	if (icon == NULL) {
		fprintf(stderr, "%s: Cannot load icon: %s.\n", G_LOG_DOMAIN, error->message);
		g_error_free(error);

	} else {
		gtk_window_set_default_icon(icon);
	}

	/* Window. */
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_container_set_border_width(GTK_CONTAINER(window), 2);
}

void
init_gtk_2(void)
{
	GtkTable *table;
	GtkWidget *label;
	GtkCellRenderer *renderer;

	/* Table. */
	table = GTK_TABLE(gtk_table_new(4, 8, FALSE));
	gtk_table_set_row_spacings(table, 5);
	gtk_table_set_col_spacings(table, 5);

	if (enable_gui)
		gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(table));

	/* Channel label and spin. */
	label = gtk_label_new("Channel:");
	gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
	gtk_table_attach(table, label, 0, 1, 0, 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
	channel_spin = gtk_spin_button_new_with_range(1, CHANNEL_MAX, 1);
	GTK_WIDGET_UNSET_FLAGS(channel_spin, GTK_CAN_FOCUS);
	gtk_table_attach(table, channel_spin, 1, 2, 0, 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
	g_signal_connect(G_OBJECT(channel_spin), "value-changed", G_CALLBACK(channel_event_handler), NULL);

	/* Bank label and spin. */
	label = gtk_label_new("Bank:");
	gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
	gtk_table_attach(table, label, 2, 3, 0, 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
	bank_spin = gtk_spin_button_new_with_range(0, BANK_MAX, 1);
	GTK_WIDGET_UNSET_FLAGS(bank_spin, GTK_CAN_FOCUS);
	gtk_table_attach(table, bank_spin, 3, 4, 0, 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
	g_signal_connect(G_OBJECT(bank_spin), "value-changed", G_CALLBACK(bank_event_handler), NULL);

	/* Program label and spin. */
	label = gtk_label_new("Program:");
	gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
	gtk_table_attach(table, label, 4, 5, 0, 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
	program_spin = gtk_spin_button_new_with_range(0, PROGRAM_MAX, 1);
	GTK_WIDGET_UNSET_FLAGS(program_spin, GTK_CAN_FOCUS);
	gtk_table_attach(table, program_spin, 5, 6, 0, 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
	g_signal_connect(G_OBJECT(program_spin), "value-changed", G_CALLBACK(program_event_handler), NULL);

	/* "Connected to" label and combo box. */
	label = gtk_label_new("Connected to:");
	gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
	gtk_table_attach(table, label, 6, 7, 0, 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);

	connected_to_store = gtk_list_store_new(1, G_TYPE_STRING);
	connected_to_combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(connected_to_store));

	renderer = gtk_cell_renderer_text_new ();
	gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(connected_to_combo), renderer, FALSE);
	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT(connected_to_combo), renderer, "text", 0, NULL);

	GTK_WIDGET_UNSET_FLAGS(connected_to_combo, GTK_CAN_FOCUS);
	gtk_combo_box_set_focus_on_click(GTK_COMBO_BOX(connected_to_combo), FALSE);
	gtk_table_attach(table, connected_to_combo, 7, 8, 0, 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
	gtk_widget_set_size_request(GTK_WIDGET(connected_to_combo), 200, -1);
	g_signal_connect(G_OBJECT(connected_to_combo), "changed", G_CALLBACK(connected_to_event_handler), NULL);

	/* Octave label and spin. */
	label = gtk_label_new("Octave:");
	gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
	gtk_table_attach(table, label, 0, 1, 1, 2, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
	octave_spin = gtk_spin_button_new_with_range(OCTAVE_MIN, OCTAVE_MAX, 1);
	GTK_WIDGET_UNSET_FLAGS(octave_spin, GTK_CAN_FOCUS);
	gtk_spin_button_set_value(GTK_SPIN_BUTTON(octave_spin), octave);
	gtk_table_attach(table, octave_spin, 1, 2, 1, 2, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
	g_signal_connect(G_OBJECT(octave_spin), "value-changed", G_CALLBACK(octave_event_handler), NULL);

	/* "Grab keyboard" label and checkbutton. */
	label = gtk_label_new("Grab keyboard:");
	gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
	gtk_table_attach(table, label, 4, 5, 1, 2, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
	grab_keyboard_checkbutton = gtk_check_button_new();
	GTK_WIDGET_UNSET_FLAGS(grab_keyboard_checkbutton, GTK_CAN_FOCUS);
	g_signal_connect(G_OBJECT(grab_keyboard_checkbutton), "toggled", G_CALLBACK(grab_keyboard_handler), NULL);
	gtk_table_attach(table, grab_keyboard_checkbutton, 5, 6, 1, 2, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);

	/* Velocity label and hscale */
	label = gtk_label_new("Velocity:");
	gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
	gtk_table_attach(table, label, 6, 7, 1, 2, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);

	velocity_hscale = gtk_hscale_new_with_range(VELOCITY_MIN, VELOCITY_MAX, 1);
	gtk_scale_set_draw_value(GTK_SCALE(velocity_hscale), FALSE);
	GTK_WIDGET_UNSET_FLAGS(velocity_hscale, GTK_CAN_FOCUS);
	g_signal_connect(G_OBJECT(velocity_hscale), "value-changed", G_CALLBACK(velocity_event_handler), NULL);
	gtk_range_set_value(GTK_RANGE(velocity_hscale), VELOCITY_NORMAL);
	gtk_table_attach(table, velocity_hscale, 7, 8, 1, 2, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
	gtk_widget_set_size_request(GTK_WIDGET(velocity_hscale), 200, -1);

	/* Sustain. It's a toggle button, not an ordinary one, because we want gtk_whatever_set_active() to work.*/
	sustain_button = gtk_toggle_button_new_with_label("Sustain");
	gtk_button_set_focus_on_click(GTK_BUTTON(sustain_button), FALSE);
	gtk_table_attach(table, sustain_button, 0, 8, 2, 3, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
	g_signal_connect(G_OBJECT(sustain_button), "pressed", G_CALLBACK(sustain_event_handler), (void *)1);
	g_signal_connect(G_OBJECT(sustain_button), "released", G_CALLBACK(sustain_event_handler), (void *)0);

	/* PianoKeyboard widget. */
	keyboard = PIANO_KEYBOARD(piano_keyboard_new());

	if (enable_gui)
		gtk_table_attach_defaults(table, GTK_WIDGET(keyboard), 0, 8, 3, 4);
	else
		gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(keyboard));

	g_signal_connect(G_OBJECT(keyboard), "note-on", G_CALLBACK(note_on_event_handler), NULL);
	g_signal_connect(G_OBJECT(keyboard), "note-off", G_CALLBACK(note_off_event_handler), NULL);
	g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
	g_signal_connect(G_OBJECT(window), "key-press-event", G_CALLBACK(keyboard_event_handler), NULL);
	g_signal_connect(G_OBJECT(window), "key-release-event", G_CALLBACK(keyboard_event_handler), NULL);
	gtk_widget_show_all(window);

	draw_window_title();
}

void   
log_handler(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer notused)
{
	GtkWidget *dialog;

	fprintf(stderr, "%s: %s\n", log_domain, message);

	if ((log_level | G_LOG_LEVEL_CRITICAL) == G_LOG_LEVEL_CRITICAL) {
		dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR,
			GTK_BUTTONS_CLOSE, "%s", message);

		gtk_dialog_run(GTK_DIALOG(dialog));

		gtk_widget_destroy(dialog);
	}
}

void
show_version(void)
{
	fprintf(stdout, "%s\n", PACKAGE_NAME " v" PACKAGE_VERSION);

	exit(EX_OK);
}

void
usage(void)
{
	fprintf(stderr, "usage: jack-keyboard [-CGKTVkturf] [ -a <input port>] [-c <channel>] [-b <bank> ] [-p <program>] [-l <layout>]\n");
	fprintf(stderr, "   where <channel> is MIDI channel to use for output, from 1 to 16,\n");
	fprintf(stderr, "   <bank> is MIDI bank to use, from 0 to 16383,\n");
	fprintf(stderr, "   <program> is MIDI program to use, from 0 to 127,\n");
	fprintf(stderr, "   and <layout> is  QWERTY, QWERTY_REV, QWERTY_UK, QWERTY_UK_REV, QWERTZ, AZERTY or DVORAK.\n");
	fprintf(stderr, "See manual page for details.\n");

	exit(EX_USAGE);
}

int 
main(int argc, char *argv[])
{
	int ch, enable_keyboard_cue = 0, initial_channel = 1, initial_bank = 0, initial_program = 0, full_midi_keyboard = 0;
	char *keyboard_layout = NULL, *autoconnect_port_name = NULL;

#ifdef HAVE_LASH
	lash_args_t *lash_args;
#endif

	g_thread_init(NULL);

#ifdef HAVE_LASH
	lash_args = lash_extract_args(&argc, &argv);
#endif

	init_gtk_1(&argc, &argv);

	g_log_set_default_handler(log_handler, NULL);

	while ((ch = getopt(argc, argv, "CGKTVa:nktur:c:b:p:l:f")) != -1) {
		switch (ch) {
		case 'C':
			enable_keyboard_cue = 1;
			break;

		case 'G':
			enable_gui = 0;
			enable_window_title = !enable_window_title;
			break;

		case 'K':
			grab_keyboard_at_startup = 1;
			break;

		case 'T':
			enable_window_title = !enable_window_title;
			break;

		case 'V':
			show_version();
			break;

		case 'a':
			autoconnect_port_name = strdup(optarg);
			break;

		case 'n':
			/* Do nothing; backward compatibility. */
			break;

		case 'k':
			allow_connecting_to_own_kind = 1;
			break;

		case 'l':
			keyboard_layout = strdup(optarg);
			break;
			
		case 't':
			time_offsets_are_zero = 1;
			break;
			
		case 'u':
			send_program_change_at_reconnect = 1;
			break;

		case 'c':
			initial_channel = atoi(optarg);

			if (initial_channel < CHANNEL_MIN || initial_channel > CHANNEL_MAX) {
				g_critical("Invalid MIDI channel number specified on the command line; "
					"valid values are %d-%d.", CHANNEL_MIN, CHANNEL_MAX);

				exit(EX_USAGE);
			}

			break;

		case 'b':
			initial_bank = atoi(optarg);

			send_program_change_once = 1;

			if (initial_bank < BANK_MIN || initial_bank > BANK_MAX) {
				g_critical("Invalid MIDI bank number specified on the command line; "
					"valid values are %d-%d.", BANK_MIN, BANK_MAX);

				exit(EX_USAGE);
			}

			break;

		case 'p':
			initial_program = atoi(optarg);

			send_program_change_once = 1;

			if (initial_program < PROGRAM_MIN || initial_program > PROGRAM_MAX) {
				g_critical("Invalid MIDI program number specified on the command line; "
					"valid values are %d-%d.", PROGRAM_MIN, PROGRAM_MAX);

				exit(EX_USAGE);
			}

			break;

		case 'r':
			rate_limit = strtod(optarg, NULL);
			if (rate_limit <= 0.0) {
				g_critical("Invalid rate limit specified.\n");

				exit(EX_USAGE);
			}

			break;

		case 'f':
			full_midi_keyboard = 1;
			break;

		case '?':
		default:
			usage();
		}
	}

	argc -= optind;
	argv += optind;

	init_gtk_2();

	if (full_midi_keyboard)
		piano_keyboard_enable_all_midi_notes(keyboard);

	if (keyboard_layout != NULL) {
		int ret = piano_keyboard_set_keyboard_layout(keyboard, keyboard_layout);

		if (ret) {
			g_critical("Invalid layout, proper choices are QWERTY, QWERTY_REV, QWERTY_UK, QWERTY_UK_REV, QWERTZ, AZERTY and DVORAK.");
			exit(EX_USAGE);
		}
	}

#ifdef HAVE_LASH
	init_lash(lash_args);
#endif

	init_jack();

	if (autoconnect_port_name) {
		if (connect_to_input_port(autoconnect_port_name)) {
			g_critical("Couldn't connect to '%s', exiting.", autoconnect_port_name);
			exit(EX_UNAVAILABLE);
		}
	}

	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(grab_keyboard_checkbutton), grab_keyboard_at_startup);
	gtk_spin_button_set_value(GTK_SPIN_BUTTON(channel_spin), initial_channel);
	gtk_spin_button_set_value(GTK_SPIN_BUTTON(bank_spin), initial_bank);
	gtk_spin_button_set_value(GTK_SPIN_BUTTON(program_spin), initial_program);
	piano_keyboard_set_keyboard_cue(keyboard, enable_keyboard_cue);

	gtk_main();

	return (0);
}