2 * Copyright (c) 2004 Greg Parker. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY GREG PARKER ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 // There is one global default prefs list.
30 // There is also a set of override values for each connection, mostly
31 // for terminal settings.
33 // Each preference is a RESOURCE
34 // res type: 'dflt' or some ID for a connection override
44 scrollback line count (500?)
45 scroll down on input (NO)
46 scroll down on keypress (YES)
49 compression (NO) (fixme implement)
50 ciphers ("aes128-cbc,3des-cbc")
51 authmethod (fixme implement)
54 wrap_mode (YES==autowrap)
55 dec_om "DEC Origin Mode" (YES)
56 lfhascr "Implicit CR in every LF" (NO)
57 bce "Background Color Erase" (?)
58 blink_text "Blink" (NO)
59 answerback "Answerback to ^E" ("pssh")
65 bksp_is_delete "Backspace is DEL" (YES==^? rather than ^H)
66 rxvt_homeend "rxvt Home and End" (NO)
67 funky_type "Function Keys" (DIGITAL, Linux, xterm, VT400, VT100+, SCO)
68 app_cursor "Application Cursor Keys" (NO?)
69 app_keypad "Application Keypad" (NO?)
70 nethack "NetHack Keypad" (NO)
71 compose (NO) (use real compose key on keyboard?)
79 #define noRecord ((UInt16)0xffff)
82 static DmOpenRef PrefsDB = 0;
83 // fixme implement overrides
85 extern DmOpenRef OpenDB(UInt32 type, char *name, Boolean resDB, Boolean create);
89 PrefsDB = OpenDB('Pref', "pssh Preferences", true, true);
90 return (PrefsDB != 0);
94 static MemHandle PrefsGetHandle(uint32_t which, Boolean fallback)
98 // fixme search override here unless fallback==false
100 // search default prefs
101 index = DmFindResource(PrefsDB, 'dflt', which, NULL);
102 if (index == noRecord) return NULL;
104 return DmGetResourceIndex(PrefsDB, index);
108 uint32_t PrefsGetInt(uint32_t which, uint32_t defaultValue)
112 MemHandle h = PrefsGetHandle(which, true);
113 if (!h) return defaultValue;
114 if (MemHandleSize(h) != 4) return defaultValue;
116 p = MemHandleLock(h);
119 DmReleaseResource(h);
125 void PrefsPutInt(uint32_t which, uint32_t value)
128 MemHandle h = PrefsGetHandle(which, false);
129 if (h && MemHandleSize(h) == sizeof(value)) {
130 // resource exists, and it's the right size
131 p = MemHandleLock(h);
132 DmWrite(p, 0, &value, sizeof(value));
134 DmReleaseResource(h);
138 // resource exists, but its the wrong size
139 h = DmResizeResource(h, sizeof(value));
140 p = MemHandleLock(h);
141 DmWrite(p, 0, &value, sizeof(value));
143 DmReleaseResource(h);
146 // resource does not exist
147 h = DmNewResource(PrefsDB, 'dflt', which, sizeof(value));
148 p = MemHandleLock(h);
149 DmWrite(p, 0, &value, sizeof(value));
151 DmReleaseResource(h);
156 char *PrefsGetString(uint32_t which, char *defaultValue)
161 MemHandle h = PrefsGetHandle(which, true);
162 if (!h) return arena_strdup(defaultValue);
163 size = MemHandleSize(h);
165 p = MemHandleLock(h);
166 result = arena_malloc(size + 1); // be paranoid about nul terminator
167 memcpy(result, p, size);
170 DmReleaseResource(h);
176 void PrefsPutString(uint32_t which, char *value)
179 size_t sizeNeeded = strlen(value)+1;
180 MemHandle h = PrefsGetHandle(which, false);
181 if (h && MemHandleSize(h) >= sizeNeeded) {
182 // resource exists, and it's big enough
183 p = MemHandleLock(h);
184 DmWrite(p, 0, value, sizeNeeded);
186 DmReleaseResource(h);
190 // resource exists, but it's the wrong size
191 h = DmResizeResource(h, sizeNeeded);
192 p = MemHandleLock(h);
193 DmWrite(p, 0, value, sizeNeeded);
195 DmReleaseResource(h);
198 // resource does not exist
199 h = DmNewResource(PrefsDB, 'dflt', which, sizeNeeded);
200 p = MemHandleLock(h);
201 DmWrite(p, 0, value, sizeNeeded);
203 DmReleaseResource(h);