vfs: check userland buffers before reading them.
[haiku.git] / src / kits / device / JoystickTweaker.cpp
blob6defb15d213ec534bbc1269614c7ac41242940f4
1 /*
2 * Copyright 2008, Haiku.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Fredrik Modeen
8 */
9 #include "JoystickTweaker.h"
11 #include <new>
12 #include <stdio.h>
13 #include <stdlib.h>
15 #include <Debug.h>
16 #include <Directory.h>
17 #include <Joystick.h>
18 #include <Path.h>
19 #include <String.h>
20 #include <UTF8.h>
23 #define STACK_STRING_BUFFER_SIZE 64
26 #if DEBUG
27 inline void
28 LOG(const char *fmt, ...)
30 char buf[1024];
31 va_list ap;
32 va_start(ap, fmt);
33 vsprintf(buf, fmt, ap);
34 va_end(ap);
36 fputs(buf, _BJoystickTweaker::sLogFile);
37 fflush(_BJoystickTweaker::sLogFile);
39 # define LOG_ERR(text...) LOG(text)
40 FILE *_BJoystickTweaker::sLogFile = NULL;
41 #else
42 # define LOG(text...)
43 # define LOG_ERR(text...) fprintf(stderr, text)
44 #endif
46 #define CALLED() LOG("%s\n", __PRETTY_FUNCTION__)
49 _BJoystickTweaker::_BJoystickTweaker()
51 #if DEBUG
52 sLogFile = fopen("/var/log/joystick.log", "a");
53 #endif
54 CALLED();
58 _BJoystickTweaker::_BJoystickTweaker(BJoystick &stick)
60 #if DEBUG
61 sLogFile = fopen("/var/log/joystick.log", "a");
62 #endif
63 CALLED();
65 fJoystick = &stick;
69 _BJoystickTweaker::~_BJoystickTweaker()
74 status_t
75 _BJoystickTweaker::save_config(const entry_ref *ref)
77 CALLED();
78 return B_ERROR;
82 status_t
83 _BJoystickTweaker::_ScanIncludingDisabled(const char *rootPath, BList *list,
84 BEntry *rootEntry)
86 BDirectory root;
88 if (rootEntry != NULL)
89 root.SetTo(rootEntry);
90 else if (rootPath != NULL)
91 root.SetTo(rootPath);
92 else
93 return B_ERROR;
95 BEntry entry;
97 ASSERT(list != NULL);
98 while (root.GetNextEntry(&entry) == B_OK) {
99 if (entry.IsDirectory()) {
100 status_t result = _ScanIncludingDisabled(rootPath, list, &entry);
101 if (result != B_OK)
102 return result;
104 continue;
107 BPath path;
108 status_t result = entry.GetPath(&path);
109 if (result != B_OK)
110 return result;
112 BString *deviceName = new(std::nothrow) BString(path.Path());
113 if (deviceName == NULL)
114 return B_NO_MEMORY;
116 deviceName->RemoveFirst(rootPath);
117 if (!list->AddItem(deviceName)) {
118 delete deviceName;
119 return B_ERROR;
123 return B_OK;
127 void
128 _BJoystickTweaker::scan_including_disabled()
130 CALLED();
131 _EmpyList(fJoystick->fDevices);
132 _ScanIncludingDisabled(DEVICE_BASE_PATH, fJoystick->fDevices);
136 void
137 _BJoystickTweaker::_EmpyList(BList *list)
139 for (int32 i = 0; i < list->CountItems(); i++)
140 delete (BString *)list->ItemAt(i);
142 list->MakeEmpty();
146 status_t
147 _BJoystickTweaker::get_info()
149 CALLED();
150 return B_ERROR;
154 status_t
155 _BJoystickTweaker::GetInfo(_joystick_info *info, const char *ref)
157 CALLED();
158 BString configFilePath(JOYSTICK_CONFIG_BASE_PATH);
159 configFilePath.Append(ref);
161 FILE *file = fopen(configFilePath.String(), "r");
162 if (file == NULL)
163 return B_ERROR;
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);
174 fclose(file);
175 return B_OK;
179 void
180 _BJoystickTweaker::_BuildFromJoystickDesc(char *string, _joystick_info *info)
182 BString str(string);
183 str.RemoveAll("\"");
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());
205 } else {
206 LOG("Path = %s\n", str.String());
211 status_t
212 _BJoystickTweaker::SendIOCT(uint32 op)
214 switch (op) {
215 case B_JOYSTICK_SET_DEVICE_MODULE:
216 break;
218 case B_JOYSTICK_GET_DEVICE_MODULE:
219 break;
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:
226 default:
227 break;
230 return B_ERROR;