📄 term.c
字号:
/* Return the state of auto-copy on selection
*/
BOOL termAutoCopy()
{
return autoCopy;
}
/* Return the state of xterm-style attached printer */
BOOL termAttachedPrinter(void)
{
return term.attachedPrinter;
}
/* Toggle the state of xterm-style attached printer */
void termToggleAttachedPrinter(void)
{
term.attachedPrinter = !term.attachedPrinter;
}
/* Return the state of printscreen */
BOOL termPrintScreenEnabled(void)
{
return term.enablePrintScreen;
}
/* Toggle the state of printscreen */
void termTogglePrintScreen(void)
{
term.enablePrintScreen = !term.enablePrintScreen;
}
/* Return the current cursor style */
CursorStyle termCursorStyle(void)
{
return cursorStyle;
}
/* Set the cursor style */
void termSetCursorStyle(CursorStyle style)
{
/* hide and destroy the old cursor */
termHideCaret();
termKillFocus();
/* create the new caret bitmap */
cursorStyle = style;
termMakeCaretBitmap();
/* restore the cursor */
termSetFocus();
}
/* User pressed the any mouse button
*
* Args:
* flags mouse key flags
* xpos - x-position of mouse in pixels
* ypos - y-position of mouse in pixels
*
* button - 0=left 1=middle 2=right 3=none
| 4=shift 8=meta 16=ctrl
*/
void termSendMouseEvent(int flags, int xpos, int ypos)
{
static char evstr[7];
unsigned char button;
evstr[0] = '\x1b';
evstr[1] = '[';
evstr[2] = 'M';
button = ((flags & MK_LBUTTON)? (char)0 :
((flags & MK_MBUTTON)? (char)1 :
((flags & MK_RBUTTON)? (char)2 : (char)3)));
/* check which button is pressed */
button |= ((flags & MK_SHIFT) ? (char)4 : (char)0);
/* check if shift is pressed */
button |= ((flags & MK_CONTROL) ? (char)16 : (char)0);
/* check if ctrl is pressed */
evstr[3] = (char)(button + 0x20);
evstr[4] = (char)((xpos / term.charSize.cx)+1 + 0x20);
evstr[5] = (char)((ypos / term.charSize.cy)+1 + 0x20);
evstr[6] = (char)0;
emulFuncKeyPress(evstr);
}
/* Return the state of mouse event reporting
*/
BOOL termMouseReporting()
{
return term.mouseReporting;
}
/* User pressed the left mouse button
*
* Args:
* flags - key flags (from Windows message)
* xpos - x-position of mouse in pixels
* ypos - y-position of mouse in pixels
*/
static void termLeftButtonDown(UINT flags, int xpos, int ypos)
{
BOOL inNextChar; /* is mouse in next character cell? */
if (term.mouseReporting)
{
termSendMouseEvent(flags, xpos, ypos);
}
else
{
/* Capture the mouse until the button is released
*/
SetCapture(termWnd);
if (GetCapture() != termWnd)
return;
/* Convert pixels to terminal character position
*/
inNextChar = termSelectAdjustPos(&xpos, &ypos);
if (flags & MK_SHIFT)
/* When left mouse pressed with SHIFT key, extend the current
* selection
*/
termSelectExtend(xpos, ypos, inNextChar);
else {
/* Start a new selection in select characters mode
*/
termSelectCharMode();
termSelectStart(xpos, ypos);
}
}
}
/* User released the left mouse button
*
* Args:
* flags - key flags (from Windows message)
* xpos - x-position of mouse in pixels
* ypos - y-position of mouse in pixels
*/
static void termLeftButtonUp(UINT flags, int xpos, int ypos)
{
BOOL inNextChar; /* is mouse in next character cell? */
if (term.mouseReporting)
{
termSendMouseEvent(flags, xpos, ypos);
}
else
{
if (GetCapture() != termWnd)
/* If we do not own the mouse, ignore this event
*/
return;
/* Release the mouse capture and stop the selection timer
*/
ReleaseCapture();
termSelectStopTimer();
/* Convert pixels to terminal character position
*/
inNextChar = termSelectAdjustPos(&xpos, &ypos);
if (flags & MK_SHIFT)
/* When left mouse pressed with SHIFT key, extend the current
* selection
*/
termSelectExtend(xpos, ypos, inNextChar);
else
/* Define the end of new selection
*/
termSelectEnd(xpos, ypos, inNextChar);
if (autoCopy && termSelectCheck())
/* If there is a selection defined, copy it to the clipboard
*/
termSelectCopy();
}
}
/* User double clicked the left mouse button
*
* Args:
* flags - key flags (from Windows message)
* xpos - x-position of mouse in pixels
* ypos - y-position of mouse in pixels
*/
static void termLeftButtonDouble(UINT flags, int xpos, int ypos)
{
BOOL inNextChar; /* is mouse in next character cell? */
if (term.mouseReporting)
{
termSendMouseEvent(flags, xpos, ypos);
}
else
{
/* Capture the mouse until the button is released
*/
SetCapture(termWnd);
if (GetCapture() != termWnd)
return;
/* Switch the selection mode to select words
*/
termSelectWordMode();
/* Convert pixels to terminal character position
*/
inNextChar = termSelectAdjustPos(&xpos, &ypos);
if (flags & MK_SHIFT)
/* When left mouse pressed with SHIFT key, extend the current
* selection
*/
termSelectExtend(xpos, ypos, inNextChar);
else
/* Start a new selection in select words mode
*/
termSelectStart(xpos, ypos);
}
}
/* User pressed the middle mouse button
*
* Args:
* flags - key flags (from Windows message)
* xpos - x-position of mouse in pixels
* ypos - y-position of mouse in pixels
*/
static void termMiddleButtonDown(UINT flags, int xpos, int ypos)
{
if (term.mouseReporting)
{
termSendMouseEvent(flags, xpos, ypos);
}
else
{
/* Act like xterm, paste selection
*/
termSelectPaste();
}
}
/* User double clicked the middle mouse button
*
* Args:
* flags - key flags (from Windows message)
* xpos - x-position of mouse in pixels
* ypos - y-position of mouse in pixels
*/
static void termMiddleButtonDouble(UINT flags, int xpos, int ypos)
{
if (term.mouseReporting)
{
termSendMouseEvent(flags, xpos, ypos);
}
else
{
/* Act like xterm - paste the selection
*/
termSelectPaste();
}
}
/* User released the middle mouse button
*
* Args:
* flags - key flags (from Windows message)
* xpos - x-position of mouse in pixels
* ypos - y-position of mouse in pixels
*/
static void termMiddleButtonUp(UINT flags, int xpos, int ypos)
{
if (term.mouseReporting)
termSendMouseEvent(flags, xpos, ypos);
}
/* User pressed the right mouse button
*
* Args:
* flags - key flags (from Windows message)
* xpos - x-position of mouse in pixels
* ypos - y-position of mouse in pixels
*/
static void termRightButtonDown(UINT flags, int xpos, int ypos)
{
BOOL inNextChar; /* is mouse in next character cell? */
if (term.mouseReporting)
{
termSendMouseEvent(flags, xpos, ypos);
}
else
{
/* Capture the mouse until the button is released
*/
SetCapture(termWnd);
if (GetCapture() != termWnd)
return;
/* Convert pixels to terminal character position
*/
inNextChar = termSelectAdjustPos(&xpos, &ypos);
/* Extend the current selection
*/
termSelectExtend(xpos, ypos, inNextChar);
}
}
/* User released the right mouse button
*
* Args:
* flags - key flags (from Windows message)
* xpos - x-position of mouse in pixels
* ypos - y-position of mouse in pixels
*/
static void termRightButtonUp(UINT flags, int xpos, int ypos)
{
BOOL inNextChar; /* is mouse in next character cell? */
if (term.mouseReporting)
{
termSendMouseEvent(flags, xpos, ypos);
}
else
{
if (GetCapture() != termWnd)
/* If we do not own the mouse, ignore this event
*/
return;
/* Release the mouse capture and stop the selection timer
*/
ReleaseCapture();
termSelectStopTimer();
/* Convert pixels to terminal character position
*/
inNextChar = termSelectAdjustPos(&xpos, &ypos);
/* Extend the current selection
*/
termSelectExtend(xpos, ypos, inNextChar);
if (autoCopy && termSelectCheck())
/* If there is a selection defined, copy it to the clipboard
*/
termSelectCopy();
}
}
/* User double clicked the right mouse button
*
* Args:
* flags - key flags (from Windows message)
* xpos - x-position of mouse in pixels
* ypos - y-position of mouse in pixels
*/
static void termRightButtonDouble(UINT flags, int xpos, int ypos)
{
BOOL inNextChar; /* is mouse in next character cell? */
if (term.mouseReporting)
{
termSendMouseEvent(flags, xpos, ypos);
}
else
{
/* Capture the mouse until the button is released
*/
SetCapture(termWnd);
if (GetCapture() != termWnd)
return;
/* Switch the selection mode to select words
*/
termSelectWordMode();
/* Convert pixels to terminal character position
*/
inNextChar = termSelectAdjustPos(&xpos, &ypos);
/* Extend the current selection
*/
termSelectExtend(xpos, ypos, inNextChar);
}
}
/* User moved the mouse in the terminal window
*
* Args:
* flags - key flags (from Windows message)
* xpos - x-position of mouse in pixels
* ypos - y-position of mouse in pixels
*/
static void termMouseMove(UINT flags, int xpos, int ypos)
{
if (GetCapture() != termWnd)
/* If we do not own the mouse, ignore this event
*/
return;
/* Convert mouse position in pixels to character cells. Do not
* use termSelectAdjustPos() as it will clip the position to the
* terminal size
*/
xpos /= term.charSize.cx;
ypos /= term.charSize.cy;
/* Limit the x-position to valid character columns
*/
if (xpos < 0)
xpos = 0;
if (xpos > term.winSize.cx)
xpos = term.winSize.cx;
if (ypos < 0 || ypos >= term.winSize.cy)
/* When the mouse is above or below the window, we start the
* select timer to trigger automatic scrolling
*/
termSelectStartTimer();
else {
/* When mouse is in the terminal window, stop the timer and
* extend the selection to include the character/word the
* mouse is over
*/
termSelectStopTimer();
termSelectExtend(xpos, ypos, TRUE);
}
}
/* We received a timer event - perform automatic scrolling and extend
* the selection
*/
static void termTimer(void)
{
POINT point; /* position of the mouse */
/* Get the position of the mouse in screen coordinates and convert
* it to terminal window coordinates (in pixels)
*/
GetCursorPos(&point);
ScreenToClient(termWnd, &point);
/* Convert the mouse position to character cells directly to avoid
* clipping in termSelectAdjustPos()
*/
point.x /= term.charSize.cx;
point.y /= term.charSize.cy;
/* Limit the x-position to valid character columns
*/
if (point.x < 0)
point.x = 0;
if (point.x > term.winSize.cx)
point.x = term.winSize.cx;
if (point.y < 0) {
/* Mouse is above window - scroll and select backwards one line
*/
termTryScroll(-1);
termSelectExtend(point.x, 0, TRUE);
} else if (point.y >= term.winSize.cy) {
/* Mouse is below window - scroll and select forwards one line
*/
termTryScroll(1);
termSelectExtend(point.x, term.winSize.cy - 1, TRUE);
} else
return;
/* Redraw the caret at the correct position
*/
winCaretPos(term.cursor.x, term.cursor.y);
}
/* User pressed a key - process function keys
*
* Args:
* key - key that was pressed
* flags - key data from Windows message
*/
void termKeyDown(UINT key, DWORD flags)
{
/* Get state of Shift key when message was sent
*/
BOOL shiftPressed = (GetKeyState(VK_SHIFT) & KF_UP) != 0;
BOOL controlPressed = (GetKeyState(VK_CONTROL) & KF_UP) != 0;
BOOL isRepeat = (HIWORD(flags) & KF_REPEAT) != 0;
if (!term.autoRepeat && isRepeat)
return;
switch (key) {
case VK_PRIOR:
if (shiftPressed)
/* Shift-PageUp acts like xterm - scroll back in history
* half a terminal height
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -