summaryrefslogtreecommitdiff
path: root/src/i830_randr.c
blob: 38ac320f0db28db784f1ac46acbf22b852a46ef6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
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
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
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
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
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
/* $XdotOrg: xc/programs/Xserver/hw/xfree86/common/xf86RandR.c,v 1.3 2004/07/30 21:53:09 eich Exp $ */
/*
 * $XFree86: xc/programs/Xserver/hw/xfree86/common/xf86RandR.c,v 1.7tsi Exp $
 *
 * Copyright © 2002 Keith Packard, member of The XFree86 Project, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Keith Packard not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Keith Packard makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "xf86.h"
#include "os.h"
#include "mibank.h"
#include "globals.h"
#include "xf86.h"
#include "xf86Priv.h"
#include "xf86DDC.h"
#include "mipointer.h"
#include "windowstr.h"
#include <randrstr.h>
#include <X11/extensions/render.h>

#include "i830_xf86Crtc.h"
#include "i830_randr.h"
#include "i830_display.h"
#include "i830.h"

typedef struct _xf86RandRInfo {
    int				    virtualX;
    int				    virtualY;
    int				    mmWidth;
    int				    mmHeight;
    int				    maxX;
    int				    maxY;
    Rotation			    rotation; /* current mode */
    Rotation                        supported_rotations; /* driver supported */
} XF86RandRInfoRec, *XF86RandRInfoPtr;

#ifdef RANDR_12_INTERFACE
static Bool xf86RandRInit12 (ScreenPtr pScreen);
static Bool xf86RandRCreateScreenResources12 (ScreenPtr pScreen);
#endif

static int	    xf86RandRIndex;
static int	    xf86RandRGeneration;

#define XF86RANDRINFO(p) \
	((XF86RandRInfoPtr)(p)->devPrivates[xf86RandRIndex].ptr)

static int
xf86RandRModeRefresh (DisplayModePtr mode)
{
    if (mode->VRefresh)
	return (int) (mode->VRefresh + 0.5);
    else
	return (int) (mode->Clock * 1000.0 / mode->HTotal / mode->VTotal + 0.5);
}

static Bool
xf86RandRGetInfo (ScreenPtr pScreen, Rotation *rotations)
{
    RRScreenSizePtr	    pSize;
    ScrnInfoPtr		    scrp = XF86SCRNINFO(pScreen);
    XF86RandRInfoPtr	    randrp = XF86RANDRINFO(pScreen);
    DisplayModePtr	    mode;
    int			    refresh0 = 60;
    int			    maxX = 0, maxY = 0;

    *rotations = randrp->supported_rotations;

    if (randrp->virtualX == -1 || randrp->virtualY == -1)
    {
	randrp->virtualX = scrp->virtualX;
	randrp->virtualY = scrp->virtualY;
    }

    /* Re-probe the outputs for new monitors or modes */
    I830ValidateXF86ModeList(scrp, FALSE);

    for (mode = scrp->modes; ; mode = mode->next)
    {
	int refresh = xf86RandRModeRefresh (mode);
	if (randrp->maxX == 0 || randrp->maxY == 0)
	{
		if (maxX < mode->HDisplay)
			maxX = mode->HDisplay;
		if (maxY < mode->VDisplay)
			maxY = mode->VDisplay;
	}
	if (mode == scrp->modes)
	    refresh0 = refresh;
	pSize = RRRegisterSize (pScreen,
				mode->HDisplay, mode->VDisplay,
				randrp->mmWidth, randrp->mmHeight);
	if (!pSize)
	    return FALSE;
	RRRegisterRate (pScreen, pSize, refresh);

	if (I830ModesEqual(mode, scrp->currentMode) &&
	    mode->HDisplay == scrp->virtualX &&
	    mode->VDisplay == scrp->virtualY)
	{
	    RRSetCurrentConfig (pScreen, randrp->rotation, refresh, pSize);
	}
	if (mode->next == scrp->modes)
	    break;
    }

    if (randrp->maxX == 0 || randrp->maxY == 0)
    {
	randrp->maxX = maxX;
	randrp->maxY = maxY;
    }

    if (scrp->currentMode->HDisplay != randrp->virtualX ||
	scrp->currentMode->VDisplay != randrp->virtualY)
    {
	pSize = RRRegisterSize (pScreen,
				randrp->virtualX, randrp->virtualY,
				randrp->mmWidth,
				randrp->mmHeight);
	if (!pSize)
	    return FALSE;
	RRRegisterRate (pScreen, pSize, refresh0);
	if (scrp->virtualX == randrp->virtualX &&
	    scrp->virtualY == randrp->virtualY)
	{
	    RRSetCurrentConfig (pScreen, randrp->rotation, refresh0, pSize);
	}
    }

    return TRUE;
}

