Tizen 2.0 Release
[framework/uifw/xorg/util/x11-xserver-utils.git] / xsetmode / xsetmode.c
1 /* $XFree86: xc/programs/xsetmode/xsetmode.c,v 3.5tsi Exp $ */
2
3 /*
4  * Copyright 1995 by Frederic Lepied, France. <fred@sugix.frmug.fr.net>       
5  *                                                                            
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is  hereby granted without fee, provided that
8  * the  above copyright   notice appear  in   all  copies and  that both  that
9  * copyright  notice   and   this  permission   notice  appear  in  supporting
10  * documentation, and that   the  name of  Frederic   Lepied not  be  used  in
11  * advertising or publicity pertaining to distribution of the software without
12  * specific,  written      prior  permission.     Frederic  Lepied   makes  no
13  * representations about the suitability of this software for any purpose.  It
14  * is provided "as is" without express or implied warranty.                   
15  *                                                                            
16  * FREDERIC  LEPIED DISCLAIMS ALL   WARRANTIES WITH REGARD  TO  THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED   WARRANTIES OF MERCHANTABILITY  AND   FITNESS, IN NO
18  * EVENT  SHALL FREDERIC  LEPIED BE   LIABLE   FOR ANY  SPECIAL, INDIRECT   OR
19  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20  * DATA  OR PROFITS, WHETHER  IN  AN ACTION OF  CONTRACT,  NEGLIGENCE OR OTHER
21  * TORTIOUS  ACTION, ARISING    OUT OF OR   IN  CONNECTION  WITH THE USE    OR
22  * PERFORMANCE OF THIS SOFTWARE.
23  *
24  */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <X11/Xproto.h>
31 #include <X11/extensions/XInput.h>
32
33 int           event_type;
34
35 static int StrCaseCmp(char *s1, char *s2)
36 {
37         char c1, c2;
38
39         if (*s1 == 0) {
40                 if (*s2 == 0)
41                         return(0);
42                 else
43                         return(1);
44         }
45         c1 = (isupper(*s1) ? tolower(*s1) : *s1);
46         c2 = (isupper(*s2) ? tolower(*s2) : *s2);
47         while (c1 == c2)
48         {
49                 if (c1 == '\0')
50                         return(0);
51                 s1++; s2++;
52                 c1 = (isupper(*s1) ? tolower(*s1) : *s1);
53                 c2 = (isupper(*s2) ? tolower(*s2) : *s2);
54         }
55         return(c1 - c2);
56 }
57
58 int
59 main(int argc, char * argv[])
60 {
61   int           loop, num_extensions, num_devices;
62   char          **extensions;
63   XDeviceInfo   *devices;
64   Display       *dpy;
65   int           list = 0;
66   
67   if (argc != 3) {
68     fprintf(stderr, "usage : %s <device name> (ABSOLUTE|RELATIVE)\n", argv[0]);
69     exit(1);
70   }
71
72   if (strcmp(argv[1], "-l") == 0) {
73     list = 1;
74   }
75   
76   dpy = XOpenDisplay(NULL);
77
78   if (!dpy) {
79     printf("unable to connect to X Server try to set the DISPLAY variable\n");
80     exit(1);
81   }
82
83 #ifdef DEBUG
84   printf("connected to %s\n", XDisplayString(dpy));
85 #endif
86
87   extensions = XListExtensions(dpy, &num_extensions);
88   for (loop = 0; loop < num_extensions &&
89          (strcmp(extensions[loop], "XInputExtension") != 0); loop++);
90   XFreeExtensionList(extensions);
91   if (loop != num_extensions)
92     {
93       devices = XListInputDevices(dpy, &num_devices);
94       for(loop=0; loop<num_devices; loop++)
95         {
96           if (devices[loop].name &&
97               (StrCaseCmp(devices[loop].name, argv[1]) == 0))
98             if (devices[loop].use == IsXExtensionDevice)
99               {
100                 XDevice *device;
101               
102 #ifdef DEBUG
103                 fprintf(stderr, "opening device %s\n",
104                         devices[loop].name ? devices[loop].name : "<noname>");
105 #endif
106                 device = XOpenDevice(dpy, devices[loop].id);
107                 if (device)
108                   {
109                     XSetDeviceMode(dpy, device, (strcmp("ABSOLUTE", argv[2]) == 0) ? Absolute
110                                    : Relative);
111                     exit(0);
112                   }
113                 else
114                   {
115                     fprintf(stderr, "error opening device\n");
116                     exit(1);
117                   }
118               }
119         }
120       XFreeDeviceList(devices);
121     }
122   else
123     {
124       fprintf(stderr, "No XInput extension available\n");
125       exit(1);
126     }
127   
128   if (list) {
129     exit(0);
130   }
131   else {
132     fprintf(stderr, "Extended device %s not found\n", argv[1]);
133     exit(1);
134   }
135 }