Fix crash on Scicos exiting when simulation was stoped and some scope closed
[scilab.git] / scilab / modules / scicos_blocks / src / c / cscope.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 cscope.c
23    \author Benoit Bayol
24    \version 1.0
25    \date September 2006 - January 2007
26    \brief CSCOPE is a typical scope which links its input to the simulation time but there is only one input instead of CMSCOPE
27    \see CSCOPE.sci in macros/scicos_blocks/Sinks/
28 */
29 #include <stdio.h>
30 #include "CurrentObjectsManagement.h"
31 #include "scoMemoryScope.h"
32 #include "scoWindowScope.h"
33 #include "scoMisc.h"
34 #include "scoGetProperty.h"
35 #include "scoSetProperty.h"
36 #include "scicos_block4.h"
37 #include "SetJavaProperty.h"
38
39 /** \fn cscope_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
40     \brief Function to draw or redraw the window
41 */
42 void cscope_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
43 {
44   int i;
45   double *rpar;
46   int *ipar, nipar;
47   double period;
48   int dimension;
49   double ymin, ymax, xmin, xmax;
50   int buffer_size;
51   int win_pos[2];
52   int win_dim[2];
53   int win;
54   int number_of_subwin;
55   int number_of_curves_by_subwin[1];
56   int * colors;
57
58   /*Retrieving Parameters*/
59   rpar = GetRparPtrs(block);
60   ipar = GetIparPtrs(block);
61   nipar = GetNipar(block);
62   
63   buffer_size = ipar[2]; //** this is the "standard" buffer size 
64   
65   win = ipar[0];
66   period  = rpar[3];
67   win_pos[0] = ipar[(nipar-1) - 3];
68   win_pos[1] = ipar[(nipar-1) - 2];
69   win_dim[0] = ipar[(nipar-1) - 1];
70   win_dim[1] = ipar[nipar-1];
71   dimension = 2;
72   number_of_curves_by_subwin[0] = GetInPortRows(block,1);
73
74   number_of_subwin = 1;
75   ymin = rpar[1];
76   ymax = rpar[2];
77
78
79   colors = (int*)scicos_malloc(number_of_curves_by_subwin[0]*sizeof(int));
80   for(i = 0 ; i < number_of_curves_by_subwin[0] ; i++)
81     {
82       colors[i] = ipar[3+i];
83     }
84
85   /*Allocating memory*/
86   if(firstdraw == 1)
87     {
88       scoInitScopeMemory(block->work,pScopeMemory, number_of_subwin, number_of_curves_by_subwin);
89       /*Must be placed before adding polyline or other elements*/
90       scoSetLongDrawSize(*pScopeMemory, 0, 50);
91
92       scoSetShortDrawSize(*pScopeMemory,0,buffer_size); //** set the short draw size 
93
94       scoSetPeriod(*pScopeMemory,0,period);
95     }
96
97   xmin = period*scoGetPeriodCounter(*pScopeMemory,0);
98   xmax = period*(scoGetPeriodCounter(*pScopeMemory,0)+1);
99
100   /*Creating the Scope*/
101   scoInitOfWindow(*pScopeMemory, dimension, win, win_pos, win_dim, &xmin, &xmax, &ymin, &ymax, NULL, NULL);
102   if(scoGetScopeActivation(*pScopeMemory) == 1)
103     {
104       scoAddTitlesScope(*pScopeMemory,"t","y",NULL);
105       /*Add a couple of polyline : one for the shortdraw and one for the longdraw*/
106       scoAddCoupleOfPolylines(*pScopeMemory,colors);
107       /* scoAddPolylineLineStyle(*pScopeMemory,colors); */
108     }
109   scicos_free(colors);
110         /* use only single buffering to be sure to draw on the screen */
111         sciSetJavaUseSingleBuffer(scoGetPointerScopeWindow(*pScopeMemory), TRUE);
112
113 }
114
115 /** \fn void cscope(scicos_block * block,int flag)
116     \brief the computational function
117     \param block A pointer to a scicos_block
118     \param flag An int which indicates the state of the block (init, update, ending)
119 */
120 void cscope(scicos_block * block,int flag)
121 {
122   ScopeMemory * pScopeMemory;
123   int i;
124   double t;
125   int NbrPtsShort;
126   double * u1;
127   scoGraphicalObject pShortDraw;
128
129   double d_current_real_time ; 
130
131   switch(flag) 
132     {
133     
134      case Initialization:
135       {
136         cscope_draw(block,&pScopeMemory,1);
137
138         //** Init the real time section
139           //** current real time as double [second] 
140           d_current_real_time = scoGetRealTime();
141           pScopeMemory->d_last_scope_update_time = d_current_real_time ; //** update the ds for the next step
142           //** printf("Init:Start! \n\n"); //** DEBUG ONLY ! 
143                                         
144         break;
145       }
146     
147      case StateUpdate:
148       {
149         scoRetrieveScopeMemory(block->work,&pScopeMemory);
150         if(scoGetScopeActivation(pScopeMemory) == 1)
151           {
152             t = get_scicos_time();
153             /*Retreiving Scope in the block->work*/
154             
155             /*If window has been destroyed we recreate it*/
156             if (scoGetPointerScopeWindow(pScopeMemory) == NULL)
157              {
158                cscope_draw(block,&pScopeMemory,0);
159              }
160             
161             /*Maybe we are in the end of axes so we have to draw new ones */
162             scoRefreshDataBoundsX(pScopeMemory,t);
163
164             // Cannot be factorized depends of the scope
165             u1 = GetRealInPortPtrs(block,1);
166             for (i = 0 ; i < scoGetNumberOfCurvesBySubwin(pScopeMemory,0) ; i++)
167                {
168                  pShortDraw  = scoGetPointerShortDraw(pScopeMemory,0,i);
169                  NbrPtsShort = pPOLYLINE_FEATURE(pShortDraw)->n1;
170                  pPOLYLINE_FEATURE(pShortDraw)->pvx[NbrPtsShort] = t;
171                  pPOLYLINE_FEATURE(pShortDraw)->pvy[NbrPtsShort] = u1[i];
172                  pPOLYLINE_FEATURE(pShortDraw)->n1++;
173                }
174             // End of Cannot
175
176             // Draw the Scope
177             scoDrawScopeAmplitudeTimeStyle(pScopeMemory, t); //** the scope update condition
178                                                              //** is hidden here 
179           }
180
181         break;
182       } //**-----------------------------------------------------------
183
184     case Ending:
185       {
186         scoRetrieveScopeMemory(block->work, &pScopeMemory);
187         if(scoGetScopeActivation(pScopeMemory) == 1)
188           {
189           //  sciSetUsedWindow(scoGetWindowID(pScopeMemory));
190           //  pShortDraw = sciGetCurrentFigure();
191           //  pFIGURE_FEATURE(pShortDraw)->user_data = NULL;
192           //  pFIGURE_FEATURE(pShortDraw)->size_of_user_data = 0;
193                         ///* restore double buffering */
194                         //sciSetJavaUseSingleBuffer(pShortDraw, FALSE);
195           //  scoDelCoupleOfPolylines(pScopeMemory);
196
197                         /* Check if figure is still opened, otherwise, don't try to destroy it again. */
198                         scoGraphicalObject figure = scoGetPointerScopeWindow(pScopeMemory);
199                         if (figure != NULL)
200                         {
201                                 clearUserData(figure);
202                                 /* restore double buffering */
203                                 sciSetJavaUseSingleBuffer(figure, FALSE);
204                                 scoDelCoupleOfPolylines(pScopeMemory);
205                         }
206
207           }
208         scoFreeScopeMemory(block->work, &pScopeMemory);
209         break;  
210       }
211     }
212 }