More bitmap fixes.
[wine] / files / change.c
1 /*
2  * Win32 file change notification functions
3  *
4  * Copyright 1998 Ulrich Weigand
5  */
6
7 #include <errno.h>
8 #include <assert.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <time.h>
13 #include "windows.h"
14 #include "winbase.h"
15 #include "winerror.h"
16 #include "process.h"
17 #include "thread.h"
18 #include "heap.h"
19 #include "debug.h"
20
21 static BOOL32 CHANGE_Signaled( K32OBJ *obj, DWORD thread_id );
22 static BOOL32 CHANGE_Satisfied( K32OBJ *obj, DWORD thread_id );
23 static void CHANGE_AddWait( K32OBJ *obj, DWORD thread_id );
24 static void CHANGE_RemoveWait( K32OBJ *obj, DWORD thread_id );
25 static void CHANGE_Destroy( K32OBJ *obj );
26
27 const K32OBJ_OPS CHANGE_Ops =
28 {
29     CHANGE_Signaled,    /* signaled */
30     CHANGE_Satisfied,   /* satisfied */
31     CHANGE_AddWait,     /* add_wait */
32     CHANGE_RemoveWait,  /* remove_wait */
33     NULL,               /* read */
34     NULL,               /* write */
35     CHANGE_Destroy      /* destroy */
36 };
37
38 /* The change notification object */
39 typedef struct
40 {
41     K32OBJ       header;
42
43     LPSTR        lpPathName;
44     BOOL32       bWatchSubtree;
45     DWORD        dwNotifyFilter;
46
47     THREAD_QUEUE wait_queue;    
48     BOOL32       notify;
49
50 } CHANGE_OBJECT;
51
52 /***********************************************************************
53  *           CHANGE_Signaled
54  */
55 static BOOL32 CHANGE_Signaled( K32OBJ *obj, DWORD thread_id )
56 {
57     CHANGE_OBJECT *change = (CHANGE_OBJECT *)obj;
58     assert( obj->type == K32OBJ_CHANGE );
59     return change->notify;
60 }
61
62 /***********************************************************************
63  *           CHANGE_Satisfied
64  *
65  * Wait on this object has been satisfied.
66  */
67 static BOOL32 CHANGE_Satisfied( K32OBJ *obj, DWORD thread_id )
68 {
69     assert( obj->type == K32OBJ_CHANGE );
70     return FALSE;  /* Not abandoned */
71 }
72
73 /***********************************************************************
74  *           CHANGE_AddWait
75  *
76  * Add thread to object wait queue.
77  */
78 static void CHANGE_AddWait( K32OBJ *obj, DWORD thread_id )
79 {
80     CHANGE_OBJECT *change = (CHANGE_OBJECT *)obj;
81     assert( obj->type == K32OBJ_CHANGE );
82     THREAD_AddQueue( &change->wait_queue, THREAD_ID_TO_THDB(thread_id) );
83 }
84
85 /***********************************************************************
86  *           CHANGE_RemoveWait
87  *
88  * Remove thread from object wait queue.
89  */
90 static void CHANGE_RemoveWait( K32OBJ *obj, DWORD thread_id )
91 {
92     CHANGE_OBJECT *change = (CHANGE_OBJECT *)obj;
93     assert( obj->type == K32OBJ_CHANGE );
94     THREAD_RemoveQueue( &change->wait_queue, THREAD_ID_TO_THDB(thread_id) );
95 }
96
97 /****************************************************************************
98  *              CHANGE_Destroy
99  */
100 static void CHANGE_Destroy( K32OBJ *obj )
101 {
102     CHANGE_OBJECT *change = (CHANGE_OBJECT *)obj;
103     assert( obj->type == K32OBJ_CHANGE );
104
105     if ( change->lpPathName )
106     {
107         HeapFree( SystemHeap, 0, change->lpPathName );
108         change->lpPathName = NULL;
109     }
110
111     obj->type = K32OBJ_UNKNOWN;
112     HeapFree( SystemHeap, 0, change );
113 }
114
115 /****************************************************************************
116  *              FindFirstChangeNotification32A (KERNEL32.248)
117  */
118 HANDLE32 WINAPI FindFirstChangeNotification32A( LPCSTR lpPathName,
119                                                 BOOL32 bWatchSubtree,
120                                                 DWORD dwNotifyFilter ) 
121 {
122     HANDLE32 handle;
123     CHANGE_OBJECT *change;
124
125     change = HeapAlloc( SystemHeap, 0, sizeof(CHANGE_OBJECT) );
126     if (!change) return INVALID_HANDLE_VALUE32;
127
128     change->header.type = K32OBJ_CHANGE;
129     change->header.refcount = 1;
130
131     change->lpPathName = HEAP_strdupA( SystemHeap, 0, lpPathName );
132     change->bWatchSubtree = bWatchSubtree;
133     change->dwNotifyFilter = dwNotifyFilter;
134
135     change->wait_queue = NULL;
136     change->notify = FALSE;
137
138     handle = HANDLE_Alloc( PROCESS_Current(), &change->header, 
139                            FILE_ALL_ACCESS /*FIXME*/, TRUE, -1 );
140     /* If the allocation failed, the object is already destroyed */
141     if (handle == INVALID_HANDLE_VALUE32) change = NULL;
142     return handle;
143 }
144
145 /****************************************************************************
146  *              FindFirstChangeNotification32W (KERNEL32.249)
147  */
148 HANDLE32 WINAPI FindFirstChangeNotification32W( LPCWSTR lpPathName,
149                                                 BOOL32 bWatchSubtree,
150                                                 DWORD dwNotifyFilter) 
151 {
152     LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpPathName );
153     HANDLE32 ret = FindFirstChangeNotification32A( nameA, bWatchSubtree, 
154                                                           dwNotifyFilter );
155     if (nameA) HeapFree( GetProcessHeap(), 0, nameA );
156     return ret;
157 }
158
159 /****************************************************************************
160  *              FindNextChangeNotification (KERNEL32.252)
161  */
162 BOOL32 WINAPI FindNextChangeNotification( HANDLE32 handle ) 
163 {
164     CHANGE_OBJECT *change;
165
166     SYSTEM_LOCK();
167     if (!(change = (CHANGE_OBJECT *)HANDLE_GetObjPtr( PROCESS_Current(),
168                                                       handle, K32OBJ_CHANGE, 
169                                                       0 /*FIXME*/, NULL )) )
170     {
171         SYSTEM_UNLOCK();
172         return FALSE;
173     }
174
175     change->notify = FALSE;
176     K32OBJ_DecCount( &change->header );
177     SYSTEM_UNLOCK();
178     return TRUE;
179 }
180
181 /****************************************************************************
182  *              FindCloseChangeNotification (KERNEL32.247)
183  */
184 BOOL32 WINAPI FindCloseChangeNotification( HANDLE32 handle) 
185 {
186     return CloseHandle( handle );
187 }
188