Commit 808d9ed6 authored by Robert Knight's avatar Robert Knight

Set the COLORFGBG environment variable in new sessions to indicate to terminal...

Set the COLORFGBG environment variable in new sessions to indicate to terminal programs whether the terminal has a light or dark background.  This allows programs such as Vim to automatically pick appropriate colors when a Konsole session has a light or dark background.  The value of 'COLORFGBG' is not strictly accurate, instead it approximates the current color scheme as 'black on white' or 'white on black' - but this covers the most important need'

svn path=/trunk/KDE/kdebase/apps/konsole/; revision=686658
parent 1c370c2d
......@@ -84,7 +84,7 @@ public:
quint16 charSequence;
};
/** A combination of *_RENDITION flags which specify options for drawing the character. */
/** A combination of RENDITION flags which specify options for drawing the character. */
quint8 rendition;
/** The foreground color used to draw this character. */
......
......@@ -233,6 +233,12 @@ QColor ColorScheme::backgroundColor() const
{
return colorTable()[1].color;
}
bool ColorScheme::hasDarkBackground() const
{
// value can range from 0 - 255, with larger values indicating higher brightness.
// so 127 is in the middle, anything less is deemed 'dark'
return backgroundColor().value() < 127;
}
void ColorScheme::setOpacity(qreal opacity) { _opacity = opacity; }
qreal ColorScheme::opacity() const { return _opacity; }
......
......@@ -105,6 +105,13 @@ public:
*/
QColor backgroundColor() const;
/**
* Returns true if this color scheme has a dark background.
* The background color is said to be dark if it has a value of less than 127
* in the HSV color space.
*/
bool hasDarkBackground() const;
/**
* Sets the opacity level of the display background. A value of
* 0 represents a completely transparent background, up to a value of 1
......
......@@ -83,7 +83,6 @@ Screen::Screen(int l, int c)
sa_cu_re(0),
lastPos(-1)
{
lineProperties.resize(lines+1);
for (int i=0;i<lines+1;i++)
lineProperties[i]=LINE_DEFAULT;
......
......@@ -79,6 +79,7 @@ Session::Session() :
, _zmodemBusy(false)
, _zmodemProc(0)
, _zmodemProgress(0)
, _hasDarkBackground(false)
{
//prepare DBus communication
//TODO - Re-add Session adaptor
......@@ -126,6 +127,14 @@ Session::Session() :
connect(_monitorTimer, SIGNAL(timeout()), this, SLOT(monitorTimerDone()));
}
void Session::setDarkBackground(bool darkBackground)
{
_hasDarkBackground = darkBackground;
}
bool Session::hasDarkBackground() const
{
return _hasDarkBackground;
}
bool Session::running() const
{
return _shellProcess->isRunning();
......@@ -273,9 +282,15 @@ void Session::run()
_shellProcess->setXonXoff(_flowControl);
_shellProcess->setErase(_emulation->getErase());
// this is not strictly accurate use of the COLORFGBG variable. This does not
// tell the terminal exactly which colors are being used, but instead approximates
// the color scheme as "black on white" or "white on black" depending on whether
// the background color is deemed dark or not
QString backgroundColorHint = _hasDarkBackground ? "COLORFGBG=15;0" : "COLORFGBG=0;15";
int result = _shellProcess->start(QFile::encodeName(_program),
arguments,
_environment,
_environment << backgroundColorHint,
_winId,
_addToUtmp,
dbusService,
......
......@@ -334,6 +334,17 @@ public:
/** Sets the text codec used by this session's terminal emulation. */
void setCodec(QTextCodec* codec);
/**
* Sets whether the session has a dark background or not. The session
* uses this information to set the COLORFGBG variable in the process's
* environment, which allows the programs running in the terminal to determine
* whether the background is light or dark and use appropriate colors by default.
*
* This has no effect once the session is running.
*/
void setDarkBackground(bool darkBackground);
bool hasDarkBackground() const;
public slots:
/**
......@@ -494,6 +505,8 @@ private:
QString _profileKey;
bool _hasDarkBackground;
static int lastSessionId;
};
......
......@@ -477,6 +477,9 @@ void ViewManager::createView(Session* session)
container->addView(display,properties);
session->addView(display);
// tell the session whether it has a light or dark background
session->setDarkBackground( colorSchemeForProfile(session->profileKey())->hasDarkBackground() );
if ( container == activeContainer )
{
container->setActiveView(display);
......@@ -560,18 +563,27 @@ TerminalDisplay* ViewManager::createTerminalDisplay(Session* session)
return display;
}
void ViewManager::applyProfile(TerminalDisplay* view , const QString& profileKey)
const ColorScheme* ViewManager::colorSchemeForProfile(const QString& profileKey) const
{
Profile* info = SessionManager::instance()->profile(profileKey);
Q_ASSERT( info );
const ColorScheme* colorScheme = ColorSchemeManager::instance()->
findColorScheme(info->colorScheme());
if ( !colorScheme )
colorScheme = ColorSchemeManager::instance()->defaultColorScheme();
Q_ASSERT( colorScheme );
return colorScheme;
}
void ViewManager::applyProfile(TerminalDisplay* view , const QString& profileKey)
{
Profile* info = SessionManager::instance()->profile(profileKey);
Q_ASSERT( info );
const ColorScheme* colorScheme = colorSchemeForProfile(profileKey);
// menu bar visibility
emit setMenuBarVisible( info->property(Profile::ShowMenuBar).value<bool>() );
......
......@@ -32,6 +32,7 @@ class KActionCollection;
namespace Konsole
{
class ColorScheme;
class Session;
class TerminalDisplay;
class Profile;
......@@ -184,6 +185,8 @@ private slots:
// moves active view to the right
void moveActiveViewRight();
private:
const ColorScheme* colorSchemeForProfile(const QString& key) const;
void setupActions();
void focusActiveView();
void registerView(TerminalDisplay* view);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment