decode fps encoded in MMC goto hour
[gmidimonitor.git] / main.c
blob93e4134f236ac0f1f23c7f10a531d3cd89a58c21
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * DESCRIPTION:
5 * gmidimonitor main code.
7 * LICENSE:
8 * GNU GENERAL PUBLIC LICENSE version 2
10 *****************************************************************************/
12 #include <gtk/gtk.h>
13 #include <alsa/asoundlib.h>
14 #include <pthread.h>
15 #include <signal.h>
16 #include <lash/lash.h>
18 #include "path.h"
19 #include "glade.h"
21 GtkWidget * g_main_window_ptr;
23 snd_seq_t * g_seq_ptr;
25 gboolean g_midi_ignore = FALSE;
27 int g_row_count;
29 lash_client_t * g_lashc;
31 static const char * g_note_names[12] =
33 "C",
34 "C#",
35 "D",
36 "D#",
37 "E",
38 "F",
39 "F#",
40 "G",
41 "G#",
42 "A",
43 "A#",
44 "B",
47 static const char * g_gm_instrument_names[] =
49 "Acoustic Grand Piano", /* 1 */
50 "Bright Acoustic Piano", /* 2 */
51 "Electric Grand Piano", /* 3 */
52 "Honky-tonk Piano", /* 4 */
53 "Electric Piano 1", /* 5 */
54 "Electric Piano 2", /* 6 */
55 "Harpsichord", /* 7 */
56 "Clavi", /* 8 */
57 "Celesta", /* 9 */
58 "Glockenspiel", /* 10 */
59 "Music Box", /* 11 */
60 "Vibraphone", /* 12 */
61 "Marimba", /* 13 */
62 "Xylophone", /* 14 */
63 "Tubular Bells", /* 15 */
64 "Dulcimer", /* 16 */
65 "Drawbar Organ", /* 17 */
66 "Percussive Organ", /* 18 */
67 "Rock Organ", /* 19 */
68 "Church Organ", /* 20 */
69 "Reed Organ", /* 21 */
70 "Accordion", /* 22 */
71 "Harmonica", /* 23 */
72 "Tango Accordion", /* 24 */
73 "Acoustic Guitar (nylon)", /* 25 */
74 "Acoustic Guitar (steel)", /* 26 */
75 "Electric Guitar (jazz)", /* 27 */
76 "Electric Guitar (clean)", /* 28 */
77 "Electric Guitar (muted)", /* 29 */
78 "Overdriven Guitar", /* 30 */
79 "Distortion Guitar", /* 31 */
80 "Guitar harmonics", /* 32 */
81 "Acoustic Bass", /* 33 */
82 "Electric Bass (finger)", /* 34 */
83 "Electric Bass (pick)", /* 35 */
84 "Fretless Bass", /* 36 */
85 "Slap Bass 1", /* 37 */
86 "Slap Bass 2", /* 38 */
87 "Synth Bass 1", /* 39 */
88 "Synth Bass 2", /* 40 */
89 "Violin", /* 41 */
90 "Viola", /* 42 */
91 "Cello", /* 43 */
92 "Contrabass", /* 44 */
93 "Tremolo Strings", /* 45 */
94 "Pizzicato Strings", /* 46 */
95 "Orchestral Harp", /* 47 */
96 "Timpani", /* 48 */
97 "String Ensemble 1", /* 49 */
98 "String Ensemble 2", /* 50 */
99 "SynthStrings 1", /* 51 */
100 "SynthStrings 2", /* 52 */
101 "Choir Aahs", /* 53 */
102 "Voice Oohs", /* 54 */
103 "Synth Voice", /* 55 */
104 "Orchestra Hit", /* 56 */
105 "Trumpet", /* 57 */
106 "Trombone", /* 58 */
107 "Tuba", /* 59 */
108 "Muted Trumpet", /* 60 */
109 "French Horn", /* 61 */
110 "Brass Section", /* 62 */
111 "SynthBrass 1", /* 63 */
112 "SynthBrass 2", /* 64 */
113 "Soprano Sax", /* 65 */
114 "Alto Sax", /* 66 */
115 "Tenor Sax", /* 67 */
116 "Baritone Sax", /* 68 */
117 "Oboe", /* 69 */
118 "English Horn", /* 70 */
119 "Bassoon", /* 71 */
120 "Clarinet", /* 72 */
121 "Piccolo", /* 73 */
122 "Flute", /* 74 */
123 "Recorder", /* 75 */
124 "Pan Flute", /* 76 */
125 "Blown Bottle", /* 77 */
126 "Shakuhachi", /* 78 */
127 "Whistle", /* 79 */
128 "Ocarina", /* 80 */
129 "Lead 1 (square)", /* 81 */
130 "Lead 2 (sawtooth)", /* 82 */
131 "Lead 3 (calliope)", /* 83 */
132 "Lead 4 (chiff)", /* 84 */
133 "Lead 5 (charang)", /* 85 */
134 "Lead 6 (voice)", /* 86 */
135 "Lead 7 (fifths)", /* 87 */
136 "Lead 8 (bass + lead)", /* 88 */
137 "Pad 1 (new age)", /* 89 */
138 "Pad 2 (warm)", /* 90 */
139 "Pad 3 (polysynth)", /* 91 */
140 "Pad 4 (choir)", /* 92 */
141 "Pad 5 (bowed)", /* 93 */
142 "Pad 6 (metallic)", /* 94 */
143 "Pad 7 (halo)", /* 95 */
144 "Pad 8 (sweep)", /* 96 */
145 "FX 1 (rain)", /* 97 */
146 "FX 2 (soundtrack)", /* 98 */
147 "FX 3 (crystal)", /* 99 */
148 "FX 4 (atmosphere)", /* 100 */
149 "FX 5 (brightness)", /* 101 */
150 "FX 6 (goblins)", /* 102 */
151 "FX 7 (echoes)", /* 103 */
152 "FX 8 (sci-fi)", /* 104 */
153 "Sitar", /* 105 */
154 "Banjo", /* 106 */
155 "Shamisen", /* 107 */
156 "Koto", /* 108 */
157 "Kalimba", /* 109 */
158 "Bag pipe", /* 110 */
159 "Fiddle", /* 111 */
160 "Shanai", /* 112 */
161 "Tinkle Bell", /* 113 */
162 "Agogo", /* 114 */
163 "Steel Drums", /* 115 */
164 "Woodblock", /* 116 */
165 "Taiko Drum", /* 117 */
166 "Melodic Tom", /* 118 */
167 "Synth Drum", /* 119 */
168 "Reverse Cymbal", /* 120 */
169 "Guitar Fret Noise", /* 121 */
170 "Breath Noise", /* 122 */
171 "Seashore", /* 123 */
172 "Bird Tweet", /* 124 */
173 "Telephone Ring", /* 125 */
174 "Helicopter", /* 126 */
175 "Applause", /* 127 */
176 "Gunshot", /* 128 */
179 static const char * g_gm_drum_names[] =
181 "Acoustic Bass Drum", /* 35 */
182 "Bass Drum 1", /* 36 */
183 "Side Stick", /* 37 */
184 "Acoustic Snare", /* 38 */
185 "Hand Clap", /* 39 */
186 "Electric Snare", /* 40 */
187 "Low Floor Tom", /* 41 */
188 "Closed Hi-Hat", /* 42 */
189 "High Floor Tom", /* 43 */
190 "Pedal Hi-Hat", /* 44 */
191 "Low Tom", /* 45 */
192 "Open Hi-Hat", /* 46 */
193 "Low-Mid Tom", /* 47 */
194 "Hi-Mid Tom", /* 48 */
195 "Crash Cymbal 1", /* 49 */
196 "High Tom", /* 50 */
197 "Ride Cymbal 1", /* 51 */
198 "Chinese Cymbal", /* 52 */
199 "Ride Bell", /* 53 */
200 "Tambourine", /* 54 */
201 "Splash Cymbal", /* 55 */
202 "Cowbell", /* 56 */
203 "Crash Cymbal 2", /* 57 */
204 "Vibraslap", /* 58 */
205 "Ride Cymbal 2", /* 59 */
206 "Hi Bongo", /* 60 */
207 "Low Bongo", /* 61 */
208 "Mute Hi Conga", /* 62 */
209 "Open Hi Conga", /* 63 */
210 "Low Conga", /* 64 */
211 "High Timbale", /* 65 */
212 "Low Timbale", /* 66 */
213 "High Agogo", /* 67 */
214 "Low Agogo", /* 68 */
215 "Cabasa", /* 69 */
216 "Maracas", /* 70 */
217 "Short Whistle", /* 71 */
218 "Long Whistle", /* 72 */
219 "Short Guiro", /* 73 */
220 "Long Guiro", /* 74 */
221 "Claves", /* 75 */
222 "Hi Wood Block", /* 76 */
223 "Low Wood Block", /* 77 */
224 "Mute Cuica", /* 78 */
225 "Open Cuica", /* 79 */
226 "Mute Triangle", /* 80 */
227 "Open Triangle", /* 81 */
230 enum
232 COL_TIME,
233 COL_CHANNEL,
234 COL_MESSAGE,
235 NUM_COLS
238 #define MAX_LIST_SIZE 2000
240 void
241 create_mainwindow()
243 GtkWidget * child_ptr;
244 GtkListStore * list_store_ptr;
245 GtkTreeViewColumn * column_ptr;
246 GtkCellRenderer * text_renderer_ptr;
248 g_main_window_ptr = construct_glade_widget("main_window");
250 child_ptr = get_glade_widget_child(g_main_window_ptr, "list");
252 text_renderer_ptr = gtk_cell_renderer_text_new();
254 g_object_set(
255 G_OBJECT(text_renderer_ptr),
256 "family",
257 "Monospace",
258 NULL);
260 /* column_ptr = gtk_tree_view_column_new_with_attributes( */
261 /* "Time", */
262 /* text_renderer_ptr, */
263 /* "text", COL_TIME, */
264 /* NULL); */
266 /* gtk_tree_view_append_column( */
267 /* GTK_TREE_VIEW(child_ptr), */
268 /* column_ptr); */
270 column_ptr = gtk_tree_view_column_new_with_attributes(
271 "Channel",
272 text_renderer_ptr,
273 "text", COL_CHANNEL,
274 NULL);
276 gtk_tree_view_append_column(
277 GTK_TREE_VIEW(child_ptr),
278 column_ptr);
280 column_ptr = gtk_tree_view_column_new_with_attributes(
281 "Message",
282 text_renderer_ptr,
283 "text", COL_MESSAGE,
284 NULL);
286 gtk_tree_view_append_column(
287 GTK_TREE_VIEW(child_ptr),
288 column_ptr);
290 gtk_widget_show_all(g_main_window_ptr);
292 list_store_ptr = gtk_list_store_new(
293 NUM_COLS,
294 G_TYPE_STRING,
295 G_TYPE_STRING,
296 G_TYPE_STRING);
298 gtk_tree_view_set_model(GTK_TREE_VIEW(child_ptr), GTK_TREE_MODEL(list_store_ptr));
301 /* stop button toggle */
302 void on_button_stop_toggled(
303 GtkAction * action_ptr,
304 GtkWidget * widget_ptr)
306 GtkWidget * child_ptr;
308 child_ptr = get_glade_widget_child(widget_ptr, "button_stop");
310 if (gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(child_ptr)))
312 g_midi_ignore = TRUE;
314 else
316 g_midi_ignore = FALSE;
320 void on_clear_clicked
321 (GtkButton *button, gpointer user_data)
323 gtk_list_store_clear(
324 GTK_LIST_STORE(
325 gtk_tree_view_get_model(
326 GTK_TREE_VIEW(
327 get_glade_widget_child(
328 g_main_window_ptr,
329 "list")))));
330 g_row_count = 0;
333 /* The midi input handling thread */
334 void *
335 midi_thread(void * context_ptr)
337 GtkTreeIter iter;
338 snd_seq_event_t * event_ptr;
339 GtkListStore * list_store_ptr;
340 GtkWidget * child_ptr;
341 GString * time_str_ptr;
342 GString * msg_str_ptr;
343 GString * channel_str_ptr;
344 const char * note_name;
345 int octave;
346 const char * drum_name;
347 const char * cc_name;
348 int i;
349 const char * mmc_command_name;
351 child_ptr = get_glade_widget_child(g_main_window_ptr, "list");
353 list_store_ptr = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(child_ptr)));
355 g_row_count = 0;
357 while (snd_seq_event_input(g_seq_ptr, &event_ptr) >= 0)
359 if (g_midi_ignore)
360 continue;
362 time_str_ptr = g_string_new("");
363 g_string_sprintf(
364 time_str_ptr,
365 "%u:%u",
366 (unsigned int)event_ptr->time.time.tv_sec,
367 (unsigned int)event_ptr->time.time.tv_nsec);
368 channel_str_ptr = g_string_new("");
370 if (event_ptr->type == SND_SEQ_EVENT_NOTE ||
371 event_ptr->type == SND_SEQ_EVENT_NOTEON ||
372 event_ptr->type == SND_SEQ_EVENT_NOTEOFF ||
373 event_ptr->type == SND_SEQ_EVENT_KEYPRESS)
375 g_string_sprintf(
376 channel_str_ptr,
377 "%u",
378 (unsigned int)event_ptr->data.note.channel+1);
379 if (event_ptr->data.note.channel+1 == 10 &&
380 event_ptr->data.note.note >= 35 &&
381 event_ptr->data.note.note < 35 + sizeof(g_gm_drum_names)/sizeof(g_gm_drum_names[0]))
383 drum_name = g_gm_drum_names[event_ptr->data.note.note - 35];
385 else
387 drum_name = NULL;
390 note_name = g_note_names[event_ptr->data.note.note % 12];
391 octave = event_ptr->data.note.note / 12 - 1;
394 if (event_ptr->type == SND_SEQ_EVENT_CONTROLLER ||
395 event_ptr->type == SND_SEQ_EVENT_PGMCHANGE ||
396 event_ptr->type == SND_SEQ_EVENT_PITCHBEND)
398 g_string_sprintf(
399 channel_str_ptr,
400 "%u",
401 (unsigned int)event_ptr->data.control.channel+1);
404 msg_str_ptr = g_string_new("unknown event");
406 switch (event_ptr->type)
408 case SND_SEQ_EVENT_SYSTEM:
409 g_string_sprintf(msg_str_ptr, "System event");
410 break;
411 case SND_SEQ_EVENT_RESULT:
412 g_string_sprintf(msg_str_ptr, "Result status event");
413 break;
414 case SND_SEQ_EVENT_NOTE:
415 g_string_sprintf(msg_str_ptr, "Note");
416 break;
417 case SND_SEQ_EVENT_NOTEON:
418 if (event_ptr->data.note.velocity != 0)
420 if (drum_name != NULL)
422 g_string_sprintf(
423 msg_str_ptr,
424 "Drum: %s (%s, octave %d, velocity %u)",
425 drum_name,
426 note_name,
427 octave,
428 event_ptr->data.note.velocity);
430 else
432 g_string_sprintf(
433 msg_str_ptr,
434 "Note on , %s, octave %d, velocity %u",
435 note_name,
436 octave,
437 event_ptr->data.note.velocity);
439 break;
441 case SND_SEQ_EVENT_NOTEOFF:
442 if (drum_name != NULL) /* ignore note off for drums */
443 continue;
445 g_string_sprintf(
446 msg_str_ptr,
447 "Note off, %s, octave %d",
448 note_name,
449 octave);
451 break;
452 case SND_SEQ_EVENT_KEYPRESS:
453 g_string_sprintf(msg_str_ptr, "Key pressure change (aftertouch)");
454 break;
455 case SND_SEQ_EVENT_CONTROLLER:
456 cc_name = NULL;
457 switch (event_ptr->data.control.param)
459 case MIDI_CTL_MSB_BANK:
460 cc_name = "Bank selection";
461 break;
462 case MIDI_CTL_MSB_MODWHEEL:
463 cc_name = "Modulation";
464 break;
465 case MIDI_CTL_MSB_BREATH:
466 cc_name = "Breath";
467 break;
468 case MIDI_CTL_MSB_FOOT:
469 cc_name = "Foot";
470 break;
471 case MIDI_CTL_MSB_PORTAMENTO_TIME:
472 cc_name = "Portamento time";
473 break;
474 case MIDI_CTL_MSB_DATA_ENTRY:
475 cc_name = "Data entry";
476 break;
477 case MIDI_CTL_MSB_MAIN_VOLUME:
478 cc_name = "Main volume";
479 break;
480 case MIDI_CTL_MSB_BALANCE:
481 cc_name = "Balance";
482 break;
483 case MIDI_CTL_MSB_PAN:
484 cc_name = "Panpot";
485 break;
486 case MIDI_CTL_MSB_EXPRESSION:
487 cc_name = "Expression";
488 break;
489 case MIDI_CTL_MSB_EFFECT1:
490 cc_name = "Effect1";
491 break;
492 case MIDI_CTL_MSB_EFFECT2:
493 cc_name = "Effect2";
494 break;
495 case MIDI_CTL_MSB_GENERAL_PURPOSE1:
496 cc_name = "General purpose 1";
497 break;
498 case MIDI_CTL_MSB_GENERAL_PURPOSE2:
499 cc_name = "General purpose 2";
500 break;
501 case MIDI_CTL_MSB_GENERAL_PURPOSE3:
502 cc_name = "General purpose 3";
503 break;
504 case MIDI_CTL_MSB_GENERAL_PURPOSE4:
505 cc_name = "General purpose 4";
506 break;
507 case MIDI_CTL_LSB_BANK:
508 cc_name = "Bank selection";
509 break;
510 case MIDI_CTL_LSB_MODWHEEL:
511 cc_name = "Modulation";
512 break;
513 case MIDI_CTL_LSB_BREATH:
514 cc_name = "Breath";
515 break;
516 case MIDI_CTL_LSB_FOOT:
517 cc_name = "Foot";
518 break;
519 case MIDI_CTL_LSB_PORTAMENTO_TIME:
520 cc_name = "Portamento time";
521 break;
522 case MIDI_CTL_LSB_DATA_ENTRY:
523 cc_name = "Data entry";
524 break;
525 case MIDI_CTL_LSB_MAIN_VOLUME:
526 cc_name = "Main volume";
527 break;
528 case MIDI_CTL_LSB_BALANCE:
529 cc_name = "Balance";
530 break;
531 case MIDI_CTL_LSB_PAN:
532 cc_name = "Panpot";
533 break;
534 case MIDI_CTL_LSB_EXPRESSION:
535 cc_name = "Expression";
536 break;
537 case MIDI_CTL_LSB_EFFECT1:
538 cc_name = "Effect1";
539 break;
540 case MIDI_CTL_LSB_EFFECT2:
541 cc_name = "Effect2";
542 break;
543 case MIDI_CTL_LSB_GENERAL_PURPOSE1:
544 cc_name = "General purpose 1";
545 break;
546 case MIDI_CTL_LSB_GENERAL_PURPOSE2:
547 cc_name = "General purpose 2";
548 break;
549 case MIDI_CTL_LSB_GENERAL_PURPOSE3:
550 cc_name = "General purpose 3";
551 break;
552 case MIDI_CTL_LSB_GENERAL_PURPOSE4:
553 cc_name = "General purpose 4";
554 break;
555 case MIDI_CTL_SUSTAIN:
556 cc_name = "Sustain pedal";
557 break;
558 case MIDI_CTL_PORTAMENTO:
559 cc_name = "Portamento";
560 break;
561 case MIDI_CTL_SOSTENUTO:
562 cc_name = "Sostenuto";
563 break;
564 case MIDI_CTL_SOFT_PEDAL:
565 cc_name = "Soft pedal";
566 break;
567 case MIDI_CTL_LEGATO_FOOTSWITCH:
568 cc_name = "Legato foot switch";
569 break;
570 case MIDI_CTL_HOLD2:
571 cc_name = "Hold2";
572 break;
573 case MIDI_CTL_SC1_SOUND_VARIATION:
574 cc_name = "SC1 Sound Variation";
575 break;
576 case MIDI_CTL_SC2_TIMBRE:
577 cc_name = "SC2 Timbre";
578 break;
579 case MIDI_CTL_SC3_RELEASE_TIME:
580 cc_name = "SC3 Release Time";
581 break;
582 case MIDI_CTL_SC4_ATTACK_TIME:
583 cc_name = "SC4 Attack Time";
584 break;
585 case MIDI_CTL_SC5_BRIGHTNESS:
586 cc_name = "SC5 Brightness";
587 break;
588 case MIDI_CTL_SC6:
589 cc_name = "SC6";
590 break;
591 case MIDI_CTL_SC7:
592 cc_name = "SC7";
593 break;
594 case MIDI_CTL_SC8:
595 cc_name = "SC8";
596 break;
597 case MIDI_CTL_SC9:
598 cc_name = "SC9";
599 break;
600 case MIDI_CTL_SC10:
601 cc_name = "SC10";
602 break;
603 case MIDI_CTL_GENERAL_PURPOSE5:
604 cc_name = "General purpose 5";
605 break;
606 case MIDI_CTL_GENERAL_PURPOSE6:
607 cc_name = "General purpose 6";
608 break;
609 case MIDI_CTL_GENERAL_PURPOSE7:
610 cc_name = "General purpose 7";
611 break;
612 case MIDI_CTL_GENERAL_PURPOSE8:
613 cc_name = "General purpose 8";
614 break;
615 case MIDI_CTL_PORTAMENTO_CONTROL:
616 cc_name = "Portamento control";
617 break;
618 case MIDI_CTL_E1_REVERB_DEPTH:
619 cc_name = "E1 Reverb Depth";
620 break;
621 case MIDI_CTL_E2_TREMOLO_DEPTH:
622 cc_name = "E2 Tremolo Depth";
623 break;
624 case MIDI_CTL_E3_CHORUS_DEPTH:
625 cc_name = "E3 Chorus Depth";
626 break;
627 case MIDI_CTL_E4_DETUNE_DEPTH:
628 cc_name = "E4 Detune Depth";
629 break;
630 case MIDI_CTL_E5_PHASER_DEPTH:
631 cc_name = "E5 Phaser Depth";
632 break;
633 case MIDI_CTL_DATA_INCREMENT:
634 cc_name = "Data Increment";
635 break;
636 case MIDI_CTL_DATA_DECREMENT:
637 cc_name = "Data Decrement";
638 break;
639 case MIDI_CTL_NONREG_PARM_NUM_LSB:
640 cc_name = "Non-registered parameter number";
641 break;
642 case MIDI_CTL_NONREG_PARM_NUM_MSB:
643 cc_name = "Non-registered parameter number";
644 break;
645 case MIDI_CTL_REGIST_PARM_NUM_LSB:
646 cc_name = "Registered parameter number";
647 break;
648 case MIDI_CTL_REGIST_PARM_NUM_MSB:
649 cc_name = "Registered parameter number";
650 break;
651 case MIDI_CTL_ALL_SOUNDS_OFF:
652 cc_name = "All sounds off";
653 break;
654 case MIDI_CTL_RESET_CONTROLLERS:
655 cc_name = "Reset Controllers";
656 break;
657 case MIDI_CTL_LOCAL_CONTROL_SWITCH:
658 cc_name = "Local control switch";
659 break;
660 case MIDI_CTL_ALL_NOTES_OFF:
661 cc_name = "All notes off";
662 break;
663 case MIDI_CTL_OMNI_OFF:
664 cc_name = "Omni off";
665 break;
666 case MIDI_CTL_OMNI_ON:
667 cc_name = "Omni on";
668 break;
669 case MIDI_CTL_MONO1:
670 cc_name = "Mono1";
671 break;
672 case MIDI_CTL_MONO2:
673 cc_name = "Mono2";
674 break;
677 if (cc_name != NULL)
679 g_string_sprintf(
680 msg_str_ptr,
681 "CC %s (%u), value %u",
682 cc_name,
683 (unsigned int)event_ptr->data.control.param,
684 (unsigned int)event_ptr->data.control.value);
686 else
688 g_string_sprintf(
689 msg_str_ptr,
690 "CC %u, value %u",
691 (unsigned int)event_ptr->data.control.param,
692 (unsigned int)event_ptr->data.control.value);
694 break;
695 case SND_SEQ_EVENT_PGMCHANGE:
696 g_string_sprintf(
697 msg_str_ptr,
698 "Program change, %d (%s)",
699 (unsigned int)event_ptr->data.control.value,
700 event_ptr->data.control.value >= sizeof(g_gm_instrument_names)/sizeof(g_gm_instrument_names[0])?
701 "???":
702 g_gm_instrument_names[event_ptr->data.control.value]);
703 break;
704 case SND_SEQ_EVENT_CHANPRESS:
705 g_string_sprintf(msg_str_ptr, "Channel pressure");
706 break;
707 case SND_SEQ_EVENT_PITCHBEND:
708 g_string_sprintf(
709 msg_str_ptr,
710 "Pitchwheel, %d",
711 (unsigned int)event_ptr->data.control.value);
712 break;
713 case SND_SEQ_EVENT_CONTROL14:
714 g_string_sprintf(msg_str_ptr, "14 bit controller value");
715 break;
716 case SND_SEQ_EVENT_NONREGPARAM:
717 g_string_sprintf(msg_str_ptr, "NRPN");
718 break;
719 case SND_SEQ_EVENT_REGPARAM:
720 g_string_sprintf(msg_str_ptr, "RPN");
721 break;
722 case SND_SEQ_EVENT_SONGPOS:
723 g_string_sprintf(msg_str_ptr, "Song position");
724 break;
725 case SND_SEQ_EVENT_SONGSEL:
726 g_string_sprintf(msg_str_ptr, "Song select");
727 break;
728 case SND_SEQ_EVENT_QFRAME:
729 g_string_sprintf(msg_str_ptr, "midi time code quarter frame");
730 break;
731 case SND_SEQ_EVENT_TIMESIGN:
732 g_string_sprintf(msg_str_ptr, "SMF Time Signature event");
733 break;
734 case SND_SEQ_EVENT_KEYSIGN:
735 g_string_sprintf(msg_str_ptr, "SMF Key Signature event");
736 break;
737 case SND_SEQ_EVENT_START:
738 g_string_sprintf(msg_str_ptr, "MIDI Real Time Start message");
739 break;
740 case SND_SEQ_EVENT_CONTINUE:
741 g_string_sprintf(msg_str_ptr, "MIDI Real Time Continue message");
742 break;
743 case SND_SEQ_EVENT_STOP:
744 g_string_sprintf(msg_str_ptr, "MIDI Real Time Stop message");
745 break;
746 case SND_SEQ_EVENT_SETPOS_TICK:
747 g_string_sprintf(msg_str_ptr, "Set tick queue position");
748 break;
749 case SND_SEQ_EVENT_SETPOS_TIME:
750 g_string_sprintf(msg_str_ptr, "Set real-time queue position");
751 break;
752 case SND_SEQ_EVENT_TEMPO:
753 g_string_sprintf(msg_str_ptr, "(SMF) Tempo event");
754 break;
755 case SND_SEQ_EVENT_CLOCK:
756 g_string_sprintf(msg_str_ptr, "MIDI Real Time Clock message");
757 break;
758 case SND_SEQ_EVENT_TICK:
759 g_string_sprintf(msg_str_ptr, "MIDI Real Time Tick message");
760 break;
761 case SND_SEQ_EVENT_QUEUE_SKEW:
762 g_string_sprintf(msg_str_ptr, "Queue timer skew");
763 break;
764 case SND_SEQ_EVENT_SYNC_POS:
765 g_string_sprintf(msg_str_ptr, "Sync position changed");
766 break;
767 case SND_SEQ_EVENT_TUNE_REQUEST:
768 g_string_sprintf(msg_str_ptr, "Tune request");
769 break;
770 case SND_SEQ_EVENT_RESET:
771 g_string_sprintf(msg_str_ptr, "Reset");
772 break;
773 case SND_SEQ_EVENT_SENSING:
774 continue; /* disable */
775 g_string_sprintf(msg_str_ptr, "Active sensing");
776 break;
777 case SND_SEQ_EVENT_ECHO:
778 g_string_sprintf(msg_str_ptr, "Echo-back event");
779 break;
780 case SND_SEQ_EVENT_OSS:
781 g_string_sprintf(msg_str_ptr, "OSS emulation raw event");
782 break;
783 case SND_SEQ_EVENT_CLIENT_START:
784 g_string_sprintf(msg_str_ptr, "New client has connected");
785 break;
786 case SND_SEQ_EVENT_CLIENT_EXIT:
787 g_string_sprintf(msg_str_ptr, "Client has left the system");
788 break;
789 case SND_SEQ_EVENT_CLIENT_CHANGE:
790 g_string_sprintf(msg_str_ptr, "Client status/info has changed");
791 break;
792 case SND_SEQ_EVENT_PORT_START:
793 g_string_sprintf(msg_str_ptr, "New port was created");
794 break;
795 case SND_SEQ_EVENT_PORT_EXIT:
796 g_string_sprintf(msg_str_ptr, "Port was deleted from system");
797 break;
798 case SND_SEQ_EVENT_PORT_CHANGE:
799 g_string_sprintf(msg_str_ptr, "Port status/info has changed");
800 break;
801 case SND_SEQ_EVENT_PORT_SUBSCRIBED:
802 g_string_sprintf(msg_str_ptr, "Port connected");
803 break;
804 case SND_SEQ_EVENT_PORT_UNSUBSCRIBED:
805 g_string_sprintf(msg_str_ptr, "Port disconnected");
806 break;
807 case SND_SEQ_EVENT_SAMPLE:
808 g_string_sprintf(msg_str_ptr, "Sample select");
809 break;
810 case SND_SEQ_EVENT_SAMPLE_CLUSTER:
811 g_string_sprintf(msg_str_ptr, "Sample cluster select");
812 break;
813 case SND_SEQ_EVENT_SAMPLE_START:
814 g_string_sprintf(msg_str_ptr, "voice start");
815 break;
816 case SND_SEQ_EVENT_SAMPLE_STOP:
817 g_string_sprintf(msg_str_ptr, "voice stop");
818 break;
819 case SND_SEQ_EVENT_SAMPLE_FREQ:
820 g_string_sprintf(msg_str_ptr, "playback frequency");
821 break;
822 case SND_SEQ_EVENT_SAMPLE_VOLUME:
823 g_string_sprintf(msg_str_ptr, "volume and balance");
824 break;
825 case SND_SEQ_EVENT_SAMPLE_LOOP:
826 g_string_sprintf(msg_str_ptr, "sample loop");
827 break;
828 case SND_SEQ_EVENT_SAMPLE_POSITION:
829 g_string_sprintf(msg_str_ptr, "sample position");
830 break;
831 case SND_SEQ_EVENT_SAMPLE_PRIVATE1:
832 g_string_sprintf(msg_str_ptr, "private (hardware dependent) event");
833 break;
834 case SND_SEQ_EVENT_USR0:
835 g_string_sprintf(msg_str_ptr, "user-defined event");
836 break;
837 case SND_SEQ_EVENT_USR1:
838 g_string_sprintf(msg_str_ptr, "user-defined event");
839 break;
840 case SND_SEQ_EVENT_USR2:
841 g_string_sprintf(msg_str_ptr, "user-defined event");
842 break;
843 case SND_SEQ_EVENT_USR3:
844 g_string_sprintf(msg_str_ptr, "user-defined event");
845 break;
846 case SND_SEQ_EVENT_USR4:
847 g_string_sprintf(msg_str_ptr, "user-defined event");
848 break;
849 case SND_SEQ_EVENT_USR5:
850 g_string_sprintf(msg_str_ptr, "user-defined event");
851 break;
852 case SND_SEQ_EVENT_USR6:
853 g_string_sprintf(msg_str_ptr, "user-defined event");
854 break;
855 case SND_SEQ_EVENT_USR7:
856 g_string_sprintf(msg_str_ptr, "user-defined event");
857 break;
858 case SND_SEQ_EVENT_USR8:
859 g_string_sprintf(msg_str_ptr, "user-defined event");
860 break;
861 case SND_SEQ_EVENT_USR9:
862 g_string_sprintf(msg_str_ptr, "user-defined event");
863 break;
864 case SND_SEQ_EVENT_INSTR_BEGIN:
865 g_string_sprintf(msg_str_ptr, "begin of instrument management");
866 break;
867 case SND_SEQ_EVENT_INSTR_END:
868 g_string_sprintf(msg_str_ptr, "end of instrument management");
869 break;
870 case SND_SEQ_EVENT_INSTR_INFO:
871 g_string_sprintf(msg_str_ptr, "query instrument interface info");
872 break;
873 case SND_SEQ_EVENT_INSTR_INFO_RESULT:
874 g_string_sprintf(msg_str_ptr, "result of instrument interface info");
875 break;
876 case SND_SEQ_EVENT_INSTR_FINFO:
877 g_string_sprintf(msg_str_ptr, "query instrument format info");
878 break;
879 case SND_SEQ_EVENT_INSTR_FINFO_RESULT:
880 g_string_sprintf(msg_str_ptr, "result of instrument format info");
881 break;
882 case SND_SEQ_EVENT_INSTR_RESET:
883 g_string_sprintf(msg_str_ptr, "reset instrument instrument memory");
884 break;
885 case SND_SEQ_EVENT_INSTR_STATUS:
886 g_string_sprintf(msg_str_ptr, "get instrument interface status");
887 break;
888 case SND_SEQ_EVENT_INSTR_STATUS_RESULT:
889 g_string_sprintf(msg_str_ptr, "result of instrument interface status");
890 break;
891 case SND_SEQ_EVENT_INSTR_PUT:
892 g_string_sprintf(msg_str_ptr, "put an instrument to port");
893 break;
894 case SND_SEQ_EVENT_INSTR_GET:
895 g_string_sprintf(msg_str_ptr, "get an instrument from port");
896 break;
897 case SND_SEQ_EVENT_INSTR_GET_RESULT:
898 g_string_sprintf(msg_str_ptr, "result of instrument query");
899 break;
900 case SND_SEQ_EVENT_INSTR_FREE:
901 g_string_sprintf(msg_str_ptr, "free instrument(s)");
902 break;
903 case SND_SEQ_EVENT_INSTR_LIST:
904 g_string_sprintf(msg_str_ptr, "get instrument list");
905 break;
906 case SND_SEQ_EVENT_INSTR_LIST_RESULT:
907 g_string_sprintf(msg_str_ptr, "result of instrument list");
908 break;
909 case SND_SEQ_EVENT_INSTR_CLUSTER:
910 g_string_sprintf(msg_str_ptr, "set cluster parameters");
911 break;
912 case SND_SEQ_EVENT_INSTR_CLUSTER_GET:
913 g_string_sprintf(msg_str_ptr, "get cluster parameters");
914 break;
915 case SND_SEQ_EVENT_INSTR_CLUSTER_RESULT:
916 g_string_sprintf(msg_str_ptr, "result of cluster parameters");
917 break;
918 case SND_SEQ_EVENT_INSTR_CHANGE:
919 g_string_sprintf(msg_str_ptr, "instrument change");
920 break;
921 case SND_SEQ_EVENT_SYSEX:
922 /* General MMC decoding, as seen at http://www.borg.com/~jglatt/tech/mmc.htm and
923 extended from "Advanced User Guide for MK-449C MIDI keyboard" info */
924 if (event_ptr->data.ext.len == 6 &&
925 ((guint8 *)event_ptr->data.ext.ptr)[0] == 0xF0 &&
926 ((guint8 *)event_ptr->data.ext.ptr)[1] == 0x7F &&
927 ((guint8 *)event_ptr->data.ext.ptr)[3] == 0x06 &&
928 ((guint8 *)event_ptr->data.ext.ptr)[5] == 0xF7)
930 switch (((guint8 *)event_ptr->data.ext.ptr)[4])
932 case 1:
933 mmc_command_name = "Stop";
934 break;
935 case 2:
936 mmc_command_name = "Play";
937 break;
938 case 3:
939 mmc_command_name = "Deferred Play";
940 break;
941 case 4:
942 mmc_command_name = "Fast Forward";
943 break;
944 case 5:
945 mmc_command_name = "Rewind";
946 break;
947 case 6:
948 mmc_command_name = "Record Strobe (Punch In)";
949 break;
950 case 7:
951 mmc_command_name = "Record Exit (Punch Out)";
952 break;
953 case 8:
954 mmc_command_name = "Record Pause";
955 break;
956 case 9:
957 mmc_command_name = "Pause";
958 break;
959 case 10:
960 mmc_command_name = "Eject";
961 break;
962 case 11:
963 mmc_command_name = "Chase";
964 break;
965 case 12:
966 mmc_command_name = "Command Error Reset";
967 break;
968 case 13:
969 mmc_command_name = "Reset";
970 break;
971 default:
972 goto generic_sysex;
974 g_string_sprintf(
975 msg_str_ptr,
976 "MMC %s, for ",
977 mmc_command_name);
979 if (((guint8 *)event_ptr->data.ext.ptr)[2] == 127)
981 g_string_append(
982 msg_str_ptr,
983 "all devices");
985 else
987 g_string_append_printf(
988 msg_str_ptr,
989 "device %u",
990 (unsigned int)(((guint8 *)event_ptr->data.ext.ptr)[2]));
993 /* The goto MMC message, as seen at http://www.borg.com/~jglatt/tech/mmc.htm*/
994 else if (event_ptr->data.ext.len == 13 &&
995 ((guint8 *)event_ptr->data.ext.ptr)[0] == 0xF0 &&
996 ((guint8 *)event_ptr->data.ext.ptr)[1] == 0x7F &&
997 ((guint8 *)event_ptr->data.ext.ptr)[3] == 0x06 &&
998 ((guint8 *)event_ptr->data.ext.ptr)[4] == 0x44 &&
999 ((guint8 *)event_ptr->data.ext.ptr)[5] == 0x06 &&
1000 ((guint8 *)event_ptr->data.ext.ptr)[6] == 0x01 &&
1001 ((guint8 *)event_ptr->data.ext.ptr)[12] == 0xF7)
1003 g_string_sprintf(
1004 msg_str_ptr,
1005 "MMC goto %u:%u:%u/%u:%u",
1006 (unsigned int)(((guint8 *)event_ptr->data.ext.ptr)[7] & 0x1F),
1007 (unsigned int)(((guint8 *)event_ptr->data.ext.ptr)[8]),
1008 (unsigned int)(((guint8 *)event_ptr->data.ext.ptr)[9]),
1009 (unsigned int)(((guint8 *)event_ptr->data.ext.ptr)[10]),
1010 (unsigned int)(((guint8 *)event_ptr->data.ext.ptr)[11]));
1012 switch (((guint8 *)event_ptr->data.ext.ptr)[7] & 0x60)
1014 case 0:
1015 g_string_append(
1016 msg_str_ptr,
1017 ", 24 fps");
1018 break;
1019 case 1:
1020 g_string_append(
1021 msg_str_ptr,
1022 ", 25 fps");
1023 break;
1024 case 2:
1025 g_string_append(
1026 msg_str_ptr,
1027 ", 29.97 fps");
1028 break;
1029 case 3:
1030 g_string_append(
1031 msg_str_ptr,
1032 ", 30 fps");
1033 break;
1036 if (((guint8 *)event_ptr->data.ext.ptr)[2] == 127)
1038 g_string_append(
1039 msg_str_ptr,
1040 ", for all devices");
1042 else
1044 g_string_append_printf(
1045 msg_str_ptr,
1046 ", for device %u",
1047 (unsigned int)(((guint8 *)event_ptr->data.ext.ptr)[2]));
1050 else
1052 generic_sysex:
1053 g_string_sprintf(
1054 msg_str_ptr,
1055 "SYSEX with size %u:",
1056 (unsigned int)event_ptr->data.ext.len);
1057 for (i = 0 ; i < event_ptr->data.ext.len ; i++)
1059 g_string_append_printf(
1060 msg_str_ptr,
1061 " %02X",
1062 (unsigned int)(((guint8 *)event_ptr->data.ext.ptr)[i]));
1065 break;
1066 case SND_SEQ_EVENT_BOUNCE:
1067 g_string_sprintf(msg_str_ptr, "error event");
1068 break;
1069 case SND_SEQ_EVENT_USR_VAR0:
1070 g_string_sprintf(msg_str_ptr, "reserved for user apps");
1071 break;
1072 case SND_SEQ_EVENT_USR_VAR1:
1073 g_string_sprintf(msg_str_ptr, "reserved for user apps");
1074 break;
1075 case SND_SEQ_EVENT_USR_VAR2:
1076 g_string_sprintf(msg_str_ptr, "reserved for user apps");
1077 break;
1078 case SND_SEQ_EVENT_USR_VAR3:
1079 g_string_sprintf(msg_str_ptr, "reserved for user apps");
1080 break;
1081 case SND_SEQ_EVENT_USR_VAR4:
1082 g_string_sprintf(msg_str_ptr, "reserved for user apps");
1083 break;
1086 /* get GTK thread lock */
1087 gdk_threads_enter();
1089 if (g_row_count >= MAX_LIST_SIZE)
1091 gtk_tree_model_get_iter_first(
1092 GTK_TREE_MODEL(list_store_ptr),
1093 &iter);
1095 gtk_list_store_remove(
1096 list_store_ptr,
1097 &iter);
1100 /* Append an empty row to the list store. Iter will point to the new row */
1101 gtk_list_store_append(list_store_ptr, &iter);
1103 gtk_list_store_set(
1104 list_store_ptr,
1105 &iter,
1106 COL_TIME, time_str_ptr->str,
1107 COL_CHANNEL, channel_str_ptr->str,
1108 COL_MESSAGE, msg_str_ptr->str,
1109 -1);
1111 g_string_free(channel_str_ptr, TRUE);
1112 g_string_free(msg_str_ptr, TRUE);
1114 gtk_tree_view_scroll_to_cell(
1115 GTK_TREE_VIEW(child_ptr),
1116 gtk_tree_model_get_path(
1117 gtk_tree_view_get_model(GTK_TREE_VIEW(child_ptr)),
1118 &iter),
1119 NULL,
1120 TRUE,
1121 0.0,
1122 1.0);
1124 /* Force update of scroll position. */
1125 /* Is it a bug that it does not update automagically ? */
1126 gtk_container_check_resize(GTK_CONTAINER(child_ptr));
1128 /* release GTK thread lock */
1129 gdk_threads_leave();
1131 g_row_count++;
1134 return NULL;
1137 void
1138 process_lash_event(lash_event_t * event_ptr)
1140 enum LASH_Event_Type type;
1141 const char * str;
1143 type = lash_event_get_type(event_ptr);
1144 str = lash_event_get_string(event_ptr);
1146 switch (type)
1148 case LASH_Quit:
1149 g_warning("LASH_Quit received.\n");
1150 g_lashc = NULL;
1151 gtk_main_quit();
1152 break;
1153 case LASH_Save_File:
1154 case LASH_Restore_File:
1155 case LASH_Save_Data_Set:
1156 default:
1157 g_warning("LASH Event. Type = %u, string = \"%s\"\n",
1158 (unsigned int)type,
1159 (str == NULL)?"":str);
1163 void
1164 process_lash_config(lash_config_t * config_ptr)
1166 const char * key;
1167 const void * data;
1168 size_t data_size;
1170 key = lash_config_get_key(config_ptr);
1171 data = lash_config_get_value(config_ptr);
1172 data_size = lash_config_get_value_size(config_ptr);
1174 g_warning("LASH Config. Key = \"%s\"\n", key);
1177 /* process lash events callback */
1178 gboolean
1179 process_lash_events(gpointer data)
1181 lash_event_t * event_ptr;
1182 lash_config_t * config_ptr;
1184 /* Process events */
1185 while ((event_ptr = lash_get_event(g_lashc)) != NULL)
1187 process_lash_event(event_ptr);
1188 lash_event_destroy(event_ptr);
1191 /* Process configs */
1192 while ((config_ptr = lash_get_config(g_lashc)) != NULL)
1194 process_lash_config(config_ptr);
1195 lash_config_destroy(config_ptr);
1198 return TRUE;
1202 main(int argc, char *argv[])
1204 int ret;
1205 snd_seq_port_info_t * port_info = NULL;
1206 pthread_t midi_tid;
1207 lash_event_t * lash_event_ptr;
1208 GString * seq_client_name_str_ptr;
1210 /* init threads */
1211 g_thread_init(NULL);
1212 gdk_threads_init();
1213 gdk_threads_enter();
1215 gtk_init(&argc, &argv);
1217 path_init(argv[0]);
1219 g_lashc = lash_init(
1220 lash_extract_args(&argc, &argv),
1221 "gmidimonitor",
1223 LASH_PROTOCOL_VERSION);
1225 if (g_lashc == NULL)
1227 g_warning("Failed to connect to LASH. Session management will not occur.\n");
1229 else
1231 lash_event_ptr = lash_event_new_with_type(LASH_Client_Name);
1232 lash_event_set_string(lash_event_ptr, "GMIDImonitor");
1233 lash_send_event(g_lashc, lash_event_ptr);
1234 g_timeout_add(250, process_lash_events, NULL);
1237 /* interface creation */
1238 create_mainwindow();
1240 /* initialisation */
1241 ret = snd_seq_open(
1242 &g_seq_ptr,
1243 "default",
1244 SND_SEQ_OPEN_INPUT,
1246 if (ret < 0)
1248 g_warning("Cannot open sequncer, %s\n", snd_strerror(ret));
1249 goto path_uninit;
1252 seq_client_name_str_ptr = g_string_new("");
1253 g_string_sprintf(seq_client_name_str_ptr, "MIDI monitor (%u)", (unsigned int)getpid());
1254 snd_seq_set_client_name(g_seq_ptr, seq_client_name_str_ptr->str);
1256 lash_alsa_client_id(g_lashc, snd_seq_client_id(g_seq_ptr));
1258 snd_seq_port_info_alloca(&port_info);
1260 snd_seq_port_info_set_capability(
1261 port_info,
1262 SND_SEQ_PORT_CAP_WRITE |
1263 SND_SEQ_PORT_CAP_SUBS_WRITE);
1264 snd_seq_port_info_set_type(
1265 port_info,
1266 SND_SEQ_PORT_TYPE_APPLICATION);
1267 snd_seq_port_info_set_midi_channels(port_info, 16);
1268 snd_seq_port_info_set_port_specified(port_info, 1);
1270 snd_seq_port_info_set_name(port_info, "midi in");
1271 snd_seq_port_info_set_port(port_info, 0);
1273 ret = snd_seq_create_port(g_seq_ptr, port_info);
1274 if (ret < 0)
1276 g_warning("Error creating ALSA sequencer port, %s\n", snd_strerror(ret));
1277 goto close_seq;
1280 /* Start midi thread */
1281 ret = pthread_create(&midi_tid, NULL, midi_thread, NULL);
1283 /* main loop */
1284 gtk_main();
1286 /* Cancel the thread. Don't know better way.
1287 Poll or unblock mechanisms seem to not be
1288 available for alsa sequencer */
1289 pthread_cancel(midi_tid);
1291 /* Wait midi thread to finish */
1292 ret = pthread_join(midi_tid, NULL);
1294 gdk_threads_leave();
1296 close_seq:
1297 ret = snd_seq_close(g_seq_ptr);
1298 if (ret < 0)
1300 g_warning("Cannot close sequncer, %s\n", snd_strerror(ret));
1303 path_uninit:
1304 path_init(argv[0]);
1306 return 0;