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 int jump_scroll_key ( struct jump_scroller *scroll, int key ) {
43 
44  /* Sanity checks */
45  assert ( scroll->rows != 0 );
46  assert ( scroll->count != 0 );
47  assert ( scroll->current < scroll->count );
48  assert ( scroll->first < scroll->count );
49  assert ( scroll->first <= scroll->current );
50  assert ( scroll->current < ( scroll->first + scroll->rows ) );
51 
52  /* Handle key, if applicable */
53  switch ( key ) {
54  case KEY_UP:
55  return -1;
56  case KEY_DOWN:
57  return +1;
58  case KEY_PPAGE:
59  return ( scroll->first - scroll->current - 1 );
60  case KEY_NPAGE:
61  return ( scroll->first - scroll->current + scroll->rows );
62  case KEY_HOME:
63  return -( scroll->count );
64  case KEY_END:
65  return +( scroll->count );
66  default:
67  return 0;
68  }
69 }
70 
71 /**
72  * Move scroller
73  *
74  * @v scroll Jump scroller
75  * @v move Scroller movement
76  * @ret move Continuing scroller movement (if applicable)
77  */
78 int jump_scroll_move ( struct jump_scroller *scroll, int move ) {
79  int current = scroll->current;
80  int last = ( scroll->count - 1 );
81 
82  /* Sanity checks */
83  assert ( move != 0 );
84  assert ( scroll->count != 0 );
85 
86  /* Move to the new current item */
87  current += move;
88 
89  /* Check for start/end of list */
90  if ( current < 0 ) {
91  /* We have attempted to move before the start of the
92  * list. Move to the start of the list and continue
93  * moving forwards (if applicable).
94  */
95  scroll->current = 0;
96  return +1;
97  } else if ( current > last ) {
98  /* We have attempted to move after the end of the
99  * list. Move to the end of the list and continue
100  * moving backwards (if applicable).
101  */
102  scroll->current = last;
103  return -1;
104  } else {
105  /* Update the current item and continue moving in the
106  * same direction (if applicable).
107  */
108  scroll->current = current;
109  return ( ( move > 0 ) ? +1 : -1 );
110  }
111 }
112 
113 /**
114  * Jump scroll to new page (if applicable)
115  *
116  * @v scroll Jump scroller
117  * @ret jumped Jumped to a new page
118  */
119 int jump_scroll ( struct jump_scroller *scroll ) {
120  unsigned int index;
121 
122  /* Sanity checks */
123  assert ( scroll->rows != 0 );
124  assert ( scroll->count != 0 );
125  assert ( scroll->current < scroll->count );
126  assert ( scroll->first < scroll->count );
127 
128  /* Do nothing if we are already on the correct page */
129  index = ( scroll->current - scroll->first );
130  if ( index < scroll->rows )
131  return 0;
132 
133  /* Move to required page */
134  while ( scroll->first < scroll->current )
135  scroll->first += scroll->rows;
136  while ( scroll->first > scroll->current )
137  scroll->first -= scroll->rows;
138 
139  return 1;
140 }
#define KEY_NPAGE
Page down.
Definition: keys.h:113
#define KEY_HOME
Home.
Definition: keys.h:109
A jump scroller.
Definition: jumpscroll.h:13
#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)
unsigned int first
First visible item.
Definition: jumpscroll.h:21
unsigned int count
Total number of items.
Definition: jumpscroll.h:17
int jump_scroll(struct jump_scroller *scroll)
Jump scroll to new page (if applicable)
Definition: jumpscroll.c:119
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define KEY_PPAGE
Page up.
Definition: keys.h:112
#define KEY_END
End.
Definition: keys.h:108
uint32_t last
Length to read in last segment, or zero.
Definition: pccrc.h:30
Key definitions.
static int move(int y, int x)
Definition: curses.h:593
int jump_scroll_key(struct jump_scroller *scroll, int key)
Jump scrolling.
Definition: jumpscroll.c:42
Jump scrolling.
unsigned int rows
Maximum number of visible rows.
Definition: jumpscroll.h:15
uint64_t index
Index of the first segment within the content.
Definition: pccrc.h:21
unsigned int current
Currently selected item.
Definition: jumpscroll.h:19
union @382 key
Sense key.
Definition: crypto.h:284
int jump_scroll_move(struct jump_scroller *scroll, int move)
Move scroller.
Definition: jumpscroll.c:78