expert.c 139 KB
Newer Older
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
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
/*
 * 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.
 *
27
 * Author: Paulo César Pereira de Andrade <pcpa@conectiva.com.br>
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
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
 *
 */

#include "config.h"
#include "xf86config.h"
#include "options.h"
#include "screen.h"
#include "vidmode.h"
#include "monitor-cfg.h"
#include <X11/Shell.h>
#include <X11/CompositeP.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/MenuButton.h>
#include <X11/Xaw/Paned.h>
#include <X11/Xaw/Panner.h>
#include <X11/Xaw/Porthole.h>
#include <X11/Xaw/SimpleMenu.h>
#include <X11/Xaw/SmeBSB.h>
#include <X11/Xaw/Toggle.h>
#include <X11/Xaw/Tree.h>
#include <ctype.h>

/*
 * Types
 */
typedef struct _TreeNode TreeNode;
typedef union _TreeData TreeData;
typedef void (*NodeDeleteFunc)(TreeNode*);
typedef void (*NodeUpdateFunc)(TreeNode*);

union _TreeData {
    struct {
	Widget text;
    } files;
    struct {
	Widget text;
	XF86LoadPtr load;
    } module;
    struct {
	Widget text;
	XF86ConfModesPtr modes;
    } modes;
    struct {
	Widget text, value;
	XF86ConfModeLinePtr modeline;
    } modeline;
    struct {
	Widget text, vendor, board, busid, driver;
	XF86ConfVideoAdaptorPtr video;
    } video;
    struct {
	Widget text;
	XF86ConfVideoPortPtr port;
    } port;
    struct {
	Widget text, vendor, model, width, height, hsync, vrefresh,
	       gammaRed, gammaGreen, gammaBlue;
	XF86ConfMonitorPtr monitor;
    } monitor;
    struct {
	Widget menu;
	XF86ConfModesLinkPtr modeslink;
    } modeslink;
    struct {
	Widget text, vendor, board, chipset, busid, card, driver, ramdac,
	       dacSpeed, videoRam, textClockFreq, biosBase, memBase, ioBase,
	       clockChip, devClock, chipId, chipRev, irq, screen;
	XF86ConfDevicePtr device;
    } device;
    struct {
	Widget text, defaultDepth, defaultBpp, defaultFbBpp,
	       monitor, device;
	XF86ConfScreenPtr screen;
    } screen;
    struct {
	Widget menu;
	XF86ConfAdaptorLinkPtr adaptorlink;
    } adaptorlink;
    struct {
	Widget viewport, c_virtual, depth, bpp, visual, weight, black, white;
	XF86ConfDisplayPtr display;
    } display;
    struct {
	Widget text;
	XF86ModePtr mode;
    } mode;
    struct {
	Widget text;
	XF86ConfInputPtr input;
    } input;
    struct {
	Widget text;
	XF86ConfLayoutPtr layout;
    } layout;
    struct {
	Widget menu, button, scrnum, adjx, adjy;
	XF86ConfScreenPtr screen;
	XF86ConfAdjacencyPtr adjacency;
    } adjacency;
    struct {
	Widget menu;
	XF86ConfInputrefPtr inputref;
    } inputref;
    struct {
	Widget text;
	XF86ConfVendorPtr vendor;
    } vendor;
    struct {
	Widget text;
	XF86ConfVendSubPtr vendsub;
    } vendsub;
    struct {
	Widget name, group, mode;
	XF86ConfDRIPtr dri;
    } dri;
    struct {
	Widget count, size, flags;
	XF86ConfBuffersPtr buffers;
    } buffers;
};

struct _TreeNode {
    Widget node, toggle, treeParent;
    TreeNode *parent, *child, *next;
    TreeData *data;
    NodeDeleteFunc destroy;
    NodeUpdateFunc update;
};

/*
 * Prototypes
 */
static Bool ExpertInitialize(void);
static TreeNode *NewNode(TreeNode*, Widget, Widget, Widget, TreeData*);
static void DeleteNode(TreeNode*);
static void DestroyCallback(Widget, XtPointer, XtPointer);
static void PannerCallback(Widget, XtPointer, XtPointer);
static void PortholeCallback(Widget, XtPointer, XtPointer);
static void ToggleCallback(Widget, XtPointer, XtPointer);
static void ToggleNode(TreeNode*, Bool);
static void ToggleNodeRecursive(TreeNode*);
static void OptionsCallback(Widget, XtPointer, XtPointer);
static void RelayoutTree(void);
static void PopdownCallback(Widget, XtPointer, XtPointer);
static void UpdateConfig(TreeNode*);
static void DestroyTree(TreeNode*);

static void CreateFiles(TreeNode*);
static void CreateFilesField(TreeNode*, char*, char*);
static void UpdateFiles(TreeNode*);

static void CreateFontPath(TreeNode*, char*);
static Widget CreateFontPathField(TreeNode*, char*, Bool);
static void FontPathChanged(TreeNode*);
static void NewFontPathCallback(Widget, XtPointer, XtPointer);
static void FontPathCallback(Widget, XtPointer, XtPointer);

static void CreateModulePath(TreeNode*, char*);
static Widget CreateModulePathField(TreeNode*, char*, Bool);
static void ModulePathChanged(TreeNode*);
static void NewModulePathCallback(Widget, XtPointer, XtPointer);

static void CreateModule(TreeNode*, XF86LoadPtr);
static void CreateModuleField(TreeNode*, Bool);
static void ModuleDestroy(TreeNode*);
static void NewModuleCallback(Widget, XtPointer, XtPointer);

static void CreateModes(TreeNode*, XF86ConfModesPtr);
static void CreateModesField(TreeNode*, Bool);
static void ModesDestroy(TreeNode*);
static void NewModesCallback(Widget, XtPointer, XtPointer);
static void CreateModesModeLine(TreeNode*, XF86ConfModeLinePtr);
static void ModesModeLineDestroy(TreeNode*);
static void NewModesModeLineCallback(Widget, XtPointer, XtPointer);

static void CreateModeLineField(TreeNode*, Bool, Bool);
static XF86ConfModeLinePtr ParseModeLine(char*, char*);

static void CreateVideoAdaptor(TreeNode*, XF86ConfVideoAdaptorPtr);
static void CreateVideoAdaptorField(TreeNode*, Bool);
static void VideoAdaptorDestroy(TreeNode*);
static void NewVideoAdaptorCallback(Widget, XtPointer, XtPointer);
static void VideoAdaptorUpdate(TreeNode*);
static void CreateVideoPort(TreeNode*, XF86ConfVideoPortPtr);
static void CreateVideoPortField(TreeNode*, Bool);
static void VideoPortDestroy(TreeNode*);
static void NewVideoPortCallback(Widget, XtPointer, XtPointer);

static void CreateMonitor(TreeNode*, XF86ConfMonitorPtr);
static void CreateMonitorField(TreeNode*, Bool);
static void MonitorDestroy(TreeNode*);
static void NewMonitorCallback(Widget, XtPointer, XtPointer);
static void MonitorUpdate(TreeNode*);
static void CreateMonitorModeLine(TreeNode*, XF86ConfModeLinePtr);
static void MonitorModeLineDestroy(TreeNode*);
static void NewMonitorModeLineCallback(Widget, XtPointer, XtPointer);
static void CreateMonitorModes(TreeNode*, XF86ConfModesLinkPtr);
static void CreateMonitorModesField(TreeNode*, Bool);
static void MonitorModesLinkDestroy(TreeNode*);
static void NewMonitorModesCallback(Widget, XtPointer, XtPointer);

static void CreateDevice(TreeNode*, XF86ConfDevicePtr);
static void CreateDeviceField(TreeNode*, Bool);
static void NewDeviceCallback(Widget, XtPointer, XtPointer);
static void DeviceDestroy(TreeNode*);
static void DeviceUpdate(TreeNode*);

static void CreateScreen(TreeNode*, XF86ConfScreenPtr);
static void CreateScreenField(TreeNode*, Bool);
static void NewScreenCallback(Widget, XtPointer, XtPointer);
static void ScreenDestroy(TreeNode*);
static void ScreenUpdate(TreeNode*);
static void CreateScreenAdaptor(TreeNode*, XF86ConfAdaptorLinkPtr);
static void CreateScreenAdaptorField(TreeNode*, Bool);
static void NewScreenAdaptorCallback(Widget, XtPointer, XtPointer);
static void ScreenAdaptorDestroy(TreeNode*);
static void CreateScreenDisplay(TreeNode*, XF86ConfDisplayPtr);
static void CreateScreenDisplayField(TreeNode*, Bool);
static void NewScreenDisplayCallback(Widget, XtPointer, XtPointer);
static void ScreenDisplayDestroy(TreeNode*);
static void ScreenDisplayUpdate(TreeNode*);
static void CreateDisplayMode(TreeNode*, XF86ModePtr);
static void CreateDisplayModeField(TreeNode*, Bool);
static void NewDisplayModeCallback(Widget, XtPointer, XtPointer);
static void DisplayModeDestroy(TreeNode*);

static void CreateInput(TreeNode*, XF86ConfInputPtr);
static void CreateInputField(TreeNode*, Bool);
static void InputDestroy(TreeNode*);
static void NewInputCallback(Widget, XtPointer, XtPointer);
static void InputUpdate(TreeNode*);

