From 012c8afd2383241f0102111f97302857adfcb599 Mon Sep 17 00:00:00 2001 From: NPScript Date: Fri, 3 Jun 2022 22:57:05 +0200 Subject: initial sketch --- sb.cpp | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 sb.cpp (limited to 'sb.cpp') diff --git a/sb.cpp b/sb.cpp new file mode 100644 index 0000000..6642e52 --- /dev/null +++ b/sb.cpp @@ -0,0 +1,130 @@ +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +bool is_valid_url(const std::string & url); +void open_url(const std::string & url); +void search(); +void page_scroll(int scroll); +class Main; +void get_window_id(); +uint32_t id; + +bool is_valid_url(const std::string & url) { + std::regex r(".*://.*\\..*"); + std::smatch m; + + return std::regex_match(url, m, r); +} + +std::string style = "QWidget { background: #181818; }" + "QLineEdit { color: #547f62; border: none; } " + "QLabel { color: #547f62; } "; + +QWebEngineView * web; +Main * view; + +class Main : public QMainWindow { + protected: + void keyPressEvent(QKeyEvent * event) { + if (event->text() == "o") { + + search(); + } else if (event->text() == "j") { + page_scroll(20); + } else if (event->text() == "k") + page_scroll(-20); + } +}; + +std::string read_from_dmenu() { + std::string output; + + std::string cmd = "printf '' | dmenu -p 'open: ' -w "; + cmd += std::to_string(view->winId()); + + FILE * f = popen(cmd.c_str(), "r"); + + char string[8092] = {0}; + fgets(string, 8092, f); + fclose(f); + + if (string[0] != 0) { + output = string; + output.pop_back(); + } + + return output; +} + +void open_url(const std::string & url) { + web->load(QString::fromStdString(url)); +} + +void search() { + auto url = read_from_dmenu(); + + if (url.empty()) + return; + + if (is_valid_url(url)) { + open_url(url); + return; + } + + if (is_valid_url("https://" + url)) { + open_url("https://" + url); + return; + } + + open_url("https://duckduckgo.com/?q=" + url); +} + +void page_scroll(int scroll) { + std::string js = "window.scrollBy(0, " + std::to_string(scroll) + ");"; + web->page()->runJavaScript(QString::fromStdString(js)); +} + +void help(char *arg0) { + std::cout << arg0 << '\n'; + std::cout << "-e [xid] - embed in other X window\n"; +} + +int +main(int argc, char **argv) { + QApplication app(argc, argv); + app.setStyleSheet(QString::fromStdString(style)); + view = new Main(); + web = new QWebEngineView(); + web->load(QUrl("")); + + if (argc == 3) { + if (argv[1] == std::string("-e")) { + Display * dpy = XOpenDisplay(NULL); + int eid = std::stoi(argv[2]); + XReparentWindow(dpy, view->winId(), eid, 0, 0); + XFlush(dpy); + } else { + help(argv[0]); + } + } else if (argc != 1) { + help(argv[0]); + } + + view->show(); + view->setCentralWidget(web); + + return app.exec(); +} -- cgit v1.2.3-70-g09d2