Skip to content

X shm implementation #16

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
2fc2157
websocket wrong callback
jishan484 Jun 12, 2022
c9e1717
websocket wrong callback
jishan484 Jun 17, 2022
0dc5cd6
backout few changes
jishan484 Jun 17, 2022
e734860
Merge branch 'code-optimization' of https://github.com/jishan484/PiWe…
jishan484 Jun 17, 2022
b04c18a
Bug fixed
jishan484 Jun 17, 2022
a29153d
fixed prev backout
jishan484 Jun 17, 2022
ab5946b
Merge branch 'code-optimization' of https://github.com/jishan484/PiWe…
jishan484 Jun 17, 2022
01dce4c
massive memory leak fixed
jishan484 Jun 25, 2022
df0c8a7
sleepDelay bug fixed : moving to 15 FPS
jishan484 Jun 25, 2022
e8fda23
client not disconnect fixed
jishan484 Jun 26, 2022
0234274
max client error added
jishan484 Jun 27, 2022
ddc4d04
MouseMove delay reduced to 0
jishan484 Jun 27, 2022
fc3f0dd
MouseMove delay reduced to 100
jishan484 Jun 27, 2022
6b30064
mouseDelay 100ms
jishan484 Jun 27, 2022
8a0a8e9
Merge branch 'code-optimization' of https://github.com/jishan484/PiWe…
jishan484 Jun 27, 2022
19881c3
MouseDeplay to 100ms
jishan484 Jun 27, 2022
7568e6f
first commit with Shm update
jishan484 Jun 29, 2022
fc0f7d5
A little miscalculation fixed
jishan484 Jun 29, 2022
749846b
hi
jishan484 Jun 29, 2022
8a6af76
kk
jishan484 Jun 29, 2022
2391c17
fixed
jishan484 Jun 30, 2022
e7d9475
fixed
jishan484 Jun 30, 2022
b64801e
test
jishan484 Jun 30, 2022
13b88f5
tes
jishan484 Jun 30, 2022
afdb0fd
Merge branch 'XShm_implementation' of https://github.com/jishan484/Pi…
jishan484 Jun 30, 2022
d9a030d
fixed
jishan484 Jun 30, 2022
038b3c1
ss
jishan484 Jun 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions PIwebVNC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ XInputs *xinputs; // XInputs object
/**
* main loop to send frames to the client
*/
void frameLoop(Websocket *ws)
void frameLoop()
{
vncServer.start_service(*ws);
vncServer.start_service(*wss);
}

void firstFrame(int sid)
Expand Down Expand Up @@ -72,7 +72,7 @@ int main(int argc, char *argv[])
xinputs = &input;
signal(SIGINT, handle_sigint);
std::thread t1 = ws.begin(SERVER_PORT);
std::thread t2(frameLoop, &ws);
std::thread t2(frameLoop);
ws.onMessage(onMessageCLBK);
ws.onConnect(firstFrame);

Expand Down
2 changes: 1 addition & 1 deletion compile.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apt install -y libx11-dev libxdamage-dev libxfixes-dev libxtst-dev liblz4-dev g++

g++ PIwebVNC.cpp -lX11 -lXdamage -lXfixes -pthread -lXtst -llz4 -o PiWebVNC
g++ PIwebVNC.cpp -lX11 -lXdamage -lXfixes -pthread -lXtst -lXext -llz4 -o PiWebVNC

echo "Compile Successful."

Expand Down
2 changes: 1 addition & 1 deletion libs/appConfigs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#define MAX_BUFFER_SIZE 1024

// =======display=========
#define FPS 10 // for pi zero set 5 [max 10 for pi4]
#define FPS 15 // for pi zero set 5 [max 10 for pi4]
#define FRAME_SEGMENTS_DELAY_MS 20 // for pi zero set 100

#endif
30 changes: 16 additions & 14 deletions libs/display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class XDisplay
ScreenInfo getScreenInfo();
std::string getCursorName();
int getBitPerLine();
int depth;

