Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ include $(DEVKITPRO)/libnx/switch_rules
# NACP building is skipped as well.
#---------------------------------------------------------------------------------
APP_TITLE := ShareNX 
APP_VERSION := 1.1.4
APP_VERSION := 1.2.0

TARGET := ovlShareNX
BUILD := build
SOURCES := source
SOURCES := source libs/qr
DATA := data
INCLUDES := libs/libtesla/include
INCLUDES := libs/libtesla/include libs/qr

ifeq ($(RELEASE),)
APP_VERSION := $(APP_VERSION)-$(shell git describe --dirty --always)
Expand Down Expand Up @@ -189,7 +189,7 @@ DEPENDS := $(OFILES:.o=.d)
#---------------------------------------------------------------------------------
all : $(OUTPUT).ovl

$(OUTPUT).ovl : $(OUTPUT).elf $(OUTPUT).nacp
$(OUTPUT).ovl : $(OUTPUT).elf $(OUTPUT).nacp
@elf2nro $< $@ $(NROFLAGS)
@echo "built ... $(notdir $(OUTPUT).ovl)"

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ Put the .ovl from the release inside /switch/.overlays and start it from "Tesla
## Credits
[WerWolv](https://github.com/WerWolv) for nx-ovlloader, libtesla and the template<br>
[averne](https://github.com/averne) for his work on libtesla<br>
[natinusala](https://github.com/natinusala) for his work on libtesla<br>
[natinusala](https://github.com/natinusala) for his work on libtesla<br>
[nayuki](https://github.com/nayuki/QR-Code-generator) for their amazing qr code generator<br>
2 changes: 1 addition & 1 deletion libs/libtesla
1,028 changes: 1,028 additions & 0 deletions libs/qr/qrcodegen.c

Large diffs are not rendered by default.

385 changes: 385 additions & 0 deletions libs/qr/qrcodegen.h

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions source/elm_qr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "elm_qr.hpp"
#include "qrcodegen.h"

namespace {

constexpr auto QR_BUFFER_SIZE = qrcodegen_BUFFER_LEN_MAX;
constexpr auto SCALE = 4;

} // namespace

void ElmQr::generateQrCode(const std::string& text) {
std::vector<u8> qr_buffer(QR_BUFFER_SIZE);
std::vector<u8> temp_buffer(QR_BUFFER_SIZE);
if (!qrcodegen_encodeText(text.c_str(), temp_buffer.data(), qr_buffer.data(), qrcodegen_Ecc_MEDIUM, qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true)) {
return;
}

const auto size = qrcodegen_getSize(qr_buffer.data());
if (size <= 0) {
return;
}

qr_size = size;
qr_data = qr_buffer;
Element::invalidate();
}

tsl::elm::Element *ElmQr::requestFocus(tsl::elm::Element *oldFocus, tsl::FocusDirection direction) {
return oldFocus;
}

void ElmQr::draw(tsl::gfx::Renderer *renderer) {
if (!qr_size) {
return;
}

const auto white = tsl::Color(0xFF, 0xFF, 0xFF, 0xFF);
const auto black = tsl::Color(0x00, 0x00, 0x00, 0xFF);

for (int y = 0; y < qr_size; y++) {
for (int x = 0; x < qr_size; x++) {
const auto c = qrcodegen_getModule(qr_data.data(), x, y) ? white : black;
for (int yy = 0; yy < SCALE; yy++) {
for (int xx = 0; xx < SCALE; xx++) {
renderer->setPixel(this->getX() + x * SCALE + xx, this->getY() + y * SCALE + yy, c);
}
}
}
}
}

void ElmQr::layout(u16 parentX, u16 parentY, u16 parentWidth, u16 parentHeight) {
this->setBoundaries(tsl::cfg::FramebufferWidth / 2 - qr_size * SCALE / 2, this->getY() + 10, qr_size * SCALE, qr_size * SCALE);
}
18 changes: 18 additions & 0 deletions source/elm_qr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <string>
#include <tesla.hpp>
#include <vector>

class ElmQr final : public tsl::elm::Element {
private:
std::vector<u8> qr_data;
int qr_size{};

public:
void generateQrCode(const std::string &text);

tsl::elm::Element *requestFocus(tsl::elm::Element *oldFocus, tsl::FocusDirection direction) override;
void draw(tsl::gfx::Renderer *renderer) override;
void layout(u16 parentX, u16 parentY, u16 parentWidth, u16 parentHeight) override;
};
15 changes: 11 additions & 4 deletions source/gui_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <string>

#include "elm_qr.hpp"
#include "gui_error.hpp"
#include "upload.hpp"

Expand All @@ -32,21 +33,27 @@ MainGui::~MainGui() {}
tsl::elm::Element *MainGui::createUI() {
auto rootFrame = new tsl::elm::OverlayFrame("ShareNX \uE134", VERSION);

auto *list = new tsl::elm::List();
auto list = new tsl::elm::List();

list->addItem(this->img);

auto *button = new tsl::elm::ListItem("Upload");
button->setClickListener([&](u64 keys) {
auto qr = new ElmQr();

auto button = new tsl::elm::ListItem("Upload");
button->setClickListener([this, qr](u64 keys) {
if (keys & HidNpadButton_A && !this->uploaded) {
std::string url = web::UploadImage(this->fileId);
auto [result, url] = web::UploadImage(this->fileId);
this->uploaded = true;
this->img->setUrl(url);
if (result) {
qr->generateQrCode(url);
}
return true;
}
return false;
});
list->addItem(button);
list->addItem(qr);

rootFrame->setContent(list);

Expand Down
9 changes: 7 additions & 2 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define TESLA_INIT_IMPL
#include <curl/curl.h>

#include "gui_error.hpp"
#include "gui_main.hpp"
#include "image_item.hpp"
Expand All @@ -28,8 +30,6 @@
}

constexpr const SocketInitConfig sockConf = {
.bsdsockets_version = 1,

.tcp_tx_buf_size = 0x800,
.tcp_rx_buf_size = 0x800,
.tcp_tx_buf_max_size = 0x25000,
Expand Down Expand Up @@ -77,8 +77,13 @@ class ShareOverlay : public tsl::Overlay {
msg = "CapSrv error!";
return;
}

if (CURLE_OK != curl_global_init(CURL_GLOBAL_DEFAULT)) {
msg = "curl_global_init error!";
}
}
virtual void exitServices() override {
curl_global_cleanup();
capsaExit();
socketExit();
}
Expand Down
20 changes: 14 additions & 6 deletions source/upload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,38 @@
#include <tesla.hpp>

namespace web {
namespace {

size_t StringWrite(const char *contents, size_t size, size_t nmemb, std::string *userp) {
userp->append(contents, size * nmemb);
return size * nmemb;
}

std::string UploadImage(const CapsAlbumFileId &fileId) {
using UploadResult = std::pair<bool, std::string>;

} // namespace

std::pair<bool, std::string> UploadImage(const CapsAlbumFileId &fileId) {
u64 size = 0;
Result rc = capsaGetAlbumFileSize(&fileId, &size);
if (R_FAILED(rc))
return "Can't get Filesize";
return UploadResult{false, "Can't get Filesize"};

char *imgBuffer = new (std::nothrow) char[size];
if (imgBuffer == nullptr)
return "Memory allocation failed";
return UploadResult{false, "Memory allocation failed"};

tsl::hlp::ScopeGuard buffer_guard([imgBuffer] { delete[] imgBuffer; });

u64 actualSize = 0;
rc = capsaLoadAlbumFile(&fileId, &actualSize, imgBuffer, size);
if (R_FAILED(rc)) {
return "Failed to load Image";
return UploadResult{false, "Failed to load Image"};
}

CURL *curl = curl_easy_init();
if (curl == nullptr)
return "Failed to init curl";
return UploadResult{false, "Failed to init curl"};

curl_mime *mime = curl_mime_init(curl);
curl_mimepart *file_part = curl_mime_addpart(mime);
Expand Down Expand Up @@ -78,19 +83,22 @@ namespace web {

long http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
bool curl_success{};

if (res != CURLE_OK) {
urlresponse = "Curl failed " + std::to_string(res);
} else if (http_code != 200) {
urlresponse = "Failed with " + std::to_string(http_code);
} else if (urlresponse.size() > 0x30) {
urlresponse = "Result too long";
} else {
curl_success = true;
}

curl_mime_free(mime);
curl_easy_cleanup(curl);

return urlresponse;
return UploadResult{curl_success, urlresponse};
}

}
2 changes: 1 addition & 1 deletion source/upload.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@

namespace web {

std::string UploadImage(const CapsAlbumFileId &fileId);
std::pair<bool, std::string> UploadImage(const CapsAlbumFileId &fileId);

}