~ubuntu-branches/ubuntu/karmic/x11-xserver-utils/karmic

« back to all changes in this revision

Viewing changes to xmodmap/xmodmap.c

  • Committer: Bazaar Package Importer
  • Author(s): Brice Goglin, Julien Cristau, Brice Goglin
  • Date: 2007-08-17 09:58:34 UTC
  • Revision ID: james.westby@ubuntu.com-20070817095834-ywge2nyzj1s3rqnd
Tags: 7.3+1
[ Julien Cristau ]
* iceauth 1.0.2.
  + removes blank line in the manpage (closes: #25285).
* xmodmap 1.0.3.
  + manpage updated to state that -pm is the default (closes: #236198)
* xgamma 1.0.2.
  + the manpage now explains how to print the gamma value more clearly
    (closes: #296021).
* xsetroot 1.0.2.
* xrdb 1.0.4.
  + fixes manpage typo (closes: #276286).
* Add upstream URL to debian/copyright, and update it from xgamma's COPYING
  file.

[ Brice Goglin ]
* Add menu entries for xrefresh and xvidtune.
* sessreg 1.0.3.
* xset 1.0.3.
* Add myself to Uploaders, and remove Branden with his permission.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Xorg: xmodmap.c,v 1.4 2001/02/09 02:05:56 xorgcvs Exp $ */
 
2
/*
 
3
 
 
4
Copyright 1988, 1998  The Open Group
 
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.
 
11
 
 
12
The above copyright notice and this permission notice shall be included
 
13
in all copies or substantial portions of the Software.
 
14
 
 
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 
16
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
17
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 
18
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
 
19
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 
20
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 
21
OTHER DEALINGS IN THE SOFTWARE.
 
22
 
 
23
Except as contained in this notice, the name of The Open Group shall
 
24
not be used in advertising or otherwise to promote the sale, use or
 
25
other dealings in this Software without prior written authorization
 
26
from The Open Group.
 
27
 
 
28
*/
 
29
/* $XFree86: xc/programs/xmodmap/xmodmap.c,v 1.8tsi Exp $ */
 
30
 
 
31
#include <X11/Xos.h>
 
32
#include <X11/Xlib.h>
 
33
#include <stdio.h>
 
34
#include <stdlib.h>
 
35
#include <ctype.h>
 
36
#include "xmodmap.h"
 
37
 
 
38
const char *ProgramName;
 
39
Display *dpy = NULL;
 
40
int min_keycode, max_keycode;
 
41
Bool verbose = False;
 
42
Bool dontExecute = False;
 
43
 
 
44
static void 
 
45
Exit(int status)
 
46
{
 
47
    if (dpy) {
 
48
        XCloseDisplay (dpy);
 
49
        dpy = NULL;
 
50
    }
 
51
    exit (status);
 
52
}
 
53
 
 
54
void *
 
55
chk_malloc(size_t n_bytes)
 
56
{
 
57
    void *buf = malloc(n_bytes);
 
58
    if (!buf) {
 
59
        fprintf(stderr, "%s: Could not allocate %d bytes\n", ProgramName, (int)n_bytes);
 
60
        Exit(-1);
 
61
    }
 
62
    return buf;
 
63
}
 
64
 
 
65
static const char help_message[] = 
 
66
"\nwhere options include:\n"
 
67
"    -display host:dpy            X server to use\n"
 
68
"    -verbose, -quiet             turn logging on or off\n"
 
69
"    -n                           don't execute changes, just show like make\n"
 
70
"    -e expression                execute string\n"
 
71
"    -pm                          print modifier map\n"
 
72
"    -pk                          print keymap table\n"
 
73
"    -pke                         print keymap table as expressions\n"
 
74
"    -pp                          print pointer map\n"
 
75
"    -grammar                     print out short help on allowable input\n"
 
76
"    -                            read standard input\n"
 
77
"\n";
 
78
 
 
79
 
 
80
static void 
 
81
usage(void)
 
82
{
 
83
    fprintf (stderr, "usage:  %s [-options ...] [filename]\n", ProgramName);
 
84
    fprintf (stderr, "%s\n", help_message);
 
85
    Exit (1);
 
86
}
 
87
 
 
88
static const char grammar_message[] = 
 
89
"    pointer = default              reset pointer buttons to default\n"
 
90
"    pointer = NUMBER ...           set pointer button codes\n"
 
91
"    keycode NUMBER = [KEYSYM ...]  map keycode to given keysyms\n"
 
92
"    keysym KEYSYM = [KEYSYM ...]   look up keysym and do a keycode operation\n"
 
93
"    clear MODIFIER                 remove all keys for this modifier\n"
 
94
"    add MODIFIER = KEYSYM ...      add the keysyms to the modifier\n"
 
95
"    remove MODIFIER = KEYSYM ...   remove the keysyms from the modifier\n"
 
96
"\n"
 
97
"where NUMBER is a decimal, octal, or hex constant; KEYSYM is a valid\n"
 
98
"Key Symbol name; and MODIFIER is one of the eight modifier names:  Shift,\n"
 
99
"Lock, Control, Mod1, Mod2, Mod3, Mod4, or Mod5.  Lines beginning with\n"
 
100
"an exclamation mark (!) are taken as comments.  Case is significant except\n"
 
101
"for MODIFIER names.\n"
 
102
"\n"
 
103
"Keysyms on the left hand side of the = sign are looked up before any changes\n"
 
104
"are made; keysyms on the right are looked up after all of those on the left\n"
 
105
"have been resolved.  This makes it possible to swap modifier keys.\n"
 
106
"\n";
 
107
 
 
108
 
 
109
static void 
 
110
grammar_usage(void)
 
111
{
 
112
    fprintf (stderr, "%s accepts the following input expressions:\n\n",
 
113
             ProgramName);
 
114
    fprintf (stderr, "%s\n", grammar_message);
 
115
    Exit (0);
 
116
}
 
117
 
 
118
int parse_errors = 0;
 
119
 
 
120
int
 
121
main(int argc, char *argv[])
 
122
{
 
123
    int i;
 
124
    char *displayname = NULL;
 
125
    int status;
 
126
    Bool printMap = False;
 
127
    Bool printKeyTable = False;
 
128
    Bool printKeyTableExprs = False;
 
129
    Bool printPointerMap = False;
 
130
    Bool didAnything = False;
 
131
 
 
132
    ProgramName = argv[0];
 
133
 
 
134
    /*
 
135
     * scan the arg list once to find out which display to use
 
136
     */
 
137
 
 
138
    for (i = 1; i < argc; i++) {
 
139
        if (strncmp (argv[i], "-d", 2) == 0) {
 
140
            if (++i >= argc) usage ();
 
141
            displayname = argv[i];
 
142
        }
 
143
    }
 
144
 
 
145
    dpy = XOpenDisplay (displayname);
 
146
    if (!dpy) {
 
147
        fprintf (stderr, "%s:  unable to open display '%s'\n",
 
148
                 ProgramName, XDisplayName (displayname));
 
149
        Exit (1);
 
150
    }
 
151
 
 
152
    XDisplayKeycodes (dpy, &min_keycode, &max_keycode);
 
153
 
 
154
    initialize_map ();
 
155
 
 
156
    /*
 
157
     * scan the arg list again to do the actual work (since it requires
 
158
     * the display being open.
 
159
     */
 
160
 
 
161
    status = 0;
 
162
    for (i = 1; i < argc; i++) {
 
163
        char *arg = argv[i];
 
164
 
 
165
        if (arg[0] == '-') {
 
166
            switch (arg[1]) {
 
167
              case 'd':                 /* -display host:dpy */
 
168
                ++i;                    /* handled above */
 
169
                continue;
 
170
              case 'v':                 /* -verbose */
 
171
                verbose = True;
 
172
                continue;
 
173
              case 'q':                 /* -quiet */
 
174
                verbose = False;
 
175
                continue;
 
176
              case 'n':                 /* -n (like make) */
 
177
                dontExecute = True;
 
178
                continue;
 
179
              case 'e':                 /* -e expression */
 
180
                didAnything = True;
 
181
                if (++i >= argc) usage ();
 
182
                process_line (argv[i]);
 
183
                continue;
 
184
              case 'p':                 /* -p... */
 
185
                switch (arg[2]) {
 
186
                  case '\0':
 
187
                  case 'm':             /* -pm */
 
188
                    printMap = True;
 
189
                    break;
 
190
                  case 'k':             /* -pk, -pke */
 
191
                    switch (arg[3]) {
 
192
                    case '\0':
 
193
                        printKeyTable = True;
 
194
                        break;
 
195
                    case 'e':
 
196
                        printKeyTableExprs = True;
 
197
                        break;
 
198
                    default:
 
199
                        usage ();
 
200
                    }
 
201
                    break;
 
202
                  case 'p':             /* -pp */
 
203
                    printPointerMap = True;
 
204
                    break;
 
205
                  default:
 
206
                    usage ();
 
207
                    /* NOTREACHED */
 
208
                }
 
209
                didAnything = True;
 
210
                continue;
 
211
              case 'g':                 /* -grammar */
 
212
                grammar_usage ();
 
213
                /*NOTREACHED*/
 
214
              case '\0':                /* - (use standard input) */
 
215
                didAnything = True;
 
216
                process_file (NULL);
 
217
                continue;
 
218
 
 
219
              /*
 
220
               * provide old xmodmap args
 
221
               */
 
222
              case 'S':
 
223
                didAnything = True;
 
224
                process_line ("clear shift");
 
225
                continue;
 
226
              case 'L':
 
227
                didAnything = True;
 
228
                process_line ("clear lock");
 
229
                continue;
 
230
              case 'C':
 
231
                didAnything = True;
 
232
                process_line ("clear control");
 
233
                continue;
 
234
              case '1':
 
235
              case '2':
 
236
              case '3':
 
237
              case '4':
 
238
              case '5': {
 
239
                  char cmd[11] = "clear modX";
 
240
                  cmd[9] = arg[1];
 
241
                  process_line (cmd);
 
242
                  continue;
 
243
              }
 
244
              case 's':
 
245
              case 'l':
 
246
              case 'c': {
 
247
                  char *cmd;
 
248
                  didAnything = True;
 
249
                  if (++i >= argc) usage ();
 
250
                  cmd = chk_malloc (strlen ("remove control = ") + strlen (argv[i]) + 1);
 
251
                  (void) sprintf (cmd, "remove %s = %s",
 
252
                                  ((arg[1] == 's') ? "shift" :
 
253
                                   ((arg[1] == 'l') ? "lock" :
 
254
                                    "control")), argv[i]);
 
255
                  process_line (cmd);
 
256
                  continue;
 
257
              }
 
258
              default:
 
259
                usage ();
 
260
                /*NOTREACHED*/
 
261
            }
 
262
        } else if (arg[0] == '+') {     /* old xmodmap args */
 
263
            switch (arg[1]) {
 
264
              case '1':
 
265
              case '2':
 
266
              case '3':
 
267
              case '4':
 
268
              case '5': {
 
269
                  char *cmd;
 
270
                  didAnything = True;
 
271
                  if (++i >= argc) usage ();
 
272
                  cmd = chk_malloc (strlen ("add modX = ") + strlen (argv[i]) + 1);
 
273
                  (void) sprintf (cmd, "add mod%c = %s", arg[1], argv[i]);
 
274
                  process_line (cmd);
 
275
                  continue;
 
276
              }
 
277
              case 'S':
 
278
              case 'L':
 
279
              case 'C':
 
280
                arg[1] = tolower (arg[1]);
 
281
                /* fall through to handler below */
 
282
              case 's':
 
283
              case 'l':
 
284
              case 'c': {
 
285
                  char *cmd;
 
286
                  didAnything = True;
 
287
                  if (++i >= argc) usage ();
 
288
                  cmd = chk_malloc (strlen ("add control = ") + strlen (argv[i]) + 1);
 
289
                  (void) sprintf (cmd, "add %s = %s",
 
290
                                  ((arg[1] == 's') ? "shift" :
 
291
                                   ((arg[1] == 'l') ? "lock" :
 
292
                                    "control")), argv[i]);
 
293
                  process_line (cmd);
 
294
                  continue;
 
295
              }
 
296
              default:
 
297
                usage ();
 
298
            }
 
299
        } else {
 
300
            didAnything = True;
 
301
            process_file (arg);
 
302
            continue;
 
303
        }
 
304
    }                                   /* end for loop */
 
305
 
 
306
    /* for compatibility */
 
307
    if (!didAnything) printMap = True;
 
308
 
 
309
    /*
 
310
     * at this point, the work list has been built and we can view it or
 
311
     * execute it
 
312
     */
 
313
 
 
314
    if (dontExecute) {
 
315
        print_work_queue ();
 
316
        Exit (0);
 
317
    }
 
318
 
 
319
    if (parse_errors != 0) {
 
320
        fprintf (stderr, "%s:  %d error%s encountered, aborting.\n",
 
321
                 ProgramName, parse_errors,
 
322
                 (parse_errors == 1 ? "" : "s"));
 
323
        status = -1;    /* return an error condition */
 
324
    } else {
 
325
        status = execute_work_queue ();
 
326
    }
 
327
 
 
328
    if (printMap) {
 
329
        print_modifier_map ();
 
330
    }
 
331
 
 
332
    if (printKeyTable) {
 
333
        print_key_table (False);
 
334
    }
 
335
 
 
336
    if (printKeyTableExprs) {
 
337
        print_key_table (True);
 
338
    }
 
339
 
 
340
    if (printPointerMap) {
 
341
        print_pointer_map ();
 
342
    }
 
343
 
 
344
    Exit (status < 0 ? 1 : 0);
 
345
 
 
346
    /* Muffle gcc */
 
347
    return 0;
 
348
}
 
349