Skip to content

Commit

Permalink
refactor recent changes
Browse files Browse the repository at this point in the history
  • Loading branch information
elfmz committed Dec 1, 2024
1 parent 7ec756b commit 1199c48
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 78 deletions.
1 change: 1 addition & 0 deletions WinPort/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ src/Backend/WinPortRGB.cpp
src/Backend/SudoAskpassImpl.cpp
src/Backend/FSClipboardBackend.cpp
src/Backend/ExtClipboardBackend.cpp
src/Backend/NotifySh.cpp
src/Backend/TTY/TTYBackend.cpp
src/Backend/TTY/TTYRevive.cpp
src/Backend/TTY/TTYInput.cpp
Expand Down
4 changes: 4 additions & 0 deletions WinPort/WinPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@

#define CONSOLE_TTY_PALETTE_OVERRIDE 0x00040000

// using this value causes SetConsoleTweaks not to change any existing tweak(s) but only return support status
#define TWEAKS_ONLY_QUERY_SUPPORTED 0xffffffffffffffff


#define TWEAK_STATUS_SUPPORT_EXCLUSIVE_KEYS 0x01
#define TWEAK_STATUS_SUPPORT_PAINT_SHARP 0x02
#define TWEAK_STATUS_SUPPORT_OSC52CLIP_SET 0x04
Expand Down
57 changes: 57 additions & 0 deletions WinPort/src/Backend/NotifySh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdexcept>
#include <string>
#include <utils.h>
#include "NotifySh.h"

static std::string GetNotifySH(const char *far2l_path)
{
std::string out(far2l_path);

if (TranslateInstallPath_Bin2Share(out)) {
out+= APP_BASENAME "/";
}

out+= "notify.sh";

struct stat s;
if (stat(out.c_str(), &s) == 0) {
return out;
}

if (TranslateInstallPath_Share2Lib(out) && stat(out.c_str(), &s) == 0) {
return out;
}

return std::string();
}

SHAREDSYMBOL void Far2l_NotifySh(const char *far2l_path, const char *title, const char *text)
{
try {
static std::string s_notify_sh = GetNotifySH(far2l_path);
if (s_notify_sh.empty()) {
throw std::runtime_error("notify.sh not found");
}

// double fork to avoid waiting for notify process that can be long
pid_t pid = fork();
if (pid == 0) {
if (fork() == 0) {
execl(s_notify_sh.c_str(), s_notify_sh.c_str(), title, text, NULL);
perror("NotifySh - execl");
}
_exit(0);
exit(0);
} else if (pid != -1) {
waitpid(pid, 0, 0);
} else {
throw std::runtime_error("fork failed");
}
} catch (std::exception &e) {
fprintf(stderr, "NotifySh('%s', '%s', '%s') - %s\n", e.what(), far2l_path, title, text);
}
}
4 changes: 4 additions & 0 deletions WinPort/src/Backend/NotifySh.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once
#include "WinCompat.h"

SHAREDSYMBOL void Far2l_NotifySh(const char *far2l_path, const char *title, const char *text);
38 changes: 10 additions & 28 deletions WinPort/src/Backend/TTY/TTYBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "TTYNegotiateFar2l.h"
#include "FarTTY.h"
#include "../FSClipboardBackend.h"
#include "../NotifySh.h"


static uint16_t g_far2l_term_width = 80, g_far2l_term_height = 25;
static volatile long s_terminal_size_change_id = 0;
Expand Down Expand Up @@ -758,8 +760,7 @@ void TTYBackend::OnConsoleAdhocQuickEdit()

