iPXE
Data Structures | Defines | Functions | Variables
slk.c File Reference

Soft label key functions. More...

#include <curses.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "mucurses.h"
#include "cursor.h"

Go to the source code of this file.

Data Structures

struct  _softlabel
struct  _softlabelkeys

Defines

#define MIN_SPACE_SIZE   2
#define SLK_MAX_LABEL_LEN   8
#define SLK_MAX_NUM_LABELS   12
#define SLK_MAX_NUM_SPACES   2

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static void _enter_slk (void)
static void _leave_slk (void)
static void _print_label (struct _softlabel sl)
attr_t slk_attr (void)
 Return the attribute used for the soft function keys.
int slk_attroff (const chtype attrs)
 Turn off soft function key attributes.
int slk_attron (const chtype attrs)
 Turn on soft function key attributes.
int slk_attrset (const chtype attrs)
 Set soft function key attributes.
int slk_attr_off (const attr_t attrs, void *opts __unused)
 Turn off soft function key attributes.
int slk_attr_on (attr_t attrs, void *opts __unused)
 Turn on soft function key attributes.
int slk_attr_set (const attr_t attrs, short colour_pair_number, void *opts __unused)
 Set soft function key attributes.
int slk_clear (void)
 Clear the soft function key labels from the screen.
int slk_colour (short colour_pair_number)
 Set soft label colour pair.
int slk_init (int fmt)
 Initialise the soft function keys.
char * slk_label (int labnum)
 Return the label for the specified soft key.
int slk_restore (void)
 Restore soft function key labels to the screen.
int slk_set (int labnum, const char *label, int fmt)
 Configure specified soft key.

Variables

static struct _softlabelkeysslks

Detailed Description

Soft label key functions.

Definition in file slk.c.


Define Documentation

#define MIN_SPACE_SIZE   2

Definition at line 16 of file slk.c.

Referenced by slk_init().

#define SLK_MAX_LABEL_LEN   8

Definition at line 18 of file slk.c.

Referenced by _print_label().

#define SLK_MAX_NUM_LABELS   12

Definition at line 20 of file slk.c.

#define SLK_MAX_NUM_SPACES   2

Definition at line 22 of file slk.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static void _enter_slk ( void  ) [static]
static void _leave_slk ( void  ) [static]
static void _print_label ( struct _softlabel  sl) [static]

Definition at line 78 of file slk.c.

References _wputstr(), assert, _softlabel::fmt, _softlabel::label, _softlabelkeys::max_label_len, memset(), NOWRAP, SLK_MAX_LABEL_LEN, stdscr, strcat(), and strlen().

Referenced by slk_restore().

                                                  {
        int space_ch;
        char str[SLK_MAX_LABEL_LEN + 1];

        assert ( slks->max_label_len <= SLK_MAX_LABEL_LEN );
        space_ch = ' ';
        memset ( str, 0, sizeof ( str ) );

        // protect against gaps in the soft label keys array
        if ( ! sl.label[0] ) {
                memset( str, space_ch, (size_t)(slks->max_label_len) );
        } else {
                /* we need to pad the label with varying amounts of leading
                   pad depending on the format of the label */
                if ( sl.fmt == 1 ) {
                        memset( str, space_ch, 
                                (size_t)(slks->max_label_len 
                                         - strlen(sl.label)) / 2 );
                }
                if ( sl.fmt == 2 ) {
                        memset( str, space_ch,
                                (size_t)(slks->max_label_len 
                                         - strlen(sl.label)) );
                }
                strcat(str,sl.label);
                
                // post-padding
                memset(str+strlen(str), space_ch,
                       (size_t)(slks->max_label_len - strlen(str)) );
        }

        // print the formatted label
        _wputstr ( stdscr, str, NOWRAP, slks->max_label_len );
}
attr_t slk_attr ( void  )

Return the attribute used for the soft function keys.

Return values:
attrsthe current attributes of the soft function keys

Definition at line 118 of file slk.c.

References _softlabelkeys::attrs, and NULL.

                         {
        return ( slks == NULL ? 0 : slks->attrs );
}
int slk_attroff ( const chtype  attrs)

Turn off soft function key attributes.

Parameters:
attrsattribute bit mask
Return values:
rcreturn status code

Definition at line 128 of file slk.c.

