Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
H-M-H committed Apr 26, 2020
0 parents commit f6514dc
Show file tree
Hide file tree
Showing 24 changed files with 1,217 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .clang-format
@@ -0,0 +1,18 @@
Language: Cpp
Standard: Latest
BasedOnStyle: LLVM

ColumnLimit: 100
IndentWidth: 4
TabWidth: 4
UseTab: ForContinuationAndIndentation

BreakBeforeBraces: Allman

AccessModifierOffset: -4

PointerAlignment: Left

AlignAfterOpenBracket: AlwaysBreak
BinPackArguments: false
BinPackParameters: false
12 changes: 12 additions & 0 deletions .eslintrc.json
@@ -0,0 +1,12 @@
{
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "script",
"ecmaFeatures": {}
},
"rules": {
},
"env": {
"es2020": true
}
}
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
/target
*.js
*.js.map
25 changes: 25 additions & 0 deletions Cargo.toml
@@ -0,0 +1,25 @@
[package]
name = "web-tablet"
version = "0.1.0"
authors = ["HMH <henry@freedesk.net>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rocket = "0.4.4"
rocket_contrib = "0.4.4"
websocket = "0.24.0"
autopilot = "0.4.0"
libc = "0.2.69"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
log = "0.4"
bitflags = "1.2"
image = "0.23"
base64 = "0.12"
mtpng = "0.3.4"
rayon = "1.3.0"

[build-dependencies]
cc = "1.0"
34 changes: 34 additions & 0 deletions build.rs
@@ -0,0 +1,34 @@
use std::process::Command;

fn main() {
println!("cargo:rerun-if-changed=ts/lib.ts");
match Command::new("tsc")
.status()
{
Err(err) => {
println!("cargo:warning=Failed to call tsc: {}", err);
std::process::exit(1);
}
Ok(status) => {
if !status.success() {
match status.code() {
Some(code) => println!("cargo:warning=tsc failed with exitcode: {}", code),
None => println!("cargo:warning=tsc terminated by signal."),
};
std::process::exit(2);
}
}
}

println!("cargo:rerun-if-changed=lib/linux/error.h");
println!("cargo:rerun-if-changed=lib/linux/error.c");
println!("cargo:rerun-if-changed=lib/linux/uniput.c");
println!("cargo:rerun-if-changed=lib/linux/capture.c");
cc::Build::new()
.file("lib/linux/error.c")
.file("lib/linux/uinput.c")
.file("lib/linux/capture.c")
.compile("linux");
println!("cargo:rustc-link-lib=X11");
println!("cargo:rustc-link-lib=Xext");
}
2 changes: 2 additions & 0 deletions compile_flags.txt
@@ -0,0 +1,2 @@
-lX11
-lXext
100 changes: 100 additions & 0 deletions lib/linux/capture.c
@@ -0,0 +1,100 @@
// gcc raw.c -o raw -lX11 -lXext -Ofast -funroll-loops && ./raw
// gcc raw.c -o raw -lX11 -lXext -Ofast -funroll-loops -mfpmath=both -march=native -m64 -mavx2 &&
// ./raw

// The MIT-SHM extension allows for shared-memory XImage objects
// This requires OS support for SYSV (System-V) shared memory, and X support for the MIT-SHM
// extension. Shared memory PIXMAPS can only be supported when the X server can use regular virtual
// memory for pixmap data; if the pixmaps are stored in some magic graphics hardware, you're out of
// luck. Xdpyinfo(1) gives NO information about this!

// Depth 16: each pixel has 16 bits. Red: 5 bits, green: 6 bits, blue: 5 bits. Total: 65536 colors

#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include <X11/extensions/XShm.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#include <stdint.h>
#include <stdio.h>

#include "error.h"

struct CaptureContext
{
Display* display;
XImage* ximg;
XShmSegmentInfo shminfo;
Window window_root;
};

typedef struct CaptureContext CaptureContext;

struct Image
{
char* data;
int width;
int height;
};

void* init_capture(Error* err)
{
CaptureContext* ctx = malloc(sizeof(CaptureContext));
ctx->display = XOpenDisplay(NULL);

int ignore, major, minor;
Bool pixmaps;
if (XQueryExtension(ctx->display, "MIT-SHM", &ignore, &ignore, &ignore))
if (XShmQueryVersion(ctx->display, &major, &minor, &pixmaps))
printf(
"XShm extension v%d.%d %s shared pixmaps\n",
major,
minor,
pixmaps ? "with" : "without");
// Macro to return the root window! It's a simple uint32
ctx->window_root = DefaultRootWindow(ctx->display);
XWindowAttributes window_attributes;
XGetWindowAttributes(ctx->display, ctx->window_root, &window_attributes);
Screen* screen = window_attributes.screen;
ctx->ximg = XShmCreateImage(
ctx->display,
DefaultVisualOfScreen(screen),
DefaultDepthOfScreen(screen),
ZPixmap,
NULL,
&ctx->shminfo,
screen->width,
screen->height);

ctx->shminfo.shmid =
shmget(IPC_PRIVATE, ctx->ximg->bytes_per_line * ctx->ximg->height, IPC_CREAT | 0777);
ctx->shminfo.shmaddr = ctx->ximg->data = (char*)shmat(ctx->shminfo.shmid, 0, 0);
ctx->shminfo.readOnly = False;
if (ctx->shminfo.shmid < 0)
puts("Fatal shminfo error!");
;
Status s1 = XShmAttach(ctx->display, &ctx->shminfo);
printf("XShmAttach() %s\n", s1 ? "success!" : "failure!");

return ctx;
}

void capture_sceen(CaptureContext* ctx, struct Image* img, Error* err)
{
XShmGetImage(ctx->display, ctx->window_root, ctx->ximg, 0, 0, 0x00ffffff);
img->width = ctx->ximg->width;
img->height = ctx->ximg->height;
img->data = ctx->ximg->data;
}

void destroy_capture(CaptureContext* ctx, Error* err)
{
XShmDetach(ctx->display, &ctx->shminfo);
XDestroyImage(ctx->ximg);
shmdt(ctx->shminfo.shmaddr);
XCloseDisplay(ctx->display);
free(ctx);
}
8 changes: 8 additions & 0 deletions lib/linux/error.c
@@ -0,0 +1,8 @@
#include "error.h"

void fill_error(Error* err, int code, const char* fmt, ...)
{
va_list args;
err->code = code;
vsnprintf(err->error_str, sizeof(err->error_str), fmt, args);
}
26 changes: 26 additions & 0 deletions lib/linux/error.h
@@ -0,0 +1,26 @@
#pragma once

#include <stdarg.h>
#include <stdio.h>

struct Error
{
int code;
char error_str[1024];
};

typedef struct Error Error;

void fill_error(Error* err, int code, const char* fmt, ...);

#define ERROR(err, code, fmt, ...) \
{ \
fill_error(err, code, fmt, ##__VA_ARGS__); \
return; \
}

#define OK_OR_ABORT(err) \
{ \
if (err->code) \
return; \
}

0 comments on commit f6514dc

Please sign in to comment.