ReactOS  0.4.12-dev-36-g472787f
pnp_list_manager.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS
3  * LICENSE: GPL - See COPYING in the top level directory
4  * FILE: base/services/audiosrv/pnp_list_manager.c
5  * PURPOSE: Audio Service List Manager
6  * COPYRIGHT: Copyright 2007 Andrew Greenwood
7  */
8 
9 #include "audiosrv.h"
10 
11 /*
12  Device descriptor
13 */
14 
15 VOID*
17 {
19 
20  int path_length = WideStringSize(path);
21  int size = sizeof(PnP_AudioDevice) + path_length;
22 
23 /* printf("path_length %d, total %d\n", path_length, size);*/
24 
25  device = malloc(size);
26 
27  if ( ! device )
28  {
29  logmsg("Failed to create a device descriptor (malloc fail)\n");
30  return NULL;
31  }
32 
33  device->enabled = is_enabled;
34  memcpy(device->path, path, path_length);
35 
36  return device;
37 }
38 
39 
40 /*
41  Device list (manager-side)
42 
43  The device list is stored in some shared-memory, with a named, global
44  mutex to provide a locking mechanism (to avoid it from being updated
45  whilst being read).
46 */
47 
50 
51 
52 /*
53  TODO: Detect duplicate entries and ignore them! (In case we receive
54  a PnP event for an existing device...)
55 */
56 
57 BOOL
59 {
60  int device_info_size;
61 
62  /* Figure out the actual structure size */
63  device_info_size = sizeof(PnP_AudioDevice);
64  device_info_size += WideStringSize(device->path);
65 
67 
68 /*
69  printf("list size is %d\n", audio_device_list->size);
70  printf("device info size is %d bytes\n", device_info_size);
71 */
72 
73  /* We DON'T want to overshoot the end of the buffer! */
74  if ( audio_device_list->size + device_info_size > audio_device_list->max_size )
75  {
76  /*printf("max_size would be exceeded! Failing...\n");*/
77 
79 
80  return FALSE;
81  }
82 
83  /* Commit the device descriptor to the list */
84  memcpy((char*)audio_device_list + audio_device_list->size,
85  device,
86  device_info_size);
87 
88  /* Update the header */
89  audio_device_list->device_count ++;
90  audio_device_list->size += device_info_size;
91 
93 
94  logmsg("Device added to list\n");
95 
96  return TRUE;
97 }
98 
99 BOOL
101 {
102 /* printf("Initializing memory device list lock\n");*/
103 
105  {
106  /*printf("Failed!\n");*/
107  return FALSE;
108  }
109 
110  /* Preliminary locking - the list memory will likely be a big
111  buffer of gibberish at this point so we don't want anyone
112  turning up before we're ready... */
114 
115  logmsg("Creating file mapping\n");
116  /* Expose our device list to the world */
117  device_list_file = CreateFileMappingW(INVALID_HANDLE_VALUE,
118  NULL,
120  0,
121  max_size,
123 
124  if ( ! device_list_file )
125  {
126  logmsg("Creation of audio device list failed (err %d)\n", GetLastError());
127 
130 
131  return FALSE;
132  }
133 
134  logmsg("Mapping view of file\n");
135  /* Of course, we'll need to access the list ourselves */
136  audio_device_list = MapViewOfFile(device_list_file,
138  0,
139  0,
140  max_size);
141 
142  if ( ! audio_device_list )
143  {
144  logmsg("MapViewOfFile FAILED (err %d)\n", GetLastError());
145 
146  CloseHandle(device_list_file);
147  device_list_file = NULL;
148 
151 
152  return FALSE;
153  }
154 
155  /* Clear the mem to avoid any random stray data */
156  memset(audio_device_list, 0, max_size);
157 
158  /* Don't want devices to overwrite the list! */
159  audio_device_list->size = sizeof(PnP_AudioHeader);
160  audio_device_list->max_size = max_size;
161  audio_device_list->device_count = 0;
162 
164 
165  logmsg("Device list created\n");
166 
167  return TRUE;
168 }
169 
170 VOID
172 {
173  logmsg("Destroying device list\n");
174 
176 
177  /*printf("Unmapping view\n");*/
178  UnmapViewOfFile(audio_device_list);
179  audio_device_list = NULL;
180 
181  /*printf("Closing memory mapped file\n");*/
182  CloseHandle(device_list_file);
183  device_list_file = NULL;
184 
186 
187  /*printf("Killing devlist lock\n");*/
189 }
VOID UnlockAudioDeviceList()
Definition: pnp_list_lock.c:53
VOID * CreateDeviceDescriptor(WCHAR *path, BOOL is_enabled)
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:398
VOID DestroyAudioDeviceList()
#define MapViewOfFile
Definition: compat.h:402
#define AUDIO_LIST_NAME
Definition: audiosrv.h:22
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
static HANDLE device_list_file
VOID KillAudioDeviceListLock()
Definition: pnp_list_lock.c:39
Definition: devices.h:39
static INT max_size
Definition: history.c:51
smooth NULL
Definition: ftsmooth.c:416
VOID LockAudioDeviceList()
Definition: pnp_list_lock.c:46
BOOL CreateAudioDeviceList(DWORD max_size)
BOOL AppendAudioDeviceToList(PnP_AudioDevice *device)
unsigned int BOOL
Definition: ntddk_ex.h:94
GLsizeiptr size
Definition: glext.h:5919
#define CreateFileMappingW(a, b, c, d, e, f)
Definition: compat.h:401
#define FILE_MAP_WRITE
Definition: winbase.h:154
static long path_length
Definition: maze.c:116
unsigned long DWORD
Definition: ntddk_ex.h:95
WCHAR path[]
Definition: audiosrv.h:30
DWORD device_count
Definition: audiosrv.h:37
void logmsg(char *string,...)
Definition: debug.c:7
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
DWORD max_size
Definition: audiosrv.h:36
Definition: services.c:325
static PnP_AudioHeader * audio_device_list
#define malloc
Definition: debug_ros.c:4
BOOL InitializeAudioDeviceListLock()
Definition: pnp_list_lock.c:16
#define WideStringSize(str)
Definition: audiosrv.h:45
#define UnmapViewOfFile
Definition: compat.h:403
#define memset(x, y, z)
Definition: compat.h:39
DWORD enabled
Definition: audiosrv.h:29
#define PAGE_READWRITE
Definition: nt_native.h:1304