summaryrefslogtreecommitdiff
path: root/hw/xfree86/utils/xorgcfg/screen-cfg.c
blob: 26e675a8fcdff571d088fedb166d9fd39390cb66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
 * Copyright (c) 2000 by Conectiva S.A. (http://www.conectiva.com)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *  
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * CONECTIVA LINUX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * Except as contained in this notice, the name of Conectiva Linux shall
 * not be used in advertising or otherwise to promote the sale, use or other
 * dealings in this Software without prior written authorization from
 * Conectiva Linux.
 *
 * Author: Paulo César Pereira de Andrade <pcpa@conectiva.com.br>
 *
 * $XFree86: xc/programs/Xserver/hw/xfree86/xf86cfg/screen-cfg.c,v 1.13tsi Exp $
 */

#include "xf86config.h"
#include "screen-cfg.h"
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/List.h>
#include <X11/Xaw/Toggle.h>
#include <X11/Xaw/Viewport.h>
#ifdef USE_MODULES
#include "loader.h"
#endif

#define CW	1
#define CCW	-1

/*
 * Prototypes
 */
static void DepthCallback(Widget, XtPointer, XtPointer);
static void SelectIndexCallback(Widget, XtPointer, XtPointer);
static void UnselectIndexCallback(Widget, XtPointer, XtPointer);
static void SelectCallback(Widget, XtPointer, XtPointer);
static void UnselectCallback(Widget, XtPointer, XtPointer);
static void MoveCallback(Widget, XtPointer, XtPointer);
static void RotateCallback(Widget, XtPointer, XtPointer);

/*
 * Initialization
 */
static char *standard_modes[] = {
    "640x400",
    "640x480",
    "800x600",
    "1024x768",
    "1280x960",
    "1280x1024",
    "320x200",
    "320x240",
    "400x300",
    "1152x864",
    "1600x1200",
    "1800x1400",
    "512x384",
    "1400x1050",
    "2048x1536",
    "1920x1440",
};

static char **modes;
static int nmodes;
static int default_depth, sel_index, unsel_index;
static Widget listL, listR;
static char **defmodes;
static int ndefmodes;
static XF86ConfScreenPtr screen;
static int rotate;

/*
 * Implementation
 */
XtPointer
ScreenConfig(XtPointer conf)
{
    XF86ConfDisplayPtr disp;
    Arg args[2];
    int i, oldrotate;

    screen = (XF86ConfScreenPtr)conf;
    if (screen == NULL)
	return (NULL);

    XtSetArg(args[0], XtNstring, screen->scrn_identifier);
    XtSetValues(ident_widget, args, 1);
    if ((default_depth = screen->scrn_defaultdepth) <= 0)
	default_depth = 8;
    sel_index = unsel_index = -1;
    for (i = 0; i < computer.num_screens; i++)
	if (computer.screens[i]->screen == screen) {
	    SetScreenRotate(computer.screens[i]);
	    rotate = computer.screens[i]->rotate;
    }
    oldrotate = rotate;

    ndefmodes = 0;
    disp = screen->scrn_display_lst;
    while (disp != NULL) {
	if (disp->disp_depth == default_depth) {
	    XF86ModePtr mod = disp->disp_mode_lst;

	    while (mod != NULL) {
		if (ndefmodes % 16 == 0)
		    defmodes = (char**)
			XtRealloc((XtPointer)defmodes,
				  (ndefmodes + 16) * sizeof(char*));
		defmodes[ndefmodes++] = XtNewString(mod->mode_name);
		mod = (XF86ModePtr)(mod->list.next);
	    }
	    break;
	}
	disp = (XF86ConfDisplayPtr)(disp->list.next);
    }
    if (ndefmodes == 0) {
	defmodes = (char**)XtMalloc(sizeof(char*));
	defmodes[0] = XtNewString("640x480");
	ndefmodes = 1;
    }

    if (listL != NULL) {
	XawListUnhighlight(listL);
	XawListUnhighlight(listR);
    }

    xf86info.cur_list = SCREEN;
    XtSetSensitive(back, xf86info.lists[SCREEN].cur_function > 0);
    XtSetSensitive(next, xf86info.lists[SCREEN].cur_function <
			 xf86info.lists[SCREEN].num_functions - 1);
    (xf86info.lists[SCREEN].functions[xf86info.lists[SCREEN].cur_function])
	(&xf86info);

    if (ConfigLoop(NULL) == True) {
	XF86ModePtr prev = NULL, mod;

	/* user may have changed the default depth, read variables again */
	disp = screen->scrn_display_lst;
	while (disp != NULL) {
	    if (disp->disp_depth == default_depth)
		break;
	    disp = (XF86ConfDisplayPtr)(disp->list.next);
	}

	if (disp == NULL) {
	    disp = (XF86ConfDisplayPtr)XtCalloc(1, sizeof(XF86ConfDisplayRec));
	    screen->scrn_display_lst = (XF86ConfDisplayPtr)
		xf86addListItem((GenericListPtr)(screen->scrn_display_lst),
			    (GenericListPtr)(disp));
	    disp->disp_depth = default_depth;
	}

	if (strcasecmp(screen->scrn_identifier, ident_string))
	    xf86renameScreen(XF86Config, screen, ident_string);

	screen->scrn_defaultdepth = default_depth;

	XtSetArg(args[0], XtNlist, NULL);
	XtSetArg(args[1], XtNnumberStrings, 0);
	XtSetValues(listL, args, 2);

	XtSetArg(args[0], XtNlist, NULL);
	XtSetArg(args[1], XtNnumberStrings, 0);
	XtSetValues(listR, args, 2);

	mod = disp->disp_mode_lst;
	/* free all modes */
	while (mod != NULL) {
	    prev = mod;
	    mod = (XF86ModePtr)(mod->list.next);
	    XtFree(prev->mode_name);
	    XtFree((XtPointer)prev);
	}
	/* readd modes */
	for (i = 0; i < ndefmodes; i++) {
	    mod = XtNew(XF86ModeRec);
	    mod->mode_name = XtNewString(defmodes[i]);
	    XtFree(defmodes[i]);
	    if (i == 0)
		disp->disp_mode_lst = mod;
	    else
		prev->list.next = mod;
	    prev = mod;
	}
	if (i == 0)
	    disp->disp_mode_lst = NULL;
	else
	    mod->list.next = NULL;

	XtFree((XtPointer)defmodes);
	defmodes = NULL;
	ndefmodes = 0;

	for (i = 0; i < computer.num_screens; i++)
	    if (computer.screens[i]->screen == screen)
		computer.screens[i]->rotate = rotate;

	if (oldrotate != rotate) {
	    static char *Rotate = "Rotate";

	    if (screen->scrn_option_lst != NULL)
		xf86removeOption(&screen->scrn_option_lst, Rotate);
	    if (rotate)
		screen->scrn_option_lst =
		    xf86addNewOption(screen->scrn_option_lst,
				     XtNewString(Rotate),
				     XtNewString(rotate > 0 ? "CW" : "CCW"));
	    UpdateScreenUI();
	    AdjustScreenUI();
	}

	return ((XtPointer)screen);
    }

    XtSetArg(args[0], XtNlist, NULL);
    XtSetArg(args[1], XtNnumberStrings, 0);
    XtSetValues(listL, args, 2);

    XtSetArg(args[0], XtNlist, NULL);
    XtSetArg(args[1], XtNnumberStrings, 0);
    XtSetValues(listR, args, 2);

    for (i = 0; i < ndefmodes; i++)
	XtFree(defmodes[i]);
    XtFree((XtPointer)defmodes);
    defmodes = NULL;
    ndefmodes = 0;

    return (NULL);
}

/*ARGSUSED*/
static void
DepthCallback(Widget w, XtPointer user_data, XtPointer call_data)
{
    if (call_data != NULL)
	default_depth = (long)user_data;
}

/*ARGSUSED*/
static void
SelectIndexCallback(Widget w, XtPointer user_data, XtPointer call_data)
{
    XawListReturnStruct *info = (XawListReturnStruct *)call_data;

    sel_index = info->list_index;
}

/*ARGSUSED*/
static void
UnselectIndexCallback(Widget w, XtPointer user_data, XtPointer call_data)
{
    XawListReturnStruct *info = (XawListReturnStruct *)call_data;

    unsel_index = info->list_index;
}

/*ARGSUSED*/
static void
SelectCallback(Widget w, XtPointer user_data, XtPointer call_data)
{
    Arg args[2];

    if (sel_index < 0 || sel_index >= nmodes)
	return;

    if (ndefmodes == 1 && *defmodes[0] == '\0') {
	/* make sure tmp and defentries are not the same pointer */
	char **tmp = defmodes;

	XtFree(defmodes[0]);
	defmodes = (char**)XtMalloc(sizeof(char*));
	--ndefmodes;
	XtFree((char*)tmp);
    }
    else
	defmodes = (char**)XtRealloc((XtPointer)defmodes,
				     sizeof(char*) * (ndefmodes + 1));
    defmodes[ndefmodes++] = XtNewString(modes[sel_index]);

    XtSetArg(args[0], XtNlist, defmodes);
    XtSetArg(args[1], XtNnumberStrings, ndefmodes);
    XtSetValues(listR, args, 2);

    XawListUnhighlight(listR);
    if (ndefmodes > 1 || (ndefmodes == 1 && *defmodes[0] != '\0')) {
	if (unsel_index >= ndefmodes)
	    unsel_index = ndefmodes - 1;
	XawListHighlight(listR, unsel_index = ndefmodes - 1);
    }
    else
	unsel_index = -1;

    /* force realyout */
    XtUnmanageChild(listR);
    XtManageChild(listR);
}

/*ARGSUSED*/
static void
UnselectCallback(Widget w, XtPointer user_data, XtPointer call_data)
{
    Arg args[2];
    char **modes;
    Cardinal num_modes;

    if (unsel_index < 0 || unsel_index >= ndefmodes)
	return;

    XawListUnhighlight(listL);
    XtSetArg(args[0], XtNlist, &modes);
    XtSetArg(args[1], XtNnumberStrings, &num_modes);
    XtGetValues(listL, args, 2);
    if (modes) {
	for (sel_index = 0; sel_index < num_modes; sel_index++)
	    if (strcmp(defmodes[unsel_index], modes[sel_index]) == 0)
		break;
	if (sel_index < num_modes)
	    XawListHighlight(listL, sel_index);
	else
	    sel_index = -1;
    }

    XtFree(defmodes[unsel_index]);
    if (--ndefmodes > unsel_index)
	memmove(&defmodes[unsel_index], &defmodes[unsel_index + 1],
		(ndefmodes - unsel_index) * sizeof(char*));
    if (ndefmodes == 0) {
	char **tmp = defmodes;

	defmodes = (char**)XtMalloc(sizeof(char*));
	defmodes[0] = XtNewString("");
	ndefmodes = 1;
	XtFree((char*)tmp);
    }

    XtSetArg(args[0], XtNlist, defmodes);
    XtSetArg(args[1], XtNnumberStrings, ndefmodes);
    XtSetValues(listR, args, 2);

    XawListUnhighlight(listR);
    if (ndefmodes > 1 || (ndefmodes == 1 && *defmodes[0] != '\0')) {
	if (unsel_index >= ndefmodes)
	    unsel_index = ndefmodes - 1;
	XawListHighlight(listR, unsel_index);
    }
    else
	unsel_index = -1;
}

/*ARGSUSED*/
static void
MoveCallback(Widget w, XtPointer user_data, XtPointer call_data)
{
    char *tmp;
    Bool down = (long)user_data;

    if (unsel_index < 0 || unsel_index >= ndefmodes)
	return;

    if ((down && unsel_index + 1 >= ndefmodes) ||
	(!down && unsel_index - 1 < 0))
	return;

    tmp = defmodes[unsel_index];
    if (down) {
	defmodes[unsel_index] = defmodes[unsel_index + 1];
	unsel_index++;
    } else {
	defmodes[unsel_index] = defmodes[unsel_index - 1];
	unsel_index--;
    }
    defmodes[unsel_index] = tmp;

    XawListUnhighlight(listR);
    XawListHighlight(listR, unsel_index);
}

/*ARGSUSED*/
void
RotateCallback(Widget w, XtPointer user_data, XtPointer call_data)
{
    if (call_data != NULL)
	rotate = (long)user_data;
    else
	rotate = 0;
}

void
ScreenDialog(XF86SetupInfo *info)
{
    static Widget dialog, d1, d4, d8, d16, d24, labelRotate, cw, ccw;
    Arg args[2];
    XF86ConfMonitorPtr mon = screen->scrn_monitor;
    XF86ConfModeLinePtr mline = mon != NULL ? mon->mon_modeline_lst : NULL;
    int i;
#ifdef USE_MODULES
    xf86cfgModuleOptions *drv_opts = module_options;
    Bool foundRotate = False;
#endif

    while (nmodes > 0)
	XtFree(modes[--nmodes]);
    XtFree((XtPointer)modes);
    modes = NULL;
    while (mline) {
	if (nmodes % 16 == 0)
	    modes = (char**)XtRealloc((XtPointer)modes,
				      sizeof(char*) * (nmodes + 16));
	modes[nmodes++] = XtNewString(mline->ml_identifier);
	mline = (XF86ConfModeLinePtr)(mline->list.next);
    }
    for (i = 0; i < sizeof(standard_modes) / sizeof(standard_modes[0]); i++) {
	if (nmodes % 16 == 0)
	    modes = (char**)XtRealloc((XtPointer)modes,
				      sizeof(char*) * (nmodes + 16));
	modes[nmodes++] = XtNewString(standard_modes[i]);
    }

    if (dialog == NULL) {
	Widget command, viewport;

	dialog = XtCreateWidget("screenD", formWidgetClass,
				configp, NULL, 0);
	XtCreateManagedWidget("depthL", labelWidgetClass,
			      dialog, NULL, 0);
	d1 = XtCreateManagedWidget("1", toggleWidgetClass, dialog, NULL, 0);
	XtAddCallback(d1, XtNcallback, DepthCallback, (XtPointer)1);
	d4 = XtVaCreateManagedWidget("4", toggleWidgetClass, dialog,
				     XtNradioGroup, d1, NULL, 0);
	XtAddCallback(d4, XtNcallback, DepthCallback, (XtPointer)4);
	d8 = XtVaCreateManagedWidget("8", toggleWidgetClass, dialog,
				      XtNradioGroup, d4, NULL, 0);
	XtAddCallback(d8, XtNcallback, DepthCallback, (XtPointer)8);
	d16 = XtVaCreateManagedWidget("16", toggleWidgetClass, dialog,
				      XtNradioGroup, d8, NULL, 0);
	XtAddCallback(d16, XtNcallback, DepthCallback, (XtPointer)16);
	d24 = XtVaCreateManagedWidget("24", toggleWidgetClass, dialog,
				      XtNradioGroup, d16, NULL, 0);
	XtAddCallback(d24, XtNcallback, DepthCallback, (XtPointer)24);

	XtCreateManagedWidget("modeL", labelWidgetClass, dialog, NULL, 0);
	viewport = XtCreateManagedWidget("viewL", viewportWidgetClass, dialog,
					 NULL, 0);
	listL = XtCreateManagedWidget("listLeft", listWidgetClass, viewport,
					NULL, 0);
	XtAddCallback(listL, XtNcallback, SelectIndexCallback, NULL);
	command = XtCreateManagedWidget("select", commandWidgetClass,
					dialog, NULL, 0);
	XtAddCallback(command, XtNcallback, SelectCallback, NULL);
	command = XtCreateManagedWidget("unselect", commandWidgetClass,
					dialog, NULL, 0);
	XtAddCallback(command, XtNcallback, UnselectCallback, NULL);
	command = XtCreateManagedWidget("up", commandWidgetClass,
					dialog, NULL, 0);
	XtAddCallback(command, XtNcallback, MoveCallback, (XtPointer)False);
	command = XtCreateManagedWidget("down", commandWidgetClass,
					dialog, NULL, 0);
	XtAddCallback(command, XtNcallback, MoveCallback, (XtPointer)True);
	viewport = XtCreateManagedWidget("viewR", viewportWidgetClass, dialog,
					 NULL, 0);
	listR = XtCreateManagedWidget("listRight", listWidgetClass, viewport,
				      NULL, 0);
	XtAddCallback(listR, XtNcallback, UnselectIndexCallback, NULL);

	labelRotate = XtCreateManagedWidget("rotate", labelWidgetClass,
					    dialog, NULL, 0);
	cw = XtCreateManagedWidget("CW", toggleWidgetClass, dialog, NULL, 0);
	XtAddCallback(cw, XtNcallback, RotateCallback, (XtPointer)CW);
	ccw = XtVaCreateManagedWidget("CCW", toggleWidgetClass, dialog,
				      XtNradioGroup, cw, NULL, 0);
	XtAddCallback(ccw, XtNcallback, RotateCallback, (XtPointer)CCW);

	XtRealizeWidget(dialog);
    }

#ifdef USE_MODULES
    if (!nomodules) {
	while (drv_opts) {
	    if (drv_opts->type == VideoModule &&
		strcmp(drv_opts->name, screen->scrn_device->dev_driver) == 0) {
		OptionInfoPtr opts = drv_opts->option;

		while (opts->name) {
		    if (xf86nameCompare(opts->name, "Rotate") == 0) {
			foundRotate = True;
			break;
		    }
		    opts++;
		}
		break;
	    }
	    drv_opts = drv_opts->next;
	}

	if (!foundRotate) {
	    XtUnmapWidget(labelRotate);
	    XtUnmapWidget(cw);
	    XtUnmapWidget(ccw);
	}
	else {
	    XtMapWidget(labelRotate);
	    XtMapWidget(cw);
	    XtMapWidget(ccw);
	}
    }
#else
    (void)labelRotate;
#endif
    if (rotate == CW) {
	XtVaSetValues(cw, XtNstate, True, NULL, 0);
	XtVaSetValues(ccw, XtNstate, False, NULL, 0);
    }
    else if (rotate == CCW) {
	XtVaSetValues(cw, XtNstate, False, NULL, 0);
	XtVaSetValues(ccw, XtNstate, True, NULL, 0);
    }
    else {
	XtVaSetValues(cw, XtNstate, False, NULL, 0);
	XtVaSetValues(ccw, XtNstate, False, NULL, 0);
    }

    XtSetArg(args[0], XtNlist, modes);
    XtSetArg(args[1], XtNnumberStrings, nmodes);
    XtSetValues(listL, args, 2);

    XtSetArg(args[0], XtNlist, defmodes);
    XtSetArg(args[1], XtNnumberStrings, ndefmodes);
    XtSetValues(listR, args, 2);

    XtSetArg(args[0], XtNstate, True);
    XtSetValues(default_depth == 1 ? d1 :
		default_depth == 4 ? d4 :
		default_depth == 16 ? d16 :
		default_depth == 24 ? d24 : d8, args, 1);

    XtChangeManagedSet(&current, 1, NULL, NULL, &dialog, 1);
    current = dialog;
}