save key mapping to memory!

This commit is contained in:
ManDude 2021-08-16 14:57:15 +01:00
parent 513b0691cb
commit 093526721c
11 changed files with 62 additions and 28 deletions

View file

@ -141,6 +141,23 @@ void input_mode_set(u32 enable) {
}
}
void input_mode_save() {
if (Pad::input_mode_get() == (u64)Pad::InputModeStatus::Enabled) {
lg::error("Can't save controller mapping while mapping controller.");
} else if (Pad::input_mode_get() == (u64)Pad::InputModeStatus::Disabled) {
g_settings.pad_mapping_info_backup = g_settings.pad_mapping_info; // copy to backup
g_settings.pad_mapping_info = Pad::g_input_mode_mapping; // set current mapping
}
}
s64 get_mapped_button(s64 pad, s64 button) {
if (pad < 0 || pad > Pad::CONTROLLER_COUNT || button < 0 || button > 16) {
lg::error("Invalid parameters to get_mapped_button({}, {})", pad, button);
return -1;
}
return (s64)g_settings.pad_mapping_info.pad_mapping[pad][button];
}
int PadIsPressed(Pad::Button button, int port) {
return Pad::IsPressed(g_settings.pad_mapping_info, button, port);
}

View file

@ -72,6 +72,8 @@ void texture_upload_now(const u8* tpage, int mode, u32 s7_ptr);
void texture_relocate(u32 destination, u32 source, u32 format);
void poll_events();
void input_mode_set(u32 enable);
void input_mode_save();
s64 get_mapped_button(s64 pad, s64 button);
int PadIsPressed(Pad::Button button, int port);

View file

