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