xref: /haiku/src/kits/device/JoystickTweaker.cpp (revision e9c4d47ad719d6fd67cd9b75b41ebbec563e7a79)
1 /*
2  * Copyright 2008, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Fredrik Modeen
7  *
8  */
9 #include "JoystickTweaker.h"
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 
14 #include <Path.h>
15 #include <Directory.h>
16 #include <String.h>
17 #include <Debug.h>
18 
19 #include "Joystick.h"
20 
21 #define STRINGLENGTHCPY 64
22 
23 #include <UTF8.h>
24 
25 #if DEBUG
26 inline void
27 LOG(const char *fmt, ...)
28 {
29 	char buf[1024];
30 	va_list ap;
31 	va_start(ap, fmt);
32 	vsprintf(buf, fmt, ap);
33 	va_end(ap);
34 	fputs(buf, BJoystick::sLogFile); fflush(BJoystick::sLogFile);
35 }
36 #	define LOG_ERR(text...) LOG(text)
37 FILE *BJoystick::sLogFile = NULL;
38 #else
39 #	define LOG(text...)
40 #	define LOG_ERR(text...) fprintf(stderr, text)
41 #endif
42 
43 #define CALLED() LOG("%s\n", __PRETTY_FUNCTION__)
44 
45 _BJoystickTweaker::_BJoystickTweaker()
46 {
47 	CALLED();
48 #if DEBUG
49 	sLogFile = fopen("/var/log/libdevice.log", "a");
50 #endif
51 }
52 
53 
54 _BJoystickTweaker::_BJoystickTweaker(BJoystick &stick)
55 {
56 	CALLED();
57 #if DEBUG
58 	sLogFile = fopen("/var/log/libdevice.log", "a");
59 #endif
60 
61 	fJoystick = &stick;
62 }
63 
64 
65 _BJoystickTweaker::~_BJoystickTweaker()
66 {
67 }
68 
69 
70 status_t
71 _BJoystickTweaker::save_config(const entry_ref *ref)
72 {
73 	CALLED();
74 	return B_ERROR;
75 }
76 
77 
78 status_t
79 _BJoystickTweaker::_ScanIncludingDisabled(const char* rootPath, BList *list,
80 	BEntry *rootEntry)
81 {
82 	BDirectory root;
83 
84 	if (rootEntry != NULL)
85 		root.SetTo( rootEntry);
86 	else if (rootPath != NULL)
87 		root.SetTo(rootPath);
88 	else
89 		return B_ERROR;
90 
91 	BEntry entry;
92 
93 	ASSERT(list != NULL);
94 	while ((root.GetNextEntry(&entry)) > B_ERROR ) {
95 		if (entry.IsDirectory()) {
96 			_ScanIncludingDisabled(rootPath, list, &entry);
97 		} else {
98 			BPath path;
99 			entry.GetPath(&path);
100 
101 			BString *str = new BString(path.Path());
102 			str->RemoveFirst(rootPath);
103 			list->AddItem(str);
104 		}
105 	}
106 	return B_OK;
107 }
108 
109 
110 void
111 _BJoystickTweaker::scan_including_disabled()
112 {
113 	CALLED();
114 	// First, we empty the list
115 	_EmpyList(fJoystick->fDevices);
116 	_ScanIncludingDisabled(DEVICEPATH, fJoystick->fDevices);
117 }
118 
119 
120 void
121 _BJoystickTweaker::_EmpyList(BList *list)
122 {
123 	for (int32 count = list->CountItems() - 1; count >= 0; count--)
124 		free(list->RemoveItem(count));
125 }
126 
127 
128 status_t
129 _BJoystickTweaker::get_info()
130 {
131 	CALLED();
132 	return B_ERROR;
133 }
134 
135 
136 status_t
137 _BJoystickTweaker::GetInfo(_joystick_info* info,
138 						const char * ref)
139 {
140 	CALLED();
141 	status_t err = B_ERROR;
142 	BString str(JOYSTICKPATH);
143 	str.Append(ref);
144 
145 	FILE *file = fopen(str.String(), "r");
146 	if (file != NULL) {
147 		char line [STRINGLENGTHCPY];
148 		while (fgets ( line, sizeof line, file ) != NULL ) {
149 			int len = strlen(line);
150     		if (len > 0 && line[len-1] == '\n')
151         		line[len-1] = '\0';
152 			_BuildFromJoystickDesc(line, info);
153 		}
154 		fclose(file);
155 	}
156 
157 	err = B_OK;
158 	return err;
159 }
160 
161 
162 void
163 _BJoystickTweaker::_BuildFromJoystickDesc(char *string, _joystick_info* info)
164 {
165 	BString str(string);
166 	str.RemoveAll("\"");
167 
168 	if (str.IFindFirst("module") != -1) {
169 		str.RemoveFirst("module = ");
170 		strncpy(info->module_name, str.String(), STRINGLENGTHCPY);
171 	} else if (str.IFindFirst("gadget") != -1) {
172 		str.RemoveFirst("gadget = ");
173 		strncpy(info->controller_name, str.String(), STRINGLENGTHCPY);
174 	} else if (str.IFindFirst("num_axes") != -1) {
175 		str.RemoveFirst("num_axes = ");
176 		info->num_axes = atoi(str.String());
177 	} else if (str.IFindFirst("num_hats") != -1) {
178 		str.RemoveFirst("num_hats = ");
179 		info->num_hats = atoi(str.String());
180 	} else if (str.IFindFirst("num_buttons") != -1) {
181 		str.RemoveFirst("num_buttons = ");
182 		info->num_buttons = atoi(str.String());
183 	} else if (str.IFindFirst("num_sticks") != -1) {
184 		str.RemoveFirst("num_sticks = ");
185 		info->num_sticks = atoi(str.String());
186 	} else {
187 		LOG("Path = %s\n", str->String());
188 	}
189 }
190 
191 
192 status_t
193 _BJoystickTweaker::SendIOCT(uint32 op)
194 {
195 	status_t err = B_ERROR;
196 	switch (op) {
197 		case B_JOYSTICK_SET_DEVICE_MODULE:
198 				break;
199 
200 		case B_JOYSTICK_GET_DEVICE_MODULE:
201 				break;
202 
203 		case B_JOYSTICK_GET_SPEED_COMPENSATION:
204 		case B_JOYSTICK_SET_SPEED_COMPENSATION:
205 		case B_JOYSTICK_GET_MAX_LATENCY:
206 		case B_JOYSTICK_SET_MAX_LATENCY:
207 		case B_JOYSTICK_SET_RAW_MODE:
208 		default:
209 				break;
210 	}
211 	return err;
212 }
213