I am having some trouble with GGI and event handling under the X target,
that seems to be related to requiring some form of mutal exclusion
between ggiEventRead() and ggiSetMode(). The test code I am using is as
follows:
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <ggi/ggi.h>
ggi_visual_t vis;
void *modeChanger(void *);
void *eventHandler(void *);
pthread_mutex_t eventMutex;
int main(int argc, char *argv[])
{
ggi_mode mode;
pthread_t thread;
pthread_t thread1;
giiMTInit();
if (ggiInit())
{
fprintf(stderr, "Couldn't initialize GGI\n");
return 1;
}
vis = ggiOpen(NULL);
if (vis == NULL)
{
fprintf( stderr, "Couldn't open default visual!\n");
ggiExit();
return 1;
}
ggiSetFlags(vis, GGIFLAG_ASYNC);
mode.frames = 1;
mode.visible.x = GGI_AUTO;
mode.visible.y = GGI_AUTO;
mode.virt.x = 200;
mode.virt.y = 400;
mode.graphtype = GT_AUTO;
mode.dpp.x = GGI_AUTO;
mode.dpp.y = GGI_AUTO;
ggiSetMode(vis, &mode);
pthread_mutex_init(&eventMutex, NULL);
pthread_create(&thread, NULL, eventHandler, NULL);
pthread_create(&thread1, NULL, modeChanger, NULL);
while (1)
;
}
void *modeChanger(void *notused)
{
ggi_mode mode;
while (1)
{
ggiGetMode(vis, &mode);
//pthread_mutex_lock(&eventMutex);
if (ggiSetMode(vis, &mode))
{
fprintf(stderr, "Couldn't set mode\n");
ggiExit();
}
//pthread_mutex_unlock(&eventMutex);
}
}
void *eventHandler(void *notused)
{
ggi_event event;
while (1)
{
//pthread_mutex_lock(&eventMutex);
ggiEventRead(vis, &event, emAll);
if (event.any.type == evKeyPress)
printf("Key Event...\n");
//pthread_mutex_unlock(&eventMutex);
printf("Event...\n");
}
}
Running this with the pthread_mutex calls commented out yields the
following (with different sequence codes each time):
zhenya@scott:~/test> ./ggitest2
Event...
Event...
Xlib: unexpected async reply (sequence 0x37)!
Event...
Event...
Key Event...
If I uncomment the pthread_mutex calls, I get:
zhenya@scott:~/test> ./ggitest2
Event...
Event...
Event...
Key Event...
Event...
Note that without mutexes, if the window loses focus and then regains
focus, key events are no longer received whereas mouse moves still work.
I have tried building GII with both --enable-mutexes=pthread and
--enable-mutexes=dynpthread. GGI has the mansync target compiled to use
pthreads. I am using the 20000601 snapshot (the same behaviour is
exhibited by the 2.0b2.1 beta). I am using glibc-2.1.3/pthreads-0.8 and
have also tried glibc-2.1.2.
Has anyone else seen the same thing, or is it just me? :)
Zhenya.