DWORD64 TTYBackend::OnConsoleSetTweaks(DWORD64 tweaks)
{
if (tweaks) {

if (tweaks != TWEAKS_ONLY_QUERY_SUPPORTED) {
const auto prev_osc52clip_set = _osc52clip_set;
_osc52clip_set = (tweaks & CONSOLE_OSC52CLIP_SET) != 0;

Expand Down Expand Up @@ -1213,33 +1214,14 @@ void TTYBackend::OnConsoleDisplayNotification(const wchar_t *title, const wchar_
stk_ser.PushNum(FARTTY_INTERACT_DESKTOP_NOTIFICATION);
Far2lInteract(stk_ser, false);
} catch (std::exception &) {}
} else {

char command[1024];
char title_utf8[256];
char text_utf8[256];

// Преобразуем wchar_t в utf-8 для дальнейшего использования
wcstombs(title_utf8, title, sizeof(title_utf8));
wcstombs(text_utf8, text, sizeof(text_utf8));

// Проверяем, используется ли X11 или Wayland
const char *display = getenv("DISPLAY");
const char *wayland_display = getenv("WAYLAND_DISPLAY");
bool nsa = false;

if (display || wayland_display) {
// Пытаемся использовать notify-send
snprintf(command, sizeof(command), "notify-send \"%s\" \"%s\"", title_utf8, text_utf8);
if (system(command) == 0) {
nsa = true;
//fprintf(stderr, "Ушло через notify-send\n");
} else {
//fprintf(stderr, "notify-send недоступен.\n");
}
} else {
//fprintf(stderr, "Графическая среда не обнаружена (ни X11, ни Wayland).\n");
}
} else if (getenv("DISPLAY") != NULL || UnderWayland()) {
const std::string &str_title = Wide2MB(title);
const std::string &str_text = Wide2MB(text);
Far2l_NotifySh(_full_exe_path, str_title.c_str(), str_text.c_str());

} else {
fprintf(stderr, "OnConsoleDisplayNotification('%ls', '%ls') - unsupported\n", title, text);
}
}

Expand Down
57 changes: 9 additions & 48 deletions WinPort/src/Backend/WX/wxMain.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "wxMain.h"
#include <dlfcn.h>
#include "../NotifySh.h"

#define AREAS_REDUCTION

Expand Down Expand Up @@ -1922,9 +1923,11 @@ DWORD64 WinPortPanel::OnConsoleSetTweaks(DWORD64 tweaks)
if (_exclusive_hotkeys.Available())
out|= TWEAK_STATUS_SUPPORT_EXCLUSIVE_KEYS;

EventWithDWORD64 *event = new(std::nothrow) EventWithDWORD64(tweaks, WX_CONSOLE_SET_TWEAKS);
if (event)
wxQueueEvent(this, event);
if (tweaks != TWEAKS_ONLY_QUERY_SUPPORTED) {
EventWithDWORD64 *event = new(std::nothrow) EventWithDWORD64(tweaks, WX_CONSOLE_SET_TWEAKS);
if (event)
wxQueueEvent(this, event);
}

return out;
}
Expand All @@ -1935,59 +1938,17 @@ bool WinPortPanel::OnConsoleIsActive()
return _focused_ts != 0;
}

static std::string GetNotifySH()
{
wxFileName fn(wxStandardPaths::Get().GetExecutablePath());
wxString fn_str = fn.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR);
std::string out(fn_str.mb_str());

if (TranslateInstallPath_Bin2Share(out)) {
out+= APP_BASENAME "/";
}

out+= "notify.sh";

struct stat s;
if (stat(out.c_str(), &s) == 0) {
return out;
}

if (TranslateInstallPath_Share2Lib(out) && stat(out.c_str(), &s) == 0) {
return out;
}

return std::string();
}

void WinPortPanel::OnConsoleDisplayNotification(const wchar_t *title, const wchar_t *text)
{
const std::string &str_title = Wide2MB(title);
const std::string &str_text = Wide2MB(text);

#ifdef __APPLE__
auto fn = std::bind(MacDisplayNotify, str_title.c_str(), str_text.c_str());
CallInMain<bool>(fn);

#else

static std::string s_notify_sh = GetNotifySH();
if (s_notify_sh.empty()) {
fprintf(stderr, "OnConsoleDisplayNotification: notify.sh not found\n");
return;
}

pid_t pid = fork();
if (pid == 0) {
if (fork() == 0) {
execl(s_notify_sh.c_str(), s_notify_sh.c_str(), str_title.c_str(), str_text.c_str(), NULL);
perror("DisplayNotification - execl");
}
_exit(0);
exit(0);

} else if (pid != -1) {
waitpid(pid, 0, 0);
}
wxFileName fn(wxStandardPaths::Get().GetExecutablePath());
wxString fn_str = fn.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR);
Far2l_NotifySh(fn_str.mb_str(), str_title.c_str(), str_text.c_str());
#endif
}

Expand Down
3 changes: 1 addition & 2 deletions far2l/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,8 @@ static int MainProcess(FARString strEditViewArg, FARString strDestName1, FARStri
if( Opt.IsFirstStart ) {
Help::Present(L"Far2lGettingStarted",L"",FHELP_NOSHOWERROR);

DWORD tweaks = WINPORT(SetConsoleTweaks)(0);
DWORD tweaks = WINPORT(SetConsoleTweaks)(TWEAKS_ONLY_QUERY_SUPPORTED);
if (tweaks & TWEAK_STATUS_SUPPORT_OSC52CLIP_SET) {

if (Message(0, 2,
Msg::ConfigOSC52ClipSet,
Msg::Yes,
Expand Down

0 comments on commit 1199c48

Please sign in to comment.