rem: win_state = (WWindowState*)wGetWindowSavedState(wwin->main_window); } else { win_state = (WWindowState*)wGetWindowSavedState(window);
add: win_state = (WWindowState*)wWindowGetSavedState(wwin->main_window); } else { win_state = (WWindowState*)wWindowGetSavedState(window);
context: wManageWindow(WScreen *scr, Window window){    WWindow *wwin;    int width, height;    XWindowAttributes wattribs;        XSetWindowAttributes attribs;    int iconic = 0;    WSavedState *wstate;    WWindowState *win_state;#ifdef MWM_HINTS    MWMHints *motif_hints = NULL;#endif    int window_level;    int foo, gx, gy;    int resized_client;    int workspace = -1;    char *title;    /* mutex. */    XGrabServer(dpy);    XSync(dpy, 0);    /* make sure the window is still there */    if (!XGetWindowAttributes(dpy, window, &wattribs)) {	XUngrabServer(dpy);	return NULL;    }    wwin = wWindowCreate();    XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);#ifdef DEBUG    printf("managing window %x\n", (unsigned)window);#endif#ifdef SHAPE    {	int junk;	unsigned int ujunk;	int b_shaped;		XShapeSelectInput(dpy, window, ShapeNotifyMask);	XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk, 			   &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);	wwin->flags.shaped = b_shaped;    }#endif        /*     *--------------------------------------------------     *      * Get hints and other information in properties     *     *--------------------------------------------------      */    wwin->wm_hints = XGetWMHints(dpy, window);    PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);    /* setup descriptor */    wwin->client_win = window;    wwin->screen_ptr = scr;    wwin->old_border_width = wattribs.border_width;    attribs.event_mask = CLIENT_EVENTS;    attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;    attribs.save_under = False;    XChangeWindowAttributes(dpy, window, CWEventMask|CWDontPropagate			    |CWSaveUnder, &attribs);        XSetWindowBorderWidth(dpy, window, 0);    /* fill in property/hint data */    if (!wFetchName(dpy, window, &title)) {	title = NULL;    }    /* get hints from GNUstep app */    if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {	wwin->wm_gnustep_attr=NULL;    }    #ifdef MWM_HINTS    PropGetMotifWMHints(window, &motif_hints);#endif /* MWM_HINTS */    if (!PropGetClientLeader(window, &wwin->client_leader)) {	wwin->client_leader = None;    } else {	wwin->main_window = wwin->client_leader;    }    if (wwin->wm_hints)	XFree(wwin->wm_hints);	    wwin->wm_hints = XGetWMHints(dpy, window);    if (wwin->wm_hints)  {	if ((wwin->wm_hints->flags&StateHint) 	    && (wwin->wm_hints->initial_state == IconicState)) {	    iconic = 1;	    /* don't do iconify animation */	    wwin->flags.skip_next_animation = 1;	}      	if (wwin->wm_hints->flags & WindowGroupHint) {	    wwin->group_id = wwin->wm_hints->window_group;	    /* window_group has priority over CLIENT_LEADER */	    wwin->main_window = wwin->group_id;	} else {	    wwin->group_id = None;	}	if (wwin->wm_hints->flags & UrgencyHint)	  wwin->flags.urgent = 1;    } else {	wwin->group_id = None;    }    PropGetProtocols(window, &wwin->protocols);        if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {	wwin->transient_for = None;    } else {	if (wwin->transient_for==None || wwin->transient_for==window) {	    wwin->transient_for = scr->root_win;	} else {	    WWindow *owner;	    owner = wWindowFor(wwin->transient_for);	    if (owner && owner->main_window!=None) {		wwin->main_window = owner->main_window;            } /*else {		wwin->main_window = None;            }*/	    /* don't let transients start miniaturized if their owners	     * are not */	    if (owner && !owner->flags.miniaturized && iconic) {		iconic = 0;		if (wwin->wm_hints)		    wwin->wm_hints->initial_state = NormalState;	    }	}    }    /* guess the focus mode */    wwin->focus_mode = getFocusMode(wwin);    /* get geometry stuff */    GetNormalHints(wwin, &wattribs, True);    /* get colormap windows */    GetColormapWindows(wwin);    /*     *--------------------------------------------------     *      * Setup the decoration/window attributes and     * geometry     *      *--------------------------------------------------     */    /* sets global default stuff */    wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class, 			   &wwin->window_flags, True);    /*     * Decoration setting is done in this precedence (lower to higher)     * - use default in the resource database     * - guess some settings     * - use GNUstep/external window attributes     * - set hints specified for the app in the resource DB     */    wwin->window_flags.broken_close = 0;        if (wwin->protocols.DELETE_WINDOW)	wwin->window_flags.kill_close = 0;    else	wwin->window_flags.kill_close = 1;    /* transients can't be iconified or maximized */    if (wwin->transient_for) {	wwin->window_flags.no_miniaturizable = 1;	wwin->window_flags.no_miniaturize_button = 1;#ifdef DEBUG	printf("%x is transient for %x\n", (unsigned)window, 	       (unsigned)wwin->transient_for);#endif    }        /* if the window can't be resized, remove the resizebar */    if (wwin->normal_hints->flags & (PMinSize|PMaxSize)	&& (wwin->normal_hints->min_width==wwin->normal_hints->max_width)	&& (wwin->normal_hints->min_height==wwin->normal_hints->max_height)) {	wwin->window_flags.no_resizable = 1;	wwin->window_flags.no_resizebar = 1;    }        /* set GNUstep window attributes */    if (wwin->wm_gnustep_attr) {	setupGNUstepHints(&wwin->window_flags, wwin->wm_gnustep_attr);	if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {	    window_level = wwin->wm_gnustep_attr->window_level;	} else {	    /* setup defaults */	    window_level = WMNormalWindowLevel;	}    } else {#ifdef MWM_HINTS	if (motif_hints) {	    setupMWMHints(&wwin->window_flags, motif_hints);	}#endif /* MWM_HINTS */	if (wwin->window_flags.floating)	    window_level = WMFloatingWindowLevel;	else	    window_level = WMNormalWindowLevel;    }#ifdef MWM_HINTS    if (motif_hints)	XFree(motif_hints);#endif    /*     * Set attributes specified only for that window/class.     * This might do duplicate work with the 1st wDefaultFillAttributes().     * TODO: Do something about that.     */    wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class, 			   &wwin->window_flags, False);            /*     * Sanity checks for attributes that depend on other attributes      */    wWindowCheckAttributeSanity(wwin, &wwin->window_flags);    wwin->window_flags.no_shadeable = wwin->window_flags.no_titlebar;    /*     * Make broken apps behave as a nice app.     */    if (wwin->window_flags.emulate_appicon) {	wwin->main_window = wwin->client_win;    }    /*     *------------------------------------------------------------      *      * Setup the initial state of the window     *      *------------------------------------------------------------     */    if (wwin->window_flags.start_miniaturized	&& !wwin->window_flags.no_miniaturizable) {	wwin->flags.miniaturized = 1;        iconic = 1;    }    /* if there is a saved state, restore it */    win_state = NULL;    if (wwin->main_window!=None/* && wwin->main_window!=window*/) {        win_state = (WWindowState*)wGetWindowSavedState(wwin->main_window);    } else {        win_state = (WWindowState*)wGetWindowSavedState(window);    }    if (win_state && !(wwin->wm_hints && wwin->wm_hints->flags&StateHint &&                       wwin->wm_hints->initial_state==WithdrawnState)) {        if (win_state->state->hidden>0)            wwin->flags.hidden = win_state->state->hidden;        if (win_state->state->shaded>0 && !wwin->window_flags.no_shadeable)            wwin->flags.shaded = win_state->state->shaded;        if (win_state->state->miniaturized>0 &&            !wwin->window_flags.no_miniaturizable) {            wwin->flags.miniaturized = win_state->state->miniaturized;            iconic = 1;        }        if (!wwin->window_flags.omnipresent) {            int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,                                              wwin->wm_class);            if (w < 0 || w >= scr->workspace_count) {                workspace = win_state->state->workspace;                if (workspace>=scr->workspace_count)                    workspace = scr->current_workspace;            } else {                workspace = w;            }        } else {            workspace = scr->current_workspace;        }    }    /* if we're restarting, restore saved state. This will overwrite previous */    if (!getSavedState(window, &wstate)) {        wstate = NULL;    } else {        wwin->flags.shaded = wstate->shaded;        wwin->flags.hidden = wstate->hidden;        wwin->flags.miniaturized = 0;        workspace = wstate->workspace;    }    /* set workspace on which the window starts */        if (workspace >= 0) {	if (workspace > scr->workspace_count-1) {	    wWorkspaceMake(scr, workspace - scr->workspace_count + 1);	    workspace = scr->workspace_count - 1;	}    } else {	int w;	w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);        if (w>=0 && w<scr->workspace_count && !wwin->window_flags.omnipresent) {            workspace = w;        } else {            workspace = scr->current_workspace;        }    }    /* setup window geometry */    if (win_state && win_state->state->use_geometry) {        width = win_state->state->w;        height = win_state->state->h;    } else {        width = wwin->client.width;        height = wwin->client.height;    }    wWindowConstrainSize(wwin, &width, &height);    resized_client = 0;    if (wwin->client.width != width) {	wwin->client.width = width;	resized_client = 1;    }    if (wwin->client.height != height) {	wwin->client.height = height;	resized_client = 1;    }    /* do not ask for window placement if the window is     * transient, during startup, if the initial workspace is another one     * or if the window wants to     * start iconic.     * If geometry was saved, restore it. */    if (win_state && win_state->state->use_geometry) {        wwin->frame_x = win_state->state->x;        wwin->frame_y = win_state->state->y;    } else if (wwin->transient_for==None && !scr->flags.startup &&               workspace==scr->current_workspace && !iconic &&               !(wwin->normal_hints->flags & (USPosition|PPosition))) {        PlaceWindow(wwin, &wwin->frame_x, &wwin->frame_y);    } else {        wwin->frame_x = wwin->client.x;        wwin->frame_y = wwin->client.y;    }        if (wwin->window_flags.dont_move_off)	wScreenBringInside(scr, &wwin->frame_x, &wwin->frame_y, width, height);        /*      *--------------------------------------------------     *      * Create frame, borders and do reparenting     *     *--------------------------------------------------     */        foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;    if (!wwin->window_flags.no_titlebar)	foo |= WFF_TITLEBAR;    if (!wwin->window_flags.no_resizebar)	foo |= WFF_RESIZEBAR;    wwin->frame = wFrameWindowCreate(scr, window_level, 				     wwin->frame_x, wwin->frame_y,				     width, height, foo,				     scr->window_title_texture,				     (WTexture**)scr->resizebar_texture,				     scr->window_title_pixel, 				     &scr->window_title_gc, 				     &scr->title_font);            wwin->frame->flags.is_client_window_frame = 1;    wwin->frame->flags.justification = wPreferences.title_justification;    /* setup button images */    wWindowUpdateButtonImages(wwin);        /* hide unused buttons */    foo = 0;    if (wwin->window_flags.no_close_button)	foo |= WFF_RIGHT_BUTTON;    if (wwin->window_flags.no_miniaturize_button)	foo |= WFF_LEFT_BUTTON;    if (foo!=0)	wFrameWindowHideButton(wwin->frame, foo);            wwin->frame->child = wwin;    wFrameWindowChangeTitle(wwin->frame, title ? title : DEF_WINDOW_TITLE);    if (title)	XFree(title);        wwin->frame->workspace = workspace;        wwin->frame->on_click_left = windowIconifyClick;        wwin->frame->on_click_right = windowCloseClick;    wwin->frame->on_dblclick_right = windowCloseDblClick;        wwin->frame->on_mousedown_titlebar = titlebarMouseDown;    wwin->frame->on_dblclick_titlebar = titlebarDblClick;        wwin->frame->on_mousedown_resizebar = resizebarMouseDown;    wwin->client.y += wwin->frame->top_width;    XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,		    0, wwin->frame->top_width);    wClientGetGravityOffsets(wwin, &gx, &gy);    /* set the positio of the frame on screen */    wwin->frame_x += gx * (wwin->old_border_width - FRAME_BORDER_WIDTH);    wwin->frame_y += gy * (wwin->old_border_width - FRAME_BORDER_WIDTH);    /* if gravity is to the south, account for the border sizes */    if (gy > 0)	wwin->frame_y -= wwin->frame->top_width + wwin->frame->bottom_width;    /*      * force wWindowConfigure() to update the client window's size     */    wwin->client.width = 0;    wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, width, height);    /*     *--------------------------------------------------     *      * Setup descriptors and save window to internal     * lists     *     *--------------------------------------------------      */    if (wwin->main_window!=None) {	WApplication *app;	WWindow *leader;	/* Leader windows do not necessary set themselves as leaders.	 * If this is the case, point the leader of this window to	 * itself */	leader = wWindowFor(wwin->main_window);	if (leader && leader->main_window==None) {	    leader->main_window = leader->client_win;       	}	app = wApplicationCreate(scr, wwin->main_window);        if (app) {            app->last_workspace = workspace;            /*             * Do application specific stuff, like setting application             * wide attributes.             */            if (wwin->flags.hidden) {                /* if the window was set to hidden because it was hidden                 * in a previous incarnation and that state was restored */                app->flags.hidden = 1;            }            if (app->flags.hidden) {                wwin->flags.hidden = 1;            }        }    }    /* setup the frame descriptor */    wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;    wwin->frame->core->descriptor.parent = wwin;    wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;    /* don't let windows go away if we die */    XAddToSaveSet(dpy, window);    XLowerWindow(dpy, window);    /* if window is in this workspace and should be mapped, then  map it */    /*    printf("%s %i %i %i %i\n", wwin->wm_class,iconic,workspace == scr->current_workspace,	   wwin->flags.hidden,(wwin->wm_hints && (wwin->wm_hints->flags & StateHint)	     && wwin->wm_hints->initial_state == WithdrawnState));     * */    if (!iconic && (workspace == scr->current_workspace		    || wwin->window_flags.omnipresent)        && !wwin->flags.hidden	&& !(wwin->wm_hints && (wwin->wm_hints->flags & StateHint)	     && wwin->wm_hints->initial_state == WithdrawnState)) {        /* The following "if" is to avoid crashing of clients that expect         * WM_STATE set before they get mapped. Else WM_STATE is set later,         * after the return from this function.         */        if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {            wClientSetState(wwin, wwin->wm_hints->initial_state, None);        } else {            wClientSetState(wwin, NormalState, None);        }	if (wPreferences.superfluous && !wPreferences.no_animations	    && !scr->flags.startup && wwin->transient_for==None) {	    DoWindowBirth(wwin);	}	XMapSubwindows(dpy, wwin->frame->core->window);	wWindowMap(wwin);    } else {	XMapSubwindows(dpy, wwin->frame->core->window);    }        /* setup stacking descriptor */    if (wPreferences.on_top_transients && wwin->transient_for!=None	&& wwin->transient_for!=scr->root_win) {	WWindow *tmp;	tmp = wWindowFor(wwin->transient_for);	if (tmp)	    wwin->frame->core->stacking->child_of = tmp->frame->core;    } else {	wwin->frame->core->stacking->child_of = NULL;    }        if (!scr->focused_window) {	/* first window on the list */	wwin->next = NULL;	wwin->prev = NULL;	scr->focused_window = wwin;    } else {	WWindow *tmp;	/* add window at beginning of focus window list */	tmp = scr->focused_window;	while (tmp->prev)	  tmp = tmp->prev;	tmp->prev = wwin;	wwin->next = tmp;	wwin->prev = NULL;    }    /*     *--------------------------------------------------     *      * Final preparations before window is ready to go     *      *--------------------------------------------------      */        wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);			        if (!iconic && workspace == scr->current_workspace) {	WWindow *tmp = wWindowFor(wwin->transient_for);	if ((tmp && tmp->flags.focused) || wPreferences.auto_focus)	    wSetFocusTo(scr, wwin);    } else {	wwin->flags.ignore_next_unmap = 1;    }    wWindowResetMouseGrabs(wwin);        if (!wwin->window_flags.no_bind_keys) {	wWindowSetKeyGrabs(wwin);    }    /*     * Prevent window withdrawal when getting the      * unmap notifies generated during reparenting     */    wwin->flags.mapped=0;    XUngrabServer(dpy);    XSync(dpy, 0);    wColormapInstallForWindow(wwin->screen_ptr, scr->cmap_window);    UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_ADD);        /*     *--------------------------------------------------     *  Cleanup temporary stuff     *--------------------------------------------------     */    if (wstate)      XFree(wstate);    if (win_state)        wDeleteWindowSavedState(win_state);    return wwin;}