pactl: add `get_default_sink` command
authorJason Nader <jason.nader@protonmail.com>
Mon, 7 Dec 2020 14:28:34 +0000 (23:28 +0900)
committerPulseAudio Marge Bot <pulseaudio-maintainers@lists.freedesktop.org>
Mon, 17 May 2021 15:35:07 +0000 (15:35 +0000)
Part-of: <https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/430>

man/pactl.1.xml.in
src/utils/pactl.c

index 197df7d..fbc486e 100644 (file)
@@ -162,6 +162,11 @@ License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
     </option>
 
     <option>
+      <p><opt>get-default-sink</opt></p>
+      <optdesc><p>Returns the symbolic name of the default sink.</p></optdesc>
+    </option>
+
+    <option>
       <p><opt>set-default-sink</opt> <arg>SINK</arg></p>
       <optdesc><p>Make the specified sink (identified by its symbolic name or numerical index) the default sink.</p></optdesc>
     </option>
index 8e218d7..e905f03 100644 (file)
@@ -121,6 +121,7 @@ static enum {
     SUSPEND_SOURCE,
     SET_CARD_PROFILE,
     SET_SINK_PORT,
+    GET_DEFAULT_SINK,
     SET_DEFAULT_SINK,
     SET_SOURCE_PORT,
     SET_DEFAULT_SOURCE,
@@ -189,6 +190,18 @@ static void stat_callback(pa_context *c, const pa_stat_info *i, void *userdata)
     complete_action();
 }
 
+static void get_default_sink(pa_context *c, const pa_server_info *i, void *userdata) {
+    if (!i) {
+        pa_log(_("Failed to get server information: %s"), pa_strerror(pa_context_errno(c)));
+        quit(1);
+        return;
+    }
+
+    printf(_("%s\n"), i->default_sink_name);
+
+    complete_action();
+}
+
 static void get_server_info_callback(pa_context *c, const pa_server_info *i, void *useerdata) {
     char ss[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
 
@@ -1481,6 +1494,10 @@ static void context_state_callback(pa_context *c, void *userdata) {
                     o = pa_context_set_sink_port_by_name(c, sink_name, port_name, simple_callback, NULL);
                     break;
 
+                case GET_DEFAULT_SINK:
+                    o = pa_context_get_server_info(c, get_default_sink, NULL);
+                    break;
+
                 case SET_DEFAULT_SINK:
                     o = pa_context_set_default_sink(c, sink_name, simple_callback, NULL);
                     break;
@@ -1717,6 +1734,7 @@ static void help(const char *argv0) {
     printf("%s %s %s %s\n", argv0, _("[options]"), "move-(sink-input|source-output)", _("#N SINK|SOURCE"));
     printf("%s %s %s %s\n", argv0, _("[options]"), "suspend-(sink|source)", _("NAME|#N 1|0"));
     printf("%s %s %s %s\n", argv0, _("[options]"), "set-card-profile ", _("CARD PROFILE"));
+    printf("%s %s %s %s\n", argv0, _("[options]"), "get-default-(sink|source)", _("NAME"));
     printf("%s %s %s %s\n", argv0, _("[options]"), "set-default-(sink|source)", _("NAME"));
     printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink|source)-port", _("NAME|#N PORT"));
     printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink|source)-volume", _("NAME|#N VOLUME [VOLUME ...]"));
@@ -2017,6 +2035,9 @@ int main(int argc, char *argv[]) {
 
             sink_name = pa_xstrdup(argv[optind+1]);
 
+        } else if (pa_streq(argv[optind], "get-default-sink")) {
+            action = GET_DEFAULT_SINK;
+
         } else if (pa_streq(argv[optind], "set-source-port")) {
             action = SET_SOURCE_PORT;