From: Denys Vlasenko <vda.linux@googlemail.com>
Date: Mon, 22 Oct 2012 16:49:41 +0200
Subject: [PATCH 1/5] keyboard input: simplify code, no logic changes
This change slightly simplifies and rearranges the code
in get_key_code(), reduces indentation levels there,
adds a few comments. The logic remains the same.
This is a preparatory patch for subsequent changes.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
---
lib/tty/key.c | 126 ++++++++++++++++++++++++++++----------------------------
1 files changed, 63 insertions(+), 63 deletions(-)
diff --git a/lib/tty/key.c b/lib/tty/key.c
index f050202..d2142fa 100644
a
|
b
|
get_key_code (int no_delay) |
1847 | 1847 | pending_keys = NULL; |
1848 | 1848 | seq_append = NULL; |
1849 | 1849 | } |
1850 | | if ((d == ESC_CHAR) && (pending_keys != NULL)) |
| 1850 | else if (d == ESC_CHAR) |
1851 | 1851 | { |
1852 | 1852 | d = ALT (*pending_keys++); |
1853 | 1853 | goto check_pend; |
… |
… |
get_key_code (int no_delay) |
1928 | 1928 | this = keys->child; |
1929 | 1929 | } |
1930 | 1930 | } |
| 1931 | |
1931 | 1932 | while (this != NULL) |
1932 | 1933 | { |
1933 | 1934 | if (c == this->ch) |
1934 | 1935 | { |
1935 | | if (this->child) |
1936 | | { |
1937 | | if (!push_char (c)) |
1938 | | { |
1939 | | pending_keys = seq_buffer; |
1940 | | goto pend_send; |
1941 | | } |
1942 | | parent = this; |
1943 | | this = this->child; |
1944 | | if (parent->action == MCKEY_ESCAPE && old_esc_mode) |
1945 | | { |
1946 | | if (no_delay) |
1947 | | { |
1948 | | GET_TIME (esctime); |
1949 | | if (this == NULL) |
1950 | | { |
1951 | | /* Shouldn't happen */ |
1952 | | fputs ("Internal error\n", stderr); |
1953 | | exit (EXIT_FAILURE); |
1954 | | } |
1955 | | goto nodelay_try_again; |
1956 | | } |
1957 | | esctime.tv_sec = -1; |
1958 | | c = xgetch_second (); |
1959 | | if (c == -1) |
1960 | | { |
1961 | | pending_keys = seq_append = NULL; |
1962 | | this = NULL; |
1963 | | return ESC_CHAR; |
1964 | | } |
1965 | | } |
1966 | | else |
1967 | | { |
1968 | | if (no_delay) |
1969 | | goto nodelay_try_again; |
1970 | | c = tty_lowlevel_getch (); |
1971 | | } |
1972 | | } |
1973 | | else |
| 1936 | if (!this->child) |
1974 | 1937 | { |
1975 | 1938 | /* We got a complete match, return and reset search */ |
1976 | 1939 | int code; |
… |
… |
get_key_code (int no_delay) |
1980 | 1943 | this = NULL; |
1981 | 1944 | return correct_key_code (code); |
1982 | 1945 | } |
1983 | | } |
1984 | | else |
1985 | | { |
1986 | | if (this->next != NULL) |
1987 | | this = this->next; |
1988 | | else |
| 1946 | /* No match yet, but it may be a prefix for a valid seq */ |
| 1947 | |
| 1948 | if (!push_char (c)) |
1989 | 1949 | { |
1990 | | if ((parent != NULL) && (parent->action == MCKEY_ESCAPE)) |
| 1950 | pending_keys = seq_buffer; |
| 1951 | goto pend_send; |
| 1952 | } |
| 1953 | parent = this; |
| 1954 | this = this->child; |
| 1955 | if (parent->action == MCKEY_ESCAPE && old_esc_mode) |
| 1956 | { |
| 1957 | if (no_delay) |
| 1958 | { |
| 1959 | GET_TIME (esctime); |
| 1960 | if (this == NULL) |
| 1961 | { |
| 1962 | /* Shouldn't happen */ |
| 1963 | fputs ("Internal error\n", stderr); |
| 1964 | exit (EXIT_FAILURE); |
| 1965 | } |
| 1966 | goto nodelay_try_again; |
| 1967 | } |
| 1968 | esctime.tv_sec = -1; |
| 1969 | c = xgetch_second (); |
| 1970 | if (c == -1) |
1991 | 1971 | { |
1992 | | /* Convert escape-digits to F-keys */ |
1993 | | if (g_ascii_isdigit (c)) |
1994 | | c = KEY_F (c - '0'); |
1995 | | else if (c == ' ') |
1996 | | c = ESC_CHAR; |
1997 | | else |
1998 | | c = ALT (c); |
1999 | | |
2000 | 1972 | pending_keys = seq_append = NULL; |
2001 | 1973 | this = NULL; |
2002 | | return correct_key_code (c); |
| 1974 | return ESC_CHAR; |
2003 | 1975 | } |
2004 | | /* Did not find a match or {c} was changed in the if above, |
2005 | | so we have to return everything we had skipped |
2006 | | */ |
2007 | | push_char (c); |
2008 | | pending_keys = seq_buffer; |
2009 | | goto pend_send; |
| 1976 | continue; |
2010 | 1977 | } |
| 1978 | if (no_delay) |
| 1979 | goto nodelay_try_again; |
| 1980 | c = tty_lowlevel_getch (); |
| 1981 | continue; |
2011 | 1982 | } |
2012 | | } |
| 1983 | |
| 1984 | /* c != this->ch. Try other keys with this prefix */ |
| 1985 | if (this->next != NULL) { |
| 1986 | this = this->next; |
| 1987 | continue; |
| 1988 | } |
| 1989 | |
| 1990 | /* No match found. Is it one of our ESC <key> specials? */ |
| 1991 | if ((parent != NULL) && (parent->action == MCKEY_ESCAPE)) |
| 1992 | { |
| 1993 | /* Convert escape-digits to F-keys */ |
| 1994 | if (g_ascii_isdigit (c)) |
| 1995 | c = KEY_F (c - '0'); |
| 1996 | else if (c == ' ') |
| 1997 | c = ESC_CHAR; |
| 1998 | else |
| 1999 | c = ALT (c); |
| 2000 | |
| 2001 | pending_keys = seq_append = NULL; |
| 2002 | this = NULL; |
| 2003 | return correct_key_code (c); |
| 2004 | } |
| 2005 | |
| 2006 | /* Unknown sequence. Maybe a prefix of a longer one. Save it. */ |
| 2007 | push_char (c); |
| 2008 | pending_keys = seq_buffer; |
| 2009 | goto pend_send; |
| 2010 | |
| 2011 | } /* while (this != NULL) */ |
| 2012 | |
2013 | 2013 | this = NULL; |
2014 | 2014 | return correct_key_code (c); |
2015 | 2015 | } |