static Bool
xf86RandRSetMode (ScreenPtr	    pScreen,
		  DisplayModePtr    mode,
		  Bool		    useVirtual,
		  int		    mmWidth,
		  int		    mmHeight)
{
    ScrnInfoPtr		scrp = XF86SCRNINFO(pScreen);
    XF86RandRInfoPtr	randrp = XF86RANDRINFO(pScreen);
    int			oldWidth = pScreen->width;
    int			oldHeight = pScreen->height;
    int			oldmmWidth = pScreen->mmWidth;
    int			oldmmHeight = pScreen->mmHeight;
    WindowPtr		pRoot = WindowTable[pScreen->myNum];
    DisplayModePtr      currentMode = NULL;
    Bool 		ret = TRUE;
    PixmapPtr 		pspix = NULL;

    if (pRoot)
	(*scrp->EnableDisableFBAccess) (pScreen->myNum, FALSE);
    if (useVirtual)
    {
	scrp->virtualX = randrp->virtualX;
	scrp->virtualY = randrp->virtualY;
    }
    else
    {
	scrp->virtualX = mode->HDisplay;
	scrp->virtualY = mode->VDisplay;
    }

    if(randrp->rotation & (RR_Rotate_90 | RR_Rotate_270))
    {
	/* If the screen is rotated 90 or 270 degrees, swap the sizes. */
	pScreen->width = scrp->virtualY;
	pScreen->height = scrp->virtualX;
	pScreen->mmWidth = mmHeight;
	pScreen->mmHeight = mmWidth;
    }
    else
    {
	pScreen->width = scrp->virtualX;
	pScreen->height = scrp->virtualY;
	pScreen->mmWidth = mmWidth;
	pScreen->mmHeight = mmHeight;
    }
    if (scrp->currentMode == mode) {
        /* Save current mode */
        currentMode = scrp->currentMode;
        /* Reset, just so we ensure the drivers SwitchMode is called */
        scrp->currentMode = NULL;
    }
    /*
     * We know that if the driver failed to SwitchMode to the rotated
     * version, then it should revert back to it's prior mode.
     */
    if (!xf86SwitchMode (pScreen, mode))
    {
        ret = FALSE;
	scrp->virtualX = pScreen->width = oldWidth;
	scrp->virtualY = pScreen->height = oldHeight;
	pScreen->mmWidth = oldmmWidth;
	pScreen->mmHeight = oldmmHeight;
        scrp->currentMode = currentMode;
    }
    /*
     * Get the new Screen pixmap ptr as SwitchMode might have called
     * ModifyPixmapHeader and xf86EnableDisableFBAccess will put it back...
     * Unfortunately.
     */
    pspix = (*pScreen->GetScreenPixmap) (pScreen);
    if (pspix->devPrivate.ptr)
       scrp->pixmapPrivate = pspix->devPrivate;

    /*
     * Make sure the layout is correct
     */
    xf86ReconfigureLayout();

    /*
     * Make sure the whole screen is visible
     */
    xf86SetViewport (pScreen, pScreen->width, pScreen->height);
    xf86SetViewport (pScreen, 0, 0);
    if (pRoot)
	(*scrp->EnableDisableFBAccess) (pScreen->myNum, TRUE);
    return ret;
}

Bool
xf86RandRSetConfig (ScreenPtr		pScreen,
		    Rotation		rotation,
		    int			rate,
		    RRScreenSizePtr	pSize)
{
    ScrnInfoPtr		    scrp = XF86SCRNINFO(pScreen);
    XF86RandRInfoPtr	    randrp = XF86RANDRINFO(pScreen);
    DisplayModePtr	    mode;
    int			    px, py;
    Bool		    useVirtual = FALSE;
    int			    maxX = 0, maxY = 0;
    Rotation		    oldRotation = randrp->rotation;

    randrp->rotation = rotation;

    if (randrp->virtualX == -1 || randrp->virtualY == -1)
    {
	randrp->virtualX = scrp->virtualX;
	randrp->virtualY = scrp->virtualY;
    }

    miPointerPosition (&px, &py);
    for (mode = scrp->modes; ; mode = mode->next)
    {
	if (randrp->maxX == 0 || randrp->maxY == 0)
	{
		if (maxX < mode->HDisplay)
			maxX = mode->HDisplay;
		if (maxY < mode->VDisplay)
			maxY = mode->VDisplay;
	}
	if (mode->HDisplay == pSize->width &&
	    mode->VDisplay == pSize->height &&
	    (rate == 0 || xf86RandRModeRefresh (mode) == rate))
	    break;
	if (mode->next == scrp->modes)
	{
	    if (pSize->width == randrp->virtualX &&
		pSize->height == randrp->virtualY)
	    {
		mode = scrp->modes;
		useVirtual = TRUE;
		break;
	    }
    	    if (randrp->maxX == 0 || randrp->maxY == 0)
    	    {
		randrp->maxX = maxX;
		randrp->maxY = maxY;
    	    }
	    return FALSE;
	}
    }

    if (randrp->maxX == 0 || randrp->maxY == 0)
    {
	randrp->maxX = maxX;
	randrp->maxY = maxY;
    }

    if (!xf86RandRSetMode (pScreen, mode, useVirtual, pSize->mmWidth,
			   pSize->mmHeight)) {
        randrp->rotation = oldRotation;
	return FALSE;
    }

    /*
     * Move the cursor back where it belongs; SwitchMode repositions it
     */
    if (pScreen == miPointerCurrentScreen ())
    {
        px = (px >= pScreen->width ? (pScreen->width - 1) : px);
        py = (py >= pScreen->height ? (pScreen->height - 1) : py);

	xf86SetViewport(pScreen, px, py);

	(*pScreen->SetCursorPosition) (pScreen, px, py, FALSE);
    }

    return TRUE;
}

