2 * Copyright (c) 2007, 2008 Edward Tomasz NapieraĆa <trasz@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 * This is piano_keyboard, piano keyboard-like GTK+ widget. It contains
29 * no MIDI-specific code.
31 * For questions and comments, contact Edward Tomasz Napierala <trasz@FreeBSD.org>.
38 #include <cairo/cairo.h>
40 #include <gdk/gdkkeysyms.h>
42 #include "gtk_pianokeyboard.h"
44 #define PIANO_KEYBOARD_DEFAULT_WIDTH 730
45 #define PIANO_KEYBOARD_DEFAULT_HEIGHT 70
53 static guint piano_keyboard_signals[LAST_SIGNAL] = { 0 };
56 draw_keyboard_cue(PianoKeyboard *pk, cairo_t* cr)
58 int w = pk->notes[0].w;
59 int h = pk->notes[0].h;
61 int first_note_in_lower_row = (pk->octave + 5) * 12;
62 int last_note_in_lower_row = (pk->octave + 6) * 12 - 1;
63 int first_note_in_higher_row = (pk->octave + 6) * 12;
64 int last_note_in_higher_row = (pk->octave + 7) * 12 + 4;
66 cairo_set_source_rgb (cr, 1.0f, 0.0f, 0.0f);
67 cairo_move_to (cr, pk->notes[first_note_in_lower_row].x + 3, h - 6);
68 cairo_line_to (cr, pk->notes[last_note_in_lower_row].x + w - 3, h - 6);
71 cairo_set_source_rgb (cr, 0.0f, 0.0f, 1.0f);
72 cairo_move_to (cr, pk->notes[first_note_in_higher_row].x + 3, h - 9);
73 cairo_line_to (cr, pk->notes[last_note_in_higher_row].x + w - 3, h - 9);
78 queue_note_draw (PianoKeyboard* pk, int note)
80 GdkWindow* w = GTK_WIDGET(pk)->window;
85 r.x = pk->notes[note].x;
87 r.width = pk->notes[note].w;
88 r.height = pk->notes[note].h;
90 gdk_window_invalidate_rect (w, &r, TRUE);
95 draw_note(PianoKeyboard *pk, cairo_t* cr, int note)
97 int is_white = pk->notes[note].white;
99 int x = pk->notes[note].x;
100 int w = pk->notes[note].w;
101 int h = pk->notes[note].h;
103 if (pk->notes[note].pressed || pk->notes[note].sustained) {
104 is_white = !is_white;
107 cairo_set_line_width (cr, 1.0);
110 cairo_set_source_rgb (cr, 1.0f, 1.0f, 1.0f);
112 cairo_set_source_rgb (cr, 0.0f, 0.0f, 0.0f);
115 cairo_rectangle (cr, x, 0, w, h);
118 cairo_set_source_rgb(cr, 0.0f, 0.0f, 0.0f); /* black outline */
119 cairo_rectangle (cr, x, 0, w, h);
122 if (pk->enable_keyboard_cue) {
123 draw_keyboard_cue (pk, cr);
126 /* We need to redraw black keys that partially obscure the white one. */
127 if (note < NNOTES - 2 && !pk->notes[note + 1].white) {
128 draw_note(pk, cr, note + 1);
131 if (note > 0 && !pk->notes[note - 1].white) {
132 draw_note(pk, cr, note - 1);
137 press_key(PianoKeyboard *pk, int key)
140 assert(key < NNOTES);
142 pk->maybe_stop_sustained_notes = 0;
144 /* This is for keyboard autorepeat protection. */
145 if (pk->notes[key].pressed)
148 if (pk->sustain_new_notes)
149 pk->notes[key].sustained = 1;
151 pk->notes[key].sustained = 0;
153 pk->notes[key].pressed = 1;
155 g_signal_emit_by_name(GTK_WIDGET(pk), "note-on", key);
156 queue_note_draw(pk, key);
162 release_key(PianoKeyboard *pk, int key)
165 assert(key < NNOTES);
167 pk->maybe_stop_sustained_notes = 0;
169 if (!pk->notes[key].pressed)
172 if (pk->sustain_new_notes)
173 pk->notes[key].sustained = 1;
175 pk->notes[key].pressed = 0;
177 if (pk->notes[key].sustained)
180 g_signal_emit_by_name(GTK_WIDGET(pk), "note-off", key);
181 queue_note_draw(pk, key);
187 stop_unsustained_notes(PianoKeyboard *pk)
191 for (i = 0; i < NNOTES; i++) {
192 if (pk->notes[i].pressed && !pk->notes[i].sustained) {
193 pk->notes[i].pressed = 0;
194 g_signal_emit_by_name(GTK_WIDGET(pk), "note-off", i);
195 queue_note_draw(pk, i);
201 stop_sustained_notes(PianoKeyboard *pk)
205 for (i = 0; i < NNOTES; i++) {
206 if (pk->notes[i].sustained) {
207 pk->notes[i].pressed = 0;
208 pk->notes[i].sustained = 0;
209 g_signal_emit_by_name(GTK_WIDGET(pk), "note-off", i);
210 queue_note_draw(pk, i);
216 key_binding(PianoKeyboard *pk, const char *key)
218 gpointer notused, note;
221 assert(pk->key_bindings != NULL);
223 found = g_hash_table_lookup_extended(pk->key_bindings, key, ¬used, ¬e);
228 return (intptr_t)note;
232 bind_key(PianoKeyboard *pk, const char *key, int note)
234 assert(pk->key_bindings != NULL);
236 g_hash_table_insert(pk->key_bindings, (gpointer)key, (gpointer)((intptr_t)note));
240 clear_notes(PianoKeyboard *pk)
242 assert(pk->key_bindings != NULL);
244 g_hash_table_remove_all(pk->key_bindings);
248 bind_keys_qwerty(PianoKeyboard *pk)
252 /* Lower keyboard row - "zxcvbnm". */
253 bind_key(pk, "z", 12); /* C0 */
254 bind_key(pk, "s", 13);
255 bind_key(pk, "x", 14);
256 bind_key(pk, "d", 15);
257 bind_key(pk, "c", 16);
258 bind_key(pk, "v", 17);
259 bind_key(pk, "g", 18);
260 bind_key(pk, "b", 19);
261 bind_key(pk, "h", 20);
262 bind_key(pk, "n", 21);
263 bind_key(pk, "j", 22);
264 bind_key(pk, "m", 23);
266 /* Upper keyboard row, first octave - "qwertyu". */
267 bind_key(pk, "q", 24);
268 bind_key(pk, "2", 25);
269 bind_key(pk, "w", 26);
270 bind_key(pk, "3", 27);
271 bind_key(pk, "e", 28);
272 bind_key(pk, "r", 29);
273 bind_key(pk, "5", 30);
274 bind_key(pk, "t", 31);
275 bind_key(pk, "6", 32);
276 bind_key(pk, "y", 33);
277 bind_key(pk, "7", 34);
278 bind_key(pk, "u", 35);
280 /* Upper keyboard row, the rest - "iop". */
281 bind_key(pk, "i", 36);
282 bind_key(pk, "9", 37);
283 bind_key(pk, "o", 38);
284 bind_key(pk, "0", 39);
285 bind_key(pk, "p", 40);
289 bind_keys_qwertz(PianoKeyboard *pk)
291 bind_keys_qwerty(pk);
293 /* The only difference between QWERTY and QWERTZ is that the "y" and "z" are swapped together. */
294 bind_key(pk, "y", 12);
295 bind_key(pk, "z", 33);
299 bind_keys_azerty(PianoKeyboard *pk)
303 /* Lower keyboard row - "wxcvbn,". */
304 bind_key(pk, "w", 12); /* C0 */
305 bind_key(pk, "s", 13);
306 bind_key(pk, "x", 14);
307 bind_key(pk, "d", 15);
308 bind_key(pk, "c", 16);
309 bind_key(pk, "v", 17);
310 bind_key(pk, "g", 18);
311 bind_key(pk, "b", 19);
312 bind_key(pk, "h", 20);
313 bind_key(pk, "n", 21);
314 bind_key(pk, "j", 22);
315 bind_key(pk, "comma", 23);
317 /* Upper keyboard row, first octave - "azertyu". */
318 bind_key(pk, "a", 24);
319 bind_key(pk, "eacute", 25);
320 bind_key(pk, "z", 26);
321 bind_key(pk, "quotedbl", 27);
322 bind_key(pk, "e", 28);
323 bind_key(pk, "r", 29);
324 bind_key(pk, "parenleft", 30);
325 bind_key(pk, "t", 31);
326 bind_key(pk, "minus", 32);
327 bind_key(pk, "y", 33);
328 bind_key(pk, "egrave", 34);
329 bind_key(pk, "u", 35);
331 /* Upper keyboard row, the rest - "iop". */
332 bind_key(pk, "i", 36);
333 bind_key(pk, "ccedilla", 37);
334 bind_key(pk, "o", 38);
335 bind_key(pk, "agrave", 39);
336 bind_key(pk, "p", 40);
340 keyboard_event_handler(GtkWidget *mk, GdkEventKey *event, gpointer notused)
346 PianoKeyboard *pk = PIANO_KEYBOARD(mk);
348 /* We're not using event->keyval, because we need keyval with level set to 0.
349 E.g. if user holds Shift and presses '7', we want to get a '7', not '&'. */
350 kk.keycode = event->hardware_keycode;
354 keyval = gdk_keymap_lookup_key(NULL, &kk);
356 key = gdk_keyval_name(gdk_keyval_to_lower(keyval));
359 g_message("gtk_keyval_name() returned NULL; please report this.");
363 note = key_binding(pk, key);
366 /* Key was not bound. Maybe it's one of the keys handled in jack-keyboard.c. */
370 note += pk->octave * 12;
373 assert(note < NNOTES);
375 if (event->type == GDK_KEY_PRESS) {
378 } else if (event->type == GDK_KEY_RELEASE) {
379 release_key(pk, note);
386 get_note_for_xy(PianoKeyboard *pk, int x, int y)
388 int height = GTK_WIDGET(pk)->allocation.height;
391 if (y <= height / 2) {
392 for (note = 0; note < NNOTES - 1; note++) {
393 if (pk->notes[note].white)
396 if (x >= pk->notes[note].x && x <= pk->notes[note].x + pk->notes[note].w)
401 for (note = 0; note < NNOTES - 1; note++) {
402 if (!pk->notes[note].white)
405 if (x >= pk->notes[note].x && x <= pk->notes[note].x + pk->notes[note].w)
413 mouse_button_event_handler(PianoKeyboard *pk, GdkEventButton *event, gpointer notused)
418 int note = get_note_for_xy(pk, x, y);
420 if (event->button != 1)
423 if (event->type == GDK_BUTTON_PRESS) {
424 /* This is possible when you make the window a little wider and then click
430 if (pk->note_being_pressed_using_mouse >= 0)
431 release_key(pk, pk->note_being_pressed_using_mouse);
434 pk->note_being_pressed_using_mouse = note;
436 } else if (event->type == GDK_BUTTON_RELEASE) {
438 release_key(pk, note);
441 if (pk->note_being_pressed_using_mouse >= 0)
442 release_key(pk, pk->note_being_pressed_using_mouse);
445 pk->note_being_pressed_using_mouse = -1;
453 mouse_motion_event_handler(PianoKeyboard *pk, GdkEventMotion *event, gpointer notused)
457 if ((event->state & GDK_BUTTON1_MASK) == 0)
460 note = get_note_for_xy(pk, event->x, event->y);
462 if (note != pk->note_being_pressed_using_mouse && note >= 0) {
464 if (pk->note_being_pressed_using_mouse >= 0)
465 release_key(pk, pk->note_being_pressed_using_mouse);
467 pk->note_being_pressed_using_mouse = note;
474 piano_keyboard_expose(GtkWidget *widget, GdkEventExpose *event)
477 PianoKeyboard *pk = PIANO_KEYBOARD(widget);
478 cairo_t* cr = gdk_cairo_create (GDK_DRAWABLE (GTK_WIDGET(pk)->window));
480 gdk_cairo_region (cr, event->region);
483 for (i = 0; i < NNOTES; i++) {
486 r.x = pk->notes[i].x;
488 r.width = pk->notes[i].w;
489 r.height = pk->notes[i].h;
491 switch (gdk_region_rect_in (event->region, &r)) {
492 case GDK_OVERLAP_RECTANGLE_PART:
493 case GDK_OVERLAP_RECTANGLE_IN:
494 draw_note (pk, cr, i);
507 piano_keyboard_size_request(GtkWidget *widget, GtkRequisition *requisition)
509 requisition->width = PIANO_KEYBOARD_DEFAULT_WIDTH;
510 requisition->height = PIANO_KEYBOARD_DEFAULT_HEIGHT;
514 recompute_dimensions(PianoKeyboard *pk)
516 int number_of_white_keys = (NNOTES - 1) * (7.0 / 12.0);
526 int width = GTK_WIDGET(pk)->allocation.width;
527 int height = GTK_WIDGET(pk)->allocation.height;
529 key_width = width / number_of_white_keys;
530 black_key_width = key_width * 0.8;
531 useful_width = number_of_white_keys * key_width;
532 pk->widget_margin = (width - useful_width) / 2;
534 for (note = 0, white_key = 0; note < NNOTES - 2; note++) {
535 note_in_octave = note % 12;
537 if (note_in_octave == 1 || note_in_octave == 3 || note_in_octave == 6 ||
538 note_in_octave == 8 || note_in_octave == 10) {
540 /* This note is black key. */
541 pk->notes[note].x = pk->widget_margin + white_key * key_width - black_key_width / 2;
542 pk->notes[note].w = black_key_width;
543 pk->notes[note].h = height / 2;
544 pk->notes[note].white = 0;
549 /* This note is white key. */
550 pk->notes[note].x = pk->widget_margin + white_key * key_width;
551 pk->notes[note].w = key_width;
552 pk->notes[note].h = height;
553 pk->notes[note].white = 1;
560 piano_keyboard_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
562 /* XXX: Are these two needed? */
563 g_return_if_fail(widget != NULL);
564 g_return_if_fail(allocation != NULL);
566 widget->allocation = *allocation;
568 recompute_dimensions(PIANO_KEYBOARD(widget));
570 if (GTK_WIDGET_REALIZED(widget)) {
571 gdk_window_move_resize (widget->window, allocation->x, allocation->y, allocation->width, allocation->height);
576 piano_keyboard_class_init(PianoKeyboardClass *klass)
578 GtkWidgetClass *widget_klass;
580 /* Set up signals. */
581 piano_keyboard_signals[NOTE_ON_SIGNAL] = g_signal_new ("note-on",
582 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
583 0, NULL, NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
585 piano_keyboard_signals[NOTE_OFF_SIGNAL] = g_signal_new ("note-off",
586 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
587 0, NULL, NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
589 widget_klass = (GtkWidgetClass*) klass;
591 widget_klass->expose_event = piano_keyboard_expose;
592 widget_klass->size_request = piano_keyboard_size_request;
593 widget_klass->size_allocate = piano_keyboard_size_allocate;
597 piano_keyboard_init(GtkWidget *mk)
599 gtk_widget_add_events(mk, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK);
601 g_signal_connect(G_OBJECT(mk), "button-press-event", G_CALLBACK(mouse_button_event_handler), NULL);
602 g_signal_connect(G_OBJECT(mk), "button-release-event", G_CALLBACK(mouse_button_event_handler), NULL);
603 g_signal_connect(G_OBJECT(mk), "motion-notify-event", G_CALLBACK(mouse_motion_event_handler), NULL);
604 g_signal_connect(G_OBJECT(mk), "key-press-event", G_CALLBACK(keyboard_event_handler), NULL);
605 g_signal_connect(G_OBJECT(mk), "key-release-event", G_CALLBACK(keyboard_event_handler), NULL);
609 piano_keyboard_get_type(void)
611 static GType mk_type = 0;
614 static const GTypeInfo mk_info = {
615 sizeof(PianoKeyboardClass),
616 NULL, /* base_init */
617 NULL, /* base_finalize */
618 (GClassInitFunc) piano_keyboard_class_init,
619 NULL, /* class_finalize */
620 NULL, /* class_data */
621 sizeof (PianoKeyboard),
623 (GInstanceInitFunc) piano_keyboard_init,
626 mk_type = g_type_register_static(GTK_TYPE_DRAWING_AREA, "PianoKeyboard", &mk_info, 0);
633 piano_keyboard_new(void)
635 GtkWidget *widget = gtk_type_new(piano_keyboard_get_type());
637 PianoKeyboard *pk = PIANO_KEYBOARD(widget);
639 pk->maybe_stop_sustained_notes = 0;
640 pk->sustain_new_notes = 0;
641 pk->enable_keyboard_cue = 0;
643 pk->note_being_pressed_using_mouse = -1;
644 memset((void *)pk->notes, 0, sizeof(struct Note) * NNOTES);
645 pk->key_bindings = g_hash_table_new(g_str_hash, g_str_equal);
646 bind_keys_qwerty(pk);
652 piano_keyboard_set_keyboard_cue(PianoKeyboard *pk, int enabled)
654 pk->enable_keyboard_cue = enabled;
658 piano_keyboard_sustain_press(PianoKeyboard *pk)
660 if (!pk->sustain_new_notes) {
661 pk->sustain_new_notes = 1;
662 pk->maybe_stop_sustained_notes = 1;
667 piano_keyboard_sustain_release(PianoKeyboard *pk)
669 if (pk->maybe_stop_sustained_notes)
670 stop_sustained_notes(pk);
672 pk->sustain_new_notes = 0;
676 piano_keyboard_set_note_on(PianoKeyboard *pk, int note)
678 if (pk->notes[note].pressed == 0) {
679 pk->notes[note].pressed = 1;
680 queue_note_draw (pk, note);
685 piano_keyboard_set_note_off(PianoKeyboard *pk, int note)
687 if (pk->notes[note].pressed || pk->notes[note].sustained) {
688 pk->notes[note].pressed = 0;
689 pk->notes[note].sustained = 0;
690 queue_note_draw (pk, note);
695 piano_keyboard_set_octave(PianoKeyboard *pk, int octave)
697 stop_unsustained_notes(pk);
699 gtk_widget_queue_draw(GTK_WIDGET(pk));
703 piano_keyboard_set_keyboard_layout(PianoKeyboard *pk, const char *layout)
707 if (!strcasecmp(layout, "QWERTY")) {
708 bind_keys_qwerty(pk);
710 } else if (!strcasecmp(layout, "QWERTZ")) {
711 bind_keys_qwertz(pk);
713 } else if (!strcasecmp(layout, "AZERTY")) {
714 bind_keys_azerty(pk);
717 /* Unknown layout name. */