Compare commits

...

10 Commits
master ... gtk3

Author SHA1 Message Date
Johannes Maibaum
2f5b69c021 [WIP] Allow building with GTK+ 3.16+.
Set CMake option `ENABLE_GTK3` to `ON` to build with GTK+ 3.0.

In GTK3 mode:

- Use `GtkGrid` instead of `GtkTable`
- Don't use `GtkMisc` to set `GtkLabel`'s alignments
- `PianoKeyboard`'s draw() code still needs to be refactored (doesn't
  show key presses).
- Grab keyboard functionality still needs to be ported (doesn't work
  yet).
2016-09-18 16:33:36 +02:00
Johannes Maibaum
44980fc2f5 Replace deprecated gtk_type_new with g_object_new. 2016-09-17 12:24:48 +02:00
Johannes Maibaum
c1fe0e7c15 sustain_event_handler listens to toggled signal.
This removes the deprecated calls to gtk_button_{pressed,released}.
2016-09-17 12:01:47 +02:00
Johannes Maibaum
f57e31dba5 Fix GSEAL_ENABLE errors for src/pianokeyboard.c. 2016-09-11 21:15:13 +02:00
Johannes Maibaum
6d5bbaca0a Use cairo for drawing. 2016-09-11 20:03:56 +02:00
Johannes Maibaum
2bd9ca2b9b Fix GSEAL_ENABLE errors for src/jack-keyboard.c. 2016-09-11 20:03:48 +02:00
Johannes Maibaum
2f4bdf615b Let GDK and GTK+ report deprecated stuff. 2016-09-11 20:03:41 +02:00
Johannes Maibaum
884ded8b95 Replace GDK_<keyname> with GDK_KEY_<keyname>. 2016-09-11 20:03:05 +02:00
Johannes Maibaum
0941f4abaa Remove calls to deprecated GLib functions.
GLib 2.32 (released in March 2012) deprecated `g_atexit()` [1] and
`g_thread_init()` [2].

[1]: https://developer.gnome.org/glib/stable/glib-Miscellaneous-Utility-Functions.html#g-atexit
[2]: https://developer.gnome.org/glib/stable/glib-Threads.html
2016-09-11 20:03:00 +02:00
Johannes Maibaum
bb80fe24b2 Remove trailing whitespace. 2016-09-11 20:02:53 +02:00
3 changed files with 335 additions and 110 deletions

View File

@ -27,6 +27,9 @@
#
cmake_minimum_required(VERSION 2.8)
option(ENABLE_GTK2 "Enable GTK2 Version" ON)
option(ENABLE_GTK3 "Enable GTK3 Version" OFF)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/")
set(VERSION "2.7.2")
@ -38,9 +41,28 @@ project(jack-keyboard)
add_executable(jack-keyboard src/jack-keyboard src/pianokeyboard)
find_package(GTK2 2.2 REQUIRED gtk)
include_directories(${GTK2_INCLUDE_DIRS})
target_link_libraries(jack-keyboard ${GTK2_LIBRARIES})
find_package(PkgConfig REQUIRED)
if(ENABLE_GTK3)
set(ENABLE_GTK2 OFF)
PKG_CHECK_MODULES(GTK3 "gtk+-3.0 >= 3.16" REQUIRED)
include_directories(${GTK3_INCLUDE_DIRS})
target_link_libraries(jack-keyboard ${GTK3_LIBRARIES})
endif()
if(ENABLE_GTK2)
PKG_CHECK_MODULES(GTK2 "gtk+-2.0 >= 2.20" REQUIRED)
include_directories(${GTK2_INCLUDE_DIRS})
target_link_libraries(jack-keyboard ${GTK2_LIBRARIES})
endif()
# In preparation for GTK+3:
# Set GDK/GTK+ flags to report deprecated includes and symbols.
set(GTK_DEPRECATED_FLAGS "-DGTK_DISABLE_SINGLE_INCLUDES \
-DGDK_DISABLE_DEPRECATED \
-DGTK_DISABLE_DEPRECATED \
-DGSEAL_ENABLE")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GTK_DEPRECATED_FLAGS}")
if(JackEnable)
find_package(JACK)

View File

