]> git.alsa-project.org Git - alsa-gobject.git/commitdiff
rawmidi: query: allow caller to take ownership of file descriptor for control charact...
authorTakashi Sakamoto <o-takashi@sakamocchi.jp>
Mon, 16 Nov 2020 13:27:54 +0000 (22:27 +0900)
committer坂本 貴史 <o-takashi@sakamocchi.jp>
Mon, 16 Nov 2020 23:40:05 +0000 (08:40 +0900)
In design of ALSA rawmidi interface, ALSA control interface has important
function to some operations. The alsarawmidi library has an internal
helper function to operate control character device. The helper function
closes file descriptor to the control character device before returning
to caller, however it's inconvenient to some operations.

This commit allows the caller to take ownership for opened file descriptor
of control character device.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
src/rawmidi/query.c

index c077cb4cf973e95833b28cf477543141eb96fde8..1acd0ab41448ad0ce05b170c8f54fb0417e04105 100644 (file)
@@ -308,7 +308,7 @@ void alsarawmidi_get_rawmidi_devnode(guint card_id, guint device_id,
 }
 
 static void rawmidi_perform_ctl_ioctl(guint card_id, long request, void *data,
-                                      const char *req_label, GError **error)
+                                      const char *req_label, int *ctl_fd, GError **error)
 {
     unsigned int length;
     char *sysname;
@@ -349,7 +349,11 @@ static void rawmidi_perform_ctl_ioctl(guint card_id, long request, void *data,
     if (ioctl(fd, request, data) < 0)
         generate_file_error_fmt(error, errno, "ioctl(%s)", req_label);
 
-    close(fd);
+    // The caller is responsible for closing the file descriptor.
+    if (ctl_fd != NULL)
+        *ctl_fd = fd;
+    else
+        close(fd);
 err_device:
     udev_device_unref(dev);
 err_ctx:
@@ -391,7 +395,7 @@ void alsarawmidi_get_subdevice_id_list(guint card, guint device,
     g_return_if_fail(entry_count != NULL);
     g_return_if_fail(error == NULL || *error == NULL);
 
-    rawmidi_perform_ctl_ioctl(card, SNDRV_CTL_IOCTL_RAWMIDI_INFO, &info, "RAWMIDI_INFO", error);
+    rawmidi_perform_ctl_ioctl(card, SNDRV_CTL_IOCTL_RAWMIDI_INFO, &info, "RAWMIDI_INFO", NULL, error);
     if (*error != NULL)
         return;
 
@@ -436,7 +440,7 @@ void alsarawmidi_get_substream_info(guint card_id, guint device_id,
     info->stream = direction;
     info->card = card_id;
 
-    rawmidi_perform_ctl_ioctl(card_id, SNDRV_CTL_IOCTL_RAWMIDI_INFO, info, "RAWMIDI_INFO", error);
+    rawmidi_perform_ctl_ioctl(card_id, SNDRV_CTL_IOCTL_RAWMIDI_INFO, info, "RAWMIDI_INFO", NULL, error);
     if (*error != NULL)
         g_object_unref(*substream_info);
 }
@@ -445,5 +449,5 @@ void rawmidi_select_subdevice(guint card_id, guint subdevice_id, GError **error)
 {
     guint data = subdevice_id;
     rawmidi_perform_ctl_ioctl(card_id, SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE,
-                              &data, "RAWMIDI_PREFER_SUBDEVICE", error);
+                              &data, "RAWMIDI_PREFER_SUBDEVICE", NULL, error);
 }