Rotation
xf86RandRGetRotation(ScreenPtr pScreen)
{
    XF86RandRInfoPtr	    randrp = XF86RANDRINFO(pScreen);

    return randrp->rotation;
}

Bool
xf86RandRCreateScreenResources (ScreenPtr pScreen)
{
#if 0
    ScrnInfoPtr		pScrn = xf86Screens[pScreen->myNum];
    I830Ptr		pI830 = I830PTR(pScrn);
#endif
#ifdef PANORAMIX
    /* XXX disable RandR when using Xinerama */
    if (!noPanoramiXExtension)
	return TRUE;
#endif
#if RANDR_12_INTERFACE
    if (xf86RandRCreateScreenResources12 (pScreen))
	return TRUE;
#endif
#if 0
    /* XXX deal with initial rotation */
    if (pI830->rotation != RR_Rotate_0) {
	RRScreenSize p;
	Rotation requestedRotation = pI830->rotation;

	pI830->rotation = RR_Rotate_0;

	/* Just setup enough for an initial rotate */
	p.width = pScreen->width;
	p.height = pScreen->height;
	p.mmWidth = pScreen->mmWidth;
	p.mmHeight = pScreen->mmHeight;

	pI830->starting = TRUE; /* abuse this for dual head & rotation */
	xf86RandRSetConfig (pScreen, requestedRotation, 0, &p);
	pI830->starting = FALSE;
    }
#endif
    return TRUE;
}


Bool
xf86RandRInit (ScreenPtr pScreen)
{
    rrScrPrivPtr	rp;
    XF86RandRInfoPtr	randrp;

#ifdef PANORAMIX
    /* XXX disable RandR when using Xinerama */
    if (!noPanoramiXExtension)
	return TRUE;
#endif
    if (xf86RandRGeneration != serverGeneration)
    {
	xf86RandRIndex = AllocateScreenPrivateIndex();
	xf86RandRGeneration = serverGeneration;
    }

    randrp = xalloc (sizeof (XF86RandRInfoRec));
    if (!randrp)
	return FALSE;

    if (!RRScreenInit(pScreen))
    {
	xfree (randrp);
	return FALSE;
    }
    rp = rrGetScrPriv(pScreen);
    rp->rrGetInfo = xf86RandRGetInfo;
    rp->rrSetConfig = xf86RandRSetConfig;

    randrp->virtualX = -1;
    randrp->virtualY = -1;
    randrp->mmWidth = pScreen->mmWidth;
    randrp->mmHeight = pScreen->mmHeight;

    randrp->rotation = RR_Rotate_0; /* initial rotated mode */

    randrp->supported_rotations = RR_Rotate_0;

    randrp->maxX = randrp->maxY = 0;

    pScreen->devPrivates[xf86RandRIndex].ptr = randrp;

#if RANDR_12_INTERFACE
    if (!xf86RandRInit12 (pScreen))
	return FALSE;
#endif
    return TRUE;
}

void
xf86RandRSetRotations (ScreenPtr pScreen, Rotation rotations)
{
    XF86RandRInfoPtr	randrp = XF86RANDRINFO(pScreen);

    randrp->supported_rotations = rotations;
}

void
xf86GetOriginalVirtualSize(ScrnInfoPtr pScrn, int *x, int *y)
{
    ScreenPtr pScreen = screenInfo.screens[pScrn->scrnIndex];

    if (xf86RandRGeneration != serverGeneration ||
	XF86RANDRINFO(pScreen)->virtualX == -1)
    {
	*x = pScrn->virtualX;
	*y = pScrn->virtualY;
    } else {
	XF86RandRInfoPtr randrp = XF86RANDRINFO(pScreen);

	*x = randrp->virtualX;
	*y = randrp->virtualY;
    }
}

#if RANDR_12_INTERFACE
static Bool
xf86RandRScreenSetSize (ScreenPtr	pScreen,
			CARD16		width,
			CARD16		height,
			CARD32		mmWidth,
			CARD32		mmHeight)
{
    XF86RandRInfoPtr	randrp = XF86RANDRINFO(pScreen);
    ScrnInfoPtr		pScrn = XF86SCRNINFO(pScreen);
    WindowPtr		pRoot = WindowTable[pScreen->myNum];
    Bool 		ret = TRUE;

    if (randrp->virtualX == -1 || randrp->virtualY == -1)
    {
	randrp->virtualX = pScrn->virtualX;
	randrp->virtualY = pScrn->virtualY;
    }
    if (pRoot)
	(*pScrn->EnableDisableFBAccess) (pScreen->myNum, FALSE);
    pScrn->virtualX = width;
    pScrn->virtualY = height;

    pScreen->width = pScrn->virtualX;
    pScreen->height = pScrn->virtualY;
    pScreen->mmWidth = mmWidth;
    pScreen->mmHeight = mmHeight;

    xf86SetViewport (pScreen, pScreen->width, pScreen->height);
    xf86SetViewport (pScreen, 0, 0);
    if (pRoot)
	(*pScrn->EnableDisableFBAccess) (pScreen->myNum, TRUE);
    if (WindowTable[pScreen->myNum])
	RRScreenSizeNotify (pScreen);
    return ret;
}