@ -717,6 +717,8 @@ void InitMachine_PCPort() {
make_function_symbol_from_c("__pc-texture-relocate", (void*)pc_texture_relocate);
// pad stuff
make_function_symbol_from_c("pc-pad-get-mapped-button", (void*)Gfx::get_mapped_button);
make_function_symbol_from_c("pc-pad-input-map-save!", (void*)Gfx::input_mode_save);
make_function_symbol_from_c("pc-pad-input-mode-set", (void*)Gfx::input_mode_set);
make_function_symbol_from_c("pc-pad-input-mode-get", (void*)Pad::input_mode_get);
make_function_symbol_from_c("pc-pad-input-key-get", (void*)Pad::input_mode_get_key);

View file

@ -22,18 +22,12 @@ std::unordered_map<int, int> g_key_status;
std::unordered_map<int, int> g_buffered_key_status;
// input mode for controller mapping
// this enum is also in pc-pad-utils.gc
enum class InputModeStatus {
Disabled,
Enabled,
Canceled
};
InputModeStatus input_mode = InputModeStatus::Disabled;
u64 input_mode_pad = 0;
u64 input_mode_key = -1;
u64 input_mode_mod = 0;
u64 input_mode_index = 0;
MappingInfo input_mode_mapping;
MappingInfo g_input_mode_mapping;
void ForceClearKeys() {
g_key_status.clear();
@ -54,8 +48,8 @@ void OnKeyPress(int key) {
return;
}
input_mode_key = key;
MapButton(input_mode_mapping, (Button)(input_mode_index++), input_mode_pad, key);
if (input_mode_index > (u64)Button::Max) {
MapButton(g_input_mode_mapping, (Button)(input_mode_index++), input_mode_pad, key);
if (input_mode_index >= (u64)Button::Max) {
ExitInputMode(false);
}
return;

View file

@ -72,6 +72,10 @@ void DefaultMapping(MappingInfo& mapping);
int IsPressed(MappingInfo& mapping, Button button, int pad);
void MapButton(MappingInfo& mapping, Button button, int pad, int key);
// this enum is also in pc-pad-utils.gc
enum class InputModeStatus { Disabled, Enabled, Canceled };
extern MappingInfo g_input_mode_mapping;
void EnterInputMode();
void ExitInputMode(bool);
u64 input_mode_get();

View file

@ -1277,7 +1277,7 @@
)
)
(set! *run* #t)
(let ((new-dproc (make-function-process *4k-dead-pool* *display-pool* process 'display display-loop)))
(let ((new-dproc (make-function-process *4k-dead-pool* *display-pool* process display-loop :name 'display)))
(set! *dproc* (the process (as-process new-dproc)))
)
(cond

View file

@ -32,7 +32,7 @@
(set! (-> *level* border?) #f)
(set! (-> *setting-control* default border-mode) #f)
(stop game-mode)
(let ((v1-3 (make-init-process *target-dead-pool* *target-pool* target 'target init-target cont)))
(let ((v1-3 (make-init-process *target-dead-pool* *target-pool* target init-target cont)))
(if v1-3
(set! *target* (the target (-> v1-3 0 self)))
(set! *target* #f)

View file

@ -272,6 +272,8 @@
(define-extern pc-pad-input-mode-get (function int))
(define-extern pc-pad-input-key-get (function int))
(define-extern pc-pad-input-index-get (function int))
(define-extern pc-pad-input-map-save! (function none))
(define-extern pc-pad-get-mapped-button (function int int int))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; vm functions

View file

@ -415,7 +415,9 @@
)
(defmacro ppointer->handle (pproc)
`(new 'static 'handle :process ,pproc :pid (-> ,pproc 0 pid))
`(let ((the-process ,pproc))
(new 'static 'handle :process the-process :pid (-> the-process 0 pid))
)
)
(defmacro process->handle (proc)

View file

@ -80,14 +80,14 @@ There are several ways to "go"
)
)
(defmacro make-function-process (dead-pool new-pool proc-type proc-name func &key (stack-size #x4000) &key (stack *kernel-dram-stack*) &rest args)
(defmacro make-function-process (dead-pool new-pool proc-type func &key (name #f) &key (stack-size #x4000) &key (stack *kernel-dram-stack*) &rest args)
"Start a new process that runs a function on its main thread.
Returns a pointer to the new process (or #f? on error)."
(with-gensyms (new-proc)
`(let ((,new-proc (the-as ,proc-type (get-process ,dead-pool ,proc-type ,stack-size))))
(when ,new-proc
((method-of-type ,proc-type activate) ,new-proc ,new-pool ,proc-name ,stack)
((method-of-type ,proc-type activate) ,new-proc ,new-pool ,(if name name `(quote ,proc-type)) ,stack)
(run-next-time-in-process ,new-proc ,func ,@args)
(-> ,new-proc ppointer)
)
@ -95,14 +95,14 @@ There are several ways to "go"
)
)
(defmacro make-init-process (dead-pool new-pool proc-type proc-name func &key (stack-size #x4000) &key (stack *kernel-dram-stack*) &rest args)
(defmacro make-init-process (dead-pool new-pool proc-type func &key (name #f) &key (stack-size #x4000) &key (stack *kernel-dram-stack*) &rest args)
"Start a new process and run an init function on it.
Returns a pointer to the new process, or #f (or is it 0?) if something goes wrong."
(with-gensyms (new-proc)
`(let ((,new-proc (the-as ,proc-type (get-process ,dead-pool ,proc-type ,stack-size))))
(when ,new-proc
((method-of-type ,proc-type activate) ,new-proc ,new-pool ,proc-name ,stack)
((method-of-type ,proc-type activate) ,new-proc ,new-pool ,(if name name `(quote ,proc-type)) ,stack)
(run-now-in-process ,new-proc ,func ,@args)
(-> ,new-proc ppointer)
)

View file

@ -19,7 +19,8 @@
(cond (*debug-segment*
(deftype pc-pad-proc-list (basic)
;; a structure to hold the handles used in this file
(deftype pc-pad-proc-list (structure)
((show handle)
(input handle)
)
@ -28,6 +29,7 @@
(set! (-> *pc-pad-proc-list* show) (the handle #f))
(set! (-> *pc-pad-proc-list* input) (the handle #f))
;; a pc pad process
(deftype pc-pad-proc (process)
((state-time seconds)
(input-index uint64)
@ -69,20 +71,16 @@
(if (not (handle->process (-> *pc-pad-proc-list* show)))
(let ((procp
(make-function-process *nk-dead-pool* *active-pool* pc-pad-proc 'pc-pad-show
(make-function-process *nk-dead-pool* *active-pool* pc-pad-proc :name 'pc-pad-show
(lambda :behavior pc-pad-proc ()
(stack-size-set! (-> self main-thread) 512)
(loop
(let ((cpad (-> *cpad-list* cpads 0)))
(format *stdcon* "~3Lcpad 0~%~0L")
(format *stdcon* "~Tbutton0: ~X~%" (-> cpad button0))
(format *stdcon* "~Tbutton0-abs 0: ~X~%" (-> cpad button0-abs 0))
(format *stdcon* "~Tbutton0-abs 1: ~X~%" (-> cpad button0-abs 1))
(format *stdcon* "~Tbutton0-abs 2: ~X~%" (-> cpad button0-abs 2))
(format *stdcon* "~Tbutton0-rel 0: ~X~%" (-> cpad button0-rel 0))
(format *stdcon* "~Tbutton0-rel 1: ~X~%" (-> cpad button0-rel 1))
(format *stdcon* "~Tbutton0-rel 2: ~X~%" (-> cpad button0-rel 2))
(dotimes (ii 2)
(format *stdcon* "~3Lcpad ~D~0L~%" ii)
(dotimes (i 16)
(format *stdcon* " ~S: ~32L~D~0L~%" (-> *pc-pad-button-names* i) (pc-pad-get-mapped-button ii i))
)
)
(suspend)
)
)
@ -151,7 +149,20 @@
(defstate pc-pi-done (pc-pad-proc)
:enter (behavior () (set! (-> self input-index) 16))
:code (behavior ()
(pc-pad-input-map-save!)
(set-state-time)
(until (time-passed? PC_PAD_INPUT_NOTICE_TIME)
(with-dma-buffer-add-bucket ((buf (current-display-frame debug-buf))
(current-display-frame bucket-group)
(bucket-id debug-draw1))
(draw-string-xy (string-format "MAPPED ~D TO ~S!"
(pc-pad-input-key-get) (-> *pc-pad-button-names* (1- (-> self input-index))))
buf 256 96 (font-color green) (font-flags shadow kerning large middle))
)
(suspend)
)
(set-state-time)
(until (time-passed? PC_PAD_INPUT_NOTICE_TIME)
(with-dma-buffer-add-bucket ((buf (current-display-frame debug-buf))
@ -214,7 +225,7 @@
(if (not (handle->process (-> *pc-pad-proc-list* input)))
(let ((procp
(make-init-process *nk-dead-pool* *active-pool* pc-pad-proc 'pc-pad-input
(make-init-process *nk-dead-pool* *active-pool* pc-pad-proc :name 'pc-pad-input
(lambda :behavior pc-pad-proc ()
(pc-pad-input-mode-set #t)
(go pc-pi-mapping-button)