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