src/video/cybergfx/SDL_cgxmodes.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 26 Apr 2001 16:45:43 +0000
changeset 0 74212992fb08
child 21 75a95f82bc1f
permissions -rw-r--r--
Initial revision
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@0
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@0
    20
    slouken@devolution.com
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
/* Utilities for getting and setting the X display mode */
slouken@0
    29
slouken@0
    30
/*
slouken@0
    31
#include <stdlib.h>
slouken@0
    32
#include <string.h>
slouken@0
    33
*/
slouken@0
    34
slouken@0
    35
#include "SDL_timer.h"
slouken@0
    36
#include "SDL_error.h"
slouken@0
    37
#include "SDL_events.h"
slouken@0
    38
#include "SDL_events_c.h"
slouken@0
    39
#include "SDL_cgxvideo.h"
slouken@0
    40
#include "SDL_cgxwm_c.h"
slouken@0
    41
#include "SDL_cgxmodes_c.h"
slouken@0
    42
slouken@0
    43
#define CGX_DEBUG
slouken@0
    44
slouken@0
    45
static void set_best_resolution(_THIS, int width, int height)
slouken@0
    46
{
slouken@0
    47
	Uint32 idok;
slouken@0
    48
	int depth=8;
slouken@0
    49
slouken@0
    50
	if(SDL_Display)
slouken@0
    51
		depth=GetCyberMapAttr(SDL_Display->RastPort.BitMap,CYBRMATTR_DEPTH);
slouken@0
    52
slouken@0
    53
	idok=BestCModeIDTags(CYBRBIDTG_NominalWidth,width,
slouken@0
    54
				CYBRBIDTG_NominalHeight,height,
slouken@0
    55
				CYBRBIDTG_Depth,depth,
slouken@0
    56
				TAG_DONE);
slouken@0
    57
slouken@0
    58
	if(idok!=INVALID_ID)
slouken@0
    59
	{
slouken@0
    60
		if(SDL_Display)
slouken@0
    61
		{
slouken@0
    62
			if(currently_fullscreen)
slouken@0
    63
				CloseScreen(SDL_Display);
slouken@0
    64
			else
slouken@0
    65
				UnlockPubScreen(NULL,SDL_Display);
slouken@0
    66
		}
slouken@0
    67
		SDL_Display=GFX_Display=OpenScreenTags(NULL,SA_Width,width,SA_Height,height,
slouken@0
    68
											SA_Depth,depth,SA_DisplayID,idok,
slouken@0
    69
											TAG_DONE);
slouken@0
    70
	}
slouken@0
    71
}
slouken@0
    72
slouken@0
    73
static void get_real_resolution(_THIS, int* w, int* h)
slouken@0
    74
{
slouken@0
    75
    *w = SDL_Display->Width;
slouken@0
    76
    *h = SDL_Display->Height;
slouken@0
    77
}
slouken@0
    78
slouken@0
    79
static void add_visual(_THIS, int depth, int class)
slouken@0
    80
{
slouken@0
    81
	Uint32 tID;
slouken@0
    82
slouken@0
    83
	tID=BestCModeIDTags(CYBRBIDTG_Depth,depth,
slouken@0
    84
						CYBRBIDTG_NominalWidth,640,
slouken@0
    85
						CYBRBIDTG_NominalHeight,480,
slouken@0
    86
						TAG_DONE);
slouken@0
    87
slouken@0
    88
	if(tID!=INVALID_ID)
slouken@0
    89
	{
slouken@0
    90
		int n = this->hidden->nvisuals;
slouken@0
    91
slouken@0
    92
		this->hidden->visuals[n].depth = depth;
slouken@0
    93
		this->hidden->visuals[n].visual = tID;
slouken@0
    94
		this->hidden->visuals[n].bpp = GetCyberIDAttr(CYBRIDATTR_BPPIX,tID);
slouken@0
    95
		this->hidden->nvisuals++;
slouken@0
    96
	}
slouken@0
    97
}
slouken@0
    98
slouken@0
    99
#define TrueColor 1
slouken@0
   100
#define PseudoColor 2
slouken@0
   101
slouken@0
   102
