L.S.

I've been using gii as I've said before and I've made the attached
program based on libgii/demos/demo.c (btw, licence = PD, but text says
(c)!). demo.c has a few problems regarding legibility/understanding
what's going on (if one hasn't read the source) so I changed some of the
output in my program. Also, the device info code doesn't work properly.
With a spaceorb e.g. it doesn't get executed (prehaps if one presses the
buttons very rapidly..) as can be expected...

See the way I handle this in giitest.c (done also with
giiQueryDeviceInfoByNumber just to experiment).

What's missing in GII is that one can't detach inputs. This gives
problems with using e.g. spaceorb and a mouse on the same serial port
alternately in the same program or using many devices, some maybe just
temporary (e.g. USB) if one wants to join inputs. Joining is essentially
not an option under many circumstances (and the use of Joining is less
resources/better efficiency, right? So then this is bad).

So, there should be a call giiDetach(giiInput input, int device_no);
(device_no = event origin).

Wouter
/*
 * giitest.c - by W.H. Scholten 1999 (based on libgii/demo.c)
 *
 * This is a demonstration of LibGII's functions and can be used as a
 * reference programming example.
 *
 *   This software is placed in the public domain and can be used freely
 *   for any purpose. It comes without any kind of warranty, either
 *   expressed or implied, including, but not limited to the implied
 *   warranties of merchantability or fitness for a particular purpose.
 *   Use it at your own risk. the author is not responsible for any damage
 *   or consequences raised by use or inability to use this program.
 */

/* Include the necessary headers used for e.g. error-reporting.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <setjmp.h>

/* Include the LibGII declarations.
 */
#include <ggi/gii.h>

sigjmp_buf sigjmp_env;
//static volatile sig_atomic_t interrupted = 0;
void sigquit(int signum) {
//      interrupted = 1;
        siglongjmp(sigjmp_env, 1);
}

int  device_no = -1;
void test_showevent(gii_event *ev)
{
        int y;

        printf("event origin: %8x ",ev->any.origin);
        if (device_no == -1) {
                device_no = ev->any.origin;
        }

        printf("event type: %d\n",ev->any.type);
        switch(ev->any.type)
        {
                case evKeyPress:
                        printf("KeyPress    "); goto showkey;
                case evKeyRepeat:
                        printf("KeyRepeat   "); goto showkey;
                case evKeyRelease:
                        printf("KeyRelease  ");
                        showkey:
                        printf("sym=%4x label=%4x button=%4x\n",
                                ev->key.sym,ev->key.label,ev->key.button);
                        break;
                case evValAbsolute:
                        printf("ValAbsolute ");goto showval;
                case evValRelative:
                        printf("ValRelative ");
                        showval:
                        for(y=0;y<ev->val.count;y++)
                                printf("[%2d]=%d",ev->val.first+y,ev->val.value[y]);
                        printf("\n");
                        break;
                case evPtrAbsolute:
                        printf("PtrAbsolute "); goto showptr;
                case evPtrRelative:
                        printf("PtrRelative ");
                        showptr:
                        printf("x=%4x y=%4x z=%4x w=%4x\n",
                                ev->pmove.x,ev->pmove.y,ev->pmove.z,ev->pmove.wheel);
                        break;
                case evCommand:
                        printf("evCommand\n"); goto hexdump;
                case evInformation:
                        printf("evInformation\n"); goto hexdump;
                case evExpose:
                        printf("evExpose\n"); goto hexdump;

                default:
                        printf("Unknown event type\n");
                  hexdump:
                        for(y=0;y<ev->any.size;y++)
                                printf("%02x  ",((unsigned char *)ev)[y]);
                        printf("\n");
        }
}

void test_gii_poll(gii_input_t inp, struct timeval *tval)
{
        gii_event_mask  mask;
        gii_event       event;

        mask=giiEventPoll(inp,emAll,tval);
        if (tval != NULL)
                printf("0x%08x=giiEventPoll(%p, emAll,&{%d,%d});\n", mask, inp,
                           (int)tval->tv_sec,(int)tval->tv_usec);
        else
                printf("0x%08x=giiEventPoll(%p, emAll, NULL);\n", mask, inp);

        if (mask)
        {
                printf("0x%08x ",mask);
                giiEventRead(inp,&event,emAll);
                test_showevent(&event);
        }
}

/* The main routine.
 * It will just open a gii_input and do some basic tests.
 */
int main(int argc, char **argv)
{
//      struct timeval tval;

        gii_input_t inp;
        gii_cmddata_getdevinfo devinfo;

        char *my_input;

        if (argc <2) {
                fprintf(stderr, "\nUse:\n  giitest <device>\nwhere device is e.g. 
input-linux-joy or input-spaceorb\n\n");
                exit(1);
        }
        my_input = argv[1];

        /* Initialize the GII library. This must be called before any other
         * GII function.
         */
        if (giiInit() != 0) {
                fprintf(stderr, "%s: unable to initialize libgii, exiting.\n",
                        argv[0]);
                exit(1);
        }

        /* Open input.
         */
        printf("Doing test of %s\n", my_input);
        if ((inp=giiOpen(my_input, NULL)) == NULL) {
                fprintf(stderr, "%s: unable to open %s, exiting.\n", argv[0], 
my_input);
                giiClose(inp);
                giiExit();
                exit(1);
        }

        if (sigsetjmp(sigjmp_env, 0)) goto exit_now;
        signal(SIGINT, sigquit);
        printf("Press Ctrl-C to quit\n");

        {
        /* Alternate way to get my_device_no */
        int my_device_no = -1;
        giiQueryDeviceInfoByNumber(inp, 0, &my_device_no, &devinfo);
        printf("my_device_no = 0x%08x\n", my_device_no);
        printf("%s (%s):\n  can generate: %x\n  num buttons: %x\n  num axes: %x\n",
                           devinfo.longname, devinfo.shortname,
                           devinfo.can_generate,
                           devinfo.num_buttons, devinfo.num_axes
                        );
        }

        for(;;)
        {
//              tval.tv_sec = 0;
//              tval.tv_usec = 1000000;
//              test_gii_poll(inp, &tval);
                test_gii_poll(inp, NULL);
//              if (interrupted) break;
        }

  exit_now:
        if (device_no > -1) {
                printf("giiQueryDeviceInfo returned: %d\n", giiQueryDeviceInfo(inp, 
device_no, &devinfo));
                printf("%s (%s):\n  can generate: %x\n  num buttons: %x\n  num axes: 
%x\n",
                           devinfo.longname, devinfo.shortname,
                           devinfo.can_generate,
                           devinfo.num_buttons, devinfo.num_axes
                        );
        }

        giiClose(inp);

        /* Now close down LibGII. */
        giiExit();

        /* Terminate the program.*/
        return 0;
}

Reply via email to