core: avoid locking 2 wayland sockets (#6971)

* fix: avoid locking 2 wayland sockets

* format

* fix formatting
This commit is contained in:
diniamo 2024-07-22 11:16:25 +00:00 committed by GitHub
parent 511e9ccdd1
commit d03fa94c2c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 24 additions and 53 deletions

View file

@ -207,7 +207,7 @@ void CCompositor::setRandomSplash() {
static std::vector<SP<Aquamarine::IOutput>> pendingOutputs; static std::vector<SP<Aquamarine::IOutput>> pendingOutputs;
// //
void CCompositor::initServer() { void CCompositor::initServer(std::string socketName, int socketFd) {
m_sWLDisplay = wl_display_create(); m_sWLDisplay = wl_display_create();
@ -267,16 +267,25 @@ void CCompositor::initServer() {
m_iDRMFD = m_pAqBackend->drmFD(); m_iDRMFD = m_pAqBackend->drmFD();
Debug::log(LOG, "Running on DRMFD: {}", m_iDRMFD); Debug::log(LOG, "Running on DRMFD: {}", m_iDRMFD);
// get socket, avoid using 0 if (!socketName.empty() && socketFd != -1) {
for (int candidate = 1; candidate <= 32; candidate++) { fcntl(socketFd, F_SETFD, FD_CLOEXEC);
const auto CANDIDATESTR = ("wayland-" + std::to_string(candidate)); const auto RETVAL = wl_display_add_socket_fd(m_sWLDisplay, socketFd);
const auto RETVAL = wl_display_add_socket(m_sWLDisplay, CANDIDATESTR.c_str());
if (RETVAL >= 0) { if (RETVAL >= 0) {
m_szWLDisplaySocket = CANDIDATESTR; m_szWLDisplaySocket = socketName;
Debug::log(LOG, "wl_display_add_socket for {} succeeded with {}", CANDIDATESTR, RETVAL); Debug::log(LOG, "wl_display_add_socket_fd for {} succeeded with {}", socketName, RETVAL);
break; } else
} else { Debug::log(WARN, "wl_display_add_socket_fd for {} returned {}: skipping", socketName, RETVAL);
Debug::log(WARN, "wl_display_add_socket for {} returned {}: skipping candidate {}", CANDIDATESTR, RETVAL, candidate); } else {
// get socket, avoid using 0
for (int candidate = 1; candidate <= 32; candidate++) {
const auto CANDIDATESTR = ("wayland-" + std::to_string(candidate));
const auto RETVAL = wl_display_add_socket(m_sWLDisplay, CANDIDATESTR.c_str());
if (RETVAL >= 0) {
m_szWLDisplaySocket = CANDIDATESTR;
Debug::log(LOG, "wl_display_add_socket for {} succeeded with {}", CANDIDATESTR, RETVAL);
break;
} else
Debug::log(WARN, "wl_display_add_socket for {} returned {}: skipping candidate {}", CANDIDATESTR, RETVAL, candidate);
} }
} }
@ -292,7 +301,6 @@ void CCompositor::initServer() {
throwError("m_szWLDisplaySocket was null! (wl_display_add_socket and wl_display_add_socket_auto failed)"); throwError("m_szWLDisplaySocket was null! (wl_display_add_socket and wl_display_add_socket_auto failed)");
} }
Debug::log(LOG, "Setting WAYLAND_DISPLAY to {}", m_szWLDisplaySocket);
setenv("WAYLAND_DISPLAY", m_szWLDisplaySocket.c_str(), 1); setenv("WAYLAND_DISPLAY", m_szWLDisplaySocket.c_str(), 1);
setenv("XDG_SESSION_TYPE", "wayland", 1); setenv("XDG_SESSION_TYPE", "wayland", 1);
@ -631,44 +639,7 @@ void CCompositor::prepareFallbackOutput() {
headless->createOutput(); headless->createOutput();
} }
void CCompositor::startCompositor(std::string socketName, int socketFd) { void CCompositor::startCompositor() {
if (!socketName.empty() && socketFd != -1) {
fcntl(socketFd, F_SETFD, FD_CLOEXEC);
const auto RETVAL = wl_display_add_socket_fd(m_sWLDisplay, socketFd);
if (RETVAL >= 0) {
m_szWLDisplaySocket = socketName;
Debug::log(LOG, "wl_display_add_socket_fd for {} succeeded with {}", socketName, RETVAL);
} else
Debug::log(WARN, "wl_display_add_socket_fd for {} returned {}: skipping", socketName, RETVAL);
} else {
// get socket, avoid using 0
for (int candidate = 1; candidate <= 32; candidate++) {
const auto CANDIDATESTR = ("wayland-" + std::to_string(candidate));
const auto RETVAL = wl_display_add_socket(m_sWLDisplay, CANDIDATESTR.c_str());
if (RETVAL >= 0) {
m_szWLDisplaySocket = CANDIDATESTR;
Debug::log(LOG, "wl_display_add_socket for {} succeeded with {}", CANDIDATESTR, RETVAL);
break;
} else
Debug::log(WARN, "wl_display_add_socket for {} returned {}: skipping candidate {}", CANDIDATESTR, RETVAL, candidate);
}
}
if (m_szWLDisplaySocket.empty()) {
Debug::log(WARN, "All candidates failed, trying wl_display_add_socket_auto");
const auto SOCKETSTR = wl_display_add_socket_auto(m_sWLDisplay);
if (SOCKETSTR)
m_szWLDisplaySocket = SOCKETSTR;
}
if (m_szWLDisplaySocket.empty()) {
Debug::log(CRIT, "m_szWLDisplaySocket NULL!");
throwError("m_szWLDisplaySocket was null! (wl_display_add_socket and wl_display_add_socket_auto failed)");
}
setenv("WAYLAND_DISPLAY", m_szWLDisplaySocket.c_str(), 1);
setenv("XDG_SESSION_TYPE", "wayland", 1);
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
if (m_pAqBackend->hasSession() /* Session-less Hyprland usually means a nest, don't update the env in that case */) { if (m_pAqBackend->hasSession() /* Session-less Hyprland usually means a nest, don't update the env in that case */) {

View file

@ -69,8 +69,8 @@ class CCompositor {
std::unordered_map<std::string, uint64_t> m_mMonitorIDMap; std::unordered_map<std::string, uint64_t> m_mMonitorIDMap;
void initServer(); void initServer(std::string socketName, int socketFd);
void startCompositor(std::string socketName, int socketFd); void startCompositor();
void cleanup(); void cleanup();
void createLockFile(); void createLockFile();
void removeLockFile(); void removeLockFile();

View file

@ -147,7 +147,7 @@ int main(int argc, char** argv) {
return 1; return 1;
} }
g_pCompositor->initServer(); g_pCompositor->initServer(socketName, socketFd);
if (!envEnabled("HYPRLAND_NO_RT")) if (!envEnabled("HYPRLAND_NO_RT"))
Init::gainRealTime(); Init::gainRealTime();
@ -155,7 +155,7 @@ int main(int argc, char** argv) {
Debug::log(LOG, "Hyprland init finished."); Debug::log(LOG, "Hyprland init finished.");
// If all's good to go, start. // If all's good to go, start.
g_pCompositor->startCompositor(socketName, socketFd); g_pCompositor->startCompositor();
g_pCompositor->m_bIsShuttingDown = true; g_pCompositor->m_bIsShuttingDown = true;