Skip to content

Commit

Permalink
TerminalShell (Linux): improve terminal detection by env
Browse files Browse the repository at this point in the history
  • Loading branch information
CarterLi committed Jan 22, 2024
1 parent 31b3fe4 commit a9a5790
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/detection/terminalfont/terminalfont_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ void ffDetectTerminalFontPlatform(const FFTerminalResult* terminal, FFTerminalFo
detectFromConfigFile("lxterminal/lxterminal.conf", "fontname =", terminalFont);
else if(ffStrbufIgnCaseEqualS(&terminal->processName, "tilix"))
detectFromGSettings("/com/gexperts/Tilix/profiles/", "com.gexperts.Tilix.ProfilesList", "com.gexperts.Tilix.Profile", "default", terminalFont);
else if(ffStrbufStartsWithIgnCaseS(&terminal->processName, "gnome-terminal-"))
else if(ffStrbufStartsWithIgnCaseS(&terminal->processName, "gnome-terminal"))
detectFromGSettings("/org/gnome/terminal/legacy/profiles:/:", "org.gnome.Terminal.ProfilesList", "org.gnome.Terminal.Legacy.Profile", "default", terminalFont);
else if(ffStrbufIgnCaseEqualS(&terminal->processName, "kgx"))
detectKgx(terminalFont);
Expand Down
2 changes: 1 addition & 1 deletion src/detection/terminalshell/terminalshell.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ bool fftsGetTerminalVersion(FFstrbuf* processName, FF_MAYBE_UNUSED FFstrbuf* exe

#if defined(__linux__) || defined(__FreeBSD__)

if(ffStrbufStartsWithIgnCaseS(processName, "gnome-terminal-"))
if(ffStrbufStartsWithIgnCaseS(processName, "gnome-terminal"))
return getTerminalVersionGnome(version);

if(ffStrbufIgnCaseEqualS(processName, "konsole"))
Expand Down
79 changes: 58 additions & 21 deletions src/detection/terminalshell/terminalshell_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,21 @@ static pid_t getTerminalInfo(FFTerminalResult* result, pid_t pid)
return ppid;
}

static bool getTerminalInfoByPidEnv(FFTerminalResult* result, const char* pidEnv)
{
pid_t pid = (pid_t) strtol(getenv(pidEnv), NULL, 10);
result->pid = (uint32_t) pid;
char name[256];
if (getProcessNameAndPpid(pid, name, (pid_t*) &result->ppid, NULL) == NULL)
{
ffStrbufSetS(&result->processName, name);
getProcessInformation(pid, &result->processName, &result->exe, &result->exeName, &result->exePath);
return true;
}

return false;
}

static void getTerminalFromEnv(FFTerminalResult* result)
{
if(
Expand All @@ -319,58 +334,80 @@ static void getTerminalFromEnv(FFTerminalResult* result)
const char* term = NULL;

//SSH
if(getenv("SSH_CONNECTION") != NULL)
if(
getenv("SSH_TTY") != NULL
)
term = getenv("SSH_TTY");
else if(
getenv("KITTY_PID") != NULL ||
getenv("KITTY_INSTALLATION_DIR") != NULL
)
{
if (getTerminalInfoByPidEnv(result, "KITTY_PID"))
return;
term = "kitty";
}

#ifdef __linux__
#ifdef __linux__ // WSL
//Windows Terminal
if(!ffStrSet(term) && (
else if(
getenv("WT_SESSION") != NULL ||
getenv("WT_PROFILE_ID") != NULL
)) term = "Windows Terminal";
) term = "Windows Terminal";

//ConEmu
if(!ffStrSet(term) && (
else if(
getenv("ConEmuPID") != NULL
)) term = "ConEmu";
) term = "ConEmu";
#endif

//Alacritty
if(!ffStrSet(term) && (
else if(
getenv("ALACRITTY_SOCKET") != NULL ||
getenv("ALACRITTY_LOG") != NULL ||
getenv("ALACRITTY_WINDOW_ID") != NULL
)) term = "Alacritty";
) term = "Alacritty";

#ifdef __ANDROID__
//Termux
if(!ffStrSet(term) && (
else if(
getenv("TERMUX_VERSION") != NULL ||
getenv("TERMUX_MAIN_PACKAGE_FORMAT") != NULL
)) term = "com.termux";
)
{
if (getTerminalInfoByPidEnv(result, "TERMUX_APP__PID"))
return;
term = "com.termux";
}
#endif

#ifdef __linux__
#if defined(__linux__) || defined(__FreeBSD__)
//Konsole
if(!ffStrSet(term) && (
else if(
getenv("KONSOLE_VERSION") != NULL
)) term = "konsole";
) term = "konsole";

else if(
getenv("GNOME_TERMINAL_SCREEN") != NULL ||
getenv("GNOME_TERMINAL_SERVICE") != NULL
) term = "gnome-terminal";
#endif

//MacOS, mintty
if(!ffStrSet(term))
else if(getenv("TERM_PROGRAM") != NULL)
term = getenv("TERM_PROGRAM");

if(!ffStrSet(term))
else if(getenv("LC_TERMINAL") != NULL)
term = getenv("LC_TERMINAL");

//Normal Terminal
if(!ffStrSet(term))
else
{
term = getenv("TERM");

//TTY
if(!ffStrSet(term) || ffStrEquals(term, "linux"))
term = ttyname(STDIN_FILENO);
//TTY
if(!ffStrSet(term) || ffStrEquals(term, "linux"))
term = ttyname(STDIN_FILENO);
}

if(ffStrSet(term))
{
Expand Down Expand Up @@ -437,7 +474,7 @@ static void setTerminalInfoDetails(FFTerminalResult* result)

#elif defined(__linux__) || defined(__FreeBSD__)

else if(ffStrbufStartsWithS(&result->processName, "gnome-terminal-"))
else if(ffStrbufStartsWithS(&result->processName, "gnome-terminal"))
ffStrbufInitStatic(&result->prettyName, "GNOME Terminal");
else if(ffStrbufStartsWithS(&result->processName, "kgx"))
ffStrbufInitStatic(&result->prettyName, "GNOME Console");
Expand Down

0 comments on commit a9a5790

Please sign in to comment.