Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Window dragging #74

Merged
merged 13 commits into from
Oct 13, 2024
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ add_executable(bolt
${WINDOW_LAUNCHER_OS_SPECIFIC} src/mime.cxx src/file_manager/directory.cxx client_cmake_gen.cxx
"${LIBRARY_IPC_OS_SPECIFIC}" ${BOLT_FILE_MANAGER_LAUNCHER_GEN} ${BOLT_STUB_INJECT_CXX}
src/browser/window_osr.cxx src/browser/window_plugin.cxx src/browser/window_plugin_requests.cxx
src/browser/request.cxx
)
if(BOLT_STUB_INJECT_CXX)
add_dependencies(bolt BOLT_STUB_INJECT_DEPENDENCY)
Expand Down
2 changes: 1 addition & 1 deletion src/browser/client.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ bool Browser::Client::IPCHandleMessage(int fd) {
if (window) window->HANDLER(&event); \
break; \
}
DEF_OSR_EVENT(RESIZE, HandleResize, ResizeEvent)
DEF_OSR_EVENT(REPOSITION, HandleReposition, RepositionEvent)
DEF_OSR_EVENT(MOUSEMOTION, HandleMouseMotion, MouseMotionEvent)
DEF_OSR_EVENT(MOUSEBUTTON, HandleMouseButton, MouseButtonEvent)
DEF_OSR_EVENT(MOUSEBUTTONUP, HandleMouseButtonUp, MouseButtonEvent)
Expand Down
21 changes: 21 additions & 0 deletions src/browser/request.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "request.hxx"

void Browser::ParseQuery(std::string_view query, std::function<void(const std::string_view&, const std::string_view&)> callback, char delim) {
size_t pos = 0;
while (true) {
const size_t next_and = query.find(delim, pos);
const size_t next_eq = query.find('=', pos);
if (next_eq == std::string_view::npos) break;
else if (next_and != std::string_view::npos && next_eq > next_and) {
pos = next_and + 1;
continue;
}
const bool is_last = next_and == std::string_view::npos;
const auto end = is_last ? query.end() : query.begin() + next_and;
const std::string_view key(query.begin() + pos, query.begin() + next_eq);
const std::string_view val(query.begin() + next_eq + 1, end);
callback(key, val);
if (is_last) break;
pos = next_and + 1;
}
}
73 changes: 73 additions & 0 deletions src/browser/request.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#ifndef _BOLT_REQUEST_H_
#define _BOLT_REQUEST_H_
#include <string_view>
#include <functional>
#include "include/cef_parser.h" // note: unused-includes warning is incorrect, this is used by the PQ macros

namespace Browser {
/// Goes through all the key-value pairs in the given query string and calls the callback for each one.
void ParseQuery(std::string_view query, std::function<void(const std::string_view& key, const std::string_view& val)> callback, char delim = '&');
}

/* the following macros are intended for use in and around ParseQuery callbacks */

#if defined(_WIN32)
typedef std::wstring QSTRING;
#define PQTOSTRING ToWString
#else
typedef std::string QSTRING;
#define PQTOSTRING ToString
#endif

#define PQCEFSTRING(KEY) \
if (key == #KEY) { \
has_##KEY = true; \
KEY = CefURIDecode(std::string(val), true, (cef_uri_unescape_rule_t)(UU_SPACES | UU_PATH_SEPARATORS | UU_URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS | UU_REPLACE_PLUS_WITH_SPACE)); \
return; \
}

#define PQSTRING(KEY) \
if (key == #KEY) { \
has_##KEY = true; \
KEY = CefURIDecode(std::string(val), true, (cef_uri_unescape_rule_t)(UU_SPACES | UU_PATH_SEPARATORS | UU_URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS | UU_REPLACE_PLUS_WITH_SPACE)).PQTOSTRING(); \
return; \
}