int CGX_GetVideoModes(_THIS)
slouken@0
   103
{
slouken@0
   104
    int i;
slouken@0
   105
	ULONG nextid;
slouken@0
   106
	int nmodes=0;
slouken@0
   107
slouken@0
   108
	SDL_modelist=NULL;
slouken@0
   109
slouken@0
   110
	nextid=NextDisplayInfo(INVALID_ID);
slouken@0
   111
slouken@0
   112
	while(nextid!=INVALID_ID)
slouken@0
   113
	{
slouken@0
   114
		if(IsCyberModeID(nextid))
slouken@0
   115
		{
slouken@0
   116
			DisplayInfoHandle h;
slouken@0
   117
slouken@0
   118
			if(h=FindDisplayInfo(nextid))
slouken@0
   119
			{
slouken@0
   120
				struct DimensionInfo info;
slouken@0
   121
slouken@0
   122
				if(GetDisplayInfoData(h,(char *)&info,sizeof(struct DimensionInfo),DTAG_DIMS,NULL))
slouken@0
   123
				{
slouken@0
   124
					int ok=0;
slouken@0
   125
slouken@0
   126
					for(i=0;i<nmodes;i++)
slouken@0
   127
					{
slouken@0
   128
						if(	SDL_modelist[i]->w == (info.Nominal.MaxX+1) &&
slouken@0
   129
							SDL_modelist[i]->h == (info.Nominal.MaxY+1) )
slouken@0
   130
							ok=1;
slouken@0
   131
					}
slouken@0
   132
slouken@0
   133
					if(!ok)
slouken@0
   134
					{
slouken@0
   135
						nmodes++;
slouken@0
   136
slouken@0
   137
						SDL_modelist = (SDL_Rect **)realloc(SDL_modelist,(nmodes+1)*sizeof(SDL_Rect *));
slouken@0
   138
						SDL_modelist[nmodes]=NULL;
slouken@0
   139
slouken@0
   140
						if ( SDL_modelist ) 
slouken@0
   141
						{
slouken@0
   142
							SDL_modelist[nmodes-1] = (SDL_Rect *)malloc(sizeof(SDL_Rect));
slouken@0
   143
slouken@0
   144
							if ( SDL_modelist[nmodes-1] == NULL ) 
slouken@0
   145
								break;
slouken@0
   146
slouken@0
   147
							SDL_modelist[nmodes-1]->x = 0;
slouken@0
   148
							SDL_modelist[nmodes-1]->y = 0;
slouken@0
   149
							SDL_modelist[nmodes-1]->w = info.Nominal.MaxX+1;
slouken@0
   150
							SDL_modelist[nmodes-1]->h = info.Nominal.MaxY+1;
slouken@0
   151
						}
slouken@0
   152
					}																	
slouken@0
   153
				}
slouken@0
   154
			}
slouken@0
   155
		}
slouken@0
   156
		nextid=NextDisplayInfo(nextid);
slouken@0
   157
	}
slouken@0
   158
slouken@0
   159
slouken@0
   160
	this->hidden->nvisuals = 0;
slouken@0
   161
	/* Search for the visuals in deepest-first order, so that the first
slouken@0
   162
	   will be the richest one */
slouken@0
   163
	add_visual(this, 32, TrueColor);
slouken@0
   164
	add_visual(this, 24, TrueColor);
slouken@0
   165
	add_visual(this, 16, TrueColor);
slouken@0
   166
	add_visual(this, 15, TrueColor);
slouken@0
   167
	add_visual(this, 8, PseudoColor);
slouken@0
   168
slouken@0
   169
	if(this->hidden->nvisuals == 0) {
slouken@0
   170
	    SDL_SetError("Found no sufficiently capable CGX visuals");
slouken@0
   171
		    return -1;
slouken@0
   172
	}
slouken@0
   173
slouken@0
   174
    if ( SDL_modelist == NULL ) {
slouken@0
   175
        SDL_modelist = (SDL_Rect **)malloc((1+1)*sizeof(SDL_Rect *));
slouken@0
   176
        i = 0;
slouken@0
   177
        if ( SDL_modelist ) {
slouken@0
   178
            SDL_modelist[i] = (SDL_Rect *)malloc(sizeof(SDL_Rect));
slouken@0
   179
            if ( SDL_modelist[i] ) {
slouken@0
   180
                SDL_modelist[i]->x = 0;
slouken@0
   181
                SDL_modelist[i]->y = 0;
slouken@0
   182
                SDL_modelist[i]->w = SDL_Display->Width;
slouken@0
   183
                SDL_modelist[i]->h = SDL_Display->Height;
slouken@0
   184
                ++i;
slouken@0
   185
            }
slouken@0
   186
            SDL_modelist[i] = NULL;
slouken@0
   187
        }
slouken@0
   188
    }
slouken@0
   189
slouken@0
   190
    D( if ( SDL_modelist ) {
slouken@0
   191
        bug("CGX video mode list: (%ld)\n",nmodes);
slouken@0
   192
        for ( i=0; SDL_modelist[i]; ++i ) {
slouken@0
   193
            bug( "\t%ld x %ld\n",
slouken@0
   194
                SDL_modelist[i]->w, SDL_modelist[i]->h);
slouken@0
   195
        }
slouken@0
   196
    	}
slouken@0
   197
    );
slouken@0
   198
slouken@0
   199
    D(  { bug("CGX visuals list: (%ld)\n",this->hidden->nvisuals);
slouken@0
   200
slouken@0
   201
	for(i=0;i<this->hidden->nvisuals;i++)
slouken@0
   202
		bug("\t%lx - depth: %ld bpp: %ld\n",this->hidden->visuals[i].visual,this->hidden->visuals[i].depth,this->hidden->visuals[i].bpp);
slouken@0
   203
	}
slouken@0
   204
    );
slouken@0
   205
    return 0;
slouken@0
   206
}
slouken@0
   207
