You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
notcurses/src/demo/qrcode.c

71 lines
2.1 KiB
C

#include "demo.h"
#ifdef USE_QRCODEGEN
#include <sys/random.h>
// FIXME duplicated--ought these just be exported?
#define QR_BASE_SIZE 17
#define PER_QR_VERSION 4
static inline int
qrcode_rows(int version){
return (QR_BASE_SIZE + (version * PER_QR_VERSION)) / 2;
}
static inline int
qrcode_cols(int version){
return QR_BASE_SIZE + (version * PER_QR_VERSION);
}
#endif
int qrcode_demo(struct notcurses* nc){
#ifdef USE_QRCODEGEN
char data[128];
int dimy, dimx;
struct ncplane *stdn = notcurses_stddim_yx(nc, &dimy, &dimx);
ncplane_erase(stdn);
struct ncplane* n = ncplane_dup(stdn, NULL);
for(int i = 0 ; i < 1024 ; ++i){
ncplane_erase(n);
size_t len = random() % sizeof(data) + 1;
ssize_t got = getrandom(data, len, 0);
Fully general ncvisual layer (#647) This represents an essentially complete rewrite of ncvisual and associated code. It had two major goals: Improve the ncvisual API based off lessons learned, pursuant to the upcoming API freeze. In particular, I wanted to: decouple ncvisuals from ncplanes. It should be possible to render a ncvisual to multiple planes, with different scaling each time. It should be possible to create an ncvisual without a plane, etc. normalize the various ways of constructing an ncvisual -- file, memory, plane, etc. Support multiple blitters, from 7-bit ASCII to Sixel. This required writing the blitters in several cases, and they're not yet in their final implementations (but the API is fine) I have not yet unified Plots and Visuals, and might not, given that the Plot code works fine. We could at this point implement Plots in terms of Visuals, though -- the blitter backend range has been unified. Sixel is not yet implemented, though it is listed. There is a new POC tool, blitter. It renders its arguments using all possible blitter+scaling combinations. Another new POC, resize, displays its argument, then resizes it to the screen size and displays that, explicitly making use of ncvisual_resize() rather than a scaling parameter to ncvisual_render(). This also eliminates some memory leaks and bugs we were seeing in trunk, and brings in Sixel scaffolding. The C++ wrapper will also need patching back up; I cut most of it down while wrestling with this crap, urk. Closes #638, #562, and #622.
4 years ago
size_t done;
// done this tedious way because getrandom() doesn't exist on freebsd 11
for(done = 0 ; done < len ; ++done){
long r = random();
memcpy(data + done, &r, sizeof(r));
done += sizeof(r);
}
if(got < 0 || (size_t)got != len){
ncplane_destroy(n);
return -1;
}
if(ncplane_cursor_move_yx(n, 0, 0)){
ncplane_destroy(n);
return -1;
}
int qlen = ncplane_qrcode(n, 0, data, len);
// can fail due to being too large for the terminal
if(qlen > 0){
ncplane_move_yx(n, dimy / 2 - qrcode_rows(qlen) / 2,
dimx / 2 - qrcode_cols(qlen) / 2);
if(ncplane_cursor_move_yx(n, 0, 0)){
ncplane_destroy(n);
return -1;
}
uint64_t tl = 0, bl = 0, br = 0, tr = 0;
channels_set_fg_rgb(&tl, random() % 255 + 1, random() % 255 + 1, random() % 255 + 1);
channels_set_fg_rgb(&tr, random() % 255 + 1, random() % 255 + 1, random() % 255 + 1);
channels_set_fg_rgb(&bl, random() % 255 + 1, random() % 255 + 1, random() % 255 + 1);
channels_set_fg_rgb(&br, random() % 255 + 1, random() % 255 + 1, random() % 255 + 1);
if(ncplane_stain(n, dimy - 1, dimx - 1, tl, tr, bl, br) <= 0){
ncplane_destroy(n);
return -1;
}
DEMO_RENDER(nc);
}
}
ncplane_destroy(n);
#endif
DEMO_RENDER(nc);
return 0;
}