27 #include "kxmessages.h"
29 #include <tdeapplication.h>
37 const long BROADCAST_MASK = PropertyChangeMask;
40 KXMessages::KXMessages(
const char* accept_broadcast_P, TQWidget* parent_P )
41 : TQWidget( parent_P )
43 if( accept_broadcast_P != NULL )
45 ( void ) kapp->desktop();
46 kapp->installX11EventFilter(
this );
47 accept_atom1 = XInternAtom( tqt_xdisplay(), accept_broadcast_P,
false );
48 accept_atom2 = accept_atom1;
52 accept_atom1 = accept_atom2 = None;
54 handle =
new TQWidget(
this );
57 KXMessages::KXMessages(
const char* accept_broadcast_P, TQWidget* parent_P,
bool obsolete_P )
58 : TQWidget( parent_P )
60 if( accept_broadcast_P != NULL )
62 ( void ) kapp->desktop();
63 kapp->installX11EventFilter(
this );
64 accept_atom2 = XInternAtom( tqt_xdisplay(), accept_broadcast_P,
false );
65 accept_atom1 = obsolete_P ? accept_atom2
66 : XInternAtom( tqt_xdisplay(), TQCString( accept_broadcast_P ) +
"_BEGIN",
false );
70 accept_atom1 = accept_atom2 = None;
72 handle =
new TQWidget(
this );
75 KXMessages::~KXMessages()
81 void KXMessages::broadcastMessage(
const char* msg_type_P,
const TQString& message_P )
83 broadcastMessage( msg_type_P, message_P, -1,
true );
86 void KXMessages::broadcastMessage(
const char* msg_type_P,
const TQString& message_P,
87 int screen_P,
bool obsolete_P )
89 Atom a2 = XInternAtom( tqt_xdisplay(), msg_type_P,
false );
90 Atom a1 = obsolete_P ? a2 : XInternAtom( tqt_xdisplay(), TQCString( msg_type_P ) +
"_BEGIN",
false );
91 Window root = screen_P == -1 ? tqt_xrootwin() : tqt_xrootwin( screen_P );
92 send_message_internal( root, message_P, BROADCAST_MASK, tqt_xdisplay(),
93 a1, a2, handle->winId());
96 void KXMessages::sendMessage( WId w_P,
const char* msg_type_P,
const TQString& message_P )
98 sendMessage( w_P, msg_type_P, message_P,
true );
101 void KXMessages::sendMessage( WId w_P,
const char* msg_type_P,
const TQString& message_P,
104 Atom a2 = XInternAtom( tqt_xdisplay(), msg_type_P,
false );
105 Atom a1 = obsolete_P ? a2 : XInternAtom( tqt_xdisplay(), TQCString( msg_type_P ) +
"_BEGIN",
false );
106 send_message_internal( w_P, message_P, 0, tqt_xdisplay(), a1, a2, handle->winId());
109 bool KXMessages::broadcastMessageX( Display* disp,
const char* msg_type_P,
110 const TQString& message_P )
112 return broadcastMessageX( disp, msg_type_P, message_P, -1,
true );
115 bool KXMessages::broadcastMessageX( Display* disp,
const char* msg_type_P,
116 const TQString& message_P,
int screen_P,
bool obsolete_P )
120 Atom a2 = XInternAtom( disp, msg_type_P,
false );
121 Atom a1 = obsolete_P ? a2 : XInternAtom( disp, TQCString( msg_type_P ) +
"_BEGIN",
false );
122 Window root = screen_P == -1 ? DefaultRootWindow( disp ) : RootWindow( disp, screen_P );
123 Window win = XCreateSimpleWindow( disp, root, 0, 0, 1, 1,
124 0, BlackPixel( disp, screen_P == -1 ? DefaultScreen( disp ) : screen_P ),
125 BlackPixel( disp, screen_P == -1 ? DefaultScreen( disp ) : screen_P ));
126 send_message_internal( root, message_P, BROADCAST_MASK, disp,
128 XDestroyWindow( disp, win );
132 bool KXMessages::sendMessageX( Display* disp, WId w_P,
const char* msg_type_P,
133 const TQString& message_P )
135 return sendMessageX( disp, w_P, msg_type_P, message_P,
true );
138 bool KXMessages::sendMessageX( Display* disp, WId w_P,
const char* msg_type_P,
139 const TQString& message_P,
bool obsolete_P )
143 Atom a2 = XInternAtom( disp, msg_type_P,
false );
144 Atom a1 = obsolete_P ? a2 : XInternAtom( disp, TQCString( msg_type_P ) +
"_BEGIN",
false );
145 Window win = XCreateSimpleWindow( disp, DefaultRootWindow( disp ), 0, 0, 1, 1,
146 0, BlackPixelOfScreen( DefaultScreenOfDisplay( disp )),
147 BlackPixelOfScreen( DefaultScreenOfDisplay( disp )));
148 send_message_internal( w_P, message_P, 0, disp, a1, a2, win );
149 XDestroyWindow( disp, win );
153 void KXMessages::send_message_internal( WId w_P,
const TQString& msg_P,
long mask_P,
154 Display* disp, Atom atom1_P, Atom atom2_P, Window handle_P )
156 unsigned int pos = 0;
157 TQCString msg = msg_P.utf8();
158 unsigned int len = strlen( msg );
160 e.xclient.type = ClientMessage;
161 e.xclient.message_type = atom1_P;
162 e.xclient.display = disp;
163 e.xclient.window = handle_P;
164 e.xclient.format = 8;
169 i < 20 && i + pos <= len;
171 e.xclient.data.b[ i ] = msg[ i + pos ];
172 XSendEvent( disp, w_P,
false, mask_P, &e );
173 e.xclient.message_type = atom2_P;
175 }
while( pos <= len );
179 bool KXMessages::x11Event( XEvent* ev_P )
181 if( ev_P->type != ClientMessage || ev_P->xclient.format != 8 )
182 return TQWidget::x11Event( ev_P );
183 if( ev_P->xclient.message_type != accept_atom1 && ev_P->xclient.message_type != accept_atom2 )
184 return TQWidget::x11Event( ev_P );
188 i < 20 && ev_P->xclient.data.b[ i ] !=
'\0';
190 buf[ i ] = ev_P->xclient.data.b[ i ];
192 if( incoming_messages.contains( ev_P->xclient.window ))
194 if( ev_P->xclient.message_type == accept_atom1 && accept_atom1 != accept_atom2 )
196 incoming_messages[ ev_P->xclient.window ] = TQCString();
197 incoming_messages[ ev_P->xclient.window ] += buf;
201 if( ev_P->xclient.message_type == accept_atom2 && accept_atom1 != accept_atom2 )
203 incoming_messages[ ev_P->xclient.window ] = buf;
207 emit gotMessage( TQString::fromUtf8( incoming_messages[ ev_P->xclient.window ] ));
208 incoming_messages.remove( ev_P->xclient.window );
213 #include "kxmessages.moc"