9 * Soft label key functions
12 #define MIN_SPACE_SIZE 2
17 /* Format of soft label
25 struct _softlabelkeys {
26 struct _softlabel fkeys[12];
28 /* Soft label layout format
32 3: 4-4-4 with index line
35 unsigned short max_label_len;
36 unsigned short maj_space_len;
37 unsigned short num_labels;
38 unsigned short num_spaces;
39 unsigned short *spaces;
42 struct _softlabelkeys *slks;
45 I either need to break the primitives here, or write a collection of
46 functions specifically for SLKs that directly access the screen
47 functions - since this technically isn't part of stdscr, I think
51 static void _movetoslk ( void ) {
52 stdscr->scr->movetoyx( stdscr->scr, LINES, 0 );
56 * Return the attribute used for the soft function keys
58 * @ret attrs the current attributes of the soft function keys
60 attr_t slk_attr ( void ) {
61 return ( slks == NULL ? 0 : slks->attrs );
65 * Turn off soft function key attributes
67 * @v attrs attribute bit mask
68 * @ret rc return status code
70 int slk_attroff ( const chtype attrs ) {
73 slks->attrs &= ~( attrs & A_ATTRIBUTES );
78 * Turn on soft function key attributes
80 * @v attrs attribute bit mask
81 * @ret rc return status code
83 int slk_attron ( const chtype attrs ) {
86 slks->attrs |= ( attrs & A_ATTRIBUTES );
91 * Set soft function key attributes
93 * @v attrs attribute bit mask
94 * @ret rc return status code
96 int slk_attrset ( const chtype attrs ) {
99 slks->attrs = ( attrs & A_ATTRIBUTES );
104 * Turn off soft function key attributes
106 * @v attrs attribute bit mask
107 * @v *opts undefined (for future implementation)
108 * @ret rc return status code
110 int slk_attr_off ( const attr_t attrs, void *opts __unused ) {
111 return slk_attroff( attrs );
115 * Turn on soft function key attributes
117 * @v attrs attribute bit mask
118 * @v *opts undefined (for future implementation)
119 * @ret rc return status code
121 int slk_attr_on ( attr_t attrs, void *opts __unused ) {
122 return slk_attron( attrs );
126 * Set soft function key attributes
128 * @v attrs attribute bit mask
129 * @v colour_pair_number colour pair integer
130 * @v *opts undefined (for future implementation)
131 * @ret rc return status code
133 int slk_attr_set ( const attr_t attrs, short colour_pair_number,
134 void *opts __unused ) {
138 if ( ( unsigned short )colour_pair_number > COLORS )
141 slks->attrs = ( (unsigned short)colour_pair_number << CPAIR_SHIFT ) |
142 ( attrs & A_ATTRIBUTES );
147 * Clear the soft function key labels from the screen
149 * @ret rc return status code
151 int slk_clear ( void ) {
158 * Set soft label colour pair
160 int slk_colour ( short colour_pair_number ) {
163 if ( ( unsigned short )colour_pair_number > COLORS )
166 slks->attrs = ( (unsigned short)colour_pair_number << CPAIR_SHIFT )
167 | ( slks->attrs & A_ATTRIBUTES );
173 * Initialise the soft function keys
175 * @v fmt format of keys
176 * @ret rc return status code
178 int slk_init ( int fmt ) {
179 unsigned short nmaj, nmin, nblocks, available_width;
181 if ( (unsigned)fmt > 3 ) {
185 slks = malloc(sizeof(struct _softlabelkeys));
186 slks->attrs = A_DEFAULT;
190 nblocks = 8; nmaj = 2; nmin = 5;
191 slks->spaces = calloc(2, sizeof(unsigned short));
192 slks->spaces[0] = 2; slks->spaces[1] = 4;
195 nblocks = 8; nmaj = 1; nmin = 6;
196 slks->spaces = calloc(1, sizeof(unsigned short));
200 // same allocations as format 3
202 nblocks = 12; nmaj = 2; nmin = 9;
203 slks->spaces = calloc(2, sizeof(unsigned short));
204 slks->spaces[0] = 3; slks->spaces[1] = 7;
208 // determine maximum label length and major space size
209 available_width = COLS - ( ( MIN_SPACE_SIZE * nmaj ) + nmin );
210 slks->max_label_len = available_width / nblocks;
211 slks->maj_space_len = ( available_width % nblocks ) / nmaj;
212 slks->num_spaces = nmaj;
214 // strip a line from the screen
221 * Return the label for the specified soft key
223 * @v labnum soft key identifier
224 * @ret label return label
226 char* slk_label ( int labnum ) {
230 return slks->fkeys[labnum].label;
234 * Restore soft function key labels to the screen
236 * @ret rc return status code
238 int slk_restore ( void ) {
240 *next_space, *last_space;
249 space_ch = (int)' ' | slks->attrs;
250 next_space = &(slks->spaces[0]);
251 last_space = &(slks->spaces[slks->num_spaces-1]);
253 for ( i = 0; i < slks->num_labels ; i++ ) {
254 while ( ( c = *(slks->fkeys[i].label++) ) != '\0' ) {
255 stdscr->scr->putc( stdscr->scr, (int)c | slks->attrs );
257 if ( i == *next_space ) {
258 for ( j = 0; j < slks->maj_space_len; j++ )
259 stdscr->scr->putc( stdscr->scr, space_ch );
260 if ( next_space < last_space )
263 stdscr->scr->putc( stdscr->scr, space_ch );
271 * Configure specified soft key
273 * @v labnum soft label position to configure
274 * @v *label string to use as soft key label
275 * @v fmt justification format of label
276 * @ret rc return status code
278 int slk_set ( int labnum, const char *label, int fmt ) {
281 if ( (unsigned short)labnum > 12 )
283 if ( (unsigned short)fmt >= 3 )
285 if ( strlen(label) > slks->max_label_len )
288 strcpy( slks->fkeys[labnum].label, label );
289 slks->fkeys[labnum].fmt = fmt;