~ubuntu-branches/ubuntu/gutsy/vnc4/gutsy

« back to all changes in this revision

Viewing changes to unix/xc/programs/xrandr/xrandr.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2006-05-15 20:35:17 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060515203517-l4lre1ku942mn26k
Tags: 4.1.1+X4.3.0-10
* Correction of critical security issue. Thanks to Martin Kogler
  <e9925248@student.tuwien.ac.at> that informed me about the issue,
  and provided the patch.
  This flaw was originally found by Steve Wiseman of intelliadmin.com.
* Applied patch from Javier Kohen <jkohen@users.sourceforge.net> that
  inform the user that only 8 first characters of the password will
  actually be used when typing more than 8 characters, closes:
  #355619.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $XFree86: xc/programs/xrandr/xrandr.c,v 1.12 2002/11/01 20:04:00 keithp Exp $
 
3
 *
 
4
 * Copyright � 2001 Keith Packard, member of The XFree86 Project, Inc.
 
5
 * Copyright � 2002 Hewlett Pacard Company, Inc.
 
6
 *
 
7
 * Permission to use, copy, modify, distribute, and sell this software and its
 
8
 * documentation for any purpose is hereby granted without fee, provided that
 
9
 * the above copyright notice appear in all copies and that both that
 
10
 * copyright notice and this permission notice appear in supporting
 
11
 * documentation, and that the name of Keith Packard or HP not be used in
 
12
 * advertising or publicity pertaining to distribution of the software without
 
13
 * specific, written prior permission.  Keith Packard and HP makes no
 
14
 * representations about the suitability of this software for any purpose.  It
 
15
 * is provided "as is" without express or implied warranty.
 
16
 *
 
17
 * KEITH PACKARD and HP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 
18
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 
19
 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 
20
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 
21
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 
22
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 
23
 * PERFORMANCE OF THIS SOFTWARE.
 
24
 *
 
25
 * Blame Jim Gettys for any bugs; he wrote most of the client side code,
 
26
 * and part of the server code for randr.
 
27
 */
 
28
 
 
29
#include <stdio.h>
 
30
#include <X11/Xlib.h>
 
31
#include <X11/Xlibint.h>
 
32
#include <X11/Xproto.h>
 
33
#include <X11/extensions/Xrandr.h>
 
34
#include <X11/extensions/Xrender.h>     /* we share subpixel information */
 
35
#include <string.h>
 
36
#include <stdlib.h>
 
37
 
 
38
static char *program_name;
 
39
 
 
40
static char *direction[5] = {
 
41
  "normal", 
 
42
  "left", 
 
43
  "inverted", 
 
44
  "right",
 
45
  "\n"};
 
46
 
 
47
/* subpixel order */
 
48
static char *order[6] = {
 
49
  "unknown",
 
50
  "horizontal rgb",
 
51
  "horizontal bgr",
 
52
  "vertical rgb",
 
53
  "vertical bgr",
 
54
  "no subpixels"};
 
55
 
 
56
 
 
57
static void
 
58
usage(void)
 
59
{
 
60
  fprintf(stderr, "usage: %s [options]\n", program_name);
 
61
  fprintf(stderr, "  where options are:\n");
 
62
  fprintf(stderr, "  -display <display> or -d <display>\n");
 
63
  fprintf(stderr, "  -help\n");
 
64
  fprintf(stderr, "  -o <normal,inverted,left,right,0,1,2,3>\n");
 
65
  fprintf(stderr, "            or --orientation <normal,inverted,left,right,0,1,2,3>\n");
 
66
  fprintf(stderr, "  -q        or --query\n");
 
67
  fprintf(stderr, "  -s <size>/<width>x<height> or --size <size>/<width>x<height>\n");
 
68
  fprintf(stderr, "  -r <rate> or --rate <rate>\n");
 
69
  fprintf(stderr, "  -v        or --version\n");
 
70
  fprintf(stderr, "  -x        (reflect in x)\n");
 
71
  fprintf(stderr, "  -y        (reflect in y)\n");
 
72
  fprintf(stderr, "  --screen <screen>\n");
 
73
  fprintf(stderr, "  --verbose\n");
 
74
  
 
75
  exit(1);
 
76
  /*NOTREACHED*/
 
77
}
 
