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 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /**
27  * Jump scrolling
28  *
29  */
30 
31 #include <assert.h>
32 #include <ipxe/keys.h>
33 #include <ipxe/jumpscroll.h>
34 
35 /**
36  * Handle keypress
37  *
38  * @v scroll Jump scroller
39  * @v key Key pressed by user
40  * @ret move Scroller movement, or zero
41  */
42 unsigned int jump_scroll_key ( struct jump_scroller *scroll, int key ) {
43  unsigned int flags = 0;
44  int16_t delta;
45 
46  /* Sanity checks */
47  assert ( scroll->rows != 0 );
48  assert ( scroll->count != 0 );
49  assert ( scroll->current < scroll->count );
50  assert ( scroll->first < scroll->count );
51  assert ( scroll->first <= scroll->current );
52  assert ( scroll->current < ( scroll->first + scroll->rows ) );
53 
54  /* Handle key, if applicable */
55  switch ( key ) {
56  case KEY_UP:
57  delta = -1;
58  break;
59  case TAB:
61  /* fall through */
62  case KEY_DOWN:
63  delta = +1;
64  break;
65  case KEY_PPAGE:
66  delta = ( scroll->first - scroll->current - 1 );
67  break;
68  case KEY_NPAGE:
69  delta = ( scroll->first - scroll->current + scroll->rows );
70  break;
71  case KEY_HOME:
72  delta = -( scroll->count );
73  break;
74  case KEY_END:
75  delta = +( scroll->count );
76  break;
77  default:
78  delta = 0;
79  break;
80  }
81 
82  return ( SCROLL ( delta ) | flags );
83 }
84 
85 /**
86  * Move scroller
87  *
88  * @v scroll Jump scroller
89  * @v move Scroller movement
90  * @ret move Continuing scroller movement (if applicable)
91  */
92 unsigned int jump_scroll_move ( struct jump_scroller *scroll,
93  unsigned int move ) {
94  int16_t delta = SCROLL_DELTA ( move );
95  int current = scroll->current;
96  int last = ( scroll->count - 1 );
97 
98  /* Sanity checks */
99  assert ( move != 0 );
100  assert ( scroll->count != 0 );
101 
102  /* Move to the new current item */
103  current += delta;
104 
105  /* Default to continuing movement in the same direction */
106  delta = ( ( delta >= 0 ) ? +1 : -1 );
107 
108  /* Check for start/end of list */
109  if ( ( current >= 0 ) && ( current <= last ) ) {
110  /* We are still within the list. Update the current
111  * item and continue moving in the same direction (if
112  * applicable).
113  */
114  scroll->current = current;
115  } else {
116  /* We have attempted to move outside the list. If we
117  * are wrapping around, then continue in the same
118  * direction (if applicable), otherwise reverse.
119  */
120  if ( ! ( move & SCROLL_WRAP ) )
121  delta = -delta;
122 
123  /* Move to start or end of list as appropriate */
124  if ( delta >= 0 ) {
125  scroll->current = 0;
126  } else {
127  scroll->current = last;
128  }
129  }
130 
131  return ( SCROLL ( delta ) | ( move & SCROLL_FLAGS ) );
132 }
133 
134 /**
135  * Jump scroll to new page (if applicable)
136  *
137  * @v scroll Jump scroller
138  * @ret jumped Jumped to a new page
139  */
140 int jump_scroll ( struct jump_scroller *scroll ) {
141  unsigned int index;
142 
143  /* Sanity checks */
144  assert ( scroll->rows != 0 );
145  assert ( scroll->count != 0 );
146  assert ( scroll->current < scroll->count );
147  assert ( scroll->first < scroll->count );
148 
149  /* Do nothing if we are already on the correct page */
150  index = ( scroll->current - scroll->first );
151  if ( index < scroll->rows )
152  return 0;
153 
154  /* Move to required page */
155  while ( scroll->first < scroll->current )
156  scroll->first += scroll->rows;
157  while ( scroll->first > scroll->current )
158  scroll->first -= scroll->rows;
159 
160  return 1;
161 }
#define SCROLL(delta)
Construct scroll movement.
Definition: jumpscroll.h:32
#define KEY_NPAGE
Page down.
Definition: keys.h:113
#define KEY_HOME
Home.
Definition: keys.h:109
A jump scroller.
Definition: jumpscroll.h:15
#define KEY_DOWN
Down arrow.
Definition: keys.h:105
#define KEY_UP
Up arrow.
Definition: keys.h:104
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define SCROLL_DELTA(scroll)
Extract change in scroller position.
Definition: jumpscroll.h:40
unsigned int first
First visible item.
Definition: jumpscroll.h:23
unsigned int count
Total number of items.
Definition: jumpscroll.h:19
int jump_scroll(struct jump_scroller *scroll)
Jump scroll to new page (if applicable)
Definition: jumpscroll.c:140
#define TAB
Definition: keys.h:46
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define KEY_PPAGE
Page up.
Definition: keys.h:112
unsigned int jump_scroll_move(struct jump_scroller *scroll, unsigned int move)
Move scroller.
Definition: jumpscroll.c:92
#define KEY_END
End.
Definition: keys.h:108
uint32_t last
Length to read in last segment, or zero.
Definition: pccrc.h:30
#define SCROLL_FLAGS
Scroll movement flags.
Definition: jumpscroll.h:43
Key definitions.
static int move(int y, int x)
Definition: curses.h:593
Jump scrolling.
unsigned int rows
Maximum number of visible rows.
Definition: jumpscroll.h:17
signed short int16_t
Definition: stdint.h:16
uint64_t index
Index of the first segment within the content.
Definition: pccrc.h:21
#define SCROLL_WRAP
Wrap around scrolling.
Definition: jumpscroll.h:44
unsigned int current
Currently selected item.
Definition: jumpscroll.h:21
unsigned int jump_scroll_key(struct jump_scroller *scroll, int key)
Jump scrolling.
Definition: jumpscroll.c:42
union @383 key
Sense key.
Definition: crypto.h:284
uint8_t flags
Flags.
Definition: ena.h:18