#define PQINT(KEY) \
if (key == #KEY) { \
bool _pq_negative = false; \
has_##KEY = true; \
KEY##_valid = true; \
KEY = 0; \
for (auto it = val.begin(); it != val.end(); it += 1) { \
if (*it == '-' && it == val.begin()) { \
_pq_negative = true; \
continue; \
} \
if (*it < '0' || *it > '9') { \
KEY##_valid = false; \
return; \
} \
KEY = (KEY * 10) + (*it - '0'); \
} \
if (_pq_negative) KEY = -KEY; \
return; \
}

#define PQBOOL(KEY) \
if (key == #KEY) { \
KEY = (val.size() > 0 && val != "0"); \
return; \
}

#define QSENDSTR(STR, CODE) return new Browser::ResourceHandler(reinterpret_cast<const unsigned char*>(STR "\n"), sizeof(STR "\n") - sizeof(*STR), CODE, "text/plain")
#define QSENDMOVED(LOC) return new Browser::ResourceHandler(nullptr, 0, 302, "text/plain", LOC)
#define QSENDOK() QSENDSTR("OK", 200)
#define QSENDNOTFOUND() QSENDSTR("Not found", 404)
#define QSENDBADREQUESTIF(COND) if (COND) QSENDSTR("Bad response", 400)
#define QSENDSYSTEMERRORIF(COND) if (COND) QSENDSTR("System error", 500)
#define QSENDNOTSUPPORTED() QSENDSTR("Not supported", 400)
#define QREQPARAM(NAME) if (!has_##NAME) QSENDSTR("Missing required param " #NAME, 400)
#define QREQPARAMINT(NAME) QREQPARAM(NAME); if (!NAME##_valid) QSENDSTR("Invalid value for required param " #NAME, 400)

#endif
31 changes: 5 additions & 26 deletions src/browser/window_launcher.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#include "window_launcher.hxx"
#include "include/internal/cef_types.h"
#include "resource_handler.hxx"

#include "include/cef_parser.h"
#include "request.hxx"

#include <fcntl.h>
#include <fmt/core.h>
Expand Down Expand Up @@ -177,7 +176,7 @@ CefRefPtr<CefResourceRequestHandler> Browser::Launcher::GetResourceRequestHandle
CefString code;
bool has_state = false;
CefString state;
this->ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
PQCEFSTRING(code)
PQCEFSTRING(state)
}, ',');
Expand Down Expand Up @@ -302,7 +301,7 @@ CefRefPtr<CefResourceRequestHandler> Browser::Launcher::GetResourceRequestHandle
#if defined(BOLT_PLUGINS)
QSTRING path;
bool has_path = false;
this->ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
PQSTRING(path)
});
QREQPARAM(path);
Expand Down Expand Up @@ -333,7 +332,7 @@ CefRefPtr<CefResourceRequestHandler> Browser::Launcher::GetResourceRequestHandle
uint64_t client;
bool has_client = false;
bool client_valid = false;
this->ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
PQCEFSTRING(id)
PQCEFSTRING(path)
PQCEFSTRING(main)
Expand All @@ -359,7 +358,7 @@ CefRefPtr<CefResourceRequestHandler> Browser::Launcher::GetResourceRequestHandle
uint64_t uid;
bool has_uid = false;
bool uid_valid = false;
this->ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
PQINT(client)
PQINT(uid)
});
Expand Down Expand Up @@ -532,26 +531,6 @@ void Browser::Launcher::Refresh() const {
this->browser->GetMainFrame()->LoadURL(this->BuildURL());
}

void Browser::Launcher::ParseQuery(std::string_view query, std::function<void(const std::string_view&, const std::string_view&)> callback, char delim) {
size_t pos = 0;
while (true) {
const size_t next_and = query.find(delim, pos);
const size_t next_eq = query.find('=', pos);
if (next_eq == std::string_view::npos) break;
else if (next_and != std::string_view::npos && next_eq > next_and) {
pos = next_and + 1;
continue;
}
const bool is_last = next_and == std::string_view::npos;
const auto end = is_last ? query.end() : query.begin() + next_and;
const std::string_view key(query.begin() + pos, query.begin() + next_eq);
const std::string_view val(query.begin() + next_eq + 1, end);
callback(key, val);
if (is_last) break;
pos = next_and + 1;
}
}

