blob: 3f9bf62877f32b2021089dd823b15e3988eb9504 [file] [log] [blame]
Steve Kondik95027ea2017-06-14 17:22:58 -07001//
2// vncflinger - Copyright (C) 2017 Steve Kondik
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16//
17
Steve Kondik55db0532017-06-12 11:27:18 -070018#ifndef VNCFLINGER_H
19#define VNCFLINGER_H
20
Steve Kondik77754522017-06-14 17:00:33 -070021#include <gui/CpuConsumer.h>
Steve Kondik55db0532017-06-12 11:27:18 -070022#include <ui/DisplayInfo.h>
Steve Kondik5c8655c2017-06-19 00:28:47 -070023#include <utils/String8.h>
Steve Kondik55db0532017-06-12 11:27:18 -070024
Steve Kondik5f7b0a82017-06-16 09:04:43 -070025#include <rfb/rfb.h>
Steve Kondik3db07472017-06-19 22:13:45 -070026#undef max
Steve Kondik55db0532017-06-12 11:27:18 -070027
Steve Kondik5c8655c2017-06-19 00:28:47 -070028#define VNC_AUTH_FILE "/data/system/vncauth"
Steve Kondik5f7b0a82017-06-16 09:04:43 -070029#define NUM_BUFS 1
Steve Kondik55db0532017-06-12 11:27:18 -070030
31namespace android {
32
Steve Kondik5c8655c2017-06-19 00:28:47 -070033class VNCFlinger : public RefBase {
Steve Kondik2c9d0cf2017-06-15 23:39:29 -070034 public:
Steve Kondik5c8655c2017-06-19 00:28:47 -070035 VNCFlinger();
Steve Kondik55db0532017-06-12 11:27:18 -070036
Steve Kondik2c9d0cf2017-06-15 23:39:29 -070037 virtual ~VNCFlinger() {
38 }
Steve Kondik55db0532017-06-12 11:27:18 -070039
40 virtual status_t start();
41 virtual status_t stop();
42
Steve Kondik7225c7f2017-06-14 00:06:16 -070043 virtual size_t addClient();
44 virtual size_t removeClient();
Steve Kondik55db0532017-06-12 11:27:18 -070045
Steve Kondik5c8655c2017-06-19 00:28:47 -070046 virtual status_t setPort(unsigned int port);
Steve Kondik3db07472017-06-19 22:13:45 -070047 virtual status_t setV4Address(const String8& address);
48 virtual status_t setV6Address(const String8& address);
Steve Kondik59104ea2017-06-20 08:30:07 -070049 virtual status_t setScale(float scale);
Steve Kondik5c8655c2017-06-19 00:28:47 -070050
51 virtual status_t clearPassword();
Steve Kondik3db07472017-06-19 22:13:45 -070052 virtual status_t setPassword(const String8& passwd);
Steve Kondik5c8655c2017-06-19 00:28:47 -070053
Steve Kondik2c9d0cf2017-06-15 23:39:29 -070054 private:
Steve Kondik77754522017-06-14 17:00:33 -070055 class FrameListener : public CpuConsumer::FrameAvailableListener {
Steve Kondik2c9d0cf2017-06-15 23:39:29 -070056 public:
57 FrameListener(VNCFlinger* vnc) : mVNC(vnc) {
58 }
Steve Kondik55db0532017-06-12 11:27:18 -070059
Steve Kondik77754522017-06-14 17:00:33 -070060 virtual void onFrameAvailable(const BufferItem& item);
61
Steve Kondik2c9d0cf2017-06-15 23:39:29 -070062 private:
63 FrameListener(FrameListener&) {
64 }
65 VNCFlinger* mVNC;
Steve Kondik77754522017-06-14 17:00:33 -070066 };
67
68 virtual void eventLoop();
69
70 virtual status_t createVirtualDisplay();
Steve Kondik6ec5bc82017-06-15 01:31:51 -070071 virtual status_t destroyVirtualDisplayLocked();
Steve Kondik5c8655c2017-06-19 00:28:47 -070072
Steve Kondik77754522017-06-14 17:00:33 -070073 virtual status_t createVNCServer();
Steve Kondik5c8655c2017-06-19 00:28:47 -070074 virtual status_t startVNCServer();
Steve Kondik77754522017-06-14 17:00:33 -070075
76 virtual void processFrame();
77
Steve Kondik6ec5bc82017-06-15 01:31:51 -070078 virtual bool isDeviceRotated(int orientation);
79 virtual bool updateDisplayProjection();
Steve Kondik5f7b0a82017-06-16 09:04:43 -070080 virtual bool updateFBSize(CpuConsumer::LockedBuffer& buf);
Steve Kondik6ec5bc82017-06-15 01:31:51 -070081
Steve Kondik77754522017-06-14 17:00:33 -070082 // vncserver callbacks
Steve Kondik55db0532017-06-12 11:27:18 -070083 static ClientGoneHookPtr onClientGone(rfbClientPtr cl);
84 static enum rfbNewClientAction onNewClient(rfbClientPtr cl);
Steve Kondikef4e8652017-06-14 15:07:54 -070085 static void onFrameStart(rfbClientPtr cl);
86 static void onFrameDone(rfbClientPtr cl, int result);
Steve Kondik2c9d0cf2017-06-15 23:39:29 -070087 static void rfbLogger(const char* format, ...);
88
Steve Kondik77754522017-06-14 17:00:33 -070089 bool mRunning;
90 bool mFrameAvailable;
Steve Kondik6ec5bc82017-06-15 01:31:51 -070091 bool mRotate;
92 bool mVDSActive;
Steve Kondik77754522017-06-14 17:00:33 -070093
94 Mutex mEventMutex;
95 Mutex mUpdateMutex;
96
97 Condition mEventCond;
Steve Kondik7225c7f2017-06-14 00:06:16 -070098
Steve Kondik5f7b0a82017-06-16 09:04:43 -070099 uint32_t mWidth, mHeight;
Steve Kondik59104ea2017-06-20 08:30:07 -0700100 uint32_t mSourceWidth, mSourceHeight;
Steve Kondik5f7b0a82017-06-16 09:04:43 -0700101 int32_t mOrientation;
Steve Kondik59104ea2017-06-20 08:30:07 -0700102 float mScale;
Steve Kondik55db0532017-06-12 11:27:18 -0700103
104 size_t mClientCount;
Steve Kondik77754522017-06-14 17:00:33 -0700105
Steve Kondik5f7b0a82017-06-16 09:04:43 -0700106 // Framebuffers
107 uint64_t mFrameNumber;
108 uint64_t mFrameSize;
109 nsecs_t mFrameStartWhen;
Steve Kondik6ec5bc82017-06-15 01:31:51 -0700110
Steve Kondik2c9d0cf2017-06-15 23:39:29 -0700111 // Server instance
112 rfbScreenInfoPtr mVNCScreen;
113
114 // Primary display
115 sp<IBinder> mMainDpy;
116
117 // Virtual display
118 sp<IBinder> mDpy;
Steve Kondik77754522017-06-14 17:00:33 -0700119
120 // Producer side of queue, passed into the virtual display.
121 sp<IGraphicBufferProducer> mProducer;
122
123 // This receives frames from the virtual display and makes them available
124 sp<CpuConsumer> mCpuConsumer;
125
Steve Kondik2c9d0cf2017-06-15 23:39:29 -0700126 // Consumer callback
127 sp<FrameListener> mListener;
Steve Kondik55db0532017-06-12 11:27:18 -0700128};
Steve Kondik55db0532017-06-12 11:27:18 -0700129};
130#endif