Results 1 to 8 of 8
Sorry for English. I need help. There is code (see below). What it does: opens gedit, then creates new X-window, and redirects all keyevents of new window to gedit. I ...
- 06-13-2010 #1Just Joined!
- Join Date
- Jun 2010
- Posts
- 5
xlib send event
Sorry for English. I need help. There is code (see below). What it does: opens gedit, then creates new X-window, and redirects all keyevents of new window to gedit. I want to see new symbols in gedit, but there is nothing. How to correct this?
Code:#include <X11/Xlib.h> #include <cassert> #include <unistd.h> #include <cstdlib> #include <cstdio> #include <wait.h> #include <iostream> #include <fcntl.h> #include <string> using namespace std; int main( int argc, char ** argv ) { string app = "gedit"; int pid = fork(); if ( pid == 0 ) { execlp( app.c_str(), app.c_str(), NULL ); exit( 0 ); } int status; waitpid( pid, &status, WNOHANG ); sleep(1); string cmd = string() + "xwininfo -tree -root -int | grep " + app.c_str() + " | awk '{ print $1 }' | tail -n 1"; FILE * wininfo = popen( cmd.c_str(), "r" ); char wid_str[ 80 ]; fread( (void *) wid_str, sizeof( char ), sizeof( wid_str ), wininfo ); pclose( wininfo ); Window w = (Window) atol( wid_str ); Display * dpy = XOpenDisplay( NULL ); Window sw = XCreateSimpleWindow( dpy, DefaultRootWindow( dpy ), 0, 0, 100, 100, 1, WhitePixel( dpy, DefaultScreen( dpy ) ), BlackPixel( dpy, DefaultScreen( dpy ) ) ); XSelectInput( dpy, sw, KeyPressMask|KeyReleaseMask ); XMapWindow( dpy, sw ); XEvent event; while (1) { XNextEvent( dpy, &event ); XSendEvent( dpy, w, False, KeyPressMask|KeyReleaseMask, &event ); } return 0; }
- 06-13-2010 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,957
What do you mean by "new symbols in gedit"? Also, are you sure that gedit is running? Also, when you call waitpid() you don't check the status variable to see if it is actually running. Finally, you have not done any checking on the X-Windows API calls to verify that they were successful. In summation, this code needs some serious reworking to either do what you want it to, or at the very least let you know when it isn't working for whatever reason. Right now, you have no way to tell if gedit is running, if your X API calls succeeded and gedit is getting the X events, or if everything is fubar'd and not working at all.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 06-14-2010 #3Just Joined!
- Join Date
- Jun 2010
- Posts
- 5
- 06-14-2010 #4Just Joined!
- Join Date
- Jun 2010
- Posts
- 5
homepage3.nifty.com/tsato/xvkbd/ events.html
- 06-14-2010 #5Just Joined!
- Join Date
- Jun 2010
- Posts
- 5
Other problem ) I want to receive events from gedit. Code below doesn't work properly. How to correct it?
Code:#include <X11/Xlib.h> #include <cassert> #include <unistd.h> #include <cstdlib> #include <cstdio> #include <wait.h> #include <iostream> #include <fcntl.h> #include <string> using namespace std; int main( int argc, char ** argv ) { string app = "gedit"; int pid = fork(); if ( pid == 0 ) { execlp( app.c_str(), app.c_str(), NULL ); exit( 0 ); } int status; waitpid( pid, &status, WNOHANG ); sleep(2); string cmd = string() + "xwininfo -tree -root -int | grep " + app.c_str() + " | awk '{ print $1 }' | tail -n 1"; FILE * wininfo = popen( cmd.c_str(), "r" ); char wid_str[ 80 ]; fread( (void *) wid_str, sizeof( char ), sizeof( wid_str ), wininfo ); pclose( wininfo ); Window w = (Window) atol( wid_str ); Display * dpy = XOpenDisplay( NULL ); XSelectInput( dpy, w, KeyPressMask | KeyReleaseMask ); while (1) { XEvent event; XNextEvent( dpy, &event ); cout << "!" << endl; } XCloseDisplay( dpy ); return 0; }
- 06-14-2010 #6Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,957
First, you need to pay attention to the return values and error codes set by the various functions. You do none of that here. Without such, it is impossible to correct your code. You MUST write your code in a way that is expecting to deal with failure somewhere along the line. The reasons are multitude. Until you do that, I cannot help you in any reasonable manner.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 06-14-2010 #7
- 01-24-2011 #8Just Joined!
- Join Date
- Jan 2011
- Posts
- 1
Hi,
just came across your post.
You have to change the source of the event
Code:. ... XEvent event; while (1) { XNextEvent( dpy, &event ); cout << event.xkey.window << " " << event.xkey.type << " " << event.xkey.keycode << endl; event.xkey.window=w; // <<< THIS IS IMPORTANT XSendEvent( dpy, w, False, KeyPressMask|KeyReleaseMask, &event ); }


Reply With Quote

