Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just 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; 
    }

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    11,173
    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!

  3. #3
    Just Joined!
    Join Date
    Jun 2010
    Posts
    5
    Quote Originally Posted by Rubberman View Post
    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.
    It means that if i pressed key 'a' then symbol 'a' should appear in gedit's textarea.
    I check all things about you say, i've just skip checking in code before posting.

  4. #4
    Just Joined!
    Join Date
    Jun 2010
    Posts
    5
    homepage3.nifty.com/tsato/xvkbd/ events.html

  5. #5
    Just 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;  
    }

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    11,173
    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!

  7. #7
    Just Joined!
    Join Date
    Jun 2010
    Posts
    5
    Rubberman, don't worry.
    Quote Originally Posted by krvladislav View Post
    i've just skip checking in code before posting.

  8. #8
    Just 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 ); 
       }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •