Skip to content

Commit

Permalink
Pause framebuffer updates when app is in background
Browse files Browse the repository at this point in the history
  • Loading branch information
gujjwal00 committed May 17, 2023
1 parent 425e25d commit 505abaa
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
11 changes: 10 additions & 1 deletion app/src/main/cpp/native-vnc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,16 @@ extern "C"
JNIEXPORT jboolean JNICALL
Java_com_gaurav_avnc_vnc_VncClient_nativeRefreshFrameBuffer(JNIEnv *env, jobject thiz, jlong clientPtr) {
auto client = (rfbClient *) clientPtr;
return (jboolean) SendFramebufferUpdateRequest(client, 0, 0, client->width, client->height, FALSE);
return (jboolean) SendFramebufferUpdateRequest(client, 0, 0, client->width, client->height, TRUE);
}

extern "C"
JNIEXPORT void JNICALL
Java_com_gaurav_avnc_vnc_VncClient_nativeSetAutomaticFramebufferUpdates(JNIEnv *env, jobject thiz, jlong client_ptr,
jboolean enabled) {
auto client = ((rfbClient *) client_ptr);
client->automaticUpdateRequests = enabled ? TRUE : FALSE;
if (enabled) SendIncrementalFramebufferUpdateRequest(client);
}

extern "C"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/gaurav/avnc/ui/vnc/VncActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,14 @@ class VncActivity : AppCompatActivity() {
override fun onStart() {
super.onStart()
binding.frameView.onResume()
viewModel.resumeFrameBufferUpdates()
}

override fun onStop() {
super.onStop()
virtualKeys.releaseMetaKeys()
binding.frameView.onPause()
viewModel.pauseFrameBufferUpdates()
}

override fun onSaveInstanceState(outState: Bundle) {
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/com/gaurav/avnc/viewmodel/VncViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,14 @@ class VncViewModel(val profile: ServerProfile, app: Application) : BaseViewModel
}
}

fun pauseFrameBufferUpdates() {
client.setAutomaticFrameBufferUpdates(false)
}

fun resumeFrameBufferUpdates() {
client.setAutomaticFrameBufferUpdates(true)
}

/**************************************************************************
* [VncClient.Observer] Implementation
**************************************************************************/
Expand Down
25 changes: 23 additions & 2 deletions app/src/main/java/com/gaurav/avnc/vnc/VncClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ class VncClient(private val observer: Observer) {
@Volatile
var ignorePointerMovesByServer = false


@Volatile
private var autoFBRequestsQueued = true
private var autoFBRequests = autoFBRequestsQueued

/**
* Setup different properties for this client.
*
Expand Down Expand Up @@ -134,6 +139,11 @@ class VncClient(private val observer: Observer) {
connected = false
throw IOException(nativeGetLastErrorStr())
}

if (autoFBRequests != autoFBRequestsQueued) {
autoFBRequests = autoFBRequestsQueued
nativeSetAutomaticFramebufferUpdates(nativePtr, autoFBRequests)
}
}

/**
Expand Down Expand Up @@ -198,9 +208,19 @@ class VncClient(private val observer: Observer) {
}

/**
* Sends a request for full frame buffer update to remote server.
* Sends frame buffer update request to remote server.
*/
fun refreshFrameBuffer() = nativeRefreshFrameBuffer(nativePtr)
fun refreshFrameBuffer() = ifConnected {
nativeRefreshFrameBuffer(nativePtr)
}

/**
* Controls whether framebuffer update requests are sent automatically.
* It takes effect after the next call to [processServerMessage].
*/
fun setAutomaticFrameBufferUpdates(enabled: Boolean) = ifConnected {
autoFBRequestsQueued = enabled
}

/**
* Puts framebuffer contents in currently active OpenGL texture.
Expand Down Expand Up @@ -242,6 +262,7 @@ class VncClient(private val observer: Observer) {
private external fun nativeSendCutText(clientPtr: Long, bytes: ByteArray, isUTF8: Boolean): Boolean
private external fun nativeSetDesktopSize(clientPtr: Long, width: Int, height: Int): Boolean
private external fun nativeRefreshFrameBuffer(clientPtr: Long): Boolean
private external fun nativeSetAutomaticFramebufferUpdates(clientPtr: Long, enabled: Boolean)
private external fun nativeGetDesktopName(clientPtr: Long): String
private external fun nativeGetWidth(clientPtr: Long): Int
private external fun nativeGetHeight(clientPtr: Long): Int
Expand Down
2 changes: 1 addition & 1 deletion extern/libvncserver

0 comments on commit 505abaa

Please sign in to comment.