Page 1 of 1

EGL focus window

Posted: Sat Mar 17, 2018 11:53 pm
by _Arthur_
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.

Re: EGL focus window

Posted: Sun Mar 18, 2018 9:18 am
by PeterO
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

Re: EGL focus window

Posted: Sun Mar 18, 2018 9:33 am
by PeterO
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

Re: EGL focus window

Posted: Sun Mar 18, 2018 6:57 pm
by _Arthur_
Thanks! that fixed the problem.