static Bool
xf86RandRCrtcNotify (RRCrtcPtr	randr_crtc)
{
    ScreenPtr		pScreen = randr_crtc->pScreen;
    ScrnInfoPtr		pScrn = xf86Screens[pScreen->myNum];
    I830Ptr		pI830 = I830PTR(pScrn);
    RRModePtr		randr_mode = NULL;
    int			x;
    int			y;
    Rotation		rotation;
    int			numOutputs;
    RROutputPtr		randr_outputs[MAX_OUTPUTS];
    RROutputPtr		randr_output;
    xf86CrtcPtr	crtc = randr_crtc->devPrivate;
    xf86OutputPtr	output;
    int			i, j;
    DisplayModePtr	curMode = &crtc->curMode;

    x = crtc->x;
    y = crtc->y;
    rotation = RR_Rotate_0;
    numOutputs = 0;
    randr_mode = NULL;
    for (i = 0; i < pI830->xf86_config.num_output; i++)
    {
	output = pI830->xf86_config.output[i];
	if (output->crtc == crtc)
	{
	    randr_output = output->randr_output;
	    randr_outputs[numOutputs++] = randr_output;
	    /*
	     * We make copies of modes, so pointer equality 
	     * isn't sufficient
	     */
	    for (j = 0; j < randr_output->numModes; j++)
	    {
		DisplayModePtr	outMode = randr_output->modes[j]->devPrivate;
		if (I830ModesEqual(curMode, outMode))
		{
		    randr_mode = randr_output->modes[j];
		    break;
		}
	    }
	}
    }
    return RRCrtcNotify (randr_crtc, randr_mode, x, y,
			 rotation, numOutputs, randr_outputs);
}

static Bool
xf86RandRCrtcSet (ScreenPtr	pScreen,
		  RRCrtcPtr	randr_crtc,
		  RRModePtr	randr_mode,
		  int		x,
		  int		y,
		  Rotation	rotation,
		  int		num_randr_outputs,
		  RROutputPtr	*randr_outputs)
{
    ScrnInfoPtr		pScrn = xf86Screens[pScreen->myNum];
    I830Ptr		pI830 = I830PTR(pScrn);
    xf86CrtcPtr	crtc = randr_crtc->devPrivate;
    DisplayModePtr	mode = randr_mode ? randr_mode->devPrivate : NULL;
    Bool		changed = FALSE;
    int			o, ro;
    xf86CrtcPtr	save_crtcs[MAX_OUTPUTS];
    Bool		save_enabled = crtc->enabled;

    if (!I830ModesEqual (&crtc->curMode, mode))
	changed = TRUE;
    
    for (o = 0; o < pI830->xf86_config.num_output; o++) 
    {
	xf86OutputPtr  output = pI830->xf86_config.output[o];
	xf86CrtcPtr    new_crtc;

	save_crtcs[o] = output->crtc;
	
	if (output->crtc == crtc)
	    new_crtc = NULL;
	else
	    new_crtc = output->crtc;
	for (ro = 0; ro < num_randr_outputs; ro++) 
	    if (output->randr_output == randr_outputs[ro])
	    {
		new_crtc = crtc;
		break;
	    }
	if (new_crtc != output->crtc)
	{
	    changed = TRUE;
	    output->crtc = new_crtc;
	}
    }
    if (changed)
    {
	crtc->enabled = mode != NULL;
	
	/* Sync the engine before adjust mode */
	if (pI830->AccelInfoRec && pI830->AccelInfoRec->NeedToSync) {
	    (*pI830->AccelInfoRec->Sync)(pScrn);
	    pI830->AccelInfoRec->NeedToSync = FALSE;
	}

	if (mode)
	{
	    if (!i830PipeSetMode (crtc, mode, TRUE))
	    {
		crtc->enabled = save_enabled;
		for (o = 0; o < pI830->xf86_config.num_output; o++)
		{
		    xf86OutputPtr	output = pI830->xf86_config.output[o];
		    output->crtc = save_crtcs[o];
		}
		return FALSE;
	    }
	    crtc->desiredMode = *mode;
	    i830PipeSetBase(crtc, x, y);
	}
	i830DisableUnusedFunctions (pScrn);
    }
    return xf86RandRCrtcNotify (randr_crtc);
}

static Bool
xf86RandRCrtcSetGamma (ScreenPtr    pScreen,
		       RRCrtcPtr    crtc)
{
    return FALSE;
}