References A_ATTRIBUTES, _softlabelkeys::attrs, ERR, NULL, and OK.

Referenced by slk_attr_off().

                                       {
        if ( slks == NULL ) 
                return ERR;
        slks->attrs &= ~( attrs & A_ATTRIBUTES );
        return OK;
}
int slk_attron ( const chtype  attrs)

Turn on soft function key attributes.

Parameters:
attrsattribute bit mask
Return values:
rcreturn status code

Definition at line 141 of file slk.c.

References A_ATTRIBUTES, _softlabelkeys::attrs, ERR, NULL, and OK.

Referenced by slk_attr_on().

                                      {
        if ( slks == NULL )
                return ERR;
        slks->attrs |= ( attrs & A_ATTRIBUTES );
        return OK;
}
int slk_attrset ( const chtype  attrs)

Set soft function key attributes.

Parameters:
attrsattribute bit mask
Return values:
rcreturn status code

Definition at line 154 of file slk.c.

References A_ATTRIBUTES, _softlabelkeys::attrs, ERR, NULL, and OK.

                                       {
        if ( slks == NULL ) 
                return ERR;
        slks->attrs = ( attrs & A_ATTRIBUTES );
        return OK;
}
int slk_attr_off ( const attr_t  attrs,
void *opts  __unused 
)

Turn off soft function key attributes.

Parameters:
attrsattribute bit mask
*optsundefined (for future implementation)
Return values:
rcreturn status code

Definition at line 168 of file slk.c.

References slk_attroff().

                                                             {
        return slk_attroff( attrs );
}
int slk_attr_on ( attr_t  attrs,
void *opts  __unused 
)

Turn on soft function key attributes.

Parameters:
attrsattribute bit mask
*optsundefined (for future implementation)
Return values:
rcreturn status code

Definition at line 179 of file slk.c.

References slk_attron().

                                                      {
        return slk_attron( attrs );
}
int slk_attr_set ( const attr_t  attrs,
short  colour_pair_number,
void *opts  __unused 
)

Set soft function key attributes.

Parameters:
attrsattribute bit mask
colour_pair_numbercolour pair integer
*optsundefined (for future implementation)
Return values:
rcreturn status code

Definition at line 191 of file slk.c.

References A_ATTRIBUTES, _softlabelkeys::attrs, COLORS, CPAIR_SHIFT, ERR, NULL, and OK.

                                         {
        if ( slks == NULL ) 
                return ERR;

        if ( ( unsigned short )colour_pair_number > COLORS )
                return ERR;

        slks->attrs = ( (unsigned short)colour_pair_number << CPAIR_SHIFT ) |
                ( attrs & A_ATTRIBUTES );
        return OK;
}
int slk_clear ( void  )

Clear the soft function key labels from the screen.

Return values:
rcreturn status code

Definition at line 209 of file slk.c.

References _enter_slk(), _leave_slk(), ERR, NULL, OK, stdscr, and wclrtoeol().

Referenced by slk_refresh().

                       {
        if ( slks == NULL )
                return ERR;

        _enter_slk();
        wclrtoeol ( stdscr );
        _leave_slk();

        return OK;
}
int slk_colour ( short  colour_pair_number)

Set soft label colour pair.

Definition at line 223 of file slk.c.

References A_ATTRIBUTES, _softlabelkeys::attrs, COLORS, CPAIR_SHIFT, ERR, NULL, and OK.

                                            {
        if ( slks == NULL ) 
                return ERR;
        if ( ( unsigned short )colour_pair_number > COLORS )
                return ERR;

        slks->attrs = ( (unsigned short)colour_pair_number << CPAIR_SHIFT )
                | ( slks->attrs & A_ATTRIBUTES );

        return OK;
}
int slk_init ( int  fmt)

Initialise the soft function keys.

Parameters:
fmtformat of keys
Return values:
rcreturn status code

Definition at line 241 of file slk.c.

