From f554216201fc8620248aa93eab43d65ba8d9b14d Mon Sep 17 00:00:00 2001 From: JianjunLiu Date: Thu, 24 Nov 2022 14:18:47 +0800 Subject: [PATCH] c++ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 淇敼c浠g爜 --- tsluic/TSLUIL.cpp | 2373 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 2373 insertions(+) create mode 100644 tsluic/TSLUIL.cpp diff --git a/tsluic/TSLUIL.cpp b/tsluic/TSLUIL.cpp new file mode 100644 index 0000000..8b21e55 --- /dev/null +++ b/tsluic/TSLUIL.cpp @@ -0,0 +1,2373 @@ +/* +TSLUIL.cpp : 定义 DLL 应用程序的导出函数。 +20161220 修改 窗口构建 的逻辑 去掉 样式默认值 + 窗口添加 getpc 方法 参数 1: 窗口 ;参数2 : 整数 1 设置父窗口,2 设置子窗口 + 添加 visible 方法获得显示属性 +20170103 窗口添加 updatewindow 方法--updatewindow +20170105 添加 clipcusor 固定鼠标到指定区域 + 添加clienttoscreen 计算窗口中的点对于屏幕位置 +20170108 修正消息参数lparam ,wparam 传递bug + 删除废弃的代码 + 为避免冲突修改成员函数名称 create 为construct +20170109 添加调试错误退出,添加 windowlong 方法 +20170112 在construct 函数中判断是否已经初始,如果重复初始化返回错误; +20170113 增加getscrollinfo setscrollinfo方法 +20170318 删删除多余代码,保留结构体转换代码 +20170322 整理代码,添加注释,回调错误写入log文件 +20170328 将主循环写入tsl +20170410 修改添加tslcstructrueobj_的 construct方法,用于构造指针引用不比拷贝内存数据 +20170502 添加 _getvalueaddr_ _getvalueaddr2_ 两个方法分别获得结构体对应索引的地址,和对应索引作为指针指向的地址 +20170629 添加float类型到转换结构体 +20170828 将c代码改写为tsl c中只提供基础的内存操作 和消息分发 +20200228 添加多线程功能,并添加线程读写锁 +20200302 修改TSL_InterpErrorInfo 线程相关 +20220418 整理代windows的代码 +20220419 整理gtk代码 + +*/ +//typedef void* (*TTSL_InterpPrepare)(TSL_State* L); +// 依赖头文件 +#include "stdafx.h" + + +#ifdef __LINUX_GTK__ +//gtk linker +//libTSSVRAPI.so libTSLInterp.so libTSLPlugin.so +//`pkg-config --libs gtk + -3.0 ` +//输出所有 __attribute__((visibility("default"))) 的函数 +#include +#include +#include +#include +#include +#include +#include +#include +extern "C" { +#include + +} + +#else + +//tssvrapi.lib +//TSLInterp.lib +//TSLKRNL.lib +//TSLPLUGIN.lib +//comctl32.lib +//gdiplus.lib + +//输出函数 +//PKG_Init + +#include +#include +#include +#include +#include +#endif + + +#include +#include +static std::recursive_mutex strmmutex; //线程锁 +typedef unsigned char byte; + +#ifdef __LINUX_GTK__ + +//typedef unsigned int size_t; +typedef short WORD; +typedef short SHORT; +typedef unsigned long int __int64; +//typedef struct HANDLE; +//只有64位 +typedef __int64 INT_PTR; +typedef int BOOL; +typedef long LONG; +typedef byte BYTE; +typedef unsigned int DWORD; +typedef float FLOAT; + + +#endif + + +extern "C" +{ +#include "TSSVRAPI.h" +} +typedef TObject* PTObject; //天软对象 +typedef const TObject * CPTObject;//const 天软对象 + +#ifdef __LINUX_GTK__ +//gtk输出函数 +extern "C" { + __attribute__((visibility("default"))) int PKG_Init(TSL_State* L, OpenLibProc proc); + __attribute__((visibility("default"))) int tsl_gtk_pipread(INT_PTR hr, char * msg, int ct); + __attribute__((visibility("default"))) int tsl_gtk_createprocessa(char * name, char * argv[], char* envp[], INT_PTR & pw); +} +//非external输出函数 +extern "C" { + + //GetGtkEventNameOrId + __attribute__((visibility("hidden"))) INT_PTR gettslvcleventhandler(const char * fname); + __attribute__((visibility("hidden"))) int deal_time(void * p); + __attribute__((visibility("hidden"))) INT_PTR getnameid(const char * b); + __attribute__((visibility("hidden"))) gint dodeleteevents(void * a, const char * b); + __attribute__((visibility("hidden"))) gint dotparamseevents(void * a, const char * b, void * p1, void * p2); + __attribute__((visibility("hidden"))) gint tsl_gtk_destroy_event(void * a, void * b, void* c); + + __attribute__((visibility("hidden"))) gint tsl_gtk_delete_event(void * a, void * b); + __attribute__((visibility("hidden"))) gint tsl_gtk_wmuser_event(void * a, void * p1, void*p2); + __attribute__((visibility("hidden"))) gint tsl_gtk_adj_value_changed_event(void * a, void * b); + __attribute__((visibility("hidden"))) gint tsl_gtk_clicked_event(void * a, void * b); + __attribute__((visibility("hidden"))) gint tsl_gtk_activate_event(void * a, void * b); + __attribute__((visibility("hidden"))) gint tsl_gtk_changed_event(void * a, void * b); + __attribute__((visibility("hidden"))) gint tsl_gtk_preedit_changed_event(void * a, void * b, void * c); + __attribute__((visibility("hidden"))) gint tsl_gtk_draw_event(void * a, void * b, void * c); + __attribute__((visibility("hidden"))) gint tsl_gtk_entry_insert_text(void * a, void * b, int c, int*p, void * gd); + __attribute__((visibility("hidden"))) gint tsl_gtk_normal_event_bc(void * a, void * b, void * c); + __attribute__((visibility("hidden"))) gint tsl_gtk_normal_event_cb(void * a, void * b, void* c); + __attribute__((visibility("hidden"))) gint tsl_gtk_day_select_event(void * a, void * b); + + gboolean tsl_gtk_idl_do(void * dlg); + + gpointer * WorkerThreadCall(void *p); + INT_PTR createathread(void *s) + { + if (!g_thread_supported()) g_thread_init(NULL); + gdk_threads_init(); + GThread* r = g_thread_create(WorkerThreadCall, s, FALSE, NULL); + return r; + } +#define _stricmp strcmp +} + + +#else + +void WorkerThreadCall(void *p); +INT_PTR createathread(void *s) +{ + return _beginthread(WorkerThreadCall, 4068000, s); //4068 +} + +#endif + +char * LibPath=0; //tsl库目录 +thread_local TSL_InterpErrorInfo * TslExe_error = TSL_InterpNewErrorInfo(); //tsl获得执行信息结构体 +thread_local TSL_State* GlobalL1; //界面线程的状态变量 +thread_local TObject* callfunctiondata=0; //远程执行回调 +std::map ThreadScripts; //多线程执行脚本缓存 +std::map ThreadMessageDatas; //多线程数据缓存 +//界面库名 +#define TSLMODELNAME "tslvcl" +//gtk接口库名 +#define TSLGTKMODALNAME "ugtkinterface" +#define AEFCLASS "aefclassobj_" + +//void * gettslbase(TSL_State * L, TObject * FA, const char * name); + +//获得c基础类型的内存大小 +//获取指针的长度 +int getctypesize(TSL_State* L, TObject* iArgs, int iNum, TObject* Value) +{ + //LRESULT + //MAKEINTRESOURCE + //GlobalL1 = L; + //PWNDCLASSEXA + //TSL_SetIntPtr(L, Value, sizeof(INT_PTR)); + TSL_SetType(L, Value, TSL_TTABLE); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "int"), sizeof(int)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "uint"), sizeof(unsigned int)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "char*"), sizeof(char*)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "intptr"), sizeof(INT_PTR)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "SHORT"), sizeof(SHORT)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "BOOL"), sizeof(BOOL)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "char"), sizeof(char)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "double"), sizeof(double)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "short"), sizeof(short)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "ushort"), sizeof(unsigned short)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "long"), sizeof(long)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "ulong"), sizeof(unsigned long)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "LONG"), sizeof(LONG)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "bool"), sizeof(bool)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "byte"), sizeof(byte)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "BYTE"), sizeof(BYTE)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "WORD"), sizeof(WORD)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "DWORD"), sizeof(DWORD)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "word"), sizeof(WORD)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "dword"), sizeof(DWORD)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "float"), sizeof(float)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "FLOAT"), sizeof(FLOAT)); + TSL_SetInt(L, TSL_HashSetItemSZString(L, Value, "pointer"), sizeof(INT_PTR)); + return 1; +} + +//tsl对象类 +struct tarmyTSLOBJ +{ + PTObject tslobj; + TSL_State *ML; + tarmyTSLOBJ(TSL_State *L) + { + tslobj = TSL_NewObject(); + ML = L; + } + ~tarmyTSLOBJ() + { + if (tslobj) + { + TSL_FreeObjectContent(ML, tslobj); + TSL_FreeObj(ML, tslobj); + tslobj = NULL; + } + + } + +}; + +//自动销毁的tsl对象 +#define AUTOTSLOBJ(tobj,L) \ + tarmyTSLOBJ (tobj##obj) = tarmyTSLOBJ(L) ;\ + PTObject tobj = (tobj##obj).tslobj \ + + + +/////////代码块定义//////////////// +#define Block_TypeClass 1 +#define Block_Function 2 +#define Block_Statements 4 +#define Block_If 8 +#define Block_Else 16 +#define Block_SubCase 32 +#define Block_Goto_Label 64 +#define Block_Empty_Begin_End 128 +#define Block_Try 256 +#define Block_NeedSql 512 +#define Block_UnitStruct 1024 +#define Block_Propertys 2048 +#define Block_Fields 4096 + +struct BlockForEditor { + int mType; + int mBeg; + int mEnd; + int mSubs; + bool mFolded; + BlockForEditor** mSub; + void Clear(); +}; + +////////////////////////代码块解析//////导入函数//////////////////////////////// +extern "C" { + void tslL_getfunctions(char*** functions, int* count); + bool TSL_TokenizeEx2(char* source, bool skiperror, char*** sWords, char*** sUnits, char*** sClasses, char**** sFunctions, int** iLines, int** iBeginEnds, char*** Dependency, BlockForEditor*** retBlocks, int* retBlockCount, int Flags, TSL_InterpErrorInfo* oResult); + char * TSL_GetLibPath(TSL_State *); + char * TSL_SetLibPath(TSL_State *, char*); + void* TSL_InterpPrepare(TSL_State* L); + void TSL_ClearError(); +} +void SetStringsToTObject(TSL_State* L,TObject*obj, char**words) +{ + TSL_SetType(L, obj, TSL_TTABLE); + int idx = 0; + char * wd; + while (true) + { + wd = words[idx]; + if (wd == 0) { return; } + TSL_SetString(L, TSL_HashSetItemInt(L, obj, idx), wd); + + idx++; + } +} +void SetIntsToTOjbect(TSL_State* L, TObject*obj, int*words,int n) +{ + TSL_SetType(L, obj, TSL_TTABLE); + + for (int idx=0; idxmType); + TSL_SetInt(L, TSL_HashSetItemSZString(L, BLKI, "mbeg"), blki->mBeg); + TSL_SetInt(L, TSL_HashSetItemSZString(L, BLKI, "mend"), blki->mEnd); + TSL_SetInt(L, TSL_HashSetItemSZString(L, BLKI, "msubs"), blki->mSubs); + if (blki->mSubs > 0) + { + SetBlkToTObject(L, TSL_HashSetItemSZString(L, BLKI, "msub"), blks[i]->mSub, blki->mSubs); + } + + } +} +void freechar2(char** words) +{ + if (words) + { + //char** twd = words; + int idx = 0; + char * wd = 0; + while (true) + { + wd = words[idx]; + if (wd == 0) { break; } + TSL_Free(wd); + idx++; + } + TSL_Free(words); + } +} +void freechar3(char*** words) +{ + if (words) + { + int idx = 0; + char ** wd = 0; + while (true) + { + wd = words[idx]; + if (wd == 0) { break; } + freechar2(wd); + idx++; + } + TSL_Free(words); + } + +} +void freeblicks(BlockForEditor** blks,int ct) +{ + for (int i = 0; i < ct; i++) + { + freeblicks(blks[i]->mSub, blks[i]->mSubs); + TSL_Free(blks[i]); + } +} +int TSL_TokenizeEx_2_(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + int flg = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256 + 512 + 1024; + if (iNum < 1) { return false; } + if (iNum = 2) { + if (TSL_AsInt(TSL_GetParamRef(L, 1)) >= 0) + { + flg = TSL_AsInt(TSL_GetParamRef(L, 1)); + } + } + char* source =(char*) TSL_AsString(TSL_GetParamRef(L, 0)); + if (source == 0) { return 0; } + static TSL_InterpErrorInfo * errResult = TSL_InterpNewErrorInfo(); + char** sword = 0; + char** sunits = 0; + char** sclass = 0; + char *** sfunctions = 0; + int *ilines = 0; + int *ibes = 0; + char ** dep = 0; + BlockForEditor **blks = 0; + int blkct = 0; + + TSL_TokenizeEx2(source, true, &sword, &sunits, &sclass, &sfunctions, &ilines, &ibes, &dep, &blks, &blkct,flg , errResult); + //返回结果处理 + TSL_SetType(L, oResult, TSL_TTABLE); + TObject *rwords = 0; + TObject *rwords2 = 0; + if (sword) { + rwords = TSL_HashSetItemSZString(L, oResult, "words"); + SetStringsToTObject(L, rwords, sword); + freechar2(sword); + } + + if (dep) { + rwords = TSL_HashSetItemSZString(L, oResult, "dep"); + SetStringsToTObject(L, rwords, dep); + freechar2(dep); + } + + if (sunits) + { + rwords = TSL_HashSetItemSZString(L, oResult, "units"); + SetStringsToTObject(L, rwords, sunits); + freechar2(sunits); + } + + if (sclass) + { + rwords = TSL_HashSetItemSZString(L, oResult, "class"); + SetStringsToTObject(L, rwords, sclass); + freechar2(sclass); + } + int fcount = 0; + if (sfunctions) + { + char ** fs; + rwords = TSL_HashSetItemSZString(L, oResult, "functions"); + TSL_SetType(L, rwords, TSL_TTABLE); + int idx = 0; + while (true) { + + fs = sfunctions[idx]; + if (fs == 0) { break; } + rwords2 = TSL_HashSetItemInt(L, rwords, idx); + SetStringsToTObject(L, rwords2, fs); + idx++; + + } + fcount = idx; + freechar3(sfunctions); + + } + if (ilines) + { + rwords = TSL_HashSetItemSZString(L, oResult, "lines"); + SetIntsToTOjbect(L, rwords, ilines, fcount); + TSL_Free(ilines); + } + if (ibes) + { + rwords = TSL_HashSetItemSZString(L, oResult, "linebegend"); + int idx = 0; + TSL_SetType(L, rwords, TSL_TTABLE); + while (ibes[idx] != -1 || ibes[idx + 1] != -1) + { + TSL_SetInt(L, TSL_HashSetItemInt(L, rwords, idx), ibes[idx]); + idx ++; + } + //SetIntsToTOjbect(L, rwords, ibes, fcount *2); + TSL_Free(ibes); + + } + if (blkct > 0) + { + rwords = TSL_HashSetItemSZString(L, oResult, "blcks"); + SetBlkToTObject(L, rwords, blks, blkct); + freeblicks(blks, blkct); + } + + //TSL_HashGet(L,oResult,) + //TSL_SetInt(L, TSL_HashSetItemSZString(L, oResult, "int"), sizeof(int)); + + return 1; +} +int tslL_getfunctions_2_(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + char **funs = 0; + int cnt = 0; + tslL_getfunctions(&funs, &cnt); + TSL_SetType(L, oResult, TSL_TTABLE); + char *f = 0; + for (int i = 0; i < cnt;i++) + { + f = funs[i]; + TSL_SetString(L, TSL_HashSetItemInt(L, oResult, i),f); + //TSL_Free(f); + + } + return true; +} + + +//////////////执行tsl////////////////////////////////////////////// + +void rawcallerrdo(TSL_State* L) //错误时候的处理 +{ + //printf(" \r\n catch err id:%d\r\n",ee); + //错误信息打印 + TSL_InterpGetLastError(TslExe_error); + WriteToLog(TslExe_error->mErrMsg); + printf(TslExe_error->mErrMsg); + //fflush(stdout); + TSL_ClearError(); + //TSL_InterpSetRuntimeStop(L, 0); + exit(1); +} +int calltslfunction_p_int(TSL_State* L, const char* name, INT_PTR* argsin, int ct, void * result) +{ + + AUTOTSLOBJ(fn, L); + TSL_SetString(L, fn, name); + + //AUTOTSLOBJ(args, L); + //TSL_SetType(L, args, TSL_TTABLE); + TObject *args = 0; + if (ct > 0) + { + args = TSL_NewObjects(ct); + for (int i = 0; i < ct; i++) + { + //TSL_SetIntPtr(L, TSL_HashSetItemInt(L, args, i), argsin[i]); + TSL_SetIntPtr(L, TSL_ReadTObjectFromPtr(args, i), argsin[i]); + } + } + int r = 0; + try + { + r = raw_call2(L, fn, args, ct, result); + } + catch (...) + { + rawcallerrdo(L); + //TSL_ClearError(); + } + if (ct > 0) + { + TSL_FreeObjs(L, args, ct); + } + + return r; +} + + +int TSL_ScriptGo_A(TSL_State* L, const char* Content, TObject* v) +{ + return TSL_ScriptGo(L, Content, v); +} +int TSL_ScriptGo_B(TSL_State* L, const char* Content, TObject* v, const char* t = 0) +{ + int r = TSL_ScriptGo_A(L, Content, v); + if (r == TSL_ERROR) + { + //错误信息打印 + TSL_InterpGetLastError(TslExe_error); + if (t) + { + WriteToLog(t); + } + WriteToLog("执行:"); + WriteToLog(Content); + WriteToLog("\r\n错误信息:"); + WriteToLog(TslExe_error->mErrMsg); + TSL_ClearError(); + } + return r; +} + + + +//获取执行脚本 +char * FeachThreadScripts(INT_PTR id) +{ + std::lock_guard g(strmmutex); + char * r = ThreadScripts[id]; + if (r) + { + ThreadScripts.erase(id); + } + return r; +} +//获取消息对象 +int FeachThreadMessagedata(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + if (iNum = 1) + { + TObject * ido = TSL_GetParamRef(L, 0); + INT_PTR id = TSL_AsIntPtr(ido); + if (id) + { + std::lock_guard g(strmmutex); + void * r = ThreadMessageDatas[id]; + if (r) + { + ThreadMessageDatas.erase(id); + TSL_StrmToObj(L, oResult, r); + TSL_DelStrm(r); + } + } + } + + return 1; +} + +//保存执行脚本 +int SetDataToThreadScripts(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + if (iNum==1) { + TObject * ExecStr = TSL_GetParamRef(L, 0);//TSL_ReadTObjectFromPtr(iArgs, 0);// + char* d = TSL_Strdup(TSL_AsString(ExecStr)); + if (d) + { + std::lock_guard g(strmmutex); + INT_PTR id = (INT_PTR)d; + ThreadScripts[id] = d; + if (0 == LibPath) { + LibPath = TSL_Strdup(TSL_GetLibPath(L)); + } + TSL_SetIntPtr(L, oResult, id); + + }else + TSL_SetInt(L, oResult, 0); + }else + TSL_SetInt(L, oResult, 0); + return true; + +} +//保存消息对象 +int SetDataToMessages(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + if (iNum == 1) { + TObject * ExecStr = TSL_GetParamRef(L, 0);//TSL_ReadTObjectFromPtr(iArgs, 0);// + void* d = TSL_ObjToStrm(L,ExecStr); + if (d) + { + std::lock_guard g(strmmutex); + INT_PTR id = (INT_PTR)d; + ThreadMessageDatas[id] = d; + TSL_SetIntPtr(L, oResult,id); + } + else + TSL_SetInt(L, oResult, 0); + }else + TSL_SetInt(L, oResult, 0); + return true; +} + + +class TWorkerMessageItem +{ +public: + INT_PTR FHandle; + INT_PTR FMsg; + INT_PTR FData; + TWorkerMessageItem * Next; + TWorkerMessageItem(INT_PTR h = 0, INT_PTR m = 0, INT_PTR d = 0) + { + FHandle = h; + FMsg = m; + FData = d; + Next = 0; + } +}; + +class TWorkerQueue { +public: + TWorkerMessageItem * FFirst; + TWorkerMessageItem * FLast; + int FItemCount; + TWorkerQueue() + { + FFirst = new TWorkerMessageItem(); + FLast = FFirst; + FItemCount = 0; + } + INT_PTR beginthread(void * id) + { + //GThread* r = g_thread_create(WorkerThreadCall, s, FALSE, NULL); + return createathread(id); + } + void Add(INT_PTR h = 0, INT_PTR m = 0, INT_PTR d = 0) + { + //printf("\r\nmessgequeue count:%d", FItemCount); + FLast->Next = new TWorkerMessageItem(h, m, d); + FLast = FLast->Next; + + FItemCount++; + + } + void DeleteHandle(INT_PTR h) + { + TWorkerMessageItem *r = Feach(h); + while (r != 0) + { + delete r; + r = Feach(h); + } + } + TWorkerMessageItem * Feach(INT_PTR h) + { + TWorkerMessageItem *pit, *it; + TWorkerMessageItem * r = 0; + pit = FFirst; + while (pit) + { + it = pit->Next; + if (it && (h == it->FHandle)) + { + r = it; + pit->Next = it->Next; + if (it->Next == 0) + { + FLast = pit; + } + + FItemCount--; + break; + } + pit = it; + } + return r; + } +}; +TWorkerQueue GWOKERQUEUE; + +int BeginThreadWorker(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + if (iNum = 1) + { + std::lock_guard g(strmmutex); + char * d = TSL_DupString( TSL_AsString(TSL_GetParamRef(L, 0))); + INT_PTR id = (INT_PTR)d; + ThreadScripts[id] = d; + if (0 == LibPath) { + LibPath = TSL_Strdup(TSL_GetLibPath(L)); + } + GWOKERQUEUE.beginthread(d ); + TSL_SetInt(L, oResult, 1); + } + else + { + TSL_SetInt(L, oResult, 0); + } + return 1; +} +int DeletehreadWorkerData(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + if (iNum = 1) + { + std::lock_guard g(strmmutex); + INT_PTR h = TSL_AsIntPtr(TSL_GetParamRef(L, 0)); + GWOKERQUEUE.DeleteHandle(h); + TSL_SetInt(L, oResult, 1); + } + else + { + TSL_SetInt(L, oResult, 0); + } + return 1; +} + +int FeachThreadWorkerData(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + if (iNum = 3) + { + TObject * ido = TSL_GetParamRef(L, 0); + INT_PTR h = TSL_AsIntPtr(ido); + std::lock_guard g(strmmutex); + TWorkerMessageItem *r = GWOKERQUEUE.Feach(h); + if (r != 0) + { + TSL_SetInt(L, TSL_GetParamRef(L, 1), r->FMsg); + TSL_StrmToObj(L, TSL_GetParamRef(L, 2),(void *) r->FData); + + TSL_SetInt(L, oResult, 1); + TSL_DelStrm((void *)r->FData); + delete r; + } + else + { + TSL_SetInt(L, oResult, 0); + } + + + } + else + { + TSL_SetInt(L, oResult, 0); + } + return 1; +} +int SetThreadWorkerData(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + if (iNum = 3) + { + std::lock_guard g(strmmutex); + INT_PTR h = TSL_AsIntPtr(TSL_GetParamRef(L, 0)); + INT_PTR msg = TSL_AsIntPtr(TSL_GetParamRef(L, 1)); + void* d = TSL_ObjToStrm(L, TSL_GetParamRef(L, 2)); + GWOKERQUEUE.Add(h, msg,(INT_PTR) d); + TSL_SetInt(L, oResult, 1); + } + else + { + TSL_SetInt(L, oResult, 0); + } + return 1; +} + + +//远程执行回调 +void callback(TSL_State* L, int RecvType, int ChannelId, int ErrNo, char* ErrMsg, TObject* ResultObj, TObject* EnvObj) +{ + if (callfunctiondata == 0) + { + callfunctiondata = TSL_NewObject(); + TSL_SetType(L, callfunctiondata, TSL_TTABLE); + } + TObject* callbackobj = TSL_HashSetItemInt(L, callfunctiondata, ChannelId); + TSL_SetType(L, callbackobj, TSL_TTABLE); + TSL_SetInt(L, TSL_HashSetItemSZString(L, callbackobj, "RecvType"), RecvType); + TSL_SetInt(L, TSL_HashSetItemSZString(L, callbackobj, "ChannelId"), ChannelId); + TSL_SetInt(L, TSL_HashSetItemSZString(L, callbackobj, "ErrNo"), ErrNo); + if(ErrNo>0) TSL_SetStringEx(L, TSL_HashSetItemSZString(L, callbackobj, "ErrMsg"), ErrMsg, ErrNo); + else TSL_SetString(L, TSL_HashSetItemSZString(L, callbackobj, "ErrMsg"), ErrMsg); + if (ResultObj) { + TObject* o1 = TSL_HashSetItemSZString(L, callbackobj, "ResultObj"); + TSL_DupObject(L, o1, ResultObj, true); + } + if (EnvObj) { + TObject* o2 = TSL_HashSetItemSZString(L, callbackobj, "EnvObj"); + TSL_DupObject(L, o2, EnvObj, true); + } + AUTOTSLOBJ(tslgoretv, L); + INT_PTR pms[1]; + pms[0] = (INT_PTR)ChannelId; + const char * ss = TSLMODELNAME "._executecallback_"; + calltslfunction_p_int(L, ss, pms, 1, tslgoretv); +} +//远程执行finaly回调 +void finalcallback(TSL_State* L, int ChannelId) +{ + if (callfunctiondata == 0) + { + callfunctiondata = TSL_NewObject(); + TSL_SetType(L, callfunctiondata, TSL_TTABLE); + } + TSL_SetInt(L, TSL_HashSetItemInt(L, callfunctiondata, ChannelId), 0); + const char * ss = TSLMODELNAME "._finalcallback_"; + //TS_EndExecute(ChannelId); + AUTOTSLOBJ(tslgoretv, L); + INT_PTR pms[1]; + pms[0] = (INT_PTR)ChannelId; + calltslfunction_p_int(L, ss, pms, 1, tslgoretv); +} +//远程执行 +int TS_SendExecuteAndWaitExcall(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ //获得消息处理函数的指针 + GlobalL1 = L; + int ChannelId = 0; + if (iNum >=6) + { + //获得系统消息函数处理的指针 + TObject * ExecStr = TSL_GetParamRef(L, 0);//TSL_ReadTObjectFromPtr(iArgs, 0);// + TObject * SysParam = TSL_GetParamRef(L, 1);//TSL_ReadTObjectFromPtr(iArgs, 0);// + TObject * oResult1 = TSL_GetParamRef(L, 2);//TSL_ReadTObjectFromPtr(iArgs, 0);// + TObject * ExecResult = TSL_GetParamRef(L, 3);//TSL_ReadTObjectFromPtr(iArgs, 0);// + TObject * ErrObj = TSL_GetParamRef(L, 4);//TSL_ReadTObjectFromPtr(iArgs, 0);// + TObject * Online = TSL_GetParamRef(L, 5);//TSL_ReadTObjectFromPtr(iArgs, 0);// + ChannelId = TS_SendExecuteAndWaitEx(L,TSL_AsString(ExecStr), + SysParam, oResult1, ExecResult, ErrObj, TSL_AsInt( Online), + callback, finalcallback); + } + TSL_SetInt(L, oResult, ChannelId); + return 1; +} + + +//内存管理辅助类 +void * aef_new(TSL_State* L, TObject* iArgs, int iNum) +{ + GlobalL1 = L; + return (void *)1; + //return new int; + //return new TSLCSTRUC(); +} +//c结构体析构 +void aef_free(TSL_State* L, void* data) +{ + //if (!data) return; + //delete (int*)data; +} +//c struct 结构体处理函数 +bool aef_method(TSL_State* L, TObject* obj, char* Name, TObject** Params, int ParamCount, TObject* Value, BOOL isSet) +{ + PTObject tptr = (TObject*)TSL_ReadINTPtrFromPtr(Params, 0); //指针 + PTObject tval = (TObject*)TSL_ReadINTPtrFromPtr(Params, 1); //长度或者值 + PTObject tvals = (TObject*)TSL_ReadINTPtrFromPtr(Params, 2); //数组 + if (!TSL_NumberCheck(tptr)) return 0; + INT_PTR ptr = TSL_AsIntPtr(tptr); //指针 + if (ptr == 0) return 0; + if (_stricmp(Name, "getstderr1") == 0) //获取结构体数据 + { + TSL_SetIntPtr(L, Value, (INT_PTR)stderr); + } + else + if (_stricmp(Name, "getstdout1") == 0) //获取结构体数据 + { + TSL_SetIntPtr(L, Value, (INT_PTR)stdout); + }else + if (_stricmp(Name, "getstdin1") == 0) //获取结构体数据 + { + TSL_SetIntPtr(L, Value, (INT_PTR)stdin); + + }else + if (_stricmp(Name, "tmalloc") == 0) //获取结构体数据 + { + //TSL_SetIntPtr(L, Value, (INT_PTR)malloc(ptr)); + TSL_SetIntPtr(L, Value,( INT_PTR)TSL_Malloc(ptr)); + } + else if (_stricmp(Name, "trealloc") == 0) + { + int len = TSL_AsInt(tval); + if (len < 1) return 0; + INT_PTR t = (INT_PTR)TSL_Realloc2((void*)ptr, len); + //INT_PTR t = (INT_PTR)realloc((void*)ptr, len); + TSL_SetIntPtr(L,Value,t); + } + else if(_stricmp(Name, "tfree") == 0) + { + //free((void*)ptr); + TSL_Free((void*)ptr); + } + else if (_stricmp(Name, "tmcopy") == 0) + { + //memccpy() + void * ret =memcpy((void*)ptr, (void*)TSL_AsIntPtr(tval), TSL_AsInt(tvals)); + TSL_SetIntPtr(L, Value, (INT_PTR)ret); + } + else if (_stricmp(Name, "tmset") == 0) + { + int n = TSL_AsInt(tval); + + memset((void*)ptr, 0, n); + } + else if (_stricmp(Name, "readbyte") == 0) + { + byte * v = (byte*)ptr; + TSL_SetInt(L, Value, *v); + } + else if (_stricmp(Name, "writebyte") == 0) + { + byte *v = (byte*)ptr; + *v = TSL_AsInt(tval); + } + else if (_stricmp(Name, "readint") == 0) + { + int * v = (int*)ptr; + TSL_SetInt(L, Value, *v); + } + else if (_stricmp(Name, "readuint") == 0) + { + unsigned int * v = (unsigned int*)ptr; + TSL_SetInt(L, Value, *v); + } + else if (_stricmp(Name, "writeint") == 0) + { + int *v = (int*)ptr; + *v = TSL_AsInt(tval); + } + else if (_stricmp(Name, "writeuint") == 0) + { + unsigned int *v = (unsigned int*)ptr; + *v = TSL_AsInt(tval); + } + else if (_stricmp(Name, "readfloat") == 0) + { + float *v = (float*)ptr; + TSL_SetReal(L, Value, *v); + } + else if (_stricmp(Name, "writefloat") == 0) + { + float *v = (float*)ptr; + *v = TSL_AsDouble(tval); + + } + else if (_stricmp(Name, "readptr") == 0) + { + INT_PTR *v = (INT_PTR*)ptr; + TSL_SetIntPtr(L, Value, *v); + } + else if (_stricmp(Name, "writeptr") == 0) + { + INT_PTR *v = (INT_PTR*)ptr; + *v = TSL_AsIntPtr(tval); + + } + else if (_stricmp(Name, "readdouble") == 0) + { + double *v = (double*)ptr; + TSL_SetReal(L, Value, *v); + } + else if (_stricmp(Name, "writedouble") == 0) + { + double *v = (double*)ptr; + *v = TSL_AsDouble(tval); + } + else if (_stricmp(Name, "readstr") == 0) + { + char *v = (char*)ptr; + //printf("\r\n--%s", v); + TSL_SetString(L, Value, v); + } + else if (_stricmp(Name, "writestr") == 0) + { + char *v = (char*)ptr; + const char* ps = TSL_AsString(tval); + int len = strlen(ps); + memcpy(v, ps, len); + v[len] = '\0'; + } + else if (_stricmp(Name, "readshort") == 0) + { + short *v = (short*)ptr; + TSL_SetInt(L, Value, *v); + } + else if (_stricmp(Name, "readushort") == 0) + { + unsigned short *v = (unsigned short*)ptr; + TSL_SetInt(L, Value, *v); + } + else if (_stricmp(Name, "writeshort") == 0) + { + short *v = (short*)ptr; + *v = TSL_AsInt(tval); + } + else if (_stricmp(Name, "writeushort") == 0) + { + unsigned short *v = (unsigned short*)ptr; + *v = TSL_AsInt(tval); + } + else if (_stricmp(Name, "readlong") == 0) + { + long *v = (long*)ptr; + TSL_SetInt(L, Value, *v); + } + else if (_stricmp(Name, "readulong") == 0) + { + unsigned long *v = (unsigned long*)ptr; + TSL_SetInt(L, Value, *v); + } + else if (_stricmp(Name, "writelong") == 0) + { + long *v = (long*)ptr; + *v = TSL_AsInt(tval); + } + else if (_stricmp(Name, "writeulong") == 0) + { + unsigned long *v = (unsigned long*)ptr; + *v = TSL_AsInt(tval); + } + else if (_stricmp(Name, "writeints")==0) + { + //if (!TSL_NumberCheck(tval)) return 0; + int *v = (int*)ptr; + for (int i = 0; i < TSL_AsInt(tval); i++) + { + *v = TSL_AsInt( TSL_HashGetItemInt(L, tvals, i)); + v++; + } + + } + else if (_stricmp(Name, "writeuints") == 0) + { + //if (!TSL_NumberCheck(tval)) return 0; + unsigned int *v = (unsigned int*)ptr; + for (int i = 0; i < TSL_AsInt(tval); i++) + { + *v = TSL_AsInt(TSL_HashGetItemInt(L, tvals, i)); + v++; + } + + } + else if (_stricmp(Name, "readints") == 0) + { + TSL_SetType(L, Value, TSL_TTABLE); + int *v = (int*)ptr; + for (int i = 0; i < TSL_AsInt(tval); i++) + { + TSL_SetInt(L, TSL_HashSetItemInt(L, Value, i), *v); + v++; + } + + } + else if (_stricmp(Name, "readuints") == 0) + { + TSL_SetType(L, Value, TSL_TTABLE); + unsigned int *v = (unsigned int*)ptr; + for (int i = 0; i < TSL_AsInt(tval); i++) + { + TSL_SetInt(L, TSL_HashSetItemInt(L, Value, i), *v); + v++; + } + + } + else if (_stricmp(Name, "writedoubles") == 0) + { + double *v = (double*)ptr; + for (int i = 0; i < TSL_AsInt(tval); i++) + { + *v = TSL_AsDouble(TSL_HashGetItemInt(L, tvals, i)); + v++; + } + + } + else if (_stricmp(Name, "readdoubles") == 0) + { + TSL_SetType(L, Value, TSL_TTABLE); + double *v = (double*)ptr; + for (int i = 0; i < TSL_AsInt(tval); i++) + { + TSL_SetReal(L, TSL_HashSetItemInt(L, Value, i), *v); + v++; + } + + } + else if (_stricmp(Name, "writebytes") == 0) + { + byte *v = (byte*)ptr; + for (int i = 0; i < TSL_AsInt(tval); i++) + { + *v = TSL_AsInt(TSL_HashGetItemInt(L, tvals, i)); + v++; + } + + } + else if (_stricmp(Name, "readbytes") == 0) + { + TSL_SetType(L, Value, TSL_TTABLE); + byte *v = (byte*)ptr; + for (int i = 0; i < TSL_AsInt(tval); i++) + { + TSL_SetInt(L, TSL_HashSetItemInt(L, Value, i), *v); + v++; + } + + } + else if (_stricmp(Name, "writeshorts") == 0) + { + short *v = (short*)ptr; + for (int i = 0; i < TSL_AsInt(tval); i++) + { + *v = TSL_AsInt(TSL_HashGetItemInt(L, tvals, i)); + v++; + } + + } + else if (_stricmp(Name, "writeushorts") == 0) + { + unsigned short *v = (unsigned short*)ptr; + for (int i = 0; i < TSL_AsInt(tval); i++) + { + *v = TSL_AsInt(TSL_HashGetItemInt(L, tvals, i)); + v++; + } + + } + else if (_stricmp(Name, "readshorts") == 0) + { + TSL_SetType(L, Value, TSL_TTABLE); + short *v = (short*)ptr; + for (int i = 0; i < TSL_AsInt(tval); i++) + { + TSL_SetInt(L, TSL_HashSetItemInt(L, Value, i), *v); + v++; + } + + } + else if (_stricmp(Name, "readushorts") == 0) + { + TSL_SetType(L, Value, TSL_TTABLE); + unsigned short *v = (unsigned short*)ptr; + for (int i = 0; i < TSL_AsInt(tval); i++) + { + TSL_SetInt(L, TSL_HashSetItemInt(L, Value, i), *v); + v++; + } + + } + else if (_stricmp(Name, "writefloats") == 0) + { + float *v = (float*)ptr; + for (int i = 0; i < TSL_AsInt(tval); i++) + { + *v = TSL_AsDouble(TSL_HashGetItemInt(L, tvals, i)); + v++; + } + + } + else if (_stricmp(Name, "readfloats") == 0) + { + TSL_SetType(L, Value, TSL_TTABLE); + float *v = (float*)ptr; + for (int i = 0; i < TSL_AsInt(tval); i++) + { + TSL_SetReal(L, TSL_HashSetItemInt(L, Value, i), *v); + v++; + } + + } + else if (_stricmp(Name, "writeintptrs") == 0) + { + INT_PTR *v = (INT_PTR*)ptr; + for (int i = 0; i < TSL_AsInt(tval); i++) + { + *v = TSL_AsIntPtr(TSL_HashGetItemInt(L, tvals, i)); + v++; + } + + } + else if (_stricmp(Name, "readintptrs") == 0) + { + TSL_SetType(L, Value, TSL_TTABLE); + INT_PTR *v = (INT_PTR*)ptr; + for (int i = 0; i < TSL_AsIntPtr(tval); i++) + { + TSL_SetIntPtr(L, TSL_HashSetItemInt(L, Value, i), *v); + v++; + } + + } + else if (_stricmp(Name, "writelongs") == 0) + { + int *v = (int*)ptr; + for (int i = 0; i < TSL_AsInt(tval); i++) + { + *v = TSL_AsInt(TSL_HashGetItemInt(L, tvals, i)); + v++; + } + + } + else if (_stricmp(Name, "writeulongs") == 0) + { + unsigned int *v = (unsigned int*)ptr; + for (int i = 0; i < TSL_AsInt(tval); i++) + { + *v = TSL_AsInt(TSL_HashGetItemInt(L, tvals, i)); + v++; + } + + } + else if (_stricmp(Name, "readintptrs") == 0) + { + TSL_SetType(L, Value, TSL_TTABLE); + int *v = (int*)ptr; + for (int i = 0; i < TSL_AsIntPtr(tval); i++) + { + TSL_SetInt(L, TSL_HashSetItemInt(L, Value, i), *v); + v++; + } + + } + else + { + return false; + } + + + return 1; +} + +//tslc +const char *ttslaefclassbuf ;// [1024]; + + +//初始虚基类结构 +void initvirtualbuf() +{ + //tslc + ttslaefclassbuf = "Type " AEFCLASS " =FackClass\r\n public \ + tmalloc;tfree;tmset;trealloc;\ + getstdin1;getstdout1;getstderr1; \ + readbyte;writebyte;\ + readint;writeint;\ + writedouble;readdouble; \ + writellong;readlong; \ + writeshort;readshort; \ + writebytes;readbytes;\ + writeshorts;readshorts;\ + End;"; + +} +//在代码中获得当前的L参数 +int GetCurrentL(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + TSL_SetIntPtr(L, oResult, (INT_PTR)L); + return 1; +}//获取对象的指针地址,为消息传递做索引 +int setunitname(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + return 1; + //获得参数对象引用 + TObject * id = TSL_GetParamRef(L, 0);// TSL_ReadTObjectFromPtr(iArgs, 0); + if (TSL_StringCheck(id)) + { + const char * na = TSL_AsString(id); + int len = strlen(na); + //strncpy(TSLMODELNAME, na, len); + //TSLMODELNAME[len] = '\0'; + } + //TSL_SetString(L, oResult, TSLMODELNAME); + return 1; +} +int gettslibpath(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + //获得参数对象引用 + if (0 == LibPath) { + LibPath = TSL_Strdup(TSL_GetLibPath(L)); + } + TSL_SetString(L, oResult, LibPath); + return 1; +} +int settslibpath(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + //获得参数对象引用 + if (1 != iNum) { return 0; } + TObject * lp = TSL_GetParamRef(L, 0);// + if (false==TSL_StringCheck(lp)) + { + return 0; + } + gettslibpath(L, iArgs, 0, oResult); + if (0 != LibPath) + { + TSL_Free(LibPath); + } + LibPath = TSL_Strdup(TSL_AsString(lp)); + TSL_SetLibPath(L, LibPath); + return 1; +} +//获得变量地址,该方法已在tsl中用int64替代了 +int getobjaddr(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + //获得参数对象引用 + TObject * id = TSL_GetParamRef(L,0);// TSL_ReadTObjectFromPtr(iArgs, 0); + INT_PTR * idt = (INT_PTR *)id; + TSL_SetIntPtr(L, oResult, *idt); + return 1; +} + + +#ifdef __LINUX_GTK__ +///////////////////////linux独有//////////////////////////////////////////////// +gpointer * WorkerThreadCall(void *p) +{ + if (!p) + { + return 0; + } + TSL_State* st = TS_GetGlobalL(); + + if (0 != LibPath) + { + TSL_SetLibPath(st, LibPath); + } + //printf("\r&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&%s\n",p); + AUTOTSLOBJ(tslgoretv, st); + if (TSL_ScriptGo_B(st, (char *)p, tslgoretv, "\r\nUI Threadworker Call") == TSL_ERROR) + { + return 0; + } + return 0; +} +int getcurrentdir(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + char buf[MAXNAMLEN]; + getcwd(buf, MAXNAMLEN); + TSL_SetString(L, oResult, buf); + return 1; +} +extern "C" { + int MessageBoxQuit(GtkWidget * w, gpointer d) + { + INT_PTR *p = d; + for (int i = 0; i<4; i++) + { + + if ((INT_PTR)w == *(p + i)) + { + if (i == 0) + { + gtk_main_quit(); + } + else + { + g_signal_emit_by_name(*(p), "destroy"); + } + *(p + i) = NULL; + break; + } + } + return 0; + } + void frame_callback(GtkWidget *window, GdkEvent *event, gpointer data) + { + int x, y; + char buf[10]; + + x = event->configure.x; + y = event->configure.y; + + sprintf(buf, "%d,%d", x, y); + + gtk_window_set_title(GTK_WINDOW(window), buf); + } + +} + +int tsl_gtk_messagebox_(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + //gtk_init(0,0); + GtkWidget *dialog, *label, *vbox, *hbox, *btn, *pt, *btns[4]; + dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);//gtk_dialog_new(); + gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE); + gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ALWAYS); + btns[0] = dialog; + g_signal_connect(GTK_WIDGET(dialog), "destroy", MessageBoxQuit, btns); + //gtk_widget_add_events(GTK_WIDGET(dialog),GDK_CONFIGURE); + //g_signal_connect(G_OBJECT(dialog),"configure-event",G_CALLBACK(frame_callback),NULL); + + // gtk_window_set_type_hint(GTK_WINDOW (dialog),GDK_WINDOW_TYPE_HINT_MENU); + TObject * id = TSL_GetParamRef(L, 0); + TObject * idt = TSL_GetParamRef(L, 1); + pt = TSL_AsIntPtr(TSL_GetParamRef(L, 2)); + if (GTK_IS_WINDOW(pt)) + { + if (!gtk_widget_is_toplevel(pt)) + { + pt = gtk_widget_get_toplevel(pt); + } + gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); + gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(pt)); + } + gtk_window_set_title(GTK_WINDOW(dialog), TSL_AsString(idt)); + gtk_container_set_border_width(GTK_CONTAINER(dialog), 5); + //gtk_widget_set_size_request (dialog, 470, 227); + gtk_widget_realize(dialog); + label = gtk_label_new(TSL_AsString(id)); + + gtk_misc_set_padding(GTK_MISC(label), 10, 10); + vbox = gtk_vbox_new(FALSE, 5); + hbox = gtk_hbox_new(FALSE, 10); + gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0); + + gtk_widget_show(label); + gtk_container_add(GTK_CONTAINER(dialog), GTK_WIDGET(vbox)); + gtk_widget_show(vbox); + gtk_widget_show(hbox); + + //TSL_SetInt(L,oResult, gtk_dialog_run (GTK_DIALOG (dialog))); + gtk_widget_show(dialog); + TObject * temp = 0; + for (int idx = 0; idx<3; idx++) + { + temp = TSL_GetParamRef(L, 3 + (idx * 2) + 0); + btn = gtk_button_new_with_label(TSL_AsString(temp)); + btns[idx + 1] = btn; + temp = TSL_GetParamRef(L, 3 + (idx * 2) + 1); + //g_signal_connect_after(GTK_WIDGET( btn),"clicked",ClosingDialog,btns); + g_signal_connect(GTK_WIDGET(btn), "clicked", MessageBoxQuit, btns); + if (TSL_AsBool(temp)) + { + gtk_widget_show(btn); + } + else + { + + } + gtk_box_pack_start(GTK_BOX(hbox), btn, TRUE, TRUE, 0); + + } + + gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0); + //gtk_grab_add(dialog); + gtk_main(); + for (int idx = 3; idx >= 0; idx--) + { + if (NULL == (btns[idx])) + { + TSL_SetInt(L, oResult, idx); + /* if(idx>0) + { + gtk_widget_destroy (dialog); + } + */ + break; + } + + } + //gtk_widget_destroy(dialog); + return 1; +} + +//typedef int (*windowProc)(void*); +typedef void* windowProc; +thread_local void * tsllibmodal = 0; + +//GetGtkEventNameOrId +INT_PTR getnameid(const char * b) +{ + if (GlobalL1 == 0) { return 0; } + const char * ss = TSLGTKMODALNAME ".GetGtkEventNameOrId"; + AUTOTSLOBJ(tslgoretv, GlobalL1); + AUTOTSLOBJ(fn, GlobalL1); + + TObject *args = TSL_NewObjects(1); + + TSL_SetString(GlobalL1, fn, ss); + TSL_SetString(GlobalL1, TSL_ReadTObjectFromPtr(args, 0), b); + + try + { + raw_call2(GlobalL1, fn, args, 1, tslgoretv); + } + catch (...) + { + //printf(" \r\n try===========3333====+++++="); + //fflush(stdout); + rawcallerrdo(GlobalL1); + } + + TSL_FreeObjs(GlobalL1, args, 1); + return TSL_AsIntPtr(tslgoretv); +} + + +gint dodeleteevents(void * a, const char * b) +{ + if (GlobalL1 == 0) { return 0; } + const char * tslscript = TSLGTKMODALNAME "._gtkeventcall_"; + AUTOTSLOBJ(tslgoretv, GlobalL1); + INT_PTR pms[3]; + pms[0] = (INT_PTR)a; + pms[1] = getnameid(b); + pms[2] = 0; + calltslfunction_p_int(GlobalL1, tslscript, pms, 3, tslgoretv); + return TSL_AsBool(tslgoretv); +} +gint dotparamseevents(void * a, const char * b, void * p1, void * p2) +{ + if (GlobalL1 == 0) { return 0; } + const char * tslscript = TSLGTKMODALNAME "._gtkeventcall_" ; + AUTOTSLOBJ(tslgoretv, GlobalL1); + INT_PTR pms[4]; + pms[0] = (INT_PTR)a; + pms[1] = getnameid(b); + pms[2] = (INT_PTR)p1; + pms[3] = (INT_PTR)p2; + calltslfunction_p_int(GlobalL1, tslscript, pms, 4, tslgoretv); + return TSL_AsBool(tslgoretv); +} +gint tsl_gtk_destroy_event(void * a, void * b, void* c) +{ + if (GlobalL1 == 0) { return 0; } + const char * tslscript = TSLGTKMODALNAME "._gtkeventcall_"; + AUTOTSLOBJ(tslgoretv, GlobalL1); + INT_PTR pms[3]; + pms[0] = (INT_PTR)a; + pms[1] = (INT_PTR)b; + pms[2] = (INT_PTR)c; + calltslfunction_p_int(GlobalL1, tslscript, pms, 3, tslgoretv); + return TSL_AsInt(tslgoretv); +} + +gint tsl_gtk_delete_event(void * a, void * b) +{ + return dodeleteevents(a, "delete-event"); +} +gint tsl_gtk_wmuser_event(void * a, void * p1, void*p2) +{ + return dotparamseevents(a, "wm-user", p1, p2); +} +int tsl_gtk_adj_value_changed_event(void * a, void * b) +{ + return dodeleteevents(a, "value-changed"); +} +gint tsl_gtk_clicked_event(void * a, void * b) +{ + return dodeleteevents(a, "clicked"); +} +gint tsl_gtk_activate_event(void * a, void * b) +{ + return dodeleteevents(a, "activate"); +} +gint tsl_gtk_changed_event(void * a, void * b) +{ + return dodeleteevents(a, "changed"); +} +gint tsl_gtk_preedit_changed_event(void * a, void * b, void * c) +{ + return dotparamseevents(a, "preedit-changed", b, c); +} +gint tsl_gtk_draw_event(void * a, void * b, void * c) +{ + return dotparamseevents(a, "draw", b, c); +} +gint tsl_gtk_entry_insert_text(void * a, void * b, int c, int*p, void * gd) +{ + g_object_set_data(a, "widget-event-param-1", p); + g_object_set_data(a, "widget-event-param--2", gd); + return dotparamseevents(a, "insert-text", b, c); +} + +gint tsl_gtk_normal_event_bc(void * a, void * b, void * c) +{ + + if (GlobalL1 == 0) { return 0; } + const char * tslscript = TSLGTKMODALNAME "._gtkeventcall_"; + AUTOTSLOBJ(tslgoretv, GlobalL1); + INT_PTR pms[3]; + pms[0] = (INT_PTR)a; + pms[1] = (INT_PTR)b; + pms[2] = (INT_PTR)c; + calltslfunction_p_int(GlobalL1, tslscript, pms, 3, tslgoretv); + return TSL_AsInt(tslgoretv); + + + //获得消息处理函数的指针 +} +gint tsl_gtk_normal_event_cb(void * a, void * b, void* c) +{ + //printf("****call in***\r\n "); + if (GlobalL1 == 0) { return 0; } + const char * tslscript = TSLGTKMODALNAME "._gtkeventcall_"; + AUTOTSLOBJ(tslgoretv, GlobalL1); + INT_PTR pms[3]; + pms[0] = (INT_PTR)a; + pms[1] = (INT_PTR)c; + pms[2] = (INT_PTR)b; + calltslfunction_p_int(GlobalL1, tslscript, pms, 3, tslgoretv); + //return TSL_AsInt(tslgoretv); + return (TSL_AsInt(tslgoretv) == 0) ? FALSE : TRUE; + + //获得消息处理函数的指针 +} +gint tsl_gtk_day_select_event(void * a, void * b) +{ + return dodeleteevents(a, "day-selected"); + + //获得消息处理函数的指针 +} + + +gboolean tsl_gtk_idl_do(void * dlg) //do idle +{ + if (GlobalL1 == 0) { return 0; } + const char * tslscript = TSLGTKMODALNAME "._gtkidledo_"; + AUTOTSLOBJ(tslgoretv, GlobalL1); + INT_PTR pms[1]; + pms[0] = (INT_PTR)0; + calltslfunction_p_int(GlobalL1, tslscript, pms, 1, tslgoretv); + //return true; + return TSL_AsBool(tslgoretv); + +} + + + + + +//////////////////////////////////////// +bool gtkcall_2(void * a, void * b) +{ + const char * tslscript = TSLGTKMODALNAME "._gtkeventcall_"; + AUTOTSLOBJ(tslgoretv, GlobalL1); + INT_PTR pms[2]; + pms[0] = (INT_PTR)a; + pms[1] = (INT_PTR)b; + //pms[2] = (INT_PTR)c; + calltslfunction_p_int(GlobalL1, tslscript, pms, 2, tslgoretv); + return TSL_AsBool(tslgoretv); +} +bool gtkcall_3(void * a, void * b, void *c) +{ + const char * tslscript = TSLGTKMODALNAME "._gtkeventcall_" ; + AUTOTSLOBJ(tslgoretv, GlobalL1); + INT_PTR pms[3]; + pms[0] = (INT_PTR)a; + pms[1] = (INT_PTR)b; + pms[2] = (INT_PTR)c; + calltslfunction_p_int(GlobalL1, tslscript, pms, 3, tslgoretv); + return TSL_AsBool(tslgoretv); +} + +bool gtkcall_4(void * a, void * b, void *c, void*d) +{ + const char * tslscript= TSLGTKMODALNAME "._gtkeventcall_"; + AUTOTSLOBJ(tslgoretv, GlobalL1); + INT_PTR pms[4]; + pms[0] = (INT_PTR)a; + pms[1] = (INT_PTR)b; + pms[2] = (INT_PTR)c; + pms[3] = (INT_PTR)d; + calltslfunction_p_int(GlobalL1, tslscript, pms, 4, tslgoretv); + return TSL_AsBool(tslgoretv); + +} +int getwinprocptr(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + GlobalL1 = L; + windowProc prc = (windowProc)deal_time; + if (iNum == 1) + { + //获得系统消息函数处理的指针 + TObject * id = TSL_GetParamRef(L, 0); + if (TSL_AsInt(id) == 1) + { + //prc = deal_time; + } + else if (TSL_AsInt(id) == 2) + { + prc = (windowProc)deal_time; + } + else if (TSL_AsInt(id) == 3) + { + //prc = deal_time; + } + else if (0x10 == TSL_AsInt(id)) + { + //prc = deal_time; + } + else if (0x11 == TSL_AsInt(id)) + { + //prc = deal_time; + } + else if (0x12 == TSL_AsInt(id)) + { + //prc = deal_time; + } + else if (0x13 == TSL_AsInt(id)) + { + //prc = deal_time; + } + else if (0x14 == TSL_AsInt(id)) + { + //prc = deal_time; + } + else if (0x101 == TSL_AsInt(id)) + { + //prc = (windowProc)deal_time; + } + else if (0x102 == TSL_AsInt(id)) + { + //prc = (windowProc)deal_time; + } + else if (0x103 == TSL_AsInt(id)) + { + //prc = (windowProc)deal_time; + } + else if (0x202 == TSL_AsInt(id)) + { + prc = (windowProc)gtkcall_2; + } + else if (0x203 == TSL_AsInt(id)) + { + prc = (windowProc)gtkcall_3; + } + else if (0x204 == TSL_AsInt(id)) + { + prc = (windowProc)gtkcall_4; + } + } + TSL_SetIntPtr(L, oResult, (INT_PTR)prc); + return 1; +} + + + + + + +struct timerstrct { + int id; + int flg; +}; +int deal_time(void * p) +{ + if (GlobalL1 == 0) { return 0; } + timerstrct * str = (timerstrct*)p; + + const char * tslscript= TSLGTKMODALNAME "._gtk_add_time_msg_"; + AUTOTSLOBJ(tslgoretv, GlobalL1); + INT_PTR pms[4]; + pms[0] = 0; + pms[1] = (INT_PTR)str->flg; + pms[2] = (INT_PTR)str->id; + pms[3] = 0x113; + calltslfunction_p_int(GlobalL1, tslscript, pms, 4, tslgoretv); + //return TSL_AsIntPtr(tslgoretv); + return true; +} +INT_PTR gettslvcleventhandler(const char * fname) +{ + if (_stricmp(fname, "tsl_gtk_wmuser_event") == 0) + { + return tsl_gtk_wmuser_event; + } + if (_stricmp(fname, "tsl_gtk_destroy_event") == 0) + { + return tsl_gtk_destroy_event; + } + if (_stricmp(fname, "tsl_gtk_activate_event") == 0) + { + return tsl_gtk_activate_event; + } + if (_stricmp(fname, "tsl_gtk_clicked_event") == 0) + { + return tsl_gtk_clicked_event; + } + if (_stricmp(fname, "tsl_gtk_adj_value_changed_event") == 0) + { + return tsl_gtk_adj_value_changed_event; + } + if (_stricmp(fname, "tsl_gtk_delete_event") == 0) + { + return tsl_gtk_delete_event; + } + + if (_stricmp(fname, "tsl_gtk_day_select_event") == 0) + { + return tsl_gtk_day_select_event; + } + + + if (_stricmp(fname, "tsl_gtk_changed_event") == 0) + { + return tsl_gtk_changed_event; + } + + if (_stricmp(fname, "tsl_gtk_preedit_changed_event") == 0) + { + return tsl_gtk_preedit_changed_event; + } + if (_stricmp(fname, "dodeleteevents") == 0) + { + return dodeleteevents; + } + if (_stricmp(fname, "tsl_gtk_normal_event_cb") == 0) + { + return tsl_gtk_normal_event_cb; + } + if (_stricmp(fname, "tsl_gtk_normal_event_bc") == 0) + { + return tsl_gtk_normal_event_bc; + } + return 0; +} + + +int tsl_gtk_createprocessa(char * name, char * argv[], char* envp[], INT_PTR & pw) +{ + int fid[2], r; + pipe(fid); + pid_t pid = fork(); + if (pid == 0) + { + close(fid[0]); + dup2(fid[1], 1); + close(fid[1]); + /* + char *argv[] = {"/home/json/tslvcllinux/tsl_linux/TSL","/home/json/tslvcllinux/tsl_linux/script/vcldesigner.tsl","-libpath","/home/json/tslvcllinux/tsl_linux/designer/",NULL}; + char argg = "/home/json/tslvcllinux/tsl_linux/TSL /home/json/tslvcllinux/tsl_linux/script/vcldesigner.tsl -libpath /home/json/tslvcllinux/tsl_linux/designer/\0\0"; + char * envp[] = {"LD_LIBRARY_PATH=/home/json/tslvcllinux/tsl_linux/","DISPLAY=:0", NULL}; + */ + //char *argv[] = {"/home/json/tslvcllinux/tsl_linux/TSL","/home/json/tslvcllinux/tsl_linux/script/techo.tsl","-libpath","/home/json/tslvcllinux/tsl_linux/designer/",NULL}; + + r = execve(name, argv, envp); + _exit(r); + + //execl("/home/json/tslvcllinux/tsl_linux/TSL",argg); + } + else + if (pid>0) + { + pw = fid[0]; + close(fid[1]); + int flag = fcntl(fid[0], F_GETFL); + fcntl(fid[0], F_SETFL, flag | O_NONBLOCK); + return pid; + } + else + { + return -1; + } + return r; +} +int tslvclgetventhandler(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + if (iNum = 1) + { + + INT_PTR ret = gettslvcleventhandler(TSL_AsString(TSL_GetParamRef(L, 0))); + TSL_SetIntPtr(L, oResult, ret); + } + else + { + TSL_SetInt(L, oResult, 0); + } + return 1; +} +int tsl_gtk_execsystem(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + if (iNum = 1) + { + + int ret = system(TSL_AsString(TSL_GetParamRef(L, 0))); + TSL_SetIntPtr(L, oResult, ret); + } + else + { + TSL_SetInt(L, oResult, 0); + } + return 1; +} +int tsl_gtk_color_selection_property(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + //获得消息处理函数的指针 + if (iNum = 1) + { + + GtkColorSelectionDialog *cdlg = (GtkColorSelectionDialog *)TSL_AsIntPtr(TSL_GetParamRef(L, 0)); + TSL_SetIntPtr(L, oResult, cdlg->priv); + } + else + { + TSL_SetInt(L, oResult, 0); + } + return 1; + +} +int tsl_gtk_idle_interface(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + //获得消息处理函数的指针 + if (iNum = 1) + { + TSL_SetIntPtr(L, oResult, g_idle_add(tsl_gtk_idl_do, TSL_AsIntPtr(TSL_GetParamRef(L, 0)))); + } + else + { + TSL_SetInt(L, oResult, 0); + } + return 1; + +} +int tsl_gtk_closehandle(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + if (iNum = 1) + { + TSL_SetIntPtr(L, oResult, close( TSL_AsIntPtr(TSL_GetParamRef(L, 0)))); + } + else + { + TSL_SetInt(L, oResult, 0); + } + return 1; +} +int tsl_gtk_pipread(INT_PTR hr, char * msg, int ct) +{ + int r = read(hr, msg, ct); + if (r>0) + { + msg[r] = '\0'; + } + return r; +} + +int tsl_gtk_waitpid(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + //int tsl_gtk_waitpid(INT_PTR id, int &s) + //return waitpid(id,&s,0); + return 1; +} + +int tsl_gtk_kill(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + if (iNum = 2) + { + TSL_SetIntPtr(L, oResult, kill(TSL_AsInt(TSL_GetParamRef(L, 0)), TSL_AsInt(TSL_GetParamRef(L, 1)))); + } + else + { + TSL_SetInt(L, oResult, 0); + } + return 1; +} + +int __xinitthreads__(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + TSL_SetInt(L, oResult, XInitThreads()); + return 1; +} + +void pkginit(TSL_State* L) +{ + //对话框 + tslL_register(L, "tsl_gtk_messagebox_", tsl_gtk_messagebox_); + tslL_register(L, "gettslvcleventhandler", tslvclgetventhandler); + tslL_register(L, "tsl_gtk_execsystem", tsl_gtk_execsystem); + tslL_register(L, "tsl_gtk_color_selection_property", tsl_gtk_color_selection_property); + tslL_register(L, "tsl_gtk_idle_interface", tsl_gtk_idle_interface); + tslL_register(L, "tsl_gtk_closehandle", tsl_gtk_closehandle); + tslL_register(L, "__xinitthreads__", __xinitthreads__); + tslL_register(L, "tsl_gtk_kill", tsl_gtk_kill); + tslL_register(L, "tsl_gtk_waitpid", tsl_gtk_waitpid); + gtk_init(0, 0); +} + + +#else +////////////////////////////////////////////////////////////////////////////////////////////// +//消息分发处理函数 +LRESULT CALLBACK TWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) +{ + //天软脚本字符串 + //return DefWindowProcA(hwnd,message,wparam,lparam); + //char tslscript[512]; + const char *tslscript=TSLMODELNAME "._timeproc_"; + if (GlobalL1 == 0) return 0; + + AUTOTSLOBJ(tslgoretv, GlobalL1); + INT_PTR pms[4]; + pms[0] = (INT_PTR)hwnd; + pms[1] = (INT_PTR)message; + pms[2] = (INT_PTR)wparam; + pms[3] = (INT_PTR)lparam; + + if ((hwnd == NULL) && (message == 275))//没有窗口的计时器 + { + tslscript = TSLMODELNAME "._timeproc_"; + + calltslfunction_p_int(GlobalL1, tslscript, pms, 4, tslgoretv); + } + else + { + tslscript = TSLMODELNAME "._twinproc_"; + calltslfunction_p_int(GlobalL1, tslscript, pms, 4, tslgoretv); + } + return TSL_AsIntPtr(tslgoretv); + +} +//定时器消息分发 +LRESULT CALLBACK TimeProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) +{ + //天软脚本字符串 + //return DefWindowProcA(hwnd,message,wparam,lparam); + char *ss = 0; + const char * tslscript = TSLMODELNAME "._timeproc_" ; + if (GlobalL1 == 0) return 0; + AUTOTSLOBJ(tslgoretv, GlobalL1); + INT_PTR pms[4]; + pms[0] = (INT_PTR)hwnd; + pms[1] = (INT_PTR)message; + pms[2] = (INT_PTR)wparam; + pms[3] = (INT_PTR)lparam; + calltslfunction_p_int(GlobalL1, tslscript, pms, 4, tslgoretv); + return TSL_AsIntPtr(tslgoretv); + + +} +LRESULT CALLBACK TWndProc2(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) +{ + //天软脚本字符串 + //return DefWindowProcA(hwnd,message,wparam,lparam); + const char * tslscript= TSLMODELNAME "._tduiproc_";; + if (GlobalL1 == 0) return 0; + AUTOTSLOBJ(tslgoretv, GlobalL1); + INT_PTR pms[4]; + pms[0] = (INT_PTR)hwnd; + pms[1] = (INT_PTR)message; + pms[2] = (INT_PTR)wparam; + pms[3] = (INT_PTR)lparam; + calltslfunction_p_int(GlobalL1, tslscript, pms, 4, tslgoretv); + + + return TSL_AsIntPtr(tslgoretv); + + +} +LRESULT CALLBACK _GetMsgProc_(UINT message, WPARAM wparam, LPARAM lparam, char * name) +{ + //hooke字符串 + //return DefWindowProcA(hwnd,message,wparam,lparam); + const char * ss = TSLMODELNAME "._GetMsgProc_%s" ; + char tslscript[512]; + if (GlobalL1 == 0) return 0; + AUTOTSLOBJ(tslgoretv, GlobalL1); + INT_PTR pms[3]; + pms[0] = (INT_PTR)message; + pms[1] = (INT_PTR)wparam; + pms[2] = (INT_PTR)lparam; + sprintf(tslscript, ss, name); + calltslfunction_p_int(GlobalL1, tslscript, pms, 3, tslgoretv); + return TSL_AsIntPtr(tslgoretv); + +} +LRESULT CALLBACK _GetMsgProc_a(UINT message, WPARAM wparam, LPARAM lparam) +{ + return _GetMsgProc_(message, wparam, lparam, "a"); +} +LRESULT CALLBACK _GetMsgProc_b(UINT message, WPARAM wparam, LPARAM lparam) +{ + return _GetMsgProc_(message, wparam, lparam, "b"); +} +LRESULT CALLBACK _GetMsgProc_c(UINT message, WPARAM wparam, LPARAM lparam) +{ + return _GetMsgProc_(message, wparam, lparam, "c"); +} + +LRESULT CALLBACK _MessgeHookBase_(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam, char * name) +{ + //hooke字符串 + //return DefWindowProcA(hwnd,message,wparam,lparam); + + + const char * ss = TSLMODELNAME "._MessgeHook_%s"; + char tslscript[512]; + if (GlobalL1 == 0) return 0; + AUTOTSLOBJ(tslgoretv, GlobalL1); + INT_PTR pms[4]; + pms[0] = (INT_PTR)hwnd; + pms[1] = (INT_PTR)message; + pms[2] = (INT_PTR)wparam; + pms[3] = (INT_PTR)lparam; + sprintf(tslscript, ss, name); + calltslfunction_p_int(GlobalL1, tslscript, pms, 4, tslgoretv); + return TSL_AsIntPtr(tslgoretv); + + +} +LRESULT CALLBACK _MessgeHook_(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) +{ + return _MessgeHookBase_(hwnd, message, wparam, lparam, ""); +} +LRESULT CALLBACK _MessgeHook_a(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) +{ + return _MessgeHookBase_(hwnd, message, wparam, lparam, "a"); +} +LRESULT CALLBACK _MessgeHook_b(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) +{ + return _MessgeHookBase_(hwnd, message, wparam, lparam, "b"); +} +LRESULT CALLBACK _MessgeHook_c(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) +{ + return _MessgeHookBase_(hwnd, message, wparam, lparam, "c"); +} +LRESULT CALLBACK _MessgeHook_d(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) +{ + return _MessgeHookBase_(hwnd, message, wparam, lparam, "d"); +} + +//事件函数指针类型 +typedef LRESULT(CALLBACK *windowProc)(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); + + +//获得winproc函数地址 +int getwinprocptr(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ //获得消息处理函数的指针 + GlobalL1 = L; + windowProc prc = TWndProc; + if (iNum ==1) + { + //获得系统消息函数处理的指针 + TObject * id = TSL_GetParamRef(L, 0); + if (TSL_AsInt(id) == 1) + { + prc = DefWindowProcA; + } + else if (TSL_AsInt(id) == 2) + { + prc = TimeProc; + } + else if (TSL_AsInt(id) == 3) + { + prc = TWndProc2; + } + else if (0x10 == TSL_AsInt(id)) + { + prc = _MessgeHook_; + } + else if (0x11 == TSL_AsInt(id)) + { + prc = _MessgeHook_a; + } + else if (0x12 == TSL_AsInt(id)) + { + prc = _MessgeHook_b; + } + else if (0x13 == TSL_AsInt(id)) + { + prc = _MessgeHook_c; + } + else if (0x14 == TSL_AsInt(id)) + { + prc = _MessgeHook_d; + } + else if (0x101 == TSL_AsInt(id)) + { + prc = (windowProc)_GetMsgProc_a; + } + else if (0x102 == TSL_AsInt(id)) + { + prc = (windowProc)_GetMsgProc_b; + } + else if (0x103 == TSL_AsInt(id)) + { + prc = (windowProc)_GetMsgProc_c; + } + } + TSL_SetIntPtr(L, oResult, (INT_PTR)*prc); + return 1; +} + + +int getcurrentdir(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + char buf[MAX_PATH]; + int n = GetCurrentDirectoryA(MAX_PATH, buf); + buf[n] = '\0'; + TSL_SetString(L, oResult, buf); + return 1; +} + +//线程启动回调 +void WorkerThreadCall(void *p) +{ + if (!p) + { + //return 0; + } + TSL_State* st = TS_GetGlobalL(); + + if (0 != LibPath) + { + + TSL_SetLibPath(st, LibPath); + } + AUTOTSLOBJ(tslgoretv, st); + CoInitializeEx(NULL, COINIT_MULTITHREADED | COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); + char * d = ThreadScripts[(INT_PTR)p]; + ThreadScripts.erase((INT_PTR)p); + if (TSL_ScriptGo_B(st, d, tslgoretv, "\r\nUI Threadworker Call") == TSL_ERROR) + { + //return 0; + } + TSL_Free(p); //处理删除 //delete p; +} +//图片编码辅助函数,gdi+用到 +int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) +{ + UINT num = 0; // number of image encoders + UINT size = 0; // size of the image encoder array in bytes + using namespace Gdiplus; + ImageCodecInfo* pImageCodecInfo = NULL; + GetImageEncodersSize(&num, &size); + if (size == 0) + return -1; // Failure + + pImageCodecInfo = (ImageCodecInfo*)(malloc(size)); + if (pImageCodecInfo == NULL) + return -1; // Failure + + GetImageEncoders(num, size, pImageCodecInfo); + + for (UINT j = 0; j < num; ++j) + { + if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0) + { + *pClsid = pImageCodecInfo[j].Clsid; + free(pImageCodecInfo); + return j; // Success + } + } + + free(pImageCodecInfo); + return -1; // Failure +} +//图片流辅助函数 +int tuicloseistream(IStream * s) +{ + if (s) + { + try + { + return s->Release(); + } + catch (...) + { + return 1; + } + + } + return -1; +} + +int tslvclGetEncoderClsid(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + if (iNum = 2) + { + int ret = GetEncoderClsid((const WCHAR *)TSL_AsString(TSL_GetParamRef(L, 0)), (CLSID*)TSL_AsIntPtr(TSL_GetParamRef(L, 1))); + TSL_SetInt(L, oResult, ret); + } + else + { + TSL_SetInt(L, oResult, -1); + } + return 1; +} +int tslvclcloseistream(TSL_State* L, TObject* iArgs, int iNum, TObject* oResult) +{ + if (iNum = 1) + { + IStream * s = (IStream *)TSL_AsIntPtr(TSL_GetParamRef(L, 0)); + int r = tuicloseistream(s); + TSL_SetInt(L, oResult, r); + } + else + { + TSL_SetInt(L, oResult, -1); + } + return 1; +} +void pkginit(TSL_State* L) +{ + /*INITCOMMONCONTROLSEX iccx; + iccx.dwSize = sizeof(INITCOMMONCONTROLSEX); + iccx.dwICC = ICC_LINK_CLASS; + int rot = InitCommonControlsEx(&iccx); + printf("\r\n============%d====\r\n",rot);*/ + tslL_register(L, "tslvclgetencoderclsid", tslvclGetEncoderClsid); + tslL_register(L, "tslvclcloseistream", tslvclcloseistream); +} + + + +#endif +//信息注册 +int PKG_Init(TSL_State* L, OpenLibProc proc) +{ + initvirtualbuf(); + TAnyMan* tslaef = TSL_NewAnyMan(AEFCLASS, aef_new, aef_free, aef_method, NULL, NULL); + TSL_RegisterAnyMan(AEFCLASS, tslaef); + + //获得变量addr实现索引 + tslL_register(L, "gettslvariableptr", getobjaddr); + //tslL_register(L, "setcallbackunitname", setunitname); + //获得消息处理函数指针 + tslL_register(L, "getwinprocptr", getwinprocptr); + + //获得对应c语言基础数据类型的大小 + tslL_register(L, "getctypesize", getctypesize); + //远程执行 + tslL_register(L, "ts_sendexecuteandwaitexcall", TS_SendExecuteAndWaitExcall); + //获得数据 + + tslL_register(L, "getcurrenttslstate", GetCurrentL); + //线程执行 + tslL_register(L, "uisetdatatothreadscripts", SetDataToThreadScripts); + tslL_register(L, "uisetdatatomessage", SetDataToMessages); + tslL_register(L, "uifeachthreadmessage", FeachThreadMessagedata); + + tslL_register(L, "uisetthreadworkerdata", SetThreadWorkerData); + tslL_register(L, "uifeachthreadworkerdata", FeachThreadWorkerData); + tslL_register(L, "uideleteworkerdata", DeletehreadWorkerData); + tslL_register(L, "uibeginthreadworker", BeginThreadWorker); + //分词和系统函数获取 + tslL_register(L, "tsl_tokenizeex_2_", TSL_TokenizeEx_2_); + tslL_register(L, "tsll_getfunctions_2_", tslL_getfunctions_2_); + tslL_register(L, "tsl_getlibpath_", gettslibpath); + tslL_register(L, "tsl_setlibpath_", settslibpath); + + // 系统相关 + tslL_register(L, "tsl_getcurrentdir_", getcurrentdir); + + pkginit(L); + + return TSL_OK; +} \ No newline at end of file