include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / shell32 / changenotify.c
blob93feedfabc743889d51f390d4dc5699fb6ffeb79
1 /*
2 * shell change notification
4 * Copyright 2000 Juergen Schmied
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <string.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wine/list.h"
27 #include "wine/debug.h"
28 #include "shell32_main.h"
29 #include "pidl.h"
30 #include "shlwapi.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(shell);
34 static CRITICAL_SECTION SHELL32_ChangenotifyCS;
35 static CRITICAL_SECTION_DEBUG critsect_debug =
37 0, 0, &SHELL32_ChangenotifyCS,
38 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
39 0, 0, { (DWORD_PTR)(__FILE__ ": SHELL32_ChangenotifyCS") }
41 static CRITICAL_SECTION SHELL32_ChangenotifyCS = { &critsect_debug, -1, 0, 0, 0, 0 };
43 typedef SHChangeNotifyEntry *LPNOTIFYREGISTER;
45 /* internal list of notification clients (internal) */
46 typedef struct _NOTIFICATIONLIST
48 struct list entry;
49 HWND hwnd; /* window to notify */
50 DWORD uMsg; /* message to send */
51 LPNOTIFYREGISTER apidl; /* array of entries to watch*/
52 UINT cidl; /* number of pidls in array */
53 LONG wEventMask; /* subscribed events */
54 DWORD dwFlags; /* client flags */
55 ULONG id;
56 } NOTIFICATIONLIST, *LPNOTIFICATIONLIST;
58 static struct list notifications = LIST_INIT( notifications );
59 static LONG next_id;
61 #define SHCNE_NOITEMEVENTS ( \
62 SHCNE_ASSOCCHANGED )
64 #define SHCNE_ONEITEMEVENTS ( \
65 SHCNE_ATTRIBUTES | SHCNE_CREATE | SHCNE_DELETE | SHCNE_DRIVEADD | \
66 SHCNE_DRIVEADDGUI | SHCNE_DRIVEREMOVED | SHCNE_FREESPACE | \
67 SHCNE_MEDIAINSERTED | SHCNE_MEDIAREMOVED | SHCNE_MKDIR | \
68 SHCNE_NETSHARE | SHCNE_NETUNSHARE | SHCNE_RMDIR | \
69 SHCNE_SERVERDISCONNECT | SHCNE_UPDATEDIR | SHCNE_UPDATEIMAGE | \
70 SHCNE_UPDATEITEM )
72 #define SHCNE_TWOITEMEVENTS ( \
73 SHCNE_RENAMEFOLDER | SHCNE_RENAMEITEM )
75 /* for dumping events */
76 static const char * DumpEvent( LONG event )
78 if( event == SHCNE_ALLEVENTS )
79 return "SHCNE_ALLEVENTS";
80 #define DUMPEV(x) ,( event & SHCNE_##x )? #x " " : ""
81 return wine_dbg_sprintf( "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s"
82 DUMPEV(RENAMEITEM)
83 DUMPEV(CREATE)
84 DUMPEV(DELETE)
85 DUMPEV(MKDIR)
86 DUMPEV(RMDIR)
87 DUMPEV(MEDIAINSERTED)
88 DUMPEV(MEDIAREMOVED)
89 DUMPEV(DRIVEREMOVED)
90 DUMPEV(DRIVEADD)
91 DUMPEV(NETSHARE)
92 DUMPEV(NETUNSHARE)
93 DUMPEV(ATTRIBUTES)
94 DUMPEV(UPDATEDIR)
95 DUMPEV(UPDATEITEM)
96 DUMPEV(SERVERDISCONNECT)
97 DUMPEV(UPDATEIMAGE)
98 DUMPEV(DRIVEADDGUI)
99 DUMPEV(RENAMEFOLDER)
100 DUMPEV(FREESPACE)
101 DUMPEV(EXTENDED_EVENT)
102 DUMPEV(ASSOCCHANGED)
103 DUMPEV(INTERRUPT)
105 #undef DUMPEV
108 static const char * NodeName(const NOTIFICATIONLIST *item)
110 const char *str;
111 WCHAR path[MAX_PATH];
113 if(SHGetPathFromIDListW(item->apidl[0].pidl, path ))
114 str = wine_dbg_sprintf("%s", debugstr_w(path));
115 else
116 str = wine_dbg_sprintf("<not a disk file>" );
117 return str;
120 static void DeleteNode(LPNOTIFICATIONLIST item)
122 UINT i;
124 TRACE("item=%p\n", item);
126 /* remove item from list */
127 list_remove( &item->entry );
129 /* free the item */
130 for (i=0; i<item->cidl; i++)
131 SHFree((LPITEMIDLIST)item->apidl[i].pidl);
132 SHFree(item->apidl);
133 SHFree(item);
136 void InitChangeNotifications(void)
140 void FreeChangeNotifications(void)
142 LPNOTIFICATIONLIST ptr, next;
144 TRACE("\n");
146 EnterCriticalSection(&SHELL32_ChangenotifyCS);
148 LIST_FOR_EACH_ENTRY_SAFE( ptr, next, &notifications, NOTIFICATIONLIST, entry )
149 DeleteNode( ptr );
151 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
153 DeleteCriticalSection(&SHELL32_ChangenotifyCS);
156 /*************************************************************************
157 * SHChangeNotifyRegister [SHELL32.2]
160 ULONG WINAPI
161 SHChangeNotifyRegister(
162 HWND hwnd,
163 int fSources,
164 LONG wEventMask,
165 UINT uMsg,
166 int cItems,
167 SHChangeNotifyEntry *lpItems)
169 LPNOTIFICATIONLIST item;
170 int i;
172 item = SHAlloc(sizeof(NOTIFICATIONLIST));
174 TRACE("(%p,0x%08x,0x%08lx,0x%08x,%d,%p) item=%p\n",
175 hwnd, fSources, wEventMask, uMsg, cItems, lpItems, item);
177 item->cidl = cItems;
178 item->apidl = SHAlloc(sizeof(SHChangeNotifyEntry) * cItems);
179 for(i=0;i<cItems;i++)
181 item->apidl[i].pidl = ILClone(lpItems[i].pidl);
182 item->apidl[i].fRecursive = lpItems[i].fRecursive;
184 item->hwnd = hwnd;
185 item->uMsg = uMsg;
186 item->wEventMask = wEventMask;
187 item->dwFlags = fSources;
188 item->id = InterlockedIncrement( &next_id );
190 TRACE("new node: %s\n", NodeName( item ));
192 EnterCriticalSection(&SHELL32_ChangenotifyCS);
194 list_add_tail( &notifications, &item->entry );
196 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
198 return item->id;
201 /*************************************************************************
202 * SHChangeNotifyDeregister [SHELL32.4]
204 BOOL WINAPI SHChangeNotifyDeregister(ULONG hNotify)
206 LPNOTIFICATIONLIST node;
208 TRACE("(0x%08lx)\n", hNotify);
210 EnterCriticalSection(&SHELL32_ChangenotifyCS);
212 LIST_FOR_EACH_ENTRY( node, &notifications, NOTIFICATIONLIST, entry )
214 if (node->id == hNotify)
216 DeleteNode( node );
217 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
218 return TRUE;
221 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
222 return FALSE;
225 /*************************************************************************
226 * SHChangeNotifyUpdateEntryList [SHELL32.5]
228 BOOL WINAPI SHChangeNotifyUpdateEntryList(DWORD unknown1, DWORD unknown2,
229 DWORD unknown3, DWORD unknown4)
231 FIXME("(0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx)\n",
232 unknown1, unknown2, unknown3, unknown4);
234 return TRUE;
237 struct new_delivery_notification
239 LONG event;
240 DWORD pidl1_size;
241 DWORD pidl2_size;
242 LPITEMIDLIST pidls[2];
243 BYTE data[1];
246 static BOOL should_notify( LPCITEMIDLIST changed, LPCITEMIDLIST watched, BOOL sub )
248 TRACE("%p %p %d\n", changed, watched, sub );
249 if ( !watched )
250 return FALSE;
251 if (ILIsEqual( watched, changed ) )
252 return TRUE;
253 if( sub && ILIsParent( watched, changed, FALSE ) )
254 return TRUE;
255 return FALSE;
258 /*************************************************************************
259 * SHChangeNotify [SHELL32.@]
261 void WINAPI SHChangeNotify(LONG wEventId, UINT uFlags, LPCVOID dwItem1, LPCVOID dwItem2)
263 struct notification_recipients {
264 struct list entry;
265 HWND hwnd;
266 DWORD msg;
267 DWORD flags;
268 } *cur, *next;
270 HANDLE shared_data = NULL;
271 LPITEMIDLIST Pidls[2];
272 LPNOTIFICATIONLIST ptr;
273 struct list recipients;
275 Pidls[0] = NULL;
276 Pidls[1] = NULL;
278 TRACE("(0x%08lx,0x%08x,%p,%p)\n", wEventId, uFlags, dwItem1, dwItem2);
280 if(uFlags & ~(SHCNF_TYPE|SHCNF_FLUSH))
281 FIXME("ignoring unsupported flags: %x\n", uFlags);
283 if( ( wEventId & SHCNE_NOITEMEVENTS ) && ( dwItem1 || dwItem2 ) )
285 TRACE("dwItem1 and dwItem2 are not zero, but should be\n");
286 dwItem1 = 0;
287 dwItem2 = 0;
288 return;
290 else if( ( wEventId & SHCNE_ONEITEMEVENTS ) && dwItem2 )
292 TRACE("dwItem2 is not zero, but should be\n");
293 dwItem2 = 0;
294 return;
297 if( ( ( wEventId & SHCNE_NOITEMEVENTS ) &&
298 ( wEventId & ~SHCNE_NOITEMEVENTS ) ) ||
299 ( ( wEventId & SHCNE_ONEITEMEVENTS ) &&
300 ( wEventId & ~SHCNE_ONEITEMEVENTS ) ) ||
301 ( ( wEventId & SHCNE_TWOITEMEVENTS ) &&
302 ( wEventId & ~SHCNE_TWOITEMEVENTS ) ) )
304 WARN("mutually incompatible events listed\n");
305 return;
308 /* convert paths in IDLists*/
309 switch (uFlags & SHCNF_TYPE)
311 case SHCNF_PATHA:
312 if (dwItem1) Pidls[0] = SHSimpleIDListFromPathA(dwItem1);
313 if (dwItem2) Pidls[1] = SHSimpleIDListFromPathA(dwItem2);
314 break;
315 case SHCNF_PATHW:
316 if (dwItem1) Pidls[0] = SHSimpleIDListFromPathW(dwItem1);
317 if (dwItem2) Pidls[1] = SHSimpleIDListFromPathW(dwItem2);
318 break;
319 case SHCNF_IDLIST:
320 Pidls[0] = ILClone(dwItem1);
321 Pidls[1] = ILClone(dwItem2);
322 break;
323 case SHCNF_PRINTERA:
324 case SHCNF_PRINTERW:
325 FIXME("SHChangeNotify with (uFlags & SHCNF_PRINTER)\n");
326 return;
327 case SHCNF_DWORD:
328 default:
329 FIXME("unknown type %08x\n", uFlags & SHCNF_TYPE);
330 return;
333 list_init(&recipients);
334 EnterCriticalSection(&SHELL32_ChangenotifyCS);
335 LIST_FOR_EACH_ENTRY( ptr, &notifications, NOTIFICATIONLIST, entry )
337 struct notification_recipients *item;
338 BOOL notify = FALSE;
339 DWORD i;
341 for( i=0; (i<ptr->cidl) && !notify ; i++ )
343 LPCITEMIDLIST pidl = ptr->apidl[i].pidl;
344 BOOL subtree = ptr->apidl[i].fRecursive;
346 if (wEventId & ptr->wEventMask)
348 if( !pidl ) /* all ? */
349 notify = TRUE;
350 else if( wEventId & SHCNE_NOITEMEVENTS )
351 notify = TRUE;
352 else if( wEventId & ( SHCNE_ONEITEMEVENTS | SHCNE_TWOITEMEVENTS ) )
353 notify = should_notify( Pidls[0], pidl, subtree );
354 else if( wEventId & SHCNE_TWOITEMEVENTS )
355 notify = should_notify( Pidls[1], pidl, subtree );
359 if( !notify )
360 continue;
362 item = SHAlloc(sizeof(struct notification_recipients));
363 if(!item) {
364 ERR("out of memory\n");
365 continue;
368 item->hwnd = ptr->hwnd;
369 item->msg = ptr->uMsg;
370 item->flags = ptr->dwFlags;
371 list_add_tail(&recipients, &item->entry);
373 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
375 LIST_FOR_EACH_ENTRY_SAFE(cur, next, &recipients, struct notification_recipients, entry)
377 TRACE("notifying %p, event %s(%lx)\n", cur->hwnd, DumpEvent(wEventId), wEventId);
379 if (cur->flags & SHCNRF_NewDelivery) {
380 if(!shared_data) {
381 struct new_delivery_notification *notification;
382 UINT size1 = ILGetSize(Pidls[0]), size2 = ILGetSize(Pidls[1]);
383 UINT offset = (size1+sizeof(int)-1)/sizeof(int)*sizeof(int);
385 notification = SHAlloc(sizeof(struct new_delivery_notification)+offset+size2);
386 if(!notification) {
387 ERR("out of memory\n");
388 } else {
389 notification->event = wEventId;
390 notification->pidl1_size = size1;
391 notification->pidl2_size = size2;
392 if(size1)
393 memcpy(notification->data, Pidls[0], size1);
394 if(size2)
395 memcpy(notification->data+offset, Pidls[1], size2);
397 shared_data = SHAllocShared(notification,
398 sizeof(struct new_delivery_notification)+size1+size2,
399 GetCurrentProcessId());
400 SHFree(notification);
404 if(shared_data)
405 SendMessageA(cur->hwnd, cur->msg, (WPARAM)shared_data, GetCurrentProcessId());
406 else
407 ERR("out of memory\n");
408 } else {
409 SendMessageA(cur->hwnd, cur->msg, (WPARAM)Pidls, wEventId);
412 list_remove(&cur->entry);
413 SHFree(cur);
415 SHFreeShared(shared_data, GetCurrentProcessId());
416 SHFree(Pidls[0]);
417 SHFree(Pidls[1]);
419 if (wEventId & SHCNE_ASSOCCHANGED)
421 TRACE("refreshing file type associations\n");
422 run_winemenubuilder( L" -a" );
426 /*************************************************************************
427 * NTSHChangeNotifyRegister [SHELL32.640]
428 * NOTES
429 * Idlist is an array of structures and Count specifies how many items in the array
430 * (usually just one I think).
432 DWORD WINAPI NTSHChangeNotifyRegister(
433 HWND hwnd,
434 LONG events1,
435 LONG events2,
436 DWORD msg,
437 int count,
438 SHChangeNotifyEntry *idlist)
440 FIXME("(%p,0x%08lx,0x%08lx,0x%08lx,0x%08x,%p):semi stub.\n",
441 hwnd,events1,events2,msg,count,idlist);
443 return SHChangeNotifyRegister(hwnd, events1, events2, msg, count, idlist);
446 /*************************************************************************
447 * SHChangeNotification_Lock [SHELL32.644]
449 HANDLE WINAPI SHChangeNotification_Lock(
450 HANDLE hChange,
451 DWORD dwProcessId,
452 LPITEMIDLIST **lppidls,
453 LPLONG lpwEventId)
455 struct new_delivery_notification *ndn;
456 UINT offset;
458 TRACE("%p %08lx %p %p\n", hChange, dwProcessId, lppidls, lpwEventId);
460 ndn = SHLockShared(hChange, dwProcessId);
461 if(!ndn) {
462 WARN("SHLockShared failed\n");
463 return NULL;
466 if(lppidls) {
467 offset = (ndn->pidl1_size+sizeof(int)-1)/sizeof(int)*sizeof(int);
468 ndn->pidls[0] = ndn->pidl1_size ? (LPITEMIDLIST)ndn->data : NULL;
469 ndn->pidls[1] = ndn->pidl2_size ? (LPITEMIDLIST)(ndn->data+offset) : NULL;
470 *lppidls = ndn->pidls;
473 if(lpwEventId)
474 *lpwEventId = ndn->event;
476 return ndn;
479 /*************************************************************************
480 * SHChangeNotification_Unlock [SHELL32.645]
482 BOOL WINAPI SHChangeNotification_Unlock ( HANDLE hLock)
484 TRACE("%p\n", hLock);
485 return SHUnlockShared(hLock);
488 /*************************************************************************
489 * NTSHChangeNotifyDeregister [SHELL32.641]
491 DWORD WINAPI NTSHChangeNotifyDeregister(ULONG x1)
493 FIXME("(0x%08lx):semi stub.\n",x1);
495 return SHChangeNotifyDeregister( x1 );