tuikit/driver/curses.py
changeset 109 105b1affc3c2
parent 97 0c2e0c09ba5c
child 113 6796adfdc7eb
equal deleted inserted replaced
97:0c2e0c09ba5c 109:105b1affc3c2
    41         (0x42,              1,      'down'          ),
    41         (0x42,              1,      'down'          ),
    42         (0x43,              1,      'right'         ),
    42         (0x43,              1,      'right'         ),
    43         (0x44,              1,      'left'          ),
    43         (0x44,              1,      'left'          ),
    44         (0x46,              1,      'end'           ),  # xterm
    44         (0x46,              1,      'end'           ),  # xterm
    45         (0x48,              1,      'home'          ),  # xterm
    45         (0x48,              1,      'home'          ),  # xterm
       
    46         (0x5a,              1,      'shift+tab'     ),  # xterm
    46         (0x5b, 0x41,        1,      'f1'            ),  # linux
    47         (0x5b, 0x41,        1,      'f1'            ),  # linux
    47         (0x5b, 0x42,        1,      'f2'            ),  # linux
    48         (0x5b, 0x42,        1,      'f2'            ),  # linux
    48         (0x5b, 0x43,        1,      'f3'            ),  # linux
    49         (0x5b, 0x43,        1,      'f3'            ),  # linux
    49         (0x5b, 0x44,        1,      'f4'            ),  # linux
    50         (0x5b, 0x44,        1,      'f4'            ),  # linux
    50         (0x5b, 0x45,        1,      'f5'            ),  # linux
    51         (0x5b, 0x45,        1,      'f5'            ),  # linux
    77         'blink':        curses.A_BLINK,
    78         'blink':        curses.A_BLINK,
    78     }
    79     }
    79 
    80 
    80     def __init__(self):
    81     def __init__(self):
    81         Driver.__init__(self)
    82         Driver.__init__(self)
    82         self._log = logging.getLogger('tuikit')
    83         self._log = logging.getLogger(__name__)
    83         self.stdscr = None
    84         self.stdscr = None
    84         self.cursor = None
    85         self.cursor = None
    85         self.colors = {}     # maps names to curses attributes
    86         self.colors = {}     # maps names to curses attributes
    86         self.colorpairs = {} # maps tuple (fg,bg) to curses color_pair
    87         self.colorpairs = {} # maps tuple (fg,bg) to curses color_pair
    87         self.colorstack = [] # pushcolor/popcolor puts or gets attributes from this
    88         self.colorstack = [] # pushcolor/popcolor puts or gets attributes from this
   376                 self._inputqueue_unget(consumed[-1])
   377                 self._inputqueue_unget(consumed[-1])
   377                 del consumed[-1]
   378                 del consumed[-1]
   378             keyname = match[-1]
   379             keyname = match[-1]
   379 
   380 
   380         if match is None:
   381         if match is None:
   381             self.log.debug('Unknown control sequence: %s',
   382             self._log.debug('Unknown control sequence: %s',
   382                            ','.join(['0x%x' % x for x in consumed]))
   383                            ','.join(['0x%x' % x for x in consumed]))
   383             return [('keypress', 'Unknown', None, set())]
   384             return [('keypress', 'Unknown', None, set())]
   384 
   385 
   385         if keyname == 'mouse':
   386         if keyname == 'mouse':
   386             return self._process_xterm_mouse()
   387             return self._process_xterm_mouse()
   458             codes = matching_codes
   459             codes = matching_codes
   459 
   460 
   460             if len(codes) == 0:
   461             if len(codes) == 0:
   461                 # no match -> unknown code
   462                 # no match -> unknown code
   462                 seq = ','.join(['0x%x' % x for x in debug_seq])
   463                 seq = ','.join(['0x%x' % x for x in debug_seq])
   463                 self.log.debug('Unknown control sequence: %s', seq)
   464                 self._log.debug('Unknown control sequence: %s', seq)
   464                 return [('keypress', 'Unknown:' + seq, None, set())]
   465                 return [('keypress', 'Unknown:' + seq, None, set())]
   465             elif len(codes) == 1:
   466             elif len(codes) == 1:
   466                 # one match -> we got the winner
   467                 # one match -> we got the winner
   467                 break
   468                 break
   468             elif len(codes[0]) == 2:
   469             elif len(codes[0]) == 2:
   481                 matching_codes.append(code)
   482                 matching_codes.append(code)
   482 
   483 
   483         if len(matching_codes) == 0:
   484         if len(matching_codes) == 0:
   484             # no match -> unknown code
   485             # no match -> unknown code
   485             seq = ','.join(['0x%x' % x for x in debug_seq])
   486             seq = ','.join(['0x%x' % x for x in debug_seq])
   486             self.log.debug('Unknown control sequence: %s', seq)
   487             self._log.debug('Unknown control sequence: %s', seq)
   487             return [('keypress', 'Unknown:' + seq, None, set())]
   488             return [('keypress', 'Unknown:' + seq, None, set())]
   488 
   489 
   489         if len(matching_codes) > 1:
   490         if len(matching_codes) > 1:
   490             raise Exception('Internal error: invalid csi_codes, more than one matching')
   491             raise Exception('Internal error: invalid csi_codes, more than one matching')
   491 
   492 
   500         mod_set = set()
   501         mod_set = set()
   501         for bit, name in enumerate(('shift', 'alt', 'ctrl', 'meta')):
   502         for bit, name in enumerate(('shift', 'alt', 'ctrl', 'meta')):
   502             if mod_bits & 1<<bit:
   503             if mod_bits & 1<<bit:
   503                 mod_set.add(name)
   504                 mod_set.add(name)
   504 
   505 
       
   506         # parse keynames in form "shift+tab"
       
   507         if '+' in keyname:
       
   508             parts = keyname.split('+')
       
   509             for mod in parts[:-1]:
       
   510                 assert(mod in ('shift', 'alt', 'ctrl', 'meta'))
       
   511                 mod_set.add(mod)
       
   512             keyname = parts[-1]
       
   513 
   505         return [('keypress', keyname, None, mod_set)]
   514         return [('keypress', keyname, None, mod_set)]
   506 
   515 
   507 
   516 
   508 driver_class = CursesDriver
   517 driver_class = CursesDriver