Blame view

build4/epsilon-master/ion/src/blackbox/library.cpp 1.67 KB
6663b6c9   adorian   projet complet av...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  // Turn Epsilon into a library
  #include "library.h"
  #include "display.h"
  #include <ion.h>
  #include <mutex>
  #include <condition_variable>
  
  void PREFIXED(main)() {
    Ion::Display::Blackbox::setFrameBufferActive(true);
    ion_main(0, nullptr);
  }
  
  static std::mutex m;
  static std::condition_variable cv;
  static Ion::Events::Event sEvent = Ion::Events::None;
  
  enum class State {
    WaitingForEvent,
    EventAvailable,
    Processing,
    Processed
  };
  
  static State state = State::WaitingForEvent;
  
  Ion::Events::Event Ion::Events::getEvent(int * timeout) {
    if (state == State::Processing) {
      std::lock_guard<std::mutex> lk(m);
      state = State::Processed;
      cv.notify_one();
    }
  
    std::unique_lock<std::mutex> lk(m);
    cv.wait(lk, []{return (state == State::EventAvailable);});
    state = State::Processing;
  
    lk.unlock();
    cv.notify_one();
  
    return sEvent;
  }
  
  void PREFIXED(send_event)(int c) {
    Ion::Events::Event e = Ion::Events::None;
    if (c == EOF) {
      e = Ion::Events::Termination;
    } else {
      e = Ion::Events::Event(c);
      if (!(e.isDefined() && e.isKeyboardEvent())) {
        return;
      }
    }
  
    sEvent = e;
    {
      std::lock_guard<std::mutex> lk(m);
      state = State::EventAvailable;
    }
    cv.notify_one();
  }
  
  void PREFIXED(wait_event_processed)() {
    if (state == State::EventAvailable || state == State::Processing) {
      std::unique_lock<std::mutex> lk(m);
      cv.wait(lk, []{return state == State::Processed;});
      state = State::WaitingForEvent;
    }
    cv.notify_one();
  }
  
  
  const KDColor * PREFIXED(frame_buffer)() {
    return Ion::Display::Blackbox::frameBufferAddress();
  }
  
  void PREFIXED(write_frame_buffer_to_file)(const char * c) {
    Ion::Display::Blackbox::writeFrameBufferToFile(c);
  }