diff options
Diffstat (limited to 'src/dbusbind.c')
| -rw-r--r-- | src/dbusbind.c | 164 |
1 files changed, 148 insertions, 16 deletions
diff --git a/src/dbusbind.c b/src/dbusbind.c index 4ef962d1507..0c59c08f5b7 100644 --- a/src/dbusbind.c +++ b/src/dbusbind.c | |||
| @@ -38,6 +38,7 @@ Lisp_Object Qdbus_call_method_asynchronously; | |||
| 38 | Lisp_Object Qdbus_method_return_internal; | 38 | Lisp_Object Qdbus_method_return_internal; |
| 39 | Lisp_Object Qdbus_method_error_internal; | 39 | Lisp_Object Qdbus_method_error_internal; |
| 40 | Lisp_Object Qdbus_send_signal; | 40 | Lisp_Object Qdbus_send_signal; |
| 41 | Lisp_Object Qdbus_register_service; | ||
| 41 | Lisp_Object Qdbus_register_signal; | 42 | Lisp_Object Qdbus_register_signal; |
| 42 | Lisp_Object Qdbus_register_method; | 43 | Lisp_Object Qdbus_register_method; |
| 43 | 44 | ||
| @@ -50,6 +51,17 @@ Lisp_Object QCdbus_system_bus, QCdbus_session_bus; | |||
| 50 | /* Lisp symbol for method call timeout. */ | 51 | /* Lisp symbol for method call timeout. */ |
| 51 | Lisp_Object QCdbus_timeout; | 52 | Lisp_Object QCdbus_timeout; |
| 52 | 53 | ||
| 54 | /* Lisp symbols for name request flags. */ | ||
| 55 | Lisp_Object QCdbus_request_name_allow_replacement; | ||
| 56 | Lisp_Object QCdbus_request_name_replace_existing; | ||
| 57 | Lisp_Object QCdbus_request_name_do_not_queue; | ||
| 58 | |||
| 59 | /* Lisp symbols for name request replies. */ | ||
| 60 | Lisp_Object QCdbus_request_name_reply_primary_owner; | ||
| 61 | Lisp_Object QCdbus_request_name_reply_in_queue; | ||
| 62 | Lisp_Object QCdbus_request_name_reply_exists; | ||
| 63 | Lisp_Object QCdbus_request_name_reply_already_owner; | ||
| 64 | |||
| 53 | /* Lisp symbols of D-Bus types. */ | 65 | /* Lisp symbols of D-Bus types. */ |
| 54 | Lisp_Object QCdbus_type_byte, QCdbus_type_boolean; | 66 | Lisp_Object QCdbus_type_byte, QCdbus_type_boolean; |
| 55 | Lisp_Object QCdbus_type_int16, QCdbus_type_uint16; | 67 | Lisp_Object QCdbus_type_int16, QCdbus_type_uint16; |
| @@ -1835,6 +1847,114 @@ xd_read_queued_messages (int fd, void *data, int for_read) | |||
| 1835 | xd_in_read_queued_messages = 0; | 1847 | xd_in_read_queued_messages = 0; |
| 1836 | } | 1848 | } |
| 1837 | 1849 | ||
| 1850 | DEFUN ("dbus-register-service", Fdbus_register_service, Sdbus_register_service, | ||
| 1851 | 2, MANY, 0, | ||
| 1852 | doc: /* Register known name SERVICE on the D-Bus BUS. | ||
| 1853 | |||
| 1854 | BUS is either a Lisp symbol, `:system' or `:session', or a string | ||
| 1855 | denoting the bus address. | ||
| 1856 | |||
| 1857 | SERVICE is the D-Bus service name that should be registered. It must | ||
| 1858 | be a known name. | ||
| 1859 | |||
| 1860 | FLAGS are keywords, which control how the service name is registered. | ||
| 1861 | The following keywords are recognized: | ||
| 1862 | |||
| 1863 | `:allow-replacement': Allow another service to become the primary | ||
| 1864 | owner if requested. | ||
| 1865 | |||
| 1866 | `:replace-existing': Request to replace the current primary owner. | ||
| 1867 | |||
| 1868 | `:do-not-queue': If we can not become the primary owner do not place | ||
| 1869 | us in the queue. | ||
| 1870 | |||
| 1871 | The function returns a keyword, indicating the result of the | ||
| 1872 | operation. One of the following keywords is returned: | ||
| 1873 | |||
| 1874 | `:primary-owner': Service has become the primary owner of the | ||
| 1875 | requested name. | ||
| 1876 | |||
| 1877 | `:in-queue': Service could not become the primary owner and has been | ||
| 1878 | placed in the queue. | ||
| 1879 | |||
| 1880 | `:exists': Service is already in the queue. | ||
| 1881 | |||
| 1882 | `:already-owner': Service is already the primary owner. | ||
| 1883 | |||
| 1884 | Example: | ||
| 1885 | |||
| 1886 | \(dbus-register-service :session dbus-service-emacs) | ||
| 1887 | |||
| 1888 | => :primary-owner. | ||
| 1889 | |||
| 1890 | \(dbus-register-service | ||
| 1891 | :session "org.freedesktop.TextEditor" | ||
| 1892 | dbus-service-allow-replacement dbus-service-replace-existing) | ||
| 1893 | |||
| 1894 | => :already-owner. | ||
| 1895 | |||
| 1896 | usage: (dbus-register-service BUS SERVICE &rest FLAGS) */) | ||
| 1897 | (int nargs, register Lisp_Object *args) | ||
| 1898 | { | ||
| 1899 | Lisp_Object bus, service; | ||
| 1900 | struct gcpro gcpro1, gcpro2; | ||
| 1901 | DBusConnection *connection; | ||
| 1902 | unsigned int i; | ||
| 1903 | unsigned int value; | ||
| 1904 | unsigned int flags = 0; | ||
| 1905 | int result; | ||
| 1906 | DBusError derror; | ||
| 1907 | |||
| 1908 | bus = args[0]; | ||
| 1909 | service = args[1]; | ||
| 1910 | |||
| 1911 | /* Check parameters. */ | ||
| 1912 | CHECK_STRING (service); | ||
| 1913 | |||
| 1914 | /* Process flags. */ | ||
| 1915 | for (i = 2; i < nargs; ++i) { | ||
| 1916 | value = ((EQ (args[i], QCdbus_request_name_replace_existing)) | ||
| 1917 | ? DBUS_NAME_FLAG_REPLACE_EXISTING | ||
| 1918 | : (EQ (args[i], QCdbus_request_name_allow_replacement)) | ||
| 1919 | ? DBUS_NAME_FLAG_ALLOW_REPLACEMENT | ||
| 1920 | : (EQ (args[i], QCdbus_request_name_do_not_queue)) | ||
| 1921 | ? DBUS_NAME_FLAG_DO_NOT_QUEUE | ||
| 1922 | : -1); | ||
| 1923 | if (value == -1) | ||
| 1924 | XD_SIGNAL2 (build_string ("Unrecognized name request flag"), args[i]); | ||
| 1925 | flags |= value; | ||
| 1926 | } | ||
| 1927 | |||
| 1928 | /* Open a connection to the bus. */ | ||
| 1929 | connection = xd_initialize (bus, TRUE); | ||
| 1930 | |||
| 1931 | /* Request the known name from the bus. */ | ||
| 1932 | dbus_error_init (&derror); | ||
| 1933 | result = dbus_bus_request_name (connection, SDATA (service), flags, | ||
| 1934 | &derror); | ||
| 1935 | if (dbus_error_is_set (&derror)) | ||
| 1936 | XD_ERROR (derror); | ||
| 1937 | |||
| 1938 | /* Cleanup. */ | ||
| 1939 | dbus_error_free (&derror); | ||
| 1940 | |||
| 1941 | /* Return object. */ | ||
| 1942 | switch (result) | ||
| 1943 | { | ||
| 1944 | case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER: | ||
| 1945 | return QCdbus_request_name_reply_primary_owner; | ||
| 1946 | case DBUS_REQUEST_NAME_REPLY_IN_QUEUE: | ||
| 1947 | return QCdbus_request_name_reply_in_queue; | ||
| 1948 | case DBUS_REQUEST_NAME_REPLY_EXISTS: | ||
| 1949 | return QCdbus_request_name_reply_exists; | ||
| 1950 | case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER: | ||
| 1951 | return QCdbus_request_name_reply_already_owner; | ||
| 1952 | default: | ||
| 1953 | /* This should not happen. */ | ||
| 1954 | XD_SIGNAL2 (build_string ("Could not register service"), service); | ||
| 1955 | } | ||
| 1956 | } | ||
| 1957 | |||
| 1838 | DEFUN ("dbus-register-signal", Fdbus_register_signal, Sdbus_register_signal, | 1958 | DEFUN ("dbus-register-signal", Fdbus_register_signal, Sdbus_register_signal, |
| 1839 | 6, MANY, 0, | 1959 | 6, MANY, 0, |
| 1840 | doc: /* Register for signal SIGNAL on the D-Bus BUS. | 1960 | doc: /* Register for signal SIGNAL on the D-Bus BUS. |
| @@ -2011,9 +2131,8 @@ discovering the still incomplete interface.*/) | |||
| 2011 | Lisp_Object dont_register_service) | 2131 | Lisp_Object dont_register_service) |
| 2012 | { | 2132 | { |
| 2013 | Lisp_Object key, key1, value; | 2133 | Lisp_Object key, key1, value; |
| 2014 | DBusConnection *connection; | ||
| 2015 | int result; | ||
| 2016 | DBusError derror; | 2134 | DBusError derror; |
| 2135 | Lisp_Object args[2] = { bus, service }; | ||
| 2017 | 2136 | ||
| 2018 | /* Check parameters. */ | 2137 | /* Check parameters. */ |
| 2019 | CHECK_STRING (service); | 2138 | CHECK_STRING (service); |
| @@ -2025,21 +2144,9 @@ discovering the still incomplete interface.*/) | |||
| 2025 | /* TODO: We must check for a valid service name, otherwise there is | 2144 | /* TODO: We must check for a valid service name, otherwise there is |
| 2026 | a segmentation fault. */ | 2145 | a segmentation fault. */ |
| 2027 | 2146 | ||
| 2028 | /* Open a connection to the bus. */ | 2147 | /* Request the name. */ |
| 2029 | connection = xd_initialize (bus, TRUE); | ||
| 2030 | |||
| 2031 | /* Request the known name from the bus. We can ignore the result, | ||
| 2032 | it is set to -1 if there is an error - kind of redundancy. */ | ||
| 2033 | if (NILP (dont_register_service)) | 2148 | if (NILP (dont_register_service)) |
| 2034 | { | 2149 | Fdbus_register_service (2, args); |
| 2035 | dbus_error_init (&derror); | ||
| 2036 | result = dbus_bus_request_name (connection, SDATA (service), 0, &derror); | ||
| 2037 | if (dbus_error_is_set (&derror)) | ||
| 2038 | XD_ERROR (derror); | ||
| 2039 | |||
| 2040 | /* Cleanup. */ | ||
| 2041 | dbus_error_free (&derror); | ||
| 2042 | } | ||
| 2043 | 2150 | ||
| 2044 | /* Create a hash table entry. We use nil for the unique name, | 2151 | /* Create a hash table entry. We use nil for the unique name, |
| 2045 | because the method might be called from anybody. */ | 2152 | because the method might be called from anybody. */ |
| @@ -2091,6 +2198,10 @@ syms_of_dbusbind (void) | |||
| 2091 | staticpro (&Qdbus_send_signal); | 2198 | staticpro (&Qdbus_send_signal); |
| 2092 | defsubr (&Sdbus_send_signal); | 2199 | defsubr (&Sdbus_send_signal); |
| 2093 | 2200 | ||
| 2201 | Qdbus_register_service = intern_c_string ("dbus-register-service"); | ||
| 2202 | staticpro (&Qdbus_register_service); | ||
| 2203 | defsubr (&Sdbus_register_service); | ||
| 2204 | |||
| 2094 | Qdbus_register_signal = intern_c_string ("dbus-register-signal"); | 2205 | Qdbus_register_signal = intern_c_string ("dbus-register-signal"); |
| 2095 | staticpro (&Qdbus_register_signal); | 2206 | staticpro (&Qdbus_register_signal); |
| 2096 | defsubr (&Sdbus_register_signal); | 2207 | defsubr (&Sdbus_register_signal); |
| @@ -2112,6 +2223,27 @@ syms_of_dbusbind (void) | |||
| 2112 | QCdbus_session_bus = intern_c_string (":session"); | 2223 | QCdbus_session_bus = intern_c_string (":session"); |
| 2113 | staticpro (&QCdbus_session_bus); | 2224 | staticpro (&QCdbus_session_bus); |
| 2114 | 2225 | ||
| 2226 | QCdbus_request_name_allow_replacement = intern_c_string (":allow-replacement"); | ||
| 2227 | staticpro (&QCdbus_request_name_allow_replacement); | ||
| 2228 | |||
| 2229 | QCdbus_request_name_replace_existing = intern_c_string (":replace-existing"); | ||
| 2230 | staticpro (&QCdbus_request_name_replace_existing); | ||
| 2231 | |||
| 2232 | QCdbus_request_name_do_not_queue = intern_c_string (":do-not-queue"); | ||
| 2233 | staticpro (&QCdbus_request_name_do_not_queue); | ||
| 2234 | |||
| 2235 | QCdbus_request_name_reply_primary_owner = intern_c_string (":primary-owner"); | ||
| 2236 | staticpro (&QCdbus_request_name_reply_primary_owner); | ||
| 2237 | |||
| 2238 | QCdbus_request_name_reply_exists = intern_c_string (":exists"); | ||
| 2239 | staticpro (&QCdbus_request_name_reply_exists); | ||
| 2240 | |||
| 2241 | QCdbus_request_name_reply_in_queue = intern_c_string (":in-queue"); | ||
| 2242 | staticpro (&QCdbus_request_name_reply_in_queue); | ||
| 2243 | |||
| 2244 | QCdbus_request_name_reply_already_owner = intern_c_string (":already-owner"); | ||
| 2245 | staticpro (&QCdbus_request_name_reply_already_owner); | ||
| 2246 | |||
| 2115 | QCdbus_timeout = intern_c_string (":timeout"); | 2247 | QCdbus_timeout = intern_c_string (":timeout"); |
| 2116 | staticpro (&QCdbus_timeout); | 2248 | staticpro (&QCdbus_timeout); |
| 2117 | 2249 | ||