summaryrefslogtreecommitdiff
path: root/modules/tclsci/src/c/InitTclTk.c
blob: 0945ff6bd5a6780e5df0cd4000328b388e4a6606 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
* Copyright (C) 2005-2008 - INRIA - Allan CORNET
* Copyright (C) 2007-2008 - INRIA - Bruno JOFRET
*
* This file must be used under the terms of the CeCILL.
* This source file is licensed as described in the file COPYING, which
* you should have received as part of this distribution.  The terms
* are also available at
* http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
*
*/


#include <string.h>
#ifndef _MSC_VER
#include <dirent.h>
#include <ctype.h>
#else
#include <windows.h>
#include "EnvTclTk.h"
#include "strdup_windows.h"
#endif
#include "InitTclTk.h"
#include "setgetSCIpath.h"
#include "sciprint.h"
#include "Scierror.h"
#include "localization.h"
#include "scilabmode.h"
#include "ScilabEval.h"
#include "TCL_Command.h"
#include "GlobalTclInterp.h"
#include "BOOL.h"
#include "PATH_MAX.h"
#include "getshortpathname.h"
/*--------------------------------------------------------------------------*/
BOOL TK_Started = FALSE;

/*--------------------------------------------------------------------------*/
static char *GetSciPath(void);
/*--------------------------------------------------------------------------*/
static void *DaemonOpenTCLsci(void* in)
/* Checks if tcl/tk has already been initialised and if not */
/* initialise it. It must find the tcl script */
{
    char *SciPath							= NULL;
    char *SciPathShort				= NULL;
    char *TkScriptpathShort		= NULL;
    BOOL tkStarted						= FALSE;
    BOOL bOK									= FALSE;

    char TkScriptpath[PATH_MAX];
    char MyCommand[2048]; /* @TODO: Check for buffer overflow */


#ifndef _MSC_VER
    DIR *tmpdir = NULL;
#endif

    FILE *tmpfile2 = NULL;

    SciPath = GetSciPath();

    /* test SCI validity */
    if (SciPath == NULL)
    {
        sciprint(_("The SCI environment variable is not set.\nTCL initialisation failed !\n"));
    }


    SciPathShort = getshortpathname(SciPath, &bOK);

#ifdef TCL_MAJOR_VERSION
#ifdef TCL_MINOR_VERSION
#if TCL_MAJOR_VERSION >= 8
#if TCL_MINOR_VERSION > 0
    Tcl_FindExecutable(" ");
#endif
#endif
#endif
#endif

#ifdef _MSC_VER

    strcpy(TkScriptpath, SciPathShort);
    strcat(TkScriptpath, "/modules/tclsci/tcl/TK_Scilab.tcl");

    TkScriptpathShort = getshortpathname(TkScriptpath, &bOK);
    tmpfile2 = fopen(TkScriptpathShort, "r");
    if (tmpfile2 == NULL)
    {
        sciprint(_("Unable to find Tcl initialisation scripts.\nCheck your SCI environment variable.\nTcl initialisation failed !"));
    }
    else
    {
        fclose(tmpfile2);
    }
#else
    tmpdir = opendir(SciPathShort);
    if (tmpdir == NULL)
    {
        sciprint(_("The SCI environment variable is not set.\nTcl initialisation failed !\n"));
    }
    else
    {
        closedir(tmpdir);
    }
    strcpy(TkScriptpath, SciPathShort);
    strcat(TkScriptpath, "/modules/tclsci/tcl/TK_Scilab.tcl");
    TkScriptpathShort = getshortpathname(TkScriptpath, &bOK);
    tmpfile2 = fopen(TkScriptpathShort, "r");
    if (tmpfile2 == NULL)
    {
        sciprint(_("Unable to find Tcl initialisation scripts.\nCheck your SCI environment variable.\nTcl initialisation failed !"));
    }
    else
    {
        fclose(tmpfile2);
    }
#endif /* _MSC_VER */

    if (getTclInterp() == NULL)
    {
        releaseTclInterp();
        initTclInterp();

#ifdef _MSC_VER
        /* Initialize TCL_LIBRARY & TK_LIBRARY variables environment */
        /* Windows only */
        SetTclTkEnvironment(SciPathShort);
#endif

        if ( getTclInterp() == NULL )
        {
            Scierror(999, _("Tcl Error: Unable to create Tcl interpreter (Tcl_CreateInterp).\n"));
        }
        releaseTclInterp();

        if ( Tcl_Init(getTclInterp()) == TCL_ERROR)
        {
            releaseTclInterp();
            Scierror(999, _("Tcl Error: Error during the Tcl initialization (Tcl_Init): %s\n"), Tcl_GetStringResult(getTclInterp()));
        }
        releaseTclInterp();
        if (getenv("SCI_DISABLE_TK") == NULL)
        {
            /* When SCI_DISABLE_TK is set in the env disable the TK init
             * process. It is causing issues when Scilab is
             * used through ssh.  */
            if ( Tk_Init(getTclInterp()) == TCL_ERROR)
            {
                releaseTclInterp();
                Scierror(999, _("Tcl Error: Error during the TK initialization (Tk_Init): %s\n"), Tcl_GetStringResult(getTclInterp()));
            }
            else
            {
                tkStarted = TRUE;
            }
            releaseTclInterp();
        }


        sprintf(MyCommand, "set SciPath \"%s\";", SciPathShort);

        if ( Tcl_Eval(getTclInterp(), MyCommand) == TCL_ERROR  )
        {
            releaseTclInterp();
            Scierror(999, _("Tcl Error: Error during the Scilab/Tcl init process. Could not set SciPath: %s\n"), Tcl_GetStringResult(getTclInterp()));
        }

        releaseTclInterp();
        Tcl_CreateCommand(getTclInterp(), "ScilabEval", TCL_EvalScilabCmd, (ClientData)1, NULL);
        releaseTclInterp();
    }

    if (TKmainWindow == NULL && tkStarted)
    {
        TKmainWindow = Tk_MainWindow(getTclInterp());
        releaseTclInterp();
        Tk_GeometryRequest(TKmainWindow, 2, 2);
        //printf("TkScriptpathShort : |%s|\n", TkScriptpathShort);
        if ( Tcl_EvalFile(getTclInterp(), TkScriptpathShort) == TCL_ERROR  )
        {
            releaseTclInterp();
            Scierror(999, _("Tcl Error: Error during the Scilab/TK init process. Error while loading %s: %s\n"), TkScriptpathShort, Tcl_GetStringResult(getTclInterp()));
        }
        releaseTclInterp();
    }


    if (SciPath)
    {
        FREE(SciPath);
        SciPath = NULL;
    }

    if (SciPathShort)
    {
        FREE(SciPathShort);
        SciPathShort = NULL;
    }

    if (TkScriptpathShort)
    {
        FREE(TkScriptpathShort);
        TkScriptpathShort = NULL;
    }

    // This start a periodic and endless call to "update"
    // TCL command. This causes any TCL application to start
    // and run as if it's in the main program thread.
    startTclLoop();
    return (0);

}
/*--------------------------------------------------------------------------*/
int OpenTCLsci(void)
{
    __InitSignalLock(&InterpReadyLock);
    __InitSignal(&InterpReady);
    // Open TCL interpreter in a separated thread.
    // Allows all Tcl application not to freeze nor decrease Scilab speed.
    // Causes also Scilab let those application live their own lifes.


    __CreateThread(&TclThread, &DaemonOpenTCLsci);
    // Wait to be sure initialisation is complete.
    __LockSignal(&InterpReadyLock);
    __Wait(&InterpReady, &InterpReadyLock);
    __UnLockSignal(&InterpReadyLock);

    return 0;
}
/*--------------------------------------------------------------------------*/
BOOL CloseTCLsci(void)
{
    if ( getScilabMode() != SCILAB_NWNI )
    {
        if (isTkStarted())
        {
            setTkStarted(FALSE);
            __Terminate(TclThread);
            __WaitThreadDie(TclThread);
            deleteTclInterp();
            TKmainWindow = NULL;
            return TRUE;
        }
    }
    return FALSE;
}
/*--------------------------------------------------------------------------*/
static char *GetSciPath(void)
/* force SciPath to Unix format for compatibility (Windows) */
{
    char *PathUnix = NULL;
    char *SciPathTmp = NULL;
    int i = 0;
    int lenPathUnix = 0;

    SciPathTmp = getSCIpath();

    if (SciPathTmp)
    {
        PathUnix = strdup(SciPathTmp);
        for (i = 0; i < lenPathUnix; i++)
        {
            if (PathUnix[i] == '\\')
            {
                PathUnix[i] = '/';
            }
        }
    }
    if (SciPathTmp)
    {
        FREE(SciPathTmp);
        SciPathTmp = NULL;
    }
    return PathUnix;
}
/*--------------------------------------------------------------------------*/
BOOL isTkStarted(void)
{
    return TK_Started;
}
/*--------------------------------------------------------------------------*/
void setTkStarted(BOOL isTkSet)
{
    TK_Started = isTkSet;
}
/*--------------------------------------------------------------------------*/