summaryrefslogtreecommitdiffstats
path: root/unix/xserver/hw/vnc/RandrGlue.c
blob: 86ec8bcc779720def329648e69dce535d2586b30 (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
/* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
 * Copyright 2011-2015 Pierre Ossman for Cendio AB
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#include <assert.h>
#include <string.h>

#include "scrnintstr.h"
#include "randrstr.h"

#include "RandrGlue.h"
#include "XorgGlue.h"

static int scrIdx;

void vncSetGlueContext(int screenIndex);

void vncSetGlueContext(int screenIndex)
{
  scrIdx = screenIndex;
}

int vncGetScreenWidth(void)
{
  return screenInfo.screens[scrIdx]->width;
}

int vncGetScreenHeight(void)
{
  return screenInfo.screens[scrIdx]->height;
}

int vncRandRResizeScreen(int width, int height)
{
  ScreenPtr pScreen = screenInfo.screens[scrIdx];
  /* Try to retain DPI when we resize */
  return RRScreenSizeSet(pScreen, width, height,
                         pScreen->mmWidth * width / pScreen->width,
                         pScreen->mmHeight * height / pScreen->height);
}

void vncRandRUpdateSetTime(void)
{
  rrScrPrivPtr rp = rrGetScrPriv(screenInfo.screens[scrIdx]);
  rp->lastSetTime = currentTime;
}

int vncRandRHasOutputClones(void)
{
  rrScrPrivPtr rp = rrGetScrPriv(screenInfo.screens[scrIdx]);
  for (int i = 0;i < rp->numCrtcs;i++) {
    if (rp->crtcs[i]->numOutputs > 1)
      return 1;
  }
  return 0;
}

int vncRandRGetOutputCount(void)
{
  rrScrPrivPtr rp = rrGetScrPriv(screenInfo.screens[scrIdx]);
  return rp->numOutputs;
}

int vncRandRGetAvailableOutputs(void)
{
  rrScrPrivPtr rp = rrGetScrPriv(screenInfo.screens[scrIdx]);

  int availableOutputs;
  RRCrtcPtr *usedCrtcs;
  int numUsed;

  int i, j, k;

  usedCrtcs = malloc(sizeof(RRCrtcPtr) * rp->numCrtcs);
  if (usedCrtcs == NULL)
    return 0;

  /*
   * This gets slightly complicated because we might need to hook a CRTC
   * up to the output, but also check that we don't try to use the same
   * CRTC for multiple outputs.
   */
  availableOutputs = 0;
  numUsed = 0;
  for (i = 0;i < rp->numOutputs;i++) {
    RROutputPtr output;

    output = rp->outputs[i];

    if (output->crtc != NULL)
      availableOutputs++;
    else {
      for (j = 0;j < output->numCrtcs;j++) {
        if (output->crtcs[j]->numOutputs != 0)
          continue;

        for (k = 0;k < numUsed;k++) {
          if (usedCrtcs[k] == output->crtcs[j])
            break;
        }
        if (k != numUsed)
          continue;

        availableOutputs++;

        usedCrtcs[numUsed] = output->crtcs[j];
        numUsed++;

        break;
      }
    }
  }

  free(usedCrtcs);

  return availableOutputs;
}

char *vncRandRGetOutputName(int outputIdx)
{
  rrScrPrivPtr rp = rrGetScrPriv(screenInfo.screens[scrIdx]);
  return strdup(rp->outputs[outputIdx]->name);
}

int vncRandRIsOutputEnabled(int outputIdx)
{
  rrScrPrivPtr rp = rrGetScrPriv(screenInfo.screens[scrIdx]);

  if (rp->outputs[outputIdx]->crtc == NULL)
    return 0;
  if (rp->outputs[outputIdx]->crtc->mode == NULL)
    return 0;

  return 1;
}

int vncRandRIsOutputUsable(int outputIdx)
{
  rrScrPrivPtr rp = rrGetScrPriv(screenInfo.screens[scrIdx]);

  RROutputPtr output;
  int i;

  output = rp->outputs[outputIdx];
  if (output->crtc != NULL)
    return 1;

  /* Any unused CRTCs? */
  for (i = 0;i < output->numCrtcs;i++) {
    if (output->crtcs[i]->numOutputs == 0)
      return 1;
  }

  return 0;
}

int vncRandRDisableOutput(int outputIdx)
{
  rrScrPrivPtr rp = rrGetScrPriv(screenInfo.screens[scrIdx]);
  RRCrtcPtr crtc;

  crtc = rp->outputs[outputIdx]->crtc;
  if (crtc == NULL)
    return 1;

  return RRCrtcSet(crtc, NULL, crtc->x, crtc->y, crtc->rotation, 0, NULL);
}

unsigned int vncRandRGetOutputId(int outputIdx)
{
  rrScrPrivPtr rp = rrGetScrPriv(screenInfo.screens[scrIdx]);
  return rp->outputs[outputIdx]->id;
}

void vncRandRGetOutputDimensions(int outputIdx,
                            int *x, int *y, int *width, int *height)
{
  rrScrPrivPtr rp = rrGetScrPriv(screenInfo.screens[scrIdx]);
  int swap;

  *x = rp->outputs[outputIdx]->crtc->x;
  *y = rp->outputs[outputIdx]->crtc->y;
  *width = rp->outputs[outputIdx]->crtc->mode->mode.width;
  *height = rp->outputs[outputIdx]->crtc->mode->mode.height;

  switch (rp->outputs[outputIdx]->crtc->rotation & 0xf) {
  case RR_Rotate_90:
  case RR_Rotate_270:
    swap = *width;
    *width = *height;
    *height = swap;
    break;
  }
}

int vncRandRReconfigureOutput(int outputIdx, int x, int y,
                              int width, int height)
{
  rrScrPrivPtr rp = rrGetScrPriv(screenInfo.screens[scrIdx]);

  RROutputPtr output;
  RRCrtcPtr crtc;
  RRModePtr mode;

  int i;

  output = rp->outputs[outputIdx];
  crtc = output->crtc;

  /* Need a CRTC? */
  if (crtc == NULL) {
    for (i = 0;i < output->numCrtcs;i++) {
      if (output->crtcs[i]->numOutputs != 0)
        continue;

      crtc = output->crtcs[i];
      break;
    }

    /* Couldn't find one... */
    if (crtc == NULL)
      return 0;
  }

  /* Make sure we have the mode we want */
  mode = vncRandRCreatePreferredMode(output, width, height);
  if (mode == NULL)
    return 0;

  /* Reconfigure new mode and position */
  return RRCrtcSet(crtc, mode, x, y, crtc->rotation, 1, &output);
}

int vncRandRCreateOutputs(int extraOutputs)
{
  return vncRandRCreateScreenOutputs(scrIdx, extraOutputs);
}