iPXE
jumpscroll.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27/**
28 * Jump scrolling
29 *
30 */
31
32#include <assert.h>
33#include <ipxe/keys.h>
34#include <ipxe/jumpscroll.h>
35
36/**
37 * Handle keypress
38 *
39 * @v scroll Jump scroller
40 * @v key Key pressed by user
41 * @ret move Scroller movement, or zero
42 */
43unsigned int jump_scroll_key ( struct jump_scroller *scroll, int key ) {
44 unsigned int flags = 0;
45 int16_t delta;
46
47 /* Sanity checks */
48 assert ( scroll->rows != 0 );
49 assert ( scroll->count != 0 );
50 assert ( scroll->current < scroll->count );
51 assert ( scroll->first < scroll->count );
52 assert ( scroll->first <= scroll->current );
53 assert ( scroll->current < ( scroll->first + scroll->rows ) );
54
55 /* Handle key, if applicable */
56 switch ( key ) {
57 case KEY_UP:
58 delta = -1;
59 break;
60 case TAB:
62 /* fall through */
63 case KEY_DOWN:
64 delta = +1;
65 break;
66 case KEY_PPAGE:
67 delta = ( scroll->first - scroll->current - 1 );
68 break;
69 case KEY_NPAGE:
70 delta = ( scroll->first - scroll->current + scroll->rows );
71 break;
72 case KEY_HOME:
73 delta = -( scroll->count );
74 break;
75 case KEY_END:
76 delta = +( scroll->count );
77 break;
78 default:
79 delta = 0;
80 break;
81 }
82
83 return ( SCROLL ( delta ) | flags );
84}
85
86/**
87 * Move scroller
88 *
89 * @v scroll Jump scroller
90 * @v move Scroller movement
91 * @ret move Continuing scroller movement (if applicable)
92 */
93unsigned int jump_scroll_move ( struct jump_scroller *scroll,
94 unsigned int move ) {
95 int16_t delta = SCROLL_DELTA ( move );
96 int current = scroll->current;
97 int last = ( scroll->count - 1 );
98
99 /* Sanity checks */
100 assert ( move != 0 );
101 assert ( scroll->count != 0 );
102
103 /* Move to the new current item */
104 current += delta;
105
106 /* Default to continuing movement in the same direction */
107 delta = ( ( delta >= 0 ) ? +1 : -1 );
108
109 /* Check for start/end of list */
110 if ( ( current >= 0 ) && ( current <= last ) ) {
111 /* We are still within the list. Update the current
112 * item and continue moving in the same direction (if
113 * applicable).
114 */
115 scroll->current = current;
116 } else {
117 /* We have attempted to move outside the list. If we
118 * are wrapping around, then continue in the same
119 * direction (if applicable), otherwise reverse.
120 */
121 if ( ! ( move & SCROLL_WRAP ) )
122 delta = -delta;
123
124 /* Move to start or end of list as appropriate */
125 if ( delta >= 0 ) {
126 scroll->current = 0;
127 } else {
128 scroll->current = last;
129 }
130 }
131
132 return ( SCROLL ( delta ) | ( move & SCROLL_FLAGS ) );
133}
134
135/**
136 * Jump scroll to new page (if applicable)
137 *
138 * @v scroll Jump scroller
139 * @ret jumped Jumped to a new page
140 */
141int jump_scroll ( struct jump_scroller *scroll ) {
142 unsigned int index;
143
144 /* Sanity checks */
145 assert ( scroll->rows != 0 );
146 assert ( scroll->count != 0 );
147 assert ( scroll->current < scroll->count );
148 assert ( scroll->first < scroll->count );
149
150 /* Do nothing if we are already on the correct page */
151 index = ( scroll->current - scroll->first );
152 if ( index < scroll->rows )
153 return 0;
154
155 /* Move to required page */
156 while ( scroll->first < scroll->current )
157 scroll->first += scroll->rows;
158 while ( scroll->first > scroll->current )
159 scroll->first -= scroll->rows;
160
161 return 1;
162}
union @162305117151260234136356364136041353210355154177 key
Sense key.
Definition scsi.h:3
signed short int16_t
Definition stdint.h:16
long index
Definition bigint.h:65
Assertions.
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
static int move(int y, int x)
Definition curses.h:594
uint8_t flags
Flags.
Definition ena.h:7
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
unsigned int jump_scroll_move(struct jump_scroller *scroll, unsigned int move)
Move scroller.
Definition jumpscroll.c:93
unsigned int jump_scroll_key(struct jump_scroller *scroll, int key)
Jump scrolling.
Definition jumpscroll.c:43
int jump_scroll(struct jump_scroller *scroll)
Jump scroll to new page (if applicable)
Definition jumpscroll.c:141
Jump scrolling.
#define SCROLL_WRAP
Wrap around scrolling.
Definition jumpscroll.h:45
#define SCROLL_DELTA(scroll)
Extract change in scroller position.
Definition jumpscroll.h:41
#define SCROLL(delta)
Construct scroll movement.
Definition jumpscroll.h:33
#define SCROLL_FLAGS
Scroll movement flags.
Definition jumpscroll.h:44
Key definitions.
#define KEY_DOWN
Down arrow.
Definition keys.h:107
#define KEY_PPAGE
Page up.
Definition keys.h:114
#define KEY_NPAGE
Page down.
Definition keys.h:115
#define KEY_END
End.
Definition keys.h:110
#define TAB
Definition keys.h:47
#define KEY_HOME
Home.
Definition keys.h:111
#define KEY_UP
Up arrow.
Definition keys.h:106
A jump scroller.
Definition jumpscroll.h:16
unsigned int count
Total number of items.
Definition jumpscroll.h:20
unsigned int first
First visible item.
Definition jumpscroll.h:24
unsigned int rows
Maximum number of visible rows.
Definition jumpscroll.h:18
unsigned int current
Currently selected item.
Definition jumpscroll.h:22