xref: /haiku/src/preferences/input/KeyboardSettings.cpp (revision 02354704729d38c3b078c696adc1bbbd33cbcf72)
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 
12 #include "KeyboardSettings.h"
13 
14 #include <File.h>
15 #include <FindDirectory.h>
16 #include <Path.h>
17 #include <stdio.h>
18 
19 
20 KeyboardSettings::KeyboardSettings()
21 {
22 	if (get_key_repeat_rate(&fSettings.key_repeat_rate) != B_OK)
23 		fSettings.key_repeat_rate = kb_default_key_repeat_rate;
24 
25 	if (get_key_repeat_delay(&fSettings.key_repeat_delay) != B_OK)
26 		fSettings.key_repeat_delay = kb_default_key_repeat_delay;
27 
28 	fOriginalSettings = fSettings;
29 
30 	BPath path;
31 	BFile file;
32 
33 	status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
34 	if (status == B_OK) {
35 		status = path.Append(kb_settings_file);
36 		if (status == B_OK)
37 			status = file.SetTo(path.Path(), B_READ_ONLY);
38 	}
39 }
40 
41 
42 KeyboardSettings::~KeyboardSettings()
43 {
44 	BPath path;
45 	BFile file;
46 
47 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) < B_OK)
48 		return;
49 
50 	if (path.Append(kb_settings_file) < B_OK)
51 		return;
52 
53 	// be careful: don't create the file if it doesn't already exist
54 	if (file.SetTo(path.Path(), B_WRITE_ONLY) < B_OK)
55 		return;
56 }
57 
58 
59 void
60 KeyboardSettings::SetKeyboardRepeatRate(int32 rate)
61 {
62 	if (set_key_repeat_rate(rate) != B_OK)
63 		fprintf(stderr, "error while set_key_repeat_rate!\n");
64 	fSettings.key_repeat_rate = rate;
65 }
66 
67 
68 void
69 KeyboardSettings::SetKeyboardRepeatDelay(bigtime_t delay)
70 {
71 	if (set_key_repeat_delay(delay) != B_OK)
72 		fprintf(stderr, "error while set_key_repeat_delay!\n");
73 	fSettings.key_repeat_delay = delay;
74 }
75 
76 
77 void
78 KeyboardSettings::Revert()
79 {
80 	SetKeyboardRepeatDelay(fOriginalSettings.key_repeat_delay);
81 	SetKeyboardRepeatRate(fOriginalSettings.key_repeat_rate);
82 }
83 
84 
85 void
86 KeyboardSettings::Defaults()
87 {
88 	SetKeyboardRepeatDelay(kb_default_key_repeat_delay);
89 	SetKeyboardRepeatRate(kb_default_key_repeat_rate);
90 }
91 
92 
93 bool
94 KeyboardSettings::IsDefaultable()
95 {
96 	return fSettings.key_repeat_delay != kb_default_key_repeat_delay
97 		|| fSettings.key_repeat_rate != kb_default_key_repeat_rate;
98 }
99