FreeRDP
Loading...
Searching...
No Matches
SessionInputManager.java
1/*
2 Android Session Input Manager
3
4 Copyright 2026 Ibrahim Sevinc <ibrahim.sevinc.mail@gmail.com>
5
6 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7 If a copy of the MPL was not distributed with this file, You can obtain one at
8 http://mozilla.org/MPL/2.0/.
9*/
10
11package com.freerdp.freerdpcore.presentation;
12
13import android.content.Context;
14import android.graphics.Bitmap;
15import android.graphics.Point;
16import android.os.Handler;
17import android.os.Looper;
18import android.os.Message;
19import android.os.SystemClock;
20import android.util.Log;
21import android.view.KeyEvent;
22import android.view.MotionEvent;
23import android.view.ScaleGestureDetector;
24import android.view.View;
25import android.view.inputmethod.InputMethodManager;
26
27import com.freerdp.freerdpcore.services.LibFreeRDP;
28import com.freerdp.freerdpcore.utils.KeyboardMapper;
29import com.freerdp.freerdpcore.utils.Mouse;
30
31public class SessionInputManager
32 implements SessionView.SessionViewListener, TouchPointerView.TouchPointerListener,
33 KeyboardMapper.KeyProcessingListener, ExtendedKeyboardView.Listener
34{
35 private static final String TAG = "FreeRDP.SessionInputManager";
36
37 private static final int SCROLLING_TIMEOUT = 16;
38 private static final int SCROLLING_DISTANCE = 12;
39 private static final int SCROLLING_EDGE_MARGIN = 16;
40 private static final int MOVE_EVENT_THROTTLE_MS = 16;
41
42 private static final int MSG_SEND_MOVE_EVENT = 1;
43 private static final int MSG_SCROLLING_REQUESTED = 2;
44
45 private final Context context;
46 private final KeyboardMapper keyboardMapper;
47 private final ScrollView2D scrollView;
48 private final SessionView sessionView;
49 private final TouchPointerView touchPointerView;
50 private final ExtendedKeyboardView keyboard;
51 private final PinchZoomListener pinchZoomListener = new PinchZoomListener();
52
53 // Native FreeRDP instance handle. 0 until attachSession() is called (i.e. before connect).
54 private long instance = 0;
55 private Bitmap bitmap;
56 private int screenWidth;
57 private int screenHeight;
58 private long lastMoveSentTime = 0;
59 // we asked the IME to show; the window has not necessarily animated it in yet
60 private boolean softInputRequested = false;
61 // the IME reported a non-zero inset, i.e. it really is on screen
62 private boolean softInputVisible = false;
63
64 private final Handler handler;
65
66 public SessionInputManager(Context context, ScrollView2D scrollView, SessionView sessionView,
67 TouchPointerView touchPointerView, ExtendedKeyboardView keyboard)
68 {
69 this.context = context;
70 this.scrollView = scrollView;
71 this.sessionView = sessionView;
72 this.touchPointerView = touchPointerView;
73 this.keyboard = keyboard;
74 this.handler = new InputHandler();
75
76 this.keyboardMapper = new KeyboardMapper();
77 this.keyboardMapper.init(context);
78
79 keyboard.setListener(this);
80 }
81
82 // Binds this manager to a live FreeRDP session. Until called, all input events are dropped.
83 public void attachSession(long instance, Bitmap surface)
84 {
85 this.instance = instance;
86 this.bitmap = surface;
87 this.lastMoveSentTime = 0;
88 keyboardMapper.reset(this);
89 }
90
91 // Called when the session bitmap is created or replaced (OnSettingsChanged / OnGraphicsResize).
92 public void setBitmap(Bitmap bitmap)
93 {
94 this.bitmap = bitmap;
95 }
96
97 // Returns a listener that can be wired into a ScaleGestureDetector for pinch-to-zoom.
98 public ScaleGestureDetector.OnScaleGestureListener getPinchZoomListener()
99 {
100 return pinchZoomListener;
101 }
102
103 // Called once the screen dimensions are known (onGlobalLayout) and on bindSession.
104 public void setScreenSize(int width, int height)
105 {
106 this.screenWidth = width;
107 this.screenHeight = height;
108 }
109
110 // Shows or hides the key bar together with the system IME.
111 public void toggleKeyboard()
112 {
113 if (keyboard.getVisibility() == View.VISIBLE)
114 {
115 hideKeyboards();
116 }
117 else
118 {
119 keyboard.setExpanded(false, false);
120 keyboard.setVisibility(View.VISIBLE);
121 setSoftInputState(true);
122 }
123 }
124
125 // Called from onPause and back-press handling.
126 public void hideKeyboards()
127 {
128 keyboard.setExpanded(false, false);
129 keyboard.setVisibility(View.GONE);
130 setSoftInputState(false);
131 keyboardMapper.clearlAllModifiers();
132 // the IME dismiss animation may re-show the nav bar after the refresh above
133 scrollView.post(this::refreshSystemBars);
134 }
135
136 // Returns true if the back press was consumed by the keyboard.
137 public boolean handleKeyboardBack()
138 {
139 if (keyboard.getVisibility() != View.VISIBLE)
140 return false;
141
142 if (keyboard.isExpanded())
143 {
144 keyboard.setExpanded(false, false);
145 // deliberately no IME here, so the next back press hides the bar as well
146 refreshSystemBars();
147 scrollView.requestApplyInsets();
148 return true;
149 }
150
151 hideKeyboards();
152 return true;
153 }
154
155 // True if the system soft keyboard (IME) is up or on its way up.
156 public boolean isSoftInputActive()
157 {
158 return keyboard.getVisibility() == View.VISIBLE && (softInputRequested || softInputVisible);
159 }
160
161 // Fed from the window insets listener. The IME only counts as gone once it has been seen on
162 // screen: the insets pass right after showSoftInput() still reports a zero inset.
163 public void onImeVisibilityChanged(boolean visible)
164 {
165 if (visible == softInputVisible)
166 return;
167 softInputVisible = visible;
168 if (!visible)
169 softInputRequested = false;
170 refreshSystemBars();
171 }
172
173 private void refreshSystemBars()
174 {
175 if (context instanceof SessionActivity)
176 ((SessionActivity)context).hideSystemBars();
177 }
178
179 private void setSoftInputState(boolean state)
180 {
181 softInputRequested = state;
182 if (!state)
183 softInputVisible = false;
184 InputMethodManager mgr =
185 (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
186
187 if (state)
188 {
189 sessionView.requestFocus();
190 mgr.showSoftInput(sessionView, InputMethodManager.SHOW_IMPLICIT);
191 }
192 else
193 {
194 mgr.hideSoftInputFromWindow(sessionView.getWindowToken(), 0);
195 }
196 refreshSystemBars();
197 scrollView.requestApplyInsets();
198 }
199
200 // Cancels any pending delayed-move events; called on connection failure / disconnect.
201 public void cancelPendingEvents()
202 {
203 handler.removeMessages(MSG_SEND_MOVE_EVENT);
204 }
205
206 // Forwards a physical-mouse scroll event (e.g. external mouse wheel) into the session.
207 public boolean onGenericMotionEvent(MotionEvent e)
208 {
209 if (instance == 0)
210 return false;
211 if (e.getAction() != MotionEvent.ACTION_SCROLL)
212 return false;
213
214 final float vScroll = e.getAxisValue(MotionEvent.AXIS_VSCROLL);
215 final float hScroll = e.getAxisValue(MotionEvent.AXIS_HSCROLL);
216 if (vScroll != 0)
217 onSessionViewScroll(vScroll > 0);
218 if (hScroll != 0)
219 onSessionViewHScroll(hScroll > 0);
220 return true;
221 }
222
223 // Forwards an Android hardware-keyboard event into the session.
224 public boolean onAndroidKeyEvent(KeyEvent event)
225 {
226 if (instance == 0)
227 return false;
228 // Read on every key, so a change in the settings applies without reconnecting.
229 keyboardMapper.setPhysicalKeyboardDeviceLayout(
230 ApplicationSettingsActivity.getPhysicalKeyboardDeviceLayout(context));
231 return keyboardMapper.processAndroidKeyEvent(event);
232 }
233
234 // Handles a long-press on the BACK key by disconnecting the active session.
235 // Returns true if the event was consumed.
236 public boolean onAndroidKeyLongPress(int keyCode)
237 {
238 if (instance == 0)
239 return false;
240 if (keyCode == KeyEvent.KEYCODE_BACK)
241 {
242 LibFreeRDP.disconnect(instance);
243 return true;
244 }
245 return false;
246 }
247
248 // If the "use back as Alt+F4" preference is enabled, sends Alt+F4 and returns true.
249 public boolean handleBackAsAltF4()
250 {
251 if (instance == 0)
252 return false;
253 if (!ApplicationSettingsActivity.getUseBackAsAltf4(context))
254 return false;
255 keyboardMapper.sendAltF4();
256 return true;
257 }
258
259 // Toggles touch-pointer overlay visibility (driven by the menu).
260 public void toggleTouchPointer()
261 {
262 if (touchPointerView.getVisibility() == View.VISIBLE)
263 {
264 touchPointerView.setVisibility(View.INVISIBLE);
265 sessionView.setTouchPointerPadding(0, 0);
266 }
267 else
268 {
269 touchPointerView.setVisibility(View.VISIBLE);
270 sessionView.setTouchPointerPadding(touchPointerView.getPointerWidth(),
271 touchPointerView.getPointerHeight());
272 }
273 }
274
275 // ****************************************************************************
276 // Private helpers
277
278 private void sendDelayedMoveEvent(int x, int y)
279 {
280 final long now = SystemClock.uptimeMillis();
281 final long elapsed = now - lastMoveSentTime;
282
283 cancelDelayedMoveEvent();
284 if (elapsed >= MOVE_EVENT_THROTTLE_MS)
285 {
286 lastMoveSentTime = now;
287 LibFreeRDP.sendCursorEvent(instance, x, y, Mouse.getMoveEvent());
288 }
289 else
290 {
291 final long delay = MOVE_EVENT_THROTTLE_MS - elapsed;
292 handler.sendMessageDelayed(Message.obtain(null, MSG_SEND_MOVE_EVENT, x, y), delay);
293 }
294 }
295
296 private void cancelDelayedMoveEvent()
297 {
298 handler.removeMessages(MSG_SEND_MOVE_EVENT);
299 }
300
301 private Point mapScreenCoordToSessionCoord(int x, int y)
302 {
303 View container = scrollView.getChildCount() > 0 ? scrollView.getChildAt(0) : sessionView;
304 int mappedX = (int)((float)(x - container.getLeft() + scrollView.getScrollX()) /
305 sessionView.getZoom());
306 int mappedY = (int)((float)(y - container.getTop() + scrollView.getScrollY()) /
307 sessionView.getZoom());
308 if (bitmap != null)
309 {
310 if (mappedX < 0)
311 mappedX = 0;
312 if (mappedY < 0)
313 mappedY = 0;
314 if (mappedX > bitmap.getWidth())
315 mappedX = bitmap.getWidth();
316 if (mappedY > bitmap.getHeight())
317 mappedY = bitmap.getHeight();
318 }
319 return new Point(mappedX, mappedY);
320 }
321
322 // ****************************************************************************
323 // SessionView.SessionViewListener
324
325 @Override public void onSessionViewBeginTouch()
326 {
327 scrollView.setScrollEnabled(false);
328 }
329
330 @Override public void onSessionViewEndTouch()
331 {
332 scrollView.setScrollEnabled(true);
333 }
334
335 @Override public void onSessionViewLeftTouch(int x, int y, boolean down)
336 {
337 if (instance == 0)
338 return;
339 cancelDelayedMoveEvent();
340 LibFreeRDP.sendCursorEvent(instance, x, y, Mouse.getLeftButtonEvent(context, down));
341 }
342
343 @Override public void onSessionViewMiddleTouch(int x, int y, boolean down)
344 {
345 if (instance == 0)
346 return;
347 cancelDelayedMoveEvent();
348 LibFreeRDP.sendCursorEvent(instance, x, y, Mouse.getMiddleButtonEvent(down));
349 }
350
351 @Override public void onSessionViewRightTouch(int x, int y, boolean down)
352 {
353 if (instance == 0)
354 return;
355 cancelDelayedMoveEvent();
356 LibFreeRDP.sendCursorEvent(instance, x, y, Mouse.getRightButtonEvent(context, down));
357 }
358
359 @Override public void onSessionViewMove(int x, int y)
360 {
361 if (instance == 0)
362 return;
363 sendDelayedMoveEvent(x, y);
364 }
365
366 @Override public void onSessionViewMouseMove(int x, int y)
367 {
368 if (instance == 0)
369 return;
370 LibFreeRDP.sendCursorEvent(instance, x, y, Mouse.getMoveEvent());
371 }
372
373 @Override public void onSessionViewScroll(boolean down)
374 {
375 if (instance == 0)
376 return;
377 LibFreeRDP.sendCursorEvent(instance, 0, 0, Mouse.getScrollEvent(context, down));
378 }
379
380 @Override public void onSessionViewHScroll(boolean right)
381 {
382 if (instance == 0)
383 return;
384 LibFreeRDP.sendCursorEvent(instance, 0, 0, Mouse.getHScrollEvent(context, right));
385 }
386
387 // ****************************************************************************
388 // TouchPointerView.TouchPointerListener
389
390 @Override public void onTouchPointerClose()
391 {
392 touchPointerView.setVisibility(View.INVISIBLE);
393 sessionView.setTouchPointerPadding(0, 0);
394 }
395
396 @Override public void onTouchPointerLeftClick(int x, int y, boolean down)
397 {
398 if (instance == 0)
399 return;
400 Point p = mapScreenCoordToSessionCoord(x, y);
401 LibFreeRDP.sendCursorEvent(instance, p.x, p.y, Mouse.getLeftButtonEvent(context, down));
402 }
403
404 @Override public void onTouchPointerRightClick(int x, int y, boolean down)
405 {
406 if (instance == 0)
407 return;
408 Point p = mapScreenCoordToSessionCoord(x, y);
409 LibFreeRDP.sendCursorEvent(instance, p.x, p.y, Mouse.getRightButtonEvent(context, down));
410 }
411
412 @Override public void onTouchPointerMiddleClick(int x, int y, boolean down)
413 {
414 if (instance == 0)
415 return;
416 Point p = mapScreenCoordToSessionCoord(x, y);
417 LibFreeRDP.sendCursorEvent(instance, p.x, p.y, Mouse.getMiddleButtonEvent(down));
418 }
419
420 @Override public void onTouchPointerMove(int x, int y)
421 {
422 if (instance == 0)
423 return;
424 Point p = mapScreenCoordToSessionCoord(x, y);
425 LibFreeRDP.sendCursorEvent(instance, p.x, p.y, Mouse.getMoveEvent());
426
427 if (ApplicationSettingsActivity.getAutoScrollTouchPointer(context) &&
428 !handler.hasMessages(MSG_SCROLLING_REQUESTED))
429 {
430 handler.sendEmptyMessageDelayed(MSG_SCROLLING_REQUESTED, SCROLLING_TIMEOUT);
431 }
432 }
433
434 @Override public void onTouchPointerMoveEnd()
435 {
436 handler.removeMessages(MSG_SCROLLING_REQUESTED);
437 }
438
439 @Override public void onTouchPointerScroll(int amount)
440 {
441 if (instance == 0)
442 return;
443 LibFreeRDP.sendCursorEvent(instance, 0, 0, Mouse.getScrollEvent(context, amount));
444 }
445
446 @Override public void onTouchPointerHScroll(int amount)
447 {
448 if (instance == 0)
449 return;
450 LibFreeRDP.sendCursorEvent(instance, 0, 0, Mouse.getHScrollEvent(context, amount));
451 }
452
453 @Override public void onTouchPointerToggleKeyboard()
454 {
455 toggleKeyboard();
456 }
457
458 @Override public void onTouchPointerResetScrollZoom()
459 {
460 sessionView.setZoom(1.0f);
461 scrollView.scrollTo(0, 0);
462 }
463
464 // ****************************************************************************
465 // KeyboardMapper.KeyProcessingListener
466
467 @Override public void processVirtualKey(int virtualKeyCode, boolean down)
468 {
469 if (instance == 0)
470 return;
471 LibFreeRDP.sendKeyEvent(instance, virtualKeyCode, down);
472 }
473
474 @Override public void processUnicodeKey(int unicodeKey)
475 {
476 if (instance == 0)
477 return;
478 if (LibFreeRDP.isUnicodeInputSupported(instance))
479 {
480 LibFreeRDP.sendUnicodeKeyEvent(instance, unicodeKey, true);
481 LibFreeRDP.sendUnicodeKeyEvent(instance, unicodeKey, false);
482 }
483 else
484 keyboardMapper.processUnicodeFallback(unicodeKey);
485 }
486
487 @Override public boolean isUnicodeInputSupported()
488 {
489 return instance != 0 && LibFreeRDP.isUnicodeInputSupported(instance);
490 }
491
492 @Override public void processKeyAsUnicode(int virtualKeyCode, int unicodeKey, int modmask)
493 {
494 if (instance == 0)
495 return;
496 LibFreeRDP.sendKeyAsUnicodeEvent(instance, virtualKeyCode, unicodeKey, modmask, true);
497 LibFreeRDP.sendKeyAsUnicodeEvent(instance, virtualKeyCode, unicodeKey, modmask, false);
498 }
499
500 @Override public void switchKeyboard(int keyboardType)
501 {
502 switch (keyboardType)
503 {
504 case KeyboardMapper.KEYBOARD_TYPE_FUNCTIONKEYS:
505 keyboard.selectPage(ExtendedKeyboardView.PAGE_SPECIAL);
506 break;
507
508 case KeyboardMapper.KEYBOARD_TYPE_NUMPAD:
509 keyboard.selectPage(ExtendedKeyboardView.PAGE_NUM);
510 break;
511
512 default:
513 break;
514 }
515 }
516
517 @Override public void modifiersChanged()
518 {
519 keyboard.refreshModifiers();
520 }
521
522 // ****************************************************************************
523 // ExtendedKeyboardView.Listener
524
525 @Override public void onKey(int keycode)
526 {
527 keyboardMapper.processCustomKeyEvent(keycode);
528 }
529
530 @Override public void onKeyLock(int keycode)
531 {
532 keyboardMapper.processCustomKeyLock(keycode);
533 }
534
535 @Override public int getModifierState(int keycode)
536 {
537 return keyboardMapper.getModifierState(keycode);
538 }
539
540 @Override public void onExpandedChanged(boolean expanded)
541 {
542 // the expanded panel replaces the system IME; collapsing brings it back
543 setSoftInputState(!expanded);
544 }
545
546 // ****************************************************************************
547 // Internal delayed-event handler
548
549 private class InputHandler extends Handler
550 {
551 InputHandler()
552 {
553 super(Looper.getMainLooper());
554 }
555
556 @Override public void handleMessage(Message msg)
557 {
558 switch (msg.what)
559 {
560 case MSG_SEND_MOVE_EVENT:
561 if (instance == 0)
562 break;
563 lastMoveSentTime = SystemClock.uptimeMillis();
564 LibFreeRDP.sendCursorEvent(instance, msg.arg1, msg.arg2, Mouse.getMoveEvent());
565 break;
566
567 case MSG_SCROLLING_REQUESTED:
568 {
569 int scrollX = 0;
570 int scrollY = 0;
571 float[] pointerPos = touchPointerView.getPointerPosition();
572 final int ow = touchPointerView.getWidth();
573 final int oh = touchPointerView.getHeight();
574 final int pw = touchPointerView.getPointerWidth();
575 final int ph = touchPointerView.getPointerHeight();
576
577 if (pointerPos[0] >= ow - pw - SCROLLING_EDGE_MARGIN)
578 scrollX = SCROLLING_DISTANCE;
579 else if (pointerPos[0] <= SCROLLING_EDGE_MARGIN)
580 scrollX = -SCROLLING_DISTANCE;
581
582 if (pointerPos[1] >= oh - ph - SCROLLING_EDGE_MARGIN)
583 scrollY = SCROLLING_DISTANCE;
584 else if (pointerPos[1] <= SCROLLING_EDGE_MARGIN)
585 scrollY = -SCROLLING_DISTANCE;
586
587 scrollView.scrollBy(scrollX, scrollY);
588
589 final int maxX = sessionView.getWidth() - scrollView.getWidth();
590 final int maxY = sessionView.getHeight() - scrollView.getHeight();
591 if ((scrollX < 0 && scrollView.getScrollX() <= 0) ||
592 (scrollX > 0 && scrollView.getScrollX() >= maxX))
593 scrollX = 0;
594 if ((scrollY < 0 && scrollView.getScrollY() <= 0) ||
595 (scrollY > 0 && scrollView.getScrollY() >= maxY))
596 scrollY = 0;
597
598 if (scrollX != 0 || scrollY != 0)
599 handler.sendEmptyMessageDelayed(MSG_SCROLLING_REQUESTED, SCROLLING_TIMEOUT);
600 break;
601 }
602 }
603 }
604 }
605
606 // ****************************************************************************
607 // Pinch-to-zoom listener (wired into SessionView's ScaleGestureDetector)
608
609 private class PinchZoomListener extends ScaleGestureDetector.SimpleOnScaleGestureListener
610 {
611 private float scaleFactor = 1.0f;
612
613 @Override public boolean onScaleBegin(ScaleGestureDetector detector)
614 {
615 scrollView.setScrollEnabled(false);
616 return true;
617 }
618
619 @Override public boolean onScale(ScaleGestureDetector detector)
620 {
621 // calc scale factor
622 scaleFactor *= detector.getScaleFactor();
623 scaleFactor = Math.max(SessionView.MIN_SCALE_FACTOR,
624 Math.min(scaleFactor, SessionView.MAX_SCALE_FACTOR));
625 sessionView.setZoom(scaleFactor);
626
627 if (!sessionView.isAtMinZoom() && !sessionView.isAtMaxZoom())
628 {
629 // transform scroll origin to the new zoom space
630 float transOriginX = scrollView.getScrollX() * detector.getScaleFactor();
631 float transOriginY = scrollView.getScrollY() * detector.getScaleFactor();
632
633 // transform center point to the zoomed space
634 float transCenterX =
635 (scrollView.getScrollX() + detector.getFocusX()) * detector.getScaleFactor();
636 float transCenterY =
637 (scrollView.getScrollY() + detector.getFocusY()) * detector.getScaleFactor();
638
639 // scroll by the difference between the distance of the
640 // transformed center/origin point and their old distance
641 // (focusX/Y)
642 scrollView.scrollBy((int)((transCenterX - transOriginX) - detector.getFocusX()),
643 (int)((transCenterY - transOriginY) - detector.getFocusY()));
644 }
645
646 return true;
647 }
648
649 @Override public void onScaleEnd(ScaleGestureDetector de)
650 {
651 scrollView.setScrollEnabled(true);
652 }
653 }
654}