private:
Display *display = 0;
Expand All @@ -59,23 +60,23 @@ XDisplay::XDisplay()
try
{
std::string envDisplay = std::string(getenv("DISPLAY"));
display = XOpenDisplay(envDisplay.c_str());
this->display = XOpenDisplay(envDisplay.c_str());
}
catch (std::exception &e)
{
#if LOG || DEBUG
std::cout << "[LOG] No env set for display. Trying default.[0 to 9]." << std::endl;
#endif
int displayNumber = 0;
while (display == 0 && displayNumber < 10)
while (this->display == 0 && displayNumber < 10)
{
std::string displayNumberStr = ":" + std::to_string(displayNumber);
std::cout << "[LOG] Trying display number " << displayNumberStr << std::endl;
display = XOpenDisplay(displayNumberStr.c_str());
this->display = XOpenDisplay(displayNumberStr.c_str());
displayNumber++;
}
}
if (display == 0)
if (this->display == 0)
{
#if ERROR || DEBUG
std::cout << "[ERROR][EXIT APP] Could not open display. Please pass --display [id].\n\t eg: --display 18." << std::endl;
Expand All @@ -89,31 +90,31 @@ XDisplay::XDisplay()
XDisplay::~XDisplay()
{
// free display
if(display != 0) {
if(this->display != 0) {
#if LOG
std::cout << "[LOG] Closing display." << std::endl;
#endif
XCloseDisplay(display);
display = 0;
XCloseDisplay(this->display);
this->display = 0;
}
}

Display * XDisplay::getDisplay()
{
return display;
return this->display;
}

int XDisplay::getBitPerLine()
{
return bitPerLine;
return this->bitPerLine;
}

std::string XDisplay::getDisplayConfig()
{
ScreenInfo screenInfo = getScreenInfo();
int width = screenInfo.width;
int height = screenInfo.height;
XImage * img = XGetImage(display, DefaultRootWindow(display), 0, 0, width, height, AllPlanes, ZPixmap);
XImage * img = XGetImage(this->display, DefaultRootWindow(this->display), 0, 0, width, height, AllPlanes, ZPixmap);
std::string config = "{ 'bytePerLine':" + std::to_string(img->bytes_per_line) +
",'redMask':" + std::to_string(img->red_mask) +
",'greenMask':" + std::to_string(img->green_mask) +
Expand All @@ -124,14 +125,15 @@ std::string XDisplay::getDisplayConfig()
",'fps':" + std::to_string(FPS) +
+" }";
this->bitPerLine = img->bytes_per_line;
this->depth = img->depth;
XDestroyImage(img);
return config;
}

std::string XDisplay::getCursorName()
{
// get cursor name using xfixes
XFixesCursorImage *cursorImage = XFixesGetCursorImage(display);
XFixesCursorImage *cursorImage = XFixesGetCursorImage(this->display);
if (cursorImage == 0)
{
#if ERROR || DEBUG
Expand All @@ -147,9 +149,9 @@ ScreenInfo XDisplay::getScreenInfo()
{
ScreenInfo screenInfo;
// get screen info from root window
screenInfo.root = DefaultRootWindow(display);
screenInfo.width = DisplayWidth(display, DefaultScreen(display));
screenInfo.height = DisplayHeight(display, DefaultScreen(display));
screenInfo.root = DefaultRootWindow(this->display);
screenInfo.width = DisplayWidth(this->display, DefaultScreen(this->display));
screenInfo.height = DisplayHeight(this->display, DefaultScreen(this->display));
return screenInfo;
}

Expand Down
4 changes: 2 additions & 2 deletions libs/httpPage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct HttpHTMLPage

void parseHttpPage()
{
htmlPage.index_html = std::string("HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n") + R"html(
htmlPage.index_html = std::string("HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nServer: PIwebVNC (by Jishan)\r\n\r\n") + R"html(
<!DOCTYPE html>
<html lang="en">
<head>
Expand Down Expand Up @@ -9742,7 +9742,7 @@ void parseHttpPage()
var y = Math.floor(e.offsetY / canvas.offsetHeight * canvas.height)
data = "M" + x + " " + y + " ";
ws.send(data);
}, 200);
}, 100);
}
canvas.onwheel = function(e){
e.preventDefault();
Expand Down
87 changes: 44 additions & 43 deletions libs/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class XInputs
void dispatchEvents();
private:
Display *display;
char* events[30] = {0};
char events[30][100] = {0};
int eventCount = 0;
};

Expand All @@ -47,20 +47,22 @@ XInputs::XInputs(Display * display)

void XInputs::queueInputs(char *data, int clinetSD)
{
if (eventCount < 30)
if (this->eventCount < 30)
{
events[eventCount] = data;
eventCount++;
strcpy(this->events[this->eventCount], data);
this->eventCount++;
}
}