@ -152,7 +152,7 @@ int channel = 0;
void draw_note(int key);
void queue_message(struct MidiMessage *ev);
double
double
get_time(void)
{
double seconds;
@ -399,7 +399,7 @@ process_midi_output(jack_nframes_t nframes)
}
}
int
int
process_callback(jack_nframes_t nframes, void *notused)
{
#ifdef MEASURE_TIME
@ -440,7 +440,7 @@ queue_message(struct MidiMessage *ev)
g_warning("jack_ringbuffer_write failed, NOTE LOST.");
}
void
void
queue_new_message(int b0, int b1, int b2)
{
struct MidiMessage ev;
@ -524,7 +524,7 @@ update_connected_to_combo_async(gpointer notused)
return (FALSE);
}
void
void
draw_window_title(void)
{
int i, off = 0;
@ -577,7 +577,7 @@ update_window_title_async(gpointer notused)
return (FALSE);
}
int
int
graph_order_callback(void *notused)
{
g_idle_add(update_window_title_async, NULL);
@ -625,7 +625,7 @@ connect_to_input_port(const char *port)
g_warning("Cannot connect to %s.", port);
return (-4);
}
}
g_warning("Connected to %s.", port);
@ -725,19 +725,19 @@ connected:
free(connected_ports);
}
void
void
connect_to_next_input_port(void)
{
connect_to_another_input_port(1);
}
void
void
connect_to_prev_input_port(void)
{
connect_to_another_input_port(0);
}
void
void
init_jack(void)
{
int err;
@ -815,7 +815,7 @@ load_config_from_lash(void)
int value;
while ((config = lash_get_config(lash_client))) {
key = lash_config_get_key(config);
value = lash_config_get_value_int(config);
@ -960,15 +960,12 @@ init_lash(lash_args_t *args)
#endif /* HAVE_LASH */
gboolean
sustain_event_handler(GtkToggleButton *widget, gpointer pressed)
sustain_event_handler(GtkToggleButton *widget, gpointer not_used)
{
if (pressed) {
gtk_toggle_button_set_active(widget, TRUE);
if (gtk_toggle_button_get_active(widget))
piano_keyboard_sustain_press(keyboard);
} else {
gtk_toggle_button_set_active(widget, FALSE);
else
piano_keyboard_sustain_release(keyboard);
}
return (FALSE);
}
@ -1032,7 +1029,7 @@ velocity_event_handler(GtkRange *range, gpointer notused)
#ifdef HAVE_X11
int grab_x_error_handler(Display *dpy, XErrorEvent *notused)
int grab_x_error_handler(Display *dpy, XErrorEvent *notused)
{
keyboard_grabbed = 0;
@ -1042,6 +1039,7 @@ int grab_x_error_handler(Display *dpy, XErrorEvent *notused)
GdkFilterReturn
keyboard_grab_filter(GdkXEvent *xevent, GdkEvent *event, gpointer notused)
{
#if !GTK_CHECK_VERSION(3,0,0)
XEvent *xe;
XKeyEvent *xke;
@ -1054,14 +1052,16 @@ keyboard_grab_filter(GdkXEvent *xevent, GdkEvent *event, gpointer notused)
/* Lie to GDK, pretending we are the proper recipient of this XEvent. Without it,
GDK would discard it. */
xke->window = GDK_WINDOW_XWINDOW(window->window);
xke->window = GDK_WINDOW_XID(gtk_widget_get_window(window));
#endif /* GTK_CHECK_VERSION */
return (GDK_FILTER_CONTINUE);
}
void
ungrab_keyboard(void)
{
#if !GTK_CHECK_VERSION(3,0,0)
static int (*standard_x_error_handler)(Display *dpy, XErrorEvent *notused);
standard_x_error_handler = XSetErrorHandler(grab_x_error_handler);
@ -1072,11 +1072,13 @@ ungrab_keyboard(void)
XSetErrorHandler(standard_x_error_handler);
keyboard_grabbed = 0;
#endif /* GTK_CHECK_VERSION */
}
void
grab_keyboard(void)
{
#if !GTK_CHECK_VERSION(3,0,0)
int i;
static int (*standard_x_error_handler)(Display *dpy, XErrorEvent *notused);
@ -1085,7 +1087,7 @@ grab_keyboard(void)
XK_q, XK_w, XK_e, XK_r, XK_t, XK_y, XK_u, XK_i, XK_o, XK_p,
XK_a, XK_s, XK_d, XK_f, XK_g, XK_h, XK_j, XK_k, XK_l,
XK_z, XK_x, XK_c, XK_v, XK_b, XK_n, XK_m,
XK_Shift_L, XK_Shift_R, XK_Control_L, XK_Control_R, XK_space,
XK_Shift_L, XK_Shift_R, XK_Control_L, XK_Control_R, XK_space,
XK_Insert, XK_Delete, XK_Home, XK_End, XK_Page_Up, XK_Page_Down,
XK_KP_1, XK_KP_2, XK_KP_3, XK_KP_4, XK_KP_5, XK_KP_6, XK_KP_7, XK_KP_8, XK_KP_9, XK_KP_0,
XK_Num_Lock, XK_KP_Multiply, XK_KP_Divide, XK_KP_Subtract, XK_KP_Add, XK_KP_Enter,
@ -1097,7 +1099,7 @@ grab_keyboard(void)
if (keyboard_grabbed)
return;
dpy = GDK_WINDOW_XDISPLAY(window->window);
dpy = GDK_WINDOW_XDISPLAY(gtk_widget_get_window(window));
keyboard_grabbed = 1;
@ -1131,9 +1133,8 @@ grab_keyboard(void)
return;
}
g_atexit(ungrab_keyboard);
gdk_window_add_filter(NULL, keyboard_grab_filter, NULL);
#endif /* GTK_CHECK_VERSION */
}
#else /* ! HAVE_X11 */
@ -1169,7 +1170,7 @@ octave_event_handler(GtkSpinButton *spinbutton, gpointer notused)
piano_keyboard_set_octave(keyboard, octave);
}
void
void
panic(void)
{
int i;
@ -1217,70 +1218,70 @@ maybe_add_digit(GdkEventKey *event)
* XXX: This is silly. Find a way to enter the number without
* all these contitional instructions.
*/
if (event->keyval == GDK_KP_0 || event->keyval == GDK_KP_Insert) {
if (event->keyval == GDK_KEY_KP_0 || event->keyval == GDK_KEY_KP_Insert) {
if (event->type == GDK_KEY_PRESS)
add_digit(0);
return (TRUE);
}
if (event->keyval == GDK_KP_1 || event->keyval == GDK_KP_End) {
if (event->keyval == GDK_KEY_KP_1 || event->keyval == GDK_KEY_KP_End) {
if (event->type == GDK_KEY_PRESS)
add_digit(1);
return (TRUE);
}
if (event->keyval == GDK_KP_2 || event->keyval == GDK_KP_Down) {
if (event->keyval == GDK_KEY_KP_2 || event->keyval == GDK_KEY_KP_Down) {
if (event->type == GDK_KEY_PRESS)
add_digit(2);
return (TRUE);
}
if (event->keyval == GDK_KP_3 || event->keyval == GDK_KP_Page_Down) {
if (event->keyval == GDK_KEY_KP_3 || event->keyval == GDK_KEY_KP_Page_Down) {
if (event->type == GDK_KEY_PRESS)
add_digit(3);
return (TRUE);
}
if (event->keyval == GDK_KP_4 || event->keyval == GDK_KP_Left) {
if (event->keyval == GDK_KEY_KP_4 || event->keyval == GDK_KEY_KP_Left) {
if (event->type == GDK_KEY_PRESS)
add_digit(4);
return (TRUE);
}
if (event->keyval == GDK_KP_5 || event->keyval == GDK_KP_Begin) {
if (event->keyval == GDK_KEY_KP_5 || event->keyval == GDK_KEY_KP_Begin) {
if (event->type == GDK_KEY_PRESS)
add_digit(5);
return (TRUE);
}
if (event->keyval == GDK_KP_6 || event->keyval == GDK_KP_Right) {
if (event->keyval == GDK_KEY_KP_6 || event->keyval == GDK_KEY_KP_Right) {
if (event->type == GDK_KEY_PRESS)
add_digit(6);
return (TRUE);
}
if (event->keyval == GDK_KP_7 || event->keyval == GDK_KP_Home) {
if (event->keyval == GDK_KEY_KP_7 || event->keyval == GDK_KEY_KP_Home) {
if (event->type == GDK_KEY_PRESS)
add_digit(7);
return (TRUE);
}
if (event->keyval == GDK_KP_8 || event->keyval == GDK_KP_Up) {
if (event->keyval == GDK_KEY_KP_8 || event->keyval == GDK_KEY_KP_Up) {
if (event->type == GDK_KEY_PRESS)
add_digit(8);
return (TRUE);
}
if (event->keyval == GDK_KP_9 || event->keyval == GDK_KP_Page_Up) {
if (event->keyval == GDK_KEY_KP_9 || event->keyval == GDK_KEY_KP_Page_Up) {
if (event->type == GDK_KEY_PRESS)
add_digit(9);
@ -1314,7 +1315,7 @@ clip(int val, int lo, int hi)
return (val);
}
gint
gint
keyboard_event_handler(GtkWidget *widget, GdkEventKey *event, gpointer notused)
{
int tmp;
@ -1336,14 +1337,14 @@ keyboard_event_handler(GtkWidget *widget, GdkEventKey *event, gpointer notused)
/*
* '+' key shifts octave up. '-' key shifts octave down.
*/
if (event->keyval == GDK_KP_Add || event->keyval == GDK_equal) {
if (event->keyval == GDK_KEY_KP_Add || event->keyval == GDK_KEY_equal) {
if (event->type == GDK_KEY_PRESS && octave < OCTAVE_MAX)
gtk_spin_button_set_value(GTK_SPIN_BUTTON(octave_spin), octave + 1);
return (TRUE);
}
if (event->keyval == GDK_KP_Subtract || event->keyval == GDK_minus) {
if (event->keyval == GDK_KEY_KP_Subtract || event->keyval == GDK_KEY_minus) {
if (event->type == GDK_KEY_PRESS && octave > OCTAVE_MIN)
gtk_spin_button_set_value(GTK_SPIN_BUTTON(octave_spin), octave - 1);
@ -1353,7 +1354,7 @@ keyboard_event_handler(GtkWidget *widget, GdkEventKey *event, gpointer notused)
/*
* '*' character increases program number. '/' character decreases it.
*/
if (event->keyval == GDK_KP_Multiply) {
if (event->keyval == GDK_KEY_KP_Multiply) {
if (event->type == GDK_KEY_PRESS) {
tmp = get_entered_number();
@ -1367,7 +1368,7 @@ keyboard_event_handler(GtkWidget *widget, GdkEventKey *event, gpointer notused)
return (TRUE);
}
if (event->keyval == GDK_KP_Divide) {
if (event->keyval == GDK_KEY_KP_Divide) {
if (event->type == GDK_KEY_PRESS) {
tmp = get_entered_number();
@ -1384,7 +1385,7 @@ keyboard_event_handler(GtkWidget *widget, GdkEventKey *event, gpointer notused)
/*
* PgUp key increases bank number, PgDown decreases it.
*/
if (event->keyval == GDK_Page_Up) {
if (event->keyval == GDK_KEY_Page_Up) {
if (event->type == GDK_KEY_PRESS) {
tmp = get_entered_number();
@ -1398,7 +1399,7 @@ keyboard_event_handler(GtkWidget *widget, GdkEventKey *event, gpointer notused)
return (TRUE);
}
if (event->keyval == GDK_Page_Down) {
if (event->keyval == GDK_KEY_Page_Down) {
if (event->type == GDK_KEY_PRESS) {
tmp = get_entered_number();
@ -1415,7 +1416,7 @@ keyboard_event_handler(GtkWidget *widget, GdkEventKey *event, gpointer notused)
/*
* Home key increases channel number, End decreases it.
*/
if (event->keyval == GDK_Home) {
if (event->keyval == GDK_KEY_Home) {
if (event->type == GDK_KEY_PRESS) {
tmp = get_entered_number();
@ -1429,7 +1430,7 @@ keyboard_event_handler(GtkWidget *widget, GdkEventKey *event, gpointer notused)
return (TRUE);
}
if (event->keyval == GDK_End) {
if (event->keyval == GDK_KEY_End) {
if (event->type == GDK_KEY_PRESS) {
tmp = get_entered_number();
@ -1446,21 +1447,21 @@ keyboard_event_handler(GtkWidget *widget, GdkEventKey *event, gpointer notused)
/*
* Insert key connects to the next input port. Delete connects to the previous one.
*/
if (event->keyval == GDK_Insert) {
if (event->keyval == GDK_KEY_Insert) {
if (event->type == GDK_KEY_PRESS)
connect_to_next_input_port();
return (TRUE);
}
if (event->keyval == GDK_Delete) {
if (event->keyval == GDK_KEY_Delete) {
if (event->type == GDK_KEY_PRESS)
connect_to_prev_input_port();
return (TRUE);
}
if (event->keyval == GDK_Escape) {
if (event->keyval == GDK_KEY_Escape) {
if (event->type == GDK_KEY_PRESS)
panic();
@ -1474,11 +1475,11 @@ keyboard_event_handler(GtkWidget *widget, GdkEventKey *event, gpointer notused)
* the sustained notes end (it will send 'note off' midi messages for
* all the sustained notes).
*/
if (event->keyval == GDK_space) {
if (event->keyval == GDK_KEY_space) {
if (event->type == GDK_KEY_PRESS)
gtk_button_pressed(GTK_BUTTON(sustain_button));
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(sustain_button), TRUE);
else
gtk_button_released(GTK_BUTTON(sustain_button));
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(sustain_button), FALSE);
return (TRUE);
}
@ -1487,7 +1488,7 @@ keyboard_event_handler(GtkWidget *widget, GdkEventKey *event, gpointer notused)
* Shift increases velocity, i.e. holding it while pressing note key
* will make the sound louder. Ctrl decreases velocity.
*/
if (event->keyval == GDK_Shift_L || event->keyval == GDK_Shift_R) {
if (event->keyval == GDK_KEY_Shift_L || event->keyval == GDK_KEY_Shift_R) {
if (event->type == GDK_KEY_PRESS)
current_velocity = &velocity_high;
else
@ -1499,7 +1500,7 @@ keyboard_event_handler(GtkWidget *widget, GdkEventKey *event, gpointer notused)
}
if (event->keyval == GDK_Control_L || event->keyval == GDK_Control_R) {
if (event->keyval == GDK_KEY_Control_L || event->keyval == GDK_KEY_Control_R) {
if (event->type == GDK_KEY_PRESS)
current_velocity = &velocity_low;
else
@ -1555,49 +1556,102 @@ init_gtk_1(int *argc, char ***argv)
void
init_gtk_2(void)
{
#if GTK_CHECK_VERSION(3,0,0)
GtkWidget *grid;
#else
GtkTable *table;
#endif
GtkWidget *label;
GtkCellRenderer *renderer;
#if GTK_CHECK_VERSION(3,0,0)
grid = gtk_grid_new();
gtk_grid_set_row_spacing(GTK_GRID(grid), 5);
gtk_grid_set_column_spacing(GTK_GRID(grid), 5);
#else
/* Table. */
table = GTK_TABLE(gtk_table_new(4, 8, FALSE));
gtk_table_set_row_spacings(table, 5);
gtk_table_set_col_spacings(table, 5);
#endif
if (enable_gui)
#if GTK_CHECK_VERSION(3,0,0)
gtk_container_add(GTK_CONTAINER(window), grid);
#else
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(table));
#endif
/* Channel label and spin. */
label = gtk_label_new("Channel:");
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_set_hexpand(label, TRUE);
gtk_label_set_xalign(GTK_LABEL(label), 1);
gtk_container_add(GTK_CONTAINER(grid), label);
#else
gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
gtk_table_attach(table, label, 0, 1, 0, 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
#endif
channel_spin = gtk_spin_button_new_with_range(1, CHANNEL_MAX, 1);
GTK_WIDGET_UNSET_FLAGS(channel_spin, GTK_CAN_FOCUS);
gtk_widget_set_can_focus(channel_spin, FALSE);
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_set_hexpand(channel_spin, TRUE);
gtk_grid_attach_next_to(GTK_GRID(grid), channel_spin, label, GTK_POS_RIGHT, 1, 1);
#else
gtk_table_attach(table, channel_spin, 1, 2, 0, 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
#endif
g_signal_connect(G_OBJECT(channel_spin), "value-changed", G_CALLBACK(channel_event_handler), NULL);
/* Bank label and spin. */
label = gtk_label_new("Bank:");
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_set_hexpand(label, TRUE);
gtk_label_set_xalign(GTK_LABEL(label), 1);
gtk_grid_attach_next_to(GTK_GRID(grid), label, channel_spin, GTK_POS_RIGHT, 1, 1);
#else
gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
gtk_table_attach(table, label, 2, 3, 0, 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
#endif
bank_spin = gtk_spin_button_new_with_range(0, BANK_MAX, 1);
GTK_WIDGET_UNSET_FLAGS(bank_spin, GTK_CAN_FOCUS);
gtk_widget_set_can_focus(bank_spin, FALSE);
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_set_hexpand(bank_spin, TRUE);
gtk_grid_attach_next_to(GTK_GRID(grid), bank_spin, label, GTK_POS_RIGHT, 1, 1);
#else
gtk_table_attach(table, bank_spin, 3, 4, 0, 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
#endif
g_signal_connect(G_OBJECT(bank_spin), "value-changed", G_CALLBACK(bank_event_handler), NULL);
/* Program label and spin. */
label = gtk_label_new("Program:");
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_set_hexpand(label, TRUE);
gtk_label_set_xalign(GTK_LABEL(label), 1);
gtk_grid_attach_next_to(GTK_GRID(grid), label, bank_spin, GTK_POS_RIGHT, 1, 1);
#else
gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
gtk_table_attach(table, label, 4, 5, 0, 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
#endif
program_spin = gtk_spin_button_new_with_range(0, PROGRAM_MAX, 1);
GTK_WIDGET_UNSET_FLAGS(program_spin, GTK_CAN_FOCUS);
gtk_widget_set_can_focus(program_spin, FALSE);
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_set_hexpand(program_spin, TRUE);
gtk_grid_attach_next_to(GTK_GRID(grid), program_spin, label, GTK_POS_RIGHT, 1, 1);
#else
gtk_table_attach(table, program_spin, 5, 6, 0, 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
#endif
g_signal_connect(G_OBJECT(program_spin), "value-changed", G_CALLBACK(program_event_handler), NULL);
/* "Connected to" label and combo box. */
label = gtk_label_new("Connected to:");
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_set_hexpand(label, TRUE);
gtk_label_set_xalign(GTK_LABEL(label), 1);
gtk_grid_attach_next_to(GTK_GRID(grid), label, program_spin, GTK_POS_RIGHT, 1, 1);
#else
gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
gtk_table_attach(table, label, 6, 7, 0, 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
#endif
connected_to_store = gtk_list_store_new(1, G_TYPE_STRING);
connected_to_combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(connected_to_store));
@ -1606,56 +1660,110 @@ init_gtk_2(void)
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(connected_to_combo), renderer, FALSE);
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT(connected_to_combo), renderer, "text", 0, NULL);
GTK_WIDGET_UNSET_FLAGS(connected_to_combo, GTK_CAN_FOCUS);
gtk_widget_set_can_focus(connected_to_combo, FALSE);
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_set_focus_on_click(connected_to_combo, FALSE);
gtk_widget_set_hexpand(connected_to_combo, TRUE);
gtk_grid_attach_next_to(GTK_GRID(grid), connected_to_combo, label, GTK_POS_RIGHT, 1, 1);
#else
gtk_combo_box_set_focus_on_click(GTK_COMBO_BOX(connected_to_combo), FALSE);
gtk_table_attach(table, connected_to_combo, 7, 8, 0, 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
#endif
gtk_widget_set_size_request(GTK_WIDGET(connected_to_combo), 200, -1);
g_signal_connect(G_OBJECT(connected_to_combo), "changed", G_CALLBACK(connected_to_event_handler), NULL);
/* Octave label and spin. */
label = gtk_label_new("Octave:");
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_set_hexpand(label, TRUE);
gtk_label_set_xalign(GTK_LABEL(label), 1);
gtk_grid_attach(GTK_GRID(grid), label, 0, 1, 1, 1);
#else
gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
gtk_table_attach(table, label, 0, 1, 1, 2, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
#endif
octave_spin = gtk_spin_button_new_with_range(OCTAVE_MIN, OCTAVE_MAX, 1);
GTK_WIDGET_UNSET_FLAGS(octave_spin, GTK_CAN_FOCUS);
gtk_widget_set_can_focus(octave_spin, FALSE);
gtk_spin_button_set_value(GTK_SPIN_BUTTON(octave_spin), octave);
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_set_hexpand(octave_spin, TRUE);
gtk_grid_attach_next_to(GTK_GRID(grid), octave_spin, label, GTK_POS_RIGHT, 1, 1);
#else
gtk_table_attach(table, octave_spin, 1, 2, 1, 2, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
#endif
g_signal_connect(G_OBJECT(octave_spin), "value-changed", G_CALLBACK(octave_event_handler), NULL);
/* "Grab keyboard" label and checkbutton. */
label = gtk_label_new("Grab keyboard:");
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_set_hexpand(label, TRUE);
gtk_label_set_xalign(GTK_LABEL(label), 1);
gtk_grid_attach(GTK_GRID(grid), label, 4, 1, 1, 1);
#else
gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
gtk_table_attach(table, label, 4, 5, 1, 2, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
#endif
grab_keyboard_checkbutton = gtk_check_button_new();
GTK_WIDGET_UNSET_FLAGS(grab_keyboard_checkbutton, GTK_CAN_FOCUS);
gtk_widget_set_can_focus(grab_keyboard_checkbutton, FALSE);
g_signal_connect(G_OBJECT(grab_keyboard_checkbutton), "toggled", G_CALLBACK(grab_keyboard_handler), NULL);
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_set_hexpand(grab_keyboard_checkbutton, TRUE);
gtk_grid_attach_next_to(GTK_GRID(grid), grab_keyboard_checkbutton, label, GTK_POS_RIGHT, 1, 1);
#else
gtk_table_attach(table, grab_keyboard_checkbutton, 5, 6, 1, 2, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
#endif
/* Velocity label and hscale */
label = gtk_label_new("Velocity:");
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_set_hexpand(label, TRUE);
gtk_label_set_xalign(GTK_LABEL(label), 1);
gtk_grid_attach_next_to(GTK_GRID(grid), label, grab_keyboard_checkbutton, GTK_POS_RIGHT, 1, 1);
velocity_hscale = gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL, VELOCITY_MIN, VELOCITY_MAX, 1);
#else
gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
gtk_table_attach(table, label, 6, 7, 1, 2, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
velocity_hscale = gtk_hscale_new_with_range(VELOCITY_MIN, VELOCITY_MAX, 1);
#endif
gtk_scale_set_draw_value(GTK_SCALE(velocity_hscale), FALSE);
GTK_WIDGET_UNSET_FLAGS(velocity_hscale, GTK_CAN_FOCUS);
gtk_widget_set_can_focus(velocity_hscale, FALSE);
g_signal_connect(G_OBJECT(velocity_hscale), "value-changed", G_CALLBACK(velocity_event_handler), NULL);
gtk_range_set_value(GTK_RANGE(velocity_hscale), VELOCITY_NORMAL);
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_set_hexpand(velocity_hscale, TRUE);
gtk_grid_attach_next_to(GTK_GRID(grid), velocity_hscale, label, GTK_POS_RIGHT, 1, 1);
#else
gtk_table_attach(table, velocity_hscale, 7, 8, 1, 2, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
#endif
gtk_widget_set_size_request(GTK_WIDGET(velocity_hscale), 200, -1);
/* Sustain. It's a toggle button, not an ordinary one, because we want gtk_whatever_set_active() to work.*/
sustain_button = gtk_toggle_button_new_with_label("Sustain");
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_set_focus_on_click(sustain_button, FALSE);
gtk_widget_set_hexpand(sustain_button, TRUE);
gtk_grid_attach(GTK_GRID(grid), sustain_button, 0, 2, 8, 1);
#else
gtk_button_set_focus_on_click(GTK_BUTTON(sustain_button), FALSE);
gtk_table_attach(table, sustain_button, 0, 8, 2, 3, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
g_signal_connect(G_OBJECT(sustain_button), "pressed", G_CALLBACK(sustain_event_handler), (void *)1);
g_signal_connect(G_OBJECT(sustain_button), "released", G_CALLBACK(sustain_event_handler), (void *)0);
#endif
g_signal_connect(G_OBJECT(sustain_button), "toggled", G_CALLBACK(sustain_event_handler), NULL);
/* PianoKeyboard widget. */
keyboard = PIANO_KEYBOARD(piano_keyboard_new());
if (enable_gui)
#if GTK_CHECK_VERSION(3,0,0)
{
gtk_widget_set_hexpand(GTK_WIDGET(keyboard), TRUE);
gtk_widget_set_vexpand(GTK_WIDGET(keyboard), TRUE);
gtk_grid_attach(GTK_GRID(grid), GTK_WIDGET(keyboard), 0, 3, 8, 1);
}
#else
gtk_table_attach_defaults(table, GTK_WIDGET(keyboard), 0, 8, 3, 4);
#endif
else
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(keyboard));
@ -1669,7 +1777,7 @@ init_gtk_2(void)
draw_window_title();
}
void
void
log_handler(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer notused)
{
GtkWidget *dialog;
@ -1707,7 +1815,7 @@ usage(void)
exit(EX_USAGE);
}
int
int
main(int argc, char *argv[])
{
int ch, enable_keyboard_cue = 0, initial_channel = 1, initial_bank = 0, initial_program = 0, full_midi_keyboard = 0;
@ -1715,11 +1823,6 @@ main(int argc, char *argv[])
#ifdef HAVE_LASH
lash_args_t *lash_args;
#endif
g_thread_init(NULL);
#ifdef HAVE_LASH
lash_args = lash_extract_args(&argc, &argv);
#endif
@ -1765,11 +1868,11 @@ main(int argc, char *argv[])
case 'l':
keyboard_layout = strdup(optarg);
break;
case 't':
time_offsets_are_zero = 1;
break;
case 'u':
send_program_change_at_reconnect = 1;
break;
@ -1872,6 +1975,9 @@ main(int argc, char *argv[])
gtk_main();
if (keyboard_grabbed)
ungrab_keyboard();
return (0);
}

View File

@ -55,27 +55,31 @@ draw_keyboard_cue(PianoKeyboard *pk)
int w, h, first_note_in_lower_row, last_note_in_lower_row,
first_note_in_higher_row, last_note_in_higher_row;
GdkGC *gc;
w = pk->notes[0].w;
h = pk->notes[0].h;
gc = GTK_WIDGET(pk)->style->fg_gc[0];
first_note_in_lower_row = (pk->octave + 5) * 12;
last_note_in_lower_row = (pk->octave + 6) * 12 - 1;
first_note_in_higher_row = (pk->octave + 6) * 12;
last_note_in_higher_row = (pk->octave + 7) * 12 + 4;
gdk_draw_line(GTK_WIDGET(pk)->window, gc, pk->notes[first_note_in_lower_row].x + 3,
h - 6, pk->notes[last_note_in_lower_row].x + w - 3, h - 6);
cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(GTK_WIDGET(pk)));
gdk_draw_line(GTK_WIDGET(pk)->window, gc, pk->notes[first_note_in_higher_row].x + 3,
h - 9, pk->notes[last_note_in_higher_row].x + w - 3, h - 9);
cairo_move_to(cr, pk->notes[first_note_in_lower_row].x + 3, h - 6);
cairo_line_to(cr, pk->notes[last_note_in_lower_row].x + w - 3, h - 6);
cairo_move_to(cr, pk->notes[first_note_in_higher_row].x + 3, h - 9);
cairo_line_to(cr, pk->notes[last_note_in_higher_row].x + w - 3, h - 9);
cairo_destroy(cr);
}
static void
static void
#if GTK_CHECK_VERSION(3,0,0)
draw_note(PianoKeyboard *pk, cairo_t *cr, int note)
#else
draw_note(PianoKeyboard *pk, int note)
#endif
{
if (note < pk->min_note)
return;
@ -83,11 +87,21 @@ draw_note(PianoKeyboard *pk, int note)
return;
int is_white, x, w, h;
#if GTK_CHECK_VERSION(3,0,0)
if (!cr)
cr = gdk_cairo_create(gtk_widget_get_window(GTK_WIDGET(pk)));
GdkRGBA black = { .red = 0.0, .green = 0.0, .blue = 0.0, .alpha = 1.0};
GdkRGBA white = { .red = 1.0, .green = 1.0, .blue = 1.0, .alpha = 1.0};
#else
cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(GTK_WIDGET(pk)));
GdkColor black = {0, 0, 0, 0};
GdkColor white = {0, 65535, 65535, 65535};
GdkGC *gc = GTK_WIDGET(pk)->style->fg_gc[0];
GtkWidget *widget;
GtkAllocation allocation;
#endif
is_white = pk->notes[note].white;
@ -99,22 +113,47 @@ draw_note(PianoKeyboard *pk, int note)
is_white = !is_white;
if (is_white)
gdk_gc_set_rgb_fg_color(gc, &white);
#if GTK_CHECK_VERSION(3,0,0)
gdk_cairo_set_source_rgba(cr, &white);
#else
gdk_cairo_set_source_color(cr, &white);
#endif
else
gdk_gc_set_rgb_fg_color(gc, &black);
#if GTK_CHECK_VERSION(3,0,0)
gdk_cairo_set_source_rgba(cr, &black);
#else
gdk_cairo_set_source_color(cr, &black);
#endif
gdk_draw_rectangle(GTK_WIDGET(pk)->window, gc, TRUE, x, 0, w, h);
gdk_gc_set_rgb_fg_color(gc, &black);
gdk_draw_rectangle(GTK_WIDGET(pk)->window, gc, FALSE, x, 0, w, h);
cairo_rectangle(cr, x, 0, w, h);
cairo_fill(cr);
#if GTK_CHECK_VERSION(3,0,0)
gdk_cairo_set_source_rgba(cr, &black);
#else
gdk_cairo_set_source_color(cr, &black);
#endif
cairo_rectangle(cr, x, 0, w, h);
cairo_stroke(cr);
#if !GTK_CHECK_VERSION(3,0,0)
cairo_destroy(cr);
#endif
if (pk->enable_keyboard_cue)
draw_keyboard_cue(pk);
/* We need to redraw black keys that partially obscure the white one. */
if (note < NNOTES - 2 && !pk->notes[note + 1].white)
#if GTK_CHECK_VERSION(3,0,0)
draw_note(pk, cr, note + 1);
#else
draw_note(pk, note + 1);
#endif
if (note > 0 && !pk->notes[note - 1].white)
#if GTK_CHECK_VERSION(3,0,0)
draw_note(pk, cr, note - 1);
#else
draw_note(pk, note - 1);
/*
@ -124,12 +163,14 @@ draw_note(PianoKeyboard *pk, int note)
* that didn't work.
*/
widget = GTK_WIDGET(pk);
gtk_paint_shadow(widget->style, widget->window, GTK_STATE_NORMAL, GTK_SHADOW_IN, NULL,
widget, NULL, pk->widget_margin, 0, widget->allocation.width - pk->widget_margin * 2 + 1,
widget->allocation.height);
gtk_widget_get_allocation(widget, &allocation);
gtk_paint_shadow(gtk_widget_get_style(widget), gtk_widget_get_window(widget), GTK_STATE_NORMAL, GTK_SHADOW_IN, NULL,
widget, NULL, pk->widget_margin, 0, allocation.width - pk->widget_margin * 2 + 1,
allocation.height);
#endif
}
static int
static int
press_key(PianoKeyboard *pk, int key)
{
assert(key >= 0);
@ -149,12 +190,16 @@ press_key(PianoKeyboard *pk, int key)
pk->notes[key].pressed = 1;
g_signal_emit_by_name(GTK_WIDGET(pk), "note-on", key);
#if GTK_CHECK_VERSION(3,0,0)
draw_note(pk, NULL, key);
#else
draw_note(pk, key);
#endif
return (1);
}
static int
static int
release_key(PianoKeyboard *pk, int key)
{
assert(key >= 0);
@ -174,12 +219,16 @@ release_key(PianoKeyboard *pk, int key)
return (0);
g_signal_emit_by_name(GTK_WIDGET(pk), "note-off", key);
#if GTK_CHECK_VERSION(3,0,0)
draw_note(pk, NULL, key);
#else
draw_note(pk, key);
#endif
return (1);
}
static void
static void
stop_unsustained_notes(PianoKeyboard *pk)
{
int i;
@ -188,12 +237,16 @@ stop_unsustained_notes(PianoKeyboard *pk)
if (pk->notes[i].pressed && !pk->notes[i].sustained) {
pk->notes[i].pressed = 0;
g_signal_emit_by_name(GTK_WIDGET(pk), "note-off", i);
#if GTK_CHECK_VERSION(3,0,0)
draw_note(pk, NULL, i);
#else
draw_note(pk, i);
#endif
}
}
}
static void
static void
stop_sustained_notes(PianoKeyboard *pk)
{
int i;
@ -203,7 +256,11 @@ stop_sustained_notes(PianoKeyboard *pk)
pk->notes[i].pressed = 0;
pk->notes[i].sustained = 0;
g_signal_emit_by_name(GTK_WIDGET(pk), "note-off", i);
#if GTK_CHECK_VERSION(3,0,0)
draw_note(pk, NULL, i);
#else
draw_note(pk, i);
#endif
}
}
}
@ -237,7 +294,7 @@ clear_notes(PianoKeyboard *pk)
g_array_set_size(pk->key_bindings, 0);
}
static void
static void
bind_keys_qwerty(PianoKeyboard *pk)
{
clear_notes(pk);
@ -291,7 +348,7 @@ bind_keys_qwerty(PianoKeyboard *pk)
bind_key(pk, 51, 45); /* yes, really, at least here... */
}
static gint
static gint
keyboard_event_handler(GtkWidget *mk, GdkEventKey *event, gpointer notused)
{
int note;
@ -319,12 +376,14 @@ keyboard_event_handler(GtkWidget *mk, GdkEventKey *event, gpointer notused)
return (TRUE);
}
static int
static int
get_note_for_xy(PianoKeyboard *pk, int x, int y)
{
int height, note;
GtkAllocation allocation;
height = GTK_WIDGET(pk)->allocation.height;
gtk_widget_get_allocation(GTK_WIDGET(pk), &allocation);
height = allocation.height;
if (y <= ((height * 2) / 3)) { /* might be a black key */
for (note = 0; note <= pk->max_note; ++note) {
@ -347,7 +406,7 @@ get_note_for_xy(PianoKeyboard *pk, int x, int y)
return (-1);
}
static gboolean
static gboolean
mouse_button_event_handler(PianoKeyboard *pk, GdkEventButton *event, gpointer notused)
{
int x, y, note;
@ -389,7 +448,7 @@ mouse_button_event_handler(PianoKeyboard *pk, GdkEventButton *event, gpointer no
return (TRUE);
}
static gboolean
static gboolean
mouse_motion_event_handler(PianoKeyboard *pk, GdkEventMotion *event, gpointer notused)
{
int note;
@ -400,7 +459,7 @@ mouse_motion_event_handler(PianoKeyboard *pk, GdkEventMotion *event, gpointer no
note = get_note_for_xy(pk, event->x, event->y);
if (note != pk->note_being_pressed_using_mouse && note >= 0) {
if (pk->note_being_pressed_using_mouse >= 0)
release_key(pk, pk->note_being_pressed_using_mouse);
press_key(pk, note);
@ -411,23 +470,45 @@ mouse_motion_event_handler(PianoKeyboard *pk, GdkEventMotion *event, gpointer no
}
static gboolean
#if GTK_CHECK_VERSION(3,0,0)
piano_keyboard_draw(GtkWidget *widget, cairo_t *cr)
#else
piano_keyboard_expose(GtkWidget *widget, GdkEventExpose *event)
#endif
{
int i;
PianoKeyboard *pk = PIANO_KEYBOARD(widget);
for (i = 0; i < NNOTES; i++)
#if GTK_CHECK_VERSION(3,0,0)
draw_note(pk, NULL, i);
#else
draw_note(pk, i);
#endif
return (TRUE);
}
static void
#if GTK_CHECK_VERSION(3,0,0)
static void
piano_keyboard_get_preferred_width(GtkWidget *widget, gint *minimal_width, gint *natural_width)
{
*minimal_width = *natural_width = PIANO_KEYBOARD_DEFAULT_WIDTH;
}
static void
piano_keyboard_get_preferred_height(GtkWidget *widget, gint *minimal_height, gint *natural_height)
{
*minimal_height = *natural_height = PIANO_KEYBOARD_DEFAULT_HEIGHT;
}
#else
static void
piano_keyboard_size_request(GtkWidget *widget, GtkRequisition *requisition)
{
requisition->width = PIANO_KEYBOARD_DEFAULT_WIDTH;
requisition->height = PIANO_KEYBOARD_DEFAULT_HEIGHT;
}
#endif
static int is_black(int key)
{
@ -464,6 +545,7 @@ recompute_dimensions(PianoKeyboard *pk)
{
int number_of_white_keys = 0, skipped_white_keys = 0, key_width, black_key_width, useful_width, note,
white_key, width, height;
GtkAllocation allocation;
for (note = pk->min_note; note <= pk->max_note; ++note)
if (!is_black(note))
@ -472,8 +554,9 @@ recompute_dimensions(PianoKeyboard *pk)
if (!is_black(note))
++skipped_white_keys;
width = GTK_WIDGET(pk)->allocation.width;
height = GTK_WIDGET(pk)->allocation.height;
gtk_widget_get_allocation(GTK_WIDGET(pk), &allocation);
width = allocation.width;
height = allocation.height;
key_width = width / number_of_white_keys;
black_key_width = key_width * 0.8;
@ -483,7 +566,7 @@ recompute_dimensions(PianoKeyboard *pk)
for (note = 0, white_key = -skipped_white_keys; note < NNOTES; note++) {
if (is_black(note)) {
/* This note is black key. */
pk->notes[note].x = pk->widget_margin +
pk->notes[note].x = pk->widget_margin +
(white_key * key_width) -
(black_key_width * black_key_left_shift(note));
pk->notes[note].w = black_key_width;
@ -509,12 +592,12 @@ piano_keyboard_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
g_return_if_fail(widget != NULL);
g_return_if_fail(allocation != NULL);
widget->allocation = *allocation;
gtk_widget_set_allocation(widget, allocation);
recompute_dimensions(PIANO_KEYBOARD(widget));
if (GTK_WIDGET_REALIZED(widget))
gdk_window_move_resize (widget->window, allocation->x, allocation->y, allocation->width, allocation->height);
if (gtk_widget_get_realized(widget))
gdk_window_move_resize (gtk_widget_get_window(widget), allocation->x, allocation->y, allocation->width, allocation->height);
}
static void
@ -531,10 +614,16 @@ piano_keyboard_class_init(PianoKeyboardClass *klass)
G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
0, NULL, NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
widget_klass = (GtkWidgetClass*)klass;
widget_klass = (GtkWidgetClass*)klass;
#if GTK_CHECK_VERSION(3,0,0)
widget_klass->draw = piano_keyboard_draw;
widget_klass->get_preferred_width = piano_keyboard_get_preferred_width;
widget_klass->get_preferred_height = piano_keyboard_get_preferred_height;
#else
widget_klass->expose_event = piano_keyboard_expose;
widget_klass->size_request = piano_keyboard_size_request;
#endif
widget_klass->size_allocate = piano_keyboard_size_allocate;
}
@ -580,7 +669,7 @@ piano_keyboard_new(void)
GtkWidget *widget;
PianoKeyboard *pk;
widget = gtk_type_new(piano_keyboard_get_type());
widget = g_object_new(TYPE_PIANO_KEYBOARD, NULL);
pk = PIANO_KEYBOARD(widget);
pk->maybe_stop_sustained_notes = 0;
@ -613,7 +702,7 @@ piano_keyboard_sustain_press(PianoKeyboard *pk)
}
}
void
void
piano_keyboard_sustain_release(PianoKeyboard *pk)
{
if (pk->maybe_stop_sustained_notes)
@ -627,7 +716,11 @@ piano_keyboard_set_note_on(PianoKeyboard *pk, int note)
{
if (pk->notes[note].pressed == 0) {
pk->notes[note].pressed = 1;
#if GTK_CHECK_VERSION(3,0,0)
draw_note(pk, NULL, note);
#else
draw_note(pk, note);
#endif
}
}
@ -637,7 +730,11 @@ piano_keyboard_set_note_off(PianoKeyboard *pk, int note)
if (pk->notes[note].pressed || pk->notes[note].sustained) {
pk->notes[note].pressed = 0;
pk->notes[note].sustained = 0;
#if GTK_CHECK_VERSION(3,0,0)
draw_note(pk, NULL, note);
#else
draw_note(pk, note);
#endif
}
}