void Browser::Launcher::NotifyClosed() {
this->client->OnLauncherClosed();
}
Expand Down
59 changes: 0 additions & 59 deletions src/browser/window_launcher.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
#include "../browser.hxx"
#include "../file_manager.hxx"

#include "include/cef_parser.h"

#include <filesystem>
#include <functional>

namespace Browser {
struct Launcher: public Window, CefRequestHandler {
Expand Down Expand Up @@ -44,9 +41,6 @@ namespace Browser {
void UpdateClientList(bool need_lock_client_mutex) const;
#endif

/// Goes through all the key-value pairs in the given query string and calls the callback for each one.
void ParseQuery(std::string_view query, std::function<void(const std::string_view&, const std::string_view&)> callback, char delim = '&');

void NotifyClosed() override;

/*
Expand Down Expand Up @@ -87,57 +81,4 @@ namespace Browser {
};
}

#if defined(_WIN32)
typedef std::wstring QSTRING;
#define PQTOSTRING ToWString
#else
typedef std::string QSTRING;
#define PQTOSTRING ToString
#endif

#define PQCEFSTRING(KEY) \
if (key == #KEY) { \
has_##KEY = true; \
KEY = CefURIDecode(std::string(val), true, (cef_uri_unescape_rule_t)(UU_SPACES | UU_PATH_SEPARATORS | UU_URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS | UU_REPLACE_PLUS_WITH_SPACE)); \
return; \
}

#define PQSTRING(KEY) \
if (key == #KEY) { \
has_##KEY = true; \
KEY = CefURIDecode(std::string(val), true, (cef_uri_unescape_rule_t)(UU_SPACES | UU_PATH_SEPARATORS | UU_URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS | UU_REPLACE_PLUS_WITH_SPACE)).PQTOSTRING(); \
return; \
}

#define PQINT(KEY) \
if (key == #KEY) { \
has_##KEY = true; \
KEY##_valid = true; \
KEY = 0; \
for (auto it = val.begin(); it != val.end(); it += 1) { \
if (*it < '0' || *it > '9') { \
KEY##_valid = false; \
return; \
} \
KEY = (KEY * 10) + (*it - '0'); \
} \
return; \
}

#define PQBOOL(KEY) \
if (key == #KEY) { \
KEY = (val.size() > 0 && val != "0"); \
return; \
}

#define QSENDSTR(STR, CODE) return new Browser::ResourceHandler(reinterpret_cast<const unsigned char*>(STR "\n"), sizeof(STR "\n") - sizeof(*STR), CODE, "text/plain")
#define QSENDMOVED(LOC) return new Browser::ResourceHandler(nullptr, 0, 302, "text/plain", LOC)
#define QSENDOK() QSENDSTR("OK", 200)
#define QSENDNOTFOUND() QSENDSTR("Not found", 404)
#define QSENDBADREQUESTIF(COND) if (COND) QSENDSTR("Bad response", 400)
#define QSENDSYSTEMERRORIF(COND) if (COND) QSENDSTR("System error", 500)
#define QSENDNOTSUPPORTED() QSENDSTR("Not supported", 400)
#define QREQPARAM(NAME) if (!has_##NAME) QSENDSTR("Missing required param " #NAME, 400)
#define QREQPARAMINT(NAME) QREQPARAM(NAME); if (!NAME##_valid) QSENDSTR("Invalid value for required param " #NAME, 400)

#endif
7 changes: 4 additions & 3 deletions src/browser/window_launcher_posix.cxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "window_launcher.hxx"
#include "resource_handler.hxx"
#include "request.hxx"

#include <archive.h>
#include <archive_entry.h>
Expand Down Expand Up @@ -77,7 +78,7 @@ CefRefPtr<CefResourceRequestHandler> Browser::Launcher::LaunchRs3Deb(CefRefPtr<C
#if defined(BOLT_PLUGINS)
bool plugin_loader = false;
#endif
this->ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
PQSTRING(hash)
PQSTRING(config_uri)
PQSTRING(jx_session_id)
Expand Down Expand Up @@ -299,7 +300,7 @@ CefRefPtr<CefResourceRequestHandler> Browser::Launcher::LaunchRuneliteJar(CefRef
bool has_jx_character_id = false;
std::string jx_display_name;
bool has_jx_display_name = false;
this->ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
PQSTRING(jar_path)
PQSTRING(id)
PQSTRING(jx_session_id)
Expand Down Expand Up @@ -411,7 +412,7 @@ CefRefPtr<CefResourceRequestHandler> Browser::Launcher::LaunchHdosJar(CefRefPtr<
bool has_jx_character_id = false;
std::string jx_display_name;
bool has_jx_display_name = false;
this->ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
PQSTRING(version)
PQSTRING(jx_session_id)
PQSTRING(jx_character_id)
Expand Down
9 changes: 5 additions & 4 deletions src/browser/window_launcher_win.cxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "window_launcher.hxx"
#include "resource_handler.hxx"
#include "request.hxx"

#include <iostream>
#include <shellapi.h>
Expand Down Expand Up @@ -223,7 +224,7 @@ CefRefPtr<CefResourceRequestHandler> Browser::Launcher::LaunchRs3Exe(CefRefPtr<C
#if defined(BOLT_PLUGINS)
bool plugin_loader = false;
#endif
this->ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
PQSTRING(hash)
PQSTRING(config_uri)
PQSTRING(jx_session_id)
Expand Down Expand Up @@ -322,7 +323,7 @@ CefRefPtr<CefResourceRequestHandler> Browser::Launcher::LaunchOsrsExe(CefRefPtr<
bool has_jx_character_id = false;
std::wstring jx_display_name;
bool has_jx_display_name = false;
this->ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
PQSTRING(hash)
PQSTRING(jx_session_id)
PQSTRING(jx_character_id)
Expand Down Expand Up @@ -409,7 +410,7 @@ CefRefPtr<CefResourceRequestHandler> Browser::Launcher::LaunchRuneliteJar(CefRef
has_jx_character_id = false,
has_jx_display_name = false;

this->ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
PQSTRING(jar_path)
PQSTRING(id)
PQSTRING(jx_session_id)
Expand Down Expand Up @@ -486,7 +487,7 @@ CefRefPtr<CefResourceRequestHandler> Browser::Launcher::LaunchHdosJar(CefRefPtr<
has_jx_character_id = false,
has_jx_display_name = false;

this->ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
ParseQuery(query, [&](const std::string_view& key, const std::string_view& val) {
PQSTRING(version)
PQSTRING(jx_session_id)
PQSTRING(jx_character_id)
Expand Down
4 changes: 2 additions & 2 deletions src/browser/window_osr.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ void Browser::WindowOSR::HandleAck() {
this->stored_lock.unlock();
}

void Browser::WindowOSR::HandleResize(const ResizeEvent* event) {
void Browser::WindowOSR::HandleReposition(const RepositionEvent* event) {
this->size_lock.lock();
this->width = event->width;
this->height = event->height;
this->size_lock.unlock();
if (this->browser) this->browser->GetHost()->WasResized();
if (this->browser && event->did_resize) this->browser->GetHost()->WasResized();
}

static void MouseEventToCef(const MouseEvent* in, CefMouseEvent* out) {
Expand Down
4 changes: 2 additions & 2 deletions src/browser/window_osr.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include <mutex>

struct ResizeEvent;
struct RepositionEvent;
struct MouseMotionEvent;
struct MouseButtonEvent;
struct MouseScrollEvent;
Expand All @@ -30,7 +30,7 @@ namespace Browser {
void Close();

void HandleAck();
void HandleResize(const ResizeEvent*);
void HandleReposition(const RepositionEvent*);
void HandleMouseMotion(const MouseMotionEvent*);
void HandleMouseButton(const MouseButtonEvent*);
void HandleMouseButtonUp(const MouseButtonEvent*);
Expand Down
Loading