#include static bool IsExternalOutputName(byteview name) { return !StartsWithIgnoreCase(name, "eDP"_s) && !StartsWithIgnoreCase(name, "LVDS"_s); } static xcb_randr_mode_t RandrPickBestMode(xcb_randr_get_output_info_reply_t* infoReply, xcb_randr_mode_info_t* resourceModes, int numResourceModes) { xcb_randr_mode_t bestMode = XCB_NONE; uint64_t bestPixels = 0; float bestRefresh = 0.0f; xcb_randr_mode_t* outputModes = xcb_randr_get_output_info_modes(infoReply); for (int mi = 0; mi < infoReply->num_modes; ++mi) { xcb_randr_mode_t modeId = outputModes[mi]; xcb_randr_mode_info_t* modeInfo = nullptr; for (int ri = 0; ri < numResourceModes; ++ri) { if (resourceModes[ri].id == modeId) { modeInfo = &resourceModes[ri]; break; } } if (!modeInfo || !modeInfo->dot_clock || !modeInfo->htotal || !modeInfo->vtotal) continue; uint64_t pixels = (uint64_t)modeInfo->width * (uint64_t)modeInfo->height; if (pixels >= bestPixels) { float refresh = (float)modeInfo->dot_clock / ((float)modeInfo->htotal * (float)modeInfo->vtotal); if (pixels > bestPixels || refresh > bestRefresh) { bestPixels = pixels; bestRefresh = refresh; bestMode = modeId; } } } if (bestMode == XCB_NONE) return outputModes[0]; else return bestMode; } static bool RandRRefresh() { if (X11RandRGetEventOffset() == 0) return false; xcb_randr_get_screen_resources_cookie_t resCookie = xcb_randr_get_screen_resources(X11GetConn(), X11GetRoot()); xcb_randr_get_screen_resources_reply_t* resReply = xcb_randr_get_screen_resources_reply(X11GetConn(), resCookie, nullptr); if (!resReply) return false; int numOutputs = xcb_randr_get_screen_resources_outputs_length(resReply); xcb_randr_output_t* outputs = xcb_randr_get_screen_resources_outputs(resReply); int numResourceModes = xcb_randr_get_screen_resources_modes_length(resReply); xcb_randr_mode_info_t* resourceModes = xcb_randr_get_screen_resources_modes(resReply); xcb_randr_output_t bestOutput = XCB_NONE; xcb_randr_mode_t bestMode = XCB_NONE; for (int pass = 0; pass < 2; ++pass) { bool wantExternal = (pass == 0); for (int i = 0; i < numOutputs; ++i) { xcb_randr_get_output_info_cookie_t ic = xcb_randr_get_output_info(X11GetConn(), outputs[i], XCB_CURRENT_TIME); xcb_randr_get_output_info_reply_t* ir = xcb_randr_get_output_info_reply(X11GetConn(), ic, nullptr); if (!ir) continue; bool ext = IsExternalOutputName({ (const uint8_t*)xcb_randr_get_output_info_name(ir), (uint64_t)xcb_randr_get_output_info_name_length(ir), }); bool ok = ir->connection == XCB_RANDR_CONNECTION_CONNECTED && wantExternal == ext && ir->num_modes > 0; if (ok) { bestOutput = outputs[i]; bestMode = PickBestMode(ir, resourceModes, numResourceModes); } free(ir); if (bestOutput != XCB_NONE) break; } if (bestOutput != XCB_NONE) break; } free(resReply); if (bestOutput == XCB_NONE) return false; xcb_randr_get_screen_resources_cookie_t resCookie2 = xcb_randr_get_screen_resources(X11GetConn(), X11GetRoot()); xcb_randr_get_screen_resources_reply_t* resReply2 = xcb_randr_get_screen_resources_reply(X11GetConn(), resCookie2, nullptr); if (!resReply2) return false; int numOutputs2 = xcb_randr_get_screen_resources_outputs_length(resReply2); xcb_randr_output_t* outputs2 = xcb_randr_get_screen_resources_outputs(resReply2); for (int i = 0; i < numOutputs2; ++i) { if (outputs2[i] == bestOutput) continue; xcb_randr_get_output_info_cookie_t ic = xcb_randr_get_output_info(X11GetConn(), outputs2[i], XCB_CURRENT_TIME); xcb_randr_get_output_info_reply_t* ir = xcb_randr_get_output_info_reply(X11GetConn(), ic, nullptr); if (ir) { if (ir->crtc != XCB_NONE) xcb_randr_set_crtc_config(X11GetConn(), ir->crtc, XCB_CURRENT_TIME, resReply2->config_timestamp, 0, 0, XCB_NONE, XCB_RANDR_ROTATION_ROTATE_0, 0, nullptr); free(ir); } } // Fresh timestamp after disable loop -- each set_crtc_config invalidates the old one. xcb_randr_get_screen_resources_cookie_t resCookie3 = xcb_randr_get_screen_resources(X11GetConn(), X11GetRoot()); xcb_randr_get_screen_resources_reply_t* resReply3 = xcb_randr_get_screen_resources_reply(X11GetConn(), resCookie3, nullptr); if (!resReply3) { free(resReply2); return false; } free(resReply2); xcb_randr_get_output_info_cookie_t bic = xcb_randr_get_output_info(X11GetConn(), bestOutput, XCB_CURRENT_TIME); xcb_randr_get_output_info_reply_t* bir = xcb_randr_get_output_info_reply(X11GetConn(), bic, nullptr); if (bir) { xcb_randr_crtc_t useCrtc = bir->crtc; if (useCrtc == XCB_NONE) { int numCrtcs = xcb_randr_get_screen_resources_crtcs_length(resReply3); xcb_randr_crtc_t* crtcs = xcb_randr_get_screen_resources_crtcs(resReply3); for (int c = 0; c < numCrtcs; ++c) { xcb_randr_get_crtc_info_cookie_t cc = xcb_randr_get_crtc_info(X11GetConn(), crtcs[c], XCB_CURRENT_TIME); xcb_randr_get_crtc_info_reply_t* cr = xcb_randr_get_crtc_info_reply(X11GetConn(), cc, nullptr); if (cr) { if (xcb_randr_get_crtc_info_outputs_length(cr) == 0) { useCrtc = crtcs[c]; free(cr); break; } free(cr); } } } if (useCrtc != XCB_NONE && bestMode != XCB_NONE) xcb_randr_set_crtc_config(X11GetConn(), useCrtc, XCB_CURRENT_TIME, resReply3->config_timestamp, 0, 0, bestMode, XCB_RANDR_ROTATION_ROTATE_0, 1, &bestOutput); free(bir); } X11Flush(); xcb_randr_get_output_info_cookie_t fic = xcb_randr_get_output_info(X11GetConn(), bestOutput, XCB_CURRENT_TIME); xcb_randr_get_output_info_reply_t* fir = xcb_randr_get_output_info_reply(X11GetConn(), fic, nullptr); bool changed = false; if (fir && fir->crtc != XCB_NONE) { xcb_randr_get_crtc_info_cookie_t cc = xcb_randr_get_crtc_info(X11GetConn(), fir->crtc, XCB_CURRENT_TIME); xcb_randr_get_crtc_info_reply_t* cr = xcb_randr_get_crtc_info_reply(X11GetConn(), cc, nullptr); if (cr) { int32_t nx = cr->x, ny = cr->y; uint32_t nw = cr->width, nh = cr->height; free(cr); if (nx != wm->monitorX || ny != wm->monitorY || nw != wm->monitorWidth || nh != wm->monitorHeight) { wm->monitorX = nx; wm->monitorY = ny; wm->monitorWidth = nw; wm->monitorHeight = nh; changed = true; } } } if (fir) free(fir); free(resReply3); return changed; }