References A_DEFAULT, _softlabelkeys::attrs, calloc(), COLS, ERR, _softlabelkeys::fmt, fmt, LINES, _softlabelkeys::maj_space_len, _softlabelkeys::max_label_len, MIN_SPACE_SIZE, _softlabelkeys::num_labels, _softlabelkeys::num_spaces, OK, and _softlabelkeys::spaces.

                         {
        unsigned short nmaj, nmin, nblocks, available_width;

        if ( (unsigned)fmt > 3 ) {
                return ERR;
        }

        /* There seems to be no API call to free this data structure... */
        if ( ! slks )
                slks = calloc(1,sizeof(*slks));
        if ( ! slks )
                return ERR;

        slks->attrs = A_DEFAULT;
        slks->fmt = fmt;
        switch(fmt) {
        case 0:
                nblocks = 8; nmaj = 2; nmin = 5;
                slks->spaces[0] = 2; slks->spaces[1] = 4;
                break;
        case 1:
                nblocks = 8; nmaj = 1; nmin = 6;
                slks->spaces[0] = 3;
                break;
        case 2:
                // same allocations as format 3
        case 3:
                nblocks = 12; nmaj = 2; nmin = 9;
                slks->spaces[0] = 3; slks->spaces[1] = 7;
                break;
        default:
                return ERR;
        }

        // determine maximum label length and major space size
        available_width = COLS - ( ( MIN_SPACE_SIZE * nmaj ) + nmin );
        slks->max_label_len = available_width / nblocks;
        slks->maj_space_len = MIN_SPACE_SIZE + 
                ( available_width % nblocks ) / nmaj;
        slks->num_spaces = nmaj;
        slks->num_labels = nblocks;

        // strip a line from the screen
        LINES -= 1;

        return OK;
}
char* slk_label ( int  labnum)

Return the label for the specified soft key.

Parameters:
labnumsoft key identifier
Return values:
labelreturn label

Definition at line 295 of file slk.c.

References _softlabelkeys::fkeys, _softlabel::label, and NULL.

                               {
        if ( slks == NULL ) 
                return NULL;

        return slks->fkeys[labnum].label;
}
int slk_restore ( void  )

Restore soft function key labels to the screen.

Return values:
rcreturn status code

Definition at line 307 of file slk.c.

References _enter_slk(), _leave_slk(), _print_label(), _wputch(), _softlabelkeys::attrs, COLS, ERR, _softlabelkeys::fkeys, _softlabelkeys::maj_space_len, _softlabelkeys::max_label_len, NOWRAP, NULL, _softlabelkeys::num_labels, _softlabelkeys::num_spaces, OK, _softlabelkeys::spaces, and stdscr.

Referenced by slk_refresh().

                         {
        unsigned int i, j, pos_x,
                *next_space, *last_space;
        chtype space_ch;

        if ( slks == NULL )
                return ERR;

        pos_x = 0;

        _enter_slk();

        space_ch = (chtype)' ' | slks->attrs;
        next_space = &(slks->spaces[0]);
        last_space = &(slks->spaces[slks->num_spaces-1]);

        for ( i = 0; i < slks->num_labels ; i++ ) {
                _print_label( slks->fkeys[i] );
                pos_x += slks->max_label_len;

                if ( i == *next_space ) {
                        for ( j = 0; j < slks->maj_space_len; j++, pos_x++ )
                                _wputch ( stdscr, space_ch, NOWRAP );
                        if ( next_space < last_space )
                                next_space++;
                } else {
                        if ( pos_x < COLS )
                                _wputch ( stdscr, space_ch, NOWRAP );
                        pos_x++;
                }
        }

        _leave_slk();

        return OK;
}
int slk_set ( int  labnum,
const char *  label,
int  fmt 
)

Configure specified soft key.

Parameters:
labnumsoft label position to configure
*labelstring to use as soft key label
fmtjustification format of label
Return values:
rcreturn status code

Definition at line 352 of file slk.c.

References ERR, _softlabelkeys::fkeys, _softlabel::fmt, fmt, _softlabel::label, NULL, _softlabelkeys::num_labels, OK, and strncpy().

                                                       {
        if ( slks == NULL ) 
                return ERR;
        if ( (unsigned short)labnum >= slks->num_labels )
                return ERR;
        if ( (unsigned short)fmt >= 3 )
                return ERR;

        strncpy(slks->fkeys[labnum].label, label,
                (sizeof(slks->fkeys[labnum].label) - 1));
        slks->fkeys[labnum].fmt = fmt;

        return OK;
}

Variable Documentation

struct _softlabelkeys* slks [static]

Definition at line 55 of file slk.c.