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