| 1 | package com.googlecode.lanterna.input; |
| 2 | |
| 3 | import com.googlecode.lanterna.TerminalPosition; |
| 4 | |
| 5 | import java.util.List; |
| 6 | |
| 7 | /** |
| 8 | * Pattern used to detect Xterm-protocol mouse events coming in on the standard input channel |
| 9 | * Created by martin on 19/07/15. |
| 10 | * |
| 11 | * @author Martin, Andreas |
| 12 | */ |
| 13 | public class MouseCharacterPattern implements CharacterPattern { |
| 14 | private static final char[] PATTERN = { KeyDecodingProfile.ESC_CODE, '[', 'M' }; |
| 15 | |
| 16 | @Override |
| 17 | public Matching match(List<Character> seq) { |
| 18 | int size = seq.size(); |
| 19 | if (size > 6) { |
| 20 | return null; // nope |
| 21 | } |
| 22 | // check first 3 chars: |
| 23 | for (int i = 0; i < 3; i++) { |
| 24 | if ( i >= size ) { |
| 25 | return Matching.NOT_YET; // maybe later |
| 26 | } |
| 27 | if ( seq.get(i) != PATTERN[i] ) { |
| 28 | return null; // nope |
| 29 | } |
| 30 | } |
| 31 | if (size < 6) { |
| 32 | return Matching.NOT_YET; // maybe later |
| 33 | } |
| 34 | MouseActionType actionType = null; |
| 35 | int button = (seq.get(3) & 0x3) + 1; |
| 36 | if(button == 4) { |
| 37 | //If last two bits are both set, it means button click release |
| 38 | button = 0; |
| 39 | } |
| 40 | int actionCode = (seq.get(3) & 0x60) >> 5; |
| 41 | switch(actionCode) { |
| 42 | case(1): |
| 43 | if(button > 0) { |
| 44 | actionType = MouseActionType.CLICK_DOWN; |
| 45 | } |
| 46 | else { |
| 47 | actionType = MouseActionType.CLICK_RELEASE; |
| 48 | } |
| 49 | break; |
| 50 | case(2): |
| 51 | if(button == 0) { |
| 52 | actionType = MouseActionType.MOVE; |
| 53 | } |
| 54 | else { |
| 55 | actionType = MouseActionType.DRAG; |
| 56 | } |
| 57 | break; |
| 58 | case(3): |
| 59 | if(button == 1) { |
| 60 | actionType = MouseActionType.SCROLL_UP; |
| 61 | button = 4; |
| 62 | } |
| 63 | else { |
| 64 | actionType = MouseActionType.SCROLL_DOWN; |
| 65 | button = 5; |
| 66 | } |
| 67 | break; |
| 68 | } |
| 69 | TerminalPosition pos = new TerminalPosition( seq.get(4) - 33, seq.get(5) - 33 ); |
| 70 | |
| 71 | MouseAction ma = new MouseAction(actionType, button, pos ); |
| 72 | return new Matching( ma ); // yep |
| 73 | } |
| 74 | } |