diff --git a/include/core/controllerframe.hpp b/include/core/controllerframe.hpp index 30f2bc3b..c69dc765 100644 --- a/include/core/controllerframe.hpp +++ b/include/core/controllerframe.hpp @@ -214,4 +214,63 @@ private: std::vector _autofire; }; +extern const char* button_symbols; +extern const char* controller_names[]; + +/** + * Generic port display function. + */ +template +inline void generic_port_display(const unsigned char* buffer, unsigned idx, char* buf) throw() +{ + if(idx > controllers) { + buf[0] = '\0'; + return; + } + size_t ptr = 0; + for(unsigned i = 0; i < analog_axis; i++) { + uint16_t a = buffer[2 * idx * analog_axis + 2 * i]; + uint16_t b = buffer[2 * idx * analog_axis + 2 * i + 1]; + ptr += sprintf(buf + ptr, "%i ", static_cast(256 * a + b)); + } + for(unsigned i = 0; i < buttons; i++) { + size_t bit = 16 * controllers * analog_axis + idx * buttons + i; + buf[ptr++] = ((buffer[bit / 8] & (1 << (bit % 8))) != 0) ? button_symbols[i + sidx] : '-'; + } + buf[ptr] = '\0'; +} + +/** + * Generic port controller name function. + */ +template +inline const char* generic_controller_name(unsigned controller) +{ + if(controller >= controllers) + return NULL; + return controller_names[nameindex]; +} + +/** + * Generic port serialization function. + */ +template +inline size_t generic_port_serialize(const unsigned char* buffer, char* textbuf) throw() +{ + size_t ptr = 0; + for(unsigned j = 0; j < controllers; j++) { + textbuf[ptr++] = '|'; + for(unsigned i = 0; i < buttons; i++) { + size_t bit = 16 * controllers * analog_axis + j * buttons + i; + textbuf[ptr++] = ((buffer[bit / 8] & (1 << (bit % 8))) != 0) ? button_symbols[i + sidx] : '.'; + } + for(unsigned i = 0; i < analog_axis; i++) { + uint16_t a = buffer[2 * j * analog_axis + 2 * i]; + uint16_t b = buffer[2 * j * analog_axis + 2 * i + 1]; + ptr += sprintf(textbuf + ptr, " %i", static_cast(256 * a + b)); + } + } + return ptr; +} + #endif diff --git a/include/library/controller-data.hpp b/include/library/controller-data.hpp index 93b627be..5a5e8af8 100644 --- a/include/library/controller-data.hpp +++ b/include/library/controller-data.hpp @@ -1325,32 +1325,6 @@ inline short generic_port_read(const unsigned char* buffer, unsigned idx, unsign return 0; } -extern const char* button_symbols; -extern const char* controller_names[]; - -/** - * Generic port display function. - */ -template -inline void generic_port_display(const unsigned char* buffer, unsigned idx, char* buf) throw() -{ - if(idx > controllers) { - buf[0] = '\0'; - return; - } - size_t ptr = 0; - for(unsigned i = 0; i < analog_axis; i++) { - uint16_t a = buffer[2 * idx * analog_axis + 2 * i]; - uint16_t b = buffer[2 * idx * analog_axis + 2 * i + 1]; - ptr += sprintf(buf + ptr, "%i ", static_cast(256 * a + b)); - } - for(unsigned i = 0; i < buttons; i++) { - size_t bit = 16 * controllers * analog_axis + idx * buttons + i; - buf[ptr++] = ((buffer[bit / 8] & (1 << (bit % 8))) != 0) ? button_symbols[i + sidx] : '-'; - } - buf[ptr] = '\0'; -} - /** * Generic port control index function. */ @@ -1362,38 +1336,6 @@ inline unsigned generic_used_indices(unsigned controller) return indices; } -/** - * Generic port controller name function. - */ -template -inline const char* generic_controller_name(unsigned controller) -{ - if(controller >= controllers) - return NULL; - return controller_names[nameindex]; -} - -/** - * Generic port serialization function. - */ -template -inline size_t generic_port_serialize(const unsigned char* buffer, char* textbuf) throw() -{ - size_t ptr = 0; - for(unsigned j = 0; j < controllers; j++) { - textbuf[ptr++] = '|'; - for(unsigned i = 0; i < buttons; i++) { - size_t bit = 16 * controllers * analog_axis + j * buttons + i; - textbuf[ptr++] = ((buffer[bit / 8] & (1 << (bit % 8))) != 0) ? button_symbols[i + sidx] : '.'; - } - for(unsigned i = 0; i < analog_axis; i++) { - uint16_t a = buffer[2 * j * analog_axis + 2 * i]; - uint16_t b = buffer[2 * j * analog_axis + 2 * i + 1]; - ptr += sprintf(textbuf + ptr, " %i", static_cast(256 * a + b)); - } - } - return ptr; -} /** * Generic port size function. diff --git a/src/library/controller-data.cpp b/src/library/controller-data.cpp index 6da7b3ed..fc01c492 100644 --- a/src/library/controller-data.cpp +++ b/src/library/controller-data.cpp @@ -28,7 +28,32 @@ namespace i.indices[0].control = 0; return i; } - + + inline size_t invalid_serialize(const unsigned char* buffer, char* textbuf) throw() + { + return 0; + } + + inline void invalid_display(const unsigned char* buffer, unsigned idx, char* buf) + { + *buf = 0; + } + + inline void basecontrol_display(const unsigned char* buffer, unsigned idx, char* buf) + { + if(idx) + *buf = 0; + else { + buf[0] = (buffer[0] & 1) ? 'F' : '-'; + buf[1] = 0; + } + } + + inline const char* invalid_controller_name(unsigned c) + { + return c ? NULL : "(system)"; + } + struct port_type_group invalid_group; struct porttype_invalid : public port_type { @@ -36,13 +61,13 @@ namespace { write = generic_port_write<0, 0, 0>; read = generic_port_read<0, 0, 0>; - display = generic_port_display<0, 0, 0, 0>; - serialize = generic_port_serialize<0, 0, 0, 0>; + display = invalid_display; + serialize = invalid_serialize; deserialize = generic_port_deserialize<0, 0, 0>; legal = generic_port_legal<0xFFFFFFFFU>; deviceflags = generic_port_deviceflags<0, 0>; used_indices = generic_used_indices<0, 0>; - controller_name = generic_controller_name<0, 0>; + controller_name = invalid_controller_name; button_id = button_id_illegal; construct_map = invalid_construct_map; controllers = 0; @@ -62,7 +87,7 @@ namespace { write = generic_port_write<1, 0, 1>; read = generic_port_read<1, 0, 1>; - display = generic_port_display<0, 0, 0, 0>; + display = basecontrol_display; serialize = basecontrol_serialize; deserialize = generic_port_deserialize<1, 0, 1>; legal = generic_port_legal<0>; @@ -70,7 +95,7 @@ namespace button_id = button_id_illegal; construct_map = invalid_construct_map; used_indices = generic_used_indices<1, 1>; - controller_name = generic_controller_name<1, 0>; + controller_name = invalid_controller_name; controllers = 1; set_core_controller = set_core_controller_illegal; }