2 * Copyright 2008, Haiku.
3 * Distributed under the terms of the MIT License.
9 #include "JoystickTweaker.h"
16 #include <Directory.h>
23 #define STACK_STRING_BUFFER_SIZE 64
28 LOG(const char *fmt
, ...)
33 vsprintf(buf
, fmt
, ap
);
36 fputs(buf
, _BJoystickTweaker::sLogFile
);
37 fflush(_BJoystickTweaker::sLogFile
);
39 # define LOG_ERR(text...) LOG(text)
40 FILE *_BJoystickTweaker::sLogFile
= NULL
;
43 # define LOG_ERR(text...) fprintf(stderr, text)
46 #define CALLED() LOG("%s\n", __PRETTY_FUNCTION__)
49 _BJoystickTweaker::_BJoystickTweaker()
52 sLogFile
= fopen("/var/log/joystick.log", "a");
58 _BJoystickTweaker::_BJoystickTweaker(BJoystick
&stick
)
61 sLogFile
= fopen("/var/log/joystick.log", "a");
69 _BJoystickTweaker::~_BJoystickTweaker()
75 _BJoystickTweaker::save_config(const entry_ref
*ref
)
83 _BJoystickTweaker::_ScanIncludingDisabled(const char *rootPath
, BList
*list
,
88 if (rootEntry
!= NULL
)
89 root
.SetTo(rootEntry
);
90 else if (rootPath
!= NULL
)
98 while (root
.GetNextEntry(&entry
) == B_OK
) {
99 if (entry
.IsDirectory()) {
100 status_t result
= _ScanIncludingDisabled(rootPath
, list
, &entry
);
108 status_t result
= entry
.GetPath(&path
);
112 BString
*deviceName
= new(std::nothrow
) BString(path
.Path());
113 if (deviceName
== NULL
)
116 deviceName
->RemoveFirst(rootPath
);
117 if (!list
->AddItem(deviceName
)) {
128 _BJoystickTweaker::scan_including_disabled()
131 _EmpyList(fJoystick
->fDevices
);
132 _ScanIncludingDisabled(DEVICE_BASE_PATH
, fJoystick
->fDevices
);
137 _BJoystickTweaker::_EmpyList(BList
*list
)
139 for (int32 i
= 0; i
< list
->CountItems(); i
++)
140 delete (BString
*)list
->ItemAt(i
);
147 _BJoystickTweaker::get_info()
155 _BJoystickTweaker::GetInfo(_joystick_info
*info
, const char *ref
)
158 BString
configFilePath(JOYSTICK_CONFIG_BASE_PATH
);
159 configFilePath
.Append(ref
);
161 FILE *file
= fopen(configFilePath
.String(), "r");
165 char line
[STACK_STRING_BUFFER_SIZE
];
166 while (fgets(line
, sizeof(line
), file
) != NULL
) {
167 int length
= strlen(line
);
168 if (length
> 0 && line
[length
- 1] == '\n')
169 line
[length
- 1] = '\0';
171 _BuildFromJoystickDesc(line
, info
);
180 _BJoystickTweaker::_BuildFromJoystickDesc(char *string
, _joystick_info
*info
)
185 if (str
.IFindFirst("module") != -1) {
186 str
.RemoveFirst("module = ");
187 strlcpy(info
->module_info
.module_name
, str
.String(),
188 STACK_STRING_BUFFER_SIZE
);
189 } else if (str
.IFindFirst("gadget") != -1) {
190 str
.RemoveFirst("gadget = ");
191 strlcpy(info
->module_info
.device_name
, str
.String(),
192 STACK_STRING_BUFFER_SIZE
);
193 } else if (str
.IFindFirst("num_axes") != -1) {
194 str
.RemoveFirst("num_axes = ");
195 info
->module_info
.num_axes
= atoi(str
.String());
196 } else if (str
.IFindFirst("num_hats") != -1) {
197 str
.RemoveFirst("num_hats = ");
198 info
->module_info
.num_hats
= atoi(str
.String());
199 } else if (str
.IFindFirst("num_buttons") != -1) {
200 str
.RemoveFirst("num_buttons = ");
201 info
->module_info
.num_buttons
= atoi(str
.String());
202 } else if (str
.IFindFirst("num_sticks") != -1) {
203 str
.RemoveFirst("num_sticks = ");
204 info
->module_info
.num_sticks
= atoi(str
.String());
206 LOG("Path = %s\n", str
.String());
212 _BJoystickTweaker::SendIOCT(uint32 op
)
215 case B_JOYSTICK_SET_DEVICE_MODULE
:
218 case B_JOYSTICK_GET_DEVICE_MODULE
:
221 case B_JOYSTICK_GET_SPEED_COMPENSATION
:
222 case B_JOYSTICK_SET_SPEED_COMPENSATION
:
223 case B_JOYSTICK_GET_MAX_LATENCY
:
224 case B_JOYSTICK_SET_MAX_LATENCY
:
225 case B_JOYSTICK_SET_RAW_MODE
: