Skip to content

Commit

Permalink
prevent macros from intercepting console-finished keypress (fix #1003)…
Browse files Browse the repository at this point in the history
…; refactor: favor using of RAII macro mode changer
  • Loading branch information
elfmz committed Aug 16, 2021
1 parent b4b94ea commit 8c8a2db
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 59 deletions.
10 changes: 2 additions & 8 deletions far2l/chgmmode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,11 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "chgmmode.hpp"
#include "ctrlobj.hpp"

ChangeMacroMode::ChangeMacroMode(int NewMode)
ChangeMacroMode::ChangeMacroMode(int NewMode) :
PrevMacroMode( CtrlObject ? CtrlObject->Macro.GetMode() : MACRO_SHELL)
{
if (CtrlObject)
{
PrevMacroMode=CtrlObject->Macro.GetMode();
CtrlObject->Macro.SetMode(NewMode);
}
else
{
PrevMacroMode=MACRO_SHELL;
}
}


Expand Down
20 changes: 5 additions & 15 deletions far2l/dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,9 @@ void DataToItemEx(const DialogDataEx *Data,DialogItemEx *Item,int Count)
Dialog::Dialog(DialogItemEx *SrcItem, // Набор элементов диалога
unsigned SrcItemCount, // Количество элементов
FARWINDOWPROC DlgProc, // Диалоговая процедура
LONG_PTR InitParam): // Ассоцированные с диалогом данные
bInitOK(false)
LONG_PTR InitParam) // Ассоцированные с диалогом данные
:
CMM(MACRO_DIALOG)
{
Dialog::Item = (DialogItemEx**)xf_malloc(sizeof(DialogItemEx*)*SrcItemCount);

Expand All @@ -426,8 +427,9 @@ Dialog::Dialog(FarDialogItem *SrcItem, // Набор элементов ди
unsigned SrcItemCount, // Количество элементов
FARWINDOWPROC DlgProc, // Диалоговая процедура
LONG_PTR InitParam) // Ассоцированные с диалогом данные
:
CMM(MACRO_DIALOG)
{
bInitOK = false;
Dialog::Item = (DialogItemEx**)xf_malloc(sizeof(DialogItemEx*)*SrcItemCount);

for (unsigned i = 0; i < SrcItemCount; i++)
Expand Down Expand Up @@ -467,20 +469,11 @@ void Dialog::Init(FARWINDOWPROC DlgProc, // Диалоговая проце

Dialog::RealDlgProc=DlgProc;

if (CtrlObject)
{
// запомним пред. режим макро.
PrevMacroMode=CtrlObject->Macro.GetMode();
// макросить будет в диалогах :-)
CtrlObject->Macro.SetMode(MACRO_DIALOG);
}

//_SVS(SysLog(L"Dialog =%d",CtrlObject->Macro.GetMode()));
// запоминаем предыдущий заголовок консоли
OldTitle=new ConsoleTitle;
IdExist=false;
memset(&Id,0,sizeof(Id));
bInitOK = true;
}

//////////////////////////////////////////////////////////////////////////
Expand All @@ -492,9 +485,6 @@ Dialog::~Dialog()
_tran(SysLog(L"[%p] Dialog::~Dialog()",this));
DeleteDialogObjects();

if (CtrlObject)
CtrlObject->Macro.SetMode(PrevMacroMode);

Hide();
ScrBuf.Flush();

Expand Down
5 changes: 2 additions & 3 deletions far2l/dialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "frame.hpp"
#include "plugin.hpp"
#include "vmenu.hpp"
#include "chgmmode.hpp"
#include "bitflags.hpp"
#include "CriticalSections.hpp"

Expand Down Expand Up @@ -262,7 +263,7 @@ class Dialog: public Frame
friend LONG_PTR WINAPI DefDlgProc(HANDLE hDlg,int Msg,int Param1,LONG_PTR Param2);

private:
bool bInitOK; // диалог был успешно инициализирован
ChangeMacroMode CMM;
INT_PTR PluginNumber; // Номер плагина, для формирования HelpTopic
unsigned FocusPos; // всегда известно какой элемент в фокусе
unsigned PrevFocusPos; // всегда известно какой элемент был в фокусе
Expand All @@ -277,7 +278,6 @@ class Dialog: public Frame
unsigned ItemCount; // количество элементов диалога

ConsoleTitle *OldTitle; // предыдущий заголовок
int PrevMacroMode; // предыдущий режим макро

FARWINDOWPROC RealDlgProc; // функция обработки диалога

Expand Down Expand Up @@ -361,7 +361,6 @@ class Dialog: public Frame
FARWINDOWPROC DlgProc=nullptr,LONG_PTR InitParam=0);
Dialog(FarDialogItem *SrcItem, unsigned SrcItemCount,
FARWINDOWPROC DlgProc=nullptr,LONG_PTR InitParam=0);
bool InitOK() {return bInitOK;}
virtual ~Dialog();

public:
Expand Down
7 changes: 6 additions & 1 deletion far2l/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "constitle.hpp"
#include "console.hpp"
#include "constitle.hpp"
#include "chgmmode.hpp"
#include "vtshell.h"
#include "InterThreadCall.hpp"
#include "ScopeHelpers.h"
Expand Down Expand Up @@ -444,7 +445,11 @@ int CommandLine::CmdExecute(const wchar_t *CmdLine, bool SeparateWindow, bool Di
auto *cp = CtrlObject->Cp();
if (!CloseFAR && cp && cp->LeftPanel && cp->RightPanel
&& (cp->LeftPanel->IsVisible() || cp->RightPanel->IsVisible())) {
int Key = WaitKey();
int Key;
{
ChangeMacroMode cmm(MACRO_OTHER); // prevent macros from intercepting key (#1003)
Key = WaitKey();
}
// allow user to open console log etc directly from pause-on-error state
if (Key == KEY_MSWHEEL_UP) {
Key|= KEY_CTRL | KEY_SHIFT;
Expand Down
8 changes: 2 additions & 6 deletions far2l/foldtree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "exitcode.hpp"

FolderTree::FolderTree(FARString &strResultFolder,int iModalMode,int IsStandalone,int IsFullScreen):
CMM(MACRO_FINDFOLDER),
Tree(nullptr),
FindEdit(nullptr),
ModalMode(iModalMode),
IsFullScreen(IsFullScreen),
IsStandalone(IsStandalone),
PrevMacroMode(CtrlObject->Macro.GetMode())
IsStandalone(IsStandalone)
{
SetDynamicallyBorn(FALSE);
SetRestoreScreenMode(TRUE);
Expand All @@ -67,8 +67,6 @@ FolderTree::FolderTree(FARString &strResultFolder,int iModalMode,int IsStandalon

if ((Tree=new(std::nothrow) TreeList(FALSE)))
{
CtrlObject->Macro.SetMode(MACRO_FINDFOLDER);
MacroMode = MACRO_FINDFOLDER;
strLastName.Clear();
Tree->SetModalMode(ModalMode);
Tree->SetPosition(X1,Y1,X2,Y2);
Expand Down Expand Up @@ -104,8 +102,6 @@ FolderTree::FolderTree(FARString &strResultFolder,int iModalMode,int IsStandalon

FolderTree::~FolderTree()
{
CtrlObject->Macro.SetMode(PrevMacroMode);

//if ( TopScreen ) delete TopScreen;
if (FindEdit) delete FindEdit;

Expand Down
3 changes: 2 additions & 1 deletion far2l/foldtree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "frame.hpp"
#include "keybar.hpp"
#include "chgmmode.hpp"

class TreeList;
class Edit;
Expand All @@ -43,14 +44,14 @@ class SaveScreen;
class FolderTree:public Frame
{
private:
ChangeMacroMode CMM;
TreeList *Tree;
Edit *FindEdit;

KeyBar TreeKeyBar; // кейбар
int ModalMode;
int IsFullScreen;
int IsStandalone;
int PrevMacroMode; // предыдущий режим макроса

FARString strNewFolder;
FARString strLastName;
Expand Down
14 changes: 6 additions & 8 deletions far2l/grabber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "clipboard.hpp"
#include "config.hpp"

Grabber::Grabber()
Grabber::Grabber() :
CMM(MACRO_OTHER)
{
Frame *pFrame = FrameManager->GetCurrentFrame();
pFrame->Lock();
SaveScr=new SaveScreen;
PrevMacroMode=CtrlObject->Macro.GetMode();
CtrlObject->Macro.SetMode(MACRO_OTHER);
memset(&GArea,0,sizeof(GArea));
memset(&PrevArea,0,sizeof(PrevArea));
bool Visible=false;
Expand Down Expand Up @@ -82,7 +81,6 @@ Grabber::Grabber()

Grabber::~Grabber()
{
CtrlObject->Macro.SetMode(PrevMacroMode);
}


Expand Down Expand Up @@ -265,10 +263,10 @@ void Grabber::DisplayObject()
int Grabber::ProcessKey(int Key)
{
/* $ 14.03.2001 SVS
[-] Íĺďđŕâčëüíî âîńďđîčçâîäčëń˙ ěŕęđîń â đĺćčěĺ ăđŕáëĺíč˙ ýęđŕíŕ.
Ďđč âîńďđîčçâĺäĺíčč ęëŕâčřŕ Home ďĺđĺěĺůŕëŕ ęóđńîđ â ęîîđäčíŕňű
0,0 ęîíńîëč.
Íĺ áűëî ó÷ňĺíî đĺćčěŕ âűďîëíĺíč˙ ěŕęđîńŕ.
[-] Неправильно воспроизводился макрос в режиме грабления экрана.
При воспроизведении клавиша Home перемещала курсор в координаты
0,0 консоли.
Не было учтено режима выполнения макроса.
*/
SetCursorType(TRUE,60);

Expand Down
3 changes: 2 additions & 1 deletion far2l/grabber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "modal.hpp"
#include "chgmmode.hpp"

struct GrabberArea
{
Expand All @@ -44,11 +45,11 @@ struct GrabberArea
class Grabber:Modal
{
private:
ChangeMacroMode CMM;
SaveScreen *SaveScr;
GrabberArea PrevArea;
GrabberArea GArea;
int ResetArea;
int PrevMacroMode;
int VerticalBlock;

private:
Expand Down
6 changes: 2 additions & 4 deletions far2l/help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,17 @@ static const wchar_t *HelpContents=L"Contents";
static int RunURL(const wchar_t *Protocol, wchar_t *URLPath);

Help::Help(const wchar_t *Topic, const wchar_t *Mask,DWORD Flags):
CMM(MACRO_HELP),
ErrorHelp(TRUE),
IsNewTopic(TRUE),
MouseDown(FALSE),
CurColor(COL_HELPTEXT),
CtrlTabSize(8),
PrevMacroMode(CtrlObject->Macro.GetMode())
CtrlTabSize(8)
{
CanLoseFocus=FALSE;
KeyBarVisible=TRUE;
/* $ OT По умолчанию все хелпы создаются статически*/
SetDynamicallyBorn(FALSE);
CtrlObject->Macro.SetMode(MACRO_HELP);
Stack=new CallBackStack;
StackData.Clear();
StackData.Flags=Flags;
Expand Down Expand Up @@ -180,7 +179,6 @@ Help::Help(const wchar_t *Topic, const wchar_t *Mask,DWORD Flags):

Help::~Help()
{
CtrlObject->Macro.SetMode(PrevMacroMode);
SetRestoreScreenMode(FALSE);

if (Stack) delete Stack;
Expand Down
4 changes: 2 additions & 2 deletions far2l/help.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "frame.hpp"
#include "keybar.hpp"
#include "array.hpp"
#include "chgmmode.hpp"

class CallBackStack;

Expand Down Expand Up @@ -120,6 +121,7 @@ class HelpRecord
class Help:public Frame
{
private:
ChangeMacroMode CMM;
BOOL ErrorHelp; // TRUE - ошибка! Например - нет такого топика
SaveScreen *TopScreen; // область сохранения под хелпом
KeyBar HelpKeyBar; // кейбар
Expand All @@ -141,8 +143,6 @@ class Help:public Frame
int CurColor; // CurColor - текущий цвет отрисовки
int CtrlTabSize; // CtrlTabSize - опция! размер табуляции

int PrevMacroMode; // предыдущий режим макроса

FARString strCurPluginContents; // помним PluginContents (для отображения в заголовке)

DWORD LastStartPos;
Expand Down
6 changes: 0 additions & 6 deletions far2l/plugapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1119,12 +1119,6 @@ static HANDLE FarDialogInitSynched(INT_PTR PluginNumber, int X1, int Y1, int X2,
if (!FarDialog)
return hDlg;

if (!FarDialog->InitOK())
{
delete FarDialog;
return hDlg;
}

hDlg = (HANDLE)FarDialog;
FarDialog->SetPosition(X1,Y1,X2,Y2);

Expand Down
6 changes: 2 additions & 4 deletions far2l/plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "keys.hpp"
#include "scantree.hpp"
#include "chgprior.hpp"
#include "chgmmode.hpp"
#include "constitle.hpp"
#include "cmdline.hpp"
#include "filepanels.hpp"
Expand Down Expand Up @@ -1357,8 +1358,7 @@ int PluginManager::CommandsMenu(int ModalType,int StartPos,const wchar_t *Histor
}

int MenuItemNumber=0;
int PrevMacroMode=CtrlObject->Macro.GetMode();
CtrlObject->Macro.SetMode(MACRO_MENU);
ChangeMacroMode CMM(MACRO_MENU);
int Editor = ModalType==MODALTYPE_EDITOR,
Viewer = ModalType==MODALTYPE_VIEWER,
Dialog = ModalType==MODALTYPE_DIALOG;
Expand Down Expand Up @@ -1534,7 +1534,6 @@ int PluginManager::CommandsMenu(int ModalType,int StartPos,const wchar_t *Histor

if (ExitCode<0)
{
CtrlObject->Macro.SetMode(PrevMacroMode);
return FALSE;
}

Expand Down Expand Up @@ -1585,7 +1584,6 @@ int PluginManager::CommandsMenu(int ModalType,int StartPos,const wchar_t *Histor
CurEditor->SetPluginTitle(nullptr);
}

CtrlObject->Macro.SetMode(PrevMacroMode);
return TRUE;
}

Expand Down

0 comments on commit 8c8a2db

Please sign in to comment.