Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement basic mouse support #56

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions app/src/main/java/com/termux/gui/protocol/protobuf/ProtoUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.text.TextWatcher
import android.util.DisplayMetrics
import android.util.Log
import android.util.TypedValue
import android.view.InputDevice
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.View
Expand Down Expand Up @@ -215,6 +216,17 @@ class ProtoUtils {
map[MotionEvent.ACTION_MOVE] = GUIProt0.TouchEvent.Action.move
TOUCH_EVENT_MAP = Collections.unmodifiableMap(map)
}

private val MOUSE_EVENT_MAP: Map<Int, GUIProt0.MouseEvent.Action>
init {
val map = HashMap<Int, GUIProt0.MouseEvent.Action>()
map[MotionEvent.ACTION_HOVER_ENTER] = GUIProt0.MouseEvent.Action.hoverEnter
map[MotionEvent.ACTION_HOVER_EXIT] = GUIProt0.MouseEvent.Action.hoverExit
map[MotionEvent.ACTION_HOVER_MOVE] = GUIProt0.MouseEvent.Action.hoverMove
map[MotionEvent.ACTION_BUTTON_PRESS] = GUIProt0.MouseEvent.Action.buttonPress
map[MotionEvent.ACTION_BUTTON_RELEASE] = GUIProt0.MouseEvent.Action.buttonRelease
MOUSE_EVENT_MAP = Collections.unmodifiableMap(map)
}

fun keyListener(eventQueue: LinkedBlockingQueue<GUIProt0.Event>, v: GUIProt0.View): OnKeyListener {
return OnKeyListener { _, _, event ->
Expand Down Expand Up @@ -309,8 +321,24 @@ class ProtoUtils {
}
true
}
v.setOnGenericMotionListener { _, event ->
if (event.isFromSource(InputDevice.SOURCE_CLASS_POINTER)) {
val mapped = MOUSE_EVENT_MAP[event.actionMasked]
if (mapped != null) {
val c = GUIProt0.MouseEvent.newBuilder()
c.setV(GUIProt0.View.newBuilder().setAid(aid).setId(v.id))
c.action = mapped
c.button = event.actionButton
c.pointer = GUIProt0.MouseEvent.Pointer.newBuilder().setX(event.x.roundToInt()).setY(event.y.roundToInt()).build()
c.time = event.eventTime
eventQueue.offer(GUIProt0.Event.newBuilder().setMouse(c).build())
}
}
true
}
} else {
v.setOnTouchListener(null)
v.setOnGenericMotionListener(null)
}
}

Expand Down
31 changes: 30 additions & 1 deletion app/src/main/proto/GUIProt0.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2879,6 +2879,34 @@ message TouchEvent {
uint64 time = 5;
}

message MouseEvent {
View v = 1;
/*
See the Android documentation for MotionEvent for info.
*/
enum Action {
hoverEnter = 0;
hoverExit = 1;
hoverMove = 2;
buttonPress = 3;
buttonRelease = 4;
}
Action action = 2;
int32 button = 3;
message Pointer {
/*
Coordinates inside the View.
*/
int32 x = 1;
int32 y = 2;
}
Pointer pointer = 4;
/*
A timestamp for when the event happened, use for gestures and the like.
*/
uint64 time = 5;
}

/*
Send when a SwipeRefreshLayout is refreshed. Automatically enabled.
*/
Expand Down Expand Up @@ -3172,7 +3200,8 @@ message Event {
ClickEvent click = 1;
TouchEvent touch = 2;
TextEvent text = 3;

MouseEvent mouse = 4;


CreateEvent create = 16;
StartEvent start = 17;
Expand Down