[PATCH 1/4] cfdisk: use keypad() in menu selection function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This makes the source code looks cleaner, and works a bit better than
the previous way with hardcoded escape sequences.

Signed-off-by: Francesco Cosoleto <cosoleto@xxxxxxxxx>
---
 fdisk/cfdisk.c |  112 ++++++++++++++++++++-----------------------------------
 1 files changed, 41 insertions(+), 71 deletions(-)

diff --git a/fdisk/cfdisk.c b/fdisk/cfdisk.c
index 7b71721..71ce9f6 100644
--- a/fdisk/cfdisk.c
+++ b/fdisk/cfdisk.c
@@ -154,12 +154,7 @@
 #define ESC '\033'
 #define DEL '\177'
 #define BELL '\007'
-#define TAB '\011'
 #define REDRAWKEY '\014'	/* ^L */
-#define UPKEY '\020'		/* ^P */
-#define UPKEYVI '\153'		/* k */
-#define DOWNKEY '\016'		/* ^N */
-#define DOWNKEYVI '\152'	/* j */
 
 /* Display units */
 #define GIGABYTES 1
@@ -1013,8 +1008,6 @@ find_logical(int i) {
 #define MENU_MAX_ITEMS 256 /* for simpleMenu function */
 #define MENU_UP 1
 #define MENU_DOWN 2
-#define MENU_RIGHT 3
-#define MENU_LEFT 4
 
 struct MenuItem
 {
@@ -1112,6 +1105,8 @@ menuSelect( int y, int x, struct MenuItem *menuItems, int itemLength,
         if( !menuItems[current].key ) current = 0;
     }
 
+    keypad(stdscr, TRUE);
+
     /* Repeat until allowable choice has been made */
     while( !key ) {
         /* Display the menu and read a command */
@@ -1133,72 +1128,45 @@ menuSelect( int y, int x, struct MenuItem *menuItems, int itemLength,
         move( WARNING_START + 1, 0 );
         clrtoeol();
 
-        /* Cursor keys - possibly split by slow connection */
-        if( key == ESC ) {
-            /* Check whether this is a real ESC or one of extended keys */
-            /*nodelay(stdscr, TRUE);*/
-            key = getch();
-            /*nodelay(stdscr, FALSE);*/
-
-            if( key == /*ERR*/ ESC ) {
-                /* This is a real ESC */
-                key = ESC;
-            }
-            if(key == '[' || key == 'O') {
-                /* This is one extended keys */
-		key = getch();
-
-                switch(key) {
-                    case 'A': /* Up arrow */
-			key = MENU_UP;
-                        break;
-                    case 'B': /* Down arrow */
-                        key = MENU_DOWN;
-                        break;
-                    case 'C': /* Right arrow */
-                        key = MENU_RIGHT;
-                        break;
-                    case 'D': /* Left arrow */
-                    case 'Z': /* Shift Tab */
-                        key = MENU_LEFT;
-                        break;
-		    default:
-			key = 0;
-                }
-            }
-        }
-
-        /* Enter equals the keyboard shortcut of current menu item */
-        if (key == '\r')
-            key = menuItems[current].key;
-
-	/* Give alternatives for arrow keys in case the window manager
-	   swallows these */
-	if (key == TAB)
-	    key = MENU_RIGHT;
-        if (key == UPKEY || key == UPKEYVI)	/* ^P or k */
-	    key = MENU_UP;
-	if (key == DOWNKEY || key == DOWNKEYVI)	/* ^N or j */
-	    key = MENU_DOWN;
-
-	if (key == MENU_RIGHT) {
+	switch (key) {
+	case KEY_UP:
+	case '\020':	/* ^P */
+	case 'k':	/* Vi-like alternative */
+		key = MENU_UP;
+		break;
+	case KEY_DOWN:
+	case '\016':	/* ^N */
+	case 'j':	/* Vi-like alternative */
+		key = MENU_DOWN;
+		break;
+	case KEY_RIGHT:
+	case '\t':
+		/* Select next menu item */
 		do {
-		    current ++ ;
-		    if( !menuItems[current].key )
-			current = 0 ;
-		} while( !strchr( available, menuItems[current].key ));
+			current++;
+			if (!menuItems[current].key)
+				current = 0;
+		} while (!strchr(available, menuItems[current].key));
 		key = 0;
-	}
-
-	if (key == MENU_LEFT) {
-		 do {
-		     current -- ;
-		     if( current < 0 ) {
-			 while( menuItems[current + 1].key )
-			      current ++ ;
-		     }
-		 } while( !strchr( available, menuItems[current].key ));
-		 key = 0;
+		break;
+	case KEY_LEFT:
+	case KEY_BTAB:	/* Back tab */
+		/* Select previous menu item */
+		do {
+			current--;
+			if (current < 0) {
+				while (menuItems[current + 1].key)
+					current++;
+			}
+		} while (!strchr(available, menuItems[current].key));
+		key = 0;
+		break;
+	case KEY_ENTER:
+	case '\n':
+	case '\r':
+		/* Enter equals the keyboard shortcut of current menu item */
+		key = menuItems[current].key;
+		break;
 	}
 
         /* Should all keys to be accepted? */
@@ -1216,6 +1184,8 @@ menuSelect( int y, int x, struct MenuItem *menuItems, int itemLength,
         }
     }
 
+    keypad(stdscr, FALSE);
+
     /* Clear out prompts and such */
     clear_warning();
     for( i = y; i <= ylast; i ++ ) {
-- 
1.7.1

--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux