2013-09-01 14:27:21 +00:00
|
|
|
/*
|
|
|
|
** eventthread.h
|
|
|
|
**
|
|
|
|
** This file is part of mkxp.
|
|
|
|
**
|
|
|
|
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
|
|
|
|
**
|
|
|
|
** mkxp is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 2 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** mkxp is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef EVENTTHREAD_H
|
|
|
|
#define EVENTTHREAD_H
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "etc-internal.h"
|
2014-09-30 07:13:12 +00:00
|
|
|
#include "sdl-util.h"
|
2014-01-25 08:24:55 +00:00
|
|
|
#include "keybindings.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-04 16:48:37 +00:00
|
|
|
#include <SDL_scancode.h>
|
|
|
|
#include <SDL_joystick.h>
|
|
|
|
#include <SDL_mouse.h>
|
|
|
|
#include <SDL_mutex.h>
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
#include <string>
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-10-06 05:05:01 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
struct RGSSThreadData;
|
2014-12-23 19:32:16 +00:00
|
|
|
typedef struct ALCdevice_struct ALCdevice;
|
2013-09-01 14:27:21 +00:00
|
|
|
struct SDL_Window;
|
2015-01-15 07:02:21 +00:00
|
|
|
union SDL_Event;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2015-01-03 18:18:52 +00:00
|
|
|
#define MAX_FINGERS 4
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
class EventThread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct JoyState
|
|
|
|
{
|
2014-12-31 15:39:28 +00:00
|
|
|
int axes[256];
|
|
|
|
uint8_t hats[256];
|
2014-01-25 08:24:55 +00:00
|
|
|
bool buttons[256];
|
2013-09-01 14:27:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct MouseState
|
|
|
|
{
|
|
|
|
int x, y;
|
2014-01-05 00:15:33 +00:00
|
|
|
bool inWindow;
|
2013-10-29 07:54:57 +00:00
|
|
|
bool buttons[32];
|
2013-09-01 14:27:21 +00:00
|
|
|
};
|
|
|
|
|
2015-01-03 18:18:52 +00:00
|
|
|
struct FingerState
|
|
|
|
{
|
|
|
|
bool down;
|
|
|
|
int x, y;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TouchState
|
|
|
|
{
|
|
|
|
FingerState fingers[MAX_FINGERS];
|
|
|
|
};
|
|
|
|
|
|
|
|
static uint8_t keyStates[SDL_NUM_SCANCODES];
|
|
|
|
static JoyState joyState;
|
2013-09-01 14:27:21 +00:00
|
|
|
static MouseState mouseState;
|
2015-01-03 18:18:52 +00:00
|
|
|
static TouchState touchState;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-01-02 01:22:10 +00:00
|
|
|
static bool allocUserEvents();
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
EventThread();
|
|
|
|
|
|
|
|
void process(RGSSThreadData &rtData);
|
|
|
|
void cleanup();
|
|
|
|
|
2013-10-06 05:05:01 +00:00
|
|
|
/* Called from RGSS thread */
|
2013-09-01 14:27:21 +00:00
|
|
|
void requestFullscreenMode(bool mode);
|
|
|
|
void requestWindowResize(int width, int height);
|
2013-09-24 20:52:42 +00:00
|
|
|
void requestShowCursor(bool mode);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
void requestTerminate();
|
|
|
|
|
2013-09-24 20:52:42 +00:00
|
|
|
bool getFullscreen() const;
|
|
|
|
bool getShowCursor() const;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
void showMessageBox(const char *body, int flags = 0);
|
|
|
|
|
2013-10-06 05:05:01 +00:00
|
|
|
/* RGSS thread calls this once per frame */
|
|
|
|
void notifyFrame();
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
private:
|
2015-01-15 07:02:21 +00:00
|
|
|
static int eventFilter(void *, SDL_Event*);
|
|
|
|
|
2013-09-04 10:10:21 +00:00
|
|
|
void resetInputStates();
|
2013-09-01 14:27:21 +00:00
|
|
|
void setFullscreen(SDL_Window *, bool mode);
|
2013-09-24 20:52:42 +00:00
|
|
|
void updateCursorState(bool inWindow);
|
2013-10-06 05:05:01 +00:00
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
bool fullscreen;
|
2013-09-24 20:52:42 +00:00
|
|
|
bool showCursor;
|
2014-08-24 05:36:19 +00:00
|
|
|
AtomicFlag msgBoxDone;
|
2013-10-06 05:05:01 +00:00
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
uint64_t lastFrame;
|
|
|
|
uint64_t displayCounter;
|
2014-12-24 05:23:28 +00:00
|
|
|
AtomicFlag sendUpdates;
|
|
|
|
AtomicFlag immInitFlag;
|
|
|
|
AtomicFlag immFiniFlag;
|
2013-10-06 12:11:50 +00:00
|
|
|
double acc;
|
2013-10-06 05:05:01 +00:00
|
|
|
uint32_t accDiv;
|
|
|
|
} fps;
|
2013-09-01 14:27:21 +00:00
|
|
|
};
|
|
|
|
|
2013-10-06 05:11:03 +00:00
|
|
|
/* Used to asynchronously inform the RGSS thread
|
2015-01-02 00:41:23 +00:00
|
|
|
* about certain value changes */
|
|
|
|
template<typename T>
|
|
|
|
struct UnidirMessage
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2015-01-02 00:41:23 +00:00
|
|
|
UnidirMessage()
|
|
|
|
: mutex(SDL_CreateMutex()),
|
|
|
|
current(T())
|
|
|
|
{}
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2015-01-02 00:41:23 +00:00
|
|
|
~UnidirMessage()
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
SDL_DestroyMutex(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Done from the sending side */
|
2015-01-02 00:41:23 +00:00
|
|
|
void post(const T &value)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
SDL_LockMutex(mutex);
|
|
|
|
|
2014-08-24 05:36:19 +00:00
|
|
|
changed.set();
|
2015-01-02 00:41:23 +00:00
|
|
|
current = value;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
SDL_UnlockMutex(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Done from the receiving side */
|
2015-01-02 00:41:23 +00:00
|
|
|
bool poll(T &out) const
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2014-08-24 05:36:19 +00:00
|
|
|
if (!changed)
|
2013-09-01 14:27:21 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
SDL_LockMutex(mutex);
|
|
|
|
|
2015-01-02 00:41:23 +00:00
|
|
|
out = current;
|
2014-08-24 05:36:19 +00:00
|
|
|
changed.clear();
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
SDL_UnlockMutex(mutex);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-02 00:41:23 +00:00
|
|
|
/* Done from either */
|
|
|
|
void get(T &out) const
|
2014-01-25 08:24:55 +00:00
|
|
|
{
|
2015-01-02 00:41:23 +00:00
|
|
|
SDL_LockMutex(mutex);
|
|
|
|
out = current;
|
|
|
|
SDL_UnlockMutex(mutex);
|
2014-01-25 08:24:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-01-02 00:41:23 +00:00
|
|
|
SDL_mutex *mutex;
|
2014-01-25 08:24:55 +00:00
|
|
|
mutable AtomicFlag changed;
|
2015-01-02 00:41:23 +00:00
|
|
|
T current;
|
2014-01-25 08:24:55 +00:00
|
|
|
};
|
|
|
|
|
2015-01-15 07:02:21 +00:00
|
|
|
struct SyncPoint
|
|
|
|
{
|
|
|
|
/* Used by eventFilter to control sleep/wakeup */
|
|
|
|
void haltThreads();
|
|
|
|
void resumeThreads();
|
|
|
|
|
|
|
|
/* Used by RGSS thread */
|
|
|
|
bool mainSyncLocked();
|
|
|
|
void waitMainSync();
|
|
|
|
|
|
|
|
/* Used by secondary (audio) threads */
|
|
|
|
void passSecondarySync();
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Util
|
|
|
|
{
|
|
|
|
Util();
|
|
|
|
~Util();
|
|
|
|
|
|
|
|
void lock();
|
|
|
|
void unlock(bool multi);
|
|
|
|
void waitForUnlock();
|
|
|
|
|
|
|
|
AtomicFlag locked;
|
|
|
|
SDL_mutex *mut;
|
|
|
|
SDL_cond *cond;
|
|
|
|
};
|
|
|
|
|
|
|
|
Util mainSync;
|
|
|
|
Util reply;
|
|
|
|
Util secondSync;
|
|
|
|
};
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
struct RGSSThreadData
|
|
|
|
{
|
2013-10-06 05:11:03 +00:00
|
|
|
/* Main thread sets this to request RGSS thread to terminate */
|
2014-08-24 05:36:19 +00:00
|
|
|
AtomicFlag rqTerm;
|
2013-10-06 05:11:03 +00:00
|
|
|
/* In response, RGSS thread sets this to confirm
|
2013-09-01 14:27:21 +00:00
|
|
|
* that it received the request and isn't stuck */
|
2014-08-24 05:36:19 +00:00
|
|
|
AtomicFlag rqTermAck;
|
|
|
|
|
|
|
|
/* Set when F12 is pressed */
|
|
|
|
AtomicFlag rqReset;
|
|
|
|
|
|
|
|
/* Set when F12 is released */
|
|
|
|
AtomicFlag rqResetFinish;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
EventThread *ethread;
|
2015-01-02 00:41:23 +00:00
|
|
|
UnidirMessage<Vec2i> windowSizeMsg;
|
|
|
|
UnidirMessage<BDescVec> bindingUpdateMsg;
|
2015-01-15 07:02:21 +00:00
|
|
|
SyncPoint syncPoint;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
const char *argv0;
|
|
|
|
|
|
|
|
SDL_Window *window;
|
2014-12-23 19:32:16 +00:00
|
|
|
ALCdevice *alcDev;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
Vec2 sizeResoRatio;
|
2013-09-23 08:39:16 +00:00
|
|
|
Vec2i screenOffset;
|
2015-01-03 17:48:09 +00:00
|
|
|
const int refreshRate;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
Config config;
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
std::string rgssErrorMsg;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
RGSSThreadData(EventThread *ethread,
|
2014-01-01 23:17:31 +00:00
|
|
|
const char *argv0,
|
|
|
|
SDL_Window *window,
|
2014-12-23 19:32:16 +00:00
|
|
|
ALCdevice *alcDev,
|
2015-01-03 17:48:09 +00:00
|
|
|
int refreshRate,
|
2014-01-01 23:17:31 +00:00
|
|
|
const Config& newconf)
|
2014-08-24 05:36:19 +00:00
|
|
|
: ethread(ethread),
|
2013-09-01 14:27:21 +00:00
|
|
|
argv0(argv0),
|
|
|
|
window(window),
|
2014-12-23 19:32:16 +00:00
|
|
|
alcDev(alcDev),
|
2013-12-20 08:17:15 +00:00
|
|
|
sizeResoRatio(1, 1),
|
2015-01-03 17:48:09 +00:00
|
|
|
refreshRate(refreshRate),
|
2014-04-16 11:48:45 +00:00
|
|
|
config(newconf)
|
2013-09-01 14:27:21 +00:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // EVENTTHREAD_H
|