Find the answer to your Linux question:
Results 1 to 4 of 4
Hello everyone. I'm in the process of programming am application for controlling the bightness of my screen, reducing it when the computer is idle for more than 5 minutes. However, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2006
    Posts
    2

    Question How to capture X11 events globally?


    Hello everyone. I'm in the process of programming am application for controlling the bightness of my screen, reducing it when the computer is idle for more than 5 minutes. However, I cannot find a way to capture the keyboard and mouse events globally. I've tried SDL and pure X11 programming, but I cannot find a way to capture the events. I read in one thread that I must bind to the root window to do that but I attemted the binding and it still doesn't work.

  2. #2
    Linux User
    Join Date
    Aug 2005
    Location
    Italy
    Posts
    401
    This is probably because your desktop enviroment. I explain: most complex desktop managers create a fake root window (wich is a normal window) wich cover the root window.

    Probably the root window has something disabled, or some hack is done to exclude completely the root window to favourite the fake root window.

    Surely what someone told you is correct: you should bind the root window. Try to execute a minimal X session (start only X, with few X11 applications like xclock and xterm), and then try to execute your application. If it works, probably it's because your actual desktop manager.
    When using Windows, have you ever told "Ehi... do your business?"
    Linux user #396597 (http://counter.li.org)

  3. #3
    Just Joined!
    Join Date
    Apr 2006
    Posts
    2
    Can you give me a shot snippet of how to do that because I think I am doing something wrong with the code.

    What I did was to query the keyboard and mouse every x milliseconds and see if their status (coordinates, keys) has changed. Here is the code I used:

    Code:
    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif 
    
    #include <iostream>
    #include <cstdlib>
    #include <unistd.h>
    #include <X11/Xlib.h>
    #include <time.h>
    
    using namespace std;
    
    int defaultBrightness=7;
    int minimumBrightness = 1;
    
    void setBrightness(int brightness){
    	if(brightness>8 || brightness <1) { cout << "Brightness cannot be set at " << brightness << endl; return;}
    	char buf[1024];
    	sprintf(buf,"echo %i > /proc/acpi/sony/brightness", brightness);
    	system(buf);
    }
    
    char keymap[32];
    char prevKeymap[32];
    
    bool hasTyped(Display* display)
    {
     	XQueryKeymap(display, keymap);
    	for(int i=0; i<32; i++){
    		if (prevKeymap[i]!=keymap[i]){
    			XQueryKeymap(display, prevKeymap);
    			return true;
    		}
    	}
    	return false;
    }
    
    int main(int argc, char *argv[])
    {
    	setBrightness(defaultBrightness);
    	Display *display;
     	Window root, fromroot, tmpwin;
       	int screen = -1, rot, x, y,prev_x,prev_y, tmp;
    	uint tmp2;
    
    	if ((display = XOpenDisplay(NULL)) == NULL) {
        		fprintf(stderr, "xpointerswap: fatal: cannot open local X display\n");
        		exit(100);
      	}
    
      	root = DefaultRootWindow(display);
    
    	time_t now = time(0);
    	time_t start = time(0);
    	XQueryPointer(display, root, &fromroot, &tmpwin, &prev_x, &prev_y, &tmp, &tmp, &tmp2);
    	XQueryKeymap(display, prevKeymap);
    	bool brightnessChanged = false;
    	while(1){
    		now = time(0);
    		XQueryPointer(display, root, &fromroot, &tmpwin, &x, &y, &tmp, &tmp, &tmp2);
    		if(x!=prev_x || y!=prev_y || hasTyped(display)){
    			prev_x = x;
    			prev_y = y;
    			printf("MOVED!\n");
    			start = time(0);
    			if(brightnessChanged){
    				brightnessChanged = false;
    				setBrightness(defaultBrightness);
    			}
    		}
    		if(now - start > 10){
    			setBrightness(minimumBrightness);
    			brightnessChanged = true;
    		}
    		//printf("%i %i %i\n",x,y, now - start);
    		usleep(50000);
    	}
    	return EXIT_SUCCESS;
    }

  4. #4
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    You should probably avoid using XQuery functions; listening is better than asking, I think:
    Code:
    /* In main */
    XEvent ev;
    Window root;
    Display *dsp;
    
    /* Other setup stuff here */
    
    XSelectInput(dsp, root, ButtonPressMask);
    for(;;) {
       XNextEvent(dsp, &ev);
       if(ev.type == ButtonPress) {
             /* Do your thing */
       }
    }
    Flies of a particular kind, i.e. time-flies, are fond of an arrow.

    Registered Linux User #408794

Posting Permissions

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