static void CreateLayout(TreeNode*, XF86ConfLayoutPtr);
static void CreateLayoutField(TreeNode*, Bool);
static void LayoutDestroy(TreeNode*);
static void NewLayoutCallback(Widget, XtPointer, XtPointer);
static void CreateAdjacency(TreeNode*, XF86ConfAdjacencyPtr);
static void CreateAdjacencyField(TreeNode*, Bool);
static void AdjacencyDestroy(TreeNode*);
static void NewAdjacencyCallback(Widget, XtPointer, XtPointer);
static void AdjacencyMenuCallback(Widget, XtPointer, XtPointer);
static void AdjacencyToggleCallback(Widget, XtPointer, XtPointer);
static void CreateInputref(TreeNode*, XF86ConfInputrefPtr);
static void CreateInputrefField(TreeNode*, Bool);
static void InputrefDestroy(TreeNode*);
static void NewInputrefCallback(Widget, XtPointer, XtPointer);

static void CreateVendor(TreeNode*, XF86ConfVendorPtr);
static void CreateVendorField(TreeNode*, Bool);
static void VendorDestroy(TreeNode*);
static void NewVendorCallback(Widget, XtPointer, XtPointer);
static void CreateVendorSub(TreeNode*, XF86ConfVendSubPtr);
static void CreateVendorSubField(TreeNode*, Bool);
static void NewVendorSubCallback(Widget, XtPointer, XtPointer);
static void VendorSubDestroy(TreeNode*);
static void VendorSubUpdate(TreeNode*);

static void CreateDRI(TreeNode*, XF86ConfDRIPtr);
static void CreateDRIField(TreeNode*);
static void DRIUpdate(TreeNode*);

static void CreateBuffers(TreeNode*, XF86ConfBuffersPtr);
static void CreateBuffersField(TreeNode*, Bool);
static void BuffersDestroy(TreeNode*);
static void NewBuffersCallback(Widget, XtPointer, XtPointer);
static void BuffersUpdate(TreeNode*);

extern void RemoveDeviceCallback(Widget, XtPointer, XtPointer);

/* interface.c */
extern void InitializeDevices(void);
extern void SelectLayoutCallback(Widget, XtPointer, XtPointer);
extern void UpdateMenuDeviceList(int);
extern void SetConfigModeCallback(Widget, XtPointer, XtPointer);
extern void DefaultLayoutCallback(Widget, XtPointer, XtPointer);
extern void RemoveLayoutCallback(Widget, XtPointer, XtPointer);

/*
 * Initialization
 */
static Widget shell, expert, tree, panner;
extern Widget work, optionsShell, config, layoutp, topMenu;
extern xf86cfgDevice cpu_device;
static TreeNode *mainNode, *monitorTree, *screenTree, *layoutTree;

/*
 * Implementation
 */
