191a17321Smrg/* $XFree86: xc/programs/xsetpointer/xsetpointer.c,v 3.7 2003/05/27 22:27:10 tsi Exp $ */
291a17321Smrg
391a17321Smrg/*
491a17321Smrg * Copyright 1995 by Frederic Lepied, France. <fred@sugix.frmug.fr.net>
591a17321Smrg *
691a17321Smrg * Permission to use, copy, modify, distribute, and sell this software and its
791a17321Smrg * documentation for any purpose is  hereby granted without fee, provided that
891a17321Smrg * the  above copyright   notice appear  in   all  copies and  that both  that
991a17321Smrg * copyright  notice   and   this  permission   notice  appear  in  supporting
1091a17321Smrg * documentation, and that   the  name of  Frederic   Lepied not  be  used  in
1191a17321Smrg * advertising or publicity pertaining to distribution of the software without
1291a17321Smrg * specific,  written      prior  permission.     Frederic  Lepied   makes  no
1391a17321Smrg * representations about the suitability of this software for any purpose.  It
1491a17321Smrg * is provided "as is" without express or implied warranty.
1591a17321Smrg *
1691a17321Smrg * FREDERIC  LEPIED DISCLAIMS ALL   WARRANTIES WITH REGARD  TO  THIS SOFTWARE,
1791a17321Smrg * INCLUDING ALL IMPLIED   WARRANTIES OF MERCHANTABILITY  AND   FITNESS, IN NO
1891a17321Smrg * EVENT  SHALL FREDERIC  LEPIED BE   LIABLE   FOR ANY  SPECIAL, INDIRECT   OR
1991a17321Smrg * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
2091a17321Smrg * DATA  OR PROFITS, WHETHER  IN  AN ACTION OF  CONTRACT,  NEGLIGENCE OR OTHER
2191a17321Smrg * TORTIOUS  ACTION, ARISING    OUT OF OR   IN  CONNECTION  WITH THE USE    OR
2291a17321Smrg * PERFORMANCE OF THIS SOFTWARE.
2391a17321Smrg *
2491a17321Smrg */
2591a17321Smrg
2691a17321Smrg#include <stdio.h>
2791a17321Smrg#include <stdlib.h>
2891a17321Smrg#include <string.h>
2991a17321Smrg#include <ctype.h>
3091a17321Smrg#include <X11/Xproto.h>
3191a17321Smrg#include <X11/extensions/XInput.h>
3291a17321Smrg
3391a17321Smrgint           event_type;
3491a17321Smrg
3591a17321Smrgstatic int
3691a17321SmrgStrCaseCmp(char *s1, char *s2)
3791a17321Smrg{
3891a17321Smrg	char c1, c2;
3991a17321Smrg
4091a17321Smrg	if (*s1 == 0) {
4191a17321Smrg		if (*s2 == 0)
4291a17321Smrg			return(0);
4391a17321Smrg		else
4491a17321Smrg			return(1);
4591a17321Smrg	}
4691a17321Smrg	c1 = (isupper(*s1) ? tolower(*s1) : *s1);
4791a17321Smrg	c2 = (isupper(*s2) ? tolower(*s2) : *s2);
4891a17321Smrg	while (c1 == c2)
4991a17321Smrg	{
5091a17321Smrg		if (c1 == '\0')
5191a17321Smrg			return(0);
5291a17321Smrg		s1++; s2++;
5391a17321Smrg		c1 = (isupper(*s1) ? tolower(*s1) : *s1);
5491a17321Smrg		c2 = (isupper(*s2) ? tolower(*s2) : *s2);
5591a17321Smrg	}
5691a17321Smrg	return(c1 - c2);
5791a17321Smrg}
5891a17321Smrg
5991a17321Smrgint
6091a17321Smrgmain(int argc, char * argv[])
6191a17321Smrg{
6291a17321Smrg  int                loop, num_extensions, num_devices;
6391a17321Smrg  char               **extensions;
6491a17321Smrg  XDeviceInfo        *devices;
6591a17321Smrg  XDeviceCoreControl corectl;
6691a17321Smrg  Display            *dpy;
6791a17321Smrg  int		     list = 0, core = 0;
6891a17321Smrg  XDevice            *device;
6991a17321Smrg
7091a17321Smrg  if (argc < 2 || argc > 3) {
7191a17321Smrg    fprintf(stderr, "usage : %s (-l | -c | +c ) <device name>)\n", argv[0]);
7291a17321Smrg    exit(1);
7391a17321Smrg  }
7491a17321Smrg
7591a17321Smrg  if (strcmp(argv[1], "-l") == 0) {
7691a17321Smrg    list = 1;
7791a17321Smrg  }
7891a17321Smrg  else if (strcmp(argv[1], "-c") == 0) {
7991a17321Smrg    core = 1;
8091a17321Smrg  }
8191a17321Smrg  else if (strcmp(argv[1], "+c") == 0) {
8291a17321Smrg    core = 2;
8391a17321Smrg  }
8491a17321Smrg
8591a17321Smrg  dpy = XOpenDisplay(NULL);
8691a17321Smrg
8791a17321Smrg  if (!dpy) {
8891a17321Smrg    printf("unable to connect to X Server try to set the DISPLAY variable\n");
8991a17321Smrg    exit(1);
9091a17321Smrg  }
9191a17321Smrg
9291a17321Smrg#ifdef DEBUG
9391a17321Smrg  printf("connected to %s\n", XDisplayString(dpy));
9491a17321Smrg#endif
9591a17321Smrg
9691a17321Smrg  extensions = XListExtensions(dpy, &num_extensions);
9791a17321Smrg  for (loop = 0; loop < num_extensions &&
9891a17321Smrg         (strcmp(extensions[loop], "XInputExtension") != 0); loop++);
9991a17321Smrg  XFreeExtensionList(extensions);
10091a17321Smrg  if (loop != num_extensions)
10191a17321Smrg    {
10291a17321Smrg      devices = XListInputDevices(dpy, &num_devices);
10391a17321Smrg      for(loop=0; loop<num_devices; loop++)
10491a17321Smrg        {
10591a17321Smrg	  if (list) {
106218c2a7cSwiz	      printf("%d: \"%s\"	[", (int)devices[loop].id, devices[loop].name ? devices[loop].name : "<noname>");
10791a17321Smrg	      switch(devices[loop].use) {
10891a17321Smrg	      case IsXPointer:
10991a17321Smrg		  printf("XPointer]\n");
11091a17321Smrg		  break;
11191a17321Smrg	      case IsXKeyboard:
11291a17321Smrg		  printf("XKeyboard]\n");
11391a17321Smrg		  break;
11491a17321Smrg	      case IsXExtensionDevice:
11591a17321Smrg		  printf("XExtensionDevice]\n");
11691a17321Smrg		  break;
11791a17321Smrg              case IsXExtensionKeyboard:
11891a17321Smrg                  printf("XExtensionKeyboard]\n");
11991a17321Smrg                  break;
12091a17321Smrg              case IsXExtensionPointer:
12191a17321Smrg                  printf("XExtensionPointer]\n");
12291a17321Smrg                  break;
12391a17321Smrg	      default:
12491a17321Smrg		  printf("invalid value]\n");
12591a17321Smrg		  break;
12691a17321Smrg	      }
12791a17321Smrg	  }
12891a17321Smrg          else if (core) {
12991a17321Smrg            if (argc == 3 && devices[loop].name &&
13091a17321Smrg                StrCaseCmp(devices[loop].name, argv[2]) == 0) {
13191a17321Smrg#ifdef DEBUG
13291a17321Smrg                fprintf(stderr, "opening device %s at %d\n",
13391a17321Smrg                        devices[loop].name ? devices[loop].name : "<noname>",
13491a17321Smrg                        devices[loop].id);
13591a17321Smrg#endif
13691a17321Smrg              device = XOpenDevice(dpy, devices[loop].id);
13791a17321Smrg              if (device) {
13891a17321Smrg                corectl.status = (core - 1);
13991a17321Smrg                corectl.length = sizeof(corectl);
14091a17321Smrg                corectl.control = DEVICE_CORE;
14191a17321Smrg                XChangeDeviceControl(dpy, device, DEVICE_CORE,
14291a17321Smrg                                     (XDeviceControl *)&corectl);
14391a17321Smrg                exit(0);
14491a17321Smrg              }
14591a17321Smrg              else {
14691a17321Smrg                fprintf(stderr, "error opening device\n");
14791a17321Smrg                exit(1);
14891a17321Smrg              }
14991a17321Smrg            }
15091a17321Smrg          }
15191a17321Smrg	  else {
15291a17321Smrg          if ((argc == 2) && devices[loop].name &&
15391a17321Smrg              (StrCaseCmp(devices[loop].name, argv[1]) == 0))
15491a17321Smrg            if (devices[loop].use == IsXExtensionDevice)
15591a17321Smrg              {
15691a17321Smrg#ifdef DEBUG
15791a17321Smrg                fprintf(stderr, "opening device %s\n",
15891a17321Smrg                        devices[loop].name ? devices[loop].name : "<noname>");
15991a17321Smrg#endif
16091a17321Smrg                device = XOpenDevice(dpy, devices[loop].id);
16191a17321Smrg                if (device)
16291a17321Smrg                  {
16391a17321Smrg                    XChangePointerDevice(dpy, device, 0, 1);
16491a17321Smrg                    exit(0);
16591a17321Smrg                  }
16691a17321Smrg                else
16791a17321Smrg                  {
16891a17321Smrg                    fprintf(stderr, "error opening device\n");
16991a17321Smrg                    exit(1);
17091a17321Smrg                  }
17191a17321Smrg              }
17291a17321Smrg	  }
17391a17321Smrg        }
17491a17321Smrg      XFreeDeviceList(devices);
17591a17321Smrg    }
17691a17321Smrg  else
17791a17321Smrg    {
17891a17321Smrg      fprintf(stderr, "No XInput extension available\n");
17991a17321Smrg      exit(1);
18091a17321Smrg    }
18191a17321Smrg
18291a17321Smrg  if (list) {
18391a17321Smrg    exit(0);
18491a17321Smrg  }
18591a17321Smrg  else {
18691a17321Smrg    fprintf(stderr, "Extended device %s not found\n", core ? argv[2] :
18791a17321Smrg                                                             argv[1]);
18891a17321Smrg    exit(1);
18991a17321Smrg  }
19091a17321Smrg}
191