GHOST_TSuccess GHOST_Window::setDrawingContextType(GHOST_TDrawingContextType type)
{
	GHOST_TSuccess success = GHOST_kSuccess;
	if (type != m_drawingContextType) {
		success = removeDrawingContext();
		if (success) {
			success = installDrawingContext(type);
			m_drawingContextType = type;
		}
		else {
			m_drawingContextType = GHOST_kDrawingContextTypeNone;
		}
	}
	return success;
}
GHOST_WindowCarbon::GHOST_WindowCarbon(
	const STR_String& title,
	GHOST_TInt32 left,
	GHOST_TInt32 top,
	GHOST_TUns32 width,
	GHOST_TUns32 height,
	GHOST_TWindowState state,
	GHOST_TDrawingContextType type,
	const bool stereoVisual,
	const GHOST_TUns16 numOfAASamples
) :
	GHOST_Window(width, height, state, GHOST_kDrawingContextTypeNone),
	m_windowRef(0),
	m_grafPtr(0),
	m_aglCtx(0),
	m_customCursor(0),
	m_fullScreenDirty(false)
{
    Str255 title255;
	OSStatus err;
	
	//fprintf(stderr," main screen top %i left %i height %i width %i\n", top, left, height, width);
	
	if (state >= GHOST_kWindowState8Normal ) {
		if(state == GHOST_kWindowState8Normal) state= GHOST_kWindowStateNormal;
		else if(state == GHOST_kWindowState8Maximized) state= GHOST_kWindowStateMaximized;
		else if(state == GHOST_kWindowState8Minimized) state= GHOST_kWindowStateMinimized;
		else if(state == GHOST_kWindowState8FullScreen) state= GHOST_kWindowStateFullScreen;
		
		// state = state - 8;	this was the simple version of above code, doesnt work in gcc 4.0
		
		setMac_windowState(1);
	} else 
		setMac_windowState(0);

	if (state != GHOST_kWindowStateFullScreen) {
        Rect bnds = { top, left, top+height, left+width };
        // Boolean visible = (state == GHOST_kWindowStateNormal) || (state == GHOST_kWindowStateMaximized); /*unused*/
        gen2mac(title, title255);
        
		err =  ::CreateNewWindow( kDocumentWindowClass,
								 kWindowStandardDocumentAttributes+kWindowLiveResizeAttribute,
								 &bnds,
								 &m_windowRef);
		
		if ( err != noErr) {
			fprintf(stderr," error creating window %i \n",(int)err);
		} else {
			
			::SetWRefCon(m_windowRef,(SInt32)this);
			setTitle(title);
			err = InstallWindowEventHandler (m_windowRef, myWEventHandlerProc, GetEventTypeCount(kWEvents), kWEvents,NULL,NULL); 
			if ( err != noErr) {
				fprintf(stderr," error creating handler %i \n",(int)err);
			} else {
				//	::TransitionWindow (m_windowRef,kWindowZoomTransitionEffect,kWindowShowTransitionAction,NULL);
				::ShowWindow(m_windowRef);
				::MoveWindow (m_windowRef, left, top,true);
				
			}
		}
        if (m_windowRef) {
            m_grafPtr = ::GetWindowPort(m_windowRef);
            setDrawingContextType(type);
            updateDrawingContext();
            activateDrawingContext();
        }
		if(ugly_hack==NULL) {
			ugly_hack= m_windowRef;
			// when started from commandline, window remains in the back... also for play anim
			ProcessSerialNumber psn;
			GetCurrentProcess(&psn);
			SetFrontProcess(&psn);
		}
    }
    else {
    /*
        Rect bnds = { top, left, top+height, left+width };
        gen2mac("", title255);
        m_windowRef = ::NewCWindow(
            nil,							// Storage 
            &bnds,							// Bounding rectangle of the window
            title255,						// Title of the window
            0,								// Window initially visible
            plainDBox, 						// procID
            (WindowRef)-1L,					// Put window before all other windows
            0,								// Window has minimize box
            (SInt32)this);					// Store a pointer to the class in the refCon
    */
        //GHOST_PRINT("GHOST_WindowCarbon::GHOST_WindowCarbon(): creating full-screen OpenGL context\n");
        setDrawingContextType(GHOST_kDrawingContextTypeOpenGL);;installDrawingContext(GHOST_kDrawingContextTypeOpenGL);
        updateDrawingContext();
        activateDrawingContext();        

	m_tablet.Active = GHOST_kTabletModeNone;
    }
}
GHOST_WindowWayland::GHOST_WindowWayland(GHOST_SystemWayland *system,
                                 const STR_String& title,
                                 GHOST_TInt32 left,
                                 GHOST_TInt32 top,
                                 GHOST_TUns32 width,
                                 GHOST_TUns32 height,
                                 GHOST_TWindowState state,
                                 const GHOST_TEmbedderWindowID parentWindow,
                                 GHOST_TDrawingContextType type,
                                 const bool stereoVisual,
                                 const bool exclusive,
                                 const GHOST_TUns16 numOfAASamples
                                 )
	: GHOST_Window(width, height, state, type, stereoVisual, exclusive, numOfAASamples)
	, m_system(system)
	, m_surface(NULL)
	, m_shell_surface(NULL)
	, m_window(NULL)
	, m_egl_surface(EGL_NO_SURFACE)
	, m_egl_context(EGL_NO_CONTEXT)
	, m_x(left)
	, m_y(top)
	, m_width(width)
	, m_height(height)
	, m_state(GHOST_kWindowStateNormal)
	, m_redraw(this)
	, m_sync(this)
{
	wl_display *display = m_system->getDisplay();
	wl_compositor *compositor = m_system->getCompositor();
	wl_shell *shell = m_system->getShell();
	EGLDisplay egl_display = m_system->getEglDisplay();

	assert(display);
	assert(compositor);
	assert(shell);

	m_surface = WL_CHK(wl_compositor_create_surface(compositor));

	m_shell_surface =
		WL_CHK(wl_shell_get_shell_surface(shell, m_surface));

	ADD_LISTENER(shell_surface);

	m_window = WL_CHK(wl_egl_window_create(m_surface, width, height));

	m_egl_surface =
		EGL_CHK(eglCreateWindowSurface(egl_display, m_system->getEglConf(),
			m_window, NULL));


	assert(m_egl_surface != EGL_NO_SURFACE);

	setTitle(title);

	/* now set up the rendering context. */
	if (installDrawingContext(type) == GHOST_kSuccess) {
		// m_valid_setup = true;
		GHOST_PRINT("Created window\n");
	}

	EGL_CHK(wl_shell_surface_set_toplevel(m_shell_surface));
	resize();
	m_sync.initialize();
}