/**
 * Given a list of xf86 modes and a RandR Output object, construct
 * RandR modes and assign them to the output
 */
static Bool
I830xf86RROutputSetModes (RROutputPtr randr_output, DisplayModePtr modes)
{
    DisplayModePtr  mode;
    RRModePtr	    *rrmodes = NULL;
    int		    nmode = 0;
    int		    npreferred = 0;
    Bool	    ret = TRUE;
    int		    pref;

    for (mode = modes; mode; mode = mode->next)
	nmode++;

    if (nmode) {
	rrmodes = xalloc (nmode * sizeof (RRModePtr));
	
	if (!rrmodes)
	    return FALSE;
	nmode = 0;

	for (pref = 1; pref >= 0; pref--) {
	    for (mode = modes; mode; mode = mode->next) {
		if ((pref != 0) == ((mode->type & M_T_PREFERRED) != 0)) {
		    xRRModeInfo		modeInfo;
		    RRModePtr		rrmode;
		    
		    modeInfo.nameLength = strlen (mode->name);
		    modeInfo.width = mode->HDisplay;
		    modeInfo.dotClock = mode->Clock * 1000;
		    modeInfo.hSyncStart = mode->HSyncStart;
		    modeInfo.hSyncEnd = mode->HSyncEnd;
		    modeInfo.hTotal = mode->HTotal;
		    modeInfo.hSkew = mode->HSkew;

		    modeInfo.height = mode->VDisplay;
		    modeInfo.vSyncStart = mode->VSyncStart;
		    modeInfo.vSyncEnd = mode->VSyncEnd;
		    modeInfo.vTotal = mode->VTotal;
		    modeInfo.modeFlags = mode->Flags;

		    rrmode = RRModeGet (&modeInfo, mode->name);
		    rrmode->devPrivate = mode;
		    if (rrmode) {
			rrmodes[nmode++] = rrmode;
			npreferred += pref;
		    }
		}
	    }
	}
    }
    
    ret = RROutputSetModes (randr_output, rrmodes, nmode, npreferred);
    xfree (rrmodes);
    return ret;
}

/*
 * Mirror the current mode configuration to RandR
 */
static Bool
xf86RandRSetInfo12 (ScrnInfoPtr pScrn)
{
    I830Ptr		    pI830 = I830PTR(pScrn);
    RROutputPtr		    clones[MAX_OUTPUTS];
    RRCrtcPtr		    crtcs[MAX_DISPLAY_PIPES];
    int			    ncrtc;
    int			    o, c, p;
    int			    clone_types;
    int			    crtc_types;
    int			    subpixel;
    RRCrtcPtr		    randr_crtc;
    int			    nclone;
    
    for (o = 0; o < pI830->xf86_config.num_output; o++)
    {
	xf86OutputPtr	output = pI830->xf86_config.output[o];
	I830OutputPrivatePtr	intel_output = output->driver_private;
	/*
	 * Valid crtcs
	 */
	switch (intel_output->type) {
	case I830_OUTPUT_DVO:
	case I830_OUTPUT_SDVO:
	    crtc_types = ((1 << 0)|
			  (1 << 1));
	    clone_types = ((1 << I830_OUTPUT_ANALOG) |
			   (1 << I830_OUTPUT_DVO) |
			   (1 << I830_OUTPUT_SDVO));
	    subpixel = SubPixelHorizontalRGB;
	    break;
	case I830_OUTPUT_ANALOG:
	    crtc_types = ((1 << 0) | (1 << 1));
	    clone_types = ((1 << I830_OUTPUT_ANALOG) |
			   (1 << I830_OUTPUT_DVO) |
			   (1 << I830_OUTPUT_SDVO));
	    subpixel = SubPixelNone;
	    break;
	case I830_OUTPUT_LVDS:
	    crtc_types = (1 << 1);
	    clone_types = (1 << I830_OUTPUT_LVDS);
	    subpixel = SubPixelHorizontalRGB;
	    break;
	case I830_OUTPUT_TVOUT:
	    crtc_types = ((1 << 0) |
			  (1 << 1));
	    clone_types = (1 << I830_OUTPUT_TVOUT);
	    subpixel = SubPixelNone;
	    break;
	default:
	    crtc_types = 0;
	    clone_types = 0;
	    subpixel = SubPixelUnknown;
	    break;
	}
	ncrtc = 0;
	for (p = 0; p < pI830->xf86_config.num_crtc; p++)
	    if (crtc_types & (1 << p))
		crtcs[ncrtc++] = pI830->xf86_config.crtc[p]->randr_crtc;

	if (output->crtc)
	    randr_crtc = output->crtc->randr_crtc;
	else
	    randr_crtc = NULL;

	if (!RROutputSetCrtcs (output->randr_output, crtcs, ncrtc))
	    return FALSE;

	RROutputSetCrtc (output->randr_output, randr_crtc);
	RROutputSetPhysicalSize(output->randr_output, 
				output->mm_width,
				output->mm_height);
	I830xf86RROutputSetModes (output->randr_output, output->probed_modes);

	switch ((*output->funcs->detect)(output)) {
	case OUTPUT_STATUS_CONNECTED:
	    RROutputSetConnection (output->randr_output, RR_Connected);
	    break;
	case OUTPUT_STATUS_DISCONNECTED:
	    RROutputSetConnection (output->randr_output, RR_Disconnected);
	    break;
	case OUTPUT_STATUS_UNKNOWN:
	    RROutputSetConnection (output->randr_output, RR_UnknownConnection);
	    break;
	}

	RROutputSetSubpixelOrder (output->randr_output, subpixel);

	/*
	 * Valid clones
	 */
	nclone = 0;
	for (c = 0; c < pI830->xf86_config.num_output; c++)
	{
	    xf86OutputPtr	    clone = pI830->xf86_config.output[c];
	    I830OutputPrivatePtr    intel_clone = clone->driver_private;
	    
	    if (o != c && ((1 << intel_clone->type) & clone_types))
		clones[nclone++] = clone->randr_output;
	}
	if (!RROutputSetClones (output->randr_output, clones, nclone))
	    return FALSE;
    }
    return TRUE;
}