void XInputs::dispatchEvents(){
for(int i = 0; i < eventCount; i++){
if(events[i] != NULL){
processInputs(events[i], 0);
for(int i = 0; i < this->eventCount; i++){
if (events[i][0] != 0)
{
processInputs(this->events[i], 0);
this->events[i][0] = 0;
}
}
eventCount = 0;
this->eventCount = 0;
}

void XInputs::processInputs(char *data, int clientSD)
Expand All @@ -74,12 +76,12 @@ void XInputs::processInputs(char *data, int clientSD)
i++;
while (data[i] != 32 && i < len)
y = y * 10 + data[i++] - 48;
if (display == 0)
if (this->display == 0)
return;
XTestFakeMotionEvent(display, -1, x, y, CurrentTime);
XTestFakeButtonEvent(display, 1, True, CurrentTime);
XTestFakeButtonEvent(display, 1, False, CurrentTime);
XFlush(display);
XTestFakeMotionEvent(this->display, -1, x, y, CurrentTime);
XTestFakeButtonEvent(this->display, 1, True, CurrentTime);
XTestFakeButtonEvent(this->display, 1, False, CurrentTime);
XFlush(this->display);
}
else if (data[0] == 'M')
{
Expand All @@ -88,10 +90,10 @@ void XInputs::processInputs(char *data, int clientSD)
i++;
while (data[i] != 32 && i < len)
y = y * 10 + data[i++] - 48;
if (display == 0)
if (this->display == 0)
return;
XTestFakeMotionEvent(display, -1, x, y, CurrentTime);
XFlush(display);
XTestFakeMotionEvent(this->display, -1, x, y, CurrentTime);
XFlush(this->display);
}
else if (data[0] == 'R')
{
Expand All @@ -100,12 +102,12 @@ void XInputs::processInputs(char *data, int clientSD)
i++;
while (data[i] != 32 && i < len)
y = y * 10 + data[i++] - 48;
if (display == 0)
if (this->display == 0)
return;
XTestFakeMotionEvent(display, -1, x, y, CurrentTime);
XTestFakeButtonEvent(display, 3, True, CurrentTime);
XTestFakeButtonEvent(display, 3, False, CurrentTime);
XFlush(display);
XTestFakeMotionEvent(this->display, -1, x, y, CurrentTime);
XTestFakeButtonEvent(this->display, 3, True, CurrentTime);
XTestFakeButtonEvent(this->display, 3, False, CurrentTime);
XFlush(this->display);
}
else if (data[0] == 'D')
{
Expand All @@ -120,34 +122,34 @@ void XInputs::processInputs(char *data, int clientSD)
i++;
while (data[i] != 32 && i < len)
y2 = y2 * 10 + data[i++] - 48;
if (display == 0)
if (this->display == 0)
return;
XTestFakeMotionEvent(display, -1, x, y, CurrentTime);
XTestFakeButtonEvent(display, 1, True, CurrentTime);
XTestFakeMotionEvent(display, -1, x2, y2, CurrentTime);
XFlush(display);
XTestFakeMotionEvent(this->display, -1, x, y, CurrentTime);
XTestFakeButtonEvent(this->display, 1, True, CurrentTime);
XTestFakeMotionEvent(this->display, -1, x2, y2, CurrentTime);
XFlush(this->display);
}
else if (data[0] == 'S')
{
if (data[1] == 'U')
{
XTestFakeButtonEvent(display, 4, 1, 0);
XTestFakeButtonEvent(display, 4, 0, 70);
XTestFakeButtonEvent(this->display, 4, 1, 0);
XTestFakeButtonEvent(this->display, 4, 0, 70);
}
else
{
XTestFakeButtonEvent(display, 5, 1, 0);
XTestFakeButtonEvent(display, 5, 0, 70);
XTestFakeButtonEvent(this->display, 5, 1, 0);
XTestFakeButtonEvent(this->display, 5, 0, 70);
}
}
else if (data[0] == 'K')
{
if (data[1] == 49)
{
int keycode = XKeysymToKeycode(display, XStringToKeysym(data + 2));
XTestFakeKeyEvent(display, keycode, True, CurrentTime);
XTestFakeKeyEvent(display, keycode, False, CurrentTime);
XFlush(display);
int keycode = XKeysymToKeycode(this->display, XStringToKeysym(data + 2));
XTestFakeKeyEvent(this->display, keycode, True, CurrentTime);
XTestFakeKeyEvent(this->display, keycode, False, CurrentTime);
XFlush(this->display);
}
else if (data[1] == 50)
{
Expand All @@ -159,17 +161,16 @@ void XInputs::processInputs(char *data, int clientSD)
i++;
}
i++;
int keycode1 = XKeysymToKeycode(display, XStringToKeysym(buffer));
int keycode2 = XKeysymToKeycode(display, XStringToKeysym(data + i));
XTestFakeKeyEvent(display, keycode1, True, 0);
XTestFakeKeyEvent(display, keycode2, True, 0);
XTestFakeKeyEvent(display, keycode2, False, 0);
XTestFakeKeyEvent(display, keycode1, False, 0);
XFlush(display);
int keycode1 = XKeysymToKeycode(this->display, XStringToKeysym(buffer));
int keycode2 = XKeysymToKeycode(this->display, XStringToKeysym(data + i));
XTestFakeKeyEvent(this->display, keycode1, True, 0);
XTestFakeKeyEvent(this->display, keycode2, True, 0);
XTestFakeKeyEvent(this->display, keycode2, False, 0);
XTestFakeKeyEvent(this->display, keycode1, False, 0);
XFlush(this->display);
}
}
XFlush(display);
free(data);
XFlush(this->display);
}

#endif
3 changes: 2 additions & 1 deletion libs/sha1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ int char2int(char input)
std::string hex2bin(std::string src)
{
std::string target = "";
for (int i = 0; i < src.length(); i += 2)
int len = src.length();
for (int i = 0; i < len; i += 2)
{
target += char2int(src[i]) * 16 + char2int(src[i + 1]);
}
Expand Down
Loading