ws2_32: Separate out hostent creation from duplication.
[wine] / programs / explorer / appbar.c
1 /*
2  * SHAppBarMessage implementation
3  *
4  * Copyright 2008 Vincent Povirk for CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  *
20  * TODO: freedesktop _NET_WM_STRUT integration
21  *
22  * TODO: find when a fullscreen app is in the foreground and send FULLSCREENAPP
23  *  notifications
24  *
25  * TODO: detect changes in the screen size and send ABN_POSCHANGED ?
26  *
27  * TODO: multiple monitor support
28  */
29
30 #include "wine/unicode.h"
31
32 #include <windows.h>
33 #include <wine/debug.h>
34 #include "explorer_private.h"
35
36 #include "wine/list.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(appbar);
39
40 struct appbar_cmd
41 {
42     HANDLE return_map;
43     DWORD return_process;
44     APPBARDATA abd;
45 };
46
47 struct appbar_response
48 {
49     UINT_PTR result;
50     APPBARDATA abd;
51 };
52
53 static HWND appbarmsg_window = NULL;
54
55 struct appbar_data
56 {
57     struct list entry;
58     HWND hwnd;
59     UINT callback_msg;
60     UINT edge;
61     RECT rc;
62     BOOL space_reserved;
63     /* BOOL autohide; */
64 };
65
66 static struct list appbars = LIST_INIT(appbars);
67
68 static struct appbar_data* get_appbar(HWND hwnd)
69 {
70     struct appbar_data* data;
71
72     LIST_FOR_EACH_ENTRY(data, &appbars, struct appbar_data, entry)
73     {
74         if (data->hwnd == hwnd)
75             return data;
76     }
77
78     return NULL;
79 }
80
81 /* send_poschanged: send ABN_POSCHANGED to every appbar except one */
82 static void send_poschanged(HWND hwnd)
83 {
84     struct appbar_data* data;
85     LIST_FOR_EACH_ENTRY(data, &appbars, struct appbar_data, entry)
86     {
87         if (data->hwnd != hwnd)
88         {
89             PostMessageW(data->hwnd, data->callback_msg, ABN_POSCHANGED, 0);
90         }
91     }
92 }
93
94 /* appbar_cliprect: cut out parts of the rectangle that interfere with existing appbars */
95 static void appbar_cliprect(PAPPBARDATA abd)
96 {
97     struct appbar_data* data;
98     LIST_FOR_EACH_ENTRY(data, &appbars, struct appbar_data, entry)
99     {
100         if (data->hwnd == abd->hWnd)
101         {
102             /* we only care about appbars that were added before this one */
103             return;
104         }
105         if (data->space_reserved)
106         {
107             /* move in the side that corresponds to the other appbar's edge */
108             switch (data->edge)
109             {
110             case ABE_BOTTOM:
111                 abd->rc.bottom = min(abd->rc.bottom, data->rc.top);
112                 break;
113             case ABE_LEFT:
114                 abd->rc.left = max(abd->rc.left, data->rc.right);
115                 break;
116             case ABE_RIGHT:
117                 abd->rc.right = min(abd->rc.right, data->rc.left);
118                 break;
119             case ABE_TOP:
120                 abd->rc.top = max(abd->rc.top, data->rc.bottom);
121                 break;
122             }
123         }
124     }
125 }
126
127 static UINT_PTR handle_appbarmessage(DWORD msg, PAPPBARDATA abd)
128 {
129     struct appbar_data* data;
130
131     switch (msg)
132     {
133     case ABM_NEW:
134         if (get_appbar(abd->hWnd))
135         {
136             /* fail when adding an hwnd the second time */
137             return FALSE;
138         }
139
140         data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct appbar_data));
141         if (!data)
142         {
143             WINE_ERR("out of memory\n");
144             return FALSE;
145         }
146         data->hwnd = abd->hWnd;
147         data->callback_msg = abd->uCallbackMessage;
148
149         list_add_tail(&appbars, &data->entry);
150
151         return TRUE;
152     case ABM_REMOVE:
153         if ((data = get_appbar(abd->hWnd)))
154         {
155             list_remove(&data->entry);
156
157             send_poschanged(abd->hWnd);
158
159             HeapFree(GetProcessHeap(), 0, data);
160         }
161         else
162             WINE_WARN("removing hwnd %p not on the list\n", abd->hWnd);
163         return TRUE;
164     case ABM_QUERYPOS:
165         if (abd->uEdge > ABE_BOTTOM)
166             WINE_WARN("invalid edge %i for %p\n", abd->uEdge, abd->hWnd);
167         appbar_cliprect(abd);
168         return TRUE;
169     case ABM_SETPOS:
170         if (abd->uEdge > ABE_BOTTOM)
171         {
172             WINE_WARN("invalid edge %i for %p\n", abd->uEdge, abd->hWnd);
173             return TRUE;
174         }
175         if ((data = get_appbar(abd->hWnd)))
176         {
177             /* calculate acceptable space */
178             appbar_cliprect(abd);
179
180             if (!EqualRect(&abd->rc, &data->rc))
181                 send_poschanged(abd->hWnd);
182
183             /* reserve that space for this appbar */
184             data->edge = abd->uEdge;
185             data->rc = abd->rc;
186             data->space_reserved = TRUE;
187         }
188         else
189         {
190             WINE_WARN("app sent ABM_SETPOS message for %p without ABM_ADD\n", abd->hWnd);
191         }
192         return TRUE;
193     case ABM_GETSTATE:
194         WINE_FIXME("SHAppBarMessage(ABM_GETSTATE): stub\n");
195         return ABS_ALWAYSONTOP | ABS_AUTOHIDE;
196     case ABM_GETTASKBARPOS:
197         WINE_FIXME("SHAppBarMessage(ABM_GETTASKBARPOS, hwnd=%p): stub\n", abd->hWnd);
198         /* Report the taskbar is at the bottom of the screen. */
199         abd->rc.left = 0;
200         abd->rc.right = GetSystemMetrics(SM_CXSCREEN);
201         abd->rc.bottom = GetSystemMetrics(SM_CYSCREEN);
202         abd->rc.top = abd->rc.bottom-1;
203         abd->uEdge = ABE_BOTTOM;
204         return TRUE;
205     case ABM_ACTIVATE:
206         return TRUE;
207     case ABM_GETAUTOHIDEBAR:
208         WINE_FIXME("SHAppBarMessage(ABM_GETAUTOHIDEBAR, hwnd=%p, edge=%x): stub\n", abd->hWnd, abd->uEdge);
209         return 0;
210     case ABM_SETAUTOHIDEBAR:
211         WINE_FIXME("SHAppBarMessage(ABM_SETAUTOHIDEBAR, hwnd=%p, edge=%x, lparam=%lx): stub\n", abd->hWnd, abd->uEdge, abd->lParam);
212         return TRUE;
213     case ABM_WINDOWPOSCHANGED:
214         return TRUE;
215     default:
216         WINE_FIXME("SHAppBarMessage(%x) unimplemented\n", msg);
217         return FALSE;
218     }
219 }
220
221 static LRESULT CALLBACK appbar_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
222 {
223     switch (msg)
224     {
225     case WM_COPYDATA:
226         {
227             COPYDATASTRUCT* cds;
228             struct appbar_cmd cmd;
229             UINT_PTR result;
230             HANDLE return_hproc;
231             HANDLE return_map;
232             LPVOID return_view;
233             struct appbar_response* response;
234
235             cds = (COPYDATASTRUCT*)lparam;
236             if (cds->cbData != sizeof(struct appbar_cmd))
237                 return TRUE;
238             CopyMemory(&cmd, cds->lpData, cds->cbData);
239
240             result = handle_appbarmessage(cds->dwData, &cmd.abd);
241
242             return_hproc = OpenProcess(PROCESS_DUP_HANDLE, FALSE, cmd.return_process);
243             if (return_hproc == NULL)
244             {
245                 WINE_ERR("couldn't open calling process\n");
246                 return TRUE;
247             }
248
249             if (!DuplicateHandle(return_hproc, cmd.return_map, GetCurrentProcess(), &return_map, 0, FALSE, DUPLICATE_SAME_ACCESS))
250             {
251                 WINE_ERR("couldn't duplicate handle\n");
252                 CloseHandle(return_hproc);
253                 return TRUE;
254             }
255             CloseHandle(return_hproc);
256
257             return_view = MapViewOfFile(return_map, FILE_MAP_WRITE, 0, 0, sizeof(struct appbar_response));
258
259             if (return_view)
260             {
261                 response = (struct appbar_response*)return_view;
262                 response->result = result;
263                 response->abd = cmd.abd;
264
265                 UnmapViewOfFile(return_view);
266             }
267             else
268                 WINE_ERR("couldn't map view of file\n");
269
270             CloseHandle(return_map);
271             return TRUE;
272         }
273     default:
274         break;
275     }
276
277     return DefWindowProcW(hwnd, msg, wparam, lparam);
278 }
279
280 void initialize_appbar(void)
281 {
282     WNDCLASSEXW class;
283     static const WCHAR classname[] = {'W','i','n','e','A','p','p','B','a','r',0};
284
285     /* register the appbar window class */
286     ZeroMemory(&class, sizeof(class));
287     class.cbSize = sizeof(class);
288     class.lpfnWndProc = appbar_wndproc;
289     class.hInstance = NULL;
290     class.lpszClassName = classname;
291
292     if (!RegisterClassExW(&class))
293     {
294         WINE_ERR("Could not register appbar message window class\n");
295         return;
296     }
297
298     appbarmsg_window = CreateWindowW(classname, classname, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
299     if (!appbarmsg_window)
300     {
301         WINE_ERR("Could not create appbar message window\n");
302         return;
303     }
304 }