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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
|
/*
*
* Copyright (c) 1997 Metro Link Incorporated
*
* 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
* THE X CONSORTIUM 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 the Metro Link shall not be
* used in advertising or otherwise to promote the sale, use or other dealings
* in this Software without prior written authorization from Metro Link.
*
*/
#include "xf86Parser.h"
#include "xf86tokens.h"
#include "Configint.h"
/*
* xconfigAddRemovedOptionComment() - Makes a note in the comment
* string "existing_comments" that a particular option has been
* removed.
*
*/
static void xconfigAddRemovedOptionComment(char **existing_comments,
XConfigOptionPtr option)
{
int len;
char *str;
char *name, *value;
if (!option || !existing_comments)
return;
name = xconfigOptionName(option);
value = xconfigOptionValue(option);
if (!name) return;
if (value) {
len = 32 + strlen(name) + strlen(value);
str = malloc(len);
if (!str) return;
snprintf(str, len, "# Removed Option \"%s\" \"%s\"", name, value);
} else {
len = 32 + strlen(name);
str = malloc(len);
if (!str) return;
snprintf(str, len, "# Removed Option \"%s\"", name);
}
*existing_comments = xconfigAddComment(*existing_comments, str);
} /* xconfigAddRemovedOptionComment() */
/*
* xconfigRemoveNamedOption() - Removes the named option from an option
* list and (if specified) adds a comment to an existing comments string
*
*/
static void xconfigRemoveNamedOption(XConfigOptionPtr *head, char *name,
char **comments)
{
XConfigOptionPtr option;
option = xconfigFindOption(*head, name);
if (option) {
if (comments) {
xconfigAddRemovedOptionComment(comments, option);
}
*head = xconfigRemoveOption(*head, option);
}
} /* xconfigRemoveNamedOption() */
/*
* xconfigOptionValuesDiffer() - return '1' if the option values for
* option0 and option1 are different; return '0' if the option values
* are the same.
*/
static int xconfigOptionValuesDiffer(XConfigOptionPtr option0,
XConfigOptionPtr option1)
{
char *value0, *value1;
value0 = value1 = NULL;
if (!option0 && !option1) return 0;
if (!option0 && option1) return 1;
if ( option0 && !option1) return 1;
value0 = xconfigOptionValue(option0);
value1 = xconfigOptionValue(option1);
if (!value0 && !value1) return 0;
if (!value0 && value1) return 1;
if ( value0 && !value1) return 1;
return (strcmp(value0, value1) != 0);
} /* xconfigOptionValuesDiffer() */
/*
* xconfigMergeOption() - Merge option "name" from option source
* list "srcHead" to option destination list "dstHead".
*
* Merging here means:
*
* If the option is not in the source config, remove it from the dest
* config.
*
* If the option is in the source config, make sure the dest config
* contains the option with the same value as the source config.
*
* if "comments" is given, a comment will be added to note when
* an option has been removed/replaced.
*
*/
static void xconfigMergeOption(XConfigOptionPtr *dstHead,
XConfigOptionPtr *srcHead,
const char *name, char **comments)
{
XConfigOptionPtr srcOption = xconfigFindOption(*srcHead, name);
XConfigOptionPtr dstOption = xconfigFindOption(*dstHead, name);
char *srcValue = NULL;
if (srcOption) srcValue = xconfigOptionValue(srcOption);
if (!srcOption && dstOption) {
/* option does not exist in src, but exists in dst: remove from dst */
*dstHead = xconfigRemoveOption(*dstHead, dstOption);
} else if (srcOption && !dstOption) {
/* option exists in src but not in dst: add to dst */
*dstHead = xconfigAddNewOption(*dstHead, name, srcValue);
} else if (srcOption && dstOption) {
/*
* option exists in src and in dst; if the option values are
* different, replace the dst's option value with src's option
* value; note that xconfigAddNewOption() will remove the old
* option first, if necessary
*/
if (xconfigOptionValuesDiffer(srcOption, dstOption)) {
if (comments) {
xconfigAddRemovedOptionComment(comments, dstOption);
}
*dstHead = xconfigAddNewOption(*dstHead, name, srcValue);
}
}
} /* xconfigMergeOption() */
/*
* xconfigMergeFlags() - Updates the destination's list of server flag
* options with the options found in the source config.
*
* Optons in the destination are either added or updated. Options that
* are found in the destination config and not in the source config are
* not modified.
*
* Returns 1 if the merge was successful and 0 if not.
*/
static int xconfigMergeFlags(XConfigPtr dstConfig, XConfigPtr srcConfig)
{
if (srcConfig->flags) {
XConfigOptionPtr option;
/* Flag section was not found, create a new one */
if (!dstConfig->flags) {
dstConfig->flags =
(XConfigFlagsPtr) calloc(1, sizeof(XConfigFlagsRec));
if (!dstConfig->flags) return 0;
}
option = srcConfig->flags->options;
while (option) {
xconfigMergeOption(&(dstConfig->flags->options),
&(srcConfig->flags->options),
xconfigOptionName(option),
&(dstConfig->flags->comment));
option = option->next;
}
}
return 1;
} /* xconfigMergeFlags() */
/*
* xconfigMergeMonitors() - Updates information in the destination monitor
* with that of the source monitor.
*
*/
static void xconfigMergeMonitors(XConfigMonitorPtr dstMonitor,
XConfigMonitorPtr srcMonitor)
{
int i;
/* Update vendor */
free(dstMonitor->vendor);
dstMonitor->vendor = xconfigStrdup(srcMonitor->vendor);
/* Update modelname */
free(dstMonitor->modelname);
dstMonitor->modelname = xconfigStrdup(srcMonitor->modelname);
/* Update horizontal sync */
dstMonitor->n_hsync = srcMonitor->n_hsync;
for (i = 0; i < srcMonitor->n_hsync; i++) {
dstMonitor->hsync[i].lo = srcMonitor->hsync[i].lo;
dstMonitor->hsync[i].hi = srcMonitor->hsync[i].hi;
}
/* Update vertical sync */
dstMonitor->n_vrefresh = srcMonitor->n_vrefresh;
for (i = 0; i < srcMonitor->n_hsync; i++) {
dstMonitor->vrefresh[i].lo = srcMonitor->vrefresh[i].lo;
dstMonitor->vrefresh[i].hi = srcMonitor->vrefresh[i].hi;
}
/* XXX Remove the destination monitor's "UseModes" references to
* avoid having the wrong modelines tied to the new monitor.
*/
xconfigFreeModesLinkList(dstMonitor->modes_sections);
dstMonitor->modes_sections = NULL;
} /* xconfigMergeMonitors() */
/*
* xconfigMergeAllMonitors() - This function ensures that all monitors in
* the source config appear in the destination config by adding and/or
* updating the "appropriate" destination monitor sections.
*
*/
static int xconfigMergeAllMonitors(XConfigPtr dstConfig, XConfigPtr srcConfig)
{
XConfigMonitorPtr dstMonitor;
XConfigMonitorPtr srcMonitor;
/* Make sure all monitors in the src config are also in the dst config */
for (srcMonitor = srcConfig->monitors;
srcMonitor;
srcMonitor = srcMonitor->next) {
dstMonitor =
xconfigFindMonitor(srcMonitor->identifier, dstConfig->monitors);
/* Monitor section was not found, create a new one and add it */
if (!dstMonitor) {
dstMonitor =
(XConfigMonitorPtr) calloc(1, sizeof(XConfigMonitorRec));
if (!dstMonitor) return 0;
dstMonitor->identifier = xconfigStrdup(srcMonitor->identifier);
dstConfig->monitors = (XConfigMonitorPtr)
xconfigAddListItem((GenericListPtr)dstConfig->monitors,
(GenericListPtr)dstMonitor);
}
/* Do the merge */
xconfigMergeMonitors(dstMonitor, srcMonitor);
}
return 1;
} /* xconfigMergeAllMonitors() */
/*
* xconfigMergeDevices() - Updates information in the destination device
* with that of the source device.
*
*/
static void xconfigMergeDevices(XConfigDevicePtr dstDevice,
XConfigDevicePtr srcDevice)
{
// XXX Zero out the device section?
/* Update driver */
free(dstDevice->driver);
dstDevice->driver = xconfigStrdup(srcDevice->driver);
/* Update vendor */
free(dstDevice->vendor);
dstDevice->vendor = xconfigStrdup(srcDevice->vendor);
/* Update bus ID */
free(dstDevice->busid);
dstDevice->busid = xconfigStrdup(srcDevice->busid);
/* Update board */
free(dstDevice->board);
dstDevice->board = xconfigStrdup(srcDevice->board);
/* Update chip info */
dstDevice->chipid = srcDevice->chipid;
dstDevice->chiprev = srcDevice->chiprev;
/* Update IRQ */
dstDevice->irq = srcDevice->irq;
/* Update screen */
dstDevice->screen = srcDevice->screen;
} /* xconfigMergeDevices() */
/*
* xconfigMergeAllDevices() - This function ensures that all devices in
* the source config appear in the destination config by adding and/or
* updating the "appropriate" destination device sections.
*
*/
static int xconfigMergeAllDevices(XConfigPtr dstConfig, XConfigPtr srcConfig)
{
XConfigDevicePtr dstDevice;
XConfigDevicePtr srcDevice;
/* Make sure all monitors in the src config are also in the dst config */
for (srcDevice = srcConfig->devices;
srcDevice;
srcDevice = srcDevice->next) {
dstDevice =
xconfigFindDevice(srcDevice->identifier, dstConfig->devices);
/* Device section was not found, create a new one and add it */
if (!dstDevice) {
dstDevice =
(XConfigDevicePtr) calloc(1, sizeof(XConfigDeviceRec));
if (!dstDevice) return 0;
dstDevice->identifier = xconfigStrdup(srcDevice->identifier);
dstConfig->devices = (XConfigDevicePtr)
xconfigAddListItem((GenericListPtr)dstConfig->devices,
(GenericListPtr)dstDevice);
}
/* Do the merge */
xconfigMergeDevices(dstDevice, srcDevice);
}
return 1;
} /* xconfigMergeAllDevices() */
/*
* xconfigMergeDriverOptions() - Update the (Screen) driver options
* of the destination config with information from the source config.
*
* - Assumes the source options are all found in the srcScreen->options.
* - Updates only those options listed in the srcScreen->options.
*
*/
static int xconfigMergeDriverOptions(XConfigScreenPtr dstScreen,
XConfigScreenPtr srcScreen)
{
XConfigOptionPtr option;
XConfigDisplayPtr display;
option = srcScreen->options;
while (option) {
char *name = xconfigOptionName(option);
/* Remove the option from all non-screen option lists */
if (dstScreen->device) {
xconfigRemoveNamedOption(&(dstScreen->device->options), name,
&(dstScreen->device->comment));
}
if (dstScreen->monitor) {
xconfigRemoveNamedOption(&(dstScreen->monitor->options), name,
&(dstScreen->monitor->comment));
}
for (display = dstScreen->displays; display; display = display->next) {
xconfigRemoveNamedOption(&(display->options), name,
&(display->comment));
}
/* Update/Add the option to the screen's option list */
{
// XXX Only add a comment if the value changed.
XConfigOptionPtr old =
xconfigFindOption(dstScreen->options, name);
if (old && xconfigOptionValuesDiffer(option, old)) {
xconfigRemoveNamedOption(&(dstScreen->options), name,
&(dstScreen->comment));
} else {
xconfigRemoveNamedOption(&(dstScreen->options), name,
NULL);
}
}
/* Add the option to the screen->options list */
dstScreen->options =
xconfigAddNewOption(dstScreen->options,
name, xconfigOptionValue(option));
option = option->next;
}
return 1;
} /* xconfigMergeDriverOptions() */
/*
* xconfigMergeDisplays() - Duplicates display information from the
* source screen to the destination screen.
*
*/
static int xconfigMergeDisplays(XConfigScreenPtr dstScreen,
XConfigScreenPtr srcScreen)
{
XConfigDisplayPtr dstDisplay;
XConfigDisplayPtr srcDisplay;
XConfigModePtr srcMode, dstMode, lastDstMode;
/* Free all the displays in the destination screen */
xconfigFreeDisplayList(dstScreen->displays);
/* Copy all te displays */
for (srcDisplay = srcScreen->displays;
srcDisplay;
srcDisplay = srcDisplay->next) {
/* Create a new display */
dstDisplay = xconfigAlloc(sizeof(XConfigDisplayRec));
if (!dstDisplay) return 0;
/* Copy display fields */
dstDisplay->frameX0 = srcDisplay->frameX0;
dstDisplay->frameY0 = srcDisplay->frameY0;
dstDisplay->virtualX = srcDisplay->virtualX;
dstDisplay->virtualY = srcDisplay->virtualY;
dstDisplay->depth = srcDisplay->depth;
dstDisplay->bpp = srcDisplay->bpp;
dstDisplay->visual = xconfigStrdup(srcDisplay->visual);
dstDisplay->weight = srcDisplay->weight;
dstDisplay->black = srcDisplay->black;
dstDisplay->white = srcDisplay->white;
dstDisplay->comment = xconfigStrdup(srcDisplay->comment);
/* Copy options over */
dstDisplay->options = xconfigOptionListDup(srcDisplay->options);
/* Copy modes over */
lastDstMode = NULL;
srcMode = srcDisplay->modes;
while (srcMode) {
/* Copy the mode */
dstMode = xconfigAddMode(NULL, srcMode->mode_name);
/* Add mode at the end of the list */
if ( !lastDstMode ) {
dstDisplay->modes = dstMode;
} else {
lastDstMode->next = dstMode;
}
lastDstMode = dstMode;
srcMode = srcMode->next;
}
}
return 1;
} /* xconfigMergeDisplays() */
/*
* xconfigMergeScreens() - Updates information in the destination screen
* with that of the source screen.
*
* NOTE: This assumes the Monitor and Device sections have already been
* merged.
*
*/
static void xconfigMergeScreens(XConfigScreenPtr dstScreen,
XConfigPtr dstConfig,
XConfigScreenPtr srcScreen,
XConfigPtr srcConfig)
{
/* Use the right device */
free(dstScreen->device_name);
dstScreen->device_name = xconfigStrdup(srcScreen->device_name);
dstScreen->device =
xconfigFindDevice(dstScreen->device_name, dstConfig->devices);
/* Use the right monitor */
free(dstScreen->monitor_name);
dstScreen->monitor_name = xconfigStrdup(srcScreen->monitor_name);
dstScreen->monitor =
xconfigFindMonitor(dstScreen->monitor_name, dstConfig->monitors);
/* Update the right default depth */
dstScreen->defaultdepth = srcScreen->defaultdepth;
/* Copy over the display section */
xconfigMergeDisplays(dstScreen, srcScreen);
/* Update the screen's driver options */
xconfigMergeDriverOptions(dstScreen, srcScreen);
} /* xconfigMergeScreens() */
/*
* xconfigMergeAllScreens() - This function ensures that all screens in
* the source config appear in the destination config by adding and/or
* updating the "appropriate" destination screen sections.
*
*/
static int xconfigMergeAllScreens(XConfigPtr dstConfig, XConfigPtr srcConfig)
{
XConfigScreenPtr srcScreen;
XConfigScreenPtr dstScreen;
/* Make sure all src screens are in the dst config */
for (srcScreen = srcConfig->screens;
srcScreen;
srcScreen = srcScreen->next) {
dstScreen =
xconfigFindScreen(srcScreen->identifier, dstConfig->screens);
/* Screen section was not found, create a new one and add it */
if (!dstScreen) {
dstScreen =
(XConfigScreenPtr) calloc(1, sizeof(XConfigScreenRec));
if (!dstScreen) return 0;
dstScreen->identifier = xconfigStrdup(srcScreen->identifier);
dstConfig->screens = (XConfigScreenPtr)
xconfigAddListItem((GenericListPtr)dstConfig->screens,
(GenericListPtr)dstScreen);
}
/* Do the merge */
xconfigMergeScreens(dstScreen, dstConfig, srcScreen, srcConfig);
}
return 1;
} /* xconfigMergeAllScreens() */
/*
* xconfigMergeLayout() - Updates information in the destination's first
* layout with that of the source's first layout.
*
*/
static int xconfigMergeLayout(XConfigPtr dstConfig, XConfigPtr srcConfig)
{
XConfigLayoutPtr srcLayout = srcConfig->layouts;
XConfigLayoutPtr dstLayout = dstConfig->layouts;
XConfigAdjacencyPtr srcAdj;
XConfigAdjacencyPtr dstAdj;
XConfigAdjacencyPtr lastDstAdj;
/* Clear the destination's adjacency list */
xconfigFreeAdjacencyList(dstLayout->adjacencies);
/* Copy adjacencies over */
lastDstAdj = NULL;
srcAdj = srcLayout->adjacencies;
while (srcAdj) {
/* Copy the adjacency */
dstAdj =
(XConfigAdjacencyPtr) calloc(1, sizeof(XConfigAdjacencyRec));
dstAdj->scrnum = srcAdj->scrnum;
dstAdj->screen_name = xconfigStrdup(srcAdj->screen_name);
dstAdj->top_name = xconfigStrdup(srcAdj->top_name);
dstAdj->bottom_name = xconfigStrdup(srcAdj->bottom_name);
dstAdj->left_name = xconfigStrdup(srcAdj->left_name);
dstAdj->right_name = xconfigStrdup(srcAdj->right_name);
dstAdj->where = srcAdj->where;
dstAdj->x = srcAdj->x;
dstAdj->y = srcAdj->y;
dstAdj->refscreen = xconfigStrdup(srcAdj->refscreen);
dstAdj->screen =
xconfigFindScreen(dstAdj->screen_name, dstConfig->screens);
dstAdj->top =
xconfigFindScreen(dstAdj->top_name, dstConfig->screens);
dstAdj->bottom =
xconfigFindScreen(dstAdj->bottom_name, dstConfig->screens);
dstAdj->left =
xconfigFindScreen(dstAdj->left_name, dstConfig->screens);
dstAdj->right =
xconfigFindScreen(dstAdj->right_name, dstConfig->screens);
/* Add adjacency at the end of the list */
if ( !lastDstAdj ) {
dstLayout->adjacencies = dstAdj;
} else {
lastDstAdj->next = dstAdj;
}
lastDstAdj = dstAdj;
srcAdj = srcAdj->next;
}
return 1;
} /* xconfigMergeLayout() */
/*
* xconfigMergeConfigs() - Merges the source X configuration with the
* destination X configuration.
*
* NOTE: This function is currently only used for merging X config files
* for display configuration reasons. As such, the merge assumes
* that the dst config file is the target config file and that
* mostly, only new display configuration information should be
* copied from the source X config to the destination X config.
*
*/
int xconfigMergeConfigs(XConfigPtr dstConfig, XConfigPtr srcConfig)
{
/* Make sure the X config is falid */
// make_xconfig_usable(dstConfig);
/* Merge the server flag (Xinerama) section */
if (!xconfigMergeFlags(dstConfig, srcConfig)) {
return 0;
}
/* Merge the monitor sections */
if (!xconfigMergeAllMonitors(dstConfig, srcConfig)) {
return 0;
}
/* Merge the device sections */
if (!xconfigMergeAllDevices(dstConfig, srcConfig)) {
return 0;
}
/* Merge the screen sections */
if (!xconfigMergeAllScreens(dstConfig, srcConfig)) {
return 0;
}
/* Merge the first layout */
if (!xconfigMergeLayout(dstConfig, srcConfig)) {
return 0;
}
return 1;
} /* xconfigMergeConfigs() */
|