| 1 |
#include "allegro5/allegro.h" |
| 2 |
#include "allegro5/internal/aintern_x.h" |
| 3 |
#include "allegro5/internal/aintern_xdisplay.h" |
| 4 |
#include "allegro5/internal/aintern_xfullscreen.h" |
| 5 |
#include "allegro5/internal/aintern_xsystem.h" |
| 6 |
|
| 7 |
#ifdef ALLEGRO_XWINDOWS_WITH_XRANDR |
| 8 |
|
| 9 |
ALLEGRO_DEBUG_CHANNEL("xrandr") |
| 10 |
|
| 11 |
typedef struct xrandr_screen xrandr_screen; |
| 12 |
typedef struct xrandr_crtc xrandr_crtc; |
| 13 |
typedef struct xrandr_output xrandr_output; |
| 14 |
typedef struct xrandr_mode xrandr_mode; |
| 15 |
|
| 16 |
struct xrandr_screen { |
| 17 |
int id; |
| 18 |
Time timestamp; |
| 19 |
Time configTimestamp; |
| 20 |
_AL_VECTOR crtcs; // xrandr_crtc |
| 21 |
_AL_VECTOR outputs; // xrandr_output |
| 22 |
_AL_VECTOR modes; // xrandr_mode |
| 23 |
|
| 24 |
XRRScreenResources *res; |
| 25 |
}; |
| 26 |
|
| 27 |
enum xrandr_crtc_position { |
| 28 |
CRTC_POS_NONE = 0, |
| 29 |
CRTC_POS_ABOVE, |
| 30 |
CRTC_POS_LEFTOF, |
| 31 |
CRTC_POS_BELOW, |
| 32 |
CRTC_POS_RIGHTOF |
| 33 |
}; |
| 34 |
|
| 35 |
struct xrandr_crtc { |
| 36 |
RRCrtc id; |
| 37 |
Time timestamp; |
| 38 |
int x, y; |
| 39 |
unsigned int width, height; |
| 40 |
RRMode mode; |
| 41 |
Rotation rotation; |
| 42 |
_AL_VECTOR connected; |
| 43 |
_AL_VECTOR possible; |
| 44 |
|
| 45 |
RRMode original_mode; |
| 46 |
int original_xoff; |
| 47 |
int original_yoff; |
| 48 |
RRCrtc align_to; |
| 49 |
int align; |
| 50 |
}; |
| 51 |
|
| 52 |
struct xrandr_output { |
| 53 |
RROutput id; |
| 54 |
Time timestamp; |
| 55 |
RRCrtc crtc; |
| 56 |
char *name; |
| 57 |
int namelen; |
| 58 |
unsigned long mm_width; |
| 59 |
unsigned long mm_height; |
| 60 |
Connection connection; |
| 61 |
SubpixelOrder subpixel_order; |
| 62 |
_AL_VECTOR crtcs; // RRCrtc |
| 63 |
_AL_VECTOR clones; // RROutput |
| 64 |
RRMode prefered_mode; |
| 65 |
_AL_VECTOR modes; // RRMode |
| 66 |
}; |
| 67 |
|
| 68 |
struct xrandr_mode { |
| 69 |
RRMode id; |
| 70 |
unsigned int width; |
| 71 |
unsigned int height; |
| 72 |
unsigned int refresh; |
| 73 |
}; |
| 74 |
|
| 75 |
struct xrandr_rect { |
| 76 |
int x1; |
| 77 |
int y1; |
| 78 |
int x2; |
| 79 |
int y2; |
| 80 |
}; |
| 81 |
|
| 82 |
static void xrandr_copy_mode(xrandr_mode *mode, XRRModeInfo *rrmode) |
| 83 |
{ |
| 84 |
mode->id = rrmode->id; |
| 85 |
mode->width = rrmode->width; |
| 86 |
mode->height = rrmode->height; |
| 87 |
|
| 88 |
if (rrmode->hTotal && rrmode->vTotal) { |
| 89 |
mode->refresh = ((float) rrmode->dotClock / ((float) rrmode->hTotal * (float) rrmode->vTotal)); |
| 90 |
} |
| 91 |
else { |
| 92 |
mode->refresh = 0; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
static void xrandr_clear_fake_refresh_rates(xrandr_mode *modes, int nmode) |
| 97 |
{ |
| 98 |
int i; |
| 99 |
|
| 100 |
if (nmode < 2) |
| 101 |
return; |
| 102 |
|
| 103 |
/* The Nvidia proprietary driver may return fake refresh rates when |
| 104 |
* DynamicTwinView is enabled, so that all modes are unique. The user has |
| 105 |
* no use for that wrong information so zero it out if we detect it. |
| 106 |
*/ |
| 107 |
|
| 108 |
for (i = 1; i < nmode; i++) { |
| 109 |
if (modes[i].refresh != modes[i-1].refresh + 1) { |
| 110 |
return; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
ALLEGRO_WARN("Zeroing out fake refresh rates from nvidia proprietary driver.\n"); |
| 115 |
ALLEGRO_WARN("Disable the DynamicTwinView driver option to avoid this.\n"); |
| 116 |
|
| 117 |
for (i = 0; i < nmode; i++) { |
| 118 |
modes[i].refresh = 0; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
static void xrandr_copy_output(xrandr_output *output, RROutput id, XRROutputInfo *rroutput) |
| 123 |
{ |
| 124 |
output->id = id; |
| 125 |
output->timestamp = rroutput->timestamp; |
| 126 |
output->crtc = rroutput->crtc; |
| 127 |
output->name = strdup(rroutput->name); |
| 128 |
output->namelen = rroutput->nameLen; |
| 129 |
output->mm_width = rroutput->mm_width; |
| 130 |
output->mm_height = rroutput->mm_height; |
| 131 |
output->connection = rroutput->connection; |
| 132 |
output->subpixel_order = rroutput->subpixel_order; |
| 133 |
|
| 134 |
ALLEGRO_DEBUG("output[%s] %s on crtc %i.\n", output->name, output->connection == RR_Connected ? "Connected" : "Not Connected", (int)output->crtc); |
| 135 |
|
| 136 |
_al_vector_init(&output->crtcs, sizeof(RRCrtc)); |
| 137 |
if(rroutput->ncrtc) { |
| 138 |
_al_vector_append_array(&output->crtcs, rroutput->ncrtc, rroutput->crtcs); |
| 139 |
} |
| 140 |
|
| 141 |
_al_vector_init(&output->clones, sizeof(RROutput)); |
| 142 |
if(rroutput->nclone) { |
| 143 |
_al_vector_append_array(&output->clones, rroutput->nclone, rroutput->clones); |
| 144 |
} |
| 145 |
|
| 146 |
_al_vector_init(&output->modes, sizeof(RRMode)); |
| 147 |
if(rroutput->nmode) { |
| 148 |
_al_vector_append_array(&output->modes, rroutput->nmode, rroutput->modes); |
| 149 |
} |
| 150 |
|
| 151 |
/* npreferred isn't the prefered mode index, it's the number of prefered modes |
| 152 |
* starting from 0. So just load up the first prefered mode. |
| 153 |
* We don't actually use it yet anyway, so it's not really important. |
| 154 |
*/ |
| 155 |
if(rroutput->npreferred) { |
| 156 |
output->prefered_mode = rroutput->modes[0]; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
static void xrandr_copy_crtc(xrandr_crtc *crtc, RRCrtc id, XRRCrtcInfo *rrcrtc) |
| 161 |
{ |
| 162 |
crtc->id = id; |
| 163 |
crtc->timestamp = rrcrtc->timestamp; |
| 164 |
crtc->x = rrcrtc->x; |
| 165 |
crtc->y = rrcrtc->y; |
| 166 |
crtc->width = rrcrtc->width; |
| 167 |
crtc->height = rrcrtc->height; |
| 168 |
crtc->mode = rrcrtc->mode; |
| 169 |
crtc->rotation = rrcrtc->rotation; |
| 170 |
|
| 171 |
_al_vector_init(&crtc->connected, sizeof(RROutput)); |
| 172 |
if(rrcrtc->noutput) { |
| 173 |
_al_vector_append_array(&crtc->connected, rrcrtc->noutput, rrcrtc->outputs); |
| 174 |
} |
| 175 |
|
| 176 |
ALLEGRO_DEBUG("found %i outputs.\n", rrcrtc->noutput); |
| 177 |
|
| 178 |
_al_vector_init(&crtc->possible, sizeof(RROutput)); |
| 179 |
if(rrcrtc->npossible) { |
| 180 |
_al_vector_append_array(&crtc->possible, rrcrtc->npossible, rrcrtc->possible); |
| 181 |
|
| 182 |
int i; |
| 183 |
for(i = 0; i < rrcrtc->npossible; i++) { |
| 184 |
ALLEGRO_DEBUG("output[%i] %i.\n", i, (int)rrcrtc->possible[i]); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
crtc->original_mode = crtc->mode; |
| 189 |
crtc->original_xoff = crtc->x; |
| 190 |
crtc->original_yoff = crtc->y; |
| 191 |
crtc->align_to = 0; |
| 192 |
crtc->align = CRTC_POS_NONE; |
| 193 |
} |
| 194 |
|
| 195 |
static void xrandr_copy_screen(ALLEGRO_SYSTEM_XGLX *s, xrandr_screen *screen, XRRScreenResources *res) |
| 196 |
{ |
| 197 |
int j; |
| 198 |
|
| 199 |
_al_vector_init(&screen->modes, sizeof(xrandr_mode)); |
| 200 |
if(res->nmode) { |
| 201 |
for(j = 0; j < res->nmode; j++) { |
| 202 |
xrandr_mode *mode = _al_vector_alloc_back(&screen->modes); |
| 203 |
xrandr_copy_mode(mode, &res->modes[j]); |
| 204 |
} |
| 205 |
|
| 206 |
xrandr_clear_fake_refresh_rates(_al_vector_ref_front(&screen->modes), res->nmode); |
| 207 |
} |
| 208 |
|
| 209 |
_al_vector_init(&screen->crtcs, sizeof(xrandr_crtc)); |
| 210 |
if(res->ncrtc) { |
| 211 |
ALLEGRO_DEBUG("found %i crtcs.\n", res->ncrtc); |
| 212 |
for(j = 0; j < res->ncrtc; j++) { |
| 213 |
ALLEGRO_DEBUG("crtc[%i] %i.\n", j, (int)res->crtcs[j]); |
| 214 |
xrandr_crtc *crtc = _al_vector_alloc_back(&screen->crtcs); |
| 215 |
XRRCrtcInfo *rrcrtc = XRRGetCrtcInfo(s->x11display, res, res->crtcs[j]); |
| 216 |
|
| 217 |
xrandr_copy_crtc(crtc, res->crtcs[j], rrcrtc); |
| 218 |
|
| 219 |
XRRFreeCrtcInfo(rrcrtc); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
_al_vector_init(&screen->outputs, sizeof(xrandr_output)); |
| 224 |
if(res->noutput) { |
| 225 |
ALLEGRO_DEBUG("found %i outputs.\n", res->noutput); |
| 226 |
for(j = 0; j < res->noutput; j++) { |
| 227 |
ALLEGRO_DEBUG("output[%i] %i.\n", j, (int)res->outputs[j]); |
| 228 |
xrandr_output *output = _al_vector_alloc_back(&screen->outputs); |
| 229 |
XRROutputInfo *rroutput = XRRGetOutputInfo(s->x11display, res, res->outputs[j]); |
| 230 |
|
| 231 |
xrandr_copy_output(output, res->outputs[j], rroutput); |
| 232 |
|
| 233 |
XRRFreeOutputInfo(rroutput); |
| 234 |
XSync(s->x11display, False); |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
static xrandr_crtc *xrandr_fetch_crtc(ALLEGRO_SYSTEM_XGLX *s, int sid, RRCrtc id) |
| 240 |
{ |
| 241 |
unsigned int i; |
| 242 |
|
| 243 |
xrandr_screen *screen = _al_vector_ref(&s->xrandr_screens, sid); |
| 244 |
|
| 245 |
for(i = 0; i < _al_vector_size(&screen->crtcs); i++) { |
| 246 |
xrandr_crtc *crtc = _al_vector_ref(&screen->crtcs, i); |
| 247 |
if(crtc->id == id) |
| 248 |
return crtc; |
| 249 |
} |
| 250 |
|
| 251 |
return NULL; |
| 252 |
} |
| 253 |
|
| 254 |
static xrandr_output *xrandr_fetch_output(ALLEGRO_SYSTEM_XGLX *s, int sid, RROutput id) |
| 255 |
{ |
| 256 |
unsigned int i; |
| 257 |
|
| 258 |
xrandr_screen *screen = _al_vector_ref(&s->xrandr_screens, sid); |
| 259 |
|
| 260 |
for(i = 0; i < _al_vector_size(&screen->outputs); i++) { |
| 261 |
xrandr_output *output = _al_vector_ref(&screen->outputs, i); |
| 262 |
if(output->id == id) |
| 263 |
return output; |
| 264 |
} |
| 265 |
|
| 266 |
return NULL; |
| 267 |
} |
| 268 |
|
| 269 |
static xrandr_mode *xrandr_fetch_mode(ALLEGRO_SYSTEM_XGLX *s, int sid, RRMode id) |
| 270 |
{ |
| 271 |
unsigned int i; |
| 272 |
|
| 273 |
xrandr_screen *screen = _al_vector_ref(&s->xrandr_screens, sid); |
| 274 |
|
| 275 |
for(i = 0; i < _al_vector_size(&screen->modes); i++) { |
| 276 |
xrandr_mode *mode = _al_vector_ref(&screen->modes, i); |
| 277 |
if(mode->id == id) |
| 278 |
return mode; |
| 279 |
} |
| 280 |
|
| 281 |
return NULL; |
| 282 |
} |
| 283 |
|
| 284 |
static inline xrandr_crtc *xrandr_map_to_crtc(ALLEGRO_SYSTEM_XGLX *s, int sid, int adapter) |
| 285 |
{ |
| 286 |
return xrandr_fetch_crtc(s, sid, *(RRCrtc*)_al_vector_ref(&s->xrandr_adaptermap, adapter)); |
| 287 |
} |
| 288 |
|
| 289 |
static inline xrandr_output *xrandr_map_adapter(ALLEGRO_SYSTEM_XGLX *s, int sid, int adapter) |
| 290 |
{ |
| 291 |
xrandr_crtc *crtc = xrandr_map_to_crtc(s, sid, adapter); |
| 292 |
return xrandr_fetch_output(s, sid, *(RROutput*)_al_vector_ref(&crtc->connected, 0)); |
| 293 |
} |
| 294 |
|
| 295 |
static void xrandr_combine_output_rect(struct xrandr_rect *rect, xrandr_crtc *crtc) |
| 296 |
{ |
| 297 |
if(rect->x1 > crtc->x) |
| 298 |
rect->x1 = crtc->x; |
| 299 |
|
| 300 |
if(rect->y1 > crtc->y) |
| 301 |
rect->y1 = crtc->y; |
| 302 |
|
| 303 |
if(crtc->x + (int)crtc->width > rect->x2) |
| 304 |
rect->x2 = crtc->x + crtc->width; |
| 305 |
|
| 306 |
if(crtc->y + (int)crtc->height > rect->y2) |
| 307 |
rect->y2 = crtc->y + crtc->height; |
| 308 |
} |
| 309 |
|
| 310 |
/* begin vtable methods */ |
| 311 |
|
| 312 |
static int xrandr_get_xscreen(ALLEGRO_SYSTEM_XGLX *s, int adapter); |
| 313 |
|
| 314 |
static bool xrandr_query(ALLEGRO_SYSTEM_XGLX *s) |
| 315 |
{ |
| 316 |
int screen_count = ScreenCount(s->x11display); |
| 317 |
int i; |
| 318 |
bool ret = true; |
| 319 |
|
| 320 |
_al_vector_init(&s->xrandr_screens, sizeof(xrandr_screen)); |
| 321 |
_al_vector_init(&s->xrandr_adaptermap, sizeof(RROutput)); |
| 322 |
|
| 323 |
for(i = 0; i < screen_count; i++) { |
| 324 |
xrandr_screen *screen = _al_vector_alloc_back(&s->xrandr_screens); |
| 325 |
|
| 326 |
XRRScreenResources *res = XRRGetScreenResources(s->x11display, XRootWindow(s->x11display, i)); |
| 327 |
if(!res) { |
| 328 |
ALLEGRO_DEBUG("failed to get screen resources for screen %i\n", i); |
| 329 |
continue; |
| 330 |
} |
| 331 |
|
| 332 |
if(!res->noutput) { |
| 333 |
ALLEGRO_DEBUG("screen %i doesn't have any outputs.\n", i); |
| 334 |
continue; |
| 335 |
} |
| 336 |
|
| 337 |
xrandr_copy_screen(s, screen, res); |
| 338 |
|
| 339 |
screen->res = res; |
| 340 |
|
| 341 |
/* Detect clones */ |
| 342 |
int j; |
| 343 |
for(j = 0; j < (int)_al_vector_size(&screen->crtcs); j++) { |
| 344 |
xrandr_crtc *crtc = _al_vector_ref(&screen->crtcs, j); |
| 345 |
|
| 346 |
/* Skip crtc with no connected outputs. */ |
| 347 |
if(_al_vector_size(&crtc->connected) < 1) |
| 348 |
continue; |
| 349 |
|
| 350 |
// XXX it might be nessesary to do something more clever about detecting clones |
| 351 |
// XXX for now I'm going with a plain origin test, it aught to work for most cases. |
| 352 |
// the other alternative is to check the crtc's connected outputs's clone's list... |
| 353 |
// sounds like pain to me. |
| 354 |
int k; |
| 355 |
bool not_clone = true; |
| 356 |
for(k = 0; k < j; k++) { |
| 357 |
xrandr_crtc *crtc_k = _al_vector_ref(&screen->crtcs, k); |
| 358 |
|
| 359 |
/* Skip crtc with no connected outputs. */ |
| 360 |
if(_al_vector_size(&crtc_k->connected) < 1) |
| 361 |
continue; |
| 362 |
|
| 363 |
if(crtc->x == crtc_k->x && crtc->y == crtc_k->y) |
| 364 |
not_clone = false; |
| 365 |
} |
| 366 |
|
| 367 |
if(not_clone) { |
| 368 |
RRCrtc *crtc_ptr = _al_vector_alloc_back(&s->xrandr_adaptermap); |
| 369 |
ALLEGRO_DEBUG("Map Allegro Adadpter %i to RandR CRTC %i.\n", (int)(_al_vector_size(&s->xrandr_adaptermap)-1), (int)crtc->id); |
| 370 |
*crtc_ptr = crtc->id; |
| 371 |
} |
| 372 |
else { |
| 373 |
ALLEGRO_DEBUG("RandR CRTC %i is a clone, ignoring.\n", (int)crtc->id); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
int mask = RRScreenChangeNotifyMask | |
| 378 |
RRCrtcChangeNotifyMask | |
| 379 |
RROutputChangeNotifyMask | |
| 380 |
RROutputPropertyNotifyMask; |
| 381 |
|
| 382 |
XRRSelectInput( s->x11display, RootWindow(s->x11display, i), 0); |
| 383 |
XRRSelectInput( s->x11display, RootWindow(s->x11display, i), mask); |
| 384 |
} |
| 385 |
|
| 386 |
/* map out crtc positions and alignments */ |
| 387 |
/* this code makes Adapter 0 the root display, everything will hang off it */ |
| 388 |
for(i = 1; i < (int)_al_vector_size(&s->xrandr_adaptermap); i++) { |
| 389 |
int xscreen = xrandr_get_xscreen(s, i); |
| 390 |
xrandr_crtc *crtc = xrandr_fetch_crtc(s, xscreen, *(RRCrtc*)_al_vector_ref(&s->xrandr_adaptermap, i)); |
| 391 |
|
| 392 |
int j; |
| 393 |
for(j = 0; j < (int)_al_vector_size(&s->xrandr_adaptermap); j++) { |
| 394 |
int xscreen_j = xrandr_get_xscreen(s, j); |
| 395 |
xrandr_crtc *crtc_j = xrandr_fetch_crtc(s, xscreen_j, *(RRCrtc*)_al_vector_ref(&s->xrandr_adaptermap, j)); |
| 396 |
|
| 397 |
if(crtc->x == crtc_j->x + (int)crtc_j->width) { |
| 398 |
crtc->align_to = crtc_j->id; |
| 399 |
crtc->align = CRTC_POS_RIGHTOF; |
| 400 |
ALLEGRO_DEBUG("Adapter %i is RightOf Adapter %i.\n", i, j); |
| 401 |
} |
| 402 |
else if(crtc->x + (int)crtc->width == crtc_j->x) { |
| 403 |
crtc->align_to = crtc_j->id; |
| 404 |
crtc->align = CRTC_POS_LEFTOF; |
| 405 |
ALLEGRO_DEBUG("Adapter %i is LeftOf Adapter %i.\n", i, j); |
| 406 |
} |
| 407 |
else if(crtc->y == crtc_j->y + (int)crtc_j->height) { |
| 408 |
crtc->align_to = crtc_j->id; |
| 409 |
crtc->align = CRTC_POS_BELOW; |
| 410 |
ALLEGRO_DEBUG("Adapter %i is Below Adapter %i.\n", i, j); |
| 411 |
} |
| 412 |
else if(crtc->y + (int)crtc->height == crtc_j->y) { |
| 413 |
crtc->align_to = crtc_j->id; |
| 414 |
crtc->align = CRTC_POS_ABOVE; |
| 415 |
ALLEGRO_DEBUG("Adapter %i is Above Adapter %i.\n", i, j); |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
#ifdef ALLEGRO_XWINDOWS_WITH_XINERAMA |
| 421 |
if (s->xinerama_available && s->xinerama_screen_count != (int)_al_vector_size(&s->xrandr_adaptermap)) { |
| 422 |
ALLEGRO_WARN("XRandR and Xinerama seem to disagree on how many screens there are (%i vs %i), going to ignore XRandR.\n", (int)_al_vector_size(&s->xrandr_adaptermap), s->xinerama_screen_count); |
| 423 |
// only actually going to ignore the output count, and setting of modes on the extra xinerama screens. |
| 424 |
ret = false; |
| 425 |
} |
| 426 |
#endif |
| 427 |
|
| 428 |
return ret; |
| 429 |
} |
| 430 |
|
| 431 |
static int xrandr_get_num_modes(ALLEGRO_SYSTEM_XGLX *s, int adapter) |
| 432 |
{ |
| 433 |
if(adapter < 0 || adapter >= (int)_al_vector_size(&s->xrandr_adaptermap)) |
| 434 |
return 0; |
| 435 |
|
| 436 |
int xscreen = _al_xglx_get_xscreen(s, adapter); |
| 437 |
xrandr_output *output = xrandr_map_adapter(s, xscreen, adapter); |
| 438 |
return _al_vector_size(&output->modes); |
| 439 |
} |
| 440 |
|
| 441 |
static ALLEGRO_DISPLAY_MODE *xrandr_get_mode(ALLEGRO_SYSTEM_XGLX *s, int adapter, int id, ALLEGRO_DISPLAY_MODE *amode) |
| 442 |
{ |
| 443 |
int xscreen = _al_xglx_get_xscreen(s, adapter); |
| 444 |
xrandr_output *output = xrandr_map_adapter(s, xscreen, adapter); |
| 445 |
|
| 446 |
if(id < 0 || id > (int)_al_vector_size(&output->modes)) |
| 447 |
return NULL; |
| 448 |
|
| 449 |
xrandr_mode *mode = xrandr_fetch_mode(s, xscreen, *(RRMode*)_al_vector_ref(&output->modes, id)); |
| 450 |
|
| 451 |
amode->width = mode->width; |
| 452 |
amode->height = mode->height; |
| 453 |
amode->format = 0; |
| 454 |
amode->refresh_rate = mode->refresh; |
| 455 |
|
| 456 |
return amode; |
| 457 |
} |
| 458 |
|
| 459 |
static bool xrandr_realign_crtc_origin(ALLEGRO_SYSTEM_XGLX *s, int xscreen, xrandr_crtc *crtc, int new_w, int new_h, int *x, int *y) |
| 460 |
{ |
| 461 |
bool ret; |
| 462 |
|
| 463 |
if(crtc->align_to == 0) |
| 464 |
return false; |
| 465 |
|
| 466 |
xrandr_crtc *align_to = xrandr_fetch_crtc(s, xscreen, crtc->align_to); |
| 467 |
|
| 468 |
switch(crtc->align) { |
| 469 |
case CRTC_POS_RIGHTOF: |
| 470 |
*x = align_to->x + align_to->width; |
| 471 |
*y = align_to->y; |
| 472 |
ret = true; |
| 473 |
break; |
| 474 |
|
| 475 |
case CRTC_POS_LEFTOF: |
| 476 |
*x = align_to->x - new_w; |
| 477 |
*y = align_to->y; |
| 478 |
ret = true; |
| 479 |
break; |
| 480 |
|
| 481 |
case CRTC_POS_BELOW: |
| 482 |
*x = align_to->x; |
| 483 |
*y = align_to->y + align_to->height; |
| 484 |
ret = true; |
| 485 |
break; |
| 486 |
|
| 487 |
case CRTC_POS_ABOVE: |
| 488 |
*x = align_to->x; |
| 489 |
*y = align_to->y - new_h; |
| 490 |
ret = true; |
| 491 |
break; |
| 492 |
|
| 493 |
default: |
| 494 |
ALLEGRO_WARN("unknown crtc alignment flag (%i)!", crtc->align); |
| 495 |
ret = false; |
| 496 |
break; |
| 497 |
} |
| 498 |
|
| 499 |
return ret; |
| 500 |
} |
| 501 |
|
| 502 |
static bool xrandr_set_mode(ALLEGRO_SYSTEM_XGLX *s, ALLEGRO_DISPLAY_XGLX *d, int w, int h, int format, int refresh) |
| 503 |
{ |
| 504 |
int adapter = _al_xglx_get_adapter(s, d, false); |
| 505 |
int xscreen = _al_xglx_get_xscreen(s, adapter); |
| 506 |
|
| 507 |
xrandr_screen *screen = _al_vector_ref(&s->xrandr_screens, xscreen); |
| 508 |
|
| 509 |
xrandr_crtc *crtc = xrandr_map_to_crtc(s, xscreen, adapter); |
| 510 |
xrandr_mode *cur_mode = xrandr_fetch_mode(s, xscreen, crtc->mode); |
| 511 |
|
| 512 |
if((int)cur_mode->width == w && (int)cur_mode->height == h && (refresh == 0 || refresh == (int)cur_mode->refresh)) { |
| 513 |
ALLEGRO_DEBUG("mode already set, good to go\n"); |
| 514 |
return true; |
| 515 |
} |
| 516 |
else { |
| 517 |
ALLEGRO_DEBUG("new mode: %dx%d@%d old mode: %dx%d@%d.\n", w, h, refresh, cur_mode->width, cur_mode->height, cur_mode->refresh); |
| 518 |
} |
| 519 |
|
| 520 |
int mode_idx = _al_xglx_fullscreen_select_mode(s, adapter, w, h, format, refresh); |
| 521 |
if(mode_idx == -1) { |
| 522 |
ALLEGRO_DEBUG("mode %dx%d@%d not found\n", w, h, refresh); |
| 523 |
return false; |
| 524 |
} |
| 525 |
|
| 526 |
xrandr_output *output = xrandr_fetch_output(s, xscreen, *(RROutput*)_al_vector_ref(&crtc->connected, 0)); |
| 527 |
xrandr_mode *mode = xrandr_fetch_mode(s, xscreen, *(RRMode*)_al_vector_ref(&output->modes, mode_idx)); |
| 528 |
|
| 529 |
int new_x = crtc->x, new_y = crtc->y; |
| 530 |
|
| 531 |
xrandr_realign_crtc_origin(s, xscreen, crtc, w, h, &new_x, &new_y); |
| 532 |
|
| 533 |
ALLEGRO_DEBUG("xrandr: set mode %i+%i-%ix%i on adapter %i\n", new_x, new_y, w, h, adapter); |
| 534 |
|
| 535 |
_al_mutex_lock(&s->lock); |
| 536 |
|
| 537 |
int ok = XRRSetCrtcConfig( |
| 538 |
s->x11display, |
| 539 |
screen->res, |
| 540 |
crtc->id, |
| 541 |
crtc->timestamp, |
| 542 |
new_x, |
| 543 |
new_y, |
| 544 |
mode->id, |
| 545 |
crtc->rotation, |
| 546 |
_al_vector_ref_front(&crtc->connected), |
| 547 |
_al_vector_size(&crtc->connected) |
| 548 |
); |
| 549 |
|
| 550 |
if (ok != RRSetConfigSuccess) { |
| 551 |
ALLEGRO_ERROR("XRandR failed to set mode.\n"); |
| 552 |
_al_mutex_unlock(&s->lock); |
| 553 |
return false; |
| 554 |
} |
| 555 |
|
| 556 |
/* make sure the virtual screen size is large enough after setting our mode */ |
| 557 |
int i; |
| 558 |
struct xrandr_rect rect = { 0, 0, 0, 0 }; |
| 559 |
for(i = 0; i < (int)_al_vector_size(&screen->crtcs); i++) { |
| 560 |
xrandr_crtc *c = _al_vector_ref(&screen->crtcs, i); |
| 561 |
if(_al_vector_size(&c->connected) > 0) { |
| 562 |
xrandr_combine_output_rect(&rect, crtc); |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
int new_width = rect.x2 - rect.x1; |
| 567 |
int new_height = rect.y2 - rect.y1; |
| 568 |
|
| 569 |
if(new_width > DisplayWidth(s->x11display, xscreen) || |
| 570 |
new_height > DisplayHeight(s->x11display, xscreen)) |
| 571 |
{ |
| 572 |
XRRSetScreenSize(s->x11display, |
| 573 |
RootWindow(s->x11display, xscreen), |
| 574 |
new_width, new_height, |
| 575 |
DisplayWidthMM(s->x11display, xscreen), |
| 576 |
DisplayHeightMM(s->x11display, xscreen)); |
| 577 |
} |
| 578 |
|
| 579 |
_al_mutex_unlock(&s->lock); |
| 580 |
|
| 581 |
return true; |
| 582 |
} |
| 583 |
|
| 584 |
static void xrandr_restore_mode(ALLEGRO_SYSTEM_XGLX *s, int adapter) |
| 585 |
{ |
| 586 |
int xscreen = _al_xglx_get_xscreen(s, adapter); |
| 587 |
xrandr_screen *screen = _al_vector_ref(&s->xrandr_screens, xscreen); |
| 588 |
xrandr_crtc *crtc = xrandr_map_to_crtc(s, xscreen, adapter); |
| 589 |
|
| 590 |
if(crtc->mode == crtc->original_mode) { |
| 591 |
ALLEGRO_DEBUG("current crtc mode (%i) equals the original mode (%i), not restoring.\n", (int)crtc->mode, (int)crtc->original_mode); |
| 592 |
return; |
| 593 |
} |
| 594 |
|
| 595 |
xrandr_mode *orig_mode = xrandr_fetch_mode(s, xscreen, crtc->original_mode); |
| 596 |
|
| 597 |
ALLEGRO_DEBUG("restore mode %i+%i-%ix%i@%i on adapter %i\n", crtc->original_xoff, crtc->original_yoff, orig_mode->width, orig_mode->height, orig_mode->refresh, adapter); |
| 598 |
|
| 599 |
_al_mutex_lock(&s->lock); |
| 600 |
|
| 601 |
int ok = XRRSetCrtcConfig |
| 602 |
( |
| 603 |
s->x11display, |
| 604 |
screen->res, |
| 605 |
crtc->id, |
| 606 |
crtc->timestamp, |
| 607 |
crtc->original_xoff, |
| 608 |
crtc->original_yoff, |
| 609 |
orig_mode->id, |
| 610 |
crtc->rotation, |
| 611 |
_al_vector_ref_front(&crtc->connected), |
| 612 |
_al_vector_size(&crtc->connected) |
| 613 |
); |
| 614 |
|
| 615 |
if(ok != RRSetConfigSuccess) { |
| 616 |
ALLEGRO_ERROR("failed to restore mode.\n"); |
| 617 |
} |
| 618 |
|
| 619 |
// XSync(s->x11display, False); |
| 620 |
|
| 621 |
_al_mutex_unlock(&s->lock); |
| 622 |
} |
| 623 |
|
| 624 |
static void xrandr_get_display_offset(ALLEGRO_SYSTEM_XGLX *s, int adapter, int *x, int *y) |
| 625 |
{ |
| 626 |
int xscreen = _al_xglx_get_xscreen(s, adapter); |
| 627 |
|
| 628 |
xrandr_crtc *crtc = xrandr_map_to_crtc(s, xscreen, adapter); |
| 629 |
|
| 630 |
// XXX Should we always return original_[xy]off here? |
| 631 |
// When does a user want to query the offset after the modes are set? |
| 632 |
|
| 633 |
*x = crtc->x; |
| 634 |
*y = crtc->y; |
| 635 |
|
| 636 |
ALLEGRO_DEBUG("display offset: %ix%i.\n", *x, *y); |
| 637 |
} |
| 638 |
|
| 639 |
static int xrandr_get_num_adapters(ALLEGRO_SYSTEM_XGLX *s) |
| 640 |
{ |
| 641 |
return _al_vector_size(&s->xrandr_adaptermap); |
| 642 |
} |
| 643 |
|
| 644 |
static bool xrandr_get_monitor_info(ALLEGRO_SYSTEM_XGLX *s, int adapter, ALLEGRO_MONITOR_INFO *mi) |
| 645 |
{ |
| 646 |
if(adapter < 0 || adapter >= (int)_al_vector_size(&s->xrandr_adaptermap)) |
| 647 |
return false; |
| 648 |
|
| 649 |
int xscreen = _al_xglx_get_xscreen(s, adapter); |
| 650 |
xrandr_output *output = xrandr_map_adapter(s, xscreen, adapter); |
| 651 |
|
| 652 |
xrandr_crtc *crtc = xrandr_fetch_crtc(s, xscreen, output->crtc); |
| 653 |
|
| 654 |
if (!crtc) |
| 655 |
return false; |
| 656 |
|
| 657 |
mi->x1 = crtc->x; |
| 658 |
mi->y1 = crtc->y; |
| 659 |
mi->x2 = crtc->x + crtc->width; |
| 660 |
mi->y2 = crtc->y + crtc->height; |
| 661 |
return true; |
| 662 |
} |
| 663 |
|
| 664 |
static int xrandr_get_default_adapter(ALLEGRO_SYSTEM_XGLX *s) |
| 665 |
{ |
| 666 |
// if we have more than one xrandr_screen, we're in multi-head x mode |
| 667 |
if(_al_vector_size(&s->xrandr_screens) > 1) |
| 668 |
return _al_xsys_mheadx_get_default_adapter(s); |
| 669 |
|
| 670 |
int center_x = 0, center_y = 0; |
| 671 |
_al_xsys_get_active_window_center(s, ¢er_x, ¢er_y); |
| 672 |
|
| 673 |
int i, default_adapter = 0; |
| 674 |
for(i = 0; i < (int)_al_vector_size(&s->xrandr_adaptermap); i++) { |
| 675 |
xrandr_crtc *crtc = xrandr_map_to_crtc(s, 0, i); |
| 676 |
|
| 677 |
if(center_x >= (int)crtc->x && center_x <= (int)(crtc->x + crtc->width) && |
| 678 |
center_y >= (int)crtc->y && center_y <= (int)(crtc->y + crtc->height)) |
| 679 |
{ |
| 680 |
default_adapter = i; |
| 681 |
break; |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
ALLEGRO_DEBUG("selected default adapter: %i.\n", default_adapter); |
| 686 |
|
| 687 |
return default_adapter; |
| 688 |
} |
| 689 |
|
| 690 |
static int xrandr_get_xscreen(ALLEGRO_SYSTEM_XGLX *s, int adapter) |
| 691 |
{ |
| 692 |
// more than one screen means we have multi-head x mode |
| 693 |
if(_al_vector_size(&s->xrandr_screens) > 1) |
| 694 |
return _al_xsys_mheadx_get_xscreen(s, adapter); |
| 695 |
|
| 696 |
// Normal XRandR will normally give us one X screen, so return 0 always. |
| 697 |
return 0; |
| 698 |
} |
| 699 |
|
| 700 |
static void xrandr_handle_xevent(ALLEGRO_SYSTEM_XGLX *s, ALLEGRO_DISPLAY_XGLX *d, XEvent *e) |
| 701 |
{ |
| 702 |
if(e->type == s->xrandr_event_base + RRNotify) { |
| 703 |
XRRNotifyEvent *rre = (XRRNotifyEvent*)e; |
| 704 |
if(rre->subtype == RRNotify_CrtcChange) { |
| 705 |
XRRCrtcChangeNotifyEvent *rrce = (XRRCrtcChangeNotifyEvent*)rre; |
| 706 |
ALLEGRO_DEBUG("RRNotify_CrtcChange!\n"); |
| 707 |
|
| 708 |
xrandr_crtc *crtc = xrandr_fetch_crtc(s, d->xscreen, rrce->crtc); |
| 709 |
if(!crtc) { |
| 710 |
ALLEGRO_DEBUG("invalid RRCrtc(%i).\n", (int)rrce->crtc); |
| 711 |
return; |
| 712 |
} |
| 713 |
|
| 714 |
if(rrce->mode != crtc->mode) { |
| 715 |
ALLEGRO_DEBUG("mode changed from %i to %i.\n", (int)crtc->mode, (int)rrce->mode); |
| 716 |
crtc->mode = rrce->mode; |
| 717 |
} |
| 718 |
|
| 719 |
if(rrce->rotation != crtc->rotation) { |
| 720 |
ALLEGRO_DEBUG("rotation changed from %i to %i.\n", crtc->rotation, rrce->rotation); |
| 721 |
crtc->rotation = rrce->rotation; |
| 722 |
} |
| 723 |
|
| 724 |
if(rrce->x != crtc->x || rrce->y != crtc->y) { |
| 725 |
ALLEGRO_DEBUG("origin changed from %i+%i to %i+%i.\n", crtc->x, crtc->y, rrce->x, rrce->y); |
| 726 |
crtc->x = rrce->x; |
| 727 |
crtc->y = rrce->y; |
| 728 |
} |
| 729 |
|
| 730 |
if(rrce->width != crtc->width || rrce->height != crtc->height) { |
| 731 |
ALLEGRO_DEBUG("size changed from %ix%i to %ix%i.\n", crtc->width, crtc->height, rrce->width, rrce->height); |
| 732 |
crtc->width = rrce->width; |
| 733 |
crtc->height = rrce->height; |
| 734 |
} |
| 735 |
|
| 736 |
xrandr_screen *screen = _al_vector_ref(&s->xrandr_screens, d->xscreen); |
| 737 |
crtc->timestamp = screen->timestamp; |
| 738 |
} |
| 739 |
else if(rre->subtype == RRNotify_OutputChange) { |
| 740 |
XRROutputChangeNotifyEvent *rroe = (XRROutputChangeNotifyEvent*)rre; |
| 741 |
|
| 742 |
xrandr_output *output = xrandr_fetch_output(s, d->xscreen, rroe->output); |
| 743 |
if(!output) { |
| 744 |
ALLEGRO_DEBUG("invalid RROutput(%i).\n", (int)rroe->output); |
| 745 |
return; |
| 746 |
} |
| 747 |
|
| 748 |
ALLEGRO_DEBUG("xrandr: RRNotify_OutputChange %s!\n", output->name); |
| 749 |
|
| 750 |
if(rroe->crtc != output->crtc) { |
| 751 |
ALLEGRO_DEBUG("crtc changed from %i to %i.\n", (int)output->crtc, (int)rroe->crtc); |
| 752 |
output->crtc = rroe->crtc; |
| 753 |
} |
| 754 |
|
| 755 |
// XXX I'm not sure how monitor connections are handled here, |
| 756 |
// that is, what happens when a monitor is connected, and disconnected... |
| 757 |
// IE: CHECK! |
| 758 |
if(rroe->connection != output->connection) { |
| 759 |
ALLEGRO_DEBUG("connection changed from %i to %i.\n", output->connection, rroe->connection); |
| 760 |
output->connection = rroe->connection; |
| 761 |
} |
| 762 |
|
| 763 |
xrandr_screen *screen = _al_vector_ref(&s->xrandr_screens, d->xscreen); |
| 764 |
output->timestamp = screen->timestamp; |
| 765 |
} |
| 766 |
else if(rre->subtype == RRNotify_OutputProperty) { |
| 767 |
ALLEGRO_DEBUG("xrandr: RRNotify_OutputProperty!\n"); |
| 768 |
} |
| 769 |
else { |
| 770 |
ALLEGRO_DEBUG("xrandr: RRNotify_Unknown(%i)!\n", rre->subtype); |
| 771 |
} |
| 772 |
} |
| 773 |
else if(e->type == s->xrandr_event_base + RRScreenChangeNotify) { |
| 774 |
XRRScreenChangeNotifyEvent *rre = (XRRScreenChangeNotifyEvent*)e; |
| 775 |
XRRUpdateConfiguration( e ); |
| 776 |
|
| 777 |
ALLEGRO_DEBUG("RRScreenChangeNotify!\n"); |
| 778 |
|
| 779 |
/* XXX I don't think we need to actually handle this event fully, |
| 780 |
* it only really deals with the virtual screen as a whole it seems |
| 781 |
* The interesting changes get sent with the RRNotify event. |
| 782 |
*/ |
| 783 |
|
| 784 |
/* update the timestamps */ |
| 785 |
xrandr_screen *screen = _al_vector_ref(&s->xrandr_screens, d->xscreen); |
| 786 |
screen->timestamp = rre->timestamp; |
| 787 |
screen->configTimestamp = rre->config_timestamp; |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
/* begin "public" ctor/dtor methods */ |
| 792 |
|
| 793 |
void _al_xsys_xrandr_init(ALLEGRO_SYSTEM_XGLX *s) |
| 794 |
{ |
| 795 |
int error_base = 0; |
| 796 |
|
| 797 |
_al_mutex_lock(&s->lock); |
| 798 |
|
| 799 |
if (XRRQueryExtension(s->x11display, &s->xrandr_event_base, &error_base)) { |
| 800 |
int minor_version = 0, major_version = 0; |
| 801 |
int status = XRRQueryVersion(s->x11display, &major_version, &minor_version); |
| 802 |
ALLEGRO_INFO("XRandR version: %i.%i\n", major_version, minor_version); |
| 803 |
|
| 804 |
if (!status) { |
| 805 |
ALLEGRO_WARN("XRandR not available, XRRQueryVersion failed.\n"); |
| 806 |
} |
| 807 |
else if (major_version == 1 && minor_version < 2) { |
| 808 |
/* this is unlikely to happen, unless the user has an ancient Xorg, |
| 809 |
Xorg will just emulate the latest version and return that |
| 810 |
instead of what the driver actually supports, stupid. */ |
| 811 |
ALLEGRO_WARN("XRandR not available, unsupported version: %i.%i\n", major_version, minor_version); |
| 812 |
} |
| 813 |
else { |
| 814 |
bool ret = xrandr_query(s); |
| 815 |
if (ret) { |
| 816 |
ALLEGRO_INFO("XRandR is active\n"); |
| 817 |
s->xrandr_available = 1; |
| 818 |
} |
| 819 |
else { |
| 820 |
ALLEGRO_INFO("XRandR is not active\n"); |
| 821 |
} |
| 822 |
} |
| 823 |
} |
| 824 |
else { |
| 825 |
ALLEGRO_WARN("XRandR extension is not available.\n"); |
| 826 |
} |
| 827 |
|
| 828 |
if (s->xrandr_available) { |
| 829 |
memset(&_al_xglx_mmon_interface, 0, sizeof(_al_xglx_mmon_interface)); |
| 830 |
_al_xglx_mmon_interface.get_num_display_modes = xrandr_get_num_modes; |
| 831 |
_al_xglx_mmon_interface.get_display_mode = xrandr_get_mode; |
| 832 |
_al_xglx_mmon_interface.set_mode = xrandr_set_mode; |
| 833 |
_al_xglx_mmon_interface.restore_mode = xrandr_restore_mode; |
| 834 |
_al_xglx_mmon_interface.get_display_offset = xrandr_get_display_offset; |
| 835 |
_al_xglx_mmon_interface.get_num_adapters = xrandr_get_num_adapters; |
| 836 |
_al_xglx_mmon_interface.get_monitor_info = xrandr_get_monitor_info; |
| 837 |
_al_xglx_mmon_interface.get_default_adapter = xrandr_get_default_adapter; |
| 838 |
_al_xglx_mmon_interface.get_xscreen = xrandr_get_xscreen; |
| 839 |
_al_xglx_mmon_interface.handle_xevent = xrandr_handle_xevent; |
| 840 |
} |
| 841 |
|
| 842 |
_al_mutex_unlock(&s->lock); |
| 843 |
} |
| 844 |
|
| 845 |
void _al_xsys_xrandr_exit(ALLEGRO_SYSTEM_XGLX *s) |
| 846 |
{ |
| 847 |
#if 0 |
| 848 |
// int i; |
| 849 |
ALLEGRO_DEBUG("XRandR exiting.\n"); |
| 850 |
|
| 851 |
// for (i = 0; i < s->xrandr_output_count; i++) { |
| 852 |
// XRRFreeOutputInfo(s->xrandr_outputs[i]); |
| 853 |
// } |
| 854 |
|
| 855 |
// for (i = 0; i < s->xrandr_res_count; i++) { |
| 856 |
// XRRFreeScreenResources(s->xrandr_res[i]); |
| 857 |
// } |
| 858 |
|
| 859 |
ALLEGRO_DEBUG("XRRFreeScreenResources\n"); |
| 860 |
//if (s->xrandr_res) |
| 861 |
// XRRFreeScreenResources(s->xrandr_res); |
| 862 |
|
| 863 |
al_free(s->xrandr_outputs); |
| 864 |
al_free(s->xrandr_res); |
| 865 |
|
| 866 |
s->xrandr_available = 0; |
| 867 |
s->xrandr_res_count = 0; |
| 868 |
s->xrandr_res = NULL; |
| 869 |
s->xrandr_output_count = 0; |
| 870 |
s->xrandr_outputs = NULL; |
| 871 |
|
| 872 |
ALLEGRO_DEBUG("XRandR exit finished.\n"); |
| 873 |
#endif /* 0 */ |
| 874 |
|
| 875 |
int i; |
| 876 |
for(i = 0; i < (int)_al_vector_size(&s->xrandr_screens); i++) { |
| 877 |
xrandr_screen *screen = _al_vector_ref(&s->xrandr_screens, i); |
| 878 |
int j; |
| 879 |
|
| 880 |
for (j = 0; j < (int)_al_vector_size(&screen->crtcs); j++) { |
| 881 |
xrandr_crtc *crtc = _al_vector_ref(&screen->crtcs, j); |
| 882 |
_al_vector_free(&crtc->connected); |
| 883 |
_al_vector_free(&crtc->possible); |
| 884 |
} |
| 885 |
|
| 886 |
for(j = 0; j < (int)_al_vector_size(&screen->outputs); j++) { |
| 887 |
xrandr_output *output = _al_vector_ref(&screen->outputs, j); |
| 888 |
free(output->name); |
| 889 |
_al_vector_free(&output->crtcs); |
| 890 |
_al_vector_free(&output->clones); |
| 891 |
_al_vector_free(&output->modes); |
| 892 |
} |
| 893 |
|
| 894 |
_al_vector_free(&screen->crtcs); |
| 895 |
_al_vector_free(&screen->outputs); |
| 896 |
_al_vector_free(&screen->modes); |
| 897 |
|
| 898 |
XRRFreeScreenResources(screen->res); |
| 899 |
screen->res = NULL; |
| 900 |
} |
| 901 |
|
| 902 |
_al_vector_free(&s->xrandr_screens); |
| 903 |
_al_vector_free(&s->xrandr_adaptermap); |
| 904 |
|
| 905 |
} |
| 906 |
|
| 907 |
#endif /* ALLEGRO_XWINDOWS_WITH_XRANDR */ |
| 908 |
|
| 909 |
/* vim: set sts=3 sw=3 et: */ |