modules/controller_linux_input.c remember the source ID returned by

2004-06-25  Michael Natterer  <mitch@gimp.org>

	* modules/controller_linux_input.c
	* modules/controller_midi.c: remember the source ID returned by
	g_io_add_watch() and remove it when changing the device, so the
	file descritor gets actually closed. Minor cleanups.
This commit is contained in:
Michael Natterer
2004-06-24 22:19:25 +00:00
committed by Michael Natterer
parent 11dfbae2f6
commit 6a42a7194b
3 changed files with 61 additions and 41 deletions

View File

@ -1,3 +1,10 @@
2004-06-25 Michael Natterer <mitch@gimp.org>
* modules/controller_linux_input.c
* modules/controller_midi.c: remember the source ID returned by
g_io_add_watch() and remove it when changing the device, so the
file descritor gets actually closed. Minor cleanups.
2004-06-24 Michael Natterer <mitch@gimp.org> 2004-06-24 Michael Natterer <mitch@gimp.org>
* app/widgets/gimpcontrollerwheel.[ch]: renamed function * app/widgets/gimpcontrollerwheel.[ch]: renamed function

View File

@ -106,10 +106,11 @@ typedef struct _ControllerLinuxInputClass ControllerLinuxInputClass;
struct _ControllerLinuxInput struct _ControllerLinuxInput
{ {
GimpController parent_instance; GimpController parent_instance;
gchar *device; gchar *device;
GIOChannel *io; GIOChannel *io;
guint io_id;
}; };
struct _ControllerLinuxInputClass struct _ControllerLinuxInputClass
@ -213,7 +214,8 @@ linux_input_class_init (ControllerLinuxInputClass *klass)
object_class->set_property = linux_input_set_property; object_class->set_property = linux_input_set_property;
g_object_class_install_property (object_class, PROP_DEVICE, g_object_class_install_property (object_class, PROP_DEVICE,
g_param_spec_string ("device", NULL, NULL, g_param_spec_string ("device",
_("Device:"), NULL,
NULL, NULL,
G_PARAM_READWRITE | G_PARAM_READWRITE |
G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT |
@ -330,6 +332,9 @@ linux_input_set_device (ControllerLinuxInput *controller,
{ {
if (controller->io) if (controller->io)
{ {
g_source_remove (controller->io_id);
controller->io_id = 0;
g_io_channel_unref (controller->io); g_io_channel_unref (controller->io);
controller->io = NULL; controller->io = NULL;
} }
@ -339,56 +344,52 @@ linux_input_set_device (ControllerLinuxInput *controller,
controller->device = g_strdup (device); controller->device = g_strdup (device);
if (device) if (controller->device && strlen (controller->device))
{ {
gint fd; gint fd;
fd = open (controller->device, O_RDONLY); fd = open (controller->device, O_RDONLY);
if (fd >= 0) if (fd >= 0)
{ {
gchar name[256]; gchar name[256];
name[0] = '\0'; name[0] = '\0';
if (ioctl (fd, EVIOCGNAME (sizeof (name)), name) == 0 && if (ioctl (fd, EVIOCGNAME (sizeof (name)), name) == 0 &&
strlen (name) > 0 && strlen (name) > 0 &&
g_utf8_validate (name, -1, NULL)) g_utf8_validate (name, -1, NULL))
{ {
g_object_set (controller, g_object_set (controller, "name", name, NULL);
"name", name,
NULL);
} }
else else
{ {
g_object_set (controller, gchar *name = g_strdup_printf (_("Reading from %s"),
"name", _("Unknown device"), controller->device);
NULL); g_object_set (controller, "name", name, NULL);
g_free (name);
} }
controller->io = g_io_channel_unix_new (fd); controller->io = g_io_channel_unix_new (fd);
g_io_channel_set_close_on_unref (controller->io, TRUE); g_io_channel_set_close_on_unref (controller->io, TRUE);
g_io_channel_set_encoding (controller->io, NULL, NULL); g_io_channel_set_encoding (controller->io, NULL, NULL);
g_io_add_watch (controller->io, controller->io_id = g_io_add_watch (controller->io,
G_IO_IN, G_IO_IN,
linux_input_read_event, linux_input_read_event,
controller); controller);
return TRUE; return TRUE;
} }
else else
{ {
gchar *name = g_strdup_printf (_("Device not available: %s"), gchar *name = g_strdup_printf (_("Device not available: %s"),
g_strerror (errno)); g_strerror (errno));
g_object_set (controller, g_object_set (controller, "name", name, NULL);
"name", name,
NULL);
g_free (name); g_free (name);
} }
} }
else else
{ {
g_object_set (controller, g_object_set (controller, "name", _("No device configured"), NULL);
"name", _("No device configured"),
NULL);
} }
return FALSE; return FALSE;

View File

@ -23,9 +23,10 @@
#include "config.h" #include "config.h"
#include <errno.h> #include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
@ -67,6 +68,7 @@ struct _ControllerMidi
gchar *device; gchar *device;
GIOChannel *io; GIOChannel *io;
guint io_id;
/* midi status */ /* midi status */
gboolean swallow; gboolean swallow;
@ -188,7 +190,8 @@ midi_class_init (ControllerMidiClass *klass)
object_class->set_property = midi_set_property; object_class->set_property = midi_set_property;
g_object_class_install_property (object_class, PROP_DEVICE, g_object_class_install_property (object_class, PROP_DEVICE,
g_param_spec_string ("device", NULL, NULL, g_param_spec_string ("device",
_("Device:"), NULL,
NULL, NULL,
G_PARAM_READWRITE | G_PARAM_READWRITE |
G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT |
@ -334,6 +337,9 @@ midi_set_device (ControllerMidi *midi,
if (midi->io) if (midi->io)
{ {
g_source_remove (midi->io_id);
midi->io_id = 0;
g_io_channel_unref (midi->io); g_io_channel_unref (midi->io);
midi->io = NULL; midi->io = NULL;
} }
@ -343,7 +349,7 @@ midi_set_device (ControllerMidi *midi,
midi->device = g_strdup (device); midi->device = g_strdup (device);
if (device) if (midi->device && strlen (midi->device))
{ {
gint fd; gint fd;
@ -355,16 +361,18 @@ midi_set_device (ControllerMidi *midi,
if (fd >= 0) if (fd >= 0)
{ {
g_object_set (midi, "name", device, NULL); gchar *name = g_strdup_printf (_("Reading from %s"), midi->device);
g_object_set (midi, "name", name, NULL);
g_free (name);
midi->io = g_io_channel_unix_new (fd); midi->io = g_io_channel_unix_new (fd);
g_io_channel_set_close_on_unref (midi->io, TRUE); g_io_channel_set_close_on_unref (midi->io, TRUE);
g_io_channel_set_encoding (midi->io, NULL, NULL); g_io_channel_set_encoding (midi->io, NULL, NULL);
g_io_add_watch (midi->io, midi->io_id = g_io_add_watch (midi->io,
G_IO_IN, G_IO_IN,
midi_read_event, midi_read_event,
midi); midi);
return TRUE; return TRUE;
} }
else else
@ -544,7 +552,8 @@ midi_read_event (GIOChannel *io,
if (midi->command == 0x9) if (midi->command == 0x9)
{ {
D (g_print ("MIDI: note on (%02x vel %02x)\n", D (g_print ("MIDI (ch %02d): note on (%02x vel %02x)\n",
midi->channel,
midi->key, midi->velocity)); midi->key, midi->velocity));
midi_event (midi, midi->key, midi_event (midi, midi->key,
@ -552,16 +561,16 @@ midi_read_event (GIOChannel *io,
} }
else if (midi->command == 0x8) else if (midi->command == 0x8)
{ {
D (g_print ("MIDI: note off (%02x vel %02x)\n", D (g_print ("MIDI (ch %02d): note off (%02x vel %02x)\n",
midi->key, midi->velocity)); midi->channel, midi->key, midi->velocity));
midi_event (midi, midi->key + 128, midi_event (midi, midi->key + 128,
(gdouble) midi->velocity / 127.0); (gdouble) midi->velocity / 127.0);
} }
else else
{ {
D (g_print ("MIDI: polyphonic aftertouch (%02x pressure %02x)\n", D (g_print ("MIDI (ch %02d): polyphonic aftertouch (%02x pressure %02x)\n",
midi->key, midi->velocity)); midi->channel, midi->key, midi->velocity));
} }
midi->key = -1; midi->key = -1;
@ -579,8 +588,8 @@ midi_read_event (GIOChannel *io,
if (midi->velocity == -1) if (midi->velocity == -1)
midi->velocity = buf[pos++]; midi->velocity = buf[pos++];
D (g_print ("MIDI: controller %d (value %d)\n", D (g_print ("MIDI (ch %02d): controller %d (value %d)\n",
midi->key, midi->velocity)); midi->channel, midi->key, midi->velocity));
midi_event (midi, midi->key + 128 + 128, midi_event (midi, midi->key + 128 + 128,
(gdouble) midi->velocity / 127.0); (gdouble) midi->velocity / 127.0);
@ -592,7 +601,8 @@ midi_read_event (GIOChannel *io,
case 0xc: /* program change */ case 0xc: /* program change */
midi->key = buf[pos++]; midi->key = buf[pos++];
D (g_print ("MIDI: program change (%d)\n", midi->key)); D (g_print ("MIDI (ch %02d): program change (%d)\n",
midi->channel, midi->key));
midi->key = -1; midi->key = -1;
break; break;
@ -600,7 +610,8 @@ midi_read_event (GIOChannel *io,
case 0xd: /* channel key pressure */ case 0xd: /* channel key pressure */
midi->velocity = buf[pos++]; midi->velocity = buf[pos++];
D (g_print ("MIDI: channel aftertouch (%d)\n", midi->velocity)); D (g_print ("MIDI (ch %02d): channel aftertouch (%d)\n",
midi->channel, midi->velocity));
midi->velocity = -1; midi->velocity = -1;
break; break;
@ -617,7 +628,8 @@ midi_read_event (GIOChannel *io,
midi->velocity = midi->lsb | (midi->msb << 7); midi->velocity = midi->lsb | (midi->msb << 7);
D (g_print ("MIDI: pitch (%d)\n", midi->velocity)); D (g_print ("MIDI (ch %02d): pitch (%d)\n",
midi->channel, midi->velocity));
midi->msb = -1; midi->msb = -1;
midi->lsb = -1; midi->lsb = -1;