|
2 | 2 |
|
3 | 3 | #include <cstdio>
|
4 | 4 | #include <cstring>
|
| 5 | +#include <fcntl.h> |
5 | 6 |
|
6 | 7 | #include "imgui.h"
|
7 | 8 |
|
|
27 | 28 | #define QOI_IMPLEMENTATION
|
28 | 29 | #define QOI_NO_STDIO
|
29 | 30 | #define QOI_FREE static_assert(false && "free should never be called")
|
30 |
| -#define QOI_MALLOC(sz) qoi_malloc(sz) |
31 |
| -static void *qoi_malloc(size_t sz) { |
| 31 | +#define QOI_MALLOC(sz) qoiMalloc(sz) |
| 32 | +static void *qoiMalloc(size_t sz) { |
32 | 33 | static void *qoiBuffer = nullptr;
|
33 | 34 | static size_t lastSz = 0;
|
34 | 35 | if(qoiBuffer == nullptr) {
|
@@ -152,7 +153,6 @@ int App::run(CliOptions &opts) {
|
152 | 153 | sk_sp<SkFontMgr> fontMgr = nullptr;
|
153 | 154 | sk_sp<SkTypeface> typeface = nullptr;
|
154 | 155 | sk_sp<SkData> ttfData = nullptr;
|
155 |
| - SkMemoryStream *ttfStream = nullptr; |
156 | 156 | { // setup skia/imgui shared objects
|
157 | 157 | if (opts.fffiInterpreter) {
|
158 | 158 | if (opts.fffiInFile != nullptr) {
|
@@ -306,6 +306,28 @@ int App::run(CliOptions &opts) {
|
306 | 306 | loopSkp(opts);
|
307 | 307 | break;
|
308 | 308 | }
|
| 309 | + if(opts.videoUserInteractionEventsInFile != nullptr && opts.videoUserInteractionEventsInFile[0] != '\0') { |
| 310 | + fUserInteractionFH = fopen(opts.videoUserInteractionEventsInFile, "rb"); |
| 311 | + if(fUserInteractionFH == nullptr) { |
| 312 | + fprintf(stderr, "unable to open user interaction events in file %s: %s\n", opts.videoUserInteractionEventsInFile, strerror(errno)); |
| 313 | + return 1; |
| 314 | + } |
| 315 | + |
| 316 | + auto const fd = fileno(fUserInteractionFH); |
| 317 | + auto const flags = fcntl(fd, F_GETFL); |
| 318 | + if(flags < 0) { |
| 319 | + fprintf(stderr, "unable to get file status flags for file %s: %s\n", opts.videoUserInteractionEventsInFile, strerror(errno)); |
| 320 | + return 1; |
| 321 | + } |
| 322 | + if(fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) { |
| 323 | + fprintf(stderr, "unable to set file status flag O_NONBLOCK file %s: %s\n", opts.videoUserInteractionEventsInFile, strerror(errno)); |
| 324 | + return 1; |
| 325 | + } |
| 326 | + if(setvbuf(fUserInteractionFH,nullptr,_IONBF,0) != 0) { |
| 327 | + fprintf(stderr, "unable to set buffering for file %s: %s\n", opts.videoUserInteractionEventsInFile, strerror(errno)); |
| 328 | + return 1; |
| 329 | + } |
| 330 | + } |
309 | 331 |
|
310 | 332 | if(opts.fffiInterpreter) {
|
311 | 333 | render_cleanup();
|
@@ -619,3 +641,85 @@ void App::loopSkp(const CliOptions &opts) {
|
619 | 641 | postPaint();
|
620 | 642 | }
|
621 | 643 | }
|
| 644 | +App::~App() { |
| 645 | + if(fUserInteractionFH != nullptr) { |
| 646 | + fclose(fUserInteractionFH); |
| 647 | + fUserInteractionFH = nullptr; |
| 648 | + } |
| 649 | +} |
| 650 | + |
| 651 | +void App::dispatchUserInteractionEvents() { |
| 652 | + size_t memorySize = 1024 * 1024; |
| 653 | + static uint8_t state = 0; |
| 654 | + static uint8_t* mem = nullptr; |
| 655 | + static uint8_t* p = nullptr; |
| 656 | + static uint32_t bytesToRead = 0; |
| 657 | + |
| 658 | + while(true) { |
| 659 | + switch(state) { |
| 660 | + case 0: // init |
| 661 | + mem = static_cast<uint8_t *>(malloc(memorySize)); |
| 662 | + if(mem == nullptr) { |
| 663 | + fprintf(stderr,"unable to allocate memory\n"); |
| 664 | + exit(2); |
| 665 | + } |
| 666 | + state = 1; |
| 667 | + bytesToRead = 4; |
| 668 | + p = mem; |
| 669 | + break; |
| 670 | + case 1: // read flatbuffers message length |
| 671 | + { |
| 672 | + auto r = fread(p,1,bytesToRead,fUserInteractionFH); |
| 673 | + bytesToRead -= r; |
| 674 | + p += r; |
| 675 | + if(bytesToRead == 0) { |
| 676 | + // read length of message |
| 677 | + bytesToRead = flatbuffers::ReadScalar<uint32_t>(mem); |
| 678 | + if(bytesToRead > memorySize) { |
| 679 | + memorySize = (bytesToRead/4096+1)*4096; |
| 680 | + mem = static_cast<uint8_t *>(realloc(mem, memorySize)); |
| 681 | + p = mem+4; |
| 682 | + } |
| 683 | + state = 2; |
| 684 | + } |
| 685 | + } |
| 686 | + break; |
| 687 | + case 2: // read flatbuffers message |
| 688 | + { |
| 689 | + auto r = fread(p,1,bytesToRead,fUserInteractionFH); |
| 690 | + bytesToRead -= r; |
| 691 | + p += r; |
| 692 | + if(bytesToRead == 0) { |
| 693 | + auto const e = UserInteractionFB::GetSizePrefixedEvent(mem); |
| 694 | + handleUserInteractionEvent(*e); |
| 695 | + state = 1; |
| 696 | + p = mem; |
| 697 | + bytesToRead = 4; |
| 698 | + } |
| 699 | + } |
| 700 | + break; |
| 701 | + } |
| 702 | + } |
| 703 | +} |
| 704 | + |
| 705 | +void App::handleUserInteractionEvent(UserInteractionFB::Event const &ev) { |
| 706 | + switch(ev.event_type()) { |
| 707 | + case UserInteractionFB::UserInteraction_NONE: |
| 708 | + break; |
| 709 | + case UserInteractionFB::UserInteraction_EventMouseMotion: |
| 710 | + { |
| 711 | + auto const e = ev.event_as_EventMouseMotion(); |
| 712 | + } |
| 713 | + break; |
| 714 | + case UserInteractionFB::UserInteraction_EventMouseWheel: |
| 715 | + { |
| 716 | + auto const e = ev.event_as_EventMouseMotion(); |
| 717 | + } |
| 718 | + break; |
| 719 | + case UserInteractionFB::UserInteraction_EventMouseButton: |
| 720 | + { |
| 721 | + auto const e = ev.event_as_EventMouseButton(); |
| 722 | + } |
| 723 | + break; |
| 724 | + } |
| 725 | +} |
0 commit comments