blob: 856bbb726c58d03b5634869203d90eb018a6d42c [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 Kondik5c8655c2017-06-19 00:28:47 -070049
50 virtual status_t clearPassword();
Steve Kondik3db07472017-06-19 22:13:45 -070051 virtual status_t setPassword(const String8& passwd);
Steve Kondik5c8655c2017-06-19 00:28:47 -070052
Steve Kondik2c9d0cf2017-06-15 23:39:29 -070053 private:
Steve Kondik77754522017-06-14 17:00:33 -070054 class FrameListener : public CpuConsumer::FrameAvailableListener {
Steve Kondik2c9d0cf2017-06-15 23:39:29 -070055 public:
56 FrameListener(VNCFlinger* vnc) : mVNC(vnc) {
57 }
Steve Kondik55db0532017-06-12 11:27:18 -070058
Steve Kondik77754522017-06-14 17:00:33 -070059 virtual void onFrameAvailable(const BufferItem& item);
60
Steve Kondik2c9d0cf2017-06-15 23:39:29 -070061 private:
62 FrameListener(FrameListener&) {
63 }
64 VNCFlinger* mVNC;
Steve Kondik77754522017-06-14 17:00:33 -070065 };
66
67 virtual void eventLoop();
68
69 virtual status_t createVirtualDisplay();
Steve Kondik6ec5bc82017-06-15 01:31:51 -070070 virtual status_t destroyVirtualDisplayLocked();
Steve Kondik5c8655c2017-06-19 00:28:47 -070071
Steve Kondik77754522017-06-14 17:00:33 -070072 virtual status_t createVNCServer();
Steve Kondik5c8655c2017-06-19 00:28:47 -070073 virtual status_t startVNCServer();
Steve Kondik77754522017-06-14 17:00:33 -070074
75 virtual void processFrame();
76
Steve Kondik6ec5bc82017-06-15 01:31:51 -070077 virtual bool isDeviceRotated(int orientation);
78 virtual bool updateDisplayProjection();
Steve Kondik5f7b0a82017-06-16 09:04:43 -070079 virtual bool updateFBSize(CpuConsumer::LockedBuffer& buf);
Steve Kondik6ec5bc82017-06-15 01:31:51 -070080
Steve Kondik77754522017-06-14 17:00:33 -070081 // vncserver callbacks
Steve Kondik55db0532017-06-12 11:27:18 -070082 static ClientGoneHookPtr onClientGone(rfbClientPtr cl);
83 static enum rfbNewClientAction onNewClient(rfbClientPtr cl);
Steve Kondikef4e8652017-06-14 15:07:54 -070084 static void onFrameStart(rfbClientPtr cl);
85 static void onFrameDone(rfbClientPtr cl, int result);
Steve Kondik2c9d0cf2017-06-15 23:39:29 -070086 static void rfbLogger(const char* format, ...);
87
Steve Kondik77754522017-06-14 17:00:33 -070088 bool mRunning;
89 bool mFrameAvailable;
Steve Kondik6ec5bc82017-06-15 01:31:51 -070090 bool mRotate;
91 bool mVDSActive;
Steve Kondik77754522017-06-14 17:00:33 -070092
93 Mutex mEventMutex;
94 Mutex mUpdateMutex;
95
96 Condition mEventCond;
Steve Kondik7225c7f2017-06-14 00:06:16 -070097
Steve Kondik5f7b0a82017-06-16 09:04:43 -070098 uint32_t mWidth, mHeight;
99 int32_t mOrientation;
Steve Kondik55db0532017-06-12 11:27:18 -0700100
101 size_t mClientCount;
Steve Kondik77754522017-06-14 17:00:33 -0700102
Steve Kondik5f7b0a82017-06-16 09:04:43 -0700103 // Framebuffers
104 uint64_t mFrameNumber;
105 uint64_t mFrameSize;
106 nsecs_t mFrameStartWhen;
Steve Kondik6ec5bc82017-06-15 01:31:51 -0700107
Steve Kondik2c9d0cf2017-06-15 23:39:29 -0700108 // Server instance
109 rfbScreenInfoPtr mVNCScreen;
110
111 // Primary display
112 sp<IBinder> mMainDpy;
113
114 // Virtual display
115 sp<IBinder> mDpy;
Steve Kondik77754522017-06-14 17:00:33 -0700116
117 // Producer side of queue, passed into the virtual display.
118 sp<IGraphicBufferProducer> mProducer;
119
120 // This receives frames from the virtual display and makes them available
121 sp<CpuConsumer> mCpuConsumer;
122
Steve Kondik2c9d0cf2017-06-15 23:39:29 -0700123 // Consumer callback
124 sp<FrameListener> mListener;
Steve Kondik55db0532017-06-12 11:27:18 -0700125};
Steve Kondik55db0532017-06-12 11:27:18 -0700126};
127#endif