/*
 * Query the hardware for the current state, then mirror
 * that to RandR
 */
static Bool
xf86RandRGetInfo12 (ScreenPtr pScreen, Rotation *rotations)
{
    ScrnInfoPtr		pScrn = xf86Screens[pScreen->myNum];

    i830_reprobe_output_modes(pScrn);
    return xf86RandRSetInfo12 (pScrn);
}

static Bool
xf86RandRCreateObjects12 (ScrnInfoPtr pScrn)
{
    I830Ptr		pI830 = I830PTR(pScrn);
    int			p;
    
    if (!RRInit ())
	return FALSE;

    /*
     * Configure crtcs
     */
    for (p = 0; p < pI830->xf86_config.num_crtc; p++)
    {
	xf86CrtcPtr    crtc = pI830->xf86_config.crtc[p];
	
	RRCrtcGammaSetSize (crtc->randr_crtc, 256);
    }

    return TRUE;
}

static Bool
xf86RandRCreateScreenResources12 (ScreenPtr pScreen)
{
    XF86RandRInfoPtr	randrp = XF86RANDRINFO(pScreen);
    ScrnInfoPtr		pScrn = xf86Screens[pScreen->myNum];
    I830Ptr		pI830 = I830PTR(pScrn);
    int			p, o;
    int			width, height;

    /*
     * Attach RandR objects to screen
     */
    for (p = 0; p < pI830->xf86_config.num_crtc; p++)
	if (!RRCrtcAttachScreen (pI830->xf86_config.crtc[p]->randr_crtc, pScreen))
	    return FALSE;

    for (o = 0; o < pI830->xf86_config.num_output; o++)
	if (!RROutputAttachScreen (pI830->xf86_config.output[o]->randr_output, pScreen))
	    return FALSE;

    /*
     * Compute width of screen
     */
    width = 0; height = 0;
    for (p = 0; p < pI830->xf86_config.num_crtc; p++)
    {
	xf86CrtcPtr    crtc = pI830->xf86_config.crtc[p];
	int		    crtc_width = crtc->x + crtc->curMode.HDisplay;
	int		    crtc_height = crtc->y + crtc->curMode.VDisplay;
	
	if (crtc->enabled && crtc_width > width)
	    width = crtc_width;
	if (crtc->enabled && crtc_height > height)
	    height = crtc_height;
    }
    
    if (width && height)
    {
	int mmWidth, mmHeight;

	mmWidth = pScreen->mmWidth;
	mmHeight = pScreen->mmHeight;
	if (width != pScreen->width)
	    mmWidth = mmWidth * width / pScreen->width;
	if (height != pScreen->height)
	    mmHeight = mmHeight * height / pScreen->height;
	xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
		   "Setting screen physical size to %d x %d\n",
		   mmWidth, mmHeight);
	xf86RandRScreenSetSize (pScreen,
				width,
				height,
				mmWidth,
				mmHeight);
    }

    for (p = 0; p < pI830->xf86_config.num_crtc; p++)
	xf86RandRCrtcNotify (pI830->xf86_config.crtc[p]->randr_crtc);
    
    if (randrp->virtualX == -1 || randrp->virtualY == -1)
    {
	randrp->virtualX = pScrn->virtualX;
	randrp->virtualY = pScrn->virtualY;
    }
    
    RRScreenSetSizeRange (pScreen, 320, 240,
			  randrp->virtualX, randrp->virtualY);
    return TRUE;
}

static void
xf86RandRPointerMoved (int scrnIndex, int x, int y)
{
}

static Bool
xf86RandRInit12 (ScreenPtr pScreen)
{
    ScrnInfoPtr		pScrn = xf86Screens[pScreen->myNum];
    rrScrPrivPtr	rp = rrGetScrPriv(pScreen);

    rp->rrGetInfo = xf86RandRGetInfo12;
    rp->rrScreenSetSize = xf86RandRScreenSetSize;
    rp->rrCrtcSet = xf86RandRCrtcSet;
    rp->rrCrtcSetGamma = xf86RandRCrtcSetGamma;
    rp->rrSetConfig = NULL;
    pScrn->PointerMoved = xf86RandRPointerMoved;
    return TRUE;
}

