/*
 * AT-SPI - Assistive Technology Service Provider Interface
 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
 *
 * Copyright 2001, 2002 Sun Microsystems Inc.,
 * Copyright 2001, 2002 Ximian, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <spi.h>
#include <glib.h>

#define XK_MISCELLANY
#include <X11/keysymdef.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include <X11/XKBlib.h>

static SPIBoolean report_command_key_event  (const AccessibleKeystroke *stroke, void *user_data);
static AccessibleKeystrokeListener *command_key_listener;

int
main (int argc, char **argv)
{
  SPIBoolean retval = FALSE;
  short key;
  Display *disp;
  AccessibleKeySet    *command_keyset;
    
  SPI_init ();

  gtk_init (&argc, &argv);
  command_key_listener = SPI_createAccessibleKeystrokeListener (report_command_key_event, NULL);
  
  disp = GDK_DISPLAY ();
  key = XKeysymToKeycode (disp, XK_Delete);
  command_keyset = SPI_createAccessibleKeySet (1, NULL, &key, NULL);
  /* register a listener with SPI_KEYMASK_NUMLOCK modifier */
  retval = SPI_registerAccessibleKeystrokeListener (command_key_listener,
			       		  command_keyset,
					  SPI_KEYMASK_NUMLOCK ,
					  (gulong) ( SPI_KEY_PRESSED),
					  SPI_KEYLISTENER_ALL_WINDOWS);
  
  fprintf (stderr, "Command key registry: result %s\n", retval ? "succeeded" : 
	"failed");

    SPI_event_main();
    SPI_exit ();

    return 0;
}

static SPIBoolean
report_command_key_event (const AccessibleKeystroke *key, void *user_data)
{
    static gint cnt = 0;
    fprintf (stderr, "\n SPI_KEYMASK_NUMLOCK: %d", SPI_KEYMASK_NUMLOCK);
    fprintf (stderr, "\n SPI_KEYMASK_MOD2: %d", SPI_KEYMASK_MOD2);
    /* the listener is called with SPI_KEYMASK_MOD2 modifier */
    fprintf (stderr, "\n ----modifiers: %d", key->modifiers);
    fprintf (stderr, "\nL%d KeyEvent %c (keycode %d); string=%s; time=%lx",
	   cnt++,
	  ((key->modifiers & SPI_KEYMASK_SHIFT)^(key->modifiers & SPI_KEYMASK_SHIFTLOCK))?
	  (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID),
	  (int) key->keycode,
   	  key->keystring,
	  (long int) key->timestamp);	


  return TRUE;
}


