Skip to content

Instantly share code, notes, and snippets.

@MCJack123
Created January 28, 2018 22:17
Show Gist options
  • Save MCJack123/2cb49016483356c777f913dc1c028601 to your computer and use it in GitHub Desktop.
Save MCJack123/2cb49016483356c777f913dc1c028601 to your computer and use it in GitHub Desktop.
Allows creating Chrome/Chromium OS apps that run a crouton program

Requirements

How to use

  1. Inside your chroot, compile both C++ sources using jsoncpp.
  2. Run initjson to create an id for your device.
  3. Run addprogram <program name> <program path> <app name> [app icon] to create the Chrome apps
  4. Copy the Chrome apps to your Downloads folder, and install them in chrome://extensions.
  5. Run gameserver.js with node. The script must be running for the app to work.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <jsoncpp/json/json.h>
int main(int argc, const char * argv[]) {
if (argc < 4) {
std::cout << "Usage: " << argv[0] << " <program name> <program path> <app name> [icon path]";
return 1;
}
std::string icon;
if (argc > 4) {
icon = std::string(argv[4]);
} else {
icon = "icon_128.png";
}
Json::Value root;
std::ifstream in;
std::ofstream out;
in.open("programs.json");
//std::stringstream pro;
//std::string p;
//in >> p;
//std::cout << ":" << p << "\n";
//pro << p;
//pro >> root;
in >> root;
in.close();
std::string id = root["id"].asString();
if (!root.isMember(argv[1])) root["keys"].append(Json::Value(argv[1]));
root[argv[1]] = Json::Value(argv[2]);
out.open("programs.json");
out << root;
out.close();
std::string dir("" + std::string(argv[1]) + "/");
system(std::string("mkdir " + dir).c_str());
Json::Value manifest;
manifest["name"] = Json::Value(argv[3]);
manifest["description"] = Json::Value("Crouton app wrapper");
manifest["version"] = Json::Value("1.0");
manifest["manifest_version"] = Json::Value(2);
manifest["icons"] = Json::Value(Json::objectValue);
manifest["icons"]["128"] = Json::Value("icon_128.png");
manifest["app"] = Json::Value(Json::objectValue);
manifest["app"]["background"] = Json::Value(Json::objectValue);
manifest["app"]["background"]["scripts"] = Json::Value(Json::arrayValue);
manifest["app"]["background"]["scripts"].append(Json::Value("background.js"));
out.open(std::string(dir + "manifest.json").c_str());
out << manifest;
out.close();
out.open(std::string(dir + "background.js").c_str());
out << "var xhr = new XMLHttpRequest(); xhr.open(\"GET\", \"http://dweet.io/dweet/for/croutonapp" << id << "?program=" << argv[1] << "\", true); xhr.setRequestHeader(\"Content-Type\", \"text/json; charset=UTF-8\"); xhr.send();";
out.close();
system(std::string("cp " + icon + " " + dir + "icon_128.png").c_str());
system(std::string("crxmake --pack-extension=" + dir).c_str());
std::cout << "programs.json has been updated, and the extension has been created as " << dir << "\b.crx.\n";
return 0;
}
//var http = require("http");
var programs = require("./programs.json");
var system = require("child_process").exec;
var dweetClient = require("node-dweetio");
var dweetio = new dweetClient();
/*server = http.createServer( function(req, res) {
console.dir(req.param);
console.log("POST");
var body = '';
req.on('data', function (data) {
body += data;
console.log("Partial body: " + body);
});
req.on('end', function () {
console.log("Body: " + body);
var data = JSON.parse(body);
for (var key in programs.keys) {
if (data.program == key) {
system("sudo enter-chroot sudo xiwi " + programs[key], function() {});
}
}
});
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('post received');
});
server.listen("localhost", 3000);
console.log("Hosting on 127.0.0.1:3000");
*/
dweetio.listen_for("croutonapp" + programs.id, function(dweet) {
//console.log("Got dweet " + dweet.content.program);
for (var key in programs.keys) {
//console.log(programs.keys[key]);
if (dweet.content.program == programs.keys[key]) {
console.log("Running " + programs.keys[key]);
system("sudo xiwi " + programs[programs.keys[key]], function(err, stdout, stderr) {
if (err) {
console.log("Error: " + err);
return;
}
//console.log("stdout: " + stdout);
//console.log("stderr: " + stderr);
});
}
}
});
#include <fstream>
#include <string>
#include <jsoncpp/json/json.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
std::string paddedHex(uint16_t hex) {
//Get length of hex.
int dlen = floor(log((int)hex) / log(16)) + 1;
printf("%d", dlen);
//Find how many leading zeros we need.
int dpad = 4 - dlen;
//Our final hex code.
char fhex[] = "0000";
char shex[5];
//Convert hex to a string.
sprintf(shex, "%x", hex);
//Copy shex to fhex, using dpad as a memory offset.
memcpy(fhex+dpad, shex, dlen);
return std::string(fhex);
}
int main() {
std::ofstream out;
Json::Value root;
srand(time(NULL));
uint16_t r = (uint16_t)(rand() % 65536);
std::string hex = paddedHex(r);
printf("done rand: %u, %s\n", r, hex.c_str());
root["keys"] = Json::Value(Json::arrayValue);
root["id"] = Json::Value(hex);
printf("writing out\n");
out.open("programs.json");
out << root;
out.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment