x11_common: fix for reconfig with pos/xineramascreen set
[mplayer/greg.git] / loader / registry.c
blob2eb449124c7fe6b289258e1d3c381f93d64fe56a
1 /*
2 * Modified for use with MPlayer, detailed changelog at
3 * http://svn.mplayerhq.hu/mplayer/trunk/
4 */
6 #include "config.h"
7 #include "debug.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <pwd.h>
14 #include <sys/types.h>
16 #include "wine/winbase.h"
17 #include "wine/winreg.h"
18 #include "wine/winnt.h"
19 #include "wine/winerror.h"
21 #include "ext.h"
22 #include "registry.h"
24 #include "path.h"
26 //#undef TRACE
27 //#define TRACE printf
29 // ...can be set before init_registry() call
30 char* regpathname = NULL;
32 static char* localregpathname = NULL;
34 typedef struct reg_handle_s
36 int handle;
37 char* name;
38 struct reg_handle_s* next;
39 struct reg_handle_s* prev;
40 } reg_handle_t;
42 struct reg_value
44 int type;
45 char* name;
46 int len;
47 char* value;
50 static struct reg_value* regs = NULL;
51 static int reg_size;
52 static reg_handle_t* head = NULL;
54 #define DIR -25
56 static void create_registry(void);
57 static void open_registry(void);
58 static void save_registry(void);
59 static void init_registry(void);
62 static void create_registry(void){
63 if(regs)
65 printf("Logic error: create_registry() called with existing registry\n");
66 save_registry();
67 return;
69 regs=malloc(3*sizeof(struct reg_value));
70 regs[0].type=regs[1].type=DIR;
71 regs[0].name=malloc(5);
72 strcpy(regs[0].name, "HKLM");
73 regs[1].name=malloc(5);
74 strcpy(regs[1].name, "HKCU");
75 regs[0].value=regs[1].value=NULL;
76 regs[0].len=regs[1].len=0;
77 reg_size=2;
78 head = 0;
79 save_registry();
82 static void open_registry(void)
84 int fd;
85 int i;
86 unsigned int len;
87 if(regs)
89 printf("Multiple open_registry(>\n");
90 return;
92 fd = open(localregpathname, O_RDONLY);
93 if (fd == -1)
95 printf("Creating new registry\n");
96 create_registry();
97 return;
99 read(fd, &reg_size, 4);
100 regs=malloc(reg_size*sizeof(struct reg_value));
101 head = 0;
102 for(i=0; i<reg_size; i++)
104 read(fd,&regs[i].type,4);
105 read(fd,&len,4);
106 regs[i].name=malloc(len+1);
107 if(regs[i].name==0)
109 reg_size=i+1;
110 goto error;
112 read(fd, regs[i].name, len);
113 regs[i].name[len]=0;
114 read(fd,&regs[i].len,4);
115 regs[i].value=malloc(regs[i].len+1);
116 if(regs[i].value==0)
118 free(regs[i].name);
119 reg_size=i+1;
120 goto error;
122 read(fd, regs[i].value, regs[i].len);
123 regs[i].value[regs[i].len]=0;
125 error:
126 close(fd);
127 return;
130 static void save_registry(void)
132 int fd, i;
133 if (!regs)
134 init_registry();
135 fd = open(localregpathname, O_WRONLY | O_CREAT, 00666);
136 if (fd == -1)
138 printf("Failed to open registry file '%s' for writing.\n",
139 localregpathname);
140 return;
142 write(fd, &reg_size, 4);
143 for(i=0; i<reg_size; i++)
145 unsigned len=strlen(regs[i].name);
146 write(fd, &regs[i].type, 4);
147 write(fd, &len, 4);
148 write(fd, regs[i].name, len);
149 write(fd, &regs[i].len, 4);
150 write(fd, regs[i].value, regs[i].len);
152 close(fd);
155 void free_registry(void)
157 reg_handle_t* t = head;
158 while (t)
160 reg_handle_t* f = t;
161 free(t->name);
162 t=t->prev;
163 free(f);
165 head = 0;
166 if (regs)
168 int i;
169 for(i=0; i<reg_size; i++)
171 free(regs[i].name);
172 free(regs[i].value);
174 free(regs);
175 regs = 0;
178 if (localregpathname && localregpathname != regpathname)
179 free(localregpathname);
180 localregpathname = 0;
184 #if 0
185 static reg_handle_t* find_handle_by_name(const char* name)
187 reg_handle_t* t;
188 for(t=head; t; t=t->prev)
190 if(!strcmp(t->name, name))
192 return t;
195 return 0;
197 #endif
198 static struct reg_value* find_value_by_name(const char* name)
200 int i;
201 for(i=0; i<reg_size; i++)
202 if(!strcmp(regs[i].name, name))
203 return regs+i;
204 return 0;
206 static reg_handle_t* find_handle(int handle)
208 reg_handle_t* t;
209 for(t=head; t; t=t->prev)
211 if(t->handle==handle)
213 return t;
216 return 0;
218 static int generate_handle(void)
220 static unsigned int zz=249;
221 zz++;
222 while((zz==HKEY_LOCAL_MACHINE) || (zz==HKEY_CURRENT_USER))
223 zz++;
224 return zz;
227 static reg_handle_t* insert_handle(long handle, const char* name)
229 reg_handle_t* t;
230 t=malloc(sizeof(reg_handle_t));
231 if(head==0)
233 t->prev=0;
235 else
237 head->next=t;
238 t->prev=head;
240 t->next=0;
241 t->name=malloc(strlen(name)+1);
242 strcpy(t->name, name);
243 t->handle=handle;
244 head=t;
245 return t;
247 static char* build_keyname(long key, const char* subkey)
249 char* full_name;
250 reg_handle_t* t;
251 if((t=find_handle(key))==0)
253 TRACE("Invalid key\n");
254 return NULL;
256 if(subkey==NULL)
257 subkey="<default>";
258 full_name=malloc(strlen(t->name)+strlen(subkey)+10);
259 strcpy(full_name, t->name);
260 strcat(full_name, "\\");
261 strcat(full_name, subkey);
262 return full_name;
264 static struct reg_value* insert_reg_value(int handle, const char* name, int type, const void* value, int len)
266 struct reg_value* v;
267 char* fullname;
268 if((fullname=build_keyname(handle, name))==NULL)
270 TRACE("Invalid handle\n");
271 return NULL;
274 if((v=find_value_by_name(fullname))==0)
275 //creating new value in registry
277 if(regs==0)
278 create_registry();
279 regs = realloc(regs, sizeof(struct reg_value) * (reg_size +1 ));
280 //regs=(struct reg_value*)my_realloc(regs, sizeof(struct reg_value)*(reg_size+1));
281 v=regs+reg_size;
282 reg_size++;
284 else
285 //replacing old one
287 free(v->value);
288 free(v->name);
290 TRACE("RegInsert '%s' %p v:%d len:%d\n", name, value, *(int*)value, len);
291 v->type=type;
292 v->len=len;
293 v->value=malloc(len);
294 memcpy(v->value, value, len);
295 v->name=malloc(strlen(fullname)+1);
296 strcpy(v->name, fullname);
297 free(fullname);
298 save_registry();
299 return v;
302 static void init_registry(void)
304 TRACE("Initializing registry\n");
305 // can't be free-ed - it's static and probably thread
306 // unsafe structure which is stored in glibc
308 regpathname = get_path("registry");
309 localregpathname = regpathname;
311 open_registry();
312 insert_handle(HKEY_LOCAL_MACHINE, "HKLM");
313 insert_handle(HKEY_CURRENT_USER, "HKCU");
316 #if 0
317 static reg_handle_t* find_handle_2(long key, const char* subkey)
319 char* full_name;
320 reg_handle_t* t;
321 if((t=find_handle(key))==0)
323 TRACE("Invalid key\n");
324 return (reg_handle_t*)-1;
326 if(subkey==NULL)
327 return t;
328 full_name=malloc(strlen(t->name)+strlen(subkey)+10);
329 strcpy(full_name, t->name);
330 strcat(full_name, "\\");
331 strcat(full_name, subkey);
332 t=find_handle_by_name(full_name);
333 free(full_name);
334 return t;
336 #endif
338 long __stdcall RegOpenKeyExA(long key, const char* subkey, long reserved, long access, int* newkey)
340 char* full_name;
341 reg_handle_t* t;
342 struct reg_value* v;
343 TRACE("Opening key %s\n", subkey);
345 if(!regs)
346 init_registry()
348 /* t=find_handle_2(key, subkey);
350 if(t==0)
351 return -1;
353 if(t==(reg_handle_t*)-1)
354 return -1;
356 full_name=build_keyname(key, subkey);
357 if(!full_name)
358 return -1;
359 TRACE("Opening key Fullname %s\n", full_name);
360 v=find_value_by_name(full_name);
362 t=insert_handle(generate_handle(), full_name);
363 *newkey=t->handle;
364 free(full_name);
366 return 0;
368 long __stdcall RegCloseKey(long key)
370 reg_handle_t *handle;
371 if(key==(long)HKEY_LOCAL_MACHINE)
372 return 0;
373 if(key==(long)HKEY_CURRENT_USER)
374 return 0;
375 handle=find_handle(key);
376 if(handle==0)
377 return 0;
378 if(handle->prev)
379 handle->prev->next=handle->next;
380 if(handle->next)
381 handle->next->prev=handle->prev;
382 free(handle->name);
383 if(handle==head)
384 head=head->prev;
385 free(handle);
386 return 0;
389 long __stdcall RegQueryValueExA(long key, const char* value, int* reserved, int* type, int* data, int* count)
391 struct reg_value* t;
392 char* c;
393 TRACE("Querying value %s\n", value);
394 if(!regs)
395 init_registry();
397 c=build_keyname(key, value);
398 if (!c)
399 return 1;
400 t=find_value_by_name(c);
401 if (t==0) {
402 // Hacks for CineForm.
403 if (strcmp(c, "HKCU\\SOFTWARE\\CineForm\\DecoderProperties\\Resolution") == 0) {
404 if (data)
405 *data = 1000;
406 if (type)
407 *type = REG_DWORD;
408 if (count)
409 *count = sizeof(DWORD);
410 free(c);
411 return ERROR_SUCCESS;
413 if (strcmp(c, "HKCU\\SOFTWARE\\CineForm\\DecoderProperties\\PixelFormats") == 0) {
414 if (data)
415 *data = 0xffff;
416 if (type)
417 *type = REG_DWORD;
418 if (count)
419 *count = sizeof(DWORD);
420 free(c);
421 return ERROR_SUCCESS;
423 free(c);
424 return ERROR_FILE_NOT_FOUND;
426 free(c);
427 if (type)
428 *type=t->type;
429 if (data)
431 memcpy(data, t->value, (t->len<*count)?t->len:*count);
432 TRACE("returning %d bytes: %d\n", t->len, *(int*)data);
434 if(*count<t->len)
436 *count=t->len;
437 return ERROR_MORE_DATA;
439 else
441 *count=t->len;
443 return ERROR_SUCCESS;
445 long __stdcall RegCreateKeyExA(long key, const char* name, long reserved,
446 void* classs, long options, long security,
447 void* sec_attr, int* newkey, int* status)
449 reg_handle_t* t;
450 char* fullname;
451 struct reg_value* v;
452 // TRACE("Creating/Opening key %s\n", name);
453 if(!regs)
454 init_registry();
456 fullname=build_keyname(key, name);
457 if (!fullname)
458 return 1;
459 TRACE("Creating/Opening key %s\n", fullname);
460 v=find_value_by_name(fullname);
461 if(v==0)
463 int qw=45708;
464 v=insert_reg_value(key, name, DIR, &qw, 4);
465 if (status) *status=REG_CREATED_NEW_KEY;
466 // return 0;
469 t=insert_handle(generate_handle(), fullname);
470 *newkey=t->handle;
471 free(fullname);
472 return 0;
476 LONG RegEnumValue(
477 HKEY hKey, // handle to key to query
478 DWORD dwIndex, // index of value to query
479 LPTSTR lpValueName, // address of buffer for value string
480 LPDWORD lpcbValueName, // address for size of value buffer
481 LPDWORD lpReserved, // reserved
482 LPDWORD lpType, // address of buffer for type code
483 LPBYTE lpData, // address of buffer for value data
484 LPDWORD lpcbData // address for size of data buffer
488 long __stdcall RegEnumValueA(HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
489 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count)
491 // currenly just made to support MSZH & ZLIB
492 //printf("Reg Enum 0x%x %d %s %d data: %p %d %d >%s<\n", hkey, index,
493 // value, *val_count, data, *count, reg_size, data);
494 reg_handle_t* t = find_handle(hkey);
495 if (t && index < 10)
497 struct reg_value* v=find_value_by_name(t->name);
498 if (v)
500 memcpy(data, v->value, (v->len < *count) ? v->len : *count);
501 if(*count < v->len)
502 *count = v->len;
503 if (type)
504 *type = v->type;
505 //printf("Found handle %s\n", v->name);
506 return 0;
509 return ERROR_NO_MORE_ITEMS;
512 long __stdcall RegSetValueExA(long key, const char* name, long v1, long v2, const void* data, long size)
514 char* c;
515 TRACE("Request to set value %s %d\n", name, *(const int*)data);
516 if(!regs)
517 init_registry();
519 c=build_keyname(key, name);
520 if(c==NULL)
521 return 1;
522 insert_reg_value(key, name, v2, data, size);
523 free(c);
524 return 0;
527 long __stdcall RegEnumKeyExA(HKEY hKey, DWORD dwIndex, LPSTR lpName, LPDWORD lpcbName,
528 LPDWORD lpReserved, LPSTR lpClass, LPDWORD lpcbClass,
529 LPFILETIME lpftLastWriteTime)
531 return ERROR_NO_MORE_ITEMS;