1 #include "demo.h"
2 
qrcode_demo(struct notcurses * nc,uint64_t startns)3 int qrcode_demo(struct notcurses* nc, uint64_t startns){
4   (void)startns;
5   if(!notcurses_canutf8(nc)){
6     return 0;
7   }
8 #ifdef USE_QRCODEGEN
9   char data[128];
10   unsigned dimy, dimx;
11   struct ncplane *stdn = notcurses_stddim_yx(nc, &dimy, &dimx);
12   ncplane_erase(stdn);
13   struct ncplane* n = ncplane_dup(stdn, NULL);
14   for(int i = 0 ; i < 1024 ; ++i){
15     ncplane_erase(n);
16     size_t len = rand() % sizeof(data) + 1;
17     size_t done = 0;
18     // done this tedious way because getrand() doesn't exist on freebsd 11
19     while(done < len){
20       long r = rand();
21       memcpy(data + done, &r, sizeof(r));
22       done += sizeof(r);
23     }
24     ncplane_home(n);
25     unsigned y = dimy, x = dimx;
26     ncplane_home(n);
27     int qlen = ncplane_qrcode(n, &y, &x, data, len);
28     if(qlen > 0){ // FIXME can fail due to being too large for display; distinguish this case
29       ncplane_move_yx(n, (dimy - y) / 2, (dimx - x) / 2);
30       ncplane_home(n);
31       ncplane_set_fg_rgb8(n, rand() % 255 + 1, rand() % 255 + 1, rand() % 255 + 1);
32       DEMO_RENDER(nc);
33     }
34   }
35   ncplane_mergedown_simple(n, stdn); // leave the last one on-screen
36   ncplane_destroy(n);
37 #endif
38   return 0;
39 }
40