slouken@0
   208
int CGX_SupportedVisual(_THIS, SDL_PixelFormat *format)
slouken@0
   209
{
slouken@0
   210
    int i;
slouken@0
   211
    for(i = 0; i < this->hidden->nvisuals; i++)
slouken@0
   212
	{
slouken@0
   213
		if(this->hidden->visuals[i].depth == format->BitsPerPixel) // Era bpp
slouken@0
   214
		    return 1;
slouken@0
   215
	}
slouken@0
   216
    return 0;
slouken@0
   217
}
slouken@0
   218
slouken@0
   219
SDL_Rect **CGX_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
slouken@0
   220
{
slouken@0
   221
    if ( CGX_SupportedVisual(this, format) ) {
slouken@0
   222
        if ( flags & SDL_FULLSCREEN ) {
slouken@0
   223
            return(SDL_modelist);
slouken@0
   224
        } else {
slouken@0
   225
            return((SDL_Rect **)-1);
slouken@0
   226
        }
slouken@0
   227
    } else {
slouken@0
   228
        return((SDL_Rect **)0);
slouken@0
   229
    }
slouken@0
   230
}
slouken@0
   231
slouken@0
   232
void CGX_FreeVideoModes(_THIS)
slouken@0
   233
{
slouken@0
   234
    int i;
slouken@0
   235
slouken@0
   236
    if ( SDL_modelist ) {
slouken@0
   237
        for ( i=0; SDL_modelist[i]; ++i ) {
slouken@0
   238
            free(SDL_modelist[i]);
slouken@0
   239
        }
slouken@0
   240
        free(SDL_modelist);
slouken@0
   241
        SDL_modelist = NULL;
slouken@0
   242
    }
slouken@0
   243
}
slouken@0
   244
slouken@0
   245
int CGX_ResizeFullScreen(_THIS)
slouken@0
   246
{
slouken@0
   247
    int x, y;
slouken@0
   248
    int real_w, real_h;
slouken@0
   249
slouken@0
   250
    if ( currently_fullscreen ) {
slouken@0
   251
/* Per ora non faccio nulla qui */
slouken@0
   252
    }
slouken@0
   253
    return(1);
slouken@0
   254
}
slouken@0
   255
slouken@0
   256
void _QueueEnterFullScreen(_THIS)
slouken@0
   257
{
slouken@0
   258
}
slouken@0
   259
