Inside Wine; HWND と直接結びついているデータ

[目次] (https://qiita.com/Yutaka_Aoki/items/9d38ea98406f4096435b)

# Inside Wine; HWND と直接結びついているデータ


## HWND と直接結びついているデータ

今分かっているだけで、大きく分けて 2種類ある。

```c:
1. typedef struct tagWND WND;
2. struct x11drv_win_data;
```

`1.` は、X Window に依存しないデータ。`2.` は X Window に依存するデータらしい。なお、WND が x11drv_win_data の基底クラス、というわけではなく、（完全に）独立したデータになっているらしい


## struct x11drv_win_data


```c:~/wine/dlls/winex11.drv/x11drv.h
/* x11drv private window data */
struct x11drv_win_data {
    Display    *display;        /* display connection for the thread owning the window */
    XVisualInfo vis;            /* X visual used by this window */
    Colormap    colormap;       /* colormap if non-default visual */
    HWND        hwnd;           /* hwnd that this private data belongs to */
    Window      whole_window;   /* X window for the complete window */
    Window      client_window;  /* X window for the client area */
    RECT        window_rect;    /* USER window rectangle relative to parent */
    RECT        whole_rect;     /* X window rectangle for the whole window relative to parent */
    RECT        client_rect;    /* client area relative to parent */
    XIC         xic;            /* X input context */
    BOOL        managed : 1;    /* is window managed? */
    BOOL        mapped : 1;     /* is window mapped? (in either normal or iconic state) */
    BOOL        iconic : 1;     /* is window in iconic state? */
    BOOL        embedded : 1;   /* is window an XEMBED client? */
    BOOL        shaped : 1;     /* is window using a custom region shape? */
    BOOL        layered : 1;    /* is window layered and with valid attributes? */
    BOOL        use_alpha : 1;  /* does window use an alpha channel? */
    int         wm_state;       /* current value of the WM_STATE property */
    DWORD       net_wm_state;   /* bit mask of active x11drv_net_wm_state values */
    Window      embedder;       /* window id of embedder */
    unsigned long configure_serial; /* serial number of last configure request */
    struct window_surface *surface;
    Pixmap         icon_pixmap;
    Pixmap         icon_mask;
    unsigned long *icon_bits;
    unsigned int   icon_size;
};
```

```c:~/wine/dlls/user32.win.h
typedef struct tagWND
{
    struct user_object obj;       /* object header */
    HWND           parent;        /* Window parent */
    HWND           owner;         /* Window owner */
    struct tagCLASS *class;       /* Window class */
    struct dce    *dce;           /* DCE pointer */
    WNDPROC        winproc;       /* Window procedure */
    DWORD          tid;           /* Owner thread id */
    HINSTANCE      hInstance;     /* Window hInstance (from CreateWindow) */
    RECT           rectClient;    /* Client area rel. to parent client area */
    RECT           rectWindow;    /* Whole window rel. to parent client area */
    RECT           visible_rect;  /* Visible part of the whole rect, rel. to parent client area */
    RECT           normal_rect;   /* Normal window rect saved when maximized/minimized */
    POINT          min_pos;       /* Position for minimized window */
    POINT          max_pos;       /* Position for maximized window */
    HWND           icon_title;    /* Icon title window */
    LPWSTR         text;          /* Window text */
    void          *pScroll;       /* Scroll-bar info */
    DWORD          dwStyle;       /* Window style (from CreateWindow) */
    DWORD          dwExStyle;     /* Extended style (from CreateWindowEx) */
    UINT_PTR       wIDmenu;       /* ID or hmenu (from CreateWindow) */
    DWORD          helpContext;   /* Help context ID */
    UINT           flags;         /* Misc. flags (see below) */
    HMENU          hSysMenu;      /* window's copy of System Menu */
    HICON          hIcon;         /* window's icon */
    HICON          hIconSmall;    /* window's small icon */
    HICON          hIconSmall2;   /* window's secondary small icon, derived from hIcon */
    struct window_surface *surface; /* Window surface if any */
    struct tagDIALOGINFO *dlgInfo;/* Dialog additional info (dialogs only) */
    int            pixel_format;  /* Pixel format set by the graphics driver */
    int            cbWndExtra;    /* class cbWndExtra at window creation */
    DWORD_PTR      userdata;      /* User private data */
    DWORD          wExtra[1];     /* Window extra bytes */
} WND;


#define WND_OTHER_PROCESS ((WND *)1)  /* returned by WIN_GetPtr on unknown window handles */
#define WND_DESKTOP       ((WND *)2)  /* returned by WIN_GetPtr on the desktop window */
```

```c:~/wine/dlls/user32/user_private.h
enum user_obj_type {
    USER_WINDOW = 1,  /* window */
    USER_MENU,        /* menu */
    USER_ACCEL,       /* accelerator */
    USER_ICON,        /* icon or cursor */
    USER_DWP          /* DeferWindowPos structure */
};

struct user_object {
    HANDLE             handle;
    enum user_obj_type type;
};
```


## HWND から struct x11drv_win_data * の取得法

```c:
struct x11drv_win_data *data = get_win_data( hwnd );
```

## HWND から WND * の取得法

```c:
WND   *win  = WIN_GetPtr( hwnd );
```



### struct x11drv_win_data *get_win_data( HWND hwnd )

```c:~/wine/dlls/winex11.drv/window.c
// Lock and return the X11 data structure associated with a window.
struct x11drv_win_data *get_win_data( HWND hwnd )
{
	char *data;

	if (!hwnd) return NULL;
	EnterCriticalSection( &win_data_section );
	if (!XFindContext( gdi_display, (XID)hwnd, win_data_context, &data ))
		return (struct x11drv_win_data *)data;
	LeaveCriticalSection( &win_data_section );
	return NULL;
}
```

### WND *WIN_GetPtr( HWND hwnd )

```c:~/wine/dlls/user32/win.c
/***********************************************************************
 *           WIN_GetPtr
 *
 * Return a pointer to the WND structure if local to the process,
 * or WND_OTHER_PROCESS if handle may be valid in other process.
 * If ret value is a valid pointer, it must be released with WIN_ReleasePtr.
 */
WND *WIN_GetPtr( HWND hwnd )
{
	WND *ptr;

	if ((ptr = get_user_handle_ptr( hwnd, USER_WINDOW )) == WND_OTHER_PROCESS)
	{
		if (is_desktop_window( hwnd )) ptr = WND_DESKTOP;
	}
	return ptr;
}
```




## struct x11drv_win_data を作成する関数

```c:
struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd, const RECT *window_rect,
                                                const RECT *client_rect )
{
    ・・・
}
```