2 * FreeRDP: A Remote Desktop Protocol Client
5 * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 #include <freerdp/utils/file.h>
22 #include <freerdp/utils/registry.h>
24 static const char registry_dir[] = "freerdp";
25 static const char registry_file[] = "config.txt";
27 static REG_SECTION global[] =
29 { REG_TYPE_SECTION, "global", 0, NULL },
30 { REG_TYPE_BOOLEAN, "fast_path", 1, "1" },
31 { REG_TYPE_STRING, "resolution", 8, "1024x768" },
32 { REG_TYPE_INTEGER, "performance_flags", 4, "0xFFFF" },
33 { REG_TYPE_NONE, "", 0, NULL }
36 static REG_SECTION licensing[] =
38 { REG_TYPE_SECTION, "licensing", 0, NULL },
39 { REG_TYPE_STRING, "platform_id", 1, "0x000201" },
40 { REG_TYPE_STRING, "hardware_id", 16, "0xe107d9d372bb6826bd81d3542a419d6" },
41 { REG_TYPE_NONE, "", 0, NULL }
44 static REG_SECTION* sections[] =
46 (REG_SECTION*) &global,
47 (REG_SECTION*) &licensing,
51 void registry_print_entry(REG_ENTRY* entry, FILE* fp)
54 value = (uint8*) entry->value;
55 fprintf(fp, "%s = %s\n", entry->name, value);
58 void registry_print_section(REG_SECTION* section, FILE* fp)
61 REG_ENTRY* entries = (REG_ENTRY*) §ion[1];
64 fprintf(fp, "[%s]\n", section->name);
66 while (entries[i].type != REG_TYPE_NONE)
68 registry_print_entry(&entries[i], fp);
73 void registry_print(rdpRegistry* registry, FILE* fp)
77 fprintf(fp, "# FreeRDP Configuration Registry\n");
79 while (sections[i] != NULL)
81 registry_print_section(sections[i], fp);
88 void registry_create(rdpRegistry* registry)
90 registry->fp = fopen((char*)registry->file, "w+");
92 if (registry->fp == NULL)
94 printf("registry_create: error opening [%s] for writing\n", registry->file);
98 registry_print(registry, registry->fp);
102 void registry_load(rdpRegistry* registry)
104 registry->fp = fopen((char*) registry->file, "r+");
107 void registry_open(rdpRegistry* registry)
109 struct stat stat_info;
111 if (stat((char*)registry->file, &stat_info) != 0)
112 registry_create(registry);
114 registry_load(registry);
117 void registry_close(rdpRegistry* registry)
119 if (registry->fp != NULL)
120 fclose(registry->fp);
123 void registry_init(rdpRegistry* registry)
127 struct stat stat_info;
129 if (registry->settings->home_path == NULL)
130 home_path = getenv("HOME");
132 home_path = registry->settings->home_path;
134 if (home_path == NULL)
136 printf("could not get home path\n");
137 registry->available = false;
141 registry->available = true;
143 if (home_path == NULL)
145 printf("could not get home path\n");
146 registry->available = false;
150 registry->home = (char*) xstrdup(home_path);
151 printf("home path: %s\n", registry->home);
153 registry->path = (char*) xmalloc(strlen(registry->home) + strlen("/.") + strlen(registry_dir) + 1);
154 sprintf(registry->path, "%s/.%s", registry->home, registry_dir);
155 printf("registry path: %s\n", registry->path);
157 if (stat(registry->path, &stat_info) != 0)
159 freerdp_mkdir(registry->path);
160 printf("creating directory %s\n", registry->path);
163 length = strlen(registry->path);
164 registry->file = (char*) xmalloc(strlen(registry->path) + strlen("/") + strlen(registry_file) + 1);
165 sprintf(registry->file, "%s/%s", registry->path, registry_file);
166 printf("registry file: %s\n", registry->file);
168 registry_open(registry);
171 rdpRegistry* registry_new(rdpSettings* settings)
173 rdpRegistry* registry = (rdpRegistry*) xzalloc(sizeof(rdpRegistry));
175 if (registry != NULL)
177 registry->settings = settings;
178 registry_init(registry);
184 void registry_free(rdpRegistry* registry)
186 if (registry != NULL)
188 registry_close(registry);
189 xfree(registry->path);
190 xfree(registry->file);
191 xfree(registry->home);