Original DBUS bindings for TQt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

integrator.cpp 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
  2. /* integrator.h: integrates D-BUS into TQt event loop
  3. *
  4. * Copyright (C) 2003 Zack Rusin <zack@kde.org>
  5. *
  6. * Licensed under the Academic Free License version 2.0
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include "integrator.h"
  24. #include "connection.h"
  25. #include <tqtimer.h>
  26. #include <tqsocketnotifier.h>
  27. #include <tqintdict.h>
  28. #include <tqptrlist.h>
  29. namespace DBusQt
  30. {
  31. namespace Internal {
  32. struct Watch {
  33. Watch(): readSocket( 0 ), writeSocket( 0 ) { }
  34. DBusWatch *watch;
  35. TQSocketNotifier *readSocket;
  36. TQSocketNotifier *writeSocket;
  37. };
  38. //////////////////////////////////////////////////////////////
  39. dbus_bool_t dbusAddWatch( DBusWatch *watch, void *data )
  40. {
  41. Integrator *con = static_cast<Integrator*>( data );
  42. con->addWatch( watch );
  43. return true;
  44. }
  45. void dbusRemoveWatch( DBusWatch *watch, void *data )
  46. {
  47. Integrator *con = static_cast<Integrator*>( data );
  48. con->removeWatch( watch );
  49. }
  50. void dbusToggleWatch( DBusWatch *watch, void *data )
  51. {
  52. Integrator *itg = static_cast<Integrator*>( data );
  53. if ( dbus_watch_get_enabled( watch ) )
  54. itg->addWatch( watch );
  55. else
  56. itg->removeWatch( watch );
  57. }
  58. dbus_bool_t dbusAddTimeout( DBusTimeout *timeout, void *data )
  59. {
  60. if ( !dbus_timeout_get_enabled(timeout) )
  61. return true;
  62. Integrator *itg = static_cast<Integrator*>( data );
  63. itg->addTimeout( timeout );
  64. return true;
  65. }
  66. void dbusRemoveTimeout( DBusTimeout *timeout, void *data )
  67. {
  68. Integrator *itg = static_cast<Integrator*>( data );
  69. itg->removeTimeout( timeout );
  70. }
  71. void dbusToggleTimeout( DBusTimeout *timeout, void *data )
  72. {
  73. Integrator *itg = static_cast<Integrator*>( data );
  74. if ( dbus_timeout_get_enabled( timeout ) )
  75. itg->addTimeout( timeout );
  76. else
  77. itg->removeTimeout( timeout );
  78. }
  79. void dbusWakeupMain( void* )
  80. {
  81. }
  82. void dbusNewConnection( DBusServer *server,
  83. DBusConnection *new_connection,
  84. void *data )
  85. {
  86. Integrator *itg = static_cast<Integrator*>( data );
  87. itg->handleConnection( new_connection );
  88. }
  89. /////////////////////////////////////////////////////////////
  90. Timeout::Timeout( TQObject *parent, DBusTimeout *t )
  91. : TQObject( parent ), m_timeout( t )
  92. {
  93. m_timer = new TQTimer( this );
  94. connect( m_timer, TQT_SIGNAL(timeout()),
  95. TQT_SLOT(slotTimeout()) );
  96. }
  97. void Timeout::slotTimeout()
  98. {
  99. emit timeout( m_timeout );
  100. }
  101. void Timeout::start()
  102. {
  103. m_timer->start( dbus_timeout_get_interval( m_timeout ) );
  104. }
  105. Integrator::Integrator( DBusConnection *conn, TQObject *parent )
  106. : TQObject( parent ), m_connection( conn )
  107. {
  108. m_timeouts.setAutoDelete( true );
  109. dbus_connection_set_watch_functions( m_connection,
  110. dbusAddWatch,
  111. dbusRemoveWatch,
  112. dbusToggleWatch,
  113. this, 0 );
  114. dbus_connection_set_timeout_functions( m_connection,
  115. dbusAddTimeout,
  116. dbusRemoveTimeout,
  117. dbusToggleTimeout,
  118. this, 0 );
  119. dbus_connection_set_wakeup_main_function( m_connection,
  120. dbusWakeupMain,
  121. this, 0 );
  122. }
  123. Integrator::Integrator( DBusServer *server, TQObject *parent )
  124. : TQObject( parent ), m_server( server )
  125. {
  126. m_connection = reinterpret_cast<DBusConnection*>( m_server );
  127. m_timeouts.setAutoDelete( true );
  128. dbus_server_set_watch_functions( m_server,
  129. dbusAddWatch,
  130. dbusRemoveWatch,
  131. dbusToggleWatch,
  132. this, 0 );
  133. dbus_server_set_timeout_functions( m_server,
  134. dbusAddTimeout,
  135. dbusRemoveTimeout,
  136. dbusToggleTimeout,
  137. this, 0 );
  138. dbus_server_set_new_connection_function( m_server,
  139. dbusNewConnection,
  140. this, 0 );
  141. }
  142. void Integrator::slotRead( int fd )
  143. {
  144. TQIntDictIterator<Watch> it( m_watches );
  145. for ( ; it.current(); ++it )
  146. dbus_watch_handle ( it.current()->watch, DBUS_WATCH_READABLE );
  147. emit readReady();
  148. }
  149. void Integrator::slotWrite( int fd )
  150. {
  151. TQIntDictIterator<Watch> it( m_watches );
  152. for ( ; it.current(); ++it )
  153. dbus_watch_handle ( it.current()->watch, DBUS_WATCH_WRITABLE );
  154. }
  155. void Integrator::slotTimeout( DBusTimeout *timeout )
  156. {
  157. dbus_timeout_handle( timeout );
  158. }
  159. void Integrator::addWatch( DBusWatch *watch )
  160. {
  161. if ( !dbus_watch_get_enabled( watch ) )
  162. return;
  163. Watch *qtwatch = new Watch;
  164. qtwatch->watch = watch;
  165. int flags = dbus_watch_get_flags( watch );
  166. int fd = dbus_watch_get_fd( watch );
  167. if ( flags & DBUS_WATCH_READABLE ) {
  168. qtwatch->readSocket = new TQSocketNotifier( fd, TQSocketNotifier::Read, this );
  169. TQObject::connect( qtwatch->readSocket, TQT_SIGNAL(activated(int)), TQT_SLOT(slotRead(int)) );
  170. }
  171. if (flags & DBUS_WATCH_WRITABLE) {
  172. qtwatch->writeSocket = new TQSocketNotifier( fd, TQSocketNotifier::Write, this );
  173. TQObject::connect( qtwatch->writeSocket, TQT_SIGNAL(activated(int)), TQT_SLOT(slotWrite(int)) );
  174. }
  175. m_watches.insert( fd, qtwatch );
  176. }
  177. void Integrator::removeWatch( DBusWatch *watch )
  178. {
  179. int key = dbus_watch_get_fd( watch );
  180. Watch *qtwatch = m_watches.take( key );
  181. if ( qtwatch ) {
  182. delete qtwatch->readSocket; qtwatch->readSocket = 0;
  183. delete qtwatch->writeSocket; qtwatch->writeSocket = 0;
  184. delete qtwatch;
  185. }
  186. }
  187. void Integrator::addTimeout( DBusTimeout *timeout )
  188. {
  189. Timeout *mt = new Timeout( this, timeout );
  190. m_timeouts.insert( timeout, mt );
  191. connect( mt, TQT_SIGNAL(timeout(DBusTimeout*)),
  192. TQT_SLOT(slotTimeout(DBusTimeout*)) );
  193. mt->start();
  194. }
  195. void Integrator::removeTimeout( DBusTimeout *timeout )
  196. {
  197. m_timeouts.remove( timeout );
  198. }
  199. void Integrator::handleConnection( DBusConnection *c )
  200. {
  201. Connection *con = new Connection( c, this );
  202. emit newConnection( con );
  203. }
  204. }//end namespace Internal
  205. }//end namespace DBusQt
  206. #include "integrator.moc"