"Fossies" - the Fresh Open Source Software Archive

Member "wine-6.0.1/dlls/winex11.drv/systray.c" (7 Jun 2021, 27547 Bytes) of package /linux/misc/wine-6.0.1.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "systray.c": 6.0_vs_6.0.1.

    1 /*
    2  * X11 system tray management
    3  *
    4  * Copyright (C) 2004 Mike Hearn, for CodeWeavers
    5  * Copyright (C) 2005 Robert Shearman
    6  * Copyright (C) 2008 Alexandre Julliard
    7  *
    8  * This library is free software; you can redistribute it and/or
    9  * modify it under the terms of the GNU Lesser General Public
   10  * License as published by the Free Software Foundation; either
   11  * version 2.1 of the License, or (at your option) any later version.
   12  *
   13  * This library is distributed in the hope that it will be useful,
   14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   16  * Lesser General Public License for more details.
   17  *
   18  * You should have received a copy of the GNU Lesser General Public
   19  * License along with this library; if not, write to the Free Software
   20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
   21  */
   22 
   23 #include "config.h"
   24 
   25 #include <assert.h>
   26 #include <stdarg.h>
   27 #include <stdlib.h>
   28 #include <stdio.h>
   29 #ifdef HAVE_UNISTD_H
   30 # include <unistd.h>
   31 #endif
   32 
   33 #include <X11/Xlib.h>
   34 
   35 #define NONAMELESSUNION
   36 #include "windef.h"
   37 #include "winbase.h"
   38 #include "wingdi.h"
   39 #include "winuser.h"
   40 #include "commctrl.h"
   41 #include "shellapi.h"
   42 
   43 #include "x11drv.h"
   44 #include "wine/list.h"
   45 #include "wine/debug.h"
   46 
   47 WINE_DEFAULT_DEBUG_CHANNEL(systray);
   48 
   49 /* an individual systray icon */
   50 struct tray_icon
   51 {
   52     struct list    entry;
   53     HICON          image;    /* the image to render */
   54     HWND           owner;    /* the HWND passed in to the Shell_NotifyIcon call */
   55     HWND           window;   /* the adaptor window */
   56     BOOL           layered;  /* whether we are using a layered window */
   57     HWND           tooltip;  /* Icon tooltip */
   58     UINT           state;    /* state flags */
   59     UINT           id;       /* the unique id given by the app */
   60     UINT           callback_message;
   61     int            display;  /* display index, or -1 if hidden */
   62     WCHAR          tiptext[128];    /* tooltip text */
   63     WCHAR          info_text[256];  /* info balloon text */
   64     WCHAR          info_title[64];  /* info balloon title */
   65     UINT           info_flags;      /* flags for info balloon */
   66     UINT           info_timeout;    /* timeout for info balloon */
   67     HICON          info_icon;       /* info balloon icon */
   68     UINT           version;         /* notify icon api version */
   69 };
   70 
   71 static struct list icon_list = LIST_INIT( icon_list );
   72 
   73 static const WCHAR icon_classname[] = {'_','_','w','i','n','e','x','1','1','_','t','r','a','y','_','i','c','o','n',0};
   74 static const WCHAR tray_classname[] = {'_','_','w','i','n','e','x','1','1','_','s','t','a','n','d','a','l','o','n','e','_','t','r','a','y',0};
   75 
   76 static BOOL show_icon( struct tray_icon *icon );
   77 static BOOL hide_icon( struct tray_icon *icon );
   78 static BOOL delete_icon( struct tray_icon *icon );
   79 
   80 #define SYSTEM_TRAY_REQUEST_DOCK  0
   81 #define SYSTEM_TRAY_BEGIN_MESSAGE   1
   82 #define SYSTEM_TRAY_CANCEL_MESSAGE  2
   83 
   84 Atom systray_atom = 0;
   85 
   86 #define MIN_DISPLAYED 8
   87 #define ICON_BORDER 2
   88 
   89 #define BALLOON_CREATE_TIMER 1
   90 #define BALLOON_SHOW_TIMER   2
   91 
   92 #define BALLOON_CREATE_TIMEOUT   2000
   93 #define BALLOON_SHOW_MIN_TIMEOUT 10000
   94 #define BALLOON_SHOW_MAX_TIMEOUT 30000
   95 
   96 static struct tray_icon *balloon_icon;
   97 static HWND balloon_window;
   98 static POINT balloon_pos;
   99 
  100 /* stand-alone tray window */
  101 static HWND standalone_tray;
  102 static int icon_cx, icon_cy;
  103 static unsigned int nb_displayed;
  104 
  105 /* retrieves icon record by owner window and ID */
  106 static struct tray_icon *get_icon(HWND owner, UINT id)
  107 {
  108     struct tray_icon *this;
  109 
  110     LIST_FOR_EACH_ENTRY( this, &icon_list, struct tray_icon, entry )
  111         if ((this->id == id) && (this->owner == owner)) return this;
  112     return NULL;
  113 }
  114 
  115 static void init_common_controls(void)
  116 {
  117     static BOOL initialized = FALSE;
  118 
  119     if (!initialized)
  120     {
  121         INITCOMMONCONTROLSEX init_tooltip;
  122 
  123         init_tooltip.dwSize = sizeof(INITCOMMONCONTROLSEX);
  124         init_tooltip.dwICC = ICC_TAB_CLASSES;
  125 
  126         InitCommonControlsEx(&init_tooltip);
  127         initialized = TRUE;
  128     }
  129 }
  130 
  131 /* create tooltip window for icon */
  132 static void create_tooltip(struct tray_icon *icon)
  133 {
  134     init_common_controls();
  135     icon->tooltip = CreateWindowExW( WS_EX_TOPMOST, TOOLTIPS_CLASSW, NULL,
  136                                      WS_POPUP | TTS_ALWAYSTIP,
  137                                      CW_USEDEFAULT, CW_USEDEFAULT,
  138                                      CW_USEDEFAULT, CW_USEDEFAULT,
  139                                      icon->window, NULL, NULL, NULL);
  140     if (icon->tooltip)
  141     {
  142         TTTOOLINFOW ti;
  143         ZeroMemory(&ti, sizeof(ti));
  144         ti.cbSize = sizeof(TTTOOLINFOW);
  145         ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
  146         ti.hwnd = icon->window;
  147         ti.uId = (UINT_PTR)icon->window;
  148         ti.lpszText = icon->tiptext;
  149         SendMessageW(icon->tooltip, TTM_ADDTOOLW, 0, (LPARAM)&ti);
  150     }
  151 }
  152 
  153 void update_systray_balloon_position(void)
  154 {
  155     RECT rect;
  156     POINT pos;
  157 
  158     if (!balloon_icon) return;
  159     GetWindowRect( balloon_icon->window, &rect );
  160     pos.x = (rect.left + rect.right) / 2;
  161     pos.y = (rect.top + rect.bottom) / 2;
  162     if (pos.x == balloon_pos.x && pos.y == balloon_pos.y) return;  /* nothing changed */
  163     balloon_pos = pos;
  164     SendMessageW( balloon_window, TTM_TRACKPOSITION, 0, MAKELONG( pos.x, pos.y ));
  165 }
  166 
  167 static void balloon_create_timer( struct tray_icon *icon )
  168 {
  169     TTTOOLINFOW ti;
  170 
  171     init_common_controls();
  172     balloon_window = CreateWindowExW( WS_EX_TOPMOST, TOOLTIPS_CLASSW, NULL,
  173                                       WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX | TTS_BALLOON | TTS_CLOSE,
  174                                       CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  175                                       icon->window, NULL, NULL, NULL);
  176 
  177     memset( &ti, 0, sizeof(ti) );
  178     ti.cbSize = sizeof(TTTOOLINFOW);
  179     ti.hwnd = icon->window;
  180     ti.uId = (UINT_PTR)icon->window;
  181     ti.uFlags = TTF_TRACK | TTF_IDISHWND;
  182     ti.lpszText = icon->info_text;
  183     SendMessageW( balloon_window, TTM_ADDTOOLW, 0, (LPARAM)&ti );
  184     if ((icon->info_flags & NIIF_ICONMASK) == NIIF_USER)
  185         SendMessageW( balloon_window, TTM_SETTITLEW, (WPARAM)icon->info_icon, (LPARAM)icon->info_title );
  186     else
  187         SendMessageW( balloon_window, TTM_SETTITLEW, icon->info_flags, (LPARAM)icon->info_title );
  188     balloon_icon = icon;
  189     balloon_pos.x = balloon_pos.y = MAXLONG;
  190     update_systray_balloon_position();
  191     SendMessageW( balloon_window, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti );
  192     KillTimer( icon->window, BALLOON_CREATE_TIMER );
  193     SetTimer( icon->window, BALLOON_SHOW_TIMER, icon->info_timeout, NULL );
  194 }
  195 
  196 static BOOL show_balloon( struct tray_icon *icon )
  197 {
  198     if (standalone_tray && !show_systray) return FALSE;  /* no systray window */
  199     if (!icon->window) return FALSE;  /* not displayed */
  200     if (!icon->info_text[0]) return FALSE;  /* no balloon */
  201     balloon_icon = icon;
  202     SetTimer( icon->window, BALLOON_CREATE_TIMER, BALLOON_CREATE_TIMEOUT, NULL );
  203     return TRUE;
  204 }
  205 
  206 static void hide_balloon(void)
  207 {
  208     if (!balloon_icon) return;
  209     if (balloon_window)
  210     {
  211         KillTimer( balloon_icon->window, BALLOON_SHOW_TIMER );
  212         DestroyWindow( balloon_window );
  213         balloon_window = 0;
  214     }
  215     else KillTimer( balloon_icon->window, BALLOON_CREATE_TIMER );
  216     balloon_icon = NULL;
  217 }
  218 
  219 static void show_next_balloon(void)
  220 {
  221     struct tray_icon *icon;
  222 
  223     LIST_FOR_EACH_ENTRY( icon, &icon_list, struct tray_icon, entry )
  224         if (show_balloon( icon )) break;
  225 }
  226 
  227 static void update_balloon( struct tray_icon *icon )
  228 {
  229     if (balloon_icon == icon)
  230     {
  231         hide_balloon();
  232         show_balloon( icon );
  233     }
  234     else if (!balloon_icon)
  235     {
  236         if (!show_balloon( icon )) return;
  237     }
  238     if (!balloon_icon) show_next_balloon();
  239 }
  240 
  241 static void balloon_timer(void)
  242 {
  243     if (balloon_icon) balloon_icon->info_text[0] = 0;  /* clear text now that balloon has been shown */
  244     hide_balloon();
  245     show_next_balloon();
  246 }
  247 
  248 /* synchronize tooltip text with tooltip window */
  249 static void update_tooltip_text(struct tray_icon *icon)
  250 {
  251     TTTOOLINFOW ti;
  252 
  253     ZeroMemory(&ti, sizeof(ti));
  254     ti.cbSize = sizeof(TTTOOLINFOW);
  255     ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
  256     ti.hwnd = icon->window;
  257     ti.uId = (UINT_PTR)icon->window;
  258     ti.lpszText = icon->tiptext;
  259 
  260     SendMessageW(icon->tooltip, TTM_UPDATETIPTEXTW, 0, (LPARAM)&ti);
  261 }
  262 
  263 /* get the size of the stand-alone tray window */
  264 static SIZE get_window_size(void)
  265 {
  266     SIZE size;
  267     RECT rect;
  268 
  269     rect.left = 0;
  270     rect.top = 0;
  271     rect.right = icon_cx * max( nb_displayed, MIN_DISPLAYED );
  272     rect.bottom = icon_cy;
  273     AdjustWindowRect( &rect, WS_CAPTION, FALSE );
  274     size.cx = rect.right - rect.left;
  275     size.cy = rect.bottom - rect.top;
  276     return size;
  277 }
  278 
  279 /* get the position of an icon in the stand-alone tray */
  280 static POINT get_icon_pos( struct tray_icon *icon )
  281 {
  282     POINT pos;
  283 
  284     pos.x = icon_cx * icon->display;
  285     pos.y = 0;
  286     return pos;
  287 }
  288 
  289 /* window procedure for the standalone tray window */
  290 static LRESULT WINAPI standalone_tray_wndproc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
  291 {
  292     switch (msg)
  293     {
  294     case WM_MOVE:
  295         update_systray_balloon_position();
  296         break;
  297     case WM_CLOSE:
  298         ShowWindow( hwnd, SW_HIDE );
  299         hide_balloon();
  300         show_systray = FALSE;
  301         return 0;
  302     case WM_DESTROY:
  303         standalone_tray = 0;
  304         break;
  305     }
  306     return DefWindowProcW( hwnd, msg, wparam, lparam );
  307 }
  308 
  309 /* add an icon to the standalone tray window */
  310 static void add_to_standalone_tray( struct tray_icon *icon )
  311 {
  312     SIZE size;
  313     POINT pos;
  314 
  315     if (!standalone_tray)
  316     {
  317         static const WCHAR winname[] = {'W','i','n','e',' ','S','y','s','t','e','m',' ','T','r','a','y',0};
  318 
  319         size = get_window_size();
  320         standalone_tray = CreateWindowExW( 0, tray_classname, winname, WS_CAPTION | WS_SYSMENU,
  321                                            CW_USEDEFAULT, CW_USEDEFAULT, size.cx, size.cy, 0, 0, 0, 0 );
  322         if (!standalone_tray) return;
  323     }
  324 
  325     icon->display = nb_displayed;
  326     pos = get_icon_pos( icon );
  327     icon->window = CreateWindowW( icon_classname, NULL, WS_CHILD | WS_VISIBLE,
  328                                   pos.x, pos.y, icon_cx, icon_cy, standalone_tray, NULL, NULL, icon );
  329     if (!icon->window)
  330     {
  331         icon->display = -1;
  332         return;
  333     }
  334     create_tooltip( icon );
  335 
  336     nb_displayed++;
  337     size = get_window_size();
  338     SetWindowPos( standalone_tray, 0, 0, 0, size.cx, size.cy, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
  339     if (nb_displayed == 1 && show_systray) ShowWindow( standalone_tray, SW_SHOWNA );
  340     TRACE( "added %u now %d icons\n", icon->id, nb_displayed );
  341 }
  342 
  343 /* remove an icon from the stand-alone tray */
  344 static void remove_from_standalone_tray( struct tray_icon *icon )
  345 {
  346     struct tray_icon *ptr;
  347     POINT pos;
  348 
  349     if (icon->display == -1) return;
  350 
  351     LIST_FOR_EACH_ENTRY( ptr, &icon_list, struct tray_icon, entry )
  352     {
  353         if (ptr == icon) continue;
  354         if (ptr->display < icon->display) continue;
  355         ptr->display--;
  356         pos = get_icon_pos( ptr );
  357         SetWindowPos( ptr->window, 0, pos.x, pos.y, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER );
  358     }
  359     icon->display = -1;
  360     if (!--nb_displayed) ShowWindow( standalone_tray, SW_HIDE );
  361     TRACE( "removed %u now %d icons\n", icon->id, nb_displayed );
  362 }
  363 
  364 static void repaint_tray_icon( struct tray_icon *icon )
  365 {
  366     BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
  367     int width = GetSystemMetrics( SM_CXSMICON );
  368     int height = GetSystemMetrics( SM_CYSMICON );
  369     BITMAPINFO *info;
  370     HBITMAP dib, mask;
  371     HDC hdc;
  372     RECT rc;
  373     SIZE size;
  374     POINT pos;
  375     int i, x, y;
  376     void *color_bits, *mask_bits;
  377     DWORD *ptr;
  378     BOOL has_alpha = FALSE;
  379 
  380     GetWindowRect( icon->window, &rc );
  381     size.cx = rc.right - rc.left;
  382     size.cy = rc.bottom - rc.top;
  383     pos.x = (size.cx - width) / 2;
  384     pos.y = (size.cy - height) / 2;
  385 
  386     info = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, FIELD_OFFSET( BITMAPINFO, bmiColors[2] ));
  387     if (!info) return;
  388     info->bmiHeader.biSize = sizeof(info->bmiHeader);
  389     info->bmiHeader.biWidth = size.cx;
  390     info->bmiHeader.biHeight = size.cy;
  391     info->bmiHeader.biBitCount = 32;
  392     info->bmiHeader.biPlanes = 1;
  393     info->bmiHeader.biCompression = BI_RGB;
  394 
  395     hdc = CreateCompatibleDC( 0 );
  396     if (!(dib = CreateDIBSection( 0, info, DIB_RGB_COLORS, &color_bits, NULL, 0 ))) goto done;
  397     SelectObject( hdc, dib );
  398     DrawIconEx( hdc, pos.x, pos.y, icon->image, width, height, 0, 0, DI_DEFAULTSIZE | DI_NORMAL );
  399 
  400     /* check if the icon was drawn with an alpha channel */
  401     for (i = 0, ptr = color_bits; i < size.cx * size.cy; i++)
  402         if ((has_alpha = (ptr[i] & 0xff000000) != 0)) break;
  403 
  404     if (!has_alpha)
  405     {
  406         unsigned int width_bytes = (size.cx + 31) / 32 * 4;
  407 
  408         info->bmiHeader.biBitCount = 1;
  409         info->bmiColors[0].rgbRed      = 0;
  410         info->bmiColors[0].rgbGreen    = 0;
  411         info->bmiColors[0].rgbBlue     = 0;
  412         info->bmiColors[0].rgbReserved = 0;
  413         info->bmiColors[1].rgbRed      = 0xff;
  414         info->bmiColors[1].rgbGreen    = 0xff;
  415         info->bmiColors[1].rgbBlue     = 0xff;
  416         info->bmiColors[1].rgbReserved = 0;
  417 
  418         if (!(mask = CreateDIBSection( 0, info, DIB_RGB_COLORS, &mask_bits, NULL, 0 ))) goto done;
  419         memset( mask_bits, 0xff, width_bytes * size.cy );
  420         SelectObject( hdc, mask );
  421         DrawIconEx( hdc, pos.x, pos.y, icon->image, width, height, 0, 0, DI_DEFAULTSIZE | DI_MASK );
  422 
  423         for (y = 0, ptr = color_bits; y < size.cy; y++)
  424             for (x = 0; x < size.cx; x++, ptr++)
  425                 if (!((((BYTE *)mask_bits)[y * width_bytes + x / 8] << (x % 8)) & 0x80))
  426                     *ptr |= 0xff000000;
  427 
  428         SelectObject( hdc, dib );
  429         DeleteObject( mask );
  430     }
  431 
  432     UpdateLayeredWindow( icon->window, 0, NULL, NULL, hdc, NULL, 0, &blend, ULW_ALPHA );
  433 done:
  434     HeapFree (GetProcessHeap(), 0, info);
  435     if (hdc) DeleteDC( hdc );
  436     if (dib) DeleteObject( dib );
  437 }
  438 
  439 static BOOL notify_owner( struct tray_icon *icon, UINT msg, LPARAM lparam )
  440 {
  441     WPARAM wp = icon->id;
  442     LPARAM lp = msg;
  443 
  444     if (icon->version >= NOTIFY_VERSION_4)
  445     {
  446         POINT pt = { (short)LOWORD(lparam), (short)HIWORD(lparam) };
  447 
  448         ClientToScreen( icon->window, &pt );
  449         wp = MAKEWPARAM( pt.x, pt.y );
  450         lp = MAKELPARAM( msg, icon->id );
  451     }
  452 
  453     TRACE( "relaying 0x%x\n", msg );
  454     if (!SendNotifyMessageW( icon->owner, icon->callback_message, wp, lp ) &&
  455         (GetLastError() == ERROR_INVALID_WINDOW_HANDLE))
  456     {
  457         WARN( "application window was destroyed, removing icon %u\n", icon->id );
  458         delete_icon( icon );
  459         return FALSE;
  460     }
  461     return TRUE;
  462 }
  463 
  464 /* window procedure for the individual tray icon window */
  465 static LRESULT WINAPI tray_icon_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
  466 {
  467     struct tray_icon *icon = NULL;
  468 
  469     TRACE("hwnd=%p, msg=0x%x\n", hwnd, msg);
  470 
  471     /* set the icon data for the window from the data passed into CreateWindow */
  472     if (msg == WM_NCCREATE)
  473         SetWindowLongPtrW(hwnd, GWLP_USERDATA, (LPARAM)((const CREATESTRUCTW *)lparam)->lpCreateParams);
  474 
  475     icon = (struct tray_icon *) GetWindowLongPtrW(hwnd, GWLP_USERDATA);
  476 
  477     switch (msg)
  478     {
  479     case WM_SIZE:
  480         if (icon->window && icon->layered) repaint_tray_icon( icon );
  481         break;
  482 
  483     case WM_PAINT:
  484         if (!icon->layered)
  485         {
  486             PAINTSTRUCT ps;
  487             RECT rc;
  488             HDC hdc;
  489             int cx = GetSystemMetrics( SM_CXSMICON );
  490             int cy = GetSystemMetrics( SM_CYSMICON );
  491 
  492             hdc = BeginPaint(hwnd, &ps);
  493             GetClientRect(hwnd, &rc);
  494             TRACE("painting rect %s\n", wine_dbgstr_rect(&rc));
  495             DrawIconEx( hdc, (rc.left + rc.right - cx) / 2, (rc.top + rc.bottom - cy) / 2,
  496                         icon->image, cx, cy, 0, 0, DI_DEFAULTSIZE|DI_NORMAL );
  497             EndPaint(hwnd, &ps);
  498             return 0;
  499         }
  500         break;
  501 
  502     case WM_MOUSEMOVE:
  503     case WM_LBUTTONDOWN:
  504     case WM_RBUTTONDOWN:
  505     case WM_MBUTTONDOWN:
  506     case WM_MBUTTONUP:
  507     case WM_LBUTTONDBLCLK:
  508     case WM_RBUTTONDBLCLK:
  509     case WM_MBUTTONDBLCLK:
  510         notify_owner( icon, msg, lparam );
  511         break;
  512 
  513     case WM_LBUTTONUP:
  514         if (!notify_owner( icon, msg, lparam )) break;
  515         if (icon->version > 0) notify_owner( icon, NIN_SELECT, lparam );
  516         break;
  517 
  518     case WM_RBUTTONUP:
  519         if (!notify_owner( icon, msg, lparam )) break;
  520         if (icon->version > 0) notify_owner( icon, WM_CONTEXTMENU, lparam );
  521         break;
  522 
  523     case WM_WINDOWPOSCHANGED:
  524         update_systray_balloon_position();
  525         break;
  526 
  527     case WM_TIMER:
  528         switch (wparam)
  529         {
  530         case BALLOON_CREATE_TIMER:
  531             balloon_create_timer( icon );
  532             break;
  533         case BALLOON_SHOW_TIMER:
  534             balloon_timer();
  535             break;
  536         }
  537         return 0;
  538 
  539     case WM_CLOSE:
  540         if (icon->display == -1)
  541         {
  542             TRACE( "icon %u no longer embedded\n", icon->id );
  543             hide_icon( icon );
  544             add_to_standalone_tray( icon );
  545         }
  546         return 0;
  547     }
  548     return DefWindowProcW( hwnd, msg, wparam, lparam );
  549 }
  550 
  551 /* find the X11 window owner the system tray selection */
  552 static Window get_systray_selection_owner( Display *display )
  553 {
  554     return XGetSelectionOwner( display, systray_atom );
  555 }
  556 
  557 static void get_systray_visual_info( Display *display, Window systray_window, XVisualInfo *info )
  558 {
  559     XVisualInfo *list, template;
  560     VisualID *visual_id;
  561     Atom type;
  562     int format, num;
  563     unsigned long count, remaining;
  564 
  565     *info = default_visual;
  566     if (XGetWindowProperty( display, systray_window, x11drv_atom(_NET_SYSTEM_TRAY_VISUAL), 0,
  567                             65536/sizeof(CARD32), False, XA_VISUALID, &type, &format, &count,
  568                             &remaining, (unsigned char **)&visual_id ))
  569         return;
  570 
  571     if (type == XA_VISUALID && format == 32)
  572     {
  573         template.visualid = visual_id[0];
  574         if ((list = XGetVisualInfo( display, VisualIDMask, &template, &num )))
  575         {
  576             *info = list[0];
  577             TRACE( "systray window %lx got visual %lx\n", systray_window, info->visualid );
  578             XFree( list );
  579         }
  580     }
  581     XFree( visual_id );
  582 }
  583 
  584 static BOOL init_systray(void)
  585 {
  586     static BOOL init_done;
  587     WNDCLASSEXW class;
  588     Display *display;
  589 
  590     if (is_virtual_desktop()) return FALSE;
  591     if (init_done) return TRUE;
  592 
  593     icon_cx = GetSystemMetrics( SM_CXSMICON ) + 2 * ICON_BORDER;
  594     icon_cy = GetSystemMetrics( SM_CYSMICON ) + 2 * ICON_BORDER;
  595 
  596     memset( &class, 0, sizeof(class) );
  597     class.cbSize        = sizeof(class);
  598     class.lpfnWndProc   = tray_icon_wndproc;
  599     class.hIcon         = LoadIconW(0, (LPCWSTR)IDI_WINLOGO);
  600     class.hCursor       = LoadCursorW( 0, (LPCWSTR)IDC_ARROW );
  601     class.lpszClassName = icon_classname;
  602     class.style         = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  603 
  604     if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
  605     {
  606         ERR( "Could not register icon tray window class\n" );
  607         return FALSE;
  608     }
  609 
  610     class.lpfnWndProc   = standalone_tray_wndproc;
  611     class.hbrBackground = (HBRUSH)COLOR_WINDOW;
  612     class.lpszClassName = tray_classname;
  613     class.style         = CS_DBLCLKS;
  614 
  615     if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
  616     {
  617         ERR( "Could not register standalone tray window class\n" );
  618         return FALSE;
  619     }
  620 
  621     display = thread_init_display();
  622     if (DefaultScreen( display ) == 0)
  623         systray_atom = x11drv_atom(_NET_SYSTEM_TRAY_S0);
  624     else
  625     {
  626         char systray_buffer[29]; /* strlen(_NET_SYSTEM_TRAY_S4294967295)+1 */
  627         sprintf( systray_buffer, "_NET_SYSTEM_TRAY_S%u", DefaultScreen( display ) );
  628         systray_atom = XInternAtom( display, systray_buffer, False );
  629     }
  630     XSelectInput( display, root_window, StructureNotifyMask );
  631 
  632     init_done = TRUE;
  633     return TRUE;
  634 }
  635 
  636 /* dock the given icon with the NETWM system tray */
  637 static void dock_systray_icon( Display *display, struct tray_icon *icon, Window systray_window )
  638 {
  639     Window window;
  640     XEvent ev;
  641     XSetWindowAttributes attr;
  642     XVisualInfo visual;
  643     struct x11drv_win_data *data;
  644 
  645     get_systray_visual_info( display, systray_window, &visual );
  646 
  647     icon->layered = (visual.depth == 32);
  648     icon->window = CreateWindowExW( icon->layered ? WS_EX_LAYERED : 0,
  649                                     icon_classname, NULL, WS_CLIPSIBLINGS | WS_POPUP,
  650                                     CW_USEDEFAULT, CW_USEDEFAULT, icon_cx, icon_cy,
  651                                     NULL, NULL, NULL, icon );
  652 
  653     if (!(data = get_win_data( icon->window ))) return;
  654     if (icon->layered) set_window_visual( data, &visual, TRUE );
  655     make_window_embedded( data );
  656     window = data->whole_window;
  657     release_win_data( data );
  658 
  659     create_tooltip( icon );
  660     ShowWindow( icon->window, SW_SHOWNA );
  661 
  662     TRACE( "icon window %p/%lx\n", icon->window, window );
  663 
  664     /* send the docking request message */
  665     ev.xclient.type = ClientMessage;
  666     ev.xclient.window = systray_window;
  667     ev.xclient.message_type = x11drv_atom( _NET_SYSTEM_TRAY_OPCODE );
  668     ev.xclient.format = 32;
  669     ev.xclient.data.l[0] = CurrentTime;
  670     ev.xclient.data.l[1] = SYSTEM_TRAY_REQUEST_DOCK;
  671     ev.xclient.data.l[2] = window;
  672     ev.xclient.data.l[3] = 0;
  673     ev.xclient.data.l[4] = 0;
  674     XSendEvent( display, systray_window, False, NoEventMask, &ev );
  675 
  676     if (!icon->layered)
  677     {
  678         attr.background_pixmap = ParentRelative;
  679         attr.bit_gravity = ForgetGravity;
  680         XChangeWindowAttributes( display, window, CWBackPixmap | CWBitGravity, &attr );
  681     }
  682     else repaint_tray_icon( icon );
  683 }
  684 
  685 /* dock systray windows again with the new owner */
  686 void change_systray_owner( Display *display, Window systray_window )
  687 {
  688     struct tray_icon *icon;
  689 
  690     TRACE( "new owner %lx\n", systray_window );
  691     LIST_FOR_EACH_ENTRY( icon, &icon_list, struct tray_icon, entry )
  692     {
  693         if (icon->display == -1) continue;
  694         hide_icon( icon );
  695         dock_systray_icon( display, icon, systray_window );
  696     }
  697 }
  698 
  699 /* hide a tray icon */
  700 static BOOL hide_icon( struct tray_icon *icon )
  701 {
  702     struct x11drv_win_data *data;
  703 
  704     TRACE( "id=0x%x, hwnd=%p\n", icon->id, icon->owner );
  705 
  706     if (!icon->window) return TRUE;  /* already hidden */
  707 
  708     /* make sure we don't try to unmap it, it confuses some systray docks */
  709     if ((data = get_win_data( icon->window )))
  710     {
  711         if (data->embedded) data->mapped = FALSE;
  712         release_win_data( data );
  713     }
  714     DestroyWindow(icon->window);
  715     DestroyWindow(icon->tooltip);
  716     icon->window = 0;
  717     icon->layered = FALSE;
  718     icon->tooltip = 0;
  719     remove_from_standalone_tray( icon );
  720     update_balloon( icon );
  721     return TRUE;
  722 }
  723 
  724 /* make the icon visible */
  725 static BOOL show_icon( struct tray_icon *icon )
  726 {
  727     Window systray_window;
  728     Display *display = thread_init_display();
  729 
  730     TRACE( "id=0x%x, hwnd=%p\n", icon->id, icon->owner );
  731 
  732     if (icon->window) return TRUE;  /* already shown */
  733 
  734     if ((systray_window = get_systray_selection_owner( display )))
  735         dock_systray_icon( display, icon, systray_window );
  736     else
  737         add_to_standalone_tray( icon );
  738 
  739     update_balloon( icon );
  740     return TRUE;
  741 }
  742 
  743 /* Modifies an existing icon record */
  744 static BOOL modify_icon( struct tray_icon *icon, NOTIFYICONDATAW *nid )
  745 {
  746     TRACE( "id=0x%x hwnd=%p flags=%x\n", nid->uID, nid->hWnd, nid->uFlags );
  747 
  748     if (nid->uFlags & NIF_STATE)
  749     {
  750         icon->state = (icon->state & ~nid->dwStateMask) | (nid->dwState & nid->dwStateMask);
  751     }
  752 
  753     if (nid->uFlags & NIF_ICON)
  754     {
  755         if (icon->image) DestroyIcon(icon->image);
  756         icon->image = CopyIcon(nid->hIcon);
  757         if (icon->window)
  758         {
  759             if (icon->display != -1) InvalidateRect( icon->window, NULL, TRUE );
  760             else if (icon->layered) repaint_tray_icon( icon );
  761             else
  762             {
  763                 Window win = X11DRV_get_whole_window( icon->window );
  764                 if (win) XClearArea( gdi_display, win, 0, 0, 0, 0, True );
  765             }
  766         }
  767     }
  768 
  769     if (nid->uFlags & NIF_MESSAGE)
  770     {
  771         icon->callback_message = nid->uCallbackMessage;
  772     }
  773     if (nid->uFlags & NIF_TIP)
  774     {
  775         lstrcpynW(icon->tiptext, nid->szTip, ARRAY_SIZE( icon->tiptext ));
  776         if (icon->tooltip) update_tooltip_text(icon);
  777     }
  778     if (nid->uFlags & NIF_INFO && nid->cbSize >= NOTIFYICONDATAA_V2_SIZE)
  779     {
  780         lstrcpynW( icon->info_text, nid->szInfo, ARRAY_SIZE( icon->info_text ));
  781         lstrcpynW( icon->info_title, nid->szInfoTitle, ARRAY_SIZE( icon->info_title ));
  782         icon->info_flags = nid->dwInfoFlags;
  783         icon->info_timeout = max(min(nid->u.uTimeout, BALLOON_SHOW_MAX_TIMEOUT), BALLOON_SHOW_MIN_TIMEOUT);
  784         icon->info_icon = nid->hBalloonIcon;
  785         update_balloon( icon );
  786     }
  787     if (icon->state & NIS_HIDDEN) hide_icon( icon );
  788     else show_icon( icon );
  789     return TRUE;
  790 }
  791 
  792 /* Adds a new icon record to the list */
  793 static BOOL add_icon(NOTIFYICONDATAW *nid)
  794 {
  795     struct tray_icon  *icon;
  796 
  797     TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
  798 
  799     if ((icon = get_icon(nid->hWnd, nid->uID)))
  800     {
  801         WARN("duplicate tray icon add, buggy app?\n");
  802         return FALSE;
  803     }
  804 
  805     if (!(icon = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*icon))))
  806     {
  807         ERR("out of memory\n");
  808         return FALSE;
  809     }
  810 
  811     ZeroMemory(icon, sizeof(struct tray_icon));
  812     icon->id     = nid->uID;
  813     icon->owner  = nid->hWnd;
  814     icon->display = -1;
  815 
  816     list_add_tail(&icon_list, &icon->entry);
  817 
  818     return modify_icon( icon, nid );
  819 }
  820 
  821 /* delete tray icon window and icon structure */
  822 static BOOL delete_icon( struct tray_icon *icon )
  823 {
  824     hide_icon( icon );
  825     list_remove( &icon->entry );
  826     DestroyIcon( icon->image );
  827     HeapFree( GetProcessHeap(), 0, icon );
  828     return TRUE;
  829 }
  830 
  831 /* cleanup all icons for a given window */
  832 static void cleanup_icons( HWND owner )
  833 {
  834     struct tray_icon *this, *next;
  835 
  836     LIST_FOR_EACH_ENTRY_SAFE( this, next, &icon_list, struct tray_icon, entry )
  837         if (this->owner == owner) delete_icon( this );
  838 }
  839 
  840 
  841 /***********************************************************************
  842  *              wine_notify_icon   (X11DRV.@)
  843  *
  844  * Driver-side implementation of Shell_NotifyIcon.
  845  */
  846 int CDECL wine_notify_icon( DWORD msg, NOTIFYICONDATAW *data )
  847 {
  848     BOOL ret = FALSE;
  849     struct tray_icon *icon;
  850 
  851     switch (msg)
  852     {
  853     case NIM_ADD:
  854         if (!init_systray()) return -1;  /* fall back to default handling */
  855         ret = add_icon( data );
  856         break;
  857     case NIM_DELETE:
  858         if ((icon = get_icon( data->hWnd, data->uID ))) ret = delete_icon( icon );
  859         break;
  860     case NIM_MODIFY:
  861         if ((icon = get_icon( data->hWnd, data->uID ))) ret = modify_icon( icon, data );
  862         break;
  863     case NIM_SETVERSION:
  864         if ((icon = get_icon( data->hWnd, data->uID )))
  865         {
  866             icon->version = data->u.uVersion;
  867             ret = TRUE;
  868         }
  869         break;
  870     case 0xdead:  /* Wine extension: owner window has died */
  871         cleanup_icons( data->hWnd );
  872         break;
  873     default:
  874         FIXME( "unhandled tray message: %u\n", msg );
  875         break;
  876     }
  877     return ret;
  878 }