TSP: The Transport Sample Protocol



Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

gdisp_plotText.c

Go to the documentation of this file.
00001 
00043 /*
00044  * System includes.
00045  */
00046 #include <stdio.h>
00047 #include <stdlib.h>
00048 #include <assert.h>
00049 #include <string.h>
00050 
00051 
00052 /*
00053  * GDISP+ includes.
00054  */
00055 #include "gdisp_kernel.h"
00056 #include "gdisp_prototypes.h"
00057 
00058 #include "gdisp_plotText.h"
00059 
00060 
00061 /*
00062  --------------------------------------------------------------------
00063                              STATIC ROUTINES
00064  --------------------------------------------------------------------
00065 */
00066 
00067 #undef DEBUG_TEXT
00068 
00069 
00070 /*
00071  * Loop over all elements of the list.
00072  * Each element is a TSP symbol the 'sReference' parameter of
00073  * which must be decremented.
00074  */
00075 static void
00076 gdisp_dereferenceSymbolList ( GList *symbolList )
00077 {
00078 
00079   GList    *symbolItem =    (GList*)NULL;
00080   Symbol_T *symbol     = (Symbol_T*)NULL;
00081 
00082   /*
00083    * Loop over all elements of the list.
00084    * Do not forget to decrement the 'sReference' of each symbol.
00085    */
00086   if (symbolList != (GList*)NULL) {
00087 
00088     symbolItem = g_list_first(symbolList);
00089 
00090     while (symbolItem != (GList*)NULL) {
00091 
00092       symbol = (Symbol_T*)symbolItem->data;
00093 
00094       symbol->sReference--;
00095 
00096       symbolItem = g_list_next(symbolItem);
00097 
00098     }
00099 
00100     g_list_free(symbolList);
00101 
00102   }
00103 
00104 }
00105 
00106 
00107 /*
00108  * Function in order to sort symbols alphabetically' when inserting
00109  * them into the double-linked list.
00110  */
00111 static gint
00112 gdisp_sortSymbolByName(gconstpointer data1,
00113                        gconstpointer data2)
00114 {
00115 
00116   Symbol_T *symbol1 = (Symbol_T*)data1,
00117            *symbol2 = (Symbol_T*)data2;
00118 
00119   return (strcmp(symbol1->sInfo.name,symbol2->sInfo.name));
00120 
00121 }
00122 
00123 
00124 /*
00125  * Callback when a row is selected into the list.
00126  */
00127 static void
00128 gdisp_selectPlotTextRow (GtkCList       *clist,
00129                          gint            row,
00130                          gint            column,
00131                          GdkEventButton *event,
00132                          gpointer        data)
00133 {
00134 
00135   /*
00136    * Nothing by now.
00137    * Info : Kernel_T *kernel = (Kernel_T*)data;
00138    */
00139 
00140 }
00141 
00142 
00143 /*
00144  * Create a 'text plot' by providing an opaque structure to the
00145  * caller. This opaque structure will be given as an argument to all
00146  * plot function. These functions remain generic.
00147  */
00148 static void*
00149 gdisp_createPlotText (Kernel_T *kernel)
00150 {
00151 
00152   PlotText_T *plot = (PlotText_T*)NULL;
00153 
00154   /*
00155    * Dynamic allocation.
00156    */
00157   plot = g_malloc0(sizeof(PlotText_T));
00158   assert(plot);
00159 
00160 
00161   /*
00162    * Few initialisations.
00163    */
00164   plot->pttType = GD_PLOT_TEXT;
00165 
00166 
00167   /*
00168    * Create a CList with 2 columns.
00169    */
00170   plot->pttCList = gtk_clist_new(GD_SYMBOL_MAX_COLUMNS);
00171 
00172   gtk_clist_set_shadow_type(GTK_CLIST(plot->pttCList),
00173                             GTK_SHADOW_IN);
00174 
00175   gtk_clist_set_column_title(GTK_CLIST(plot->pttCList),
00176                              GD_SYMBOL_NAME_COLUMN, /* first column */
00177                              "Symbols");
00178 
00179   gtk_clist_set_column_title(GTK_CLIST(plot->pttCList),
00180                              GD_SYMBOL_VALUE_COLUMN, /* second column */
00181                              "Values");
00182 
00183   gtk_clist_set_sort_column(GTK_CLIST(plot->pttCList),
00184                             GD_SYMBOL_NAME_COLUMN); /* first column */
00185 
00186   gtk_clist_set_sort_type(GTK_CLIST(plot->pttCList),
00187                           GTK_SORT_ASCENDING);
00188 
00189   gtk_clist_set_auto_sort(GTK_CLIST(plot->pttCList),
00190                           TRUE); /* Auto-sort is allowed */
00191 
00192   gtk_clist_set_button_actions(GTK_CLIST(plot->pttCList),
00193                                0, /* left button */
00194                                GTK_BUTTON_SELECTS);
00195 
00196   gtk_clist_set_selection_mode(GTK_CLIST(plot->pttCList),
00197                                GTK_SELECTION_EXTENDED);
00198 
00199   gtk_clist_column_titles_show(GTK_CLIST(plot->pttCList));
00200 
00201   gtk_signal_connect(GTK_OBJECT(plot->pttCList),
00202                      "select-row",
00203                      gdisp_selectPlotTextRow,
00204                      (gpointer)kernel);
00205 
00206   gtk_object_set_data(GTK_OBJECT(plot->pttCList),
00207                       "plotPointer",
00208                       (gpointer)plot);
00209 
00210 
00211   /*
00212    * GTK Style from default style.
00213    */
00214   plot->pttStyle = gtk_style_copy(gtk_widget_get_default_style());
00215 
00216   plot->pttStyle->base[GTK_STATE_NORMAL]   = kernel->colors[_BLACK_ ];
00217   plot->pttStyle->fg  [GTK_STATE_NORMAL]   = kernel->colors[_WHITE_];
00218 
00219   plot->pttStyle->base[GTK_STATE_ACTIVE]   = kernel->colors[_BLACK_ ];
00220   plot->pttStyle->fg  [GTK_STATE_ACTIVE]   = kernel->colors[_WHITE_];
00221 
00222   plot->pttStyle->base[GTK_STATE_SELECTED] = kernel->colors[_WHITE_];
00223   plot->pttStyle->fg  [GTK_STATE_SELECTED] = kernel->colors[_BLACK_ ];
00224   plot->pttStyle->bg  [GTK_STATE_SELECTED] = kernel->colors[_WHITE_];
00225 
00226   gtk_widget_set_style(plot->pttCList,
00227                        plot->pttStyle);
00228 
00229   /*
00230    * Return the opaque structure.
00231    */
00232   return (void*)plot;
00233 
00234 }
00235 
00236 
00237 /*
00238  * Destroy a 'text' plot opaque structure.
00239  */
00240 static void
00241 gdisp_destroyPlotText(Kernel_T *kernel,
00242                       void     *data)
00243 {
00244 
00245   PlotText_T *plot = (PlotText_T*)data;
00246 
00247 
00248   /*
00249    * Free symbol list.
00250    */
00251   gdisp_dereferenceSymbolList(plot->pttSymbolList);
00252 
00253 
00254   /*
00255    * Now destroy everything.
00256    */
00257   if (plot->pttStyle != (GtkStyle*)NULL) {
00258     gtk_style_unref(plot->pttStyle);
00259   }
00260   gtk_widget_destroy(plot->pttCList);
00261 
00262 
00263   /*
00264    * Free opaque structure.
00265    */
00266   memset(plot,0,sizeof(PlotText_T));
00267   g_free(plot);
00268 
00269 }
00270 
00271 
00272 /*
00273  * Record the parent widget.
00274  */
00275 static void
00276 gdisp_setPlotTextParent (Kernel_T  *kernel,
00277                          void      *data,
00278                          GtkWidget *parent)
00279 {
00280 
00281   PlotText_T *plot = (PlotText_T*)data;
00282 
00283   /*
00284    * Remember my parent.
00285    */
00286   plot->pttParent = parent;
00287 
00288 }
00289 
00290 
00291 /*
00292  * Record initial dimensions provided by the calling process.
00293  */
00294 static void
00295 gdisp_setPlotTextInitialDimensions (Kernel_T *kernel,
00296                                     void     *data,
00297                                     guint     width,
00298                                     guint     height)
00299 {
00300 
00301   PlotText_T *plot = (PlotText_T*)data;
00302 
00303   /*
00304    * Remember my initial width and height.
00305    */
00306   plot->pttCListWidth  = width;
00307   plot->pttCListHeight = height;
00308 
00309   if (plot->pttCList != (GtkWidget*)NULL) {
00310 
00311     /*
00312      * What however is important, is that we set the column widths as
00313      * they will never be right otherwise.
00314      * Note that the columns are numbered from 0 and up (to 2 in our case).
00315      */
00316     gtk_clist_set_column_width(GTK_CLIST(plot->pttCList),
00317                                GD_SYMBOL_NAME_COLUMN, /* first column */
00318                                2 * width / 3);
00319 
00320     gtk_clist_set_column_width(GTK_CLIST(plot->pttCList),
00321                                GD_SYMBOL_VALUE_COLUMN, /* second column */
00322                                1 * width / 3);
00323 
00324   }
00325 
00326 }
00327 
00328 
00329 /*
00330  * Give back to the calling process the top level widget
00331  * in order to be inserted in a possible container for further
00332  * dynamic X management.
00333  */
00334 static GtkWidget*
00335 gdisp_getPlotTextTopLevelWidget (Kernel_T  *kernel,
00336                                  void      *data)
00337 {
00338 
00339   PlotText_T *plot = (PlotText_T*)data;
00340 
00341 #if defined(DEBUG_TEXT)
00342   fprintf(stdout,"Getting back text plot top level widget.\n");
00343   fflush (stdout);
00344 #endif
00345 
00346   return (GtkWidget*)plot->pttCList;
00347 
00348 }
00349 
00350 
00351 /*
00352  * By now, the 'text plot' widgets are created, but not shown yet.
00353  * Show them here.
00354  */
00355 static void
00356 gdisp_showPlotText (Kernel_T  *kernel,
00357                     void      *data)
00358 {
00359 
00360   PlotText_T *plot = (PlotText_T*)data;
00361 
00362 #if defined(DEBUG_TEXT)
00363   fprintf(stdout,"Showing text plot.\n");
00364   fflush (stdout);
00365 #endif
00366 
00367   /*
00368    * Now show everything.
00369    */
00370   gtk_widget_show(plot->pttCList);
00371 
00372 }
00373 
00374 
00375 /*
00376  * Return to calling process what king of plot we are.
00377  */
00378 static PlotType_T
00379 gdisp_getPlotTextType (Kernel_T *kernel,
00380                        void     *data)
00381 {
00382 
00383   PlotText_T *plot = (PlotText_T*)data;
00384 
00385 #if defined(DEBUG_TEXT)
00386   fprintf(stdout,"Getting back text plot type.\n");
00387 #endif
00388 
00389   /*
00390    * Must be GD_PLOT_TEXT. See 'create' routine.
00391    */
00392   return plot->pttType;
00393 
00394 }
00395 
00396 
00397 /*
00398  * Record any incoming symbols.
00399  */
00400 static void
00401 gdisp_addSymbolsToPlotText (Kernel_T *kernel,
00402                             void     *data,
00403                             GList    *symbolList,
00404                             guchar    zoneId)
00405 {
00406 
00407   PlotText_T *plot       = (PlotText_T*)data;
00408   GList      *symbolItem =      (GList*)NULL;
00409   Symbol_T   *symbol     =   (Symbol_T*)NULL;
00410   gint        rowNumber  = 0;
00411   gchar       sValue [128];
00412   gchar      *rowInfo[GD_SYMBOL_MAX_COLUMNS];
00413 
00414 #if defined(DEBUG_TEXT)
00415   fprintf(stdout,"Adding symbols to text plot.\n");
00416   fflush (stdout);
00417 #endif
00418 
00419   /*
00420    * Incoming symbols are to be attached to the list.
00421    */
00422   rowInfo[1] = (gchar*)sValue;
00423   gtk_clist_freeze(GTK_CLIST(plot->pttCList));
00424 
00425   /*
00426    * Loop over all incoming symbols and store only those that
00427    * are not already in the final list.
00428    */
00429   symbolItem = g_list_first(symbolList);
00430   while (symbolItem != (GList*)NULL) {
00431 
00432     if (g_list_find(plot->pttSymbolList,symbolItem->data) == (GList*)NULL) {
00433 
00434       plot->pttSymbolList = g_list_append(plot->pttSymbolList,
00435                                           symbolItem->data);
00436 
00437       /*
00438        * Do not forget to increment the reference of the Y symbol.
00439        */
00440       symbol = (Symbol_T*)symbolItem->data;
00441       symbol->sReference++;
00442 
00443       /*
00444        * Insert symbol into the graphic list.
00445        */
00446       rowInfo[0] = symbol->sInfo.name;
00447       sprintf(rowInfo[1],"%f ",symbol->sLastValue);
00448 
00449       rowNumber = gtk_clist_append(GTK_CLIST(plot->pttCList),
00450                                    rowInfo);
00451 
00452       gtk_clist_set_row_data(GTK_CLIST(plot->pttCList),
00453                              rowNumber,
00454                              (gpointer)symbol);
00455 
00456     }
00457 
00458     symbolItem = g_list_next(symbolItem);
00459 
00460   }
00461 
00462   /*
00463    * Sort symbols by name.
00464    */
00465   plot->pttSymbolList = g_list_sort(plot->pttSymbolList,
00466                                     gdisp_sortSymbolByName);
00467 
00468   gtk_clist_thaw(GTK_CLIST(plot->pttCList));
00469 
00470 }
00471 
00472 
00473 /*
00474  * Broadcast all symbols.
00475  */
00476 static GList*
00477 gdisp_getSymbolsFromPlotText (Kernel_T *kernel,
00478                               void     *data,
00479                               gchar     axis)
00480 {
00481 
00482   PlotText_T *plot = (PlotText_T*)data;
00483 
00484 #if defined(DEBUG_TEXT)
00485   fprintf(stdout,"Give back the list of symbols handled by the text plot.\n");
00486   fflush (stdout);
00487 #endif
00488 
00489   return plot->pttSymbolList;
00490 
00491 }
00492 
00493 
00494 /*
00495  * Real time Starting Step Action.
00496  */
00497 static gboolean
00498 gdisp_startStepOnPlotText (Kernel_T *kernel,
00499                            void     *data)
00500 {
00501 
00502   /*
00503    * Nothing to be done on text plot, except that we must
00504    * return TRUE to the calling procedure in order to allow the general
00505    * step management to proceed.
00506    *
00507    * Returning FALSE means that our plot is not enabled to perform its
00508    * step operations, because of this or that...
00509    */
00510   return TRUE;
00511 
00512 }
00513 
00514 
00515 /*
00516  * Real time Step Action.
00517  */
00518 static void
00519 gdisp_stepOnPlotText (Kernel_T *kernel,
00520                       void     *data)
00521 {
00522 
00523   PlotText_T *plot = (PlotText_T*)data;
00524   gchar       sValue [128];
00525   GList      *symbolItem =      (GList*)NULL;
00526   Symbol_T   *symbol     =   (Symbol_T*)NULL;
00527   gint        rowNumber  = 0;
00528 
00529 
00530   /*
00531    * Loop over all symbols.
00532    */
00533   symbolItem = g_list_first(plot->pttSymbolList);
00534   while (symbolItem != (GList*)NULL) {
00535 
00536     symbol = (Symbol_T*)symbolItem->data;
00537 
00538     if (symbol->sHasChanged == TRUE) {
00539 
00540       /*
00541        * Insert symbol into the graphic list.
00542        */
00543       sprintf(sValue,"%g ",symbol->sLastValue);
00544 
00545       gtk_clist_set_text(GTK_CLIST(plot->pttCList),
00546                          rowNumber,
00547                          GD_SYMBOL_VALUE_COLUMN,
00548                          sValue);
00549 
00550 #if defined(TRY_PIXMAP)
00551 
00552       gtk_clist_set_pixtext(GTK_CLIST(plot->pttCList),
00553                             rowNumber,
00554                             GD_SYMBOL_NAME_COLUMN,
00555                             symbol->sInfo.name,
00556                             10, /* spacing */
00557                             pixmap,
00558                             mask);
00559 
00560 #endif
00561 
00562     }
00563 
00564     rowNumber++;
00565 
00566     symbolItem = g_list_next(symbolItem);
00567 
00568   }
00569 
00570 }
00571 
00572 
00573 /*
00574  * Real time Starting Step Action.
00575  */
00576 static void
00577 gdisp_stopStepOnPlotText (Kernel_T *kernel,
00578                           void     *data)
00579 {
00580 
00581   /*
00582    * Nothing to be done on default plot.
00583    */
00584 
00585 }
00586 
00587 
00588 /*
00589  * Get back to the calling procedure my information.
00590  */
00591 static void
00592 gdisp_getPlotTextInformation (Kernel_T         *kernel,
00593                               PlotSystemInfo_T *information)
00594 {
00595 
00596 #include "pixmaps/gdisp_textLogo.xpm"
00597 
00598   /*
00599    *   - Name,
00600    *   - Formula,
00601    *   - Descripton for tooltip purpose.
00602    */
00603   information->psName        = "Text Plot";
00604   information->psFormula     = "Value = F ( Time )";
00605   information->psDescription = "A typical textual plot that shows the "
00606     "evolution of several symbols by printing the last known value.";
00607   information->psLogo        = gdisp_textLogo;
00608 
00609 }
00610 
00611 
00612 /*
00613  * Get back to the calling procedure my period, expressed in milliseconds.
00614  * CAUTION : The period must be an exact multiple of 10.
00615  *           Should not be lower than 100.
00616  */
00617 static guint
00618 gdisp_getPlotTextPeriod (Kernel_T *kernel,
00619                          void     *data)
00620 {
00621 
00622   /*
00623    * My period is 1000 milli-seconds.
00624    * FIXME : This value is hardcoded. It should be easily modifiable.
00625    * But how ?
00626    */
00627   return 1000;
00628 
00629 }
00630 
00631 
00632 /*
00633  * This procedure is called whenever all symbols have been time-tagged
00634  * by the corresponding provider sampling thread.
00635  * The last value of all symbols can now be retreived by the graphic plot.
00636  *
00637  * CAUTION : This procedure is called in another thread, compared to all
00638  * other procedures of the graphic plot that are called by GTK main thread.
00639  */
00640 static void
00641 gdisp_treatPlotTextSymbolValues (Kernel_T *kernel,
00642                                  void     *data)
00643 {
00644 
00645   /*
00646    * Nothing to be done here for text plot, because the last
00647    * value of all symbols is retrieved in the "step" procedure.
00648    */
00649 
00650 }
00651 
00652 
00653 /*
00654  * Get back the zones that have been defined on that plot.
00655  */
00656 static GArray*
00657 gdisp_getPlotTextDropZones (Kernel_T *kernel)
00658 {
00659 
00660   /*
00661    * No zones on text plots.
00662    */
00663   return (GArray*)NULL;
00664 
00665 }
00666 
00667 
00668 /*
00669  --------------------------------------------------------------------
00670                              PUBLIC ROUTINES
00671  --------------------------------------------------------------------
00672 */
00673 
00674 
00675 void
00676 gdisp_initPlotTextSystem (Kernel_T     *kernel,
00677                           PlotSystem_T *plotSystem)
00678 {
00679 
00680   /*
00681    * We must here provide all 'PlotText' private functions
00682    * that remain 'static' here, but accessible from everywhere
00683    * via the kernel.
00684    */
00685   plotSystem->psCreate            = gdisp_createPlotText;
00686   plotSystem->psDestroy           = gdisp_destroyPlotText;
00687   plotSystem->psSetParent         = gdisp_setPlotTextParent;
00688   plotSystem->psGetTopLevelWidget = gdisp_getPlotTextTopLevelWidget;
00689   plotSystem->psShow              = gdisp_showPlotText;
00690   plotSystem->psGetType           = gdisp_getPlotTextType;
00691   plotSystem->psAddSymbols        = gdisp_addSymbolsToPlotText;
00692   plotSystem->psGetSymbols        = gdisp_getSymbolsFromPlotText;
00693   plotSystem->psSetDimensions     = gdisp_setPlotTextInitialDimensions;
00694   plotSystem->psStartStep         = gdisp_startStepOnPlotText;
00695   plotSystem->psStep              = gdisp_stepOnPlotText;
00696   plotSystem->psStopStep          = gdisp_stopStepOnPlotText;
00697   plotSystem->psGetInformation    = gdisp_getPlotTextInformation;
00698   plotSystem->psTreatSymbolValues = gdisp_treatPlotTextSymbolValues;
00699   plotSystem->psGetPeriod         = gdisp_getPlotTextPeriod;
00700   plotSystem->psGetDropZones      = gdisp_getPlotTextDropZones;
00701 
00702 }
00703 
00704 
Framework Home Page.

Beware !! TSP wave is coming...