slouken@0
   260
int CGX_EnterFullScreen(_THIS)
slouken@0
   261
{
slouken@0
   262
    int okay;
slouken@0
   263
slouken@0
   264
    okay = 1;
slouken@0
   265
    if ( ! currently_fullscreen ) 
slouken@0
   266
	{
slouken@0
   267
        int real_w, real_h;
slouken@0
   268
slouken@0
   269
        /* Map the fullscreen window to blank the screen */
slouken@0
   270
        get_real_resolution(this, &real_w, &real_h);
slouken@0
   271
		
slouken@0
   272
		CGX_DestroyWindow(this,this->screen);
slouken@0
   273
		set_best_resolution(this, real_w,real_h);
slouken@0
   274
slouken@0
   275
        /* Grab the mouse on the fullscreen window
slouken@0
   276
           The event handling will know when we become active, and then
slouken@0
   277
           enter fullscreen mode if we can't grab the mouse this time.
slouken@0
   278
         */
slouken@0
   279
#if 0
slouken@0
   280
        if ( (XGrabPointer(SDL_Display, FSwindow, True, 0,
slouken@0
   281
                          GrabModeAsync, GrabModeAsync,
slouken@0
   282
                          FSwindow, None, CurrentTime) != GrabSuccess) ||
slouken@0
   283
             (XGrabKeyboard(SDL_Display, WMwindow, True,
slouken@0
   284
                          GrabModeAsync, GrabModeAsync, CurrentTime) != 0) ) {
slouken@0
   285
            XUnmapWindow(SDL_Display, FSwindow);
slouken@0
   286
            X11_WaitUnmapped(this, FSwindow);
slouken@0
   287
            X11_QueueEnterFullScreen(this);
slouken@0
   288
            return(0);
slouken@0
   289
        }
slouken@0
   290
#endif
slouken@0
   291
slouken@0
   292
        currently_fullscreen = 1;
slouken@0
   293
slouken@0
   294
		CGX_CreateWindow(this,this->screen,real_w,real_h,GetCyberMapAttr(SDL_Display->RastPort.BitMap,CYBRMATTR_DEPTH),this->screen->flags);
slouken@0
   295
slouken@0
   296
        /* Set the new resolution */
slouken@0
   297
        okay = CGX_ResizeFullScreen(this);
slouken@0
   298
        if ( ! okay ) {
slouken@0
   299
            CGX_LeaveFullScreen(this);
slouken@0
   300
        }
slouken@0
   301
	/* Set the colormap */
slouken@0
   302
/*
slouken@0
   303
		if ( SDL_XColorMap ) {
slouken@0
   304
			XInstallColormap(SDL_Display, SDL_XColorMap);
slouken@0
   305
		}
slouken@0
   306
*/
slouken@0
   307
    }    
slouken@0
   308
//    CGX_GrabInputNoLock(this, this->input_grab | SDL_GRAB_FULLSCREEN);
slouken@0
   309
    return(okay);
slouken@0
   310
}
slouken@0
   311
slouken@0
   312
int CGX_LeaveFullScreen(_THIS)
slouken@0
   313
{
slouken@0
   314
    if ( currently_fullscreen ) {
slouken@0
   315
		int width,height;
slouken@0
   316
		if ( SDL_Window ) {
slouken@0
   317
			CloseWindow(SDL_Window);
slouken@0
   318
			SDL_Window=NULL;
slouken@0
   319
		}
slouken@0
   320
		CloseScreen(SDL_Display);
slouken@0
   321
slouken@0
   322
		GFX_Display=SDL_Display=LockPubScreen(NULL);
slouken@0
   323
slouken@0
   324
	        currently_fullscreen = 0;
slouken@0
   325
slouken@0
   326
		CGX_CreateWindow(this,this->screen,this->screen->w,this->screen->h,GetCyberMapAttr(SDL_Display->RastPort.BitMap,CYBRMATTR_DEPTH),this->screen->flags);
slouken@0
   327
		CGX_ResizeImage(this,this->screen,0L);
slouken@0
   328
    }
slouken@0
   329
slouken@0
   330
    return(0);
slouken@0
   331
}