void
ExpertConfigureStart(void)
{
    ExpertInitialize();

    XtPopup(shell, XtGrabExclusive);
    if (optionsShell == NULL)
	CreateOptionsShell();
328
    XtVaSetValues(optionsShell, XtNtransientFor, shell, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
329
330
331
332
333
334
335
336
337
}

void
ExpertConfigureEnd(void)
{
    int i, save_config_mode = config_mode;
    Widget sme, layopt, layoutsme = NULL;
    XF86ConfLayoutPtr lay;

338
    XtVaSetValues(optionsShell, XtNtransientFor, toplevel, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
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
    XtPopdown(shell);

    /* Need to do this to avoid all code elsewhere needing to update the
     * "expert" widget tree
     */
    UpdateConfig(mainNode);
    DestroyTree(mainNode);
    XtDestroyWidget(shell);
    expert = NULL;

    if (save_config_mode != CONFIG_LAYOUT)
	SetConfigModeCallback(topMenu, (XtPointer)CONFIG_LAYOUT, NULL);

    /* Reset everything as the "expert" interface can do almost anything
     * to the configuration.
     */
    for (i = 0; i < computer.num_screens; i++) {
	XtDestroyWidget(computer.screens[i]->widget);
	XtFree((XtPointer)computer.screens[i]);
    }
    XtFree((XtPointer)computer.screens);
    computer.screens = NULL;
    computer.num_screens = 0;

    for (i = 0; i < computer.num_devices; i++) {
	XtDestroyWidget(computer.devices[i]->widget);
	XtFree((XtPointer)computer.devices[i]);
    }
    XtFree((XtPointer)computer.devices);
    computer.devices = NULL;
    computer.num_devices = 0;

    for (i = 0; i < computer.num_layouts; i++) {
	XtFree((XtPointer)computer.layouts[i]->position);
	XtFree((XtPointer)computer.layouts[i]);
    }
    XtFree((XtPointer)computer.layouts);
    computer.layouts = NULL;
    computer.num_layouts = 0;

    for (i = 0; i < computer.num_vidmodes; i++)
	XtFree((XtPointer)computer.vidmodes[i]);
    XtFree((XtPointer)computer.vidmodes);
    computer.vidmodes = NULL;
    computer.num_vidmodes = 0;

    /* Reinitialize devices/screens */
    InitializeDevices();
    UpdateMenuDeviceList(MOUSE);
    UpdateMenuDeviceList(KEYBOARD);
    UpdateMenuDeviceList(CARD);
    UpdateMenuDeviceList(MONITOR);

    /* Update layout menu */
	/* first entry is "New server layout" */
    for (i = 1; i < ((CompositeWidget)layoutp)->composite.num_children; i++)
	XtDestroyWidget(((CompositeWidget)layoutp)->composite.children[i]);
    for (i = 0; i < layoutp->core.num_popups; i++)
	XtDestroyWidget(layoutp->core.popup_list[i]);
    lay = XF86Config->conf_layout_lst;
    while (lay != NULL) {
	sme = XtVaCreateManagedWidget("sme", smeBSBObjectClass,
				      layoutp,
				      XtNlabel, lay->lay_identifier,
				      XtNmenuName, lay->lay_identifier,
				      XtNleftBitmap, menuPixmap,
405
				      NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
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
	XtAddCallback(sme, XtNcallback, SelectLayoutCallback, (XtPointer)lay);
	if (layoutsme == NULL)
	    layoutsme = sme;
	layopt = XtCreatePopupShell(lay->lay_identifier, simpleMenuWidgetClass,
				    layoutp, NULL, 0);
	sme = XtCreateManagedWidget("default", smeBSBObjectClass,
				    layopt, NULL, 0);
	XtAddCallback(sme, XtNcallback, DefaultLayoutCallback, NULL);
	sme = XtCreateManagedWidget("remove", smeBSBObjectClass,
				    layopt, NULL, 0);
	XtAddCallback(sme, XtNcallback, RemoveLayoutCallback, NULL);
	XtRealizeWidget(layopt);

	lay = (XF86ConfLayoutPtr)(lay->list.next);
    }
    computer.layout = NULL;
    SelectLayoutCallback(layoutsme,
			 XF86Config->conf_layout_lst, NULL);


    if (XF86Config->conf_flags && XF86Config->conf_flags->flg_option_lst)
	SetTip(&cpu_device);
    for (i = 0; i < computer.num_devices; i++)
	SetTip(computer.devices[i]);

    /* Reinitialize vidmodes */
    InitializeVidmodes();

    if (save_config_mode != CONFIG_LAYOUT)
	SetConfigModeCallback(topMenu, (XtPointer)(long)save_config_mode, NULL);
}

/*ARGSUSED*/
void
ExpertCloseAction(Widget w, XEvent *event, String *params, Cardinal *num_params)
{
    ExpertConfigureEnd();
}

/*ARGSUSED*/
void
ExpertCallback(Widget w, XtPointer user_data, XtPointer call_data)
{
    ExpertConfigureStart();
}

/*ARGSUSED*/
static void
PopdownCallback(Widget w, XtPointer user_data, XtPointer call_data)
{
    ExpertConfigureEnd();
}

/* Files */
static void
CreateFiles(TreeNode *files)
{
    XF86ConfFilesPtr file = XF86Config->conf_files;
    TreeNode *node, *fontpath, *modulepath;
    Widget w;
    char *value;

    value = file->file_logfile ? file->file_logfile : "";
    node = NewNode(files, NULL, NULL, files->node, 
		   (TreeData*)XtCalloc(1, sizeof(TreeData)));
    CreateFilesField(node, "LogFile", value);
    files->child = node;
    files->update = UpdateFiles;

    w = XtVaCreateManagedWidget("ModulePath", toggleWidgetClass, tree,
476
				XtNtreeParent, files->node, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
477
478
479
480
481
    node->next = modulepath = NewNode(files, w, w, files->node, NULL);
    node = node->next;
    CreateModulePath(modulepath, NULL);

    w = XtVaCreateManagedWidget("FontPath", toggleWidgetClass, tree,
482
				XtNtreeParent, files->node, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
483
484
485
486
487
488
489
490
    node->next = fontpath = NewNode(files, w, w, files->node, NULL);
    node = node->next;
    CreateFontPath(fontpath, NULL);
}

static void
CreateFilesField(TreeNode *node, char *name, char *value)
{
491
    Widget box, text;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
492
493

    box = XtVaCreateManagedWidget(name, boxWidgetClass, tree,
494
				  XtNtreeParent, node->node, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
495
    node->node = box;
496
    (void) XtVaCreateManagedWidget("label", labelWidgetClass, box,
497
				    XtNlabel, name, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
498
499
    text = XtVaCreateManagedWidget("value", asciiTextWidgetClass, box,
				   XtNeditType, XawtextEdit, XtNstring, value,
500
				   NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
501
502
503
504
505
506
507
508
509
510
    node->data->files.text = text;
}

static void
UpdateFiles(TreeNode *files)
{
    char *str;

    /* LogFile */
    files = files->child;
511
    XtVaGetValues(files->data->files.text, XtNstring, &str, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
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
    XtFree(XF86Config->conf_files->file_logfile);
    if (*str)
	XF86Config->conf_files->file_logfile = XtNewString(str);
    else
	XF86Config->conf_files->file_logfile = NULL;
}

/* FontPath */
/* Don't need to set the update tree field, as it is already set
 * as the destroy field */
static void
CreateFontPath(TreeNode *fontpath, char *path)
{
    TreeNode *prev = NULL, *node;

    if (path == NULL) {
	if (XF86Font_path) {
	    path = XtNewString(XF86Font_path);
	    if (XF86Config->conf_files && XF86Config->conf_files->file_fontpath) {
		XtFree(XF86Config->conf_files->file_fontpath);
		XF86Config->conf_files->file_fontpath = XtNewString(path);
	    }
	}
	else if (XF86Config->conf_files && XF86Config->conf_files->file_fontpath)
	    path = XtNewString(XF86Config->conf_files->file_fontpath);
    }
    else {
	path = XtNewString(path);
	if ((prev = fontpath->child) != NULL)
	    while (prev->next)
		prev = prev->next;
    }

    if (path) {
	char *s;

	for (s = strtok(path, ","); s != NULL; s = strtok(NULL, ",")) {
	    node = NewNode(fontpath, NULL, NULL, fontpath->node, NULL);
	    node->destroy = FontPathChanged;
551
	    (void) CreateFontPathField(node, s, False);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
552
553
554
555
556
557
558
559
560
561
	    if (fontpath->child == NULL)
		fontpath->child = node;
	    else
		prev->next = node;
	    prev = node;
	}
	XtFree(path);
    }

    node = NewNode(fontpath, NULL, NULL, fontpath->node, NULL);
562
    (void) CreateFontPathField(node, "", True);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
563
564
565
566
567
568
569
570
571
572
573
574
575
    if (fontpath->child == NULL)
	fontpath->child = node;
    else
	prev->next = node;
}

static Widget
CreateFontPathField(TreeNode *fontpath, char *value, Bool addnew)
{
    Widget box, command, text;
    TreeData *data;

    box = XtVaCreateWidget("fontpath", formWidgetClass, tree,
576
			   XtNtreeParent, fontpath->treeParent, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
577
578
579
580
581
582
583
584
585
586
587
588
589
590
    fontpath->node = box;
    if (!addnew) {
	command = XtCreateManagedWidget("remove", commandWidgetClass, box,
					NULL, 0);
	XtAddCallback(command, XtNcallback, DestroyCallback,
		      (XtPointer)fontpath);
	command = XtCreateManagedWidget("up", commandWidgetClass, box, NULL, 0);
	XtAddCallback(command, XtNcallback, FontPathCallback,
		      (XtPointer)fontpath);
	command = XtCreateManagedWidget("down", commandWidgetClass, box, NULL, 0);
	XtAddCallback(command, XtNcallback, FontPathCallback,
		      (XtPointer)fontpath);
	text = XtVaCreateManagedWidget("value", asciiTextWidgetClass, box,
				       XtNeditType, XawtextEdit,
591
				       XtNstring, value, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
592
593
594
595
596
597
598
    }
    else {
	command = XtCreateManagedWidget("new", commandWidgetClass, box, NULL, 0);
	XtAddCallback(command, XtNcallback, NewFontPathCallback, 
		      (XtPointer)fontpath);
	text = XtVaCreateManagedWidget("valueNew", asciiTextWidgetClass, box,
				       XtNeditType, XawtextEdit,
599
				       XtNstring, value, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
    }
    data = (TreeData*)XtCalloc(1, sizeof(TreeData));
    data->files.text = text;
    fontpath->data = data;

    if (fontpath->treeParent && XtIsRealized(fontpath->treeParent))
	XtRealizeWidget(box);
    XtManageChild(box);

    return (box);
}

static void
FontPathChanged(TreeNode *node)
{
    TreeNode *parent = node->parent;
    char *fontpath = NULL, *str;
    Arg args[1];
    int pos = 0, len;

    /* last node is the "new" */
    for (node = parent->child; node->next != NULL; node = node->next) {
	if (pos)
	    fontpath[pos++] = ',';
	XtSetArg(args[0], XtNstring, &str);
	XtGetValues(node->data->files.text, args, 1);
	len = strlen(str) + 2;
	fontpath = XtRealloc(fontpath, pos + len);
	strcpy(fontpath + pos, str);
	pos += len - 2;
    }

    if (XF86Config->conf_files->file_fontpath)
	XtFree(XF86Config->conf_files->file_fontpath);
    XF86Config->conf_files->file_fontpath = fontpath;
}

static void
NewFontPathCallback(Widget unused, XtPointer user_data, XtPointer call_data)
{
    TreeNode *fontpath, *node = (TreeNode*)user_data;
    Arg args[1];
    char *str;

    XtSetArg(args[0], XtNstring, &str);
    XtGetValues(node->data->files.text, args, 1);
    if (*str == '\0')
	return;

    fontpath = node->parent;
    DeleteNode(node);
    CreateFontPath(fontpath, str);

    FontPathChanged(fontpath->child);
    RelayoutTree();
}

/*ARGSUSED*/
static void
FontPathCallback(Widget w, XtPointer user_data, XtPointer call_data)
{
    TreeNode *parent, *node, *fontpath = (TreeNode*)user_data;
    char *t1, *t2;
    Widget w1, w2;

    parent = fontpath->parent;
    node = parent->child;
    if (!node->next->next)
	return;
    if (strcmp(XtName(w), "up") == 0) {
	if (node == fontpath)
	    while (node->next->next)
		node = node->next;
	else
	    while (node && node->next != fontpath)
		node = node->next;
    }
    else {
	if (fontpath->next->next)
	    node = fontpath->next;
	/* else is already correct */
    }

    w1 = node->data->files.text;
    w2 = fontpath->data->files.text;

    XtVaGetValues(w1, XtNstring, &t1, NULL);
    XtVaGetValues(w2, XtNstring, &t2, NULL);
    t1 = XtNewString(t1);
    XtVaSetValues(w1, XtNstring, t2, NULL);
    XtVaSetValues(w2, XtNstring, t1, NULL);
    XtFree(t1);
}


/* ModulePath */
/* Don't need to set the update tree field, as it is already set
 * as the destroy field */
static void
CreateModulePath(TreeNode *modulepath, char *path)
{
    TreeNode *prev = NULL, *node;

    if (path == NULL) {
	if (XF86Module_path) {
	    path = XtNewString(XF86Module_path);
	    if (XF86Config->conf_files && XF86Config->conf_files->file_modulepath) {
		XtFree(XF86Config->conf_files->file_modulepath);
		XF86Config->conf_files->file_modulepath = XtNewString(path);
	    }
	}
	else if (XF86Config->conf_files && XF86Config->conf_files->file_modulepath)
	    path = XtNewString(XF86Config->conf_files->file_modulepath);
    }
    else {
	path = XtNewString(path);
	if ((prev = modulepath->child) != NULL)
	    while (prev->next)
		prev = prev->next;
    }

    if (path) {
	char *s;

	for (s = strtok(path, ","); s != NULL; s = strtok(NULL, ",")) {
	    node = NewNode(modulepath, NULL, NULL, modulepath->node, NULL);
	    node->destroy = ModulePathChanged;
727
	    (void) CreateModulePathField(node, s, False);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
728
729
730
731
732
733
734
735
736
737
	    if (modulepath->child == NULL)
		modulepath->child = node;
	    else
		prev->next = node;
	    prev = node;
	}
	XtFree(path);
    }

    node = NewNode(modulepath, NULL, NULL, modulepath->node, NULL);
738
    (void) CreateModulePathField(node, "", True);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
739
740
741
742
743
744
745
746
747
748
749
750
751
    if (modulepath->child == NULL)
	modulepath->child = node;
    else
	prev->next = node;
}

static Widget
CreateModulePathField(TreeNode *modulepath, char *value, Bool addnew)
{
    Widget box, command, text;
    TreeData *data;

    box = XtVaCreateWidget("modulepath", formWidgetClass, tree,
752
			   XtNtreeParent, modulepath->treeParent, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
753
754
755
756
757
758
759
760
    modulepath->node = box;
    if (!addnew) {
	command = XtCreateManagedWidget("remove", commandWidgetClass, box,
					NULL, 0);
	XtAddCallback(command, XtNcallback, DestroyCallback,
		      (XtPointer)modulepath);
	text = XtVaCreateManagedWidget("value", asciiTextWidgetClass, box,
				       XtNeditType, XawtextEdit,
761
				       XtNstring, value, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
762
763
764
765
766
767
768
    }
    else {
	command = XtCreateManagedWidget("new", commandWidgetClass, box, NULL, 0);
	XtAddCallback(command, XtNcallback, NewModulePathCallback, 
		      (XtPointer)modulepath);
	text = XtVaCreateManagedWidget("valueNew", asciiTextWidgetClass, box,
				       XtNeditType, XawtextEdit,
769
				       XtNstring, value, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
    }
    data = (TreeData*)XtCalloc(1, sizeof(TreeData));
    data->files.text = text;
    modulepath->data = data;

    if (modulepath->treeParent && XtIsRealized(modulepath->treeParent))
	XtRealizeWidget(box);
    XtManageChild(box);

    return (box);
}

static void
ModulePathChanged(TreeNode *node)
{
    TreeNode *parent = node->parent;
    char *modulepath = NULL, *str;
    Arg args[1];
    int pos = 0, len;

    /* last node is the "new" */
    for (node = parent->child; node->next != NULL; node = node->next) {
	if (pos)
	    modulepath[pos++] = ',';
	XtSetArg(args[0], XtNstring, &str);
	XtGetValues(node->data->files.text, args, 1);
	len = strlen(str) + 2;
	modulepath = XtRealloc(modulepath, pos + len);
	strcpy(modulepath + pos, str);
	pos += len - 2;
    }

    if (XF86Config->conf_files->file_modulepath)
	XtFree(XF86Config->conf_files->file_modulepath);
    XF86Config->conf_files->file_modulepath = modulepath;
}

static void
NewModulePathCallback(Widget unused, XtPointer user_data, XtPointer call_data)
{
    TreeNode *modulepath, *node = (TreeNode*)user_data;
    Arg args[1];
    char *str;

    XtSetArg(args[0], XtNstring, &str);
    XtGetValues(node->data->files.text, args, 1);
    if (*str == '\0')
	return;

    modulepath = node->parent;
    DeleteNode(node);
    CreateModulePath(modulepath, str);

    ModulePathChanged(modulepath->child);
    RelayoutTree();
}

/* Module */
static void
CreateModule(TreeNode *module, XF86LoadPtr load)
{
    TreeNode *prev, *node;
    TreeData *data;

    if ((prev = module->child) != NULL)
	while (prev->next)
	    prev = prev->next;

    while (load) {
	data = (TreeData*)XtCalloc(1, sizeof(TreeData));
	data->module.load = load;
	node = NewNode(module, NULL, NULL, module->node, data);
	node->destroy = ModuleDestroy;
	CreateModuleField(node, False);
	if (module->child == NULL)
	    module->child = node;
	else
	    prev->next = node;
	prev = node;
	load = (XF86LoadPtr)(load->list.next);
    }

    data = (TreeData*)XtCalloc(1, sizeof(TreeData));
    node = NewNode(module, NULL, NULL, module->node, data);
    CreateModuleField(node, True);
    if (module->child == NULL)
	module->child = node;
    else
	prev->next = node;
}

static void
CreateModuleField(TreeNode *node, Bool addnew)
{
    Widget box, command, label;

    box = XtVaCreateWidget("module", formWidgetClass, tree,
867
			   XtNtreeParent, node->treeParent, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
868
869
870
871
872
873
874
875
876
877
878
879
880
881
    node->node = box;

    if (!addnew) {
	XF86OptionPtr *options;
	XF86LoadPtr load = node->data->module.load;

	options = &(load->load_opt);
	command = XtCreateManagedWidget("remove", commandWidgetClass, box,
					NULL, 0);
	XtAddCallback(command, XtNcallback, DestroyCallback, (XtPointer)node);
	command = XtCreateManagedWidget("options", commandWidgetClass, box,
					NULL, 0);
	XtAddCallback(command, XtNcallback, OptionsCallback, (XtPointer)options);
	label = XtVaCreateManagedWidget("label", labelWidgetClass, box,
882
					XtNlabel, load->load_name, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
883
884
885
886
887
888
889
    }
    else {
	command = XtCreateManagedWidget("new", commandWidgetClass, box,
					NULL, 0);
	XtAddCallback(command, XtNcallback, NewModuleCallback, (XtPointer)node);
	label = XtVaCreateManagedWidget("value", asciiTextWidgetClass, box,
					XtNeditType, XawtextEdit,
890
					NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
	node->data->module.text = label;
    }
    if (XtIsRealized(node->treeParent))
	XtRealizeWidget(box);
    XtManageChild(box);
}

/*ARGUSED*/
static void
ModuleDestroy(TreeNode *node)
{
    if (node->data->module.load)
	xf86removeModule(XF86Config, node->data->module.load);
}

/*ARGSUSED*/
static void
NewModuleCallback(Widget unused, XtPointer user_data, XtPointer call_data)
{
    TreeNode *module, *node = (TreeNode*)user_data;
    XF86LoadPtr load;
    Arg args[1];
    char *label;

    XtSetArg(args[0], XtNstring, &label);
    XtGetValues(node->data->module.text, args, 1);
    if (*label == '\0')
	return;

    module = node->parent;
    DeleteNode(node);
    load = (XF86LoadPtr)XtCalloc(1, sizeof(XF86LoadRec));
    load->load_name = XtNewString(label);
    XF86Config->conf_modules->mod_load_lst =
	xf86addModule(XF86Config->conf_modules->mod_load_lst, load);

    CreateModule(module, load);
    RelayoutTree();
}

/* Modes */
static void
CreateModes(TreeNode *parent, XF86ConfModesPtr modes)
{
    TreeNode *node, *prev;
    TreeData *data;

    if ((prev = parent->child) != NULL)
	while (prev->next)
	    prev = prev->next;

    while (modes) {
	data = (TreeData*)XtCalloc(1, sizeof(TreeData));
	data->modes.modes = modes;
	node = NewNode(parent, NULL, NULL, parent->node, data);
	node->destroy = ModesDestroy;
	CreateModesField(node, False);
	if (parent->child == NULL)
	    parent->child = node;
	else
	    prev->next = node;
	prev = node;

	modes = (XF86ConfModesPtr)(modes->list.next);
    }

    data = (TreeData*)XtCalloc(1, sizeof(TreeData));
    node = NewNode(parent, NULL, NULL, parent->node, data);
    CreateModesField(node, True);
    if (parent->child == NULL)
	parent->child = node;
    else
	prev->next = node;
}

static void
CreateModesField(TreeNode *node, Bool addnew)
{
    Widget box, command, label;

    box = XtVaCreateWidget("modes", formWidgetClass, tree,
972
			   XtNtreeParent, node->treeParent, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
973
974
975
976
977
978
979
980
981
982
983
    node->node = box;

    if (!addnew) {
	XF86ConfModesPtr modes = node->data->modes.modes;

	command = XtCreateManagedWidget("remove", commandWidgetClass, box,
					NULL, 0);
	XtAddCallback(command, XtNcallback, DestroyCallback, (XtPointer)node);
	label = XtVaCreateManagedWidget("mode", toggleWidgetClass, box,
					XtNlabel, modes->modes_identifier,
					XtNstate, True,
984
					NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
985
986
987
988
989
990
991
992
993
994
	node->toggle = label;
	XtAddCallback(label, XtNcallback, ToggleCallback, (XtPointer)node);
	CreateModesModeLine(node, node->data->modes.modes->mon_modeline_lst);
    }
    else {
	command = XtCreateManagedWidget("new", commandWidgetClass, box,
					NULL, 0);
	XtAddCallback(command, XtNcallback, NewModesCallback, (XtPointer)node);
	label = XtVaCreateManagedWidget("value", asciiTextWidgetClass, box,
					XtNeditType, XawtextEdit,
995
					NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
	node->data->modes.text = label;
    }
    if (XtIsRealized(node->treeParent))
	XtRealizeWidget(box);
    XtManageChild(box);
}

/*ARGUSED*/
static void
ModesDestroy(TreeNode *node)
{
    if (node->data->modes.modes) {
	int i;
	TreeNode *mon = monitorTree->child;

	/* last one is the "new" entry */
	while (mon && mon->next) {
	    /* UseModes is the second entry */
	    TreeNode *mod = mon->child->next->child;
	    CompositeWidget composite;

	    while (mod && mod->next) {
		TreeNode *next = mod->next;

		if (mod && strcmp(mod->data->modeslink.modeslink->ml_modes_str,
				  node->data->modes.modes->modes_identifier) == 0)
		    /* Needs to do string comparison because may be deleting
		     * a "test" Modes section, with no Modelines.
		     */
		    DeleteNode(mod);
		mod = next;
	    }
	    composite = (CompositeWidget)mod->data->modeslink.menu;

	    for (i = 0; i < composite->composite.num_children; ++i)
		if (strcmp(XtName(composite->composite.children[i]),
			   node->data->modes.modes->modes_identifier) == 0)
		    XtDestroyWidget(composite->composite.children[i]);

	    mon = mon->next;
	}

	xf86removeModes(XF86Config, node->data->modes.modes);
    }
}

/*ARGSUSED*/
static void
NewModesCallback(Widget unused, XtPointer user_data, XtPointer call_data)
{
    TreeNode *parent, *node = (TreeNode*)user_data;
    XF86ConfModesPtr modes;
    Arg args[1];
    char *label;

    XtSetArg(args[0], XtNstring, &label);
    XtGetValues(node->data->modes.text, args, 1);
    if (*label == '\0')
	return;

    parent = node->parent;
    DeleteNode(node);
    modes = (XF86ConfModesPtr)XtCalloc(1, sizeof(XF86ConfModesRec));
    modes->modes_identifier = XtNewString(label);
    XF86Config->conf_modes_lst =
	xf86addModes(XF86Config->conf_modes_lst, modes);

    {
	TreeNode *mon = monitorTree->child;
	Widget sme;

	/* last one is the "new" entry */
	while (mon && mon->next) {
	    /* UseModes is the second entry */
	    TreeNode *mod = mon->child->next->child;

	    while (mod && mod->next)
		mod = mod->next;

	    sme = XtCreateManagedWidget(modes->modes_identifier,
					smeBSBObjectClass,
					mod->data->modeslink.menu, NULL, 0);
	    XtAddCallback(sme, XtNcallback, NewMonitorModesCallback,
			  (XtPointer)mod);

	    mon = mon->next;
	}
    }

    CreateModes(parent, modes);
    RelayoutTree();
}

static void
CreateModesModeLine(TreeNode *parent, XF86ConfModeLinePtr modeline)
{
    TreeNode *node, *prev;
    TreeData *data;

    if ((prev = parent->child) != NULL)
	while (prev->next)
	    prev = prev->next;

    while (modeline) {
	data = (TreeData*)XtCalloc(1, sizeof(TreeData));
	data->modeline.modeline = modeline;
	node = NewNode(parent, NULL, NULL, parent->node, data);
	node->destroy = ModesModeLineDestroy;
	CreateModeLineField(node, False, False);
	if (parent->child == NULL)
	    parent->child = node;
	else
	    prev->next = node;
	prev = node;
	modeline = (XF86ConfModeLinePtr)(modeline->list.next);
    }
    data = (TreeData*)XtCalloc(1, sizeof(TreeData));
    node = NewNode(parent, NULL, NULL, parent->node, data);
    if (parent->child == NULL)
	parent->child = node;
    else
	prev->next = node;
    prev = node;
    CreateModeLineField(node, True, False);
}

/* This function should allow creating modelines for the
   Mode and Monitor section */
static void
CreateModeLineField(TreeNode *node, Bool addnew, Bool monitor)
{
    Widget box, command;
    char buf[512], tmp[32];

    box = XtVaCreateWidget("modeline", formWidgetClass, tree,
1131
			   XtNtreeParent, node->treeParent, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1132
1133
1134
1135
1136
1137
1138
1139
1140
    node->node = box;

    if (!addnew) {
	XF86ConfModeLinePtr mod = node->data->modeline.modeline;

	command = XtCreateManagedWidget("remove", commandWidgetClass,
					box, NULL, 0);
	XtAddCallback(command, XtNcallback, DestroyCallback, (XtPointer)node);
	XtVaCreateManagedWidget("label", labelWidgetClass, box,
1141
				XtNlabel, mod->ml_identifier, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179

	XmuSnprintf(buf, sizeof(buf), "%g %d %d %d %d %d %d %d %d",
	    mod->ml_clock / 1000., mod->ml_hdisplay, mod->ml_hsyncstart,
	    mod->ml_hsyncend, mod->ml_htotal, mod->ml_vdisplay,
	    mod->ml_vsyncstart, mod->ml_vsyncend, mod->ml_vtotal);
	if (mod->ml_flags & XF86CONF_INTERLACE)
	    strcat(buf, " interlace");
	if (mod->ml_flags & XF86CONF_PHSYNC)
	    strcat(buf, " +hsync");
	if (mod->ml_flags & XF86CONF_NHSYNC)
	    strcat(buf, " -hsync");
	if (mod->ml_flags & XF86CONF_PVSYNC)
	    strcat(buf, " +vsync");
	if (mod->ml_flags & XF86CONF_NVSYNC)
	    strcat(buf, " -vsync");
	if (mod->ml_flags & XF86CONF_CSYNC)
	    strcat(buf, " composite");
	if (mod->ml_flags & XF86CONF_PCSYNC)
	    strcat(buf, " +csync");
	if (mod->ml_flags & XF86CONF_NCSYNC)
	    strcat(buf, " -csync");
	if (mod->ml_flags & XF86CONF_DBLSCAN)
	    strcat(buf, " doublescan");
	if (mod->ml_flags & XF86CONF_BCAST)
	    strcat(buf, " bcast");
	if (mod->ml_flags & XF86CONF_HSKEW) {
	    XmuSnprintf(tmp, sizeof(tmp), " hskew %d", mod->ml_hskew);
	    strcat(buf, tmp);
	}
	if (mod->ml_flags & XF86CONF_VSCAN) {
	    XmuSnprintf(tmp, sizeof(tmp), " vscan %d", mod->ml_vscan);
	    strcat(buf, tmp);
	}
	if (mod->ml_flags & XF86CONF_CUSTOM)
	    strcat(buf, " custom");
	node->data->modeline.value =
	    XtVaCreateManagedWidget("modeline", asciiTextWidgetClass, box,
				    XtNeditType, XawtextEdit,
1180
				    XtNstring, buf, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
    }
    else {
	*buf = '\0';
	command = XtCreateManagedWidget("new", commandWidgetClass,
					box, NULL, 0);
	XtAddCallback(command, XtNcallback, monitor ?
		      NewMonitorModeLineCallback : NewModesModeLineCallback,
		      (XtPointer)node);
	node->data->modeline.text =
		XtVaCreateManagedWidget("value", asciiTextWidgetClass, box,
1191
					XtNeditType, XawtextEdit, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1192
1193
	node->data->modeline.value =
		XtVaCreateManagedWidget("modelineNew", asciiTextWidgetClass, box,
1194
					XtNeditType, XawtextEdit, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
    }
    if (XtIsRealized(node->treeParent))
	XtRealizeWidget(box);
    XtManageChild(box);
}

/*ARGUSED*/
static void
ModesModeLineDestroy(TreeNode *node)
{
    if (node->data->modeline.modeline)
	xf86removeModesModeLine(node->parent->data->modes.modes,
				node->data->modeline.modeline);
}

/*ARGSUSED*/
static void
NewModesModeLineCallback(Widget unused, XtPointer user_data, XtPointer call_data)
{
    TreeNode *parent, *node = (TreeNode*)user_data;
    XF86ConfModeLinePtr modeline;
    Arg args[1];
    char *ident, *value;

    XtSetArg(args[0], XtNstring, &ident);
    XtGetValues(node->data->modeline.text, args, 1);
    XtSetArg(args[0], XtNstring, &value);
    XtGetValues(node->data->modeline.value, args, 1);
    if (*ident == '\0' || *value == '\0')
	return;

    parent = node->parent;
    DeleteNode(node);
    modeline = ParseModeLine(ident, value);
    parent->data->modes.modes->mon_modeline_lst =
	xf86addModeLine(parent->data->modes.modes->mon_modeline_lst, modeline);

    CreateModesModeLine(parent, modeline);
    RelayoutTree();
}

static XF86ConfModeLinePtr
ParseModeLine(char *identifier, char *modeline)
{
    XF86ConfModeLinePtr ml = (XF86ConfModeLinePtr)
	XtCalloc(1, sizeof(XF86ConfModeLineRec));
    char *s, *ptr = modeline;

    /* Identifier */
    ml->ml_identifier = XtNewString(identifier);

    ml->ml_clock = (int)(strtod(ptr, &ptr) * 1000.0 + 0.5);
    while (*ptr && isspace(*ptr)) ++ptr;

    ml->ml_hdisplay = strtol(ptr, &ptr, 10);
    while (*ptr && isspace(*ptr)) ++ptr;

    ml->ml_hsyncstart = strtol(ptr, &ptr, 10);
    while (*ptr && isspace(*ptr)) ++ptr;

    ml->ml_hsyncend = strtol(ptr, &ptr, 10);
    while (*ptr && isspace(*ptr)) ++ptr;

    ml->ml_htotal = strtol(ptr, &ptr, 10);
    while (*ptr && isspace(*ptr)) ++ptr;

    ml->ml_vdisplay = strtol(ptr, &ptr, 10);
    while (*ptr && isspace(*ptr)) ++ptr;

    ml->ml_vsyncstart = strtol(ptr, &ptr, 10);
    while (*ptr && isspace(*ptr)) ++ptr;

    ml->ml_vsyncend = strtol(ptr, &ptr, 10);
    while (*ptr && isspace(*ptr)) ++ptr;

    ml->ml_vtotal = strtol(ptr, &ptr, 10);
    while (*ptr && isspace(*ptr)) ++ptr;

    s = ptr;
    while (*s) {
	*s = tolower(*s);
	++s;
    }
    s = ptr;

    while (*ptr) {
	while (*s && isspace(*s))
	    s++;
	ptr = s;
	while (*s && !isspace(*s))
	    s++;

	if (s != ptr) {
	    Bool done = *s == '\0';

	    *s = '\0';
	    if (strcmp(ptr, "interlace") == 0)
		ml->ml_flags |= XF86CONF_INTERLACE;
	    else if (strcmp(ptr, "+hsync") == 0)
		ml->ml_flags |= XF86CONF_PHSYNC;
	    else if (strcmp(ptr, "-hsync") == 0)
		ml->ml_flags |= XF86CONF_NHSYNC;
	    else if (strcmp(ptr, "+vsync") == 0)
		ml->ml_flags |= XF86CONF_PVSYNC;
	    else if (strcmp(ptr, "-vsync") == 0)
		ml->ml_flags |= XF86CONF_NVSYNC;
	    else if (strcmp(ptr, "composite") == 0)
		ml->ml_flags |= XF86CONF_CSYNC;
	    else if (strcmp(ptr, "+csync") == 0)
		ml->ml_flags |= XF86CONF_PCSYNC;
	    else if (strcmp(ptr, "-csync") == 0)
		ml->ml_flags |= XF86CONF_NCSYNC;
	    else if (strcmp(ptr, "doublescan") == 0)
		ml->ml_flags |= XF86CONF_DBLSCAN;
	    else if (strcmp(ptr, "bcast") == 0)
		ml->ml_flags |= XF86CONF_BCAST;
	    else if (strcmp(ptr, "hskew") == 0) {
		++s;
		while (*s && isspace(*s))
		    ++s;
		ptr = s;
		while (*s && !isspace(*s))
		    ++s;
		if (ptr != s) {
		    ml->ml_hskew = strtol(ptr, &s, 10);
		    ml->ml_flags |= XF86CONF_HSKEW;
		    --s;
		}
	    }
	    else if (strcmp(ptr, "vscan") == 0) {
		++s;
		while (*s && isspace(*s))
		    ++s;
		ptr = s;
		while (*s && !isspace(*s))
		    ++s;
		if (ptr != s) {
		    ml->ml_vscan = strtol(ptr, &s, 10);
		    ml->ml_flags |= XF86CONF_VSCAN;
		    --s;
		}
	    }
	    else if (strcmp(ptr, "custom") == 0)
		ml->ml_flags |= XF86CONF_CUSTOM;
	    ++s;
	    if (done)
		break;
	    ptr = s;
	}
    }

    return (ml);
}

/* VideoAdpator */
static void
CreateVideoAdaptor(TreeNode *parent, XF86ConfVideoAdaptorPtr video)
{
    TreeNode *node, *prev;
    TreeData *data;

    if ((prev = parent->child) != NULL)
	while (prev->next)
	    prev = prev->next;

    while (video) {
	data = (TreeData*)XtCalloc(1, sizeof(TreeData));
	data->video.video = video;
	node = NewNode(parent, NULL, NULL, parent->node, data);
	node->destroy = VideoAdaptorDestroy;
	node->update = VideoAdaptorUpdate;
	CreateVideoAdaptorField(node, False);
	if (parent->child == NULL)
	    parent->child = node;
	else
	    prev->next = node;
	prev = node;

	video = (XF86ConfVideoAdaptorPtr)(video->list.next);
    }

    data = (TreeData*)XtCalloc(1, sizeof(TreeData));
    node = NewNode(parent, NULL, NULL, parent->node, data);
    CreateVideoAdaptorField(node, True);
    if (parent->child == NULL)
	parent->child = node;
    else
	prev->next = node;
}

static void
CreateVideoAdaptorField(TreeNode *node, Bool addnew)
{
    Widget box, command, label;

    box = XtVaCreateWidget("video", formWidgetClass, tree,
1391
			   XtNtreeParent, node->treeParent, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
    node->node = box;

    if (!addnew) {
	char *str;
	TreeNode *port;
	XF86ConfVideoAdaptorPtr video = node->data->video.video;

	command = XtCreateManagedWidget("remove", commandWidgetClass, box,
					NULL, 0);
	XtAddCallback(command, XtNcallback, DestroyCallback, (XtPointer)node);
	command = XtCreateManagedWidget("options", commandWidgetClass, box,
					NULL, 0);
	XtAddCallback(command, XtNcallback, OptionsCallback,
		      (XtPointer)&(video->va_option_lst));
	label = XtVaCreateManagedWidget("adaptor", labelWidgetClass, box,
					XtNlabel, video->va_identifier,
1408
					NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1409
1410
1411
1412
1413
1414
1415

	XtCreateManagedWidget("vendorL", labelWidgetClass, box, NULL, 0);
	str = video->va_vendor ? video->va_vendor : "";
	node->data->video.vendor =
		XtVaCreateManagedWidget("vendor", asciiTextWidgetClass, box,
					XtNeditType, XawtextEdit,
					XtNstring, str,
1416
					NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1417
1418
1419
1420
1421
1422
1423

	XtCreateManagedWidget("boardL", labelWidgetClass, box, NULL, 0);
	str = video->va_board ? video->va_board : "";
	node->data->video.board =
		XtVaCreateManagedWidget("board", asciiTextWidgetClass, box,
					XtNeditType, XawtextEdit,
					XtNstring, str,
1424
					NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1425
1426
1427
1428
1429
1430
1431

	XtCreateManagedWidget("busidL", labelWidgetClass, box, NULL, 0);
	str = video->va_busid ? video->va_busid : "";
	node->data->video.busid =
		XtVaCreateManagedWidget("busid", asciiTextWidgetClass, box,
					XtNeditType, XawtextEdit,
					XtNstring, str,
1432
					NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1433
1434
1435
1436
1437
1438
1439

	XtCreateManagedWidget("driverL", labelWidgetClass, box, NULL, 0);
	str = video->va_driver ? video->va_driver : "";
	node->data->video.driver =
		XtVaCreateManagedWidget("driver", asciiTextWidgetClass, box,
					XtNeditType, XawtextEdit,
					XtNstring, str,
1440
					NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1441
1442
1443
1444

	label = XtVaCreateManagedWidget("VideoPort", toggleWidgetClass, tree,
					XtNstate, True,
					XtNtreeParent, box,
1445
					NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
	port = NewNode(node, label, label, node->node, NULL);
	node->child = port;
	CreateVideoPort(port, video->va_port_lst);
    }
    else {
	command = XtCreateManagedWidget("new", commandWidgetClass, box,
					NULL, 0);
	XtAddCallback(command, XtNcallback, NewVideoAdaptorCallback,
		      (XtPointer)node);
	label = XtVaCreateManagedWidget("value", asciiTextWidgetClass, box,
					XtNeditType, XawtextEdit,
1457
					NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
	node->data->video.text = label;
    }
    if (XtIsRealized(node->treeParent))
	XtRealizeWidget(box);
    XtManageChild(box);
}

/*ARGUSED*/
static void
VideoAdaptorDestroy(TreeNode *node)
{
    if (node->data->video.video) {
	int i;
	TreeNode *scrn = screenTree->child;

	/* last one is the "new" entry */
	while (scrn && scrn->next) {
	    /* VideoAdator is the first entry */
	    TreeNode *ad = scrn->child->child;
	    CompositeWidget composite;

	    while (ad && ad->next) {
		TreeNode *next = ad->next;

		if (ad && strcmp(ad->data->adaptorlink.adaptorlink->al_adaptor_str,
				  node->data->video.video->va_identifier) == 0)
		    DeleteNode(ad);
		ad = next;
	    }
	    composite = (CompositeWidget)ad->data->adaptorlink.menu;

	    for (i = 0; i < composite->composite.num_children; ++i)
		if (strcmp(XtName(composite->composite.children[i]),
			   node->data->video.video->va_identifier) == 0)
		    XtDestroyWidget(composite->composite.children[i]);

	    scrn = scrn->next;
	}

	xf86removeVideoAdaptor(XF86Config, node->data->video.video);
    }
}

/*ARGSUSED*/
static void
NewVideoAdaptorCallback(Widget unused, XtPointer user_data, XtPointer call_data)
{
    TreeNode *parent, *node = (TreeNode*)user_data;
    XF86ConfVideoAdaptorPtr video;
    Arg args[1];
    char *label;

    XtSetArg(args[0], XtNstring, &label);
    XtGetValues(node->data->video.text, args, 1);
    if (*label == '\0')
	return;

    parent = node->parent;
    DeleteNode(node);
    video = (XF86ConfVideoAdaptorPtr)
	XtCalloc(1, sizeof(XF86ConfVideoAdaptorRec));
    video->va_identifier = XtNewString(label);
    XF86Config->conf_videoadaptor_lst =
	xf86addVideoAdaptor(XF86Config->conf_videoadaptor_lst, video);

    {
	TreeNode *scrn = screenTree->child;
	Widget sme;

	/* last one is the "new" entry */
	while (scrn && scrn->next) {
	    /* VideoAdaptor is the first entry */
	    TreeNode *ad = scrn->child->child;

	    while (ad && ad->next)
		ad = ad->next;

	    sme = XtCreateManagedWidget(video->va_identifier,
					smeBSBObjectClass,
					ad->data->adaptorlink.menu, NULL, 0);
	    XtAddCallback(sme, XtNcallback, NewScreenAdaptorCallback,
			  (XtPointer)ad);

	    scrn = scrn->next;
	}
    }

    CreateVideoAdaptor(parent, video);
    RelayoutTree();
}

static void
VideoAdaptorUpdate(TreeNode *node)
{
    char *str;

    /* vendor */
1555
    XtVaGetValues(node->data->video.vendor, XtNstring, &str, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1556
1557
1558
1559
1560
1561
1562
    XtFree(node->data->video.video->va_vendor);
    if (*str)
	node->data->video.video->va_vendor = XtNewString(str);
    else
	node->data->video.video->va_vendor = NULL;

    /* board */
1563
    XtVaGetValues(node->data->video.board, XtNstring, &str, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1564
1565
1566
1567
1568
1569
1570
    XtFree(node->data->video.video->va_board);
    if (*str)
	node->data->video.video->va_board = XtNewString(str);
    else
	node->data->video.video->va_board = NULL;

    /* busid */
1571
    XtVaGetValues(node->data->video.busid, XtNstring, &str, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1572
1573
1574
1575
1576
1577
1578
    XtFree(node->data->video.video->va_busid);
    if (*str)
	node->data->video.video->va_busid = XtNewString(str);
    else
	node->data->video.video->va_busid = NULL;

    /* driver */
1579
    XtVaGetValues(node->data->video.driver, XtNstring, &str, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
    XtFree(node->data->video.video->va_driver);
    if (*str)
	node->data->video.video->va_driver = XtNewString(str);
    else
	node->data->video.video->va_driver = NULL;
}

static void
CreateVideoPort(TreeNode *parent, XF86ConfVideoPortPtr port)
{
    TreeNode *prev, *node;
    TreeData *data;

    if ((prev = parent->child) != NULL)
	while (prev->next)
	    prev = prev->next;

    while (port) {
	data = (TreeData*)XtCalloc(1, sizeof(TreeData));
	data->port.port = port;
	node = NewNode(parent, NULL, NULL, parent->node, data);
	node->destroy = VideoPortDestroy;
	CreateVideoPortField(node, False);
	if (parent->child == NULL)
	    parent->child = node;
	else
	    prev->next = node;
	prev = node;
	port = (XF86ConfVideoPortPtr)(port->list.next);
    }

    data = (TreeData*)XtCalloc(1, sizeof(TreeData));
    node = NewNode(parent, NULL, NULL, parent->node, data);
    CreateVideoPortField(node, True);
    if (parent->child == NULL)
	parent->child = node;
    else
	prev->next = node;
}

static void
CreateVideoPortField(TreeNode *node, Bool addnew)
{
    Widget box, command, label;

    box = XtVaCreateWidget("port", formWidgetClass, tree,
1626
			   XtNtreeParent, node->treeParent, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
    node->node = box;

    if (!addnew) {
	XF86OptionPtr *options;
	XF86ConfVideoPortPtr port = node->data->port.port;

	options = &(port->vp_option_lst);
	command = XtCreateManagedWidget("remove", commandWidgetClass, box,
					NULL, 0);
	XtAddCallback(command, XtNcallback, DestroyCallback, (XtPointer)node);
	command = XtCreateManagedWidget("options", commandWidgetClass, box,
					NULL, 0);
	XtAddCallback(command, XtNcallback, OptionsCallback, (XtPointer)options);
	label = XtVaCreateManagedWidget("label", labelWidgetClass, box,
1641
					XtNlabel, port->vp_identifier, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1642
1643
1644
1645
1646
1647
1648
    }
    else {
	command = XtCreateManagedWidget("new", commandWidgetClass, box,
					NULL, 0);
	XtAddCallback(command, XtNcallback, NewVideoPortCallback, (XtPointer)node);
	label = XtVaCreateManagedWidget("value", asciiTextWidgetClass, box,
					XtNeditType, XawtextEdit,
1649
					NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
	node->data->port.text = label;
    }
    if (XtIsRealized(node->treeParent))
	XtRealizeWidget(box);
    XtManageChild(box);
}

/*ARGUSED*/
static void
VideoPortDestroy(TreeNode *node)
{
    if (node->data->port.port)
	xf86removeVideoPort(node->parent->parent->data->video.video,
			    node->data->port.port);
}

/*ARGSUSED*/
static void
NewVideoPortCallback(Widget unused, XtPointer user_data, XtPointer call_data)
{
    TreeNode *video, *node = (TreeNode*)user_data;
    XF86ConfVideoPortPtr port;
    Arg args[1];
    char *label;

    XtSetArg(args[0], XtNstring, &label);
    XtGetValues(node->data->port.text, args, 1);
    if (*label == '\0')
	return;

    video = node->parent->parent;
    DeleteNode(node);
    port = (XF86ConfVideoPortPtr)XtCalloc(1, sizeof(XF86ConfVideoPortRec));
    port->vp_identifier = XtNewString(label);
    video->data->video.video->va_port_lst =
	xf86addVideoPort(video->data->video.video->va_port_lst, port);

    CreateVideoPort(video, port);
    RelayoutTree();
}

/* Monitor */
static void
CreateMonitor(TreeNode *parent, XF86ConfMonitorPtr mon)
{
    TreeNode *prev, *node;
    TreeData *data;

    if ((prev = parent->child) != NULL)
	while (prev->next)
	    prev = prev->next;

    while (mon) {
	data = (TreeData*)XtCalloc(1, sizeof(TreeData));
	data->monitor.monitor = mon;
	node = NewNode(parent, NULL, NULL, parent->node, data);
	node->destroy = MonitorDestroy;
	node->update = MonitorUpdate;
	CreateMonitorField(node, False);
	if (parent->child == NULL)
	    parent->child = node;
	else
	    prev->next = node;
	prev = node;
	mon = (XF86ConfMonitorPtr)(mon->list.next);
    }

    data = (TreeData*)XtCalloc(1, sizeof(TreeData));
    node = NewNode(parent, NULL, NULL, parent->node, data);
    CreateMonitorField(node, True);
    if (parent->child == NULL)
	parent->child = node;
    else
	prev->next = node;
}

static void
CreateMonitorField(TreeNode *node, Bool addnew)
{
    Widget box, command, label;

    box = XtVaCreateWidget("monitor", formWidgetClass, tree,
1732
			   XtNtreeParent, node->treeParent, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
    node->node = box;

    if (!addnew) {
	char *str, buf[256];
	XF86OptionPtr *options;
	XF86ConfMonitorPtr mon = node->data->monitor.monitor;
	Widget useModes;
	TreeNode *modeline, *modes, *prev;

	options = &(mon->mon_option_lst);
	command = XtCreateManagedWidget("remove", commandWidgetClass, box,
					NULL, 0);
	XtAddCallback(command, XtNcallback, DestroyCallback, (XtPointer)node);
	command = XtCreateManagedWidget("options", commandWidgetClass, box,
					NULL, 0);
	XtAddCallback(command, XtNcallback, OptionsCallback, (XtPointer)options);
	label = XtVaCreateManagedWidget("label", labelWidgetClass, box,
1750
					XtNlabel, mon->mon_identifier, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1751
1752
1753
1754
1755
1756
1757

	XtCreateManagedWidget("vendorL", labelWidgetClass, box, NULL, 0);
	str = mon->mon_vendor ? mon->mon_vendor : "";
	node->data->monitor.vendor =
		XtVaCreateManagedWidget("vendor", asciiTextWidgetClass, box,
					XtNeditType, XawtextEdit,
					XtNstring, str,
1758
					NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1759
1760
1761
1762
1763
1764
1765

	XtCreateManagedWidget("modelnameL", labelWidgetClass, box, NULL, 0);
	str = mon->mon_modelname ? mon->mon_modelname : "";
	node->data->monitor.model =
		XtVaCreateManagedWidget("modelname", asciiTextWidgetClass, box,
					XtNeditType, XawtextEdit,
					XtNstring, str,
1766
					NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776

	XtCreateManagedWidget("widthL", labelWidgetClass, box, NULL, 0);
	if (mon->mon_width)
	    XmuSnprintf(buf, sizeof(buf), "%d", mon->mon_width);
	else
	    *buf = '\0';
	node->data->monitor.width =
		XtVaCreateManagedWidget("width", asciiTextWidgetClass, box,
					XtNeditType, XawtextEdit,
					XtNstring, buf,
1777
					NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787

	XtCreateManagedWidget("heightL", labelWidgetClass, box, NULL, 0);
	if (mon->mon_height)
	    XmuSnprintf(buf, sizeof(buf), "%d", mon->mon_height);
	else
	    *buf = '\0';
	node->data->monitor.height =
		XtVaCreateManagedWidget("height", asciiTextWidgetClass, box,
					XtNeditType, XawtextEdit,
					XtNstring, buf,
1788
					NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799

	XtCreateManagedWidget("hsyncL", labelWidgetClass, box, NULL, 0);
	if (mon->mon_n_hsync > 0)
	    parser_range_to_string(buf, &(mon->mon_hsync[0]),
				   mon->mon_n_hsync);
	else
	    *buf = '\0';
	node->data->monitor.hsync =
		XtVaCreateManagedWidget("hsync", asciiTextWidgetClass, box,
					XtNeditType, XawtextEdit,
					XtNstring, buf,
1800
					NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811

	XtCreateManagedWidget("vrefreshL", labelWidgetClass, box, NULL, 0);
	if (mon->mon_n_vrefresh > 0)
	    parser_range_to_string(buf, &(mon->mon_vrefresh[0]),
				   mon->mon_n_vrefresh);
	else
	    *buf = '\0';
	node->data->monitor.vrefresh =
		XtVaCreateManagedWidget("vrefresh", asciiTextWidgetClass, box,
					XtNeditType, XawtextEdit,
					XtNstring, buf,
1812
					NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822

	XtCreateManagedWidget("gammaRedL", labelWidgetClass, box, NULL, 0);
	if (mon->mon_gamma_red)	
	    XmuSnprintf(buf, sizeof(buf), "%g", mon->mon_gamma_red);
	else
	    *buf = '\0';
	node->data->monitor.gammaRed =
		XtVaCreateManagedWidget("gammaRed", asciiTextWidgetClass, box,
					XtNeditType, XawtextEdit,
					XtNstring, buf,
1823
					NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833

	XtCreateManagedWidget("gammaGreenL", labelWidgetClass, box, NULL, 0);
	if (mon->mon_gamma_green)
	    XmuSnprintf(buf, sizeof(buf), "%g", mon->mon_gamma_green);
	else
	    *buf = '\0';
	node->data->monitor.gammaGreen =
		XtVaCreateManagedWidget("gammaGreen", asciiTextWidgetClass, box,
					XtNeditType, XawtextEdit,
					XtNstring, buf,
1834
					NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844

	XtCreateManagedWidget("gammaBlueL", labelWidgetClass, box, NULL, 0);
	if (mon->mon_gamma_blue)
	    XmuSnprintf(buf, sizeof(buf), "%g", mon->mon_gamma_blue);
	else
	    *buf = '\0';
	node->data->monitor.gammaBlue =
		XtVaCreateManagedWidget("gammaBlue", asciiTextWidgetClass, box,
					XtNeditType, XawtextEdit,
					XtNstring, buf,
1845
					NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1846
1847
1848
1849
1850
1851

	if ((prev = node->child) != NULL)
	    while (prev->next)
		prev = prev->next;
	command = XtVaCreateManagedWidget("ModeLine", toggleWidgetClass, tree,
					  XtNstate, True,
1852
					  XtNtreeParent, box, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
	modeline = NewNode(node, command, command, node->node, NULL);
	CreateMonitorModeLine(modeline,
			      node->data->monitor.monitor->mon_modeline_lst);
	if (prev == NULL)
	    prev = node->child = modeline;
	else {
	    prev->next = modeline;
	    prev = prev->next;
	}

	useModes = XtVaCreateManagedWidget("UseModes", toggleWidgetClass, tree,
					   XtNstate, True,
1865
					   XtNtreeParent, box, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
	prev->next = modes = NewNode(node, useModes, useModes, node->node, NULL);
	CreateMonitorModes(modes,
			   node->data->monitor.monitor->mon_modes_sect_lst);
    }
    else {
	command = XtCreateManagedWidget("new", commandWidgetClass, box,
					NULL, 0);
	XtAddCallback(command, XtNcallback, NewMonitorCallback, (XtPointer)node);
	label = XtVaCreateManagedWidget("value", asciiTextWidgetClass, box,
					XtNeditType, XawtextEdit,
1876
					NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
	node->data->monitor.text = label;
    }
    if (XtIsRealized(node->treeParent))
	XtRealizeWidget(box);
    XtManageChild(box);
}

static void
MonitorDestroy(TreeNode *node)
{
    int i;
    TreeNode *sc = screenTree;

    for (i = 0; i < computer.num_devices; i++)
	if ((XF86ConfMonitorPtr)(computer.devices[i]->config) ==
	    node->data->monitor.monitor) {
	    config = computer.devices[i]->widget;
	    RemoveDeviceCallback(NULL, NULL, NULL);
	}

    if (sc) {
	TreeNode *prev;

	sc = prev = sc->child;
	while (sc->next) {
	    TreeNode *next = sc->next;

	    if (sc->data->screen.screen->scrn_monitor ==
		node->data->monitor.monitor) {
		XtDestroyWidget(sc->node);

		if (sc->child)
		    DestroyTree(sc->child);
		if (sc->data)
		    XtFree((XtPointer)sc->data);
		XtFree((XtPointer)sc);

		if (sc == screenTree->child)
		    sc = prev = next = screenTree->child = next;
		else
		    prev->next = sc = next;
		continue;
	    }
	    prev = sc;
	    sc = next;
	}
    }
}

static void
NewMonitorCallback(Widget w, XtPointer user_data, XtPointer call_data)
{
    TreeNode *parent, *node = (TreeNode*)user_data;
    XF86ConfMonitorPtr mon;
    Arg args[1];
    char *label;

    XtSetArg(args[0], XtNstring, &label);
    XtGetValues(node->data->monitor.text, args, 1);
    if (*label == '\0')
	return;

    parent = node->parent;
    DeleteNode(node);
    mon = (XF86ConfMonitorPtr)XtCalloc(1, sizeof(XF86ConfMonitorRec));
    mon->mon_identifier = XtNewString(label);
    XF86Config->conf_monitor_lst =
	xf86addMonitor(XF86Config->conf_monitor_lst, mon);

    CreateMonitor(parent, mon);

    RelayoutTree();
}

static void
MonitorUpdate(TreeNode *node)
{
    char *str;

    /* vendor */
1957
    XtVaGetValues(node->data->monitor.vendor, XtNstring, &str, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1958
1959
1960
1961
1962
1963
1964
    XtFree(node->data->monitor.monitor->mon_vendor);
    if (*str)
	node->data->monitor.monitor->mon_vendor = XtNewString(str);
    else
	node->data->monitor.monitor->mon_vendor = NULL;

    /* model */
1965
    XtVaGetValues(node->data->monitor.model, XtNstring, &str, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1966
1967
1968
1969
1970
1971
1972
    XtFree(node->data->monitor.monitor->mon_modelname);
    if (*str)
	node->data->monitor.monitor->mon_modelname = XtNewString(str);
    else
	node->data->monitor.monitor->mon_modelname = NULL;

    /* width */
1973
    XtVaGetValues(node->data->monitor.width, XtNstring, &str, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1974
1975
1976
    node->data->monitor.monitor->mon_width = strtoul(str, NULL, 0);

    /* height */
1977
    XtVaGetValues(node->data->monitor.height, XtNstring, &str, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1978
1979
1980
    node->data->monitor.monitor->mon_height = strtoul(str, NULL, 0);

    /* hsync */
1981
    XtVaGetValues(node->data->monitor.hsync, XtNstring, &str, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1982
1983
1984
1985
1986
1987
    node->data->monitor.monitor->mon_n_hsync =
	string_to_parser_range(str,
			       &(node->data->monitor.monitor->mon_hsync[0]),
			       CONF_MAX_HSYNC);

    /* vrefresh */
1988
    XtVaGetValues(node->data->monitor.vrefresh, XtNstring, &str, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1989
1990
1991
1992
1993
1994
    node->data->monitor.monitor->mon_n_vrefresh =
	string_to_parser_range(str,
			       &(node->data->monitor.monitor->mon_vrefresh[0]),
			       CONF_MAX_VREFRESH);

    /* gammaRed */
1995
    XtVaGetValues(node->data->monitor.gammaRed, XtNstring, &str, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1996
1997
1998
    node->data->monitor.monitor->mon_gamma_red = strtod(str, NULL);

    /* gammaGreen */
1999
    XtVaGetValues(node->data->monitor.gammaGreen, XtNstring, &str, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
2000
2001
2002
    node->data->monitor.monitor->mon_gamma_green = strtod(str, NULL);

    /* gammaBlue */
2003
    XtVaGetValues(node->data->monitor.gammaBlue, XtNstring, &str, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
    node->data->monitor.monitor->mon_gamma_blue = strtod(str, NULL);
}

static void
CreateMonitorModeLine(TreeNode *parent, XF86ConfModeLinePtr modeline)
{
    TreeNode *node, *prev;
    TreeData *data;

    if ((prev = parent->child) != NULL)
	while (prev->next)
	    prev = prev->next;

    while (modeline) {
	data = (TreeData*)XtCalloc(1, sizeof(TreeData));
	data->modeline.modeline = modeline;
	node = NewNode(parent, NULL, NULL, parent->node, data);
	node->destroy = MonitorModeLineDestroy;
	CreateModeLineField(node, False, True);
	if (parent->child == NULL)
	    parent->child = node;
	else
	    prev->next = node;
	prev = node;
	modeline = (XF86ConfModeLinePtr)(modeline->list.next);
    }
    data = (TreeData*)XtCalloc(1, sizeof(TreeData));
    node = NewNode(parent, NULL, NULL, parent->node, data);
    if (parent->child == NULL)
	parent->child = node;
    else
	prev->next = node;
    prev = node;
    CreateModeLineField(node, True, True);
}

/*ARGUSED*/
static void
MonitorModeLineDestroy(TreeNode *node)
{
    if (node->data->modeline.modeline)
	xf86removeMonitorModeLine(node->parent->parent->data->monitor.monitor,
				  node->data->modeline.modeline);
}

/*ARGSUSED*/
static void
NewMonitorModeLineCallback(Widget w, XtPointer user_data, XtPointer call_data)
{
    TreeNode *parent, *node = (TreeNode*)user_data;
    XF86ConfModeLinePtr modeline;
    Arg args[1];
    char *ident, *value;

    XtSetArg(args[0], XtNstring, &ident);
    XtGetValues(node->data->modeline.text, args, 1);
    XtSetArg(args[0], XtNstring, &value);
    XtGetValues(node->data->modeline.value, args, 1);
    if (*ident == '\0' || *value == '\0')
	return;

    parent = node->parent;
    DeleteNode(node);
    modeline = ParseModeLine(ident, value);
    parent->parent->data->monitor.monitor->mon_modeline_lst =
	xf86addModeLine(parent->parent->data->monitor.monitor->mon_modeline_lst,
			modeline);

    CreateMonitorModeLine(parent, modeline);
    RelayoutTree();
}

static void
CreateMonitorModes(TreeNode *parent, XF86ConfModesLinkPtr lnk)
{
    TreeNode *node, *prev;
    TreeData *data;

    if ((prev = parent->child) != NULL)
	while (prev->next)
	    prev = prev->next;

    while (lnk) {
	data = (TreeData*)XtCalloc(1, sizeof(TreeData));
	data->modeslink.modeslink = lnk;
	node = NewNode(parent, NULL, NULL, parent->node, data);
	node->destroy = MonitorModesLinkDestroy;
	CreateMonitorModesField(node, False);
	if (parent->child == NULL)
	    parent->child = node;
	else
	    prev->next = node;
	prev = node;
	lnk = (XF86ConfModesLinkPtr)(lnk->list.next);
    }
    data = (TreeData*)XtCalloc(1, sizeof(TreeData));
    node = NewNode(parent, NULL, NULL, parent->node<