Pack movie data in memory
[lsnes.git] / src / core / controller_justifier.cpp
blob77429bb9b3a7ac453ec8bc419cc2452801c4659e
1 #include "lsnes.hpp"
2 #include <snes/snes.hpp>
3 #include <ui-libsnes/libsnes.hpp>
5 #include "core/controllerframe.hpp"
7 namespace
9 struct porttype_justifier : public porttype_info
11 porttype_justifier() : porttype_info(PT_JUSTIFIER, "justifier", 5) {}
12 void write(unsigned char* buffer, unsigned idx, unsigned ctrl, short x) const throw()
14 if(ctrl >= 4 || idx > 0)
15 return;
16 switch(ctrl) {
17 case 0:
18 case 1:
19 serialize_short(buffer + 2 * ctrl + 1, x);
20 break;
21 case 2:
22 case 3:
23 if(x)
24 buffer[0] |= (1 << (ctrl - 2));
25 else
26 buffer[0] &= ~(1 << (ctrl - 2));
27 break;
31 short read(const unsigned char* buffer, unsigned idx, unsigned ctrl) const throw()
33 if(ctrl >= 4 || idx > 0)
34 return 0;
35 switch(ctrl) {
36 case 0:
37 case 1:
38 return unserialize_short(buffer + 2 * ctrl + 1);
39 case 2:
40 case 3:
41 return ((buffer[0] & (1 << (ctrl - 2))) != 0);
45 void display(const unsigned char* buffer, unsigned idx, char* buf) const throw()
47 if(idx > 0) {
48 buf[0] = '\0';
49 return;
51 sprintf(buf, "%i %i %c%c", unserialize_short(buffer + 1), unserialize_short(buffer + 3),
52 ((buffer[0] & 1) ? 'T' : '-'), ((buffer[0] & 2) ? 'S' : '-'));
55 size_t serialize(const unsigned char* buffer, char* textbuf) const throw()
57 char tmp[128];
58 sprintf(tmp, "|%c%c %i %i", ((buffer[0] & 1) ? 'T' : '.'), ((buffer[0] & 2) ? 'S' : '.'),
59 unserialize_short(buffer + 1), unserialize_short(buffer + 3));
60 size_t len = strlen(tmp);
61 memcpy(textbuf, tmp, len);
62 return len;
65 size_t deserialize(unsigned char* buffer, const char* textbuf) const throw()
67 buffer[0] = 0;
68 size_t idx = 0;
69 if(read_button_value(textbuf, idx))
70 buffer[0] |= 1;
71 if(read_button_value(textbuf, idx))
72 buffer[0] |= 2;
73 serialize_short(buffer + 1, read_axis_value(textbuf, idx));
74 serialize_short(buffer + 3, read_axis_value(textbuf, idx));
75 skip_rest_of_field(textbuf, idx, false);
76 return idx;
79 devicetype_t devicetype(unsigned idx) const throw()
81 return (idx == 0) ? DT_JUSTIFIER : DT_NONE;
84 unsigned controllers() const throw()
86 return 1;
89 unsigned internal_type() const throw()
91 return SNES_DEVICE_JUSTIFIER;
94 bool legal(unsigned port) const throw()
96 return (port > 0);
98 } justifier;