You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jumpscroll.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /**
  25. * Jump scrolling
  26. *
  27. */
  28. #include <assert.h>
  29. #include <ipxe/keys.h>
  30. #include <ipxe/jumpscroll.h>
  31. /**
  32. * Handle keypress
  33. *
  34. * @v scroll Jump scroller
  35. * @v key Key pressed by user
  36. * @ret move Scroller movement, or zero
  37. */
  38. int jump_scroll_key ( struct jump_scroller *scroll, int key ) {
  39. /* Sanity checks */
  40. assert ( scroll->rows != 0 );
  41. assert ( scroll->count != 0 );
  42. assert ( scroll->current < scroll->count );
  43. assert ( scroll->first < scroll->count );
  44. assert ( scroll->first <= scroll->current );
  45. assert ( scroll->current < ( scroll->first + scroll->rows ) );
  46. /* Handle key, if applicable */
  47. switch ( key ) {
  48. case KEY_UP:
  49. return -1;
  50. case KEY_DOWN:
  51. return +1;
  52. case KEY_PPAGE:
  53. return ( scroll->first - scroll->current - 1 );
  54. case KEY_NPAGE:
  55. return ( scroll->first - scroll->current + scroll->rows );
  56. case KEY_HOME:
  57. return -( scroll->count );
  58. case KEY_END:
  59. return +( scroll->count );
  60. default:
  61. return 0;
  62. }
  63. }
  64. /**
  65. * Move scroller
  66. *
  67. * @v scroll Jump scroller
  68. * @v move Scroller movement
  69. * @ret move Continuing scroller movement (if applicable)
  70. */
  71. int jump_scroll_move ( struct jump_scroller *scroll, int move ) {
  72. int current = scroll->current;
  73. int last = ( scroll->count - 1 );
  74. /* Sanity checks */
  75. assert ( move != 0 );
  76. assert ( scroll->count != 0 );
  77. /* Move to the new current item */
  78. current += move;
  79. /* Check for start/end of list */
  80. if ( current < 0 ) {
  81. /* We have attempted to move before the start of the
  82. * list. Move to the start of the list and continue
  83. * moving forwards (if applicable).
  84. */
  85. scroll->current = 0;
  86. return +1;
  87. } else if ( current > last ) {
  88. /* We have attempted to move after the end of the
  89. * list. Move to the end of the list and continue
  90. * moving backwards (if applicable).
  91. */
  92. scroll->current = last;
  93. return -1;
  94. } else {
  95. /* Update the current item and continue moving in the
  96. * same direction (if applicable).
  97. */
  98. scroll->current = current;
  99. return ( ( move > 0 ) ? +1 : -1 );
  100. }
  101. }
  102. /**
  103. * Jump scroll to new page (if applicable)
  104. *
  105. * @v scroll Jump scroller
  106. * @ret jumped Jumped to a new page
  107. */
  108. int jump_scroll ( struct jump_scroller *scroll ) {
  109. unsigned int index;
  110. /* Sanity checks */
  111. assert ( scroll->rows != 0 );
  112. assert ( scroll->count != 0 );
  113. assert ( scroll->current < scroll->count );
  114. assert ( scroll->first < scroll->count );
  115. /* Do nothing if we are already on the correct page */
  116. index = ( scroll->current - scroll->first );
  117. if ( index < scroll->rows )
  118. return 0;
  119. /* Move to required page */
  120. while ( scroll->first < scroll->current )
  121. scroll->first += scroll->rows;
  122. while ( scroll->first > scroll->current )
  123. scroll->first -= scroll->rows;
  124. return 1;
  125. }