Преглед изворни кода

implemented Client.get_ports()

Fabian Peter Hammerle пре 9 година
родитељ
комит
2188fabe7d
2 измењених фајлова са 64 додато и 0 уклоњено
  1. 27 0
      jack.c
  2. 37 0
      tests/ports-list.py

+ 27 - 0
jack.c

@@ -96,6 +96,27 @@ static PyObject* client_get_name(Client* self)
     return (PyObject*)PyString_FromString(jack_get_client_name(self->client));
 }
 
+static PyObject* client_get_ports(Client* self)
+{
+    PyObject* ports = PyList_New(0);
+    if(!ports) {
+        return NULL;
+    }
+
+    const char** port_names = jack_get_ports(self->client, NULL, NULL, 0);
+    int port_index;
+    for(port_index = 0; port_names[port_index] != NULL; port_index++) {
+        Port* port = PyObject_New(Port, &port_type);
+        port->port = jack_port_by_name(self->client, port_names[port_index]);
+        if(PyList_Append(ports, (PyObject*)port)) {
+            return NULL;
+        }
+    }
+    jack_free(port_names);
+
+    return ports;
+}
+
 static PyObject* client_set_port_registration_callback(Client* self, PyObject* args)
 {
     PyObject* callback;
@@ -134,6 +155,12 @@ static PyMethodDef client_methods[] = {
         METH_NOARGS,
         "Return client's actual name.",
         },
+    {
+        "get_ports",
+        (PyCFunction)client_get_ports,
+        METH_NOARGS,
+        "Return list of ports.",
+        },
     {
         "set_port_registration_callback",
         (PyCFunction)client_set_port_registration_callback,

+ 37 - 0
tests/ports-list.py

@@ -0,0 +1,37 @@
+#!/usr/bin/env python
+# PYTHON_ARGCOMPLETE_OK
+
+import sys
+import jack
+import time
+import pprint
+import argparse
+
+def run():
+
+    client = jack.Client("registration callback test");
+
+    for port in client.get_ports():
+        print port.get_name()
+
+def _init_argparser():
+
+    argparser = argparse.ArgumentParser(description = None)
+    return argparser
+
+def main(argv):
+
+    argparser = _init_argparser()
+    try:
+        import argcomplete
+        argcomplete.autocomplete(argparser)
+    except ImportError:
+        pass
+    args = argparser.parse_args(argv)
+
+    run(**vars(args))
+
+    return 0
+
+if __name__ == "__main__":
+    sys.exit(main(sys.argv[1:]))