Skip to content

Commit

Permalink
LuaFAR: allow another API for actl.Synchro
Browse files Browse the repository at this point in the history
  • Loading branch information
shmuz committed Nov 11, 2024
1 parent 1ac034e commit e512701
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 27 deletions.
32 changes: 25 additions & 7 deletions enc/enc_lua/luafar_manual.tsi
Original file line number Diff line number Diff line change
Expand Up @@ -1842,16 +1842,34 @@ lv=4
dt=Text
nm=ACTL_SYNCHRO
ctime=3936005255
mtime=3936009912
mtime=3940507744
<article>
#_Result = far.AdvControl (ACTL_SYNCHRO, Param1)
#_**Syntax 1**
#_**--------**
#_ Result = far.AdvControl (ACTL_SYNCHRO, Param1)
#_
#_**Parameters:**
#_ Param1: integer
#_ Param2: N/A
#_ **Parameters:**
#_ Param1: integer
#_ Param2: N/A
#_
#_**Returns:**
#_ Result: integer
#_ **Returns:**
#_ Result: integer
#_
#_
#_**Syntax 2**
#_**--------**
#_ Result = far.AdvControl (ACTL_SYNCHRO, Handler, ...)
#_
#_ **Parameters:**
#_ Handler: function
#_ ...: 0 or more Lua values of any type
#_
#_ **Returns:**
#_ Result: integer
#_
#_ **Description:**
#_ When Far Manager gets control it will call *Handler*
#_ passing it the extra arguments (if any)
#_
#_@@@
#_{actl_synchro}: http://api.farmanager.com/ru/service_functions/advcontrol.html#ACTL_SYNCHRO
Expand Down
2 changes: 1 addition & 1 deletion plugins/luamacro/_globalinfo.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function export.GetGlobalInfo()
return {
Version = { 3, 0, 0, 858 },
Version = { 3, 0, 0, 859 },
MinFarVersion = { 3, 0, 0, 6380 },
Guid = win.Uuid("4EBBEFC8-2084-4B7F-94C0-692CE136894D"),
Title = "LuaMacro",
Expand Down
4 changes: 4 additions & 0 deletions plugins/luamacro/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
shmuel 2024-11-11 17:26:22+02:00 - build 859

1. LuaFAR: allow another API for actl.Synchro

shmuel 2024-11-07 19:43:26+02:00 - build 858

1. LuaFAR: prevent a certain kind of crashes
Expand Down
20 changes: 16 additions & 4 deletions plugins/luamacro/luafar/exported.c
Original file line number Diff line number Diff line change
Expand Up @@ -1499,9 +1499,9 @@ intptr_t LF_ProcessSynchroEvent(lua_State* L, const struct ProcessSynchroEventIn
TSynchroData sd = *(TSynchroData*)Info->Param; // copy
free(Info->Param);

if (sd.regAction != 0)
if (sd.type & (SYNCHRO_TIMER_CALL | SYNCHRO_TIMER_UNREF))
{
if (!sd.timerData->needClose && (sd.regAction & LUAFAR_TIMER_CALL))
if (!sd.timerData->needClose && (sd.type & SYNCHRO_TIMER_CALL))
{
lua_rawgeti(L, LUA_REGISTRYINDEX, sd.timerData->tabRef); //+1: Table

Expand All @@ -1524,15 +1524,27 @@ intptr_t LF_ProcessSynchroEvent(lua_State* L, const struct ProcessSynchroEventIn
else lua_pop(L, 1);
}

if (sd.regAction & LUAFAR_TIMER_UNREF)
if (sd.type & SYNCHRO_TIMER_UNREF)
{
luaL_unref(L, LUA_REGISTRYINDEX, sd.timerData->tabRef);
}
}
else
else if (sd.type == SYNCHRO_COMMON)
{
Common_ProcessSynchroEvent(L, Info->Event, sd.data);
}
else if (sd.type == SYNCHRO_FUNCTION)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, sd.ref);
luaL_unref(L, LUA_REGISTRYINDEX, sd.ref);
if (lua_istable(L,-1) && lua_checkstack(L, sd.narg)) {
for (int i=1; i <= sd.narg; i++) {
lua_rawgeti(L, -i, i);
}
pcall_msg(L, sd.narg - 1, 0);
}
lua_pop(L, 1);
}
}
else if (Info->Event == SE_FOLDERCHANGED)
{
Expand Down
39 changes: 28 additions & 11 deletions plugins/luamacro/luafar/service.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,13 @@ void DeleteLuaStateTimerQueue(lua_State *L)
lua_setfield(L, LUA_REGISTRYINDEX, FarTimerQueueKey);
}

