From f78b75b15dba41bdcad1e2020d900f10263ef9f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Duval?= Date: Fri, 5 Nov 2021 20:34:56 +0100 Subject: [PATCH] Terminal: handle color queries this allows apps like vim to select a color scheme based on a dark or light background. Change-Id: Ia9f98d2373523a8b5fa379225a1c906ae075edf7 Reviewed-on: https://review.haiku-os.org/c/haiku/+/4693 Tested-by: Commit checker robot Reviewed-by: Adrien Destugues --- src/apps/terminal/TermConst.h | 1 + src/apps/terminal/TermParse.cpp | 4 +++ src/apps/terminal/TermView.cpp | 39 ++++++++++++++++++++++++++++ src/apps/terminal/TermView.h | 1 + src/apps/terminal/TerminalBuffer.cpp | 11 ++++++++ src/apps/terminal/TerminalBuffer.h | 1 + 6 files changed, 57 insertions(+) diff --git a/src/apps/terminal/TermConst.h b/src/apps/terminal/TermConst.h index aa726cfbfa..a8c5f401cb 100644 --- a/src/apps/terminal/TermConst.h +++ b/src/apps/terminal/TermConst.h @@ -87,6 +87,7 @@ static const uint32 MSG_MOVE_TAB_LEFT = 'mvtl'; static const uint32 MSG_MOVE_TAB_RIGHT = 'mvtr'; static const uint32 MSG_ACTIVATE_TERM = 'msat'; static const uint32 MSG_SET_CURSOR_STYLE = 'mscs'; +static const uint32 MSG_GET_TERMINAL_COLOR = 'getc'; // Preference Read/Write Keys diff --git a/src/apps/terminal/TermParse.cpp b/src/apps/terminal/TermParse.cpp index be8f3c35f5..4e1a291a65 100644 --- a/src/apps/terminal/TermParse.cpp +++ b/src/apps/terminal/TermParse.cpp @@ -1571,6 +1571,10 @@ TermParse::_ProcessOperatingSystemControls(uchar* params) { int32 offset = mode - 10; int32 count = 0; + if (strcmp((char*)params, "?") == 0) { + fBuffer->GetColor(mode); + break; + } char* p = strtok((char*)params, ";"); do { if (gXColorsTable.LookUpColor(p, &colors[count]) != B_OK) { diff --git a/src/apps/terminal/TermView.cpp b/src/apps/terminal/TermView.cpp index 630fd44011..a69c2b9e63 100644 --- a/src/apps/terminal/TermView.cpp +++ b/src/apps/terminal/TermView.cpp @@ -720,6 +720,30 @@ TermView::SetTermColor(uint index, rgb_color color, bool dynamic) } +status_t +TermView::GetTermColor(uint index, rgb_color* color) +{ + if (color == NULL) + return B_BAD_VALUE; + + switch (index) { + case 10: + *color = fTextForeColor; + break; + case 11: + *color = fTextBackColor; + break; + case 12: + *color = fCursorBackColor; + break; + default: + return B_BAD_VALUE; + break; + } + return B_OK; +} + + int TermView::Encoding() const { @@ -1833,6 +1857,21 @@ TermView::MessageReceived(BMessage *message) } break; } + case MSG_GET_TERMINAL_COLOR: + { + uint8 index = 0; + if (message->FindUInt8("index", &index) != B_OK) + break; + rgb_color color; + status_t status = GetTermColor(index, &color); + if (status == B_OK) { + BString reply; + reply.SetToFormat("\033]%u;rgb:%02x/%02x/%02x\033\\", + index, color.red, color.green, color.blue); + fShell->Write(reply.String(), reply.Length()); + } + break; + } case MSG_SET_CURSOR_STYLE: { int32 style = BLOCK_CURSOR; diff --git a/src/apps/terminal/TermView.h b/src/apps/terminal/TermView.h index cb738f077f..a1b14a8283 100644 --- a/src/apps/terminal/TermView.h +++ b/src/apps/terminal/TermView.h @@ -94,6 +94,7 @@ public: void SetSelectColor(rgb_color fore, rgb_color back); void SetTermColor(uint index, rgb_color color, bool dynamic = false); + status_t GetTermColor(uint index, rgb_color* color); int Encoding() const; void SetEncoding(int encoding); diff --git a/src/apps/terminal/TerminalBuffer.cpp b/src/apps/terminal/TerminalBuffer.cpp index 83e70ecbcc..ccec6fed30 100644 --- a/src/apps/terminal/TerminalBuffer.cpp +++ b/src/apps/terminal/TerminalBuffer.cpp @@ -228,6 +228,17 @@ TerminalBuffer::ResetColors(uint8* indexes, int32 count, bool dynamic) } +void +TerminalBuffer::GetColor(uint8 index) +{ + if (fListenerValid) { + BMessage message(MSG_GET_TERMINAL_COLOR); + message.AddUInt8("index", index); + fListener.SendMessage(&message); + } +} + + void TerminalBuffer::SetCursorStyle(int32 style, bool blinking) { diff --git a/src/apps/terminal/TerminalBuffer.h b/src/apps/terminal/TerminalBuffer.h index 6d5c4ae1d0..2004ab02a9 100644 --- a/src/apps/terminal/TerminalBuffer.h +++ b/src/apps/terminal/TerminalBuffer.h @@ -37,6 +37,7 @@ public: int32 count = 1, bool dynamic = false); void ResetColors(uint8* indexes, int32 count = 1, bool dynamic = false); + void GetColor(uint8 index); void SetCursorStyle(int32 style, bool blinking); void SetCursorBlinking(bool blinking); void SetCursorHidden(bool hidden);