slk.c

Go to the documentation of this file.
00001 #include <curses.h>
00002 #include <stddef.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 #include <assert.h>
00006 #include "mucurses.h"
00007 #include "cursor.h"
00008 
00009 /** @file
00010  *
00011  * Soft label key functions
00012  */
00013 
00014 #define MIN_SPACE_SIZE 2
00015 
00016 #define SLK_MAX_LABEL_LEN 8
00017 
00018 #define SLK_MAX_NUM_LABELS 12
00019 
00020 #define SLK_MAX_NUM_SPACES 2
00021 
00022 struct _softlabel {
00023         // label string
00024         char label[SLK_MAX_LABEL_LEN];
00025         /* Format of soft label 
00026            0: left justify
00027            1: centre justify
00028            2: right justify
00029          */
00030         unsigned int fmt;
00031 };
00032 
00033 struct _softlabelkeys {
00034         struct _softlabel fkeys[SLK_MAX_NUM_LABELS];
00035         attr_t attrs;
00036         /* Soft label layout format
00037            0: 3-2-3
00038            1: 4-4
00039            2: 4-4-4
00040            3: 4-4-4 with index line
00041         */
00042         unsigned int fmt;
00043         unsigned int max_label_len;
00044         unsigned int maj_space_len;
00045         unsigned int num_labels;
00046         unsigned int num_spaces;
00047         unsigned int spaces[SLK_MAX_NUM_SPACES];
00048         struct cursor_pos saved_cursor;
00049         attr_t saved_attrs;
00050         short saved_pair;
00051 };
00052 
00053 static struct _softlabelkeys *slks;
00054 
00055 /*
00056   I either need to break the primitives here, or write a collection of
00057   functions specifically for SLKs that directly access the screen
00058   functions - since this technically isn't part of stdscr, I think
00059   this should be ok...
00060  */
00061 
00062 static void _enter_slk ( void ) {
00063         _store_curs_pos ( stdscr, &slks->saved_cursor );
00064         wattr_get ( stdscr, &slks->saved_attrs, &slks->saved_pair, NULL );
00065         LINES++;
00066         wmove ( stdscr, LINES, 0 );
00067         wattrset ( stdscr, slks->attrs );
00068 }
00069 
00070 static void _leave_slk ( void ) {
00071         LINES--;
00072         wattr_set ( stdscr, slks->saved_attrs, slks->saved_pair, NULL );
00073         _restore_curs_pos ( stdscr, &slks->saved_cursor );
00074 }
00075 
00076 static void _print_label ( struct _softlabel sl ) {
00077         int space_ch;
00078         char str[SLK_MAX_LABEL_LEN + 1];
00079 
00080         assert ( slks->max_label_len <= SLK_MAX_LABEL_LEN );
00081         space_ch = ' ';
00082 
00083         // protect against gaps in the soft label keys array
00084         if ( sl.label == NULL ) {
00085                 memset( str, space_ch, (size_t)(slks->max_label_len) );
00086         } else {
00087                 /* we need to pad the label with varying amounts of leading
00088                    pad depending on the format of the label */
00089                 if ( sl.fmt == 1 ) {
00090                         memset( str, space_ch, 
00091                                 (size_t)(slks->max_label_len 
00092                                          - strlen(sl.label)) / 2 );
00093                 }
00094                 if ( sl.fmt == 2 ) {
00095                         memset( str, space_ch,
00096                                 (size_t)(slks->max_label_len 
00097                                          - strlen(sl.label)) );
00098                 }
00099                 strcat(str,sl.label);
00100                 
00101                 // post-padding
00102                 memset(str+strlen(str), space_ch,
00103                        (size_t)(slks->max_label_len - strlen(str)) );
00104         }
00105 
00106         // print the formatted label
00107         _wputstr ( stdscr, str, NOWRAP, slks->max_label_len );
00108 }
00109 
00110 /**
00111  * Return the attribute used for the soft function keys
00112  *
00113  * @ret attrs   the current attributes of the soft function keys
00114  */
00115 attr_t slk_attr ( void ) {
00116         return ( slks == NULL ? 0 : slks->attrs );
00117 }
00118 
00119 /**
00120  * Turn off soft function key attributes
00121  *
00122  * @v attrs     attribute bit mask
00123  * @ret rc      return status code
00124  */
00125 int slk_attroff ( const chtype attrs ) {
00126         if ( slks == NULL ) 
00127                 return ERR;
00128         slks->attrs &= ~( attrs & A_ATTRIBUTES );
00129         return OK;
00130 }
00131 
00132 /**
00133  * Turn on soft function key attributes
00134  *
00135  * @v attrs     attribute bit mask
00136  * @ret rc      return status code
00137  */
00138 int slk_attron ( const chtype attrs ) {
00139         if ( slks == NULL )
00140                 return ERR;
00141         slks->attrs |= ( attrs & A_ATTRIBUTES );
00142         return OK;
00143 }
00144 
00145 /**
00146  * Set soft function key attributes
00147  *
00148  * @v attrs     attribute bit mask
00149  * @ret rc      return status code
00150  */
00151 int slk_attrset ( const chtype attrs ) {
00152         if ( slks == NULL ) 
00153                 return ERR;
00154         slks->attrs = ( attrs & A_ATTRIBUTES );
00155         return OK;
00156 }
00157 
00158 /**
00159  * Turn off soft function key attributes
00160  *
00161  * @v attrs     attribute bit mask
00162  * @v *opts     undefined (for future implementation)
00163  * @ret rc      return status code
00164  */
00165 int slk_attr_off ( const attr_t attrs, void *opts __unused ) {
00166         return slk_attroff( attrs );
00167 }
00168 
00169 /**
00170  * Turn on soft function key attributes
00171  *
00172  * @v attrs     attribute bit mask
00173  * @v *opts     undefined (for future implementation)
00174  * @ret rc      return status code
00175  */
00176 int slk_attr_on ( attr_t attrs, void *opts __unused ) {
00177         return slk_attron( attrs );
00178 }
00179 
00180 /**
00181  * Set soft function key attributes
00182  *
00183  * @v attrs                     attribute bit mask
00184  * @v colour_pair_number        colour pair integer
00185  * @v *opts                     undefined (for future implementation)
00186  * @ret rc                      return status code
00187  */
00188 int slk_attr_set ( const attr_t attrs, short colour_pair_number,
00189                    void *opts __unused ) {
00190         if ( slks == NULL ) 
00191                 return ERR;
00192 
00193         if ( ( unsigned short )colour_pair_number > COLORS )
00194                 return ERR;
00195 
00196         slks->attrs = ( (unsigned short)colour_pair_number << CPAIR_SHIFT ) |
00197                 ( attrs & A_ATTRIBUTES );
00198         return OK;
00199 }
00200 
00201 /**
00202  * Clear the soft function key labels from the screen
00203  *
00204  * @ret rc      return status code
00205  */
00206 int slk_clear ( void ) {
00207         if ( slks == NULL )
00208                 return ERR;
00209 
00210         _enter_slk();
00211         wclrtoeol ( stdscr );
00212         _leave_slk();
00213 
00214         return OK;
00215 }
00216 
00217 /**
00218  * Set soft label colour pair
00219  */
00220 int slk_colour ( short colour_pair_number ) {
00221         if ( slks == NULL ) 
00222                 return ERR;
00223         if ( ( unsigned short )colour_pair_number > COLORS )
00224                 return ERR;
00225 
00226         slks->attrs = ( (unsigned short)colour_pair_number << CPAIR_SHIFT )
00227                 | ( slks->attrs & A_ATTRIBUTES );
00228 
00229         return OK;
00230 }
00231 
00232 /**
00233  * Initialise the soft function keys
00234  *
00235  * @v fmt       format of keys
00236  * @ret rc      return status code
00237  */
00238 int slk_init ( int fmt ) {
00239         unsigned short nmaj, nmin, nblocks, available_width;
00240 
00241         if ( (unsigned)fmt > 3 ) {
00242                 return ERR;
00243         }
00244 
00245         /* There seems to be no API call to free this data structure... */
00246         if ( ! slks )
00247                 slks = calloc(1,sizeof(*slks));
00248         if ( ! slks )
00249                 return ERR;
00250 
00251         slks->attrs = A_DEFAULT;
00252         slks->fmt = fmt;
00253         switch(fmt) {
00254         case 0:
00255                 nblocks = 8; nmaj = 2; nmin = 5;
00256                 slks->spaces[0] = 2; slks->spaces[1] = 4;
00257                 break;
00258         case 1:
00259                 nblocks = 8; nmaj = 1; nmin = 6;
00260                 slks->spaces[0] = 3;
00261                 break;
00262         case 2:
00263                 // same allocations as format 3
00264         case 3:
00265                 nblocks = 12; nmaj = 2; nmin = 9;
00266                 slks->spaces[0] = 3; slks->spaces[1] = 7;
00267                 break;
00268         default:
00269                 nblocks = 0; nmaj = 0; nmin = 0;
00270                 break;
00271         }
00272 
00273         // determine maximum label length and major space size
00274         available_width = COLS - ( ( MIN_SPACE_SIZE * nmaj ) + nmin );
00275         slks->max_label_len = available_width / nblocks;
00276         slks->maj_space_len = MIN_SPACE_SIZE + 
00277                 ( available_width % nblocks ) / nmaj;
00278         slks->num_spaces = nmaj;
00279         slks->num_labels = nblocks;
00280 
00281         // strip a line from the screen
00282         LINES -= 1;
00283 
00284         return OK;
00285 }
00286 
00287 /**
00288  * Return the label for the specified soft key
00289  *
00290  * @v labnum    soft key identifier
00291  * @ret label   return label
00292  */
00293 char* slk_label ( int labnum ) {
00294         if ( slks == NULL ) 
00295                 return NULL;
00296 
00297         return slks->fkeys[labnum].label;
00298 }
00299 
00300 /**
00301  * Restore soft function key labels to the screen
00302  *
00303  * @ret rc      return status code
00304  */
00305 int slk_restore ( void ) {
00306         unsigned int i, j, pos_x,
00307                 *next_space, *last_space;
00308         chtype space_ch;
00309 
00310         if ( slks == NULL )
00311                 return ERR;
00312 
00313         pos_x = 0;
00314 
00315         _enter_slk();
00316 
00317         space_ch = (chtype)' ' | slks->attrs;
00318         next_space = &(slks->spaces[0]);
00319         last_space = &(slks->spaces[slks->num_spaces-1]);
00320 
00321         for ( i = 0; i < slks->num_labels ; i++ ) {
00322                 _print_label( slks->fkeys[i] );
00323                 pos_x += slks->max_label_len;
00324 
00325                 if ( i == *next_space ) {
00326                         for ( j = 0; j < slks->maj_space_len; j++, pos_x++ )
00327                                 _wputch ( stdscr, space_ch, NOWRAP );
00328                         if ( next_space < last_space )
00329                                 next_space++;
00330                 } else {
00331                         if ( pos_x < COLS )
00332                                 _wputch ( stdscr, space_ch, NOWRAP );
00333                         pos_x++;
00334                 }
00335         }
00336 
00337         _leave_slk();
00338 
00339         return OK;
00340 }
00341 
00342 /**
00343  * Configure specified soft key
00344  *
00345  * @v labnum    soft label position to configure
00346  * @v *label    string to use as soft key label
00347  * @v fmt       justification format of label
00348  * @ret rc      return status code
00349  */
00350 int slk_set ( int labnum, const char *label, int fmt ) {
00351         if ( slks == NULL ) 
00352                 return ERR;
00353         if ( (unsigned short)labnum >= slks->num_labels )
00354                 return ERR;
00355         if ( (unsigned short)fmt >= 3 )
00356                 return ERR;
00357 
00358         strncpy(slks->fkeys[labnum].label, label,
00359                 sizeof(slks->fkeys[labnum].label));
00360         slks->fkeys[labnum].fmt = fmt;
00361 
00362         return OK;
00363 }

Generated on Tue Apr 6 20:01:06 2010 for gPXE by  doxygen 1.5.7.1