static TSynchroData* CreateSynchroData(TTimerData *td, int action, int data)
static TSynchroData* CreateSynchroData(int type, int data, TTimerData *td)
{
TSynchroData* SD = (TSynchroData*) malloc(sizeof(TSynchroData));
SD->timerData = td;
SD->regAction = action;
SD->type = type;
SD->data = data;
SD->ref = LUA_REFNIL;
SD->timerData = td;
return SD;
}

Expand Down Expand Up @@ -4812,7 +4813,9 @@ static int DoAdvControl (lua_State *L, int Command, int Delta)
PSInfo *Info = pd->Info;
intptr_t Param1 = 0;
void *Param2 = NULL;
lua_settop(L,pos3); /* for proper calling GetOptIntFromTable and the like */

if (Command != ACTL_SYNCHRO)
lua_settop(L,pos3); /* for proper calling GetOptIntFromTable and the like */

if (Delta == 0)
Command = CAST(int, check_env_flag(L, 1));
Expand Down Expand Up @@ -4866,11 +4869,25 @@ static int DoAdvControl (lua_State *L, int Command, int Delta)
}

case ACTL_SYNCHRO:
{
intptr_t p = luaL_checkinteger(L, pos2);
Param2 = CreateSynchroData(NULL, 0, (int)p);
break;
}
if (lua_isfunction(L, pos2)) {
TSynchroData *sd = CreateSynchroData(SYNCHRO_FUNCTION, 0, NULL);
int top = lua_gettop(L);
sd->narg = top - pos2 + 1;
lua_newtable(L);
for (int i=pos2,j=1; i <= top; ) {
lua_pushvalue(L, i++);
lua_rawseti(L, -2, j++);
}
sd->ref = luaL_ref(L, LUA_REGISTRYINDEX);
lua_pushinteger(L, Info->AdvControl(PluginId, Command, 0, sd));
return 1;
}
else {
luaL_argcheck(L, lua_isnumber(L,pos2), pos2, "integer or function expected");
TSynchroData *sd = CreateSynchroData(SYNCHRO_COMMON, lua_tointeger(L,pos2), NULL);
lua_pushinteger(L, Info->AdvControl(PluginId, Command, 0, sd));
return 1;
}

case ACTL_SETPROGRESSSTATE:
Param1 = (intptr_t) check_env_flag(L, pos2);
Expand Down Expand Up @@ -5748,7 +5765,7 @@ void CALLBACK TimerCallback(void *lpParameter, BOOLEAN TimerOrWaitFired)
(void)TimerOrWaitFired;
if (!td->needClose && td->enabled)
{
sd = CreateSynchroData(td, LUAFAR_TIMER_CALL, 0);
sd = CreateSynchroData(SYNCHRO_TIMER_CALL, 0, td);
td->Info->AdvControl(td->PluginGuid, ACTL_SYNCHRO, 0, sd);
}
}
Expand Down Expand Up @@ -5824,7 +5841,7 @@ static int timer_Close(lua_State *L)
hQueue = GetLuaStateTimerQueue(L);
if (hQueue)
DeleteTimerQueueTimer(hQueue, td->hTimer, NULL);
sd = CreateSynchroData(td, LUAFAR_TIMER_UNREF, 0);
sd = CreateSynchroData(SYNCHRO_TIMER_UNREF, 0, td);
td->Info->AdvControl(td->PluginGuid, ACTL_SYNCHRO, 0, sd);
}
return 0;
Expand Down
10 changes: 7 additions & 3 deletions plugins/luamacro/luafar/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ _CRTIMP unsigned int __cdecl __MINGW_NOTHROW _control87 (unsigned int unNew, uns
/* convert a stack index to positive */
#define abs_index(L,i) ((i)>0 || (i)<=LUA_REGISTRYINDEX ? (i):lua_gettop(L)+(i)+1)

#define LUAFAR_TIMER_CALL 0x1
#define LUAFAR_TIMER_UNREF 0x2
#define SYNCHRO_COMMON 0x0
#define SYNCHRO_TIMER_CALL 0x1
#define SYNCHRO_TIMER_UNREF 0x2
#define SYNCHRO_FUNCTION 0x4

typedef struct
{
Expand All @@ -45,8 +47,10 @@ typedef struct
typedef struct
{
TTimerData *timerData;
int regAction;
int type;
int data;
int ref;
int narg;
} TSynchroData;

typedef struct
Expand Down
2 changes: 1 addition & 1 deletion plugins/luamacro/luafar/version.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#include <farversion.hpp>

#define PLUGIN_BUILD 858
#define PLUGIN_BUILD 859

1 comment on commit e512701

@shmuz
Copy link
Contributor Author

@shmuz shmuz commented on e512701 Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See also: shmuz/far2m#78

Please sign in to comment.