Imported Upstream version 1.15.1
[deb_xorg-server.git] / hw / dmx / examples / xled.c
CommitLineData
a09e091a
JB
1/*
2 * Copyright 2003 Red Hat Inc., Durham, North Carolina.
3 *
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation on the rights to use, copy, modify, merge,
10 * publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NON-INFRINGEMENT. IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28/*
29 * Authors:
30 * Rickard E. (Rik) Faith <faith@redhat.com>
31 *
32 */
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <X11/Xlib.h>
38#include <X11/XKBlib.h>
39#include <X11/extensions/XKB.h>
40#include <X11/extensions/XKBstr.h>
41#include <sys/time.h>
42
43int
44main(int argc, char **argv)
45{
46 Display *display = NULL;
47 int mask = 0;
48 unsigned i;
49 XKeyboardState ks;
50 XKeyboardControl kc;
51 XkbDescPtr xkb;
52 int old[32];
53
54 if (argc == 2 || argc == 3) {
55 if (!(display = XOpenDisplay(argv[1]))) {
56 printf("Cannot open display %s\n", argv[1]);
57 return -1;
58 }
59 if (argc >= 3)
60 mask = strtol(argv[2], NULL, 0);
61 }
62 else {
63 printf("Usage: %s display [mask]\n", argv[0]);
64 return -1;
65 }
66
67 if (!display && !(display = XOpenDisplay(NULL))) {
68 printf("Cannot open default display\n");
69 return -1;
70 }
71
72 if (!(xkb = XkbAllocKeyboard())) {
73 printf("Cannot allocate\n");
74 return -1;
75 }
76 if (XkbGetIndicatorMap(display, XkbAllIndicatorsMask, xkb)) {
77 printf("Cannot Get Indicators\n");
78 return -1;
79 }
80 if (XkbGetNames(display, XkbAllNamesMask, xkb)) {
81 printf("Cannot Get Names\n");
82 return -1;
83 }
84 for (i = 0; i < XkbNumIndicators; i++) {
85 if (xkb->indicators->phys_indicators & (1 << i)) {
86 printf("led %d = %d\n", i, xkb->indicators->maps[i].flags);
87 old[i] = xkb->indicators->maps[i].flags;
88 xkb->indicators->maps[i].flags = XkbIM_NoAutomatic;
89 }
90 }
91 printf("XkbSetIndicatorMap = %d\n", XkbSetIndicatorMap(display, ~0, xkb));
92 XkbFreeKeyboard(xkb, 0, True);
93
94 if (!(xkb = XkbAllocKeyboard())) {
95 printf("Cannot allocate\n");
96 return -1;
97 }
98 if (XkbGetIndicatorMap(display, XkbAllIndicatorsMask, xkb)) {
99 printf("Cannot Get Indicators\n");
100 return -1;
101 }
102 for (i = 0; i < XkbNumIndicators; i++) {
103 if (xkb->indicators->phys_indicators & (1 << i))
104 printf("led %d = %d\n", i, xkb->indicators->maps[i].flags);
105 }
106
107 printf("XGetKeyboardControl = %d\n", XGetKeyboardControl(display, &ks));
108 printf("old mask = 0x%08lx\n", ks.led_mask);
109 for (i = 0; i < 5; i++) {
110 kc.led = i + 1;
111 kc.led_mode = (mask & (1 << i)) ? LedModeOn : LedModeOff;
112 printf("XChangeKeyboardControl = %d\n",
113 XChangeKeyboardControl(display, KBLed | KBLedMode, &kc));
114 }
115 printf("XGetKeyboardControl = %d\n", XGetKeyboardControl(display, &ks));
116 printf("new mask = 0x%08lx\n", ks.led_mask);
117
118 for (i = 0; i < XkbNumIndicators; i++)
119 if (xkb->indicators->phys_indicators & (i << 1))
120 xkb->indicators->maps[i].flags = old[i];
121 printf("XkbSetIndicatorMap = %d\n", XkbSetIndicatorMap(display, ~0, xkb));
122
123 XkbFreeKeyboard(xkb, 0, True);
124 XCloseDisplay(display);
125 return 0;
126}