static RRModePtr
I830RRDefaultMode (RROutputPtr output)
{
    RRModePtr   target_mode = NULL;
    int		target_diff = 0;
    int		mmHeight;
    int		num_modes;
    int		m;
    
    num_modes = output->numPreferred ? output->numPreferred : output->numModes;
    mmHeight = output->mmHeight;
    if (!mmHeight)
	mmHeight = 203;	/* 768 pixels at 96dpi */
    /*
     * Pick a mode closest to 96dpi 
     */
    for (m = 0; m < num_modes; m++)
    {
	RRModePtr   mode = output->modes[m];
	int	    dpi;
	int	    diff;

	dpi = (mode->mode.height * 254) / (mmHeight * 10);
	diff = dpi - 96;
	diff = diff < 0 ? -diff : diff;
	if (target_mode == NULL || diff < target_diff)
	{
	    target_mode = mode;
	    target_diff = diff;
	}
    }
    return target_mode;
}

static RRModePtr
I830ClosestMode (RROutputPtr output, RRModePtr match)
{
    RRModePtr   target_mode = NULL;
    int		target_diff = 0;
    int		m;
    
    /*
     * Pick a mode closest to the specified mode
     */
    for (m = 0; m < output->numModes; m++)
    {
	RRModePtr   mode = output->modes[m];
	int	    dx, dy;
	int	    diff;

	/* exact matches are preferred */
	if (mode == match)
	    return mode;
	
	dx = match->mode.width - mode->mode.width;
	dy = match->mode.height - mode->mode.height;
	diff = dx * dx + dy * dy;
	if (target_mode == NULL || diff < target_diff)
	{
	    target_mode = mode;
	    target_diff = diff;
	}
    }
    return target_mode;
}

static int
I830RRPickCrtcs (RROutputPtr	*outputs,
		 RRCrtcPtr	*best_crtcs,
		 RRModePtr	*modes,
		 int		num_output,
		 int		n)
{
    int		c, o, l;
    RROutputPtr	output;
    RRCrtcPtr	crtc;
    RRCrtcPtr	*crtcs;
    RRCrtcPtr	best_crtc;
    int		best_score;
    int		score;
    int		my_score;
    
    if (n == num_output)
	return 0;
    output = outputs[n];
    
    /*
     * Compute score with this output disabled
     */
    best_crtcs[n] = NULL;
    best_crtc = NULL;
    best_score = I830RRPickCrtcs (outputs, best_crtcs, modes, num_output, n+1);
    if (modes[n] == NULL)
	return best_score;
    
    crtcs = xalloc (num_output * sizeof (RRCrtcPtr));
    if (!crtcs)
	return best_score;

    my_score = 1;
    /* Score outputs that are known to be connected higher */
    if (output->connection == RR_Connected)
	my_score++;
    /* Score outputs with preferred modes higher */
    if (output->numPreferred)
	my_score++;
    /*
     * Select a crtc for this output and
     * then attempt to configure the remaining
     * outputs
     */
    for (c = 0; c < output->numCrtcs; c++)
    {
	crtc = output->crtcs[c];
	/*
	 * Check to see if some other output is
	 * using this crtc
	 */
	for (o = 0; o < n; o++)
	    if (best_crtcs[o] == crtc)
		break;
	if (o < n)
	{
	    /*
	     * If the two outputs desire the same mode,
	     * see if they can be cloned
	     */
	    if (modes[o] == modes[n])
	    {
		for (l = 0; l < output->numClones; l++)
		    if (output->clones[l] == outputs[o])
			break;
		if (l == output->numClones)
		    continue;		/* nope, try next CRTC */
	    }
	    else
		continue;		/* different modes, can't clone */
	}
	crtcs[n] = crtc;
	memcpy (crtcs, best_crtcs, n * sizeof (RRCrtcPtr));
	score = my_score + I830RRPickCrtcs (outputs, crtcs, modes,
					    num_output, n+1);
	if (score >= best_score)
	{
	    best_crtc = crtc;
	    best_score = score;
	    memcpy (best_crtcs, crtcs, num_output * sizeof (RRCrtcPtr));
	}
    }
    xfree (crtcs);
    return best_score;
}

