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>
* app/widgets/gimpcontrollerwheel.[ch]: renamed function

View file

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

View file

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