_Arthur_
Posts: 2
Joined: Sat Mar 17, 2018 11:36 pm

EGL focus window

Sat Mar 17, 2018 11:53 pm

I'm creating a game using an EGL window. I get mouse input from the /dev/input folder on my Pi, however when I click on some UI element in my game I also click on something on the Raspbian stretch desktop. How can I focus the EGL window like on Windows so that all controls used in my game aren't actually performed in the desktop environment.

User avatar
PeterO
Posts: 4183
Joined: Sun Jul 22, 2012 4:14 pm

Re: EGL focus window

Sun Mar 18, 2018 9:18 am

Here's the code from one of my OpenGLES programs that opens the mouse device and sets exclusive access.

Code: Select all

// Find and open input devices in /dev/input/by-id that end in "event-kbd" and "event-mouse"
// These are then set for exclusive access so mouse and keyboard events are not passed to 
// the console ( or X).

                if((mouseFd == -1) && (regexec (&mouse, dp->d_name, 0, NULL, 0) == 0))
                {
                    printf("match for the mouse = %s\n",dp->d_name);
                    sprintf(fullPath,"%s/%s",dirName,dp->d_name);
                    mouseFd = open(fullPath,O_RDONLY | O_NONBLOCK);
                    printf("%s Fd = %d\n",fullPath,mouseFd);

                    printf("Getting exclusive access: ");
                    result = ioctl(mouseFd, EVIOCGRAB, 1);
                    printf("%s\n", (result == 0) ? "SUCCESS" : "FAILURE");

                }
At the end ...

Code: Select all

   ioctl(mouseFd, EVIOCGRAB, 0);
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),Aeromodelling,1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
PeterO
Posts: 4183
Joined: Sun Jul 22, 2012 4:14 pm

Re: EGL focus window

Sun Mar 18, 2018 9:33 am

Another thing to watch for ...

If you try to get exclusive access for the keyboard in a similar way, it is important to have a short pause before doing it.
See the comment in the code below.

Code: Select all

                if((keyboardFd == -1) && (regexec (&kbd, dp->d_name, 0, NULL, 0) == 0))
                {
                    printf("match for the kbd = %s\n",dp->d_name);
                    sprintf(fullPath,"%s/%s",dirName,dp->d_name);
                    keyboardFd = open(fullPath,O_RDONLY | O_NONBLOCK);
                    printf("%s Fd = %d\n",fullPath,keyboardFd);

                    sleep(1); // Give the shell a chance to see the return key up
                    //event before grabbing exclusive access.
                    printf("Getting exclusive access: ");
                    result = ioctl(keyboardFd, EVIOCGRAB, 1);
                    printf("%s\n", (result == 0) ? "SUCCESS" : "FAILURE");
                }
Without "sleep(1)" the shell gets lots of auto repeated return keys as the keyboard driver never see the "key up" event that matches the "key-down" event that started the program.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),Aeromodelling,1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

_Arthur_
Posts: 2
Joined: Sat Mar 17, 2018 11:36 pm

Re: EGL focus window

Sun Mar 18, 2018 6:57 pm

Thanks! that fixed the problem.

Return to “OpenGLES”

Who is online

Users browsing this forum: No registered users and 1 guest