xref: /haiku/src/servers/input/KeyboardSettings.cpp (revision 1a3518cf757c2da8006753f83962da5935bbc82b)
1 /*
2 ** Copyright 2004-2006, the Haiku project. All rights reserved.
3 ** Distributed under the terms of the MIT License.
4 **
5 ** Authors in chronological order:
6 **  mccall@digitalparadise.co.uk
7 **  Jérôme Duval
8 **  Marcus Overhagen
9 */
10 
11 #include <FindDirectory.h>
12 #include <File.h>
13 #include <Path.h>
14 #include "KeyboardSettings.h"
15 
16 KeyboardSettings::KeyboardSettings()
17 {
18 	BPath path;
19 	BFile file;
20 
21 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) < B_OK)
22 		goto err;
23 	if (path.Append(kb_settings_file) < B_OK)
24 		goto err;
25 	if (file.SetTo(path.Path(), B_READ_ONLY) < B_OK)
26 		goto err;
27 	if (file.Read(&fSettings, sizeof(kb_settings)) != sizeof(kb_settings))
28 		goto err;
29 
30 	return;
31 err:
32 	fSettings.key_repeat_delay = kb_default_key_repeat_delay;
33 	fSettings.key_repeat_rate  = kb_default_key_repeat_rate;
34 }
35 
36 
37 KeyboardSettings::~KeyboardSettings()
38 {
39 }
40 
41 
42 void
43 KeyboardSettings::SetKeyboardRepeatRate(int32 rate)
44 {
45 	fSettings.key_repeat_rate = rate;
46 	Save();
47 }
48 
49 
50 void
51 KeyboardSettings::SetKeyboardRepeatDelay(bigtime_t delay)
52 {
53 	fSettings.key_repeat_delay = delay;
54 	Save();
55 }
56 
57 
58 void
59 KeyboardSettings::Save()
60 {
61 	BPath path;
62 	BFile file;
63 
64 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) < B_OK)
65 		return;
66 	if (path.Append(kb_settings_file) < B_OK)
67 		return;
68 	if (file.SetTo(path.Path(), B_WRITE_ONLY | B_CREATE_FILE) < B_OK)
69 		return;
70 
71 	file.Write(&fSettings, sizeof(kb_settings));
72 }
73