Fix crash on Scicos exiting when simulation was stoped and some scope closed
[scilab.git] / scilab / modules / scicos_blocks / src / c / cevscpe.c
1 /*  Scicos
2 *
3 *  Copyright (C) INRIA - METALAU Project <scicos@inria.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 * See the file ./license.txt
20 */
21 /**
22    \file cevscpe.c
23    \author Benoit Bayol
24    \version 1.0
25    \date September 2006 - January 2007
26    \brief CEVSCPE is a scope that indicates when the clocks is activated
27    \see CEVENTSCOPE.sci in macros/scicos_blocks/Sinks/
28 */
29 #include "CurrentObjectsManagement.h"
30 #include "scoMemoryScope.h"
31 #include "scoWindowScope.h"
32 #include "scoMisc.h"
33 #include "scoGetProperty.h"
34 #include "scoSetProperty.h"
35 #include "scicos_block4.h"
36 #include "DrawingBridge.h"
37 #include "SetJavaProperty.h"
38
39 /** \fn cscopxy_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
40     \brief Function to draw or redraw the window
41 */
42 void cevscpe_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
43 {
44   /* Declarations */
45
46   int nipar; //Number of elements in ipar vector
47   int i; //As usual
48   int * ipar;
49   double * rpar; //Integer Parameter
50   int nbr_colors; //Number of colors and lines IS ALSO number of channels
51   int win; //To give a name to the window
52   int color_flag; //0/1 color flag -- NOT USED
53   int  * colors; //Begin at ipar[2] and has a measure of 8 max
54   int dimension = 2;
55   double period; //Refresh Period of the scope is a vector here
56   int number_of_subwin;
57   int number_of_curves_by_subwin;
58   double xmin, xmax, ymin, ymax;
59   int win_pos[2], win_dim[2];
60
61   /* Initialization */
62   ipar =  GetIparPtrs(block);
63   win = ipar[0];
64   color_flag = ipar[1]; /*not used*/
65   rpar = GetRparPtrs(block);
66   period = rpar[0];
67   nipar = GetNipar(block);
68   nbr_colors = nipar-6;
69   colors=(int*)scicos_malloc(nbr_colors*sizeof(int));
70   for( i = 2 ; i < nbr_colors+2 ; i++)
71     {
72       colors[i-2] = ipar[i];
73     }
74
75   number_of_subwin = 1;
76   number_of_curves_by_subwin = nbr_colors;
77
78   ymin = 0;
79   ymax = 1;
80
81   win_pos[0] = ipar[(nipar-1) - 3];
82   win_pos[1] = ipar[(nipar-1) - 2];
83   win_dim[0] = ipar[(nipar-1) - 1];
84   win_dim[1] = ipar[nipar-1];
85
86   if(firstdraw == 1)
87     {
88       scoInitScopeMemory(block->work,pScopeMemory, number_of_subwin, &number_of_curves_by_subwin);
89       scoSetLongDrawSize(*pScopeMemory,0,5000);
90       scoSetShortDrawSize(*pScopeMemory,0,1);
91       scoSetPeriod(*pScopeMemory,0,period);
92     }
93
94   xmin = period*scoGetPeriodCounter(*pScopeMemory,0);
95   xmax = period*(scoGetPeriodCounter(*pScopeMemory,0)+1);
96
97   scoInitOfWindow(*pScopeMemory, dimension, win, win_pos, win_dim, &xmin, &xmax, &ymin, &ymax, NULL, NULL);
98   if(scoGetScopeActivation(*pScopeMemory) == 1)
99     {
100       scoAddTitlesScope(*pScopeMemory,"t","y",NULL);
101       scoAddCoupleOfSegments(*pScopeMemory,colors);
102     }
103   scicos_free(colors);
104
105         /* use only single buffering to be sure to draw on the screen */
106         sciSetJavaUseSingleBuffer(scoGetPointerScopeWindow(*pScopeMemory), TRUE);
107 }
108
109 /** \fn void cevscpe(scicos_block * block, int flag)
110     \brief the computational function
111     \param block A pointer to a scicos_block
112     \param flag An int which indicates the state of the block (init, update, ending)
113 */
114 void cevscpe(scicos_block * block, int flag)
115 {
116
117   ScopeMemory * pScopeMemory;
118   int nbseg = 0;
119   int tab[20];
120   scoGraphicalObject pShortDraw, pLongDraw;
121   int i;
122   double t;
123
124   switch(flag)
125     {
126     case Initialization:
127       {
128         cevscpe_draw(block,&pScopeMemory,1);
129         break;
130       }
131
132     case StateUpdate:
133       {
134
135         /* Charging elements */
136
137         scoRetrieveScopeMemory(block->work,&pScopeMemory);
138
139         if(scoGetScopeActivation(pScopeMemory) == 1)
140           {
141            
142             t = get_scicos_time();
143             if(scoGetPointerScopeWindow(pScopeMemory) == NULL)
144               {
145                 cevscpe_draw(block,&pScopeMemory,0);
146               }
147
148             scoRefreshDataBoundsX(pScopeMemory,t);
149         
150             /*Not Factorize*/
151
152             for(i = 0 ; i < scoGetNumberOfCurvesBySubwin(pScopeMemory,0) ; i++)
153               {
154                 if((GetNevIn(block)&(1<<i))==(1<<i))
155                   {
156                     tab[nbseg]=i;
157                     nbseg++;
158                   }
159               }
160
161             for(i = 0 ; i < nbseg ; i++)
162               {
163                 pShortDraw = scoGetPointerShortDraw(pScopeMemory,0,tab[i]);
164                 pSEGS_FEATURE(pShortDraw)->vx[0] = t;
165                 pSEGS_FEATURE(pShortDraw)->vx[1] = t;
166                 pSEGS_FEATURE(pShortDraw)->vy[0] = i*0.8/nbseg;
167                 pSEGS_FEATURE(pShortDraw)->vy[1] = (i+1)*0.8/nbseg;
168                 pSEGS_FEATURE(pShortDraw)->Nbr1 = 2;
169                 pSEGS_FEATURE(pShortDraw)->Nbr2 = 2;
170               }
171             /*End of Not Factorize*/
172             scoDrawScopeAmplitudeTimeStyle(pScopeMemory,t);
173           }
174         break;
175       }
176
177     case Ending:
178       {
179
180                                 scoRetrieveScopeMemory(block->work, &pScopeMemory);
181                                 if(scoGetScopeActivation(pScopeMemory) == 1)
182                                 {
183                                         /* sciSetUsedWindow(scoGetWindowID(pScopeMemory)); */
184                                         /* Check if figure is still opened, otherwise, don't try to destroy it again. */
185                                         if(scoGetPointerScopeWindow(pScopeMemory) != NULL)
186                                         {
187                                                 for(i = 0 ; i < scoGetNumberOfCurvesBySubwin(pScopeMemory,0) ; i++)
188                                                 {
189                                                         /* maybe a bug here in the last argument of the following instruction (see tab[i]) */
190                                                         pLongDraw = scoGetPointerLongDraw(pScopeMemory,0,i);
191                                                         forceRedraw(pLongDraw);
192                                                 }
193                                         
194
195                                                 /* pShortDraw = sciGetCurrentFigure(); */
196                                                 pShortDraw = scoGetPointerScopeWindow(pScopeMemory);
197                                                 clearUserData(pShortDraw);
198                                                 /* pFIGURE_FEATURE(pShortDraw)->user_data = NULL; */
199                                                 /* pFIGURE_FEATURE(pShortDraw)->size_of_user_data = 0; */
200                                                 /* restore double buffering */
201                                                 sciSetJavaUseSingleBuffer(pShortDraw, FALSE);
202                                                 scoDelCoupleOfSegments(pScopeMemory);
203                                         }
204                                 }
205                                 scoFreeScopeMemory(block->work,&pScopeMemory);
206                                 break;
207                         }
208         }
209 }