Make various instance stuff to take references to other instance objs
[lsnes.git] / include / core / instance.hpp
blob80f877fb48edcc5a5e825824ef0994bb0ad45b82
1 #ifndef _instance__hpp__included__
2 #define _instance__hpp__included__
4 #include <deque>
5 #include "core/command.hpp"
6 #include "core/controllerframe.hpp"
7 #include "core/debug.hpp"
8 #include "core/emustatus.hpp"
9 #include "core/framebuffer.hpp"
10 #include "core/framerate.hpp"
11 #include "core/inthread.hpp"
12 #include "core/movie.hpp"
13 #include "core/moviedata.hpp"
14 #include "core/mbranch.hpp"
15 #include "core/memorymanip.hpp"
16 #include "core/memorywatch.hpp"
17 #include "core/multitrack.hpp"
18 #include "core/project.hpp"
19 #include "library/command.hpp"
20 #include "library/exrethrow.hpp"
21 #include "library/lua-base.hpp"
22 #include "library/memoryspace.hpp"
23 #include "library/settingvar.hpp"
24 #include "library/keyboard.hpp"
25 #include "library/keyboard-mapper.hpp"
27 /**
28 * Information about keypress.
30 struct keypress_info
32 /**
33 * Create null keypress (no modifiers, NULL key and released).
35 keypress_info();
36 /**
37 * Create new keypress.
39 keypress_info(keyboard::modifier_set mod, keyboard::key& _key, short _value);
40 /**
41 * Create new keypress (two keys).
43 keypress_info(keyboard::modifier_set mod, keyboard::key& _key, keyboard::key& _key2, short _value);
44 /**
45 * Modifier set.
47 keyboard::modifier_set modifiers;
48 /**
49 * The actual key (first)
51 keyboard::key* key1;
52 /**
53 * The actual key (second)
55 keyboard::key* key2;
56 /**
57 * Value for the press
59 short value;
62 template<typename T>
63 void functor_call_helper2(void* args)
65 (*reinterpret_cast<T*>(args))();
66 delete reinterpret_cast<T*>(args);
69 struct input_queue
71 struct function_queue_entry
73 std::function<void()> fn;
74 std::function<void(std::exception& e)> onerror;
77 //Queue stuff.
78 threads::lock queue_lock;
79 threads::cv queue_condition;
80 std::deque<keypress_info> keypresses;
81 std::deque<std::string> commands;
82 std::deque<function_queue_entry> functions;
83 volatile uint64_t functions_executed;
84 volatile uint64_t next_function;
85 volatile bool system_thread_available;
86 bool queue_function_run;
87 /**
88 * Ctor.
90 input_queue(command::group& _command);
91 /**
92 * Queue keypress.
94 * - Can be called from any thread.
96 * Parameter k: The keypress to queue.
98 void queue(const keypress_info& k) throw(std::bad_alloc);
99 /**
100 * Queue command.
102 * - Can be called from any thread.
104 * Parameter c: The command to queue.
106 void queue(const std::string& c) throw(std::bad_alloc);
108 * Queue function to be called in emulation thread.
110 * - Can be called from any thread (exception: Synchronous mode can not be used from emulation nor main threads).
112 * Parameter f: The function to execute.
113 * Parameter onerror: Function to call on error.
114 * Parameter arg: Argument to pass to the function.
115 * Parameter sync: If true, execute function call synchronously, else asynchronously.
117 void queue(std::function<void()> f, std::function<void(std::exception& e)> onerror, bool sync)
118 throw(std::bad_alloc);
120 * Run all queues.
122 void run_queues() throw();
124 * Call function synchronously in emulation thread.
126 void run(std::function<void()> fn)
128 exrethrow::storage ex;
129 queue(fn, [&ex](std::exception& e) { ex = exrethrow::storage(e); }, true);
130 if(ex) ex.rethrow();
133 * Queue asynchrous function in emulation thread.
135 void run_async(std::function<void()> fn,
136 std::function<void(std::exception& e)> onerror)
138 queue(fn, onerror, false);
141 * Run internal queues.
143 void run_queue(bool unlocked) throw();
144 private:
145 command::group& command;
148 struct emulator_instance
150 emulator_instance();
151 movie_logic mlogic;
152 memory_space memory;
153 lua::state lua;
154 memwatch_set mwatch;
155 settingvar::group settings;
156 settingvar::cache setcache;
157 voice_commentary commentary;
158 subtitle_commentary subtitles;
159 movie_branches mbranch;
160 multitrack_edit mteditor;
161 _lsnes_status status_A;
162 _lsnes_status status_B;
163 _lsnes_status status_C;
164 triplebuffer::triplebuffer<_lsnes_status> status;
165 keyboard::keyboard keyboard;
166 keyboard::mapper mapper;
167 command::group command;
168 alias_binds_manager abindmanager;
169 rrdata nrrdata;
170 cart_mappings_refresher cmapper;
171 controller_state controls;
172 project_state project;
173 debug_context dbg;
174 framerate_regulator framerate;
175 emu_framebuffer fbuf;
176 input_queue iqueue;
177 threads::id emu_thread;
180 extern emulator_instance lsnes_instance;
182 emulator_instance& CORE();
184 #endif