mirror of
https://github.com/hyprwm/Hyprland.git
synced 2024-10-30 14:01:40 +00:00
Major IPC Rework + Added Socket2
IPC is now done with UNIX sockets instead of TCP ones (security!), and added Socket2, see Wiki.
This commit is contained in:
parent
d8d8a29fc2
commit
ba7e3cd9a1
@ -7,6 +7,7 @@
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <iostream>
|
||||
@ -29,7 +30,7 @@ usage: hyprctl [command] [(opt)args]
|
||||
)#";
|
||||
|
||||
void request(std::string arg) {
|
||||
const auto SERVERSOCKET = socket(AF_INET, SOCK_STREAM, 0);
|
||||
const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
|
||||
if (SERVERSOCKET < 0) {
|
||||
std::cout << "Couldn't open a socket (1)";
|
||||
@ -43,39 +44,12 @@ void request(std::string arg) {
|
||||
return;
|
||||
}
|
||||
|
||||
sockaddr_in serverAddress = {0};
|
||||
serverAddress.sin_family = AF_INET;
|
||||
bcopy((char*)SERVER->h_addr, (char*)&serverAddress.sin_addr.s_addr, SERVER->h_length);
|
||||
sockaddr_un serverAddress = {0};
|
||||
serverAddress.sun_family = AF_UNIX;
|
||||
strcpy(serverAddress.sun_path, "/tmp/hypr/.socket.sock");
|
||||
|
||||
std::ifstream socketPortStream;
|
||||
socketPortStream.open("/tmp/hypr/.socket");
|
||||
|
||||
if (!socketPortStream.good()) {
|
||||
std::cout << "No socket port file (2a)";
|
||||
return;
|
||||
}
|
||||
|
||||
std::string port = "";
|
||||
std::getline(socketPortStream, port);
|
||||
socketPortStream.close();
|
||||
|
||||
int portInt = 0;
|
||||
try {
|
||||
portInt = std::stoi(port.c_str());
|
||||
} catch (...) {
|
||||
std::cout << "Port not an int?! (2b)";
|
||||
return;
|
||||
}
|
||||
|
||||
if (portInt == 0) {
|
||||
std::cout << "Port not an int?! (2c)";
|
||||
return;
|
||||
}
|
||||
|
||||
serverAddress.sin_port = portInt;
|
||||
|
||||
if (connect(SERVERSOCKET, (sockaddr*)&serverAddress, sizeof(serverAddress)) < 0) {
|
||||
std::cout << "Couldn't connect to port " << port << " (3) Is Hyprland running?";
|
||||
if (connect(SERVERSOCKET, (sockaddr*)&serverAddress, SUN_LEN(&serverAddress)) < 0) {
|
||||
std::cout << "Couldn't connect to /tmp/hypr/.socket.sock. (3) Is Hyprland running?";
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -188,6 +188,10 @@ void CCompositor::startCompositor() {
|
||||
|
||||
Debug::log(LOG, "Creating the LayoutManager!");
|
||||
g_pLayoutManager = std::make_unique<CLayoutManager>();
|
||||
|
||||
Debug::log(LOG, "Creating the EventManager!");
|
||||
g_pEventManager = std::make_unique<CEventManager>();
|
||||
g_pEventManager->startThread();
|
||||
//
|
||||
//
|
||||
|
||||
|
@ -14,6 +14,7 @@
|
||||
#include "managers/LayoutManager.hpp"
|
||||
#include "managers/KeybindManager.hpp"
|
||||
#include "managers/AnimationManager.hpp"
|
||||
#include "managers/EventManager.hpp"
|
||||
#include "helpers/Monitor.hpp"
|
||||
#include "helpers/Workspace.hpp"
|
||||
#include "Window.hpp"
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
@ -238,28 +239,20 @@ std::string getRequestFromThread(std::string rq) {
|
||||
|
||||
void HyprCtl::startHyprCtlSocket() {
|
||||
std::thread([&]() {
|
||||
uint16_t connectPort = 9187;
|
||||
|
||||
const auto SOCKET = socket(AF_INET, SOCK_STREAM, 0);
|
||||
const auto SOCKET = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
|
||||
if (SOCKET < 0) {
|
||||
Debug::log(ERR, "Couldn't start the Hyprland Socket. (1) IPC will not work.");
|
||||
return;
|
||||
}
|
||||
|
||||
sockaddr_in SERVERADDRESS = {.sin_family = AF_INET, .sin_port = connectPort, .sin_addr = (in_addr)INADDR_ANY};
|
||||
unlink("/tmp/hypr/.socket.sock");
|
||||
|
||||
while(connectPort < 11000) {
|
||||
if (bind(SOCKET, (sockaddr*)&SERVERADDRESS, sizeof(SERVERADDRESS)) < 0) {
|
||||
Debug::log(LOG, "IPC: Port %d failed with an error: %s", connectPort, strerror(errno));
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
sockaddr_un SERVERADDRESS = {.sun_family = AF_UNIX};
|
||||
|
||||
connectPort++;
|
||||
SERVERADDRESS.sin_port = connectPort;
|
||||
}
|
||||
|
||||
strcpy(SERVERADDRESS.sun_path, "/tmp/hypr/.socket.sock");
|
||||
|
||||
bind(SOCKET, (sockaddr*)&SERVERADDRESS, SUN_LEN(&SERVERADDRESS));
|
||||
|
||||
// 10 max queued.
|
||||
listen(SOCKET, 10);
|
||||
@ -269,12 +262,7 @@ void HyprCtl::startHyprCtlSocket() {
|
||||
|
||||
char readBuffer[1024] = {0};
|
||||
|
||||
Debug::log(LOG, "Hypr socket started on port %i", connectPort);
|
||||
|
||||
std::string cmd = "rm -f /tmp/hypr/.socket";
|
||||
system(cmd.c_str()); // forgive me for using system() but it works and it doesnt matter here that much
|
||||
cmd = "echo \"" + std::to_string(connectPort) + "\" > /tmp/hypr/.socket";
|
||||
system(cmd.c_str());
|
||||
Debug::log(LOG, "Hypr socket started.");
|
||||
|
||||
while(1) {
|
||||
const auto ACCEPTEDCONNECTION = accept(SOCKET, (sockaddr*)&clientAddress, &clientSize);
|
||||
@ -285,7 +273,7 @@ void HyprCtl::startHyprCtlSocket() {
|
||||
}
|
||||
|
||||
auto messageSize = read(ACCEPTEDCONNECTION, readBuffer, 1024);
|
||||
readBuffer[messageSize == 1024 ? 1024 : messageSize] = '\0';
|
||||
readBuffer[messageSize == 1024 ? 1023 : messageSize] = '\0';
|
||||
|
||||
std::string request(readBuffer);
|
||||
|
||||
|
129
src/managers/EventManager.cpp
Normal file
129
src/managers/EventManager.cpp
Normal file
@ -0,0 +1,129 @@
|
||||
#include "EventManager.hpp"
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
CEventManager::CEventManager() {
|
||||
}
|
||||
|
||||
void CEventManager::startThread() {
|
||||
std::thread([&]() {
|
||||
const auto SOCKET = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
|
||||
if (SOCKET < 0) {
|
||||
Debug::log(ERR, "Couldn't start the Hyprland Socket 2. (1) IPC will not work.");
|
||||
return;
|
||||
}
|
||||
|
||||
unlink("/tmp/hypr/.socket2.sock");
|
||||
|
||||
sockaddr_un SERVERADDRESS = {.sun_family = AF_UNIX};
|
||||
strcpy(SERVERADDRESS.sun_path, "/tmp/hypr/.socket2.sock");
|
||||
|
||||
bind(SOCKET, (sockaddr*)&SERVERADDRESS, SUN_LEN(&SERVERADDRESS));
|
||||
|
||||
// 10 max queued.
|
||||
listen(SOCKET, 10);
|
||||
|
||||
char readBuf[1024] = {0};
|
||||
|
||||
sockaddr_in clientAddress;
|
||||
socklen_t clientSize = sizeof(clientAddress);
|
||||
|
||||
Debug::log(LOG, "Hypr socket 2 started.");
|
||||
|
||||
// set the socket nonblock
|
||||
int flags = fcntl(SOCKET, F_GETFL, 0);
|
||||
fcntl(SOCKET, F_SETFL, flags | O_NONBLOCK);
|
||||
|
||||
while (1) {
|
||||
m_bCanWriteEventQueue = true;
|
||||
|
||||
const auto ACCEPTEDCONNECTION = accept(SOCKET, (sockaddr*)&clientAddress, &clientSize);
|
||||
|
||||
if (ACCEPTEDCONNECTION > 0) {
|
||||
// new connection!
|
||||
m_dAcceptedSocketFDs.push_back(ACCEPTEDCONNECTION);
|
||||
|
||||
int flagsNew = fcntl(ACCEPTEDCONNECTION, F_GETFL, 0);
|
||||
fcntl(ACCEPTEDCONNECTION, F_SETFL, flagsNew | O_NONBLOCK);
|
||||
|
||||
Debug::log(LOG, "Socket 2 accepted a new client at FD %d", ACCEPTEDCONNECTION);
|
||||
}
|
||||
|
||||
// pong if all FDs valid
|
||||
for (auto it = m_dAcceptedSocketFDs.begin(); it != m_dAcceptedSocketFDs.end();) {
|
||||
auto sizeRead = recv(*it, &readBuf, 1024, 0);
|
||||
|
||||
if (sizeRead != 0) {
|
||||
it++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// invalid!
|
||||
Debug::log(LOG, "Removed invalid socket (2) FD: %d", *it);
|
||||
it = m_dAcceptedSocketFDs.erase(it);
|
||||
}
|
||||
|
||||
// valid FDs, check the queue
|
||||
// don't do anything if main thread is writing to the eventqueue
|
||||
while (!m_bCanReadEventQueue) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
|
||||
// if we got here, we'll be reading the queue, let's disallow writing
|
||||
m_bCanWriteEventQueue = false;
|
||||
|
||||
if (m_dQueuedEvents.empty()){ // if queue empty, sleep and ignore
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
continue;
|
||||
}
|
||||
|
||||
// write all queued events
|
||||
for (auto& ev : m_dQueuedEvents) {
|
||||
std::string eventString = ev.event + ">>" + ev.data;
|
||||
for (auto& fd : m_dAcceptedSocketFDs) {
|
||||
write(fd, eventString.c_str(), eventString.length());
|
||||
}
|
||||
}
|
||||
|
||||
m_dQueuedEvents.clear();
|
||||
|
||||
m_bCanWriteEventQueue = true;
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
|
||||
close(SOCKET);
|
||||
}).detach();
|
||||
}
|
||||
|
||||
void CEventManager::postEvent(const SHyprIPCEvent event) {
|
||||
|
||||
m_bCanReadEventQueue = false;
|
||||
if (!m_bCanWriteEventQueue) {
|
||||
// if we can't write rn, make a thread to write whenever possible, don't block calling events.
|
||||
std::thread([&](const SHyprIPCEvent ev) {
|
||||
while(!m_bCanWriteEventQueue) {
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(200));
|
||||
}
|
||||
|
||||
m_dQueuedEvents.push_back(ev);
|
||||
m_bCanReadEventQueue = true;
|
||||
}, event).detach();
|
||||
} else {
|
||||
m_dQueuedEvents.push_back(event);
|
||||
m_bCanReadEventQueue = true;
|
||||
}
|
||||
}
|
30
src/managers/EventManager.hpp
Normal file
30
src/managers/EventManager.hpp
Normal file
@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
#include <deque>
|
||||
#include <fstream>
|
||||
|
||||
#include "../defines.hpp"
|
||||
#include "../helpers/MiscFunctions.hpp"
|
||||
|
||||
struct SHyprIPCEvent {
|
||||
std::string event;
|
||||
std::string data;
|
||||
};
|
||||
|
||||
class CEventManager {
|
||||
public:
|
||||
CEventManager();
|
||||
|
||||
void postEvent(const SHyprIPCEvent event);
|
||||
|
||||
void startThread();
|
||||
|
||||
private:
|
||||
|
||||
bool m_bCanReadEventQueue = true;
|
||||
bool m_bCanWriteEventQueue = true;
|
||||
std::deque<SHyprIPCEvent> m_dQueuedEvents;
|
||||
|
||||
std::deque<int> m_dAcceptedSocketFDs;
|
||||
};
|
||||
|
||||
inline std::unique_ptr<CEventManager> g_pEventManager;
|
@ -89,6 +89,9 @@ void CInputManager::mouseMoveUnified(uint32_t time, bool refocus) {
|
||||
// set active workspace and deactivate all other in wlr
|
||||
g_pCompositor->deactivateAllWLRWorkspaces(g_pCompositor->getWorkspaceByID(PMONITOR->activeWorkspace)->m_pWlrHandle);
|
||||
wlr_ext_workspace_handle_v1_set_active(g_pCompositor->getWorkspaceByID(PMONITOR->activeWorkspace)->m_pWlrHandle, true);
|
||||
|
||||
// event
|
||||
g_pEventManager->postEvent(SHyprIPCEvent("workspace", g_pCompositor->getWorkspaceByID(PMONITOR->activeWorkspace)->m_szName));
|
||||
}
|
||||
|
||||
Vector2D surfaceCoords;
|
||||
|
@ -183,6 +183,8 @@ void CKeybindManager::changeworkspace(std::string args) {
|
||||
if (g_pCompositor->getWorkspaceByID(workspaceToChangeTo)) {
|
||||
const auto PMONITOR = g_pCompositor->getMonitorFromID(g_pCompositor->getWorkspaceByID(workspaceToChangeTo)->m_iMonitorID);
|
||||
|
||||
const auto PWORKSPACETOCHANGETO = g_pCompositor->getWorkspaceByID(workspaceToChangeTo);
|
||||
|
||||
// if it's not visible, make it visible.
|
||||
if (!g_pCompositor->isWorkspaceVisible(workspaceToChangeTo)) {
|
||||
const auto OLDWORKSPACEID = PMONITOR->activeWorkspace;
|
||||
@ -205,7 +207,7 @@ void CKeybindManager::changeworkspace(std::string args) {
|
||||
g_pCompositor->getWorkspaceByID(OLDWORKSPACEID)->startAnim(false, ANIMTOLEFT);
|
||||
|
||||
// start anim on new workspace
|
||||
g_pCompositor->getWorkspaceByID(workspaceToChangeTo)->startAnim(true, ANIMTOLEFT);
|
||||
PWORKSPACETOCHANGETO->startAnim(true, ANIMTOLEFT);
|
||||
}
|
||||
|
||||
|
||||
@ -219,8 +221,8 @@ void CKeybindManager::changeworkspace(std::string args) {
|
||||
g_pCompositor->focusWindow(g_pCompositor->getFirstWindowOnWorkspace(workspaceToChangeTo));
|
||||
|
||||
// set active and deactivate all other in wlr
|
||||
g_pCompositor->deactivateAllWLRWorkspaces(g_pCompositor->getWorkspaceByID(workspaceToChangeTo)->m_pWlrHandle);
|
||||
wlr_ext_workspace_handle_v1_set_active(g_pCompositor->getWorkspaceByID(workspaceToChangeTo)->m_pWlrHandle, true);
|
||||
g_pCompositor->deactivateAllWLRWorkspaces(PWORKSPACETOCHANGETO->m_pWlrHandle);
|
||||
wlr_ext_workspace_handle_v1_set_active(PWORKSPACETOCHANGETO->m_pWlrHandle, true);
|
||||
|
||||
Debug::log(LOG, "Changed to workspace %i", workspaceToChangeTo);
|
||||
|
||||
@ -230,6 +232,10 @@ void CKeybindManager::changeworkspace(std::string args) {
|
||||
// mark the monitor dirty
|
||||
g_pHyprRenderer->damageMonitor(PMONITOR);
|
||||
|
||||
// Event
|
||||
if (!m_bSuppressWorkspaceChangeEvents)
|
||||
g_pEventManager->postEvent(SHyprIPCEvent("workspace", PWORKSPACETOCHANGETO->m_szName));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@ -273,6 +279,10 @@ void CKeybindManager::changeworkspace(std::string args) {
|
||||
// focus (clears the last)
|
||||
g_pInputManager->refocus();
|
||||
|
||||
// Event
|
||||
if (!m_bSuppressWorkspaceChangeEvents)
|
||||
g_pEventManager->postEvent(SHyprIPCEvent("workspace", PWORKSPACE->m_szName));
|
||||
|
||||
Debug::log(LOG, "Changed to workspace %i", workspaceToChangeTo);
|
||||
}
|
||||
|
||||
@ -376,6 +386,8 @@ void CKeybindManager::moveActiveToWorkspaceSilent(std::string args) {
|
||||
const auto POLDWORKSPACEONMON = g_pCompositor->getWorkspaceByID(OLDWORKSPACEIDONMONITOR);
|
||||
const auto POLDWORKSPACEIDRETURN = g_pCompositor->getWorkspaceByID(OLDWORKSPACEIDRETURN);
|
||||
|
||||
m_bSuppressWorkspaceChangeEvents = true;
|
||||
|
||||
moveActiveToWorkspace(args);
|
||||
|
||||
PWORKSPACE = g_pCompositor->getWorkspaceByID(workspaceToMoveTo);
|
||||
@ -392,6 +404,8 @@ void CKeybindManager::moveActiveToWorkspaceSilent(std::string args) {
|
||||
|
||||
POLDWORKSPACEONMON->m_vRenderOffset.setValueAndWarp(Vector2D(0, 0));
|
||||
POLDWORKSPACEONMON->m_fAlpha.setValueAndWarp(255.f);
|
||||
|
||||
m_bSuppressWorkspaceChangeEvents = false;
|
||||
}
|
||||
|
||||
void CKeybindManager::moveFocusTo(std::string args) {
|
||||
|
@ -30,6 +30,8 @@ private:
|
||||
|
||||
bool handleInternalKeybinds(xkb_keysym_t);
|
||||
|
||||
inline static bool m_bSuppressWorkspaceChangeEvents = false;
|
||||
|
||||
// -------------- Dispatchers -------------- //
|
||||
static void killActive(std::string);
|
||||
static void spawn(std::string);
|
||||
|
Loading…
Reference in New Issue
Block a user