merge resolution with master
[ardour.git] / gtk2_ardour / gtk_pianokeyboard.c
1 /*-
2  * Copyright (c) 2007, 2008 Edward Tomasz Napiera�ła <trasz@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 /*
28  * This is piano_keyboard, piano keyboard-like GTK+ widget.  It contains
29  * no MIDI-specific code.
30  *
31  * For questions and comments, contact Edward Tomasz Napierala <trasz@FreeBSD.org>.
32  */
33
34 #include <assert.h>
35 #include <string.h>
36 #include <strings.h>
37 #include <stdint.h>
38 #include <cairo/cairo.h>
39 #include <gtk/gtk.h>
40 #include <gdk/gdkkeysyms.h>
41
42 #include "gtk_pianokeyboard.h"
43
44 #define PIANO_KEYBOARD_DEFAULT_WIDTH 730
45 #define PIANO_KEYBOARD_DEFAULT_HEIGHT 70
46
47 enum {
48         NOTE_ON_SIGNAL,
49         NOTE_OFF_SIGNAL,
50         REST_SIGNAL,
51         LAST_SIGNAL
52 };
53
54 static guint    piano_keyboard_signals[LAST_SIGNAL] = { 0 };
55
56 static void
57 draw_keyboard_cue(PianoKeyboard *pk, cairo_t* cr)
58 {
59         int             w = pk->notes[0].w;
60         int             h = pk->notes[0].h;
61
62         int             first_note_in_lower_row = (pk->octave + 5) * 12;
63         int             last_note_in_lower_row = (pk->octave + 6) * 12 - 1;
64         int             first_note_in_higher_row = (pk->octave + 6) * 12;
65         int             last_note_in_higher_row = (pk->octave + 7) * 12 + 4;
66
67         cairo_set_source_rgb (cr, 1.0f, 0.0f, 0.0f);
68         cairo_move_to (cr, pk->notes[first_note_in_lower_row].x + 3, h - 6);
69         cairo_line_to (cr, pk->notes[last_note_in_lower_row].x + w - 3, h - 6);
70         cairo_stroke (cr);
71
72         cairo_set_source_rgb (cr, 0.0f, 0.0f, 1.0f);
73         cairo_move_to (cr, pk->notes[first_note_in_higher_row].x + 3, h - 9);
74         cairo_line_to (cr, pk->notes[last_note_in_higher_row].x + w - 3, h - 9);
75         cairo_stroke (cr);
76 }
77
78 static void
79 queue_note_draw (PianoKeyboard* pk, int note)
80 {
81         GdkWindow* w = GTK_WIDGET(pk)->window;
82
83         if (w) {
84                 GdkRectangle r;
85
86                 r.x = pk->notes[note].x;
87                 r.y = 0;
88                 r.width = pk->notes[note].w;
89                 r.height = pk->notes[note].h;
90
91                 gdk_window_invalidate_rect (w, &r, TRUE);
92         }
93 }
94
95 static void
96 draw_note(PianoKeyboard *pk, cairo_t* cr, int note)
97 {
98         int             is_white = pk->notes[note].white;
99
100         int             x = pk->notes[note].x;
101         int             w = pk->notes[note].w;
102         int             h = pk->notes[note].h;
103
104         if (pk->notes[note].pressed || pk->notes[note].sustained) {
105                 if (is_white) {
106                         cairo_set_source_rgb (cr, 0.60f, 0.60f, 0.60f);
107                 } else {
108                         cairo_set_source_rgb (cr, 0.50f, 0.50f, 0.50f);
109                 }
110         } else {
111                 if (is_white) {
112                         cairo_set_source_rgb (cr, 1.0f, 1.0f, 1.0f);
113                 } else {
114                         cairo_set_source_rgb (cr, 0.0f, 0.0f, 0.0f);
115                 }
116         }
117
118         cairo_set_line_width (cr, 1.0);
119
120         cairo_rectangle (cr, x, 0, w, h);
121         cairo_fill (cr);
122
123         cairo_set_source_rgb(cr, 0.0f, 0.0f, 0.0f); /* black outline */
124         cairo_rectangle (cr, x, 0, w, h);
125         cairo_stroke (cr);
126
127         if (pk->enable_keyboard_cue) {
128                 draw_keyboard_cue (pk, cr);
129         }
130
131         /* We need to redraw black keys that partially obscure the white one. */
132         if (note < NNOTES - 2 && !pk->notes[note + 1].white) {
133                 draw_note(pk, cr, note + 1);
134         }
135
136         if (note > 0 && !pk->notes[note - 1].white) {
137                 draw_note(pk, cr, note - 1);
138         }
139 }
140
141 static int
142 press_key(PianoKeyboard *pk, int key)
143 {
144         assert(key >= 0);
145         assert(key < NNOTES);
146
147         pk->maybe_stop_sustained_notes = 0;
148
149         /* This is for keyboard autorepeat protection. */
150         if (pk->notes[key].pressed)
151                 return 0;
152
153         if (pk->sustain_new_notes)
154                 pk->notes[key].sustained = 1;
155         else
156                 pk->notes[key].sustained = 0;
157
158         pk->notes[key].pressed = 1;
159
160         g_signal_emit_by_name(GTK_WIDGET(pk), "note-on", key);
161         queue_note_draw(pk, key);
162
163         return 1;
164 }
165
166 static int
167 release_key(PianoKeyboard *pk, int key)
168 {
169         assert(key >= 0);
170         assert(key < NNOTES);
171
172         pk->maybe_stop_sustained_notes = 0;
173
174         if (!pk->notes[key].pressed)
175                 return 0;
176
177         if (pk->sustain_new_notes)
178                 pk->notes[key].sustained = 1;
179
180         pk->notes[key].pressed = 0;
181
182         if (pk->notes[key].sustained)
183                 return 0;
184
185         g_signal_emit_by_name(GTK_WIDGET(pk), "note-off", key);
186         queue_note_draw(pk, key);
187
188         return 1;
189 }
190
191 static void
192 rest (PianoKeyboard* pk)
193 {
194         g_signal_emit_by_name(GTK_WIDGET(pk), "rest");
195 }
196
197 static void
198 stop_unsustained_notes(PianoKeyboard *pk)
199 {
200         int             i;
201
202         for (i = 0; i < NNOTES; i++) {
203                 if (pk->notes[i].pressed && !pk->notes[i].sustained) {
204                         pk->notes[i].pressed = 0;
205                         g_signal_emit_by_name(GTK_WIDGET(pk), "note-off", i);
206                         queue_note_draw(pk, i);
207                 }
208         }
209 }
210
211 static void
212 stop_sustained_notes(PianoKeyboard *pk)
213 {
214         int             i;
215
216         for (i = 0; i < NNOTES; i++) {
217                 if (pk->notes[i].sustained) {
218                         pk->notes[i].pressed = 0;
219                         pk->notes[i].sustained = 0;
220                         g_signal_emit_by_name(GTK_WIDGET(pk), "note-off", i);
221                         queue_note_draw(pk, i);
222                 }
223         }
224 }
225
226 static int
227 key_binding(PianoKeyboard *pk, const char *key)
228 {
229         gpointer notused, note;
230         gboolean found;
231
232         assert(pk->key_bindings != NULL);
233
234         found = g_hash_table_lookup_extended(pk->key_bindings, key, &notused, &note);
235
236         if (!found)
237                 return -1;
238
239         return (intptr_t)note;
240 }
241
242 static void
243 bind_key(PianoKeyboard *pk, const char *key, int note)
244 {
245         assert(pk->key_bindings != NULL);
246
247         g_hash_table_insert(pk->key_bindings, key, (gpointer)((intptr_t)note));
248 }
249
250 static void
251 clear_notes(PianoKeyboard *pk)
252 {
253         assert(pk->key_bindings != NULL);
254
255         g_hash_table_remove_all(pk->key_bindings);
256 }
257
258 static void
259 bind_keys_qwerty(PianoKeyboard *pk)
260 {
261         clear_notes(pk);
262
263         bind_key(pk, "space", 128);
264
265         /* Lower keyboard row - "zxcvbnm". */
266         bind_key(pk, "z", 12);  /* C0 */
267         bind_key(pk, "s", 13);
268         bind_key(pk, "x", 14);
269         bind_key(pk, "d", 15);
270         bind_key(pk, "c", 16);
271         bind_key(pk, "v", 17);
272         bind_key(pk, "g", 18);
273         bind_key(pk, "b", 19);
274         bind_key(pk, "h", 20);
275         bind_key(pk, "n", 21);
276         bind_key(pk, "j", 22);
277         bind_key(pk, "m", 23);
278
279         /* Upper keyboard row, first octave - "qwertyu". */
280         bind_key(pk, "q", 24);
281         bind_key(pk, "2", 25);
282         bind_key(pk, "w", 26);
283         bind_key(pk, "3", 27);
284         bind_key(pk, "e", 28);
285         bind_key(pk, "r", 29);
286         bind_key(pk, "5", 30);
287         bind_key(pk, "t", 31);
288         bind_key(pk, "6", 32);
289         bind_key(pk, "y", 33);
290         bind_key(pk, "7", 34);
291         bind_key(pk, "u", 35);
292
293         /* Upper keyboard row, the rest - "iop". */
294         bind_key(pk, "i", 36);
295         bind_key(pk, "9", 37);
296         bind_key(pk, "o", 38);
297         bind_key(pk, "0", 39);
298         bind_key(pk, "p", 40);
299 }
300
301 static void
302 bind_keys_qwertz(PianoKeyboard *pk)
303 {
304         bind_keys_qwerty(pk);
305
306         /* The only difference between QWERTY and QWERTZ is that the "y" and "z" are swapped together. */
307         bind_key(pk, "y", 12);
308         bind_key(pk, "z", 33);
309 }
310
311 static void
312 bind_keys_azerty(PianoKeyboard *pk)
313 {
314         clear_notes(pk);
315
316         bind_key(pk, "space", 128);
317
318         /* Lower keyboard row - "wxcvbn,". */
319         bind_key(pk, "w", 12);  /* C0 */
320         bind_key(pk, "s", 13);
321         bind_key(pk, "x", 14);
322         bind_key(pk, "d", 15);
323         bind_key(pk, "c", 16);
324         bind_key(pk, "v", 17);
325         bind_key(pk, "g", 18);
326         bind_key(pk, "b", 19);
327         bind_key(pk, "h", 20);
328         bind_key(pk, "n", 21);
329         bind_key(pk, "j", 22);
330         bind_key(pk, "comma", 23);
331
332         /* Upper keyboard row, first octave - "azertyu". */
333         bind_key(pk, "a", 24);
334         bind_key(pk, "eacute", 25);
335         bind_key(pk, "z", 26);
336         bind_key(pk, "quotedbl", 27);
337         bind_key(pk, "e", 28);
338         bind_key(pk, "r", 29);
339         bind_key(pk, "parenleft", 30);
340         bind_key(pk, "t", 31);
341         bind_key(pk, "minus", 32);
342         bind_key(pk, "y", 33);
343         bind_key(pk, "egrave", 34);
344         bind_key(pk, "u", 35);
345
346         /* Upper keyboard row, the rest - "iop". */
347         bind_key(pk, "i", 36);
348         bind_key(pk, "ccedilla", 37);
349         bind_key(pk, "o", 38);
350         bind_key(pk, "agrave", 39);
351         bind_key(pk, "p", 40);
352 }
353
354 static gint
355 keyboard_event_handler(GtkWidget *mk, GdkEventKey *event, gpointer ignored)
356 {
357         int             note;
358         char            *key;
359         guint           keyval;
360         GdkKeymapKey    kk;
361         PianoKeyboard   *pk = PIANO_KEYBOARD(mk);
362
363         (void) ignored;
364
365         /* We're not using event->keyval, because we need keyval with level set to 0.
366            E.g. if user holds Shift and presses '7', we want to get a '7', not '&'. */
367         kk.keycode = event->hardware_keycode;
368         kk.level = 0;
369         kk.group = 0;
370
371         keyval = gdk_keymap_lookup_key(NULL, &kk);
372
373         key = gdk_keyval_name(gdk_keyval_to_lower(keyval));
374
375         if (key == NULL) {
376                 g_message("gtk_keyval_name() returned NULL; please report this.");
377                 return FALSE;
378         }
379
380         note = key_binding(pk, key);
381
382         if (note < 0) {
383                 /* Key was not bound.  Maybe it's one of the keys handled in jack-keyboard.c. */
384                 return FALSE;
385         }
386
387         if (note == 128) {
388                 if (event->type == GDK_KEY_RELEASE) {
389                         rest (pk);
390                 }
391
392                 return TRUE;
393         }
394
395         note += pk->octave * 12;
396
397         assert(note >= 0);
398         assert(note < NNOTES);
399
400         if (event->type == GDK_KEY_PRESS) {
401                 press_key(pk, note);
402
403         } else if (event->type == GDK_KEY_RELEASE) {
404                 release_key(pk, note);
405         }
406
407         return TRUE;
408 }
409
410 static int
411 get_note_for_xy(PianoKeyboard *pk, int x, int y)
412 {
413         int             height = GTK_WIDGET(pk)->allocation.height;
414         int             note;
415
416         if (y <= height / 2) {
417                 for (note = 0; note < NNOTES - 1; note++) {
418                         if (pk->notes[note].white)
419                                 continue;
420
421                         if (x >= pk->notes[note].x && x <= pk->notes[note].x + pk->notes[note].w)
422                                 return note;
423                 }
424         }
425
426         for (note = 0; note < NNOTES - 1; note++) {
427                 if (!pk->notes[note].white)
428                         continue;
429
430                 if (x >= pk->notes[note].x && x <= pk->notes[note].x + pk->notes[note].w)
431                         return note;
432         }
433
434         return -1;
435 }
436
437 static gboolean
438 mouse_button_event_handler(PianoKeyboard *pk, GdkEventButton *event, gpointer ignored)
439 {
440         int             x = event->x;
441         int             y = event->y;
442
443         int             note = get_note_for_xy(pk, x, y);
444
445         (void) ignored;
446
447         if (event->button != 1)
448                 return TRUE;
449
450         if (event->type == GDK_BUTTON_PRESS) {
451                 /* This is possible when you make the window a little wider and then click
452                    on the grey area. */
453                 if (note < 0) {
454                         return TRUE;
455                 }
456
457                 if (pk->note_being_pressed_using_mouse >= 0)
458                         release_key(pk, pk->note_being_pressed_using_mouse);
459
460                 press_key(pk, note);
461                 pk->note_being_pressed_using_mouse = note;
462
463         } else if (event->type == GDK_BUTTON_RELEASE) {
464                 if (note >= 0) {
465                         release_key(pk, note);
466
467                 } else {
468                         if (pk->note_being_pressed_using_mouse >= 0)
469                                 release_key(pk, pk->note_being_pressed_using_mouse);
470                 }
471
472                 pk->note_being_pressed_using_mouse = -1;
473
474         }
475
476         return TRUE;
477 }
478
479 static gboolean
480 mouse_motion_event_handler(PianoKeyboard *pk, GdkEventMotion *event, gpointer ignored)
481 {
482         int             note;
483
484         (void) ignored;
485
486         if ((event->state & GDK_BUTTON1_MASK) == 0)
487                 return TRUE;
488
489         note = get_note_for_xy(pk, event->x, event->y);
490
491         if (note != pk->note_being_pressed_using_mouse && note >= 0) {
492
493                 if (pk->note_being_pressed_using_mouse >= 0)
494                         release_key(pk, pk->note_being_pressed_using_mouse);
495                 press_key(pk, note);
496                 pk->note_being_pressed_using_mouse = note;
497         }
498
499         return TRUE;
500 }
501
502 static gboolean
503 piano_keyboard_expose(GtkWidget *widget, GdkEventExpose *event)
504 {
505         int i;
506         PianoKeyboard *pk = PIANO_KEYBOARD(widget);
507         cairo_t* cr = gdk_cairo_create (GDK_DRAWABLE (GTK_WIDGET(pk)->window));
508
509         gdk_cairo_region (cr, event->region);
510         cairo_clip (cr);
511
512         for (i = 0; i < NNOTES; i++) {
513                 GdkRectangle r;
514
515                 r.x = pk->notes[i].x;
516                 r.y = 0;
517                 r.width = pk->notes[i].w;
518                 r.height = pk->notes[i].h;
519
520                 switch (gdk_region_rect_in (event->region, &r)) {
521                 case GDK_OVERLAP_RECTANGLE_PART:
522                 case GDK_OVERLAP_RECTANGLE_IN:
523                         draw_note (pk, cr, i);
524                         break;
525                 default:
526                         break;
527                 }
528         }
529
530         cairo_destroy (cr);
531
532         return TRUE;
533 }
534
535 static void
536 piano_keyboard_size_request(GtkWidget* w, GtkRequisition *requisition)
537 {
538         (void) w;
539
540         requisition->width = PIANO_KEYBOARD_DEFAULT_WIDTH;
541         requisition->height = PIANO_KEYBOARD_DEFAULT_HEIGHT;
542 }
543
544 static void
545 recompute_dimensions(PianoKeyboard *pk)
546 {
547         int             number_of_white_keys = (NNOTES - 1) * (7.0 / 12.0);
548
549         int             key_width;
550         int             black_key_width;
551         int             useful_width;
552
553         int             note;
554         int             white_key = 0;
555         int             note_in_octave;
556
557         int             width = GTK_WIDGET(pk)->allocation.width;
558         int             height = GTK_WIDGET(pk)->allocation.height;
559
560         key_width = width / number_of_white_keys;
561         black_key_width = key_width * 0.8;
562         useful_width = number_of_white_keys * key_width;
563         pk->widget_margin = (width - useful_width) / 2;
564
565         for (note = 0, white_key = 0; note < NNOTES - 2; note++) {
566                 note_in_octave = note % 12;
567
568                 if (note_in_octave == 1 || note_in_octave == 3 || note_in_octave == 6 ||
569                         note_in_octave == 8 || note_in_octave == 10) {
570
571                         /* This note is black key. */
572                         pk->notes[note].x = pk->widget_margin + white_key * key_width - black_key_width / 2;
573                         pk->notes[note].w = black_key_width;
574                         pk->notes[note].h = height / 2;
575                         pk->notes[note].white = 0;
576
577                         continue;
578                 }
579
580                 /* This note is white key. */
581                 pk->notes[note].x = pk->widget_margin + white_key * key_width;
582                 pk->notes[note].w = key_width;
583                 pk->notes[note].h = height;
584                 pk->notes[note].white = 1;
585
586                 white_key++;
587         }
588 }
589
590 static void
591 piano_keyboard_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
592 {
593         /* XXX: Are these two needed? */
594         g_return_if_fail(widget != NULL);
595         g_return_if_fail(allocation != NULL);
596
597         widget->allocation = *allocation;
598
599         recompute_dimensions(PIANO_KEYBOARD(widget));
600
601         if (GTK_WIDGET_REALIZED(widget)) {
602                 gdk_window_move_resize (widget->window, allocation->x, allocation->y, allocation->width, allocation->height);
603         }
604 }
605
606 static void
607 piano_keyboard_class_init(PianoKeyboardClass *klass)
608 {
609         GtkWidgetClass  *widget_klass;
610
611         /* Set up signals. */
612         piano_keyboard_signals[NOTE_ON_SIGNAL] = g_signal_new ("note-on",
613                 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
614                 0, NULL, NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
615
616         piano_keyboard_signals[NOTE_OFF_SIGNAL] = g_signal_new ("note-off",
617                 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
618                 0, NULL, NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
619
620         piano_keyboard_signals[REST_SIGNAL] = g_signal_new ("rest",
621                 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
622                 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
623
624         widget_klass = (GtkWidgetClass*) klass;
625
626         widget_klass->expose_event = piano_keyboard_expose;
627         widget_klass->size_request = piano_keyboard_size_request;
628         widget_klass->size_allocate = piano_keyboard_size_allocate;
629 }
630
631 static void
632 piano_keyboard_init(GtkWidget *mk)
633 {
634         gtk_widget_add_events(mk, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK);
635
636         g_signal_connect(G_OBJECT(mk), "button-press-event", G_CALLBACK(mouse_button_event_handler), NULL);
637         g_signal_connect(G_OBJECT(mk), "button-release-event", G_CALLBACK(mouse_button_event_handler), NULL);
638         g_signal_connect(G_OBJECT(mk), "motion-notify-event", G_CALLBACK(mouse_motion_event_handler), NULL);
639         g_signal_connect(G_OBJECT(mk), "key-press-event", G_CALLBACK(keyboard_event_handler), NULL);
640         g_signal_connect(G_OBJECT(mk), "key-release-event", G_CALLBACK(keyboard_event_handler), NULL);
641 }
642
643 GType
644 piano_keyboard_get_type(void)
645 {
646         static GType mk_type = 0;
647
648         if (!mk_type) {
649                 static const GTypeInfo mk_info = {
650                         sizeof(PianoKeyboardClass),
651                         NULL, /* base_init */
652                         NULL, /* base_finalize */
653                         (GClassInitFunc) piano_keyboard_class_init,
654                         NULL, /* class_finalize */
655                         NULL, /* class_data */
656                         sizeof (PianoKeyboard),
657                         0,    /* n_preallocs */
658                         (GInstanceInitFunc) piano_keyboard_init,
659                         0,    /* value_table */
660                 };
661
662                 mk_type = g_type_register_static(GTK_TYPE_DRAWING_AREA, "PianoKeyboard", &mk_info, 0);
663         }
664
665         return mk_type;
666 }
667
668 GtkWidget *
669 piano_keyboard_new(void)
670 {
671         GtkWidget *widget = gtk_type_new(piano_keyboard_get_type());
672
673         PianoKeyboard *pk = PIANO_KEYBOARD(widget);
674
675         pk->maybe_stop_sustained_notes = 0;
676         pk->sustain_new_notes = 0;
677         pk->enable_keyboard_cue = 0;
678         pk->octave = 4;
679         pk->note_being_pressed_using_mouse = -1;
680         memset((void *)pk->notes, 0, sizeof(struct Note) * NNOTES);
681         pk->key_bindings = g_hash_table_new(g_str_hash, g_str_equal);
682         bind_keys_qwerty(pk);
683
684         return widget;
685 }
686
687 void
688 piano_keyboard_set_keyboard_cue(PianoKeyboard *pk, int enabled)
689 {
690         pk->enable_keyboard_cue = enabled;
691 }
692
693 void
694 piano_keyboard_sustain_press(PianoKeyboard *pk)
695 {
696         if (!pk->sustain_new_notes) {
697                 pk->sustain_new_notes = 1;
698                 pk->maybe_stop_sustained_notes = 1;
699         }
700 }
701
702 void
703 piano_keyboard_sustain_release(PianoKeyboard *pk)
704 {
705         if (pk->maybe_stop_sustained_notes)
706                 stop_sustained_notes(pk);
707
708         pk->sustain_new_notes = 0;
709 }
710
711 void
712 piano_keyboard_set_note_on(PianoKeyboard *pk, int note)
713 {
714         if (pk->notes[note].pressed == 0) {
715                 pk->notes[note].pressed = 1;
716                 queue_note_draw (pk, note);
717         }
718 }
719
720 void
721 piano_keyboard_set_note_off(PianoKeyboard *pk, int note)
722 {
723         if (pk->notes[note].pressed || pk->notes[note].sustained) {
724                 pk->notes[note].pressed = 0;
725                 pk->notes[note].sustained = 0;
726                 queue_note_draw (pk, note);
727         }
728 }
729
730 void
731 piano_keyboard_set_octave(PianoKeyboard *pk, int octave)
732 {
733         stop_unsustained_notes(pk);
734         pk->octave = octave;
735         gtk_widget_queue_draw(GTK_WIDGET(pk));
736 }
737
738 gboolean
739 piano_keyboard_set_keyboard_layout(PianoKeyboard *pk, const char *layout)
740 {
741         assert(layout);
742
743         if (!strcasecmp(layout, "QWERTY")) {
744                 bind_keys_qwerty(pk);
745
746         } else if (!strcasecmp(layout, "QWERTZ")) {
747                 bind_keys_qwertz(pk);
748
749         } else if (!strcasecmp(layout, "AZERTY")) {
750                 bind_keys_azerty(pk);
751
752         } else {
753                 /* Unknown layout name. */
754                 return TRUE;
755         }
756
757         return FALSE;
758 }