static Bool
I830RRInitialConfiguration (RROutputPtr *outputs,
			    RRCrtcPtr	*crtcs,
			    RRModePtr	*modes,
			    int		num_output)
{
    int		o;
    RRModePtr	target_mode = NULL;

    for (o = 0; o < num_output; o++)
	modes[o] = NULL;
    
    /*
     * Let outputs with preferred modes drive screen size
     */
    for (o = 0; o < num_output; o++)
    {
	RROutputPtr output = outputs[o];

	if (output->connection != RR_Disconnected && output->numPreferred)
	{
	    target_mode = I830RRDefaultMode (output);
	    if (target_mode)
	    {
		modes[o] = target_mode;
		break;
	    }
	}
    }
    if (!target_mode)
    {
	for (o = 0; o < num_output; o++)
	{
	    RROutputPtr output = outputs[o];
	    if (output->connection != RR_Disconnected)
	    {
		target_mode = I830RRDefaultMode (output);
		if (target_mode)
		{
		    modes[o] = target_mode;
		    break;
		}
	    }
	}
    }
    for (o = 0; o < num_output; o++)
    {
	RROutputPtr output = outputs[o];
	
	if (output->connection != RR_Disconnected && !modes[o])
	    modes[o] = I830ClosestMode (output, target_mode);
    }

    if (!I830RRPickCrtcs (outputs, crtcs, modes, num_output, 0))
	return FALSE;
    
    return TRUE;
}

/*
 * Compute the virtual size necessary to place all of the available
 * crtcs in a panorama configuration
 */

static void
I830RRDefaultScreenLimits (RROutputPtr *outputs, int num_output,
			   RRCrtcPtr *crtcs, int num_crtc,
			   int *widthp, int *heightp)
{
    int	    width = 0, height = 0;
    int	    o;
    int	    c;
    int	    m;
    int	    s;

    for (c = 0; c < num_crtc; c++)
    {
	RRCrtcPtr   crtc = crtcs[c];
	int	    crtc_width = 1600, crtc_height = 1200;

	for (o = 0; o < num_output; o++) 
	{
	    RROutputPtr	output = outputs[o];

	    for (s = 0; s < output->numCrtcs; s++)
		if (output->crtcs[s] == crtc)
		{
		    for (m = 0; m < output->numModes; m++)
		    {
			RRModePtr   mode = output->modes[m];
			if (mode->mode.width > crtc_width)
			    crtc_width = mode->mode.width;
			if (mode->mode.height > crtc_width)
			    crtc_height = mode->mode.height;
		    }
		}
	}
	if (crtc_width > width)
	    width = crtc_width;
	if (crtc_height > height)
	    height = crtc_height;
    }
    *widthp = width;
    *heightp = height;
}

#endif

Bool
xf86RandRPreInit (ScrnInfoPtr pScrn)
{
    I830Ptr pI830 = I830PTR(pScrn);
#if RANDR_12_INTERFACE
    RROutputPtr	outputs[MAX_OUTPUTS];
    RRCrtcPtr	output_crtcs[MAX_OUTPUTS];
    RRModePtr	output_modes[MAX_OUTPUTS];
    RRCrtcPtr	crtcs[MAX_DISPLAY_PIPES];
    int		width, height;
    int		o;
    int		c;
#endif
    
    if (pI830->xf86_config.num_output <= 0)
	return FALSE;
    
    i830_reprobe_output_modes(pScrn);

#if RANDR_12_INTERFACE
    if (!xf86RandRCreateObjects12 (pScrn))
	return FALSE;

    /*
     * Configure output modes
     */
    if (!xf86RandRSetInfo12 (pScrn))
	return FALSE;
    /*
     * With RandR info set up, let RandR choose
     * the initial configuration
     */
    for (o = 0; o < pI830->xf86_config.num_output; o++)
	outputs[o] = pI830->xf86_config.output[o]->randr_output;
    for (c = 0; c < pI830->xf86_config.num_crtc; c++)
	crtcs[c] = pI830->xf86_config.crtc[c]->randr_crtc;
    
    if (!I830RRInitialConfiguration (outputs, output_crtcs, output_modes,
				     pI830->xf86_config.num_output))
	return FALSE;
    
    I830RRDefaultScreenLimits (outputs, pI830->xf86_config.num_output, 
			       crtcs, pI830->xf86_config.num_crtc,
			       &width, &height);
    
    if (width > pScrn->virtualX)
	pScrn->virtualX = width;
    if (width > pScrn->display->virtualX)
	pScrn->display->virtualX = width;
    if (height > pScrn->virtualY)
	pScrn->virtualY = height;
    if (height > pScrn->display->virtualY)
	pScrn->display->virtualY = height;
    
    /* XXX override xf86 common frame computation code */
    pScrn->display->frameX0 = 0;
    pScrn->display->frameY0 = 0;
    for (o = 0; o < pI830->xf86_config.num_output; o++)
    {
	xf86OutputPtr  output = pI830->xf86_config.output[o];
	RRModePtr	    randr_mode = output_modes[o];
        RRCrtcPtr	    randr_crtc = output_crtcs[o];
	DisplayModePtr	    mode;

	if (randr_mode && randr_crtc)
	{
	    xf86CrtcPtr    crtc = randr_crtc->devPrivate;
	    
	    mode = (DisplayModePtr) randr_mode->devPrivate;
	    crtc->desiredMode = *mode;
	    output->crtc = crtc;
	}
    }
#endif
    i830_set_xf86_modes_from_outputs (pScrn);
    
    i830_set_default_screen_size(pScrn);

    return TRUE;
}