3단계: 입력 주입, 창 크기 조절, GPU 디코더 동시 실행 문제 수정
- 기기: 터치(멀티 포인터)·스크롤·키·텍스트 주입. 키맵으로 만들 수 없는 문자(한글 등)는 클립보드에 넣고 붙여넣기, 세션 종료 시 눌린 터치·키 정리, 회전 등으로 프레임 크기가 다르면 터치를 버림 - 호스트: 왼쪽 버튼 터치/드래그/롱프레스, 휠 스크롤(1/120 노치), 키보드(문자는 TEXT_INPUT, 특수키는 KEY_EVENT, Ctrl+C/V/X/A는 SYSTEM_ACTION), 한글 IME 조합 표시와 완성 문자 전송, 연결 중 egui 포커스 이동 차단 - 창 가장자리·모서리를 끌어 크기 조절, 끄는 동안에도 휴대폰 화면 비율 유지 - GPU(NVDEC) 디코더 생성·디코딩·해제를 프로세스 전체에서 직렬화해 동시 실행 시 드라이버에서 멈추던 문제 수정 - probe에 탭·스와이프·스크롤·텍스트·키 명령, 입력·크기 조절 상호작용 테스트 - 실제 Galaxy S21+에서 스와이프, 영문·한글 입력, Backspace, 스크롤 동작 확인
- 작성자
- LeeJunyoung
- 작성 시각
- 커밋
531b39c831b001612e90a803d7f9db09c3e6c25d- 부모
- b424ee6
변경된 파일 13개
+2,175-18| @@ -0,0 +1,240 @@ | ||
| 1 | 추가: package com.androidremote.daemon | |
| 2 | 추가: | |
| 3 | 추가: import android.content.ClipData | |
| 4 | 추가: import android.os.IBinder | |
| 5 | 추가: import android.os.SystemClock | |
| 6 | 추가: import android.view.InputDevice | |
| 7 | 추가: import android.view.KeyCharacterMap | |
| 8 | 추가: import android.view.KeyEvent | |
| 9 | 추가: import android.view.MotionEvent | |
| 10 | 추가: import java.lang.reflect.Method | |
| 11 | 추가: | |
| 12 | 추가: /** | |
| 13 | 추가: * 호스트 입력을 Android 입력 이벤트로 바꿔 주입한다 (기획서 7.3 4~5항, 3.1). | |
| 14 | 추가: * 세션마다 하나씩 두고 제어 연결 스레드에서만 호출한다. | |
| 15 | 추가: */ | |
| 16 | 추가: class InputInjector { | |
| 17 | 추가: private class Pointer(val id: Int, var x: Float, var y: Float, var pressure: Float) | |
| 18 | 추가: | |
| 19 | 추가: private val pointers = ArrayList<Pointer>() | |
| 20 | 추가: private var touchDownTime = 0L | |
| 21 | 추가: private val keyDownTimes = HashMap<Int, Long>() | |
| 22 | 추가: private var displayCache: Pair<Int, Int>? = null | |
| 23 | 추가: private var displayCachedAt = 0L | |
| 24 | 추가: | |
| 25 | 추가: /** 현재 디스플레이 크기. 회전에 따라 바뀌므로 0.5초마다 다시 읽는다. */ | |
| 26 | 추가: private fun display(): Pair<Int, Int> { | |
| 27 | 추가: val now = SystemClock.uptimeMillis() | |
| 28 | 추가: if (displayCache == null || now - displayCachedAt > 500) { | |
| 29 | 추가: displayCache = SystemAccess.displaySize() ?: displayCache ?: (1080 to 2400) | |
| 30 | 추가: displayCachedAt = now | |
| 31 | 추가: } | |
| 32 | 추가: return displayCache!! | |
| 33 | 추가: } | |
| 34 | 추가: | |
| 35 | 추가: /** 영상 프레임 좌표를 실제 디스플레이 좌표로 바꾼다. */ | |
| 36 | 추가: private fun toDisplay(x: Int, y: Int, fw: Int, fh: Int): Pair<Float, Float> { | |
| 37 | 추가: val (dw, dh) = display() | |
| 38 | 추가: return (x + 0.5f) * dw / fw to (y + 0.5f) * dh / fh | |
| 39 | 추가: } | |
| 40 | 추가: | |
| 41 | 추가: fun touch(t: Messages.TouchEvent) { | |
| 42 | 추가: val (x, y) = toDisplay(t.x, t.y, t.frameWidth, t.frameHeight) | |
| 43 | 추가: val now = SystemClock.uptimeMillis() | |
| 44 | 추가: val pressure = t.pressure / 65535f | |
| 45 | 추가: val action: Int | |
| 46 | 추가: when (t.action) { | |
| 47 | 추가: TouchAction.DOWN -> { | |
| 48 | 추가: if (pointers.any { it.id == t.pointerId }) return | |
| 49 | 추가: if (pointers.isEmpty()) touchDownTime = now | |
| 50 | 추가: pointers.add(Pointer(t.pointerId, x, y, pressure)) | |
| 51 | 추가: val index = pointers.size - 1 | |
| 52 | 추가: action = if (index == 0) MotionEvent.ACTION_DOWN | |
| 53 | 추가: else MotionEvent.ACTION_POINTER_DOWN or (index shl MotionEvent.ACTION_POINTER_INDEX_SHIFT) | |
| 54 | 추가: } | |
| 55 | 추가: TouchAction.MOVE -> { | |
| 56 | 추가: val p = pointers.firstOrNull { it.id == t.pointerId } ?: return | |
| 57 | 추가: p.x = x; p.y = y; p.pressure = pressure | |
| 58 | 추가: action = MotionEvent.ACTION_MOVE | |
| 59 | 추가: } | |
| 60 | 추가: TouchAction.UP -> { | |
| 61 | 추가: val index = pointers.indexOfFirst { it.id == t.pointerId } | |
| 62 | 추가: if (index < 0) return | |
| 63 | 추가: pointers[index].x = x | |
| 64 | 추가: pointers[index].y = y | |
| 65 | 추가: action = if (pointers.size == 1) MotionEvent.ACTION_UP | |
| 66 | 추가: else MotionEvent.ACTION_POINTER_UP or (index shl MotionEvent.ACTION_POINTER_INDEX_SHIFT) | |
| 67 | 추가: } | |
| 68 | 추가: TouchAction.CANCEL -> { | |
| 69 | 추가: if (pointers.isEmpty()) return | |
| 70 | 추가: action = MotionEvent.ACTION_CANCEL | |
| 71 | 추가: } | |
| 72 | 추가: else -> return | |
| 73 | 추가: } | |
| 74 | 추가: injectMotion(action, now) | |
| 75 | 추가: when (t.action) { | |
| 76 | 추가: TouchAction.UP -> pointers.removeAll { it.id == t.pointerId } | |
| 77 | 추가: TouchAction.CANCEL -> pointers.clear() | |
| 78 | 추가: } | |
| 79 | 추가: } | |
| 80 | 추가: | |
| 81 | 추가: /** 연결이 끊길 때 눌린 채로 남은 터치를 정리한다. */ | |
| 82 | 추가: fun releaseAll() { | |
| 83 | 추가: if (pointers.isNotEmpty()) { | |
| 84 | 추가: injectMotion(MotionEvent.ACTION_CANCEL, SystemClock.uptimeMillis()) | |
| 85 | 추가: pointers.clear() | |
| 86 | 추가: } | |
| 87 | 추가: for (keycode in keyDownTimes.keys.toList()) key(KeyAction.UP, keycode, 0, 0, 0) | |
| 88 | 추가: } | |
| 89 | 추가: | |
| 90 | 추가: private fun injectMotion(action: Int, now: Long) { | |
| 91 | 추가: val props = Array(pointers.size) { i -> | |
| 92 | 추가: MotionEvent.PointerProperties().apply { | |
| 93 | 추가: id = pointers[i].id | |
| 94 | 추가: toolType = MotionEvent.TOOL_TYPE_FINGER | |
| 95 | 추가: } | |
| 96 | 추가: } | |
| 97 | 추가: val coords = Array(pointers.size) { i -> | |
| 98 | 추가: MotionEvent.PointerCoords().apply { | |
| 99 | 추가: x = pointers[i].x | |
| 100 | 추가: y = pointers[i].y | |
| 101 | 추가: pressure = if (action == MotionEvent.ACTION_UP) 0f else pointers[i].pressure | |
| 102 | 추가: size = 1f | |
| 103 | 추가: } | |
| 104 | 추가: } | |
| 105 | 추가: val e = MotionEvent.obtain( | |
| 106 | 추가: touchDownTime, now, action, pointers.size, props, coords, | |
| 107 | 추가: 0, 0, 1f, 1f, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0, | |
| 108 | 추가: ) | |
| 109 | 추가: SystemAccess.injectEvent(e) | |
| 110 | 추가: e.recycle() | |
| 111 | 추가: } | |
| 112 | 추가: | |
| 113 | 추가: fun scroll(s: Messages.ScrollEvent) { | |
| 114 | 추가: val (x, y) = toDisplay(s.x, s.y, s.frameWidth, s.frameHeight) | |
| 115 | 추가: val now = SystemClock.uptimeMillis() | |
| 116 | 추가: val props = arrayOf(MotionEvent.PointerProperties().apply { | |
| 117 | 추가: id = 0 | |
| 118 | 추가: toolType = MotionEvent.TOOL_TYPE_MOUSE | |
| 119 | 추가: }) | |
| 120 | 추가: val coords = arrayOf(MotionEvent.PointerCoords().apply { | |
| 121 | 추가: this.x = x | |
| 122 | 추가: this.y = y | |
| 123 | 추가: setAxisValue(MotionEvent.AXIS_VSCROLL, s.vScroll / 120f) | |
| 124 | 추가: setAxisValue(MotionEvent.AXIS_HSCROLL, s.hScroll / 120f) | |
| 125 | 추가: }) | |
| 126 | 추가: val e = MotionEvent.obtain( | |
| 127 | 추가: now, now, MotionEvent.ACTION_SCROLL, 1, props, coords, | |
| 128 | 추가: 0, 0, 1f, 1f, 0, 0, InputDevice.SOURCE_MOUSE, 0, | |
| 129 | 추가: ) | |
| 130 | 추가: SystemAccess.injectEvent(e) | |
| 131 | 추가: e.recycle() | |
| 132 | 추가: } | |
| 133 | 추가: | |
| 134 | 추가: fun key(action: Int, keycode: Int, meta: Int, repeat: Int, scancode: Int) { | |
| 135 | 추가: val now = SystemClock.uptimeMillis() | |
| 136 | 추가: fun inject(a: Int, down: Long) = SystemAccess.injectEvent( | |
| 137 | 추가: KeyEvent(down, now, a, keycode, repeat, meta, KeyCharacterMap.VIRTUAL_KEYBOARD, | |
| 138 | 추가: scancode, 0, InputDevice.SOURCE_KEYBOARD), | |
| 139 | 추가: ) | |
| 140 | 추가: when (action) { | |
| 141 | 추가: KeyAction.DOWN -> { | |
| 142 | 추가: val down = if (repeat > 0) keyDownTimes[keycode] ?: now else now | |
| 143 | 추가: keyDownTimes[keycode] = down | |
| 144 | 추가: inject(KeyEvent.ACTION_DOWN, down) | |
| 145 | 추가: } | |
| 146 | 추가: KeyAction.UP -> inject(KeyEvent.ACTION_UP, keyDownTimes.remove(keycode) ?: now) | |
| 147 | 추가: KeyAction.CLICK -> { | |
| 148 | 추가: inject(KeyEvent.ACTION_DOWN, now) | |
| 149 | 추가: inject(KeyEvent.ACTION_UP, now) | |
| 150 | 추가: } | |
| 151 | 추가: } | |
| 152 | 추가: } | |
| 153 | 추가: | |
| 154 | 추가: /** | |
| 155 | 추가: * 문자열을 입력한다. 가상 키보드 키맵으로 만들 수 있는 문자(ASCII 등)는 키 이벤트로 넣고, | |
| 156 | 추가: * 한글처럼 키 이벤트로 만들 수 없는 문자는 클립보드에 넣고 붙여넣는다. | |
| 157 | 추가: */ | |
| 158 | 추가: fun text(text: String) { | |
| 159 | 추가: val events = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD).getEvents(text.toCharArray()) | |
| 160 | 추가: if (events != null) { | |
| 161 | 추가: for (e in events) SystemAccess.injectEvent(e) | |
| 162 | 추가: return | |
| 163 | 추가: } | |
| 164 | 추가: if (ClipboardAccess.setText(text)) { | |
| 165 | 추가: SystemAccess.pressKey(KeyEvent.KEYCODE_PASTE) | |
| 166 | 추가: } else { | |
| 167 | 추가: Log.w("텍스트 입력 실패: 클립보드 설정 불가") | |
| 168 | 추가: } | |
| 169 | 추가: } | |
| 170 | 추가: | |
| 171 | 추가: object TouchAction { | |
| 172 | 추가: const val DOWN = 0 | |
| 173 | 추가: const val UP = 1 | |
| 174 | 추가: const val MOVE = 2 | |
| 175 | 추가: const val CANCEL = 3 | |
| 176 | 추가: } | |
| 177 | 추가: | |
| 178 | 추가: object KeyAction { | |
| 179 | 추가: const val DOWN = 0 | |
| 180 | 추가: const val UP = 1 | |
| 181 | 추가: const val CLICK = 2 | |
| 182 | 추가: } | |
| 183 | 추가: } | |
| 184 | 추가: | |
| 185 | 추가: /** | |
| 186 | 추가: * 시스템 클립보드 접근 (shell 권한). IClipboard 메서드 시그니처가 Android 버전마다 달라 | |
| 187 | 추가: * 매개변수 개수와 형식을 보고 맞는 것을 고른다. | |
| 188 | 추가: */ | |
| 189 | 추가: object ClipboardAccess { | |
| 190 | 추가: private const val SHELL_PACKAGE = "com.android.shell" | |
| 191 | 추가: | |
| 192 | 추가: private val service: Any? by lazy { | |
| 193 | 추가: try { | |
| 194 | 추가: val sm = Class.forName("android.os.ServiceManager") | |
| 195 | 추가: val binder = sm.getMethod("getService", String::class.java).invoke(null, "clipboard") as IBinder | |
| 196 | 추가: val stub = Class.forName("android.content.IClipboard\$Stub") | |
| 197 | 추가: stub.getMethod("asInterface", IBinder::class.java).invoke(null, binder) | |
| 198 | 추가: } catch (e: Exception) { | |
| 199 | 추가: Log.w("클립보드 서비스 연결 실패: $e") | |
| 200 | 추가: null | |
| 201 | 추가: } | |
| 202 | 추가: } | |
| 203 | 추가: | |
| 204 | 추가: private fun findMethod(name: String): Method? = | |
| 205 | 추가: service?.javaClass?.methods?.filter { it.name == name }?.maxByOrNull { it.parameterTypes.size } | |
| 206 | 추가: | |
| 207 | 추가: /** (callingPackage, attributionTag?, userId, deviceId?) 순서의 나머지 인자를 형식에 맞춰 채운다. */ | |
| 208 | 추가: private fun args(method: Method, first: Any?): Array<Any?> { | |
| 209 | 추가: val types = method.parameterTypes | |
| 210 | 추가: val out = arrayOfNulls<Any>(types.size) | |
| 211 | 추가: var stringCount = 0 | |
| 212 | 추가: for (i in types.indices) { | |
| 213 | 추가: out[i] = when { | |
| 214 | 추가: i == 0 && first != null -> first | |
| 215 | 추가: types[i] == String::class.java -> if (stringCount++ == 0) SHELL_PACKAGE else null | |
| 216 | 추가: types[i] == Int::class.javaPrimitiveType -> 0 | |
| 217 | 추가: else -> null | |
| 218 | 추가: } | |
| 219 | 추가: } | |
| 220 | 추가: return out | |
| 221 | 추가: } | |
| 222 | 추가: | |
| 223 | 추가: fun setText(text: String): Boolean = try { | |
| 224 | 추가: val m = findMethod("setPrimaryClip") ?: throw NoSuchMethodException("setPrimaryClip") | |
| 225 | 추가: m.invoke(service, *args(m, ClipData.newPlainText("android-remote", text))) | |
| 226 | 추가: true | |
| 227 | 추가: } catch (e: Exception) { | |
| 228 | 추가: Log.w("클립보드 설정 실패: ${e.cause ?: e}") | |
| 229 | 추가: false | |
| 230 | 추가: } | |
| 231 | 추가: | |
| 232 | 추가: fun getText(): String? = try { | |
| 233 | 추가: val m = findMethod("getPrimaryClip") ?: throw NoSuchMethodException("getPrimaryClip") | |
| 234 | 추가: val clip = m.invoke(service, *args(m, null)) as ClipData? | |
| 235 | 추가: clip?.takeIf { it.itemCount > 0 }?.getItemAt(0)?.text?.toString() | |
| 236 | 추가: } catch (e: Exception) { | |
| 237 | 추가: Log.w("클립보드 읽기 실패: ${e.cause ?: e}") | |
| 238 | 추가: null | |
| 239 | 추가: } | |
| 240 | 추가: } |
| @@ -186,6 +186,60 @@ object Messages { | ||
| 186 | 186 | return p[0].toInt() and 0xFF |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 추가: class TouchEvent( | |
| 190 | 추가: val action: Int, val pointerId: Int, val x: Int, val y: Int, | |
| 191 | 추가: val frameWidth: Int, val frameHeight: Int, val pressure: Int, | |
| 192 | 추가: ) | |
| 193 | 추가: | |
| 194 | 추가: fun decodeTouchEvent(p: ByteArray): TouchEvent { | |
| 195 | 추가: if (p.size != 20) throw ProtocolException("TOUCH_EVENT 길이 오류: ${p.size}") | |
| 196 | 추가: val b = ByteBuffer.wrap(p) | |
| 197 | 추가: val action = b.get().toInt() and 0xFF | |
| 198 | 추가: val pointerId = b.get().toInt() and 0xFF | |
| 199 | 추가: b.short | |
| 200 | 추가: val x = b.int | |
| 201 | 추가: val y = b.int | |
| 202 | 추가: val fw = b.short.toInt() and 0xFFFF | |
| 203 | 추가: val fh = b.short.toInt() and 0xFFFF | |
| 204 | 추가: val pressure = b.short.toInt() and 0xFFFF | |
| 205 | 추가: if (action > 3) throw ProtocolException("TOUCH_EVENT action 오류: $action") | |
| 206 | 추가: if (fw == 0 || fh == 0) throw ProtocolException("TOUCH_EVENT 프레임 크기 0") | |
| 207 | 추가: return TouchEvent(action, pointerId, x, y, fw, fh, pressure) | |
| 208 | 추가: } | |
| 209 | 추가: | |
| 210 | 추가: class ScrollEvent(val x: Int, val y: Int, val frameWidth: Int, val frameHeight: Int, val hScroll: Int, val vScroll: Int) | |
| 211 | 추가: | |
| 212 | 추가: fun decodeScrollEvent(p: ByteArray): ScrollEvent { | |
| 213 | 추가: if (p.size != 16) throw ProtocolException("SCROLL_EVENT 길이 오류: ${p.size}") | |
| 214 | 추가: val b = ByteBuffer.wrap(p) | |
| 215 | 추가: val x = b.int | |
| 216 | 추가: val y = b.int | |
| 217 | 추가: val fw = b.short.toInt() and 0xFFFF | |
| 218 | 추가: val fh = b.short.toInt() and 0xFFFF | |
| 219 | 추가: if (fw == 0 || fh == 0) throw ProtocolException("SCROLL_EVENT 프레임 크기 0") | |
| 220 | 추가: return ScrollEvent(x, y, fw, fh, b.short.toInt(), b.short.toInt()) | |
| 221 | 추가: } | |
| 222 | 추가: | |
| 223 | 추가: class KeyEvent(val action: Int, val keycode: Int, val metaState: Int, val repeat: Int, val scancode: Int) | |
| 224 | 추가: | |
| 225 | 추가: fun decodeKeyEvent(p: ByteArray): KeyEvent { | |
| 226 | 추가: if (p.size != 12) throw ProtocolException("KEY_EVENT 길이 오류: ${p.size}") | |
| 227 | 추가: val b = ByteBuffer.wrap(p) | |
| 228 | 추가: val action = b.get().toInt() and 0xFF | |
| 229 | 추가: b.get() | |
| 230 | 추가: val keycode = b.short.toInt() and 0xFFFF | |
| 231 | 추가: val meta = b.int | |
| 232 | 추가: val repeat = b.short.toInt() and 0xFFFF | |
| 233 | 추가: val scancode = b.short.toInt() and 0xFFFF | |
| 234 | 추가: if (action > 2) throw ProtocolException("KEY_EVENT action 오류: $action") | |
| 235 | 추가: return KeyEvent(action, keycode, meta, repeat, scancode) | |
| 236 | 추가: } | |
| 237 | 추가: | |
| 238 | 추가: fun decodeTextInput(p: ByteArray): String { | |
| 239 | 추가: if (p.isEmpty()) throw ProtocolException("TEXT_INPUT 빈 문자열") | |
| 240 | 추가: return Utf8.decodeStrict(p) ?: throw ProtocolException("TEXT_INPUT UTF-8 오류") | |
| 241 | 추가: } | |
| 242 | 추가: | |
| 189 | 243 | class VideoConfigRequest(val maxSize: Int, val maxFps: Int, val codecMask: Int, val bitrate: Long) |
| 190 | 244 | |
| 191 | 245 | fun decodeVideoConfigRequest(p: ByteArray): VideoConfigRequest { |
| @@ -37,6 +37,7 @@ class Server( | ||
| 37 | 37 | @Volatile var videoConfig: Messages.VideoConfig? = null |
| 38 | 38 | @Volatile var streamer: VideoStreamer? = null |
| 39 | 39 | var configGen = 0 |
| 40 | 추가: val input = InputInjector() | |
| 40 | 41 | |
| 41 | 42 | @Synchronized |
| 42 | 43 | fun track(c: Closeable) = closeables.add(c) |
| @@ -138,6 +139,7 @@ class Server( | ||
| 138 | 139 | serveControl(io, peer, session) |
| 139 | 140 | } finally { |
| 140 | 141 | sessions.remove(Hex.encode(token)) |
| 142 | 추가: runCatching { session.input.releaseAll() } | |
| 141 | 143 | session.closeAll() |
| 142 | 144 | Log.i("$peer: 세션 종료") |
| 143 | 145 | } |
| @@ -167,6 +169,20 @@ class Server( | ||
| 167 | 169 | Protocol.MsgType.PONG -> Unit |
| 168 | 170 | Protocol.MsgType.VIDEO_REQUEST_KEYFRAME -> session.streamer?.requestKeyframe() |
| 169 | 171 | Protocol.MsgType.SYSTEM_ACTION -> handleSystemAction(Messages.decodeSystemAction(f.payload), peer) |
| 172 | 추가: Protocol.MsgType.TOUCH_EVENT -> { | |
| 173 | 추가: val t = Messages.decodeTouchEvent(f.payload) | |
| 174 | 추가: // 회전 직후처럼 프레임 크기가 현재 설정과 다르면 엉뚱한 곳을 누르지 않도록 버린다 (기획서 7.3 4항). | |
| 175 | 추가: if (frameMatches(session, t.frameWidth, t.frameHeight)) session.input.touch(t) | |
| 176 | 추가: } | |
| 177 | 추가: Protocol.MsgType.SCROLL_EVENT -> { | |
| 178 | 추가: val s = Messages.decodeScrollEvent(f.payload) | |
| 179 | 추가: if (frameMatches(session, s.frameWidth, s.frameHeight)) session.input.scroll(s) | |
| 180 | 추가: } | |
| 181 | 추가: Protocol.MsgType.KEY_EVENT -> { | |
| 182 | 추가: val k = Messages.decodeKeyEvent(f.payload) | |
| 183 | 추가: session.input.key(k.action, k.keycode, k.metaState, k.repeat, k.scancode) | |
| 184 | 추가: } | |
| 185 | 추가: Protocol.MsgType.TEXT_INPUT -> session.input.text(Messages.decodeTextInput(f.payload)) | |
| 170 | 186 | Protocol.MsgType.VIDEO_CONFIG_REQUEST -> { |
| 171 | 187 | val req = Messages.decodeVideoConfigRequest(f.payload) |
| 172 | 188 | val cfg = videoConfigFor(req, session) |
| @@ -189,6 +205,11 @@ class Server( | ||
| 189 | 205 | } |
| 190 | 206 | } |
| 191 | 207 | |
| 208 | 추가: private fun frameMatches(session: Session, w: Int, h: Int): Boolean { | |
| 209 | 추가: val cfg = session.videoConfig ?: return false | |
| 210 | 추가: return cfg.width == w && cfg.height == h | |
| 211 | 추가: } | |
| 212 | 추가: | |
| 192 | 213 | private fun handleSystemAction(code: Int, peer: String) { |
| 193 | 214 | val keycode = when (code) { |
| 194 | 215 | Protocol.SystemAction.BACK -> KeyEvent.KEYCODE_BACK |
| @@ -197,6 +218,11 @@ class Server( | ||
| 197 | 218 | Protocol.SystemAction.COPY -> KeyEvent.KEYCODE_COPY |
| 198 | 219 | Protocol.SystemAction.PASTE -> KeyEvent.KEYCODE_PASTE |
| 199 | 220 | Protocol.SystemAction.CUT -> KeyEvent.KEYCODE_CUT |
| 221 | 추가: Protocol.SystemAction.SELECT_ALL -> { | |
| 222 | 추가: SystemAccess.pressKey(KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON or KeyEvent.META_CTRL_LEFT_ON) | |
| 223 | 추가: Log.i("$peer: SYSTEM_ACTION SELECT_ALL -> Ctrl+A") | |
| 224 | 추가: return | |
| 225 | 추가: } | |
| 200 | 226 | else -> { |
| 201 | 227 | Log.w("$peer: 처리하지 않는 SYSTEM_ACTION 0x%02x".format(code)) |
| 202 | 228 | return |
| @@ -30,11 +30,11 @@ object SystemAccess { | ||
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | /** 키를 눌렀다 뗀다. 뒤로·홈·최근 앱 버튼에 쓴다. */ |
| 33 | 삭제: fun pressKey(keycode: Int) { | |
| 33 | 추가: fun pressKey(keycode: Int, meta: Int = 0) { | |
| 34 | 34 | val now = SystemClock.uptimeMillis() |
| 35 | 35 | for (action in intArrayOf(KeyEvent.ACTION_DOWN, KeyEvent.ACTION_UP)) { |
| 36 | 36 | val e = KeyEvent( |
| 37 | 삭제: now, now, action, keycode, 0, 0, | |
| 37 | 추가: now, now, action, keycode, 0, meta, | |
| 38 | 38 | KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD, |
| 39 | 39 | ) |
| 40 | 40 | injectEvent(e) |
| @@ -4,6 +4,9 @@ | ||
| 4 | 4 | //! |
| 5 | 5 | //! `--record`를 주면 영상 채널로 받은 Annex-B 스트림을 파일로 저장한다(ffprobe/ffplay로 확인). |
| 6 | 6 | //! |
| 7 | 추가: //! 입력 주입 점검(좌표는 영상 프레임 기준, 주어진 순서대로 실행): | |
| 8 | 추가: //! `--tap x,y`, `--swipe x1,y1,x2,y2`, `--scroll x,y,v120`, `--text 문자열`, `--keycode N`, `--wait ms` | |
| 9 | 추가: //! | |
| 7 | 10 | //! GUI와 같은 PC 키(`~/.config/android-remote/device_key`)와 기기 지문 목록을 쓰므로, |
| 8 | 11 | //! 여기서 허용된 PC는 GUI에서도 승인 없이 연결된다. |
| 9 | 12 | |
| @@ -80,6 +83,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| 80 | 83 | .await?; |
| 81 | 84 | let reply = tokio::time::timeout(Duration::from_secs(3), control.read_message()).await??; |
| 82 | 85 | println!("영상 설정 응답: {:?}", reply.1); |
| 86 | 추가: let (fw, fh) = match &reply.1 { | |
| 87 | 추가: Message::VideoConfig(c) => (c.width, c.height), | |
| 88 | 추가: _ => (864, 1920), | |
| 89 | 추가: }; | |
| 90 | 추가: run_input_ops(control, &args, fw, fh).await?; | |
| 83 | 91 | |
| 84 | 92 | if let Some(action) = key { |
| 85 | 93 | control.send(&Message::SystemAction(action)).await?; |
| @@ -90,6 +98,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| 90 | 98 | let seconds: u64 = option(&args, "--seconds") |
| 91 | 99 | .and_then(|s| s.parse().ok()) |
| 92 | 100 | .unwrap_or(3); |
| 101 | 추가: if seconds == 0 { | |
| 102 | 추가: return Ok(()); | |
| 103 | 추가: } | |
| 93 | 104 | let mut video = session.open_video(Duration::from_secs(5)).await?; |
| 94 | 105 | let opened = Instant::now(); |
| 95 | 106 | let mut file = record.map(std::fs::File::create).transpose()?; |
| @@ -136,6 +147,107 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| 136 | 147 | Ok(()) |
| 137 | 148 | } |
| 138 | 149 | |
| 150 | 추가: async fn run_input_ops( | |
| 151 | 추가: control: &mut wayland_android_remote::net::FramedStream< | |
| 152 | 추가: wayland_android_remote::net::DeviceStream, | |
| 153 | 추가: >, | |
| 154 | 추가: args: &[String], | |
| 155 | 추가: fw: u16, | |
| 156 | 추가: fh: u16, | |
| 157 | 추가: ) -> Result<(), Box<dyn std::error::Error>> { | |
| 158 | 추가: use wayland_android_remote::protocol::{ | |
| 159 | 추가: KeyAction, KeyEvent, TextInput, TouchAction, TouchEvent, | |
| 160 | 추가: }; | |
| 161 | 추가: let touch = |action, x: u32, y: u32| { | |
| 162 | 추가: Message::TouchEvent(TouchEvent { | |
| 163 | 추가: action, | |
| 164 | 추가: pointer_id: 0, | |
| 165 | 추가: x, | |
| 166 | 추가: y, | |
| 167 | 추가: frame_width: fw, | |
| 168 | 추가: frame_height: fh, | |
| 169 | 추가: pressure: if action == TouchAction::Up { 0 } else { 0xFFFF }, | |
| 170 | 추가: }) | |
| 171 | 추가: }; | |
| 172 | 추가: let nums = | |
| 173 | 추가: |s: &str| -> Vec<u32> { s.split(',').filter_map(|v| v.trim().parse().ok()).collect() }; | |
| 174 | 추가: let mut i = 0; | |
| 175 | 추가: while i < args.len() { | |
| 176 | 추가: let value = args.get(i + 1).map(String::as_str).unwrap_or(""); | |
| 177 | 추가: match args[i].as_str() { | |
| 178 | 추가: "--tap" => { | |
| 179 | 추가: let v = nums(value); | |
| 180 | 추가: control.send(&touch(TouchAction::Down, v[0], v[1])).await?; | |
| 181 | 추가: tokio::time::sleep(Duration::from_millis(60)).await; | |
| 182 | 추가: control.send(&touch(TouchAction::Up, v[0], v[1])).await?; | |
| 183 | 추가: println!("탭 {},{}", v[0], v[1]); | |
| 184 | 추가: } | |
| 185 | 추가: "--swipe" => { | |
| 186 | 추가: let v = nums(value); | |
| 187 | 추가: control.send(&touch(TouchAction::Down, v[0], v[1])).await?; | |
| 188 | 추가: for step in 1..=15u32 { | |
| 189 | 추가: tokio::time::sleep(Duration::from_millis(16)).await; | |
| 190 | 추가: let x = v[0] as i64 + (v[2] as i64 - v[0] as i64) * step as i64 / 15; | |
| 191 | 추가: let y = v[1] as i64 + (v[3] as i64 - v[1] as i64) * step as i64 / 15; | |
| 192 | 추가: control | |
| 193 | 추가: .send(&touch(TouchAction::Move, x as u32, y as u32)) | |
| 194 | 추가: .await?; | |
| 195 | 추가: } | |
| 196 | 추가: control.send(&touch(TouchAction::Up, v[2], v[3])).await?; | |
| 197 | 추가: println!("스와이프 {value}"); | |
| 198 | 추가: } | |
| 199 | 추가: "--scroll" => { | |
| 200 | 추가: // x,y,v120 (양수 = 위로) | |
| 201 | 추가: let v: Vec<i64> = value | |
| 202 | 추가: .split(',') | |
| 203 | 추가: .filter_map(|n| n.trim().parse().ok()) | |
| 204 | 추가: .collect(); | |
| 205 | 추가: control | |
| 206 | 추가: .send(&Message::ScrollEvent( | |
| 207 | 추가: wayland_android_remote::protocol::ScrollEvent { | |
| 208 | 추가: x: v[0] as u32, | |
| 209 | 추가: y: v[1] as u32, | |
| 210 | 추가: frame_width: fw, | |
| 211 | 추가: frame_height: fh, | |
| 212 | 추가: h_scroll: 0, | |
| 213 | 추가: v_scroll: v[2] as i16, | |
| 214 | 추가: }, | |
| 215 | 추가: )) | |
| 216 | 추가: .await?; | |
| 217 | 추가: println!("스크롤 {value}"); | |
| 218 | 추가: } | |
| 219 | 추가: "--text" => { | |
| 220 | 추가: control | |
| 221 | 추가: .send(&Message::TextInput(TextInput { | |
| 222 | 추가: text: value.to_owned(), | |
| 223 | 추가: })) | |
| 224 | 추가: .await?; | |
| 225 | 추가: println!("텍스트 {value:?}"); | |
| 226 | 추가: } | |
| 227 | 추가: "--keycode" => { | |
| 228 | 추가: let keycode: u16 = value.parse()?; | |
| 229 | 추가: control | |
| 230 | 추가: .send(&Message::KeyEvent(KeyEvent { | |
| 231 | 추가: action: KeyAction::Click, | |
| 232 | 추가: keycode, | |
| 233 | 추가: meta_state: 0, | |
| 234 | 추가: repeat: 0, | |
| 235 | 추가: scancode: 0, | |
| 236 | 추가: })) | |
| 237 | 추가: .await?; | |
| 238 | 추가: println!("키 {keycode}"); | |
| 239 | 추가: } | |
| 240 | 추가: "--wait" => tokio::time::sleep(Duration::from_millis(value.parse()?)).await, | |
| 241 | 추가: _ => { | |
| 242 | 추가: i += 1; | |
| 243 | 추가: continue; | |
| 244 | 추가: } | |
| 245 | 추가: } | |
| 246 | 추가: i += 2; | |
| 247 | 추가: } | |
| 248 | 추가: Ok(()) | |
| 249 | 추가: } | |
| 250 | 추가: | |
| 139 | 251 | fn option<'a>(args: &'a [String], name: &str) -> Option<&'a str> { |
| 140 | 252 | args.iter() |
| 141 | 253 | .position(|a| a == name) |
| @@ -57,6 +57,8 @@ pub enum ConnEvent { | ||
| 57 | 57 | pub enum ConnCommand { |
| 58 | 58 | System(SystemAction), |
| 59 | 59 | RequestVideo(VideoConfigRequest), |
| 60 | 추가: /// 터치·휠·키·텍스트 입력 메시지. 받은 순서대로 바로 보낸다. | |
| 61 | 추가: Input(Message), | |
| 60 | 62 | } |
| 61 | 63 | |
| 62 | 64 | pub type Waker = Arc<dyn Fn() + Send + Sync>; |
| @@ -423,6 +425,7 @@ async fn control_loop( | ||
| 423 | 425 | let msg = match cmd { |
| 424 | 426 | Some(ConnCommand::System(a)) => Message::SystemAction(a), |
| 425 | 427 | Some(ConnCommand::RequestVideo(r)) => Message::VideoConfigRequest(r), |
| 428 | 추가: Some(ConnCommand::Input(m)) => m, | |
| 426 | 429 | None => return "연결을 끊었습니다".to_owned(), |
| 427 | 430 | }; |
| 428 | 431 | if let Err(e) = control.send(&msg).await { |
| @@ -0,0 +1,615 @@ | ||
| 1 | 추가: //! 메인 창의 마우스·휠·키보드·IME 입력을 제어 메시지로 바꾼다. | |
| 2 | 추가: //! | |
| 3 | 추가: //! egui가 프레임마다 모은 원시 이벤트를 순서대로 읽어 메시지를 만든다. 위젯 응답 대신 원시 | |
| 4 | 추가: //! 이벤트를 쓰는 이유는 드래그 판정 지연 없이 누르는 즉시 DOWN을 보내고, 한 프레임 안의 | |
| 5 | 추가: //! 누름·이동·뗌 순서를 그대로 지키기 위해서다. | |
| 6 | 추가: | |
| 7 | 추가: use egui::{Event, ImeEvent, Key, Modifiers, MouseWheelUnit, PointerButton, Pos2, Rect, Vec2}; | |
| 8 | 추가: use wayland_android_remote::protocol::{ | |
| 9 | 추가: KeyAction, KeyEvent, Message, ScrollEvent, SystemAction, TextInput, TouchAction, TouchEvent, | |
| 10 | 추가: }; | |
| 11 | 추가: | |
| 12 | 추가: use super::keymap; | |
| 13 | 추가: | |
| 14 | 추가: /// 터치 압력 최댓값 (누른 상태). | |
| 15 | 추가: const FULL_PRESSURE: u16 = 0xFFFF; | |
| 16 | 추가: /// 휠 한 노치 = 120 단위 (Android AXIS_VSCROLL 1.0). | |
| 17 | 추가: const NOTCH: f32 = 120.0; | |
| 18 | 추가: /// 픽셀 단위 휠(터치패드)에서 한 노치로 치는 논리 픽셀 수. egui의 줄 단위 스크롤 | |
| 19 | 추가: /// 속도(40pt)보다 조금 크게 잡아 손가락 이동량과 휴대폰 스크롤 양이 비슷하게 느껴지도록 했다. | |
| 20 | 추가: pub const POINTS_PER_NOTCH: f32 = 50.0; | |
| 21 | 추가: /// 페이지 단위 휠은 3줄(3노치)로 친다. | |
| 22 | 추가: const LINES_PER_PAGE: f32 = 3.0; | |
| 23 | 추가: | |
| 24 | 추가: /// 입력을 받을 휴대폰 화면 영역. | |
| 25 | 추가: #[derive(Debug, Clone, Copy, PartialEq)] | |
| 26 | 추가: pub struct VideoTarget { | |
| 27 | 추가: /// 창 안에서 영상이 그려진 사각형 (논리 픽셀). | |
| 28 | 추가: pub rect: Rect, | |
| 29 | 추가: /// 휴대폰이 기대하는 좌표계 크기 (`VideoConfig.width/height`). | |
| 30 | 추가: pub width: u16, | |
| 31 | 추가: pub height: u16, | |
| 32 | 추가: } | |
| 33 | 추가: | |
| 34 | 추가: impl VideoTarget { | |
| 35 | 추가: /// 창 좌표를 영상 프레임 픽셀 좌표로 바꾼다. 영역 밖은 가장자리로 붙인다. | |
| 36 | 추가: pub fn to_frame(self, p: Pos2) -> (u32, u32) { | |
| 37 | 추가: let axis = |v: f32, min: f32, len: f32, size: u16| { | |
| 38 | 추가: let max = u32::from(size.max(1) - 1); | |
| 39 | 추가: let n = if len > 0.0 { (v - min) / len } else { 0.0 }; | |
| 40 | 추가: ((n * f32::from(size)).floor().max(0.0) as u32).min(max) | |
| 41 | 추가: }; | |
| 42 | 추가: ( | |
| 43 | 추가: axis(p.x, self.rect.min.x, self.rect.width(), self.width), | |
| 44 | 추가: axis(p.y, self.rect.min.y, self.rect.height(), self.height), | |
| 45 | 추가: ) | |
| 46 | 추가: } | |
| 47 | 추가: } | |
| 48 | 추가: | |
| 49 | 추가: /// 누르고 있는 터치 하나 (pointer_id 0). | |
| 50 | 추가: #[derive(Debug, Clone, Copy)] | |
| 51 | 추가: struct Touch { | |
| 52 | 추가: /// 마지막으로 보낸 좌표. | |
| 53 | 추가: last: (u32, u32), | |
| 54 | 추가: /// 누를 때의 프레임 크기. 바뀌면 취소한다. | |
| 55 | 추가: frame: (u16, u16), | |
| 56 | 추가: /// 이번 프레임에서 아직 보내지 않은 최신 이동 위치. | |
| 57 | 추가: pending: Option<Pos2>, | |
| 58 | 추가: } | |
| 59 | 추가: | |
| 60 | 추가: /// 휴대폰에 DOWN을 보낸 키. UP도 같은 키코드로 보낸다. | |
| 61 | 추가: #[derive(Debug, Clone, Copy)] | |
| 62 | 추가: struct HeldKey { | |
| 63 | 추가: key: Key, | |
| 64 | 추가: keycode: u16, | |
| 65 | 추가: repeat: u16, | |
| 66 | 추가: } | |
| 67 | 추가: | |
| 68 | 추가: /// 프레임 사이에 유지하는 입력 상태. | |
| 69 | 추가: #[derive(Debug, Default)] | |
| 70 | 추가: pub struct InputState { | |
| 71 | 추가: touch: Option<Touch>, | |
| 72 | 추가: /// 마지막 포인터 위치. 휠 이벤트 위치로 쓴다. | |
| 73 | 추가: pointer: Option<Pos2>, | |
| 74 | 추가: /// 아직 보내지 않은 휠 양 (1/120 노치 단위, 위·오른쪽 양수). | |
| 75 | 추가: wheel_acc: Vec2, | |
| 76 | 추가: held_keys: Vec<HeldKey>, | |
| 77 | 추가: /// IME 조합 중인 글자. 휴대폰에는 보내지 않고 화면에만 표시한다. | |
| 78 | 추가: preedit: String, | |
| 79 | 추가: } | |
| 80 | 추가: | |
| 81 | 추가: impl InputState { | |
| 82 | 추가: /// 조합 중인 IME 글자. | |
| 83 | 추가: pub fn preedit(&self) -> &str { | |
| 84 | 추가: &self.preedit | |
| 85 | 추가: } | |
| 86 | 추가: | |
| 87 | 추가: /// 한 프레임의 이벤트를 처리해 보낼 메시지를 `out`에 순서대로 넣는다. | |
| 88 | 추가: /// | |
| 89 | 추가: /// `keyboard`는 연결되어 키보드 입력을 휴대폰으로 보낼 상태인지, `target`은 터치·휠을 | |
| 90 | 추가: /// 받을 영상 영역(연결되고 영상 설정과 프레임이 있을 때)이다. | |
| 91 | 추가: pub fn process( | |
| 92 | 추가: &mut self, | |
| 93 | 추가: events: &[Event], | |
| 94 | 추가: keyboard: bool, | |
| 95 | 추가: target: Option<VideoTarget>, | |
| 96 | 추가: out: &mut Vec<Message>, | |
| 97 | 추가: ) { | |
| 98 | 추가: // 연결이 끊기거나 영상 크기가 바뀌면 누르던 터치를 취소한다. | |
| 99 | 추가: if let Some(t) = self.touch | |
| 100 | 추가: && target.is_none_or(|v| (v.width, v.height) != t.frame) | |
| 101 | 추가: { | |
| 102 | 추가: self.cancel_touch(out); | |
| 103 | 추가: } | |
| 104 | 추가: if !keyboard { | |
| 105 | 추가: self.held_keys.clear(); | |
| 106 | 추가: self.preedit.clear(); | |
| 107 | 추가: } | |
| 108 | 추가: | |
| 109 | 추가: let mut skip_text = false; | |
| 110 | 추가: for (i, event) in events.iter().enumerate() { | |
| 111 | 추가: // 직전 키 이벤트로 이미 보낸 키가 만든 글자는 건너뛴다 (예: Alt+문자). | |
| 112 | 추가: let skip = std::mem::take(&mut skip_text); | |
| 113 | 추가: match event { | |
| 114 | 추가: Event::PointerMoved(p) => { | |
| 115 | 추가: self.pointer = Some(*p); | |
| 116 | 추가: if let Some(t) = &mut self.touch { | |
| 117 | 추가: t.pending = Some(*p); | |
| 118 | 추가: } | |
| 119 | 추가: } | |
| 120 | 추가: Event::PointerGone => self.pointer = None, | |
| 121 | 추가: Event::PointerButton { | |
| 122 | 추가: pos, | |
| 123 | 추가: button: PointerButton::Primary, | |
| 124 | 추가: pressed, | |
| 125 | 추가: .. | |
| 126 | 추가: } => { | |
| 127 | 추가: self.pointer = Some(*pos); | |
| 128 | 추가: match (*pressed, target) { | |
| 129 | 추가: (true, Some(v)) if self.touch.is_none() && v.rect.contains(*pos) => { | |
| 130 | 추가: self.touch_down(v, *pos, out); | |
| 131 | 추가: } | |
| 132 | 추가: (false, Some(v)) if self.touch.is_some() => self.touch_up(v, *pos, out), | |
| 133 | 추가: _ => {} | |
| 134 | 추가: } | |
| 135 | 추가: } | |
| 136 | 추가: Event::MouseWheel { unit, delta, .. } => { | |
| 137 | 추가: if let Some(v) = target { | |
| 138 | 추가: self.wheel(v, *unit, *delta, out); | |
| 139 | 추가: } | |
| 140 | 추가: } | |
| 141 | 추가: Event::WindowFocused(false) => { | |
| 142 | 추가: self.cancel_touch(out); | |
| 143 | 추가: self.release_keys(Modifiers::NONE, out); | |
| 144 | 추가: self.preedit.clear(); | |
| 145 | 추가: } | |
| 146 | 추가: Event::Key { | |
| 147 | 추가: key, | |
| 148 | 추가: pressed, | |
| 149 | 추가: repeat, | |
| 150 | 추가: modifiers, | |
| 151 | 추가: .. | |
| 152 | 추가: } if keyboard => { | |
| 153 | 추가: let next_is_text = matches!(events.get(i + 1), Some(Event::Text(_))); | |
| 154 | 추가: if *pressed { | |
| 155 | 추가: skip_text = self.key_down(*key, *modifiers, *repeat, next_is_text, out); | |
| 156 | 추가: } else { | |
| 157 | 추가: self.key_up(*key, *modifiers, out); | |
| 158 | 추가: } | |
| 159 | 추가: } | |
| 160 | 추가: Event::Text(text) if keyboard && !skip => send_text(text, out), | |
| 161 | 추가: Event::Ime(ime) if keyboard => match ime { | |
| 162 | 추가: ImeEvent::Preedit { text, .. } => self.preedit.clone_from(text), | |
| 163 | 추가: ImeEvent::Commit(text) => { | |
| 164 | 추가: self.preedit.clear(); | |
| 165 | 추가: send_text(text, out); | |
| 166 | 추가: } | |
| 167 | 추가: _ => {} | |
| 168 | 추가: }, | |
| 169 | 추가: // 클립보드 단축키를 egui-winit이 가로챈 경우 (메인 창은 연결 중 끄지만 대비해 둔다). | |
| 170 | 추가: Event::Copy if keyboard => out.push(Message::SystemAction(SystemAction::Copy)), | |
| 171 | 추가: Event::Cut if keyboard => out.push(Message::SystemAction(SystemAction::Cut)), | |
| 172 | 추가: Event::Paste(_) if keyboard => out.push(Message::SystemAction(SystemAction::Paste)), | |
| 173 | 추가: _ => {} | |
| 174 | 추가: } | |
| 175 | 추가: } | |
| 176 | 추가: | |
| 177 | 추가: // 이동은 프레임마다 최신 위치 한 번만 보낸다. | |
| 178 | 추가: if let (Some(v), Some(_)) = (target, self.touch) { | |
| 179 | 추가: self.flush_move(v, out); | |
| 180 | 추가: } | |
| 181 | 추가: } | |
| 182 | 추가: | |
| 183 | 추가: fn touch_down(&mut self, v: VideoTarget, pos: Pos2, out: &mut Vec<Message>) { | |
| 184 | 추가: let last = v.to_frame(pos); | |
| 185 | 추가: out.push(touch_message(TouchAction::Down, last, (v.width, v.height))); | |
| 186 | 추가: self.touch = Some(Touch { | |
| 187 | 추가: last, | |
| 188 | 추가: frame: (v.width, v.height), | |
| 189 | 추가: pending: None, | |
| 190 | 추가: }); | |
| 191 | 추가: } | |
| 192 | 추가: | |
| 193 | 추가: fn flush_move(&mut self, v: VideoTarget, out: &mut Vec<Message>) { | |
| 194 | 추가: let Some(t) = &mut self.touch else { return }; | |
| 195 | 추가: let Some(p) = t.pending.take() else { return }; | |
| 196 | 추가: let pos = v.to_frame(p); | |
| 197 | 추가: if pos != t.last { | |
| 198 | 추가: t.last = pos; | |
| 199 | 추가: out.push(touch_message(TouchAction::Move, pos, t.frame)); | |
| 200 | 추가: } | |
| 201 | 추가: } | |
| 202 | 추가: | |
| 203 | 추가: fn touch_up(&mut self, v: VideoTarget, pos: Pos2, out: &mut Vec<Message>) { | |
| 204 | 추가: self.flush_move(v, out); | |
| 205 | 추가: if let Some(t) = self.touch.take() { | |
| 206 | 추가: out.push(touch_message(TouchAction::Up, v.to_frame(pos), t.frame)); | |
| 207 | 추가: } | |
| 208 | 추가: } | |
| 209 | 추가: | |
| 210 | 추가: fn cancel_touch(&mut self, out: &mut Vec<Message>) { | |
| 211 | 추가: if let Some(t) = self.touch.take() { | |
| 212 | 추가: out.push(touch_message(TouchAction::Cancel, t.last, t.frame)); | |
| 213 | 추가: } | |
| 214 | 추가: } | |
| 215 | 추가: | |
| 216 | 추가: fn wheel(&mut self, v: VideoTarget, unit: MouseWheelUnit, delta: Vec2, out: &mut Vec<Message>) { | |
| 217 | 추가: let Some(p) = self.pointer.filter(|p| v.rect.contains(*p)) else { | |
| 218 | 추가: self.wheel_acc = Vec2::ZERO; | |
| 219 | 추가: return; | |
| 220 | 추가: }; | |
| 221 | 추가: // egui(=winit) 휠 delta는 내용이 움직일 방향이다: y>0이면 내용이 아래로 내려가 위쪽이 | |
| 222 | 추가: // 보인다(휠을 위로 굴림), x>0이면 내용이 오른쪽으로 가 왼쪽이 보인다(왼쪽 스크롤). | |
| 223 | 추가: // Android AXIS_VSCROLL은 위가 양수, AXIS_HSCROLL은 오른쪽이 양수라 x만 부호를 뒤집는다. | |
| 224 | 추가: let notches = match unit { | |
| 225 | 추가: MouseWheelUnit::Line => delta, | |
| 226 | 추가: MouseWheelUnit::Point => delta / POINTS_PER_NOTCH, | |
| 227 | 추가: MouseWheelUnit::Page => delta * LINES_PER_PAGE, | |
| 228 | 추가: }; | |
| 229 | 추가: self.wheel_acc += Vec2::new(-notches.x, notches.y) * NOTCH; | |
| 230 | 추가: // 정수 부분만 보내고 남은 소수는 다음 이벤트에 더한다 (터치패드의 작은 이동 보존). | |
| 231 | 추가: let send = Vec2::new( | |
| 232 | 추가: self.wheel_acc | |
| 233 | 추가: .x | |
| 234 | 추가: .trunc() | |
| 235 | 추가: .clamp(i16::MIN.into(), i16::MAX.into()), | |
| 236 | 추가: self.wheel_acc | |
| 237 | 추가: .y | |
| 238 | 추가: .trunc() | |
| 239 | 추가: .clamp(i16::MIN.into(), i16::MAX.into()), | |
| 240 | 추가: ); | |
| 241 | 추가: if send == Vec2::ZERO { | |
| 242 | 추가: return; | |
| 243 | 추가: } | |
| 244 | 추가: self.wheel_acc -= send; | |
| 245 | 추가: let (x, y) = v.to_frame(p); | |
| 246 | 추가: out.push(Message::ScrollEvent(ScrollEvent { | |
| 247 | 추가: x, | |
| 248 | 추가: y, | |
| 249 | 추가: frame_width: v.width, | |
| 250 | 추가: frame_height: v.height, | |
| 251 | 추가: h_scroll: send.x as i16, | |
| 252 | 추가: v_scroll: send.y as i16, | |
| 253 | 추가: })); | |
| 254 | 추가: } | |
| 255 | 추가: | |
| 256 | 추가: /// 키 누름을 처리한다. 키 이벤트(또는 시스템 동작)를 보냈으면 true. | |
| 257 | 추가: fn key_down( | |
| 258 | 추가: &mut self, | |
| 259 | 추가: key: Key, | |
| 260 | 추가: modifiers: Modifiers, | |
| 261 | 추가: repeat: bool, | |
| 262 | 추가: next_is_text: bool, | |
| 263 | 추가: out: &mut Vec<Message>, | |
| 264 | 추가: ) -> bool { | |
| 265 | 추가: if let Some(action) = keymap::clipboard_action(key, modifiers) { | |
| 266 | 추가: if !repeat { | |
| 267 | 추가: out.push(Message::SystemAction(action)); | |
| 268 | 추가: } | |
| 269 | 추가: return true; | |
| 270 | 추가: } | |
| 271 | 추가: let keycode = keymap::special_keycode(key) | |
| 272 | 추가: .or_else(|| keymap::combo_keycode(key, modifiers)) | |
| 273 | 추가: // 스페이스는 글자를 만들지 않았을 때(Ctrl+Space 등)만 키로 보낸다. | |
| 274 | 추가: .or_else(|| (key == Key::Space && !next_is_text).then_some(keymap::KEYCODE_SPACE)); | |
| 275 | 추가: let Some(keycode) = keycode else { | |
| 276 | 추가: return false; | |
| 277 | 추가: }; | |
| 278 | 추가: let held = match self.held_keys.iter_mut().find(|h| h.key == key) { | |
| 279 | 추가: Some(h) if repeat => { | |
| 280 | 추가: h.repeat = h.repeat.saturating_add(1); | |
| 281 | 추가: *h | |
| 282 | 추가: } | |
| 283 | 추가: Some(h) => { | |
| 284 | 추가: *h = HeldKey { | |
| 285 | 추가: key, | |
| 286 | 추가: keycode, | |
| 287 | 추가: repeat: 0, | |
| 288 | 추가: }; | |
| 289 | 추가: *h | |
| 290 | 추가: } | |
| 291 | 추가: None => { | |
| 292 | 추가: let h = HeldKey { | |
| 293 | 추가: key, | |
| 294 | 추가: keycode, | |
| 295 | 추가: repeat: 0, | |
| 296 | 추가: }; | |
| 297 | 추가: self.held_keys.push(h); | |
| 298 | 추가: h | |
| 299 | 추가: } | |
| 300 | 추가: }; | |
| 301 | 추가: out.push(key_message( | |
| 302 | 추가: KeyAction::Down, | |
| 303 | 추가: held.keycode, | |
| 304 | 추가: modifiers, | |
| 305 | 추가: held.repeat, | |
| 306 | 추가: )); | |
| 307 | 추가: true | |
| 308 | 추가: } | |
| 309 | 추가: | |
| 310 | 추가: fn key_up(&mut self, key: Key, modifiers: Modifiers, out: &mut Vec<Message>) { | |
| 311 | 추가: if let Some(i) = self.held_keys.iter().position(|h| h.key == key) { | |
| 312 | 추가: let h = self.held_keys.remove(i); | |
| 313 | 추가: out.push(key_message(KeyAction::Up, h.keycode, modifiers, 0)); | |
| 314 | 추가: } | |
| 315 | 추가: } | |
| 316 | 추가: | |
| 317 | 추가: /// 누르고 있던 키를 모두 뗀다 (창 포커스를 잃으면 뗌 이벤트가 오지 않을 수 있다). | |
| 318 | 추가: fn release_keys(&mut self, modifiers: Modifiers, out: &mut Vec<Message>) { | |
| 319 | 추가: for h in self.held_keys.drain(..) { | |
| 320 | 추가: out.push(key_message(KeyAction::Up, h.keycode, modifiers, 0)); | |
| 321 | 추가: } | |
| 322 | 추가: } | |
| 323 | 추가: } | |
| 324 | 추가: | |
| 325 | 추가: fn touch_message(action: TouchAction, (x, y): (u32, u32), frame: (u16, u16)) -> Message { | |
| 326 | 추가: Message::TouchEvent(TouchEvent { | |
| 327 | 추가: action, | |
| 328 | 추가: pointer_id: 0, | |
| 329 | 추가: x, | |
| 330 | 추가: y, | |
| 331 | 추가: frame_width: frame.0, | |
| 332 | 추가: frame_height: frame.1, | |
| 333 | 추가: pressure: match action { | |
| 334 | 추가: TouchAction::Down | TouchAction::Move => FULL_PRESSURE, | |
| 335 | 추가: TouchAction::Up | TouchAction::Cancel => 0, | |
| 336 | 추가: }, | |
| 337 | 추가: }) | |
| 338 | 추가: } | |
| 339 | 추가: | |
| 340 | 추가: fn key_message(action: KeyAction, keycode: u16, modifiers: Modifiers, repeat: u16) -> Message { | |
| 341 | 추가: Message::KeyEvent(KeyEvent { | |
| 342 | 추가: action, | |
| 343 | 추가: keycode, | |
| 344 | 추가: meta_state: keymap::meta_state(modifiers), | |
| 345 | 추가: repeat, | |
| 346 | 추가: scancode: 0, | |
| 347 | 추가: }) | |
| 348 | 추가: } | |
| 349 | 추가: | |
| 350 | 추가: fn send_text(text: &str, out: &mut Vec<Message>) { | |
| 351 | 추가: if !text.is_empty() { | |
| 352 | 추가: out.push(Message::TextInput(TextInput { | |
| 353 | 추가: text: text.to_owned(), | |
| 354 | 추가: })); | |
| 355 | 추가: } | |
| 356 | 추가: } | |
| 357 | 추가: | |
| 358 | 추가: #[cfg(test)] | |
| 359 | 추가: mod tests { | |
| 360 | 추가: use super::*; | |
| 361 | 추가: use egui::{pos2, vec2}; | |
| 362 | 추가: | |
| 363 | 추가: fn target() -> VideoTarget { | |
| 364 | 추가: VideoTarget { | |
| 365 | 추가: rect: Rect::from_min_size(pos2(40.0, 22.0), vec2(360.0, 640.0)), | |
| 366 | 추가: width: 1080, | |
| 367 | 추가: height: 1920, | |
| 368 | 추가: } | |
| 369 | 추가: } | |
| 370 | 추가: | |
| 371 | 추가: #[test] | |
| 372 | 추가: fn maps_window_points_to_frame_pixels() { | |
| 373 | 추가: let v = target(); | |
| 374 | 추가: assert_eq!(v.to_frame(v.rect.min), (0, 0)); | |
| 375 | 추가: assert_eq!(v.to_frame(v.rect.center()), (540, 960)); | |
| 376 | 추가: assert_eq!(v.to_frame(v.rect.max), (1079, 1919)); | |
| 377 | 추가: assert_eq!(v.to_frame(pos2(0.0, 2000.0)), (0, 1919)); | |
| 378 | 추가: } | |
| 379 | 추가: | |
| 380 | 추가: fn wheel(unit: MouseWheelUnit, delta: Vec2) -> Event { | |
| 381 | 추가: Event::MouseWheel { | |
| 382 | 추가: unit, | |
| 383 | 추가: delta, | |
| 384 | 추가: phase: egui::TouchPhase::Move, | |
| 385 | 추가: source: egui::MouseWheelSource::Unknown, | |
| 386 | 추가: modifiers: Modifiers::NONE, | |
| 387 | 추가: } | |
| 388 | 추가: } | |
| 389 | 추가: | |
| 390 | 추가: fn scrolls(out: &[Message]) -> Vec<(i16, i16)> { | |
| 391 | 추가: out.iter() | |
| 392 | 추가: .filter_map(|m| match m { | |
| 393 | 추가: Message::ScrollEvent(s) => Some((s.h_scroll, s.v_scroll)), | |
| 394 | 추가: _ => None, | |
| 395 | 추가: }) | |
| 396 | 추가: .collect() | |
| 397 | 추가: } | |
| 398 | 추가: | |
| 399 | 추가: #[test] | |
| 400 | 추가: fn wheel_units_and_signs() { | |
| 401 | 추가: let v = target(); | |
| 402 | 추가: let mut s = InputState::default(); | |
| 403 | 추가: let mut out = Vec::new(); | |
| 404 | 추가: let events = [ | |
| 405 | 추가: Event::PointerMoved(v.rect.center()), | |
| 406 | 추가: wheel(MouseWheelUnit::Line, vec2(0.0, 1.0)), | |
| 407 | 추가: wheel(MouseWheelUnit::Line, vec2(1.0, 0.0)), | |
| 408 | 추가: wheel(MouseWheelUnit::Page, vec2(0.0, -1.0)), | |
| 409 | 추가: wheel(MouseWheelUnit::Point, vec2(0.0, POINTS_PER_NOTCH)), | |
| 410 | 추가: ]; | |
| 411 | 추가: s.process(&events, true, Some(v), &mut out); | |
| 412 | 추가: assert_eq!(scrolls(&out), [(0, 120), (-120, 0), (0, -360), (0, 120)]); | |
| 413 | 추가: } | |
| 414 | 추가: | |
| 415 | 추가: #[test] | |
| 416 | 추가: fn small_pixel_deltas_accumulate() { | |
| 417 | 추가: let v = target(); | |
| 418 | 추가: let mut s = InputState::default(); | |
| 419 | 추가: let mut out = Vec::new(); | |
| 420 | 추가: let step = wheel(MouseWheelUnit::Point, vec2(0.0, 0.2)); | |
| 421 | 추가: s.process( | |
| 422 | 추가: &[Event::PointerMoved(v.rect.center())], | |
| 423 | 추가: true, | |
| 424 | 추가: Some(v), | |
| 425 | 추가: &mut out, | |
| 426 | 추가: ); | |
| 427 | 추가: for _ in 0..10 { | |
| 428 | 추가: s.process(std::slice::from_ref(&step), true, Some(v), &mut out); | |
| 429 | 추가: } | |
| 430 | 추가: // 0.2pt = 0.48 단위씩 10번 = 4.8 → 정수 부분만 나간다. | |
| 431 | 추가: let total: i32 = scrolls(&out).iter().map(|&(_, y)| i32::from(y)).sum(); | |
| 432 | 추가: assert_eq!(total, 4); | |
| 433 | 추가: } | |
| 434 | 추가: | |
| 435 | 추가: #[test] | |
| 436 | 추가: fn huge_wheel_delta_is_clamped() { | |
| 437 | 추가: let v = target(); | |
| 438 | 추가: let mut s = InputState::default(); | |
| 439 | 추가: let mut out = Vec::new(); | |
| 440 | 추가: let events = [ | |
| 441 | 추가: Event::PointerMoved(v.rect.center()), | |
| 442 | 추가: wheel(MouseWheelUnit::Line, vec2(0.0, 1000.0)), | |
| 443 | 추가: ]; | |
| 444 | 추가: s.process(&events, true, Some(v), &mut out); | |
| 445 | 추가: assert_eq!(scrolls(&out), [(0, i16::MAX)]); | |
| 446 | 추가: } | |
| 447 | 추가: | |
| 448 | 추가: #[test] | |
| 449 | 추가: fn focus_loss_cancels_touch_and_releases_keys() { | |
| 450 | 추가: let v = target(); | |
| 451 | 추가: let mut s = InputState::default(); | |
| 452 | 추가: let mut out = Vec::new(); | |
| 453 | 추가: let press = Event::PointerButton { | |
| 454 | 추가: pos: v.rect.center(), | |
| 455 | 추가: button: PointerButton::Primary, | |
| 456 | 추가: pressed: true, | |
| 457 | 추가: modifiers: Modifiers::NONE, | |
| 458 | 추가: }; | |
| 459 | 추가: let enter = Event::Key { | |
| 460 | 추가: key: Key::Enter, | |
| 461 | 추가: physical_key: None, | |
| 462 | 추가: pressed: true, | |
| 463 | 추가: repeat: false, | |
| 464 | 추가: modifiers: Modifiers::NONE, | |
| 465 | 추가: }; | |
| 466 | 추가: s.process(&[press, enter], true, Some(v), &mut out); | |
| 467 | 추가: out.clear(); | |
| 468 | 추가: s.process(&[Event::WindowFocused(false)], true, Some(v), &mut out); | |
| 469 | 추가: assert!(matches!( | |
| 470 | 추가: out.as_slice(), | |
| 471 | 추가: [ | |
| 472 | 추가: Message::TouchEvent(TouchEvent { | |
| 473 | 추가: action: TouchAction::Cancel, | |
| 474 | 추가: .. | |
| 475 | 추가: }), | |
| 476 | 추가: Message::KeyEvent(KeyEvent { | |
| 477 | 추가: action: KeyAction::Up, | |
| 478 | 추가: keycode: 66, | |
| 479 | 추가: .. | |
| 480 | 추가: }), | |
| 481 | 추가: ] | |
| 482 | 추가: )); | |
| 483 | 추가: } | |
| 484 | 추가: | |
| 485 | 추가: #[test] | |
| 486 | 추가: fn lost_target_cancels_touch() { | |
| 487 | 추가: let v = target(); | |
| 488 | 추가: let mut s = InputState::default(); | |
| 489 | 추가: let mut out = Vec::new(); | |
| 490 | 추가: let press = Event::PointerButton { | |
| 491 | 추가: pos: v.rect.center(), | |
| 492 | 추가: button: PointerButton::Primary, | |
| 493 | 추가: pressed: true, | |
| 494 | 추가: modifiers: Modifiers::NONE, | |
| 495 | 추가: }; | |
| 496 | 추가: s.process(&[press], true, Some(v), &mut out); | |
| 497 | 추가: out.clear(); | |
| 498 | 추가: s.process(&[], false, None, &mut out); | |
| 499 | 추가: assert!(matches!( | |
| 500 | 추가: out.as_slice(), | |
| 501 | 추가: [Message::TouchEvent(TouchEvent { | |
| 502 | 추가: action: TouchAction::Cancel, | |
| 503 | 추가: x: 540, | |
| 504 | 추가: y: 960, | |
| 505 | 추가: .. | |
| 506 | 추가: })] | |
| 507 | 추가: )); | |
| 508 | 추가: } | |
| 509 | 추가: | |
| 510 | 추가: #[test] | |
| 511 | 추가: fn key_repeat_increments_count() { | |
| 512 | 추가: let mut s = InputState::default(); | |
| 513 | 추가: let mut out = Vec::new(); | |
| 514 | 추가: let key = |pressed, repeat| Event::Key { | |
| 515 | 추가: key: Key::Backspace, | |
| 516 | 추가: physical_key: None, | |
| 517 | 추가: pressed, | |
| 518 | 추가: repeat, | |
| 519 | 추가: modifiers: Modifiers::NONE, | |
| 520 | 추가: }; | |
| 521 | 추가: s.process( | |
| 522 | 추가: &[ | |
| 523 | 추가: key(true, false), | |
| 524 | 추가: key(true, true), | |
| 525 | 추가: key(true, true), | |
| 526 | 추가: key(false, false), | |
| 527 | 추가: ], | |
| 528 | 추가: true, | |
| 529 | 추가: None, | |
| 530 | 추가: &mut out, | |
| 531 | 추가: ); | |
| 532 | 추가: let got: Vec<_> = out | |
| 533 | 추가: .iter() | |
| 534 | 추가: .map(|m| match m { | |
| 535 | 추가: Message::KeyEvent(k) => (k.action, k.repeat), | |
| 536 | 추가: other => panic!("{other:?}"), | |
| 537 | 추가: }) | |
| 538 | 추가: .collect(); | |
| 539 | 추가: assert_eq!( | |
| 540 | 추가: got, | |
| 541 | 추가: [ | |
| 542 | 추가: (KeyAction::Down, 0), | |
| 543 | 추가: (KeyAction::Down, 1), | |
| 544 | 추가: (KeyAction::Down, 2), | |
| 545 | 추가: (KeyAction::Up, 0) | |
| 546 | 추가: ] | |
| 547 | 추가: ); | |
| 548 | 추가: } | |
| 549 | 추가: | |
| 550 | 추가: #[test] | |
| 551 | 추가: fn space_sends_key_only_without_text() { | |
| 552 | 추가: let mut s = InputState::default(); | |
| 553 | 추가: let mut out = Vec::new(); | |
| 554 | 추가: let space = |pressed, modifiers| Event::Key { | |
| 555 | 추가: key: Key::Space, | |
| 556 | 추가: physical_key: None, | |
| 557 | 추가: pressed, | |
| 558 | 추가: repeat: false, | |
| 559 | 추가: modifiers, | |
| 560 | 추가: }; | |
| 561 | 추가: s.process( | |
| 562 | 추가: &[ | |
| 563 | 추가: space(true, Modifiers::NONE), | |
| 564 | 추가: Event::Text(" ".into()), | |
| 565 | 추가: space(false, Modifiers::NONE), | |
| 566 | 추가: ], | |
| 567 | 추가: true, | |
| 568 | 추가: None, | |
| 569 | 추가: &mut out, | |
| 570 | 추가: ); | |
| 571 | 추가: assert_eq!(out, [Message::TextInput(TextInput { text: " ".into() })]); | |
| 572 | 추가: out.clear(); | |
| 573 | 추가: s.process( | |
| 574 | 추가: &[space(true, Modifiers::CTRL), space(false, Modifiers::CTRL)], | |
| 575 | 추가: true, | |
| 576 | 추가: None, | |
| 577 | 추가: &mut out, | |
| 578 | 추가: ); | |
| 579 | 추가: assert_eq!(out.len(), 2); | |
| 580 | 추가: assert!(matches!( | |
| 581 | 추가: out[0], | |
| 582 | 추가: Message::KeyEvent(KeyEvent { | |
| 583 | 추가: action: KeyAction::Down, | |
| 584 | 추가: keycode: keymap::KEYCODE_SPACE, | |
| 585 | 추가: meta_state: keymap::META_CTRL, | |
| 586 | 추가: .. | |
| 587 | 추가: }) | |
| 588 | 추가: )); | |
| 589 | 추가: } | |
| 590 | 추가: | |
| 591 | 추가: #[test] | |
| 592 | 추가: fn alt_letter_text_is_not_doubled() { | |
| 593 | 추가: let mut s = InputState::default(); | |
| 594 | 추가: let mut out = Vec::new(); | |
| 595 | 추가: let events = [ | |
| 596 | 추가: Event::Key { | |
| 597 | 추가: key: Key::F, | |
| 598 | 추가: physical_key: None, | |
| 599 | 추가: pressed: true, | |
| 600 | 추가: repeat: false, | |
| 601 | 추가: modifiers: Modifiers::ALT, | |
| 602 | 추가: }, | |
| 603 | 추가: Event::Text("f".into()), | |
| 604 | 추가: ]; | |
| 605 | 추가: s.process(&events, true, None, &mut out); | |
| 606 | 추가: assert!(matches!( | |
| 607 | 추가: out.as_slice(), | |
| 608 | 추가: [Message::KeyEvent(KeyEvent { | |
| 609 | 추가: keycode: 34, | |
| 610 | 추가: meta_state: keymap::META_ALT, | |
| 611 | 추가: .. | |
| 612 | 추가: })] | |
| 613 | 추가: )); | |
| 614 | 추가: } | |
| 615 | 추가: } |
| @@ -1,11 +1,18 @@ | ||
| 1 | 삭제: //! 마우스 조작 시뮬레이션: 아이콘 클릭과 빈 공간 드래그(창 이동)가 서로 가로채지 않는지 확인한다. | |
| 1 | 추가: //! 마우스 조작 시뮬레이션: 아이콘 클릭과 빈 공간 드래그(창 이동)가 서로 가로채지 않는지, | |
| 2 | 추가: //! 휴대폰 화면 위 입력이 올바른 제어 메시지로 바뀌는지 확인한다. | |
| 2 | 3 | |
| 3 | 삭제: use egui::{Pos2, Rect, pos2, vec2}; | |
| 4 | 추가: use std::sync::Arc; | |
| 5 | 추가: | |
| 6 | 추가: use egui::{Event, Key, Modifiers, Pos2, Rect, pos2, vec2}; | |
| 4 | 7 | use egui_kittest::Harness; |
| 5 | 삭제: use wayland_android_remote::protocol::{AuthGrant, SystemAction}; | |
| 8 | 추가: use wayland_android_remote::protocol::{ | |
| 9 | 추가: AuthGrant, KeyAction, Message, ScrollEvent, SystemAction, TextInput, TouchAction, TouchEvent, | |
| 10 | 추가: VideoCodec, VideoConfig, | |
| 11 | 추가: }; | |
| 6 | 12 | |
| 7 | 13 | use super::conn::ConnEvent; |
| 8 | 14 | use super::layout; |
| 15 | 추가: use super::video_render::YuvFrame; | |
| 9 | 16 | use super::view::{Actions, View}; |
| 10 | 17 | |
| 11 | 18 | #[derive(Default)] |
| @@ -14,6 +21,8 @@ struct Recorded { | ||
| 14 | 21 | drag: bool, |
| 15 | 22 | open_settings: bool, |
| 16 | 23 | system: Option<SystemAction>, |
| 24 | 추가: input: Vec<Message>, | |
| 25 | 추가: resize: Option<layout::ResizeDir>, | |
| 17 | 26 | } |
| 18 | 27 | |
| 19 | 28 | struct State { |
| @@ -21,6 +30,10 @@ struct State { | ||
| 21 | 30 | rec: Recorded, |
| 22 | 31 | } |
| 23 | 32 | |
| 33 | 추가: /// 영상 프레임 크기. 기본 화면(360x800)과 종횡비가 같아 영상이 휴대폰 화면 영역을 꽉 채운다. | |
| 34 | 추가: const FRAME_W: u16 = 864; | |
| 35 | 추가: const FRAME_H: u16 = 1920; | |
| 36 | 추가: | |
| 24 | 37 | fn harness(connected: bool) -> Harness<'static, State> { |
| 25 | 38 | let mut view = View::for_test(); |
| 26 | 39 | if connected { |
| @@ -29,6 +42,38 @@ fn harness(connected: bool) -> Harness<'static, State> { | ||
| 29 | 42 | grant: AuthGrant::Always, |
| 30 | 43 | }); |
| 31 | 44 | } |
| 45 | 추가: build(view) | |
| 46 | 추가: } | |
| 47 | 추가: | |
| 48 | 추가: /// 연결되고 영상 설정과 프레임까지 받은 상태. 프레임 내용은 그리지 않으므로 비워 둔다. | |
| 49 | 추가: fn harness_with_video() -> Harness<'static, State> { | |
| 50 | 추가: let mut view = View::for_test(); | |
| 51 | 추가: view.start_connecting_for_test("10.0.0.2:27200".parse().unwrap()); | |
| 52 | 추가: view.on_conn_event(ConnEvent::Connected { | |
| 53 | 추가: grant: AuthGrant::Always, | |
| 54 | 추가: }); | |
| 55 | 추가: view.on_conn_event(ConnEvent::VideoConfig(VideoConfig { | |
| 56 | 추가: codec: VideoCodec::H264, | |
| 57 | 추가: config_gen: 1, | |
| 58 | 추가: width: FRAME_W, | |
| 59 | 추가: height: FRAME_H, | |
| 60 | 추가: fps: 60, | |
| 61 | 추가: bitrate: 8_000_000, | |
| 62 | 추가: display_width: 1080, | |
| 63 | 추가: display_height: 2400, | |
| 64 | 추가: })); | |
| 65 | 추가: view.set_video_frame(Arc::new(YuvFrame { | |
| 66 | 추가: width: u32::from(FRAME_W), | |
| 67 | 추가: height: u32::from(FRAME_H), | |
| 68 | 추가: nv12: true, | |
| 69 | 추가: planes: Vec::new(), | |
| 70 | 추가: bt709: true, | |
| 71 | 추가: full_range: false, | |
| 72 | 추가: })); | |
| 73 | 추가: build(view) | |
| 74 | 추가: } | |
| 75 | 추가: | |
| 76 | 추가: fn build(view: View) -> Harness<'static, State> { | |
| 32 | 77 | let size = view.window_size(); |
| 33 | 78 | let state = State { |
| 34 | 79 | view, |
| @@ -46,6 +91,8 @@ fn harness(connected: bool) -> Harness<'static, State> { | ||
| 46 | 91 | s.rec.drag |= a.start_drag; |
| 47 | 92 | s.rec.open_settings |= a.open_settings; |
| 48 | 93 | s.rec.system = s.rec.system.or(a.system_action); |
| 94 | 추가: s.rec.input.extend(a.input); | |
| 95 | 추가: s.rec.resize = s.rec.resize.or(a.start_resize); | |
| 49 | 96 | }, |
| 50 | 97 | state, |
| 51 | 98 | ); |
| @@ -170,3 +217,423 @@ fn mouse_back_side_button_ignored_when_disconnected() { | ||
| 170 | 217 | side_button(&mut h, l.viewport.center(), egui::PointerButton::Extra1); |
| 171 | 218 | assert_eq!(h.state().rec.system, None); |
| 172 | 219 | } |
| 220 | 추가: | |
| 221 | 추가: // --------------------------------------------------------------------------- | |
| 222 | 추가: // 휴대폰 화면 입력 | |
| 223 | 추가: // --------------------------------------------------------------------------- | |
| 224 | 추가: | |
| 225 | 추가: fn touches(h: &Harness<'_, State>) -> Vec<TouchEvent> { | |
| 226 | 추가: h.state() | |
| 227 | 추가: .rec | |
| 228 | 추가: .input | |
| 229 | 추가: .iter() | |
| 230 | 추가: .filter_map(|m| match m { | |
| 231 | 추가: Message::TouchEvent(t) => Some(*t), | |
| 232 | 추가: _ => None, | |
| 233 | 추가: }) | |
| 234 | 추가: .collect() | |
| 235 | 추가: } | |
| 236 | 추가: | |
| 237 | 추가: fn keys(h: &Harness<'_, State>) -> Vec<(KeyAction, u16, u32)> { | |
| 238 | 추가: h.state() | |
| 239 | 추가: .rec | |
| 240 | 추가: .input | |
| 241 | 추가: .iter() | |
| 242 | 추가: .filter_map(|m| match m { | |
| 243 | 추가: Message::KeyEvent(k) => Some((k.action, k.keycode, k.meta_state)), | |
| 244 | 추가: _ => None, | |
| 245 | 추가: }) | |
| 246 | 추가: .collect() | |
| 247 | 추가: } | |
| 248 | 추가: | |
| 249 | 추가: fn text(s: &str) -> Message { | |
| 250 | 추가: Message::TextInput(TextInput { text: s.to_owned() }) | |
| 251 | 추가: } | |
| 252 | 추가: | |
| 253 | 추가: /// 같은 프레임에 이벤트를 한꺼번에 넣는다. egui-winit은 키와 그 글자를 한 번에 넘긴다. | |
| 254 | 추가: fn frame_events(h: &mut Harness<'_, State>, events: impl IntoIterator<Item = Event>) { | |
| 255 | 추가: h.input_mut().events.extend(events); | |
| 256 | 추가: h.step(); | |
| 257 | 추가: } | |
| 258 | 추가: | |
| 259 | 추가: fn key_event(key: Key, pressed: bool, modifiers: Modifiers) -> Event { | |
| 260 | 추가: Event::Key { | |
| 261 | 추가: key, | |
| 262 | 추가: physical_key: None, | |
| 263 | 추가: pressed, | |
| 264 | 추가: repeat: false, | |
| 265 | 추가: modifiers, | |
| 266 | 추가: } | |
| 267 | 추가: } | |
| 268 | 추가: | |
| 269 | 추가: #[test] | |
| 270 | 추가: fn click_on_video_sends_touch_down_and_up() { | |
| 271 | 추가: let mut h = harness_with_video(); | |
| 272 | 추가: let l = layout_of(&h); | |
| 273 | 추가: click(&mut h, l.viewport.center()); | |
| 274 | 추가: let t = touches(&h); | |
| 275 | 추가: assert_eq!(t.len(), 2, "{t:?}"); | |
| 276 | 추가: let (down, up) = (t[0], t[1]); | |
| 277 | 추가: assert_eq!(down.action, TouchAction::Down); | |
| 278 | 추가: assert_eq!(up.action, TouchAction::Up); | |
| 279 | 추가: assert_eq!( | |
| 280 | 추가: (down.x, down.y), | |
| 281 | 추가: (u32::from(FRAME_W) / 2, u32::from(FRAME_H) / 2) | |
| 282 | 추가: ); | |
| 283 | 추가: assert_eq!((up.x, up.y), (down.x, down.y)); | |
| 284 | 추가: assert_eq!((down.frame_width, down.frame_height), (FRAME_W, FRAME_H)); | |
| 285 | 추가: assert_eq!(down.pointer_id, 0); | |
| 286 | 추가: assert_eq!(down.pressure, 0xFFFF); | |
| 287 | 추가: assert_eq!(up.pressure, 0); | |
| 288 | 추가: assert_eq!(h.state().rec.input.len(), 2); | |
| 289 | 추가: assert!(!h.state().rec.drag); | |
| 290 | 추가: } | |
| 291 | 추가: | |
| 292 | 추가: #[test] | |
| 293 | 추가: fn dragging_on_video_sends_moves_in_order() { | |
| 294 | 추가: let mut h = harness_with_video(); | |
| 295 | 추가: let l = layout_of(&h); | |
| 296 | 추가: let start = l.viewport.center(); | |
| 297 | 추가: h.hover_at(start); | |
| 298 | 추가: h.drag_at(start); | |
| 299 | 추가: h.step(); | |
| 300 | 추가: for dx in [10.0, 20.0, 30.0] { | |
| 301 | 추가: h.hover_at(start + vec2(dx, 0.0)); | |
| 302 | 추가: h.step(); | |
| 303 | 추가: } | |
| 304 | 추가: h.drop_at(start + vec2(30.0, 0.0)); | |
| 305 | 추가: h.step(); | |
| 306 | 추가: | |
| 307 | 추가: let t = touches(&h); | |
| 308 | 추가: let actions: Vec<_> = t.iter().map(|e| e.action).collect(); | |
| 309 | 추가: assert_eq!(actions.first(), Some(&TouchAction::Down)); | |
| 310 | 추가: assert_eq!(actions.last(), Some(&TouchAction::Up)); | |
| 311 | 추가: assert!( | |
| 312 | 추가: actions[1..actions.len() - 1] | |
| 313 | 추가: .iter() | |
| 314 | 추가: .all(|a| *a == TouchAction::Move) | |
| 315 | 추가: ); | |
| 316 | 추가: assert_eq!(actions.len(), 5, "{actions:?}"); | |
| 317 | 추가: assert!(t.windows(2).all(|w| w[0].x <= w[1].x)); | |
| 318 | 추가: assert!(t[1].x > t[0].x && t[3].x > t[1].x); | |
| 319 | 추가: assert!(t.iter().all(|e| e.y == t[0].y)); | |
| 320 | 추가: } | |
| 321 | 추가: | |
| 322 | 추가: #[test] | |
| 323 | 추가: fn moves_are_coalesced_per_frame() { | |
| 324 | 추가: let mut h = harness_with_video(); | |
| 325 | 추가: let l = layout_of(&h); | |
| 326 | 추가: let start = l.viewport.center(); | |
| 327 | 추가: frame_events( | |
| 328 | 추가: &mut h, | |
| 329 | 추가: [ | |
| 330 | 추가: Event::PointerMoved(start), | |
| 331 | 추가: Event::PointerButton { | |
| 332 | 추가: pos: start, | |
| 333 | 추가: button: egui::PointerButton::Primary, | |
| 334 | 추가: pressed: true, | |
| 335 | 추가: modifiers: Modifiers::NONE, | |
| 336 | 추가: }, | |
| 337 | 추가: ], | |
| 338 | 추가: ); | |
| 339 | 추가: frame_events( | |
| 340 | 추가: &mut h, | |
| 341 | 추가: (1..=5).map(|i| Event::PointerMoved(start + vec2(i as f32 * 4.0, 0.0))), | |
| 342 | 추가: ); | |
| 343 | 추가: // 움직이지 않은 프레임은 이동을 보내지 않는다. | |
| 344 | 추가: frame_events(&mut h, [Event::PointerMoved(start + vec2(20.0, 0.0))]); | |
| 345 | 추가: let actions: Vec<_> = touches(&h).iter().map(|e| e.action).collect(); | |
| 346 | 추가: assert_eq!(actions, [TouchAction::Down, TouchAction::Move]); | |
| 347 | 추가: } | |
| 348 | 추가: | |
| 349 | 추가: #[test] | |
| 350 | 추가: fn release_outside_video_is_clamped() { | |
| 351 | 추가: let mut h = harness_with_video(); | |
| 352 | 추가: let l = layout_of(&h); | |
| 353 | 추가: let start = l.viewport.center(); | |
| 354 | 추가: h.hover_at(start); | |
| 355 | 추가: h.drag_at(start); | |
| 356 | 추가: h.step(); | |
| 357 | 추가: let outside = pos2(l.viewport.max.x + 200.0, l.viewport.max.y + 300.0); | |
| 358 | 추가: h.hover_at(outside); | |
| 359 | 추가: h.step(); | |
| 360 | 추가: h.drop_at(outside); | |
| 361 | 추가: h.step(); | |
| 362 | 추가: let up = *touches(&h).last().unwrap(); | |
| 363 | 추가: assert_eq!(up.action, TouchAction::Up); | |
| 364 | 추가: assert_eq!( | |
| 365 | 추가: (up.x, up.y), | |
| 366 | 추가: (u32::from(FRAME_W) - 1, u32::from(FRAME_H) - 1) | |
| 367 | 추가: ); | |
| 368 | 추가: } | |
| 369 | 추가: | |
| 370 | 추가: #[test] | |
| 371 | 추가: fn press_outside_video_sends_no_touch() { | |
| 372 | 추가: let mut h = harness_with_video(); | |
| 373 | 추가: let l = layout_of(&h); | |
| 374 | 추가: click(&mut h, l.nav_bar.min + vec2(8.0, 8.0)); | |
| 375 | 추가: assert!(touches(&h).is_empty()); | |
| 376 | 추가: } | |
| 377 | 추가: | |
| 378 | 추가: #[test] | |
| 379 | 추가: fn right_and_middle_click_send_nothing() { | |
| 380 | 추가: let mut h = harness_with_video(); | |
| 381 | 추가: let l = layout_of(&h); | |
| 382 | 추가: for button in [ | |
| 383 | 추가: egui::PointerButton::Secondary, | |
| 384 | 추가: egui::PointerButton::Middle, | |
| 385 | 추가: egui::PointerButton::Extra2, | |
| 386 | 추가: ] { | |
| 387 | 추가: side_button(&mut h, l.viewport.center(), button); | |
| 388 | 추가: } | |
| 389 | 추가: assert!(h.state().rec.input.is_empty(), "{:?}", h.state().rec.input); | |
| 390 | 추가: assert_eq!(h.state().rec.system, None); | |
| 391 | 추가: } | |
| 392 | 추가: | |
| 393 | 추가: #[test] | |
| 394 | 추가: fn wheel_up_scrolls_positive() { | |
| 395 | 추가: let mut h = harness_with_video(); | |
| 396 | 추가: let l = layout_of(&h); | |
| 397 | 추가: h.hover_at(l.viewport.center()); | |
| 398 | 추가: h.step(); | |
| 399 | 추가: h.event(Event::MouseWheel { | |
| 400 | 추가: unit: egui::MouseWheelUnit::Line, | |
| 401 | 추가: delta: vec2(0.0, 1.0), | |
| 402 | 추가: phase: egui::TouchPhase::Move, | |
| 403 | 추가: source: egui::MouseWheelSource::Wheel, | |
| 404 | 추가: modifiers: Modifiers::NONE, | |
| 405 | 추가: }); | |
| 406 | 추가: h.step(); | |
| 407 | 추가: assert_eq!( | |
| 408 | 추가: h.state().rec.input, | |
| 409 | 추가: [Message::ScrollEvent(ScrollEvent { | |
| 410 | 추가: x: u32::from(FRAME_W) / 2, | |
| 411 | 추가: y: u32::from(FRAME_H) / 2, | |
| 412 | 추가: frame_width: FRAME_W, | |
| 413 | 추가: frame_height: FRAME_H, | |
| 414 | 추가: h_scroll: 0, | |
| 415 | 추가: v_scroll: 120, | |
| 416 | 추가: })] | |
| 417 | 추가: ); | |
| 418 | 추가: } | |
| 419 | 추가: | |
| 420 | 추가: #[test] | |
| 421 | 추가: fn wheel_outside_video_sends_nothing() { | |
| 422 | 추가: let mut h = harness_with_video(); | |
| 423 | 추가: let l = layout_of(&h); | |
| 424 | 추가: h.hover_at(l.rail.center()); | |
| 425 | 추가: h.step(); | |
| 426 | 추가: h.event(Event::MouseWheel { | |
| 427 | 추가: unit: egui::MouseWheelUnit::Line, | |
| 428 | 추가: delta: vec2(0.0, 1.0), | |
| 429 | 추가: phase: egui::TouchPhase::Move, | |
| 430 | 추가: source: egui::MouseWheelSource::Wheel, | |
| 431 | 추가: modifiers: Modifiers::NONE, | |
| 432 | 추가: }); | |
| 433 | 추가: h.step(); | |
| 434 | 추가: assert!(h.state().rec.input.is_empty()); | |
| 435 | 추가: } | |
| 436 | 추가: | |
| 437 | 추가: #[test] | |
| 438 | 추가: fn typing_letter_sends_text_only() { | |
| 439 | 추가: let mut h = harness_with_video(); | |
| 440 | 추가: frame_events( | |
| 441 | 추가: &mut h, | |
| 442 | 추가: [ | |
| 443 | 추가: key_event(Key::A, true, Modifiers::NONE), | |
| 444 | 추가: Event::Text("a".to_owned()), | |
| 445 | 추가: ], | |
| 446 | 추가: ); | |
| 447 | 추가: frame_events(&mut h, [key_event(Key::A, false, Modifiers::NONE)]); | |
| 448 | 추가: assert_eq!(h.state().rec.input, [text("a")]); | |
| 449 | 추가: } | |
| 450 | 추가: | |
| 451 | 추가: #[test] | |
| 452 | 추가: fn enter_sends_key_down_and_up() { | |
| 453 | 추가: let mut h = harness(true); | |
| 454 | 추가: h.key_press(Key::Enter); | |
| 455 | 추가: h.step(); | |
| 456 | 추가: assert_eq!(keys(&h), [(KeyAction::Down, 66, 0), (KeyAction::Up, 66, 0)]); | |
| 457 | 추가: assert_eq!(h.state().rec.input.len(), 2); | |
| 458 | 추가: } | |
| 459 | 추가: | |
| 460 | 추가: #[test] | |
| 461 | 추가: fn ctrl_c_sends_copy_action_only() { | |
| 462 | 추가: let mut h = harness(true); | |
| 463 | 추가: h.key_press_modifiers(Modifiers::CTRL, Key::C); | |
| 464 | 추가: h.step(); | |
| 465 | 추가: assert_eq!( | |
| 466 | 추가: h.state().rec.input, | |
| 467 | 추가: [Message::SystemAction(SystemAction::Copy)] | |
| 468 | 추가: ); | |
| 469 | 추가: } | |
| 470 | 추가: | |
| 471 | 추가: #[test] | |
| 472 | 추가: fn ctrl_z_sends_key_with_ctrl_meta() { | |
| 473 | 추가: let mut h = harness(true); | |
| 474 | 추가: h.key_press_modifiers(Modifiers::CTRL, Key::Z); | |
| 475 | 추가: h.step(); | |
| 476 | 추가: assert_eq!( | |
| 477 | 추가: keys(&h), | |
| 478 | 추가: [(KeyAction::Down, 54, 0x3000), (KeyAction::Up, 54, 0x3000)] | |
| 479 | 추가: ); | |
| 480 | 추가: assert_eq!(h.state().rec.input.len(), 2); | |
| 481 | 추가: } | |
| 482 | 추가: | |
| 483 | 추가: #[test] | |
| 484 | 추가: fn tab_goes_to_phone_instead_of_moving_focus() { | |
| 485 | 추가: let mut h = harness(true); | |
| 486 | 추가: // egui 기본 동작이면 Tab 두 번에 아이콘 열 빈 곳 → 설정 버튼 순서로 포커스가 간다. | |
| 487 | 추가: for _ in 0..2 { | |
| 488 | 추가: h.key_press(Key::Tab); | |
| 489 | 추가: h.step(); | |
| 490 | 추가: } | |
| 491 | 추가: assert_eq!(h.ctx.memory(|m| m.focused()), None); | |
| 492 | 추가: h.key_press(Key::Enter); | |
| 493 | 추가: h.key_press(Key::Space); | |
| 494 | 추가: h.step(); | |
| 495 | 추가: // 설정 버튼이 포커스를 받았다면 Enter·Space가 버튼을 눌렀을 것이다. | |
| 496 | 추가: assert!(!h.state().rec.open_settings); | |
| 497 | 추가: assert_eq!(h.state().rec.system, None); | |
| 498 | 추가: let codes: Vec<_> = keys(&h) | |
| 499 | 추가: .into_iter() | |
| 500 | 추가: .filter(|(a, ..)| *a == KeyAction::Down) | |
| 501 | 추가: .map(|(_, code, _)| code) | |
| 502 | 추가: .collect(); | |
| 503 | 추가: assert_eq!(codes, [61, 61, 66, 62]); | |
| 504 | 추가: } | |
| 505 | 추가: | |
| 506 | 추가: #[test] | |
| 507 | 추가: fn ime_commit_sends_text_and_preedit_is_local() { | |
| 508 | 추가: let mut h = harness_with_video(); | |
| 509 | 추가: assert!( | |
| 510 | 추가: h.output().platform_output.ime.is_some(), | |
| 511 | 추가: "연결 중에는 IME를 켜 달라고 요청해야 한다" | |
| 512 | 추가: ); | |
| 513 | 추가: h.event(Event::Ime(egui::ImeEvent::Preedit { | |
| 514 | 추가: text: "하".to_owned(), | |
| 515 | 추가: active_range_chars: None, | |
| 516 | 추가: })); | |
| 517 | 추가: h.event(Event::Ime(egui::ImeEvent::Preedit { | |
| 518 | 추가: text: "한".to_owned(), | |
| 519 | 추가: active_range_chars: None, | |
| 520 | 추가: })); | |
| 521 | 추가: h.step(); | |
| 522 | 추가: assert!(h.state().rec.input.is_empty()); | |
| 523 | 추가: h.event(Event::Ime(egui::ImeEvent::Commit("한글".to_owned()))); | |
| 524 | 추가: h.step(); | |
| 525 | 추가: assert_eq!(h.state().rec.input, [text("한글")]); | |
| 526 | 추가: } | |
| 527 | 추가: | |
| 528 | 추가: #[test] | |
| 529 | 추가: fn nothing_is_sent_when_disconnected() { | |
| 530 | 추가: let mut h = harness(false); | |
| 531 | 추가: let l = layout_of(&h); | |
| 532 | 추가: click(&mut h, l.viewport.center() + vec2(0.0, 120.0)); | |
| 533 | 추가: h.hover_at(l.viewport.center()); | |
| 534 | 추가: h.event(Event::MouseWheel { | |
| 535 | 추가: unit: egui::MouseWheelUnit::Line, | |
| 536 | 추가: delta: vec2(0.0, 1.0), | |
| 537 | 추가: phase: egui::TouchPhase::Move, | |
| 538 | 추가: source: egui::MouseWheelSource::Wheel, | |
| 539 | 추가: modifiers: Modifiers::NONE, | |
| 540 | 추가: }); | |
| 541 | 추가: h.step(); | |
| 542 | 추가: frame_events( | |
| 543 | 추가: &mut h, | |
| 544 | 추가: [ | |
| 545 | 추가: key_event(Key::A, true, Modifiers::NONE), | |
| 546 | 추가: Event::Text("a".to_owned()), | |
| 547 | 추가: key_event(Key::A, false, Modifiers::NONE), | |
| 548 | 추가: ], | |
| 549 | 추가: ); | |
| 550 | 추가: h.key_press(Key::Enter); | |
| 551 | 추가: h.key_press_modifiers(Modifiers::CTRL, Key::C); | |
| 552 | 추가: h.event(Event::Ime(egui::ImeEvent::Commit("한".to_owned()))); | |
| 553 | 추가: h.step(); | |
| 554 | 추가: assert!(h.state().rec.input.is_empty(), "{:?}", h.state().rec.input); | |
| 555 | 추가: } | |
| 556 | 추가: | |
| 557 | 추가: #[test] | |
| 558 | 추가: fn connected_without_video_sends_keys_but_no_touch() { | |
| 559 | 추가: let mut h = harness(true); | |
| 560 | 추가: let l = layout_of(&h); | |
| 561 | 추가: click(&mut h, l.viewport.center()); | |
| 562 | 추가: assert!(touches(&h).is_empty()); | |
| 563 | 추가: h.event(Event::Text("x".to_owned())); | |
| 564 | 추가: h.step(); | |
| 565 | 추가: assert_eq!(h.state().rec.input, [text("x")]); | |
| 566 | 추가: } | |
| 567 | 추가: | |
| 568 | 추가: #[test] | |
| 569 | 추가: fn ended_connection_cancels_held_touch() { | |
| 570 | 추가: let mut h = harness_with_video(); | |
| 571 | 추가: let l = layout_of(&h); | |
| 572 | 추가: h.hover_at(l.viewport.center()); | |
| 573 | 추가: h.drag_at(l.viewport.center()); | |
| 574 | 추가: h.step(); | |
| 575 | 추가: h.state_mut() | |
| 576 | 추가: .view | |
| 577 | 추가: .on_conn_event(ConnEvent::Ended("연결이 끊겼습니다".to_owned())); | |
| 578 | 추가: h.step(); | |
| 579 | 추가: let actions: Vec<_> = touches(&h).iter().map(|e| e.action).collect(); | |
| 580 | 추가: assert_eq!(actions, [TouchAction::Down, TouchAction::Cancel]); | |
| 581 | 추가: } | |
| 582 | 추가: | |
| 583 | 추가: #[test] | |
| 584 | 추가: fn window_focus_loss_cancels_touch() { | |
| 585 | 추가: let mut h = harness_with_video(); | |
| 586 | 추가: let l = layout_of(&h); | |
| 587 | 추가: h.hover_at(l.viewport.center()); | |
| 588 | 추가: h.drag_at(l.viewport.center()); | |
| 589 | 추가: h.step(); | |
| 590 | 추가: h.event(Event::WindowFocused(false)); | |
| 591 | 추가: h.step(); | |
| 592 | 추가: let actions: Vec<_> = touches(&h).iter().map(|e| e.action).collect(); | |
| 593 | 추가: assert_eq!(actions, [TouchAction::Down, TouchAction::Cancel]); | |
| 594 | 추가: } | |
| 595 | 추가: | |
| 596 | 추가: #[test] | |
| 597 | 추가: fn pressing_window_edges_starts_resize_not_touch() { | |
| 598 | 추가: type Case = (fn(Rect) -> Pos2, layout::ResizeDir); | |
| 599 | 추가: let cases: [Case; 4] = [ | |
| 600 | 추가: ( | |
| 601 | 추가: |r: Rect| pos2(r.max.x - 2.0, r.center().y), | |
| 602 | 추가: layout::ResizeDir::East, | |
| 603 | 추가: ), | |
| 604 | 추가: ( | |
| 605 | 추가: |r: Rect| pos2(r.min.x + 2.0, r.center().y), | |
| 606 | 추가: layout::ResizeDir::West, | |
| 607 | 추가: ), | |
| 608 | 추가: ( | |
| 609 | 추가: |r: Rect| pos2(r.center().x, r.max.y - 2.0), | |
| 610 | 추가: layout::ResizeDir::South, | |
| 611 | 추가: ), | |
| 612 | 추가: ( | |
| 613 | 추가: |r: Rect| pos2(r.max.x - 2.0, r.max.y - 2.0), | |
| 614 | 추가: layout::ResizeDir::SouthEast, | |
| 615 | 추가: ), | |
| 616 | 추가: ]; | |
| 617 | 추가: for (at, dir) in cases { | |
| 618 | 추가: let mut h = harness_with_video(); | |
| 619 | 추가: let screen = h.state().rec.screen.unwrap(); | |
| 620 | 추가: click(&mut h, at(screen)); | |
| 621 | 추가: assert_eq!(h.state().rec.resize, Some(dir), "{dir:?}"); | |
| 622 | 추가: assert!( | |
| 623 | 추가: !h.state() | |
| 624 | 추가: .rec | |
| 625 | 추가: .input | |
| 626 | 추가: .iter() | |
| 627 | 추가: .any(|m| matches!(m, Message::TouchEvent(_))), | |
| 628 | 추가: "가장자리에서 터치가 나가면 안 된다: {dir:?}" | |
| 629 | 추가: ); | |
| 630 | 추가: } | |
| 631 | 추가: } | |
| 632 | 추가: | |
| 633 | 추가: #[test] | |
| 634 | 추가: fn pressing_inside_phone_screen_does_not_resize() { | |
| 635 | 추가: let mut h = harness_with_video(); | |
| 636 | 추가: let l = layout_of(&h); | |
| 637 | 추가: click(&mut h, l.viewport.center()); | |
| 638 | 추가: assert_eq!(h.state().rec.resize, None); | |
| 639 | 추가: } |
| @@ -0,0 +1,220 @@ | ||
| 1 | 추가: //! egui 키를 Android `KeyEvent` 값(`KEYCODE_*`, `META_*`)으로 바꾼다. | |
| 2 | 추가: //! | |
| 3 | 추가: //! 글자를 만드는 키는 TEXT_INPUT으로 보내므로 여기서 다루지 않는다. | |
| 4 | 추가: //! 예외로 Ctrl·Alt와 함께 누른 문자·숫자 키는 [`combo_keycode`]로 키 이벤트를 만든다. | |
| 5 | 추가: | |
| 6 | 추가: use egui::{Key, Modifiers}; | |
| 7 | 추가: use wayland_android_remote::protocol::SystemAction; | |
| 8 | 추가: | |
| 9 | 추가: /// Android `KeyEvent.KEYCODE_SPACE`. | |
| 10 | 추가: pub const KEYCODE_SPACE: u16 = 62; | |
| 11 | 추가: | |
| 12 | 추가: /// `META_SHIFT_ON | META_SHIFT_LEFT_ON`. | |
| 13 | 추가: pub const META_SHIFT: u32 = 0x41; | |
| 14 | 추가: /// `META_ALT_ON | META_ALT_LEFT_ON`. | |
| 15 | 추가: pub const META_ALT: u32 = 0x12; | |
| 16 | 추가: /// `META_CTRL_ON | META_CTRL_LEFT_ON`. | |
| 17 | 추가: pub const META_CTRL: u32 = 0x3000; | |
| 18 | 추가: | |
| 19 | 추가: /// 글자를 만들지 않는 키의 Android 키코드. 스페이스는 글자를 만들 수 있어 따로 처리한다. | |
| 20 | 추가: pub fn special_keycode(key: Key) -> Option<u16> { | |
| 21 | 추가: Some(match key { | |
| 22 | 추가: Key::Enter => 66, | |
| 23 | 추가: Key::Backspace => 67, // KEYCODE_DEL | |
| 24 | 추가: Key::Delete => 112, // KEYCODE_FORWARD_DEL | |
| 25 | 추가: Key::Tab => 61, | |
| 26 | 추가: Key::Escape => 111, | |
| 27 | 추가: Key::ArrowUp => 19, | |
| 28 | 추가: Key::ArrowDown => 20, | |
| 29 | 추가: Key::ArrowLeft => 21, | |
| 30 | 추가: Key::ArrowRight => 22, | |
| 31 | 추가: Key::Home => 122, // KEYCODE_MOVE_HOME | |
| 32 | 추가: Key::End => 123, // KEYCODE_MOVE_END | |
| 33 | 추가: Key::PageUp => 92, | |
| 34 | 추가: Key::PageDown => 93, | |
| 35 | 추가: Key::Insert => 124, | |
| 36 | 추가: Key::F1 => 131, | |
| 37 | 추가: Key::F2 => 132, | |
| 38 | 추가: Key::F3 => 133, | |
| 39 | 추가: Key::F4 => 134, | |
| 40 | 추가: Key::F5 => 135, | |
| 41 | 추가: Key::F6 => 136, | |
| 42 | 추가: Key::F7 => 137, | |
| 43 | 추가: Key::F8 => 138, | |
| 44 | 추가: Key::F9 => 139, | |
| 45 | 추가: Key::F10 => 140, | |
| 46 | 추가: Key::F11 => 141, | |
| 47 | 추가: Key::F12 => 142, | |
| 48 | 추가: _ => return None, | |
| 49 | 추가: }) | |
| 50 | 추가: } | |
| 51 | 추가: | |
| 52 | 추가: /// 문자·숫자 키의 Android 키코드 (A=29..Z=54, 0=7..9=16). | |
| 53 | 추가: pub fn letter_digit_keycode(key: Key) -> Option<u16> { | |
| 54 | 추가: const LETTERS: [Key; 26] = [ | |
| 55 | 추가: Key::A, | |
| 56 | 추가: Key::B, | |
| 57 | 추가: Key::C, | |
| 58 | 추가: Key::D, | |
| 59 | 추가: Key::E, | |
| 60 | 추가: Key::F, | |
| 61 | 추가: Key::G, | |
| 62 | 추가: Key::H, | |
| 63 | 추가: Key::I, | |
| 64 | 추가: Key::J, | |
| 65 | 추가: Key::K, | |
| 66 | 추가: Key::L, | |
| 67 | 추가: Key::M, | |
| 68 | 추가: Key::N, | |
| 69 | 추가: Key::O, | |
| 70 | 추가: Key::P, | |
| 71 | 추가: Key::Q, | |
| 72 | 추가: Key::R, | |
| 73 | 추가: Key::S, | |
| 74 | 추가: Key::T, | |
| 75 | 추가: Key::U, | |
| 76 | 추가: Key::V, | |
| 77 | 추가: Key::W, | |
| 78 | 추가: Key::X, | |
| 79 | 추가: Key::Y, | |
| 80 | 추가: Key::Z, | |
| 81 | 추가: ]; | |
| 82 | 추가: const DIGITS: [Key; 10] = [ | |
| 83 | 추가: Key::Num0, | |
| 84 | 추가: Key::Num1, | |
| 85 | 추가: Key::Num2, | |
| 86 | 추가: Key::Num3, | |
| 87 | 추가: Key::Num4, | |
| 88 | 추가: Key::Num5, | |
| 89 | 추가: Key::Num6, | |
| 90 | 추가: Key::Num7, | |
| 91 | 추가: Key::Num8, | |
| 92 | 추가: Key::Num9, | |
| 93 | 추가: ]; | |
| 94 | 추가: if let Some(i) = LETTERS.iter().position(|&k| k == key) { | |
| 95 | 추가: return Some(29 + i as u16); | |
| 96 | 추가: } | |
| 97 | 추가: DIGITS.iter().position(|&k| k == key).map(|i| 7 + i as u16) | |
| 98 | 추가: } | |
| 99 | 추가: | |
| 100 | 추가: /// Ctrl·Alt 조합일 때만 문자·숫자 키를 키 이벤트로 보낸다. 그 밖에는 TEXT_INPUT이 맡는다. | |
| 101 | 추가: pub fn combo_keycode(key: Key, modifiers: Modifiers) -> Option<u16> { | |
| 102 | 추가: if modifiers.ctrl || modifiers.alt { | |
| 103 | 추가: letter_digit_keycode(key) | |
| 104 | 추가: } else { | |
| 105 | 추가: None | |
| 106 | 추가: } | |
| 107 | 추가: } | |
| 108 | 추가: | |
| 109 | 추가: /// egui 보조키 상태를 Android `META_*` 비트로 바꾼다. 왼쪽/오른쪽 구분이 없어 왼쪽으로 둔다. | |
| 110 | 추가: pub fn meta_state(modifiers: Modifiers) -> u32 { | |
| 111 | 추가: let mut meta = 0; | |
| 112 | 추가: if modifiers.shift { | |
| 113 | 추가: meta |= META_SHIFT; | |
| 114 | 추가: } | |
| 115 | 추가: if modifiers.alt { | |
| 116 | 추가: meta |= META_ALT; | |
| 117 | 추가: } | |
| 118 | 추가: if modifiers.ctrl { | |
| 119 | 추가: meta |= META_CTRL; | |
| 120 | 추가: } | |
| 121 | 추가: meta | |
| 122 | 추가: } | |
| 123 | 추가: | |
| 124 | 추가: /// Ctrl+C/V/X/A (다른 보조키 없이)와 전용 복사·붙여넣기·잘라내기 키를 시스템 동작으로 바꾼다. | |
| 125 | 추가: pub fn clipboard_action(key: Key, modifiers: Modifiers) -> Option<SystemAction> { | |
| 126 | 추가: match key { | |
| 127 | 추가: Key::Copy => return Some(SystemAction::Copy), | |
| 128 | 추가: Key::Paste => return Some(SystemAction::Paste), | |
| 129 | 추가: Key::Cut => return Some(SystemAction::Cut), | |
| 130 | 추가: _ => {} | |
| 131 | 추가: } | |
| 132 | 추가: if !modifiers.ctrl || modifiers.alt || modifiers.shift { | |
| 133 | 추가: return None; | |
| 134 | 추가: } | |
| 135 | 추가: match key { | |
| 136 | 추가: Key::C => Some(SystemAction::Copy), | |
| 137 | 추가: Key::V => Some(SystemAction::Paste), | |
| 138 | 추가: Key::X => Some(SystemAction::Cut), | |
| 139 | 추가: Key::A => Some(SystemAction::SelectAll), | |
| 140 | 추가: _ => None, | |
| 141 | 추가: } | |
| 142 | 추가: } | |
| 143 | 추가: | |
| 144 | 추가: #[cfg(test)] | |
| 145 | 추가: mod tests { | |
| 146 | 추가: use super::*; | |
| 147 | 추가: | |
| 148 | 추가: #[test] | |
| 149 | 추가: fn special_keys_map_to_android_keycodes() { | |
| 150 | 추가: assert_eq!(special_keycode(Key::Enter), Some(66)); | |
| 151 | 추가: assert_eq!(special_keycode(Key::Backspace), Some(67)); | |
| 152 | 추가: assert_eq!(special_keycode(Key::Delete), Some(112)); | |
| 153 | 추가: assert_eq!(special_keycode(Key::Tab), Some(61)); | |
| 154 | 추가: assert_eq!(special_keycode(Key::Escape), Some(111)); | |
| 155 | 추가: assert_eq!(special_keycode(Key::ArrowUp), Some(19)); | |
| 156 | 추가: assert_eq!(special_keycode(Key::ArrowDown), Some(20)); | |
| 157 | 추가: assert_eq!(special_keycode(Key::ArrowLeft), Some(21)); | |
| 158 | 추가: assert_eq!(special_keycode(Key::ArrowRight), Some(22)); | |
| 159 | 추가: assert_eq!(special_keycode(Key::Home), Some(122)); | |
| 160 | 추가: assert_eq!(special_keycode(Key::End), Some(123)); | |
| 161 | 추가: assert_eq!(special_keycode(Key::PageUp), Some(92)); | |
| 162 | 추가: assert_eq!(special_keycode(Key::PageDown), Some(93)); | |
| 163 | 추가: assert_eq!(special_keycode(Key::Insert), Some(124)); | |
| 164 | 추가: assert_eq!(special_keycode(Key::F1), Some(131)); | |
| 165 | 추가: assert_eq!(special_keycode(Key::F12), Some(142)); | |
| 166 | 추가: } | |
| 167 | 추가: | |
| 168 | 추가: #[test] | |
| 169 | 추가: fn text_keys_are_not_special() { | |
| 170 | 추가: for key in [Key::A, Key::Num1, Key::Space, Key::Minus, Key::Comma] { | |
| 171 | 추가: assert_eq!(special_keycode(key), None, "{key:?}"); | |
| 172 | 추가: } | |
| 173 | 추가: } | |
| 174 | 추가: | |
| 175 | 추가: #[test] | |
| 176 | 추가: fn letters_and_digits() { | |
| 177 | 추가: assert_eq!(letter_digit_keycode(Key::A), Some(29)); | |
| 178 | 추가: assert_eq!(letter_digit_keycode(Key::Z), Some(54)); | |
| 179 | 추가: assert_eq!(letter_digit_keycode(Key::Num0), Some(7)); | |
| 180 | 추가: assert_eq!(letter_digit_keycode(Key::Num9), Some(16)); | |
| 181 | 추가: assert_eq!(letter_digit_keycode(Key::Enter), None); | |
| 182 | 추가: } | |
| 183 | 추가: | |
| 184 | 추가: #[test] | |
| 185 | 추가: fn combo_needs_ctrl_or_alt() { | |
| 186 | 추가: assert_eq!(combo_keycode(Key::Z, Modifiers::NONE), None); | |
| 187 | 추가: assert_eq!(combo_keycode(Key::Z, Modifiers::SHIFT), None); | |
| 188 | 추가: assert_eq!(combo_keycode(Key::Z, Modifiers::CTRL), Some(54)); | |
| 189 | 추가: assert_eq!(combo_keycode(Key::Num1, Modifiers::ALT), Some(8)); | |
| 190 | 추가: } | |
| 191 | 추가: | |
| 192 | 추가: #[test] | |
| 193 | 추가: fn meta_bits() { | |
| 194 | 추가: assert_eq!(meta_state(Modifiers::NONE), 0); | |
| 195 | 추가: assert_eq!(meta_state(Modifiers::SHIFT), 0x41); | |
| 196 | 추가: assert_eq!(meta_state(Modifiers::ALT), 0x12); | |
| 197 | 추가: assert_eq!(meta_state(Modifiers::CTRL), 0x3000); | |
| 198 | 추가: assert_eq!(meta_state(Modifiers::CTRL | Modifiers::SHIFT), 0x3041); | |
| 199 | 추가: } | |
| 200 | 추가: | |
| 201 | 추가: #[test] | |
| 202 | 추가: fn clipboard_shortcuts() { | |
| 203 | 추가: let ctrl = Modifiers::CTRL; | |
| 204 | 추가: assert_eq!(clipboard_action(Key::C, ctrl), Some(SystemAction::Copy)); | |
| 205 | 추가: assert_eq!(clipboard_action(Key::V, ctrl), Some(SystemAction::Paste)); | |
| 206 | 추가: assert_eq!(clipboard_action(Key::X, ctrl), Some(SystemAction::Cut)); | |
| 207 | 추가: assert_eq!( | |
| 208 | 추가: clipboard_action(Key::A, ctrl), | |
| 209 | 추가: Some(SystemAction::SelectAll) | |
| 210 | 추가: ); | |
| 211 | 추가: assert_eq!(clipboard_action(Key::Z, ctrl), None); | |
| 212 | 추가: assert_eq!(clipboard_action(Key::C, Modifiers::NONE), None); | |
| 213 | 추가: assert_eq!(clipboard_action(Key::C, ctrl | Modifiers::SHIFT), None); | |
| 214 | 추가: assert_eq!(clipboard_action(Key::C, ctrl | Modifiers::ALT), None); | |
| 215 | 추가: assert_eq!( | |
| 216 | 추가: clipboard_action(Key::Copy, Modifiers::NONE), | |
| 217 | 추가: Some(SystemAction::Copy) | |
| 218 | 추가: ); | |
| 219 | 추가: } | |
| 220 | 추가: } |
| @@ -1,7 +1,8 @@ | ||
| 1 | 1 | //! 창 각 영역의 크기와 위치. 단위는 논리 픽셀. |
| 2 | 2 | //! |
| 3 | 3 | //! 창 = [제목 표시줄] 아래에 [왼쪽 아이콘 열][휴대폰 화면 + 그 아래 내비게이션 줄]. |
| 4 | 삭제: //! 창 크기는 고정이고 설정 등은 별도 창으로 띄우므로 휴대폰 화면은 움직이지 않는다. | |
| 4 | 추가: //! 메뉴를 열어도 창 크기는 그대로이고, 사용자가 창 가장자리를 끌면 휴대폰 화면 비율을 | |
| 5 | 추가: //! 유지하며 커지거나 줄어든다. | |
| 5 | 6 | |
| 6 | 7 | use egui::{Rect, Vec2, pos2, vec2}; |
| 7 | 8 | |
| @@ -15,8 +16,14 @@ pub const TITLE_H: f32 = 22.0; | ||
| 15 | 16 | pub const NAV_H: f32 = 40.0; |
| 16 | 17 | /// 내비게이션 버튼 사이 간격(가운데 기준). |
| 17 | 18 | pub const NAV_SPACING: f32 = 72.0; |
| 18 | 삭제: /// 휴대폰 화면을 감싸는 검은 테두리 두께. | |
| 19 | 삭제: pub const BORDER: f32 = 4.0; | |
| 19 | 추가: /// 휴대폰 화면을 감싸는 검은 테두리 두께. 오른쪽 가장자리 크기 조절 영역을 겸한다. | |
| 20 | 추가: pub const BORDER: f32 = 6.0; | |
| 21 | 추가: /// 창 가장자리에서 크기 조절이 걸리는 두께. | |
| 22 | 추가: pub const RESIZE_EDGE: f32 = 6.0; | |
| 23 | 추가: /// 모서리 크기 조절 영역 한 변 (가장자리를 따라 이만큼은 대각선 방향). | |
| 24 | 추가: pub const RESIZE_CORNER: f32 = 16.0; | |
| 25 | 추가: /// 휴대폰 화면의 최소 높이. | |
| 26 | 추가: pub const MIN_VIEWPORT_H: f32 = 400.0; | |
| 20 | 27 | /// 연결 전 기본 화면 크기. Galaxy S21+ 화면 비율(1080x2400, 9:20)에 맞춘다. |
| 21 | 28 | pub const DEFAULT_VIEWPORT: Vec2 = vec2(360.0, 800.0); |
| 22 | 29 | |
| @@ -56,6 +63,63 @@ pub fn compute(screen: Rect) -> Layout { | ||
| 56 | 63 | } |
| 57 | 64 | } |
| 58 | 65 | |
| 66 | 추가: /// 창 가장자리 크기 조절 방향. | |
| 67 | 추가: #[derive(Debug, Clone, Copy, PartialEq, Eq)] | |
| 68 | 추가: pub enum ResizeDir { | |
| 69 | 추가: North, | |
| 70 | 추가: South, | |
| 71 | 추가: East, | |
| 72 | 추가: West, | |
| 73 | 추가: NorthEast, | |
| 74 | 추가: NorthWest, | |
| 75 | 추가: SouthEast, | |
| 76 | 추가: SouthWest, | |
| 77 | 추가: } | |
| 78 | 추가: | |
| 79 | 추가: /// 창 안쪽 좌표 `pos`가 가장자리 크기 조절 영역이면 방향을 돌려준다. | |
| 80 | 추가: pub fn resize_dir(screen: Rect, pos: egui::Pos2) -> Option<ResizeDir> { | |
| 81 | 추가: if !screen.contains(pos) { | |
| 82 | 추가: return None; | |
| 83 | 추가: } | |
| 84 | 추가: let near = |d: f32| d < RESIZE_EDGE; | |
| 85 | 추가: let corner = |d: f32| d < RESIZE_CORNER; | |
| 86 | 추가: let (l, r) = (pos.x - screen.min.x, screen.max.x - pos.x); | |
| 87 | 추가: let (t, b) = (pos.y - screen.min.y, screen.max.y - pos.y); | |
| 88 | 추가: let on_edge = near(l) || near(r) || near(t) || near(b); | |
| 89 | 추가: if !on_edge { | |
| 90 | 추가: return None; | |
| 91 | 추가: } | |
| 92 | 추가: Some(match (corner(t), corner(b), corner(l), corner(r)) { | |
| 93 | 추가: (true, _, true, _) => ResizeDir::NorthWest, | |
| 94 | 추가: (true, _, _, true) => ResizeDir::NorthEast, | |
| 95 | 추가: (_, true, true, _) => ResizeDir::SouthWest, | |
| 96 | 추가: (_, true, _, true) => ResizeDir::SouthEast, | |
| 97 | 추가: _ if near(t) => ResizeDir::North, | |
| 98 | 추가: _ if near(b) => ResizeDir::South, | |
| 99 | 추가: _ if near(l) => ResizeDir::West, | |
| 100 | 추가: _ => ResizeDir::East, | |
| 101 | 추가: }) | |
| 102 | 추가: } | |
| 103 | 추가: | |
| 104 | 추가: /// 사용자가 창 크기를 바꾼 뒤 휴대폰 화면 비율(`aspect` = 너비/높이)에 맞는 화면 크기를 구한다. | |
| 105 | 추가: /// 이전 크기와 비교해 더 많이 바뀐 쪽(너비 또는 높이)을 기준으로 나머지를 맞춘다. | |
| 106 | 추가: pub fn snap_viewport(previous: Vec2, current: Vec2, aspect: f32) -> Vec2 { | |
| 107 | 추가: let dw = (current.x - previous.x).abs(); | |
| 108 | 추가: let dh = (current.y - previous.y).abs() * aspect; | |
| 109 | 추가: let mut h = if dw > dh { | |
| 110 | 추가: current.x / aspect | |
| 111 | 추가: } else { | |
| 112 | 추가: current.y | |
| 113 | 추가: }; | |
| 114 | 추가: h = h.max(MIN_VIEWPORT_H); | |
| 115 | 추가: vec2((h * aspect).round(), h.round()) | |
| 116 | 추가: } | |
| 117 | 추가: | |
| 118 | 추가: /// 창 안쪽 크기에서 휴대폰 화면 크기를 구한다 ([`window_size`]의 역). | |
| 119 | 추가: pub fn viewport_of(window: Vec2) -> Vec2 { | |
| 120 | 추가: vec2(window.x - RAIL_W - BORDER, window.y - TITLE_H - NAV_H) | |
| 121 | 추가: } | |
| 122 | 추가: | |
| 59 | 123 | /// 내비게이션 줄의 버튼 3칸(뒤로·홈·최근 앱). 휴대폰 화면 가운데를 기준으로 배치한다. |
| 60 | 124 | pub fn nav_slots(nav_bar: Rect) -> [Rect; 3] { |
| 61 | 125 | let size = vec2(NAV_SPACING * 0.8, nav_bar.height()); |
| @@ -94,6 +158,44 @@ mod tests { | ||
| 94 | 158 | } |
| 95 | 159 | } |
| 96 | 160 | |
| 161 | 추가: #[test] | |
| 162 | 추가: fn resize_zones() { | |
| 163 | 추가: let s = Rect::from_min_size(pos2(0.0, 0.0), vec2(400.0, 800.0)); | |
| 164 | 추가: assert_eq!(resize_dir(s, pos2(2.0, 400.0)), Some(ResizeDir::West)); | |
| 165 | 추가: assert_eq!(resize_dir(s, pos2(398.0, 400.0)), Some(ResizeDir::East)); | |
| 166 | 추가: assert_eq!(resize_dir(s, pos2(200.0, 1.0)), Some(ResizeDir::North)); | |
| 167 | 추가: assert_eq!(resize_dir(s, pos2(200.0, 799.0)), Some(ResizeDir::South)); | |
| 168 | 추가: assert_eq!( | |
| 169 | 추가: resize_dir(s, pos2(398.0, 790.0)), | |
| 170 | 추가: Some(ResizeDir::SouthEast) | |
| 171 | 추가: ); | |
| 172 | 추가: assert_eq!(resize_dir(s, pos2(10.0, 2.0)), Some(ResizeDir::NorthWest)); | |
| 173 | 추가: assert_eq!(resize_dir(s, pos2(200.0, 400.0)), None); | |
| 174 | 추가: assert_eq!(resize_dir(s, pos2(20.0, 20.0)), None); | |
| 175 | 추가: } | |
| 176 | 추가: | |
| 177 | 추가: #[test] | |
| 178 | 추가: fn snap_keeps_the_dimension_the_user_changed() { | |
| 179 | 추가: let aspect = 0.45; | |
| 180 | 추가: let prev = vec2(360.0, 800.0); | |
| 181 | 추가: // 너비만 넓혔으면 높이를 맞춘다. | |
| 182 | 추가: assert_eq!( | |
| 183 | 추가: snap_viewport(prev, vec2(450.0, 800.0), aspect), | |
| 184 | 추가: vec2(450.0, 1000.0) | |
| 185 | 추가: ); | |
| 186 | 추가: // 높이만 줄였으면 너비를 맞춘다. | |
| 187 | 추가: assert_eq!( | |
| 188 | 추가: snap_viewport(prev, vec2(360.0, 600.0), aspect), | |
| 189 | 추가: vec2(270.0, 600.0) | |
| 190 | 추가: ); | |
| 191 | 추가: // 최소 높이 아래로는 줄지 않는다. | |
| 192 | 추가: assert_eq!( | |
| 193 | 추가: snap_viewport(prev, vec2(360.0, 100.0), aspect), | |
| 194 | 추가: vec2((MIN_VIEWPORT_H * aspect).round(), MIN_VIEWPORT_H) | |
| 195 | 추가: ); | |
| 196 | 추가: assert_eq!(viewport_of(window_size(prev)), prev); | |
| 197 | 추가: } | |
| 198 | 추가: | |
| 97 | 199 | #[test] |
| 98 | 200 | fn rail_slots_stay_inside_rail() { |
| 99 | 201 | let layout = compute(Rect::from_min_size( |
| @@ -7,8 +7,10 @@ | ||
| 7 | 7 | mod conn; |
| 8 | 8 | mod fonts; |
| 9 | 9 | mod icons; |
| 10 | 추가: mod input; | |
| 10 | 11 | #[cfg(test)] |
| 11 | 12 | mod interaction_tests; |
| 13 | 추가: mod keymap; | |
| 12 | 14 | mod layout; |
| 13 | 15 | mod recent; |
| 14 | 16 | #[cfg(test)] |
| @@ -36,6 +38,33 @@ use winit::window::{Window, WindowAttributes, WindowId}; | ||
| 36 | 38 | use conn::{ConnCommand, ConnHandle, Connector, video_request}; |
| 37 | 39 | use view::{Actions, View}; |
| 38 | 40 | |
| 41 | 추가: /// 창 크기 조절이 멈추고 이만큼 지나면 휴대폰 화면 비율에 맞춘다. | |
| 42 | 추가: const SNAP_DELAY: Duration = Duration::from_millis(250); | |
| 43 | 추가: | |
| 44 | 추가: fn min_window_size() -> LogicalSize<f32> { | |
| 45 | 추가: let aspect = layout::DEFAULT_VIEWPORT.x / layout::DEFAULT_VIEWPORT.y; | |
| 46 | 추가: let w = layout::window_size(egui::vec2( | |
| 47 | 추가: layout::MIN_VIEWPORT_H * aspect, | |
| 48 | 추가: layout::MIN_VIEWPORT_H, | |
| 49 | 추가: )); | |
| 50 | 추가: LogicalSize::new(w.x, w.y) | |
| 51 | 추가: } | |
| 52 | 추가: | |
| 53 | 추가: fn resize_direction(dir: layout::ResizeDir) -> winit::window::ResizeDirection { | |
| 54 | 추가: use layout::ResizeDir as D; | |
| 55 | 추가: use winit::window::ResizeDirection as W; | |
| 56 | 추가: match dir { | |
| 57 | 추가: D::North => W::North, | |
| 58 | 추가: D::South => W::South, | |
| 59 | 추가: D::East => W::East, | |
| 60 | 추가: D::West => W::West, | |
| 61 | 추가: D::NorthEast => W::NorthEast, | |
| 62 | 추가: D::NorthWest => W::NorthWest, | |
| 63 | 추가: D::SouthEast => W::SouthEast, | |
| 64 | 추가: D::SouthWest => W::SouthWest, | |
| 65 | 추가: } | |
| 66 | 추가: } | |
| 67 | 추가: | |
| 39 | 68 | /// 네트워크 스레드가 UI를 깨울 때 보내는 이벤트. |
| 40 | 69 | #[derive(Debug, Clone, Copy)] |
| 41 | 70 | struct Wake; |
| @@ -160,6 +189,11 @@ struct App { | ||
| 160 | 189 | conn: Option<ConnHandle>, |
| 161 | 190 | /// 시작 인자로 받은 접속 주소. 창이 뜬 뒤 한 번 연결한다. |
| 162 | 191 | autoconnect: Option<String>, |
| 192 | 추가: /// 마지막으로 맞춘 휴대폰 화면 크기와 비율. 창 크기 조절 후 비율 맞추기에 쓴다. | |
| 193 | 추가: snapped_viewport: egui::Vec2, | |
| 194 | 추가: snapped_aspect: f32, | |
| 195 | 추가: /// 메인 창 크기가 마지막으로 바뀐 시각. 잠시 멈추면 비율에 맞춘다. | |
| 196 | 추가: main_resized_at: Option<Instant>, | |
| 163 | 197 | } |
| 164 | 198 | |
| 165 | 199 | impl App { |
| @@ -171,6 +205,9 @@ impl App { | ||
| 171 | 205 | connector, |
| 172 | 206 | conn: None, |
| 173 | 207 | autoconnect: None, |
| 208 | 추가: snapped_viewport: layout::DEFAULT_VIEWPORT, | |
| 209 | 추가: snapped_aspect: layout::DEFAULT_VIEWPORT.x / layout::DEFAULT_VIEWPORT.y, | |
| 210 | 추가: main_resized_at: None, | |
| 174 | 211 | } |
| 175 | 212 | } |
| 176 | 213 | |
| @@ -179,8 +216,9 @@ impl App { | ||
| 179 | 216 | let attrs = Window::default_attributes() |
| 180 | 217 | .with_title("Android Remote") |
| 181 | 218 | .with_decorations(false) |
| 182 | 삭제: .with_resizable(false) | |
| 183 | 삭제: .with_inner_size(LogicalSize::new(size.x, size.y)); | |
| 219 | 추가: .with_resizable(true) | |
| 220 | 추가: .with_inner_size(LogicalSize::new(size.x, size.y)) | |
| 221 | 추가: .with_min_inner_size(min_window_size()); | |
| 184 | 222 | let mut setup = WgpuSetupCreateNew::from_display_handle(event_loop.owned_display_handle()); |
| 185 | 223 | setup.power_preference = egui_wgpu::wgpu::PowerPreference::HighPerformance; |
| 186 | 224 | let gfx = Gfx::new(event_loop, attrs, WgpuSetup::CreateNew(setup))?; |
| @@ -239,6 +277,10 @@ impl App { | ||
| 239 | 277 | // 연결 상태 이벤트를 먼저 반영해야 첫 프레임이 버려지지 않는다. |
| 240 | 278 | if let Some(frame) = conn.frames.take() { |
| 241 | 279 | self.view.set_video_frame(frame); |
| 280 | 추가: // 영상 비율이 바뀌면(해상도 변경 등) 창도 다시 맞춘다. | |
| 281 | 추가: if (self.view.video_aspect() - self.snapped_aspect).abs() > 0.005 { | |
| 282 | 추가: self.main_resized_at = Some(Instant::now()); | |
| 283 | 추가: } | |
| 242 | 284 | } |
| 243 | 285 | } |
| 244 | 286 | |
| @@ -268,7 +310,11 @@ impl App { | ||
| 268 | 310 | } |
| 269 | 311 | if let Some(main) = &self.main { |
| 270 | 312 | let window = &main.window; |
| 271 | 삭제: if actions.start_drag | |
| 313 | 추가: if let Some(dir) = actions.start_resize { | |
| 314 | 추가: if let Err(e) = window.drag_resize_window(resize_direction(dir)) { | |
| 315 | 추가: log::debug!("창 크기 조절 실패: {e}"); | |
| 316 | 추가: } | |
| 317 | 추가: } else if actions.start_drag | |
| 272 | 318 | && let Err(e) = window.drag_window() |
| 273 | 319 | { |
| 274 | 320 | log::debug!("창 이동 실패: {e}"); |
| @@ -298,6 +344,9 @@ impl App { | ||
| 298 | 344 | if let Some(action) = actions.system_action { |
| 299 | 345 | conn.send(ConnCommand::System(action)); |
| 300 | 346 | } |
| 347 | 추가: for msg in actions.input { | |
| 348 | 추가: conn.send(ConnCommand::Input(msg)); | |
| 349 | 추가: } | |
| 301 | 350 | if actions.video_settings_changed { |
| 302 | 351 | let s = self.view.settings(); |
| 303 | 352 | conn.send(ConnCommand::RequestVideo(video_request( |
| @@ -309,6 +358,53 @@ impl App { | ||
| 309 | 358 | } |
| 310 | 359 | } |
| 311 | 360 | |
| 361 | 추가: /// 창 크기가 바뀔 때마다(가장자리를 끄는 중에도) 휴대폰 화면 비율에 맞는 크기로 바로 고친다. | |
| 362 | 추가: /// | |
| 363 | 추가: /// Wayland에서는 컴포지터가 제안한 크기 대신 앱이 다른 크기로 커밋할 수 있고, 컴포지터는 | |
| 364 | 추가: /// 끌고 있는 반대쪽 가장자리를 고정한 채 그 크기를 쓴다. X11에서는 창 관리자가 다시 맞춘다. | |
| 365 | 추가: fn keep_main_aspect(&mut self) { | |
| 366 | 추가: let Some(main) = self.main.as_mut() else { | |
| 367 | 추가: return; | |
| 368 | 추가: }; | |
| 369 | 추가: let window = main.window.clone(); | |
| 370 | 추가: let scale = window.scale_factor(); | |
| 371 | 추가: let size = window.inner_size().to_logical::<f32>(scale); | |
| 372 | 추가: let current = layout::viewport_of(egui::vec2(size.width, size.height)); | |
| 373 | 추가: let aspect = self.view.video_aspect(); | |
| 374 | 추가: let target = layout::snap_viewport(self.snapped_viewport, current, aspect); | |
| 375 | 추가: self.snapped_viewport = target; | |
| 376 | 추가: self.snapped_aspect = aspect; | |
| 377 | 추가: if (target - current).length() <= 1.5 { | |
| 378 | 추가: return; | |
| 379 | 추가: } | |
| 380 | 추가: let w = layout::window_size(target); | |
| 381 | 추가: if let Some(applied) = window.request_inner_size(LogicalSize::new(w.x, w.y)) | |
| 382 | 추가: && let (Some(pw), Some(ph)) = ( | |
| 383 | 추가: NonZeroU32::new(applied.width), | |
| 384 | 추가: NonZeroU32::new(applied.height), | |
| 385 | 추가: ) | |
| 386 | 추가: { | |
| 387 | 추가: main.painter.on_window_resized(ViewportId::ROOT, pw, ph); | |
| 388 | 추가: } | |
| 389 | 추가: window.request_redraw(); | |
| 390 | 추가: } | |
| 391 | 추가: | |
| 392 | 추가: /// 사용자가 창 크기를 바꾼 뒤 휴대폰 화면 비율에 맞게 창 크기를 다시 정한다. | |
| 393 | 추가: fn snap_main_window(&mut self) { | |
| 394 | 추가: let Some(main) = &self.main else { return }; | |
| 395 | 추가: let window = &main.window; | |
| 396 | 추가: let size = window.inner_size().to_logical::<f32>(window.scale_factor()); | |
| 397 | 추가: let current = layout::viewport_of(egui::vec2(size.width, size.height)); | |
| 398 | 추가: let aspect = self.view.video_aspect(); | |
| 399 | 추가: let target = layout::snap_viewport(self.snapped_viewport, current, aspect); | |
| 400 | 추가: self.snapped_viewport = target; | |
| 401 | 추가: self.snapped_aspect = aspect; | |
| 402 | 추가: if (target - current).length() > 1.5 { | |
| 403 | 추가: let w = layout::window_size(target); | |
| 404 | 추가: let _ = window.request_inner_size(LogicalSize::new(w.x, w.y)); | |
| 405 | 추가: } | |
| 406 | 추가: } | |
| 407 | 추가: | |
| 312 | 408 | fn request_redraw_all(&self) { |
| 313 | 409 | for gfx in [&self.main, &self.settings].into_iter().flatten() { |
| 314 | 410 | gfx.window.request_redraw(); |
| @@ -344,6 +440,7 @@ impl ApplicationHandler<Wake> for App { | ||
| 344 | 440 | |
| 345 | 441 | fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) { |
| 346 | 442 | let Some(kind) = self.kind_of(id) else { return }; |
| 443 | 추가: let captures_keyboard = self.view.captures_keyboard(); | |
| 347 | 444 | |
| 348 | 445 | match &event { |
| 349 | 446 | WindowEvent::CloseRequested => { |
| @@ -365,13 +462,27 @@ impl ApplicationHandler<Wake> for App { | ||
| 365 | 462 | Kind::Settings => self.settings.as_mut(), |
| 366 | 463 | }; |
| 367 | 464 | let Some(gfx) = gfx else { return }; |
| 465 | 추가: if kind == Kind::Main { | |
| 466 | 추가: // 연결 중에는 Ctrl+C/V/X를 egui-winit이 복사·붙여넣기 이벤트로 바꾸지 않고 키로 넘기게 한다. | |
| 467 | 추가: // 그래야 클립보드가 비어도 Ctrl+V가 휴대폰에 전달된다. | |
| 468 | 추가: gfx.egui_winit.set_clipboard_shortcuts(!captures_keyboard); | |
| 469 | 추가: } | |
| 368 | 470 | let response = gfx.egui_winit.on_window_event(&gfx.window, &event); |
| 471 | 추가: let repaint = response.repaint; | |
| 369 | 472 | if let WindowEvent::Resized(size) = event |
| 370 | 473 | && let (Some(w), Some(h)) = (NonZeroU32::new(size.width), NonZeroU32::new(size.height)) |
| 371 | 474 | { |
| 372 | 475 | gfx.painter.on_window_resized(ViewportId::ROOT, w, h); |
| 476 | 추가: if kind == Kind::Main { | |
| 477 | 추가: self.keep_main_aspect(); | |
| 478 | 추가: self.main_resized_at = Some(Instant::now()); | |
| 479 | 추가: } | |
| 373 | 480 | } |
| 374 | 삭제: if response.repaint { | |
| 481 | 추가: if repaint && let Some(gfx) = self.main.as_ref().filter(|_| kind == Kind::Main) { | |
| 482 | 추가: gfx.window.request_redraw(); | |
| 483 | 추가: } else if repaint | |
| 484 | 추가: && let Some(gfx) = self.settings.as_ref().filter(|_| kind == Kind::Settings) | |
| 485 | 추가: { | |
| 375 | 486 | gfx.window.request_redraw(); |
| 376 | 487 | } |
| 377 | 488 | } |
| @@ -379,6 +490,15 @@ impl ApplicationHandler<Wake> for App { | ||
| 379 | 490 | fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) { |
| 380 | 491 | let now = Instant::now(); |
| 381 | 492 | let mut next: Option<Instant> = None; |
| 493 | 추가: if let Some(at) = self.main_resized_at { | |
| 494 | 추가: let due = at + SNAP_DELAY; | |
| 495 | 추가: if due <= now { | |
| 496 | 추가: self.main_resized_at = None; | |
| 497 | 추가: self.snap_main_window(); | |
| 498 | 추가: } else { | |
| 499 | 추가: next = Some(due); | |
| 500 | 추가: } | |
| 501 | 추가: } | |
| 382 | 502 | for gfx in [&mut self.main, &mut self.settings].into_iter().flatten() { |
| 383 | 503 | match gfx.next_repaint { |
| 384 | 504 | Some(at) if at <= now => { |
| @@ -7,10 +7,11 @@ use egui::{ | ||
| 7 | 7 | Align2, Button, Color32, FontId, Key, Rect, RichText, Sense, TextEdit, Ui, Vec2, pos2, vec2, |
| 8 | 8 | }; |
| 9 | 9 | |
| 10 | 삭제: use wayland_android_remote::protocol::{AuthGrant, SystemAction, VideoCodec, VideoConfig}; | |
| 10 | 추가: use wayland_android_remote::protocol::{AuthGrant, Message, SystemAction, VideoCodec, VideoConfig}; | |
| 11 | 11 | |
| 12 | 12 | use super::conn::ConnEvent; |
| 13 | 13 | use super::icons::{self, Icon}; |
| 14 | 추가: use super::input::{InputState, VideoTarget}; | |
| 14 | 15 | use super::layout::{self, DEFAULT_VIEWPORT}; |
| 15 | 16 | use super::recent::{RecentTargets, display_target, parse_target}; |
| 16 | 17 | use super::video_render::{VideoCallback, YuvFrame}; |
| @@ -28,6 +29,8 @@ const ERROR_TEXT: Color32 = Color32::from_rgb(0xe0, 0x6c, 0x6c); | ||
| 28 | 29 | #[derive(Debug, Default)] |
| 29 | 30 | pub struct Actions { |
| 30 | 31 | pub start_drag: bool, |
| 32 | 추가: /// 창 가장자리를 눌렀다. 이 방향으로 창 크기 조절을 시작한다. | |
| 33 | 추가: pub start_resize: Option<layout::ResizeDir>, | |
| 31 | 34 | pub minimize: bool, |
| 32 | 35 | pub close: bool, |
| 33 | 36 | /// 설정 창을 열거나 앞으로 가져온다. |
| @@ -41,6 +44,8 @@ pub struct Actions { | ||
| 41 | 44 | pub system_action: Option<SystemAction>, |
| 42 | 45 | /// 설정 창에서 영상 설정이 바뀌었다. |
| 43 | 46 | pub video_settings_changed: bool, |
| 47 | 추가: /// 휴대폰으로 보낼 입력 메시지. 만든 순서대로 보낸다. | |
| 48 | 추가: pub input: Vec<Message>, | |
| 44 | 49 | } |
| 45 | 50 | |
| 46 | 51 | #[derive(Debug, Clone)] |
| @@ -97,6 +102,7 @@ pub struct View { | ||
| 97 | 102 | /// 최신 영상 프레임과 번호. 있으면 휴대폰 화면 영역에 영상을 그린다. |
| 98 | 103 | video_frame: Option<(u64, std::sync::Arc<YuvFrame>)>, |
| 99 | 104 | video_seq: u64, |
| 105 | 추가: input: InputState, | |
| 100 | 106 | } |
| 101 | 107 | |
| 102 | 108 | impl View { |
| @@ -112,6 +118,7 @@ impl View { | ||
| 112 | 118 | settings: StreamSettings::default(), |
| 113 | 119 | video_frame: None, |
| 114 | 120 | video_seq: 0, |
| 121 | 추가: input: InputState::default(), | |
| 115 | 122 | } |
| 116 | 123 | } |
| 117 | 124 | |
| @@ -179,6 +186,35 @@ impl View { | ||
| 179 | 186 | } |
| 180 | 187 | } |
| 181 | 188 | |
| 189 | 추가: /// 연결되어 메인 창 키보드 입력을 휴대폰으로 보내는 중인지. | |
| 190 | 추가: pub fn captures_keyboard(&self) -> bool { | |
| 191 | 추가: matches!(self.connection, Connection::Connected { .. }) | |
| 192 | 추가: } | |
| 193 | 추가: | |
| 194 | 추가: /// 터치·휠을 받을 영상 영역. 연결되고 영상 설정과 그린 프레임이 있을 때만 있다. | |
| 195 | 추가: fn video_target(&self, viewport: Rect) -> Option<VideoTarget> { | |
| 196 | 추가: let Connection::Connected { | |
| 197 | 추가: video: Some(cfg), .. | |
| 198 | 추가: } = &self.connection | |
| 199 | 추가: else { | |
| 200 | 추가: return None; | |
| 201 | 추가: }; | |
| 202 | 추가: let (_, frame) = self.video_frame.as_ref()?; | |
| 203 | 추가: Some(VideoTarget { | |
| 204 | 추가: rect: video_rect(viewport, frame), | |
| 205 | 추가: width: cfg.width, | |
| 206 | 추가: height: cfg.height, | |
| 207 | 추가: }) | |
| 208 | 추가: } | |
| 209 | 추가: | |
| 210 | 추가: /// 휴대폰 화면 비율(너비/높이). 영상을 받기 전에는 기본 비율을 쓴다. | |
| 211 | 추가: pub fn video_aspect(&self) -> f32 { | |
| 212 | 추가: match &self.video_frame { | |
| 213 | 추가: Some((_, f)) if f.height > 0 => f.width as f32 / f.height as f32, | |
| 214 | 추가: _ => DEFAULT_VIEWPORT.x / DEFAULT_VIEWPORT.y, | |
| 215 | 추가: } | |
| 216 | 추가: } | |
| 217 | 추가: | |
| 182 | 218 | /// 현재 상태에 맞는 창 안쪽 크기 (논리 픽셀). |
| 183 | 219 | pub fn window_size(&self) -> Vec2 { |
| 184 | 220 | layout::window_size(self.viewport_size) |
| @@ -190,6 +226,26 @@ impl View { | ||
| 190 | 226 | |
| 191 | 227 | ui.painter().rect_filled(screen, 0.0, FRAME_COLOR); |
| 192 | 228 | |
| 229 | 추가: let keyboard = self.captures_keyboard(); | |
| 230 | 추가: if keyboard { | |
| 231 | 추가: // 연결 중에는 Tab·화살표가 휴대폰으로 가야 한다. egui는 begin_pass에서 이 키로 | |
| 232 | 추가: // 포커스 이동 방향을 정하고 위젯이 등록될 때 포커스를 옮기므로, 위젯을 그리기 전에 | |
| 233 | 추가: // 방향을 지우고 남은 포커스도 놓아 아이콘 버튼이 Enter·Space로 눌리지 않게 한다. | |
| 234 | 추가: ui.memory_mut(|m| { | |
| 235 | 추가: m.move_focus(egui::FocusDirection::None); | |
| 236 | 추가: if let Some(id) = m.focused() { | |
| 237 | 추가: m.surrender_focus(id); | |
| 238 | 추가: } | |
| 239 | 추가: }); | |
| 240 | 추가: } | |
| 241 | 추가: let phone = self.video_target(layout.viewport); | |
| 242 | 추가: let events = ui.input(|i| i.events.clone()); | |
| 243 | 추가: self.input | |
| 244 | 추가: .process(&events, keyboard, phone, &mut actions.input); | |
| 245 | 추가: if keyboard && ui.input(|i| i.focused) { | |
| 246 | 추가: request_ime(ui, phone.map_or(layout.viewport, |t| t.rect)); | |
| 247 | 추가: } | |
| 248 | 추가: | |
| 193 | 249 | self.icon_rail(ui, layout.rail, actions); |
| 194 | 250 | self.nav_bar(ui, layout.nav_bar, actions); |
| 195 | 251 | self.mouse_back_button(ui, actions); |
| @@ -223,6 +279,14 @@ impl View { | ||
| 223 | 279 | } |
| 224 | 280 | }, |
| 225 | 281 | } |
| 282 | 추가: | |
| 283 | 추가: if keyboard { | |
| 284 | 추가: let rect = phone.map_or(layout.viewport, |t| t.rect); | |
| 285 | 추가: draw_preedit(ui, rect, self.input.preedit()); | |
| 286 | 추가: } | |
| 287 | 추가: | |
| 288 | 추가: // 가장자리 크기 조절은 다른 위젯보다 우선한다. 휴대폰 화면과는 겹치지 않는다. | |
| 289 | 추가: resize_handles(ui, screen, actions); | |
| 226 | 290 | } |
| 227 | 291 | |
| 228 | 292 | /// 왼쪽 세로 아이콘 열. 배경과 테두리 없이 아이콘만 그린다. |
| @@ -523,13 +587,54 @@ fn connected_screen( | ||
| 523 | 587 | |
| 524 | 588 | /// 영상을 휴대폰 화면 영역에 종횡비를 유지해 맞춰 그린다. |
| 525 | 589 | fn draw_video(ui: &mut Ui, viewport: Rect, seq: u64, frame: std::sync::Arc<YuvFrame>) { |
| 526 | 삭제: let rect = fit_rect(viewport, frame.width as f32 / frame.height.max(1) as f32); | |
| 590 | 추가: let rect = video_rect(viewport, &frame); | |
| 527 | 591 | ui.painter().add(egui_wgpu::Callback::new_paint_callback( |
| 528 | 592 | rect, |
| 529 | 593 | VideoCallback { frame, seq }, |
| 530 | 594 | )); |
| 531 | 595 | } |
| 532 | 596 | |
| 597 | 추가: /// 프레임이 그려지는 사각형. 입력 좌표 변환도 같은 사각형을 쓴다. | |
| 598 | 추가: fn video_rect(viewport: Rect, frame: &YuvFrame) -> Rect { | |
| 599 | 추가: fit_rect(viewport, frame.width as f32 / frame.height.max(1) as f32) | |
| 600 | 추가: } | |
| 601 | 추가: | |
| 602 | 추가: /// IME 조합 글자를 표시할 위치 (영상 아래쪽 가운데). | |
| 603 | 추가: fn preedit_anchor(rect: Rect) -> egui::Pos2 { | |
| 604 | 추가: pos2(rect.center().x, rect.max.y - 28.0) | |
| 605 | 추가: } | |
| 606 | 추가: | |
| 607 | 추가: /// 휴대폰 화면에 키보드 입력 중일 때 winit에 IME를 켜 달라고 매 프레임 요청한다. | |
| 608 | 추가: /// IME 후보 창은 조합 글자 표시 위치 근처에 뜨게 한다. | |
| 609 | 추가: fn request_ime(ui: &mut Ui, rect: Rect) { | |
| 610 | 추가: let anchor = preedit_anchor(rect); | |
| 611 | 추가: let cursor_rect = Rect::from_min_size(anchor - vec2(0.0, 10.0), vec2(1.0, 20.0)); | |
| 612 | 추가: ui.output_mut(|o| { | |
| 613 | 추가: o.ime = Some(egui::output::IMEOutput { | |
| 614 | 추가: purpose: egui::IMEPurpose::Normal, | |
| 615 | 추가: rect, | |
| 616 | 추가: cursor_rect, | |
| 617 | 추가: should_interrupt_composition: false, | |
| 618 | 추가: }); | |
| 619 | 추가: }); | |
| 620 | 추가: } | |
| 621 | 추가: | |
| 622 | 추가: /// IME로 조합 중인 글자를 영상 아래쪽에 작게 띄운다. 확정된 글자만 휴대폰으로 보낸다. | |
| 623 | 추가: fn draw_preedit(ui: &mut Ui, rect: Rect, preedit: &str) { | |
| 624 | 추가: if preedit.is_empty() { | |
| 625 | 추가: return; | |
| 626 | 추가: } | |
| 627 | 추가: let painter = ui.painter(); | |
| 628 | 추가: let galley = painter.layout_no_wrap( | |
| 629 | 추가: preedit.to_owned(), | |
| 630 | 추가: FontId::proportional(18.0), | |
| 631 | 추가: Color32::WHITE, | |
| 632 | 추가: ); | |
| 633 | 추가: let bg = Rect::from_center_size(preedit_anchor(rect), galley.size() + vec2(16.0, 8.0)); | |
| 634 | 추가: painter.rect_filled(bg, 6.0, Color32::from_black_alpha(200)); | |
| 635 | 추가: painter.galley(bg.min + vec2(8.0, 4.0), galley, Color32::WHITE); | |
| 636 | 추가: } | |
| 637 | 추가: | |
| 533 | 638 | /// `outer` 안에 종횡비 `aspect`(너비/높이)를 유지하는 가장 큰 사각형을 가운데 정렬로 구한다. |
| 534 | 639 | fn fit_rect(outer: Rect, aspect: f32) -> Rect { |
| 535 | 640 | let outer_aspect = outer.width() / outer.height(); |
| @@ -558,6 +663,30 @@ fn spaced(code: &str) -> String { | ||
| 558 | 663 | } |
| 559 | 664 | } |
| 560 | 665 | |
| 666 | 추가: /// 창 가장자리에 마우스를 올리면 크기 조절 커서를 보이고, 누르면 크기 조절을 시작한다. | |
| 667 | 추가: fn resize_handles(ui: &mut Ui, screen: Rect, actions: &mut Actions) { | |
| 668 | 추가: use layout::ResizeDir as D; | |
| 669 | 추가: let Some(pos) = ui.input(|i| i.pointer.hover_pos()) else { | |
| 670 | 추가: return; | |
| 671 | 추가: }; | |
| 672 | 추가: let Some(dir) = layout::resize_dir(screen, pos) else { | |
| 673 | 추가: return; | |
| 674 | 추가: }; | |
| 675 | 추가: ui.ctx().set_cursor_icon(match dir { | |
| 676 | 추가: D::North => egui::CursorIcon::ResizeNorth, | |
| 677 | 추가: D::South => egui::CursorIcon::ResizeSouth, | |
| 678 | 추가: D::East => egui::CursorIcon::ResizeEast, | |
| 679 | 추가: D::West => egui::CursorIcon::ResizeWest, | |
| 680 | 추가: D::NorthEast => egui::CursorIcon::ResizeNorthEast, | |
| 681 | 추가: D::NorthWest => egui::CursorIcon::ResizeNorthWest, | |
| 682 | 추가: D::SouthEast => egui::CursorIcon::ResizeSouthEast, | |
| 683 | 추가: D::SouthWest => egui::CursorIcon::ResizeSouthWest, | |
| 684 | 추가: }); | |
| 685 | 추가: if ui.input(|i| i.pointer.primary_pressed()) { | |
| 686 | 추가: actions.start_resize = Some(dir); | |
| 687 | 추가: } | |
| 688 | 추가: } | |
| 689 | 추가: | |
| 561 | 690 | /// 빈 공간을 누른 채 움직이면 창을 옮긴다. |
| 562 | 691 | fn drag_area(ui: &mut Ui, rect: Rect, id: &str, actions: &mut Actions) { |
| 563 | 692 | if ui |
| @@ -5,10 +5,14 @@ | ||
| 5 | 5 | //! - 재정렬 지연이 없도록 `AV_CODEC_FLAG_LOW_DELAY`를 켜고 프레임 스레딩은 쓰지 않는다. |
| 6 | 6 | //! - 하드웨어(CUDA → VA-API) 디코딩이 실패하면 소프트웨어로 한 번 전환한다. |
| 7 | 7 | |
| 8 | 추가: use std::cell::Cell; | |
| 8 | 9 | use std::error::Error; |
| 9 | 10 | use std::ffi::CString; |
| 10 | 11 | use std::fmt; |
| 12 | 추가: use std::mem::ManuallyDrop; | |
| 13 | 추가: use std::ops::{Deref, DerefMut}; | |
| 11 | 14 | use std::ptr; |
| 15 | 추가: use std::sync::{Mutex, MutexGuard}; | |
| 12 | 16 | |
| 13 | 17 | use ff::codec::{self, threading}; |
| 14 | 18 | use ff::color; |
| @@ -157,12 +161,74 @@ impl fmt::Display for DecodeError { | ||
| 157 | 161 | |
| 158 | 162 | impl Error for DecodeError {} |
| 159 | 163 | |
| 164 | 추가: /// 하드웨어 디코더(CUDA 등) 생성·디코딩·해제를 프로세스 전체에서 한 번에 하나로 묶는 잠금. | |
| 165 | 추가: /// | |
| 166 | 추가: /// 여러 스레드가 동시에 NVDEC 디코더를 만들거나 해제하면 드라이버 안에서 멈추는 일이 있었다 | |
| 167 | 추가: /// (병렬 테스트에서 재현). 앱은 디코더를 하나만 쓰므로 비용은 없고, 빠르게 다시 연결할 때 | |
| 168 | 추가: /// 이전 디코더 해제와 새 디코더 생성이 겹치는 경우도 막는다. 같은 스레드 안에서는 다시 | |
| 169 | 추가: /// 잡을 수 있다(디코딩 중 컨텍스트 재생성). | |
| 170 | 추가: static HW_LOCK: Mutex<()> = Mutex::new(()); | |
| 171 | 추가: | |
| 172 | 추가: thread_local! { | |
| 173 | 추가: static HW_DEPTH: Cell<u32> = const { Cell::new(0) }; | |
| 174 | 추가: } | |
| 175 | 추가: | |
| 176 | 추가: struct HwGuard { | |
| 177 | 추가: _lock: Option<MutexGuard<'static, ()>>, | |
| 178 | 추가: } | |
| 179 | 추가: | |
| 180 | 추가: impl HwGuard { | |
| 181 | 추가: fn acquire() -> Self { | |
| 182 | 추가: let depth = HW_DEPTH.get(); | |
| 183 | 추가: let guard = (depth == 0).then(|| HW_LOCK.lock().unwrap_or_else(|e| e.into_inner())); | |
| 184 | 추가: HW_DEPTH.set(depth + 1); | |
| 185 | 추가: Self { _lock: guard } | |
| 186 | 추가: } | |
| 187 | 추가: } | |
| 188 | 추가: | |
| 189 | 추가: impl Drop for HwGuard { | |
| 190 | 추가: fn drop(&mut self) { | |
| 191 | 추가: HW_DEPTH.set(HW_DEPTH.get() - 1); | |
| 192 | 추가: // 잠금은 필드(MutexGuard)가 이 다음에 해제될 때 풀린다. | |
| 193 | 추가: } | |
| 194 | 추가: } | |
| 195 | 추가: | |
| 196 | 추가: /// 잠금을 잡은 채 해제되는 코덱 컨텍스트. | |
| 197 | 추가: struct Ctx(ManuallyDrop<ff::decoder::Video>); | |
| 198 | 추가: | |
| 199 | 추가: impl Ctx { | |
| 200 | 추가: fn new(dec: ff::decoder::Video) -> Self { | |
| 201 | 추가: Self(ManuallyDrop::new(dec)) | |
| 202 | 추가: } | |
| 203 | 추가: } | |
| 204 | 추가: | |
| 205 | 추가: impl Deref for Ctx { | |
| 206 | 추가: type Target = ff::decoder::Video; | |
| 207 | 추가: fn deref(&self) -> &Self::Target { | |
| 208 | 추가: &self.0 | |
| 209 | 추가: } | |
| 210 | 추가: } | |
| 211 | 추가: | |
| 212 | 추가: impl DerefMut for Ctx { | |
| 213 | 추가: fn deref_mut(&mut self) -> &mut Self::Target { | |
| 214 | 추가: &mut self.0 | |
| 215 | 추가: } | |
| 216 | 추가: } | |
| 217 | 추가: | |
| 218 | 추가: impl Drop for Ctx { | |
| 219 | 추가: fn drop(&mut self) { | |
| 220 | 추가: let _hw = HwGuard::acquire(); | |
| 221 | 추가: // SAFETY: 이 Drop에서 한 번만 해제하고 이후 접근하지 않는다. | |
| 222 | 추가: unsafe { ManuallyDrop::drop(&mut self.0) }; | |
| 223 | 추가: } | |
| 224 | 추가: } | |
| 225 | 추가: | |
| 160 | 226 | /// 저지연 디코더. 한 비디오 채널에 하나. |
| 161 | 227 | pub struct Decoder { |
| 162 | 228 | codec: VideoCodec, |
| 163 | 229 | /// None이면 소프트웨어 디코딩. |
| 164 | 230 | device: Option<HwDevice>, |
| 165 | 삭제: dec: ff::decoder::Video, | |
| 231 | 추가: dec: Ctx, | |
| 166 | 232 | /// 현재 컨텍스트에 패킷을 넣은 적이 있는지. |
| 167 | 233 | fed: bool, |
| 168 | 234 | /// 현재 컨텍스트가 프레임을 한 장이라도 냈는지. hw 실패 판단에 쓴다. |
| @@ -189,6 +255,7 @@ impl Decoder { | ||
| 189 | 255 | |
| 190 | 256 | /// `order` 순서대로 처음 열리는 백엔드를 쓴다. |
| 191 | 257 | pub fn with_backends(codec: VideoCodec, order: &[Backend]) -> Result<Self, DecodeError> { |
| 258 | 추가: let _hw = HwGuard::acquire(); | |
| 192 | 259 | let mut last = String::from("백엔드 목록이 비어 있음"); |
| 193 | 260 | for &backend in order { |
| 194 | 261 | match Self::open_backend(codec, backend) { |
| @@ -210,7 +277,7 @@ impl Decoder { | ||
| 210 | 277 | None => None, |
| 211 | 278 | Some(_) => Some(HwDevice::create(codec, backend)?), |
| 212 | 279 | }; |
| 213 | 삭제: let dec = open_context(codec, device.as_ref())?; | |
| 280 | 추가: let dec = Ctx::new(open_context(codec, device.as_ref())?); | |
| 214 | 281 | Ok(Self { |
| 215 | 282 | codec, |
| 216 | 283 | device, |
| @@ -244,6 +311,7 @@ impl Decoder { | ||
| 244 | 311 | header: &VideoPacketHeader, |
| 245 | 312 | data: &[u8], |
| 246 | 313 | ) -> Result<Vec<DecodedFrame>, DecodeError> { |
| 314 | 추가: let _hw = HwGuard::acquire(); | |
| 247 | 315 | if header.is_config { |
| 248 | 316 | return self.handle_config(header.config_gen, data); |
| 249 | 317 | } |
| @@ -355,7 +423,7 @@ impl Decoder { | ||
| 355 | 423 | fn reopen(&mut self) -> Result<(), DecodeError> { |
| 356 | 424 | match open_context(self.codec, self.device.as_ref()) { |
| 357 | 425 | Ok(dec) => { |
| 358 | 삭제: self.dec = dec; | |
| 426 | 추가: self.dec = Ctx::new(dec); | |
| 359 | 427 | self.fed = false; |
| 360 | 428 | self.produced = false; |
| 361 | 429 | Ok(()) |
| @@ -372,7 +440,7 @@ impl Decoder { | ||
| 372 | 440 | } |
| 373 | 441 | |
| 374 | 442 | fn switch_to_software(&mut self) -> Result<(), DecodeError> { |
| 375 | 삭제: self.dec = open_context(self.codec, None).map_err(DecodeError::Init)?; | |
| 443 | 추가: self.dec = Ctx::new(open_context(self.codec, None).map_err(DecodeError::Init)?); | |
| 376 | 444 | self.device = None; |
| 377 | 445 | self.fed = false; |
| 378 | 446 | self.produced = false; |
| @@ -589,6 +657,7 @@ fn render_nodes() -> Vec<CString> { | ||
| 589 | 657 | |
| 590 | 658 | impl Drop for HwDevice { |
| 591 | 659 | fn drop(&mut self) { |
| 660 | 추가: let _hw = HwGuard::acquire(); | |
| 592 | 661 | // SAFETY: create에서 얻은 참조를 한 번만 해제한다. |
| 593 | 662 | unsafe { sys::av_buffer_unref(&mut self.buf) }; |
| 594 | 663 | } |