comparison src/dbusbind.c @ 110609:7867879a43ce

* dbusbind.c (dbus_fd_cb, xd_get_dispatch_status) (xd_pending_messages): Functions removed. (xd_read_queued_messages): Add parameters fd, *data, for_read in order to be compatible with add_read_fd. Determine bus from data, and call xd_read_message just for this bus. (xd_add_watch): Use xd_read_queued_messages as callback function. Add data. * lisp.h (xd_pending_messages, xd_read_queued_messages): Remove.
author Michael Albinus <michael.albinus@gmx.de>
date Mon, 27 Sep 2010 17:34:04 +0200
parents c06958da83b5
children 6a7fb61524be
comparison
equal deleted inserted replaced
110608:06497cf3e920 110609:7867879a43ce
798 798
799 /* Return the result. */ 799 /* Return the result. */
800 return connection; 800 return connection;
801 } 801 }
802 802
803 /* Callback called when something is read to read ow write. */
804
805 static void
806 dbus_fd_cb (int fd, void *data, int for_read)
807 {
808 xd_read_queued_messages ();
809 }
810
811 /* Return the file descriptor for WATCH, -1 if not found. */ 803 /* Return the file descriptor for WATCH, -1 if not found. */
812
813 static int 804 static int
814 xd_find_watch_fd (DBusWatch *watch) 805 xd_find_watch_fd (DBusWatch *watch)
815 { 806 {
816 #if HAVE_DBUS_WATCH_GET_UNIX_FD 807 #if HAVE_DBUS_WATCH_GET_UNIX_FD
817 /* TODO: Reverse these on Win32, which prefers the opposite. */ 808 /* TODO: Reverse these on Win32, which prefers the opposite. */
822 int fd = dbus_watch_get_fd (watch); 813 int fd = dbus_watch_get_fd (watch);
823 #endif 814 #endif
824 return fd; 815 return fd;
825 } 816 }
826 817
818 /* Prototype. */
819 static void
820 xd_read_queued_messages (int fd, void *data, int for_read);
827 821
828 /* Start monitoring WATCH for possible I/O. */ 822 /* Start monitoring WATCH for possible I/O. */
829
830 static dbus_bool_t 823 static dbus_bool_t
831 xd_add_watch (DBusWatch *watch, void *data) 824 xd_add_watch (DBusWatch *watch, void *data)
832 { 825 {
833 unsigned int flags = dbus_watch_get_flags (watch); 826 unsigned int flags = dbus_watch_get_flags (watch);
834 int fd = xd_find_watch_fd (watch); 827 int fd = xd_find_watch_fd (watch);
841 return FALSE; 834 return FALSE;
842 835
843 if (dbus_watch_get_enabled (watch)) 836 if (dbus_watch_get_enabled (watch))
844 { 837 {
845 if (flags & DBUS_WATCH_WRITABLE) 838 if (flags & DBUS_WATCH_WRITABLE)
846 add_write_fd (fd, dbus_fd_cb, NULL); 839 add_write_fd (fd, xd_read_queued_messages, data);
847 if (flags & DBUS_WATCH_READABLE) 840 if (flags & DBUS_WATCH_READABLE)
848 add_read_fd (fd, dbus_fd_cb, NULL); 841 add_read_fd (fd, xd_read_queued_messages, data);
849 } 842 }
850 return TRUE; 843 return TRUE;
851 } 844 }
852 845
853 /* Stop monitoring WATCH for possible I/O. 846 /* Stop monitoring WATCH for possible I/O.
854 DATA is the used bus, either a string or QCdbus_system_bus or 847 DATA is the used bus, either a string or QCdbus_system_bus or
855 QCdbus_session_bus. */ 848 QCdbus_session_bus. */
856
857 static void 849 static void
858 xd_remove_watch (DBusWatch *watch, void *data) 850 xd_remove_watch (DBusWatch *watch, void *data)
859 { 851 {
860 unsigned int flags = dbus_watch_get_flags (watch); 852 unsigned int flags = dbus_watch_get_flags (watch);
861 int fd = xd_find_watch_fd (watch); 853 int fd = xd_find_watch_fd (watch);
862 854
863 XD_DEBUG_MESSAGE ("fd %d", fd); 855 XD_DEBUG_MESSAGE ("fd %d", fd);
864 856
865 if (fd == -1) return; 857 if (fd == -1)
866 858 return;
867 859
868 /* Unset session environment. */ 860 /* Unset session environment. */
869 if (data != NULL && data == (void*) XHASH (QCdbus_session_bus)) 861 if (data != NULL && data == (void*) XHASH (QCdbus_session_bus))
870 { 862 {
871 XD_DEBUG_MESSAGE ("unsetenv DBUS_SESSION_BUS_ADDRESS"); 863 XD_DEBUG_MESSAGE ("unsetenv DBUS_SESSION_BUS_ADDRESS");
877 if (flags & DBUS_WATCH_READABLE) 869 if (flags & DBUS_WATCH_READABLE)
878 delete_read_fd (fd); 870 delete_read_fd (fd);
879 } 871 }
880 872
881 /* Toggle monitoring WATCH for possible I/O. */ 873 /* Toggle monitoring WATCH for possible I/O. */
882
883 static void 874 static void
884 xd_toggle_watch (DBusWatch *watch, void *data) 875 xd_toggle_watch (DBusWatch *watch, void *data)
885 { 876 {
886 if (dbus_watch_get_enabled (watch)) 877 if (dbus_watch_get_enabled (watch))
887 xd_add_watch (watch, data); 878 xd_add_watch (watch, data);
1611 1602
1612 /* Return. */ 1603 /* Return. */
1613 return Qt; 1604 return Qt;
1614 } 1605 }
1615 1606
1616 /* Check, whether there is pending input in the message queue of the
1617 D-Bus BUS. BUS is either a Lisp symbol, :system or :session, or a
1618 string denoting the bus address. */
1619 int
1620 xd_get_dispatch_status (Lisp_Object bus)
1621 {
1622 DBusConnection *connection;
1623
1624 /* Open a connection to the bus. */
1625 connection = xd_initialize (bus, FALSE);
1626 if (connection == NULL) return FALSE;
1627
1628 /* Non blocking read of the next available message. */
1629 dbus_connection_read_write (connection, 0);
1630
1631 /* Return. */
1632 return
1633 (dbus_connection_get_dispatch_status (connection)
1634 == DBUS_DISPATCH_DATA_REMAINS)
1635 ? TRUE : FALSE;
1636 }
1637
1638 /* Check for queued incoming messages from the buses. */
1639 int
1640 xd_pending_messages (void)
1641 {
1642 Lisp_Object busp = Vdbus_registered_buses;
1643
1644 while (!NILP (busp))
1645 {
1646 /* We do not want to have an autolaunch for the session bus. */
1647 if (EQ ((CAR_SAFE (busp)), QCdbus_session_bus)
1648 && getenv ("DBUS_SESSION_BUS_ADDRESS") == NULL)
1649 continue;
1650
1651 if (xd_get_dispatch_status (CAR_SAFE (busp)))
1652 return TRUE;
1653
1654 busp = CDR_SAFE (busp);
1655 }
1656
1657 return FALSE;
1658 }
1659
1660 /* Read one queued incoming message of the D-Bus BUS. 1607 /* Read one queued incoming message of the D-Bus BUS.
1661 BUS is either a Lisp symbol, :system or :session, or a string denoting 1608 BUS is either a Lisp symbol, :system or :session, or a string denoting
1662 the bus address. */ 1609 the bus address. */
1663
1664 static void 1610 static void
1665 xd_read_message_1 (DBusConnection *connection, Lisp_Object bus) 1611 xd_read_message_1 (DBusConnection *connection, Lisp_Object bus)
1666 { 1612 {
1667 Lisp_Object args, key, value; 1613 Lisp_Object args, key, value;
1668 struct gcpro gcpro1; 1614 struct gcpro gcpro1;
1812 } 1758 }
1813 1759
1814 /* Read queued incoming messages of the D-Bus BUS. 1760 /* Read queued incoming messages of the D-Bus BUS.
1815 BUS is either a Lisp symbol, :system or :session, or a string denoting 1761 BUS is either a Lisp symbol, :system or :session, or a string denoting
1816 the bus address. */ 1762 the bus address. */
1817
1818 static Lisp_Object 1763 static Lisp_Object
1819 xd_read_message (Lisp_Object bus) 1764 xd_read_message (Lisp_Object bus)
1820 { 1765 {
1821 /* Open a connection to the bus. */ 1766 /* Open a connection to the bus. */
1822 DBusConnection *connection = xd_initialize (bus, TRUE); 1767 DBusConnection *connection = xd_initialize (bus, TRUE);
1828 != DBUS_DISPATCH_COMPLETE) 1773 != DBUS_DISPATCH_COMPLETE)
1829 xd_read_message_1 (connection, bus); 1774 xd_read_message_1 (connection, bus);
1830 return Qnil; 1775 return Qnil;
1831 } 1776 }
1832 1777
1833 /* Read queued incoming messages from all buses. */ 1778 /* Callback called when something is ready to read or write. */
1834 void 1779 static void
1835 xd_read_queued_messages (void) 1780 xd_read_queued_messages (int fd, void *data, int for_read)
1836 { 1781 {
1837 Lisp_Object busp = Vdbus_registered_buses; 1782 Lisp_Object busp = Vdbus_registered_buses;
1838 1783 Lisp_Object bus = Qnil;
1784
1785 /* Find bus related to fd. */
1786 if (data != NULL)
1787 while (!NILP (busp))
1788 {
1789 if (data == (void*) XHASH (CAR_SAFE (busp)))
1790 bus = CAR_SAFE (busp);
1791 busp = CDR_SAFE (busp);
1792 }
1793
1794 if (NILP(bus))
1795 return;
1796
1797 /* We ignore all Lisp errors during the call. */
1839 xd_in_read_queued_messages = 1; 1798 xd_in_read_queued_messages = 1;
1840 while (!NILP (busp)) 1799 internal_catch (Qdbus_error, xd_read_message, bus);
1841 {
1842 /* We ignore all Lisp errors during the call. */
1843 internal_catch (Qdbus_error, xd_read_message, CAR_SAFE (busp));
1844 busp = CDR_SAFE (busp);
1845 }
1846 xd_in_read_queued_messages = 0; 1800 xd_in_read_queued_messages = 0;
1847 } 1801 }
1848 1802
1849 DEFUN ("dbus-register-signal", Fdbus_register_signal, Sdbus_register_signal, 1803 DEFUN ("dbus-register-signal", Fdbus_register_signal, Sdbus_register_signal,
1850 6, MANY, 0, 1804 6, MANY, 0,