Made all CodeConv methods static, since there was no point for them to

be nonstatic. Moved TermParse under Shell, Removed some parameters 
passing around from TermWindow/TermView/TermParse. Now TermParse threads are 
started when the shell is attached to a TermView. Might still be 
improved.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@21680 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini
2007-07-21 20:50:06 +00:00
parent 0ed5650b01
commit ac3a8f5446
10 changed files with 148 additions and 114 deletions
+5 -10
View File
@@ -33,17 +33,8 @@ UTF8
extern char gUTF8WidthTable[]; // defined in UTF8WidthTbl.c
CodeConv::CodeConv()
{
}
CodeConv::~CodeConv()
{
}
//! get font width in coding.
/* static */
int32
CodeConv::UTF8GetFontWidth(const char *string)
{
@@ -61,6 +52,7 @@ CodeConv::UTF8GetFontWidth(const char *string)
//! Convert internal coding from src coding.
/* static */
int32
CodeConv::ConvertFromInternal(const char *src, int32 srclen, char *dst, int coding)
{
@@ -92,6 +84,7 @@ CodeConv::ConvertFromInternal(const char *src, int32 srclen, char *dst, int codi
//! Convert internal coding to src coding.
/* static */
int32
CodeConv::ConvertToInternal(const char *src, int32 srclen, char *dst, int coding)
{
@@ -131,6 +124,7 @@ CodeConv::ConvertToInternal(const char *src, int32 srclen, char *dst, int coding
}
/* static */
unsigned short
CodeConv::UTF8toUnicode(const char *utf8)
{
@@ -157,6 +151,7 @@ CodeConv::UTF8toUnicode(const char *utf8)
}
/* static */
void
CodeConv::euc_to_sjis(uchar *buf)
{
+5 -11
View File
@@ -13,23 +13,17 @@
class CodeConv {
public:
CodeConv();
~CodeConv();
int32 UTF8GetFontWidth(const char *string);
static int32 UTF8GetFontWidth(const char *string);
/* internal(UTF8) -> coding */
int32 ConvertFromInternal(const char *src, int32 bytes, char *dst, int coding);
static int32 ConvertFromInternal(const char *src, int32 bytes, char *dst, int coding);
/* coding -> internal(UTF8) */
int32 ConvertToInternal(const char *src, int32 bytes, char *dst, int coding);
static int32 ConvertToInternal(const char *src, int32 bytes, char *dst, int coding);
private:
void euc_to_sjis(uchar *buf);
unsigned short UTF8toUnicode(const char *utf8);
int fNowCoding;
static void euc_to_sjis(uchar *buf);
static unsigned short UTF8toUnicode(const char *utf8);
};
#endif /* CODECONV_H */
+44 -2
View File
@@ -33,6 +33,7 @@
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <new>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
@@ -48,6 +49,8 @@
#include <OS.h>
#include "TermConst.h"
#include "TermParse.h"
#include "TermView.h"
#include "Shell.h"
#include "PrefHandler.h"
@@ -108,7 +111,10 @@ static pid_t sShPid;
Shell::Shell()
:fFd(-1)
:
fFd(-1),
fTermParse(NULL),
fAttached(false)
{
}
@@ -125,13 +131,25 @@ Shell::Open(int row, int col, const char *command, const char *coding)
if (fFd >= 0)
return B_ERROR;
return _Spawn(row, col, command, coding);
status_t status = _Spawn(row, col, command, coding);
if (status < B_OK)
return status;
fTermParse = new (std::nothrow) TermParse(fFd);
if (fTermParse == NULL) {
Close();
return B_NO_MEMORY;
}
return B_OK;
}
void
Shell::Close()
{
delete fTermParse;
if (fFd >= 0) {
close(fFd);
kill(-sShPid, SIGHUP);
@@ -208,6 +226,30 @@ Shell::FD() const
}
void
Shell::ViewAttached(TermView *view)
{
if (fAttached)
return;
status_t status = fTermParse->StartThreads(view);
if (status < B_OK) {
// TODO: What can we do here ?
fprintf(stderr, "Shell:ViewAttached():"
" cannot start parser threads: %s",
strerror(status));
}
}
void
Shell::ViewDetached()
{
if (fAttached)
fTermParse->StopThreads();
}
// private
static status_t
send_handshake_message(thread_id target, const handshake_t& handshake)
+10 -2
View File
@@ -81,10 +81,13 @@
#define RDEL 0xFF
// TODO: Maybe merge TermParse and Shell classes ?
class TermParse;
class TermView;
class Shell {
public:
Shell();
~Shell();
virtual ~Shell();
status_t Open(int row, int col, const char *command, const char *coding);
void Close();
@@ -101,9 +104,14 @@ public:
status_t SetAttr(struct termios &attr);
int FD() const;
virtual void ViewAttached(TermView *view);
virtual void ViewDetached();
private:
int fFd;
int fFd;
TermParse *fTermParse;
bool fAttached;
status_t _Spawn(int row, int col, const char *command, const char *coding);
};
+70 -61
View File
@@ -47,11 +47,10 @@ extern int mbcstable[]; /* ESC $ */
#define NPARAM 10 // Max parameters
TermParse::TermParse(int fd, TermView *inViewObj, CodeConv *inConvObj)
TermParse::TermParse(int fd)
:
fFd(fd),
fViewObj(inViewObj),
fConvObj(inConvObj),
fViewObj(NULL),
fParseThread(-1),
fParseSem(-1),
fReaderThread(-1),
@@ -66,6 +65,33 @@ TermParse::TermParse(int fd, TermView *inViewObj, CodeConv *inConvObj)
TermParse::~TermParse()
{
StopThreads();
}
status_t
TermParse::StartThreads(TermView *view)
{
fViewObj = view;
status_t status = InitPtyReader();
if (status < B_OK)
return status;
status = InitTermParse();
if (status < B_OK) {
//AbortPtyReader();
return status;
}
return B_OK;
}
status_t
TermParse::StopThreads()
{
fQuitting = true;
@@ -78,22 +104,8 @@ TermParse::~TermParse()
status_t dummy;
wait_for_thread(fReaderThread, &dummy);
wait_for_thread(fParseThread, &dummy);
}
status_t
TermParse::StartThreads()
{
status_t status = InitPtyReader();
if (status < B_OK)
return status;
status = InitTermParse();
if (status < B_OK) {
//AbortPtyReader();
return status;
}
fViewObj = NULL;
return B_OK;
}
@@ -241,10 +253,7 @@ TermParse::EscParse()
int top, bot;
int cs96;
uchar curess = 0;
TermView *viewObj = fViewObj;
CodeConv *convObj = fConvObj;
uchar cbuf[4], dstbuf[4];
uchar *ptr;
@@ -305,7 +314,7 @@ TermParse::EscParse()
cbuf[0] = c;
cbuf[1] = '\0';
width = HALF_WIDTH;
viewObj->PutChar(cbuf, attr, width);
fViewObj->PutChar(cbuf, attr, width);
break;
case CASE_PRINT_GR:
@@ -346,11 +355,11 @@ TermParse::EscParse()
}
if (now_coding != M_ISO_2022_JP)
convObj->ConvertToInternal((char*)cbuf, -1, (char*)dstbuf, now_coding);
CodeConv::ConvertToInternal((char*)cbuf, -1, (char*)dstbuf, now_coding);
else
convObj->ConvertToInternal((char*)cbuf, -1, (char*)dstbuf, M_EUC_JP);
CodeConv::ConvertToInternal((char*)cbuf, -1, (char*)dstbuf, M_EUC_JP);
viewObj->PutChar(dstbuf, attr, width);
fViewObj->PutChar(dstbuf, attr, width);
break;
case CASE_PRINT_CS96:
@@ -359,33 +368,33 @@ TermParse::EscParse()
cbuf[1] |= 0x80;
cbuf[2] = 0;
width = 2;
convObj->ConvertToInternal((char*)cbuf, 2, (char*)dstbuf, M_EUC_JP);
viewObj->PutChar(dstbuf, attr, width);
CodeConv::ConvertToInternal((char*)cbuf, 2, (char*)dstbuf, M_EUC_JP);
fViewObj->PutChar(dstbuf, attr, width);
break;
case CASE_LF:
viewObj->PutLF();
fViewObj->PutLF();
break;
case CASE_CR:
viewObj->PutCR();
fViewObj->PutCR();
break;
case CASE_SJIS_KANA:
cbuf[0] = (uchar)c;
cbuf[1] = '\0';
convObj->ConvertToInternal((char*)cbuf, 1, (char*)dstbuf, now_coding);
CodeConv::ConvertToInternal((char*)cbuf, 1, (char*)dstbuf, now_coding);
width = 1;
viewObj->PutChar(dstbuf, attr, width);
fViewObj->PutChar(dstbuf, attr, width);
break;
case CASE_SJIS_INSTRING:
cbuf[0] = (uchar)c;
GetReaderBuf(cbuf[1]);
cbuf[2] = '\0';
convObj->ConvertToInternal((char*)cbuf, 2, (char*)dstbuf, now_coding);
CodeConv::ConvertToInternal((char*)cbuf, 2, (char*)dstbuf, now_coding);
width = 2;
viewObj->PutChar(dstbuf, attr, width);
fViewObj->PutChar(dstbuf, attr, width);
break;
case CASE_UTF8_2BYTE:
@@ -395,8 +404,8 @@ TermParse::EscParse()
break;
cbuf[1] = (uchar)c;
cbuf[2] = '\0';
width = convObj->UTF8GetFontWidth((char*)cbuf);
viewObj->PutChar(cbuf, attr, width);
width = CodeConv::UTF8GetFontWidth((char*)cbuf);
fViewObj->PutChar(cbuf, attr, width);
break;
case CASE_UTF8_3BYTE:
@@ -411,8 +420,8 @@ TermParse::EscParse()
break;
cbuf[2] = c;
cbuf[3] = '\0';
width = convObj->UTF8GetFontWidth((char*)cbuf);
viewObj->PutChar (cbuf, attr, width);
width = CodeConv::UTF8GetFontWidth((char*)cbuf);
fViewObj->PutChar (cbuf, attr, width);
break;
case CASE_MBCS:
@@ -444,13 +453,13 @@ TermParse::EscParse()
break;
case CASE_BS:
viewObj->MoveCurLeft(1);
fViewObj->MoveCurLeft(1);
break;
case CASE_TAB:
tmp = viewObj->GetCurX();
tmp = fViewObj->GetCurX();
tmp %= 8;
viewObj->MoveCurRight(8 - tmp);
fViewObj->MoveCurRight(8 - tmp);
break;
case CASE_ESC:
@@ -510,7 +519,7 @@ TermParse::EscParse()
/* ICH */
if ((row = param[0]) < 1)
row = 1;
viewObj->InsertSpace(row);
fViewObj->InsertSpace(row);
parsestate = groundtable;
break;
@@ -518,7 +527,7 @@ TermParse::EscParse()
/* CUU */
if ((row = param[0]) < 1)
row = 1;
viewObj->MoveCurUp(row);
fViewObj->MoveCurUp(row);
parsestate = groundtable;
break;
@@ -526,7 +535,7 @@ TermParse::EscParse()
/* CUD */
if ((row = param[0]) < 1)
row = 1;
viewObj->MoveCurDown(row);
fViewObj->MoveCurDown(row);
parsestate = groundtable;
break;
@@ -534,7 +543,7 @@ TermParse::EscParse()
/* CUF */
if ((row = param[0]) < 1)
row = 1;
viewObj->MoveCurRight(row);
fViewObj->MoveCurRight(row);
parsestate = groundtable;
break;
@@ -542,7 +551,7 @@ TermParse::EscParse()
/* CUB */
if ((row = param[0]) < 1)
row = 1;
viewObj->MoveCurLeft(row);
fViewObj->MoveCurLeft(row);
parsestate = groundtable;
break;
@@ -553,7 +562,7 @@ TermParse::EscParse()
if (nparam < 2 || (col = param[1]) < 1)
col = 1;
viewObj->SetCurPos(col - 1, row - 1 );
fViewObj->SetCurPos(col - 1, row - 1 );
parsestate = groundtable;
break;
@@ -562,15 +571,15 @@ TermParse::EscParse()
switch (param[0]) {
case DEFAULT:
case 0:
viewObj->EraseBelow();
fViewObj->EraseBelow();
break;
case 1:
break;
case 2:
viewObj->SetCurPos(0, 0);
viewObj->EraseBelow();
fViewObj->SetCurPos(0, 0);
fViewObj->EraseBelow();
break;
}
parsestate = groundtable;
@@ -578,7 +587,7 @@ TermParse::EscParse()
case CASE_EL: // delete line
/* EL */
viewObj->DeleteColumns();
fViewObj->DeleteColumns();
parsestate = groundtable;
break;
@@ -586,7 +595,7 @@ TermParse::EscParse()
/* IL */
if ((row = param[0]) < 1)
row = 1;
viewObj->PutNL(row);
fViewObj->PutNL(row);
parsestate = groundtable;
break;
@@ -594,7 +603,7 @@ TermParse::EscParse()
/* DL */
if ((row = param[0]) < 1)
row = 1;
viewObj->DeleteLine(row);
fViewObj->DeleteLine(row);
parsestate = groundtable;
break;
@@ -602,19 +611,19 @@ TermParse::EscParse()
/* DCH */
if ((row = param[0]) < 1)
row = 1;
viewObj->DeleteChar(row);
fViewObj->DeleteChar(row);
parsestate = groundtable;
break;
case CASE_SET:
/* SET */
viewObj->SetInsertMode(MODE_INSERT);
fViewObj->SetInsertMode(MODE_INSERT);
parsestate = groundtable;
break;
case CASE_RST:
/* RST */
viewObj->SetInsertMode(MODE_OVER);
fViewObj->SetInsertMode(MODE_OVER);
parsestate = groundtable;
break;
@@ -681,7 +690,7 @@ TermParse::EscParse()
case CASE_CPR:
// Q & D hack by Y.Hayakawa ([email protected])
// 21-JUL-99
viewObj->DeviceStatusReport(param[0]);
fViewObj->DeviceStatusReport(param[0]);
parsestate = groundtable;
break;
@@ -700,7 +709,7 @@ TermParse::EscParse()
bot--;
if (bot > top)
viewObj->SetScrollRegion(top, bot);
fViewObj->SetScrollRegion(top, bot);
parsestate = groundtable;
break;
@@ -737,13 +746,13 @@ TermParse::EscParse()
case CASE_DECSC:
/* DECSC */
viewObj->SaveCursor();
fViewObj->SaveCursor();
parsestate = groundtable;
break;
case CASE_DECRC:
/* DECRC */
viewObj->RestoreCursor();
fViewObj->RestoreCursor();
parsestate = groundtable;
break;
@@ -755,7 +764,7 @@ TermParse::EscParse()
case CASE_RI:
/* RI */
viewObj->ScrollRegion(-1, -1, SCRDOWN, 1);
fViewObj->ScrollRegion(-1, -1, SCRDOWN, 1);
parsestate = groundtable;
break;
+5 -7
View File
@@ -34,21 +34,20 @@
#include "TermConst.h"
#include <OS.h>
#include <Handler.h>
#include <MessageRunner.h>
class TermView;
class CodeConv;
//PtyReader buffer size.
#define READ_BUF_SIZE 2048
class TermView;
class TermParse : public BHandler {
public:
TermParse(int fd, TermView *inViewObj, CodeConv *inConvObj);
TermParse(int fd);
~TermParse();
status_t StartThreads();
status_t StartThreads(TermView *view);
status_t StopThreads();
private:
// Initialize TermParse and PtyReader thread.
@@ -71,7 +70,6 @@ private:
int fFd;
TermView *fViewObj;
CodeConv *fConvObj;
thread_id fParseThread;
sem_id fParseSem;
+6 -5
View File
@@ -61,7 +61,7 @@ const static rgb_color kTermColorTable[16] = {
};
TermView::TermView(BRect frame, CodeConv *inCodeConv)
TermView::TermView(BRect frame)
: BView(frame, "termview", B_FOLLOW_ALL, B_WILL_DRAW | B_FRAME_EVENTS),
fShell(NULL),
fFontWidth(0),
@@ -85,7 +85,6 @@ TermView::TermView(BRect frame, CodeConv *inCodeConv)
fTermColumns(PrefHandler::Default()->getInt32(PREF_COLS)),
fTop(0),
fTextBuffer(new (nothrow) TermBuffer(fTermRows, fTermColumns)),
fCodeConv(inCodeConv),
fScrollBar(NULL),
fScrTop(0),
fScrBot(fTermRows - 1),
@@ -127,7 +126,8 @@ TermView::AttachShell(Shell *shell)
return B_BAD_VALUE;
fShell = shell;
fShell->ViewAttached(this);
return B_OK;
}
@@ -135,6 +135,7 @@ TermView::AttachShell(Shell *shell)
void
TermView::DetachShell()
{
fShell->ViewDetached();
fShell = NULL;
}
@@ -1352,7 +1353,7 @@ TermView::KeyDown(const char *bytes, int32 numBytes)
// input multibyte character
if (GetEncoding() != M_UTF8) {
int cnum = fCodeConv->ConvertFromInternal(bytes, numBytes,
int cnum = CodeConv::ConvertFromInternal(bytes, numBytes,
(char *)dstbuf, GetEncoding());
fShell->Write(dstbuf, cnum);
return;
@@ -1600,7 +1601,7 @@ TermView::WritePTY(const uchar *text, int numBytes)
{
if (GetEncoding() != M_UTF8) {
uchar *destBuffer = (uchar *)malloc(numBytes * 3);
numBytes = fCodeConv->ConvertFromInternal((char*)text, numBytes,
numBytes = CodeConv::ConvertFromInternal((char*)text, numBytes,
(char*)destBuffer, GetEncoding());
fShell->Write(destBuffer, numBytes);
free(destBuffer);
+1 -3
View File
@@ -90,7 +90,6 @@ const unsigned char M_ADD_CURSOR [] = {
};
class TermBuffer;
class CodeConv;
class BPopUpMenu;
class BScrollBar;
class BString;
@@ -98,7 +97,7 @@ class Shell;
class TermView : public BView {
public:
TermView(BRect frame, CodeConv *inCodeConv);
TermView(BRect frame);
~TermView();
status_t AttachShell(Shell *shell);
@@ -282,7 +281,6 @@ class TermView : public BView {
// Object pointer.
TermBuffer *fTextBuffer;
CodeConv *fCodeConv;
BScrollBar *fScrollBar;
// Offscreen Bitmap and View.
+2 -11
View File
@@ -27,7 +27,7 @@
#include <sys/time.h>
#include <unistd.h>
#include "CodeConv.h"
#include "Coding.h"
#include "ColorWindow.h"
#include "MenuUtil.h"
#include "FindWindow.h"
@@ -54,7 +54,6 @@
TermWindow::TermWindow(BRect frame, const char* title, const char *command)
: BWindow(frame, title, B_DOCUMENT_WINDOW, B_CURRENT_WORKSPACE|B_QUIT_ON_WINDOW_CLOSE),
fShell(NULL),
fTermParse(NULL),
fMenubar(NULL),
fFilemenu(NULL),
fEditmenu(NULL),
@@ -64,7 +63,6 @@ TermWindow::TermWindow(BRect frame, const char* title, const char *command)
fWindowSizeMenu(NULL),
fNewFontMenu(NULL),
fTermView(NULL),
fCodeConv(NULL),
fPrintSettings(NULL),
fPrefWindow(NULL),
fFindPanel(NULL),
@@ -106,8 +104,6 @@ TermWindow::~TermWindow()
fTermView->DetachShell();
delete fShell;
delete fTermParse;
delete fCodeConv;
if (fPrefWindow)
fPrefWindow->PostMessage(B_QUIT_REQUESTED);
@@ -159,13 +155,11 @@ TermWindow::InitWindow()
BRect textframe = Bounds();
textframe.top = fMenubar->Bounds().bottom + 1.0;
fCodeConv = new CodeConv();
fTermView = new TermView(textframe, fCodeConv);
fTermView = new TermView(textframe);
fTermView->AttachShell(fShell);
// Initialize TermView. (font, size and color)
fTermView->SetTermFont(&halfFont, &fullFont);
BRect rect = fTermView->SetTermSize(PrefHandler::Default()->getInt32(PREF_ROWS),
@@ -205,9 +199,6 @@ TermWindow::InitWindow()
// Initialize TermParse
SetEncoding(longname2id(PrefHandler::Default()->getString(PREF_TEXT_ENCODING)));
fTermParse = new TermParse(fShell->FD(), fTermView, fCodeConv);
if (fTermParse->StartThreads() < B_OK)
return;
// Initialize MessageRunner.
fWindowUpdate = new BMessageRunner(BMessenger(this),
-2
View File
@@ -37,7 +37,6 @@
class BMenu;
class BMenuBar;
class BMessageRunner;
class CodeConv;
class FindWindow;
class PrefWindow;
class Shell;
@@ -79,7 +78,6 @@ private:
*fWindowSizeMenu,
*fNewFontMenu;
TermView *fTermView;
CodeConv *fCodeConv;
BMessage *fPrintSettings;
PrefWindow *fPrefWindow;
FindWindow *fFindPanel;