78
 
 
79
int
 
80
main (int argc, char **argv)
 
81
{
 
82
  Display       *dpy;
 
83
  XRRScreenSize *sizes;
 
84
  XRRScreenConfiguration *sc;
 
85
  int           nsize;
 
86
  int           nrate;
 
87
  short         *rates;
 
88
  Window        root;
 
89
  Status        status = RRSetConfigFailed;
 
90
  int           rot = -1;
 
91
  int           verbose = 0, query = 0;
 
92
  Rotation      rotation, current_rotation, rotations;
 
93
  XEvent        event;
 
94
  XRRScreenChangeNotifyEvent *sce;    
 
95
  char          *display_name = NULL;
 
96
  int           i, j;
 
97
  SizeID        current_size;
 
98
  short         current_rate;
 
99
  int           rate = -1;
 
100
  int           size = -1;
 
101
  int           dirind = 0;
 
102
  int           setit = 0;
 
103
  int           screen = -1;
 
104
  int           version = 0;
 
105
  int           event_base, error_base;
 
106
  int           reflection = 0;
 
107
  int           width = 0, height = 0;
 
108
  int           have_pixel_size = 0;
 
109
 
 
110
  program_name = argv[0];
 
111
  if (argc == 1) query = 1;
 
112
  for (i = 1; i < argc; i++) {
 
113
    if (!strcmp ("-display", argv[i]) || !strcmp ("-d", argv[i])) {
 
114
      if (++i>=argc) usage ();
 
115
      display_name = argv[i];
 
116
      continue;
 
117
    }
 
118
    if (!strcmp("-help", argv[i])) {
 
119
      usage();
 
120
      continue;
 
121
    }
 
122
    if (!strcmp ("--verbose", argv[i])) {
 
123
      verbose = 1;
 
124
      continue;
 
125
    }
 
126
 
 
127
    if (!strcmp ("-s", argv[i]) || !strcmp ("--size", argv[i])) {
 
128
      if (++i>=argc) usage ();
 
129
      if (sscanf (argv[i], "%dx%d", &width, &height) == 2)
 
130
        have_pixel_size = 1;
 
131
      else {
 
132
        size = atoi (argv[i]);
 
133
        if (size < 0) usage();
 
134
      }
 
135
      setit = 1;
 
136
      continue;
 
137
    }
 
138
 
 
139
    if (!strcmp ("-r", argv[i]) || !strcmp ("--rate", argv[i])) {
 
140
      if (++i>=argc) usage ();
 
141
      rate = atoi (argv[i]);
 
142
      if (rate < 0) usage();
 
143
      setit = 1;
 
144
      continue;
 
145
    }
 
146
 
 
147
    if (!strcmp ("-v", argv[i]) || !strcmp ("--version", argv[i])) {
 
148
      version = 1;
 
149
      continue;
 
150
    }
 
151
 
 
152
    if (!strcmp ("-x", argv[i])) {
 
153
      reflection |= RR_Reflect_X;
 
154
      setit = 1;
 
155
      continue;
 
156
    }
 
157
    if (!strcmp ("-y", argv[i])) {
 
158
      reflection |= RR_Reflect_Y;
 
159
      setit = 1;
 
160
      continue;
 
161
    }
 
162
    if (!strcmp ("--screen", argv[i])) {
 
163
      if (++i>=argc) usage ();
 
164
      screen = atoi (argv[i]);
 
165
      if (screen < 0) usage();
 
166
      continue;
 
167
    }
 
168
    if (!strcmp ("-q", argv[i]) || !strcmp ("--query", argv[i])) {
 
169
      query = 1;
 
170
      continue;
 
171
    }
 
172
    if (!strcmp ("-o", argv[i]) || !strcmp ("--orientation", argv[i])) {
 
173
      char *endptr;
 
174
      if (++i>=argc) usage ();
 
175
      dirind = strtol(argv[i], &endptr, 0);
 
176
      if (*endptr != '\0') {
 
177
        for (dirind = 0; dirind < 4; dirind++) {
 
178
          if (strcmp (direction[dirind], argv[i]) == 0) break;
 
179
        }
 
180
        if ((dirind < 0) || (dirind > 3))  usage();
 
181
      }
 
182
      rot = dirind;
 
183
      setit = 1;
 
184
      continue;
 
185
    }
 
186
    usage();
 
187
  }
 
188
  if (verbose) query = 1;
 
189
 
 
190
  dpy = XOpenDisplay (display_name);
 
191
 
 
192
  if (dpy == NULL) {
 
193
      fprintf (stderr, "Can't open display %s\n", display_name);
 
194
      exit (1);
 
195
  }
 
196
  if (screen < 0)
 
197
    screen = DefaultScreen (dpy);
 
198
  if (screen >= ScreenCount (dpy)) {
 
199
    fprintf (stderr, "Invalid screen number %d (display has %d)\n",
 
200
             screen, ScreenCount (dpy));
 
201
    exit (1);
 
202
  }
 
203
 
 
204
  root = RootWindow (dpy, screen);
 
205
 
 
206
  sc = XRRGetScreenInfo (dpy, root);
 
207
 
 
208
  if (sc == NULL) 
 
209
      exit (1);
 
210
  
 
211
  current_size = XRRConfigCurrentConfiguration (sc, &current_rotation);
 
212
 
 
213
  sizes = XRRConfigSizes(sc, &nsize);
 
214
 
 
215
  if (have_pixel_size) {
 
216
    for (size = 0; size < nsize; size++)
 
217
    {
 
218
      if (sizes[size].width == width && sizes[size].height == height)
 
219
        break;
 
220
    }
 
221
  }
 
222
  else if (size < 0)
 
223
    size = current_size;
 
224
 
 
225
  if (size >= nsize) usage();
 
226
 
 
227
  if (rot < 0)
 
228
  {
 
229
    for (rot = 0; rot < 4; rot++)
 
230
        if (1 << rot == (current_rotation & 0xf))
 
231
            break;
 
232
  }
 
233
 
 
234
  current_rate = XRRConfigCurrentRate (sc);
 
235
 
 
236
  if (rate < 0)
 
237
  {
 
238
    if (size == current_size)
 
239
        rate = current_rate;
 
240
    else
 
241
        rate = 0;
 
242
  }
 
243
 
 
244
  if (version) {
 
245
    int major_version, minor_version;
 
246
    XRRQueryVersion (dpy, &major_version, &minor_version);
 
247
    printf("Server reports RandR version %d.%d\n", 
 
248
           major_version, minor_version);
 
249
  }
 
250
 
 
251
  if (query) {
 
252
    printf(" SZ:    Pixels          Physical       Refresh\n");
 
253
    for (i = 0; i < nsize; i++) {
 
254
      printf ("%c%-2d %5d x %-5d  (%4dmm x%4dmm )",
 
255
           i == current_size ? '*' : ' ',
 
256
           i, sizes[i].width, sizes[i].height,
 
257
           sizes[i].mwidth, sizes[i].mheight);
 
258
      rates = XRRConfigRates (sc, i, &nrate);
 
259
      if (nrate) printf ("  ");
 
260
      for (j = 0; j < nrate; j++)
 
261
        printf ("%c%-4d",
 
262
                i == current_size && rates[j] == current_rate ? '*' : ' ',
 
263
                rates[j]);
 
264
      printf ("\n");
 
265
    }
 
266
  }
 
267
 
 
268
  rotations = XRRConfigRotations(sc, &current_rotation);
 
269
 
 
270
  rotation = 1 << rot ;
 
271
  if (query) {
 
272
    for (i = 0; i < 4; i ++) {
 
273
      if ((current_rotation >> i) & 1) 
 
274
        printf("Current rotation - %s\n", direction[i]);
 
275
    }
 
276
 
 
277
    printf("Current reflection - ");
 
278
    if (current_rotation & (RR_Reflect_X|RR_Reflect_Y))
 
279
    {
 
280
        if (current_rotation & RR_Reflect_X) printf ("X Axis ");
 
281
        if (current_rotation & RR_Reflect_Y) printf ("Y Axis");
 
282
    }
 
283
    else
 
284
        printf ("none");
 
285
    printf ("\n");
 
286
    
 
287
 
 
288
    printf ("Rotations possible - ");
 
289
    for (i = 0; i < 4; i ++) {
 
290
      if ((rotations >> i) & 1)  printf("%s ", direction[i]);
 
291
    }
 
292
    printf ("\n");
 
293
 
 
294
    printf ("Reflections possible - ");
 
295
    if (rotations & (RR_Reflect_X|RR_Reflect_Y))
 
296
    {
 
297
        if (rotations & RR_Reflect_X) printf ("X Axis ");
 
298
        if (rotations & RR_Reflect_Y) printf ("Y Axis");
 
299
    }
 
300
    else
 
301
        printf ("none");
 
302
    printf ("\n");
 
303
  }
 
304
 
 
305
  if (verbose) { 
 
306
    printf("Setting size to %d, rotation to %s\n",  size, direction[rot]);
 
307
 
 
308
    printf ("Setting reflection on ");
 
309
    if (reflection)
 
310
    {
 
311
        if (reflection & RR_Reflect_X) printf ("X Axis ");
 
312
        if (reflection & RR_Reflect_Y) printf ("Y Axis");
 
313
    }
 
314
    else
 
315
        printf ("neither axis");
 
316
    printf ("\n");
 
317
 
 
318
    if (reflection & RR_Reflect_X) printf("Setting reflection on X axis\n");
 
319
 
 
320
    if (reflection & RR_Reflect_Y) printf("Setting reflection on Y axis\n");
 
321
  }
 
322
 
 
323
  /* we should test configureNotify on the root window */
 
324
  XSelectInput (dpy, root, StructureNotifyMask);
 
325
 
 
326
  if (setit) XRRSelectInput (dpy, root,
 
327
                        RRScreenChangeNotifyMask);
 
328
  if (setit) status = XRRSetScreenConfigAndRate (dpy, sc,
 
329
                                                 DefaultRootWindow (dpy), 
 
330
               (SizeID) size, (Rotation) (rotation | reflection), rate, CurrentTime);
 
331
  
 
332
  XRRQueryExtension(dpy, &event_base, &error_base);
 
333
  if (verbose && setit) {
 
334
    if (status == RRSetConfigSuccess)
 
335
      {
 
336
        while (1) {
 
337
        int spo;
 
338
        XNextEvent(dpy, (XEvent *) &event);
 
339
        
 
340
        printf ("Event received, type = %d\n", event.type);
 
341
        /* update Xlib's knowledge of the event */
 
342
        XRRUpdateConfiguration (&event);
 
343
        if (event.type == ConfigureNotify)
 
344
          printf("Received ConfigureNotify Event!\n");
 
345
 
 
346
        switch (event.type - event_base) {
 
347
        case RRScreenChangeNotify:
 
348
          sce = (XRRScreenChangeNotifyEvent *) &event;
 
349
 
 
350
          printf("Got a screen change notify event!\n");
 
351
          printf(" window = %d\n root = %d\n size_index = %d\n rotation %d\n", 
 
352
               (int) sce->window, (int) sce->root, 
 
353
               sce->size_index,  sce->rotation);
 
354
          printf(" timestamp = %ld, config_timestamp = %ld\n",
 
355
               sce->timestamp, sce->config_timestamp);
 
356
          printf(" Rotation = %x\n", sce->rotation);
 
357
          printf(" %d X %d pixels, %d X %d mm\n",
 
358
                 sce->width, sce->height, sce->mwidth, sce->mheight);
 
359
          printf("Display width   %d, height   %d\n",
 
360
                 DisplayWidth(dpy, screen), DisplayHeight(dpy, screen));
 
361
          printf("Display widthmm %d, heightmm %d\n", 
 
362
                 DisplayWidthMM(dpy, screen), DisplayHeightMM(dpy, screen));
 
363
          spo = sce->subpixel_order;
 
364
          if ((spo < 0) || (spo > 5))
 
365
            printf ("Unknown subpixel order, value = %d\n", spo);
 
366
          else printf ("new Subpixel rendering model is %s\n", order[spo]);
 
367
          break;
 
368
        default:
 
369
          if (event.type != ConfigureNotify) 
 
370
            printf("unknown event received, type = %d!\n", event.type);
 
371
        }
 
372
        }
 
373
      }
 
374
  }
 
375
  XRRFreeScreenConfigInfo(sc);
 
376
  return(0);
 
377
}