1 /*
2  * support.c - user interface support code
3  *
4  * Copyright (c) 2000 Sebastian Bauer
5  * Copyright (c) 2000-2003 Atari800 development team (see DOC/CREDITS)
6  *
7  * This file is part of the Atari800 emulator project which emulates
8  * the Atari 400, 800, 800XL, 130XE, and 5200 8-bit computers.
9  *
10  * Atari800 is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * Atari800 is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Atari800; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24 
25 #define __USE_INLINE__
26 #define DoSuperMethod IDoSuperMethod
27 #define DoSuperMethodA IDoSuperMethodA
28 #define DoMethod IDoMethod
29 #define DoMethodA IDoMethodA
30 
31 #include <string.h>
32 
33 #include <cybergraphx/cybergraphics.h>
34 #include <libraries/gadtools.h>
35 
36 #include <clib/alib_protos.h>
37 
38 #include <proto/exec.h>
39 #include <proto/dos.h>
40 #include <proto/graphics.h>
41 #include <proto/cybergraphics.h>
42 #include <proto/intuition.h>
43 
44 /**************************************************************************
45  Some general supports
46 **************************************************************************/
StrLen(const STRPTR str)47 LONG StrLen( const STRPTR str)
48 {
49 	if(str) return (LONG)strlen(str);
50 	return 0;
51 }
52 
StrCopy(const STRPTR str)53 STRPTR StrCopy( const STRPTR str )
54 {
55 	STRPTR dst;
56 	if( !str ) return NULL;
57 	if( !*str) return NULL;
58 
59 	dst = (STRPTR)AllocVec(strlen(str)+1,0);
60 	if(dst) strcpy(dst,str);
61 	return dst;
62 }
63 
GetFullPath(STRPTR drw,STRPTR file)64 STRPTR GetFullPath( STRPTR drw, STRPTR file)
65 {
66 	WORD dl = StrLen(drw);
67 	WORD fl = StrLen( file );
68 	LONG length = dl + fl + 6;
69 	STRPTR fp = (STRPTR)AllocVec( length+1, 0 );
70 
71 	if( fp )
72 	{
73 		strcpy( fp, drw );
74 
75 		if( AddPart( fp, file, length ))	return fp;
76 		else FreeVec( fp );
77 	}
78 	return NULL;
79 }
80 
AddSuffix(const STRPTR name,const STRPTR suf)81 STRPTR AddSuffix(const STRPTR name, const STRPTR suf)
82 {
83 	STRPTR str;
84 	if(!strstr(name,suf))
85 	{
86 		LONG len = StrLen(name)+StrLen(suf)+2;
87 		str = (STRPTR)AllocVec(len,0);
88 		if(str)
89 		{
90 			strcpy(str,name);
91 			strcat(str,suf);
92 		}
93 	}	else str = StrCopy(name);
94 	return str;
95 }
96 
GetBestID(ULONG width,ULONG height,ULONG depth)97 ULONG GetBestID( ULONG width, ULONG height, ULONG depth )
98 {
99 	struct Screen *defscr;
100 	ULONG displayID;
101 
102 	if ((defscr = LockPubScreen(NULL)))
103 	{
104 		struct ViewPort *vp;
105 
106 		vp = &defscr->ViewPort;
107 
108 		displayID = BestModeID( BIDTAG_Depth,depth,
109 									BIDTAG_NominalWidth, width,
110 									BIDTAG_NominalHeight, height,
111 									BIDTAG_MonitorID, GetVPModeID( vp ) & MONITOR_ID_MASK,
112 									TAG_DONE);
113 
114 		if (CyberGfxBase)
115 		{
116 			if (IsCyberModeID(displayID))
117 			{
118 				struct DimensionInfo dims;
119 
120 				/* Get the normal dimensions of the returned displayID */
121 				if (GetDisplayInfoData(NULL,&dims,sizeof(dims),DTAG_DIMS,displayID) > 0)
122 				{
123 					ULONG modeWidth = dims.Nominal.MaxX - dims.Nominal.MinX + 1;
124 					ULONG modeHeight = dims.Nominal.MaxY - dims.Nominal.MinY + 1;
125 
126 					/* If dimensions differ to "much", try to get a better one via cybergfx calls */
127 					if (modeWidth > width * 4 / 3 || modeHeight > height * 4 / 3)
128 					{
129 						displayID = BestCModeIDTags(
130 							CYBRBIDTG_Depth, depth,
131 							CYBRBIDTG_NominalWidth, width,
132 							CYBRBIDTG_NominalHeight, height,
133 							CYBRBIDTG_MonitorID, GetVPModeID( vp ) & MONITOR_ID_MASK,
134 							TAG_DONE);
135 					}
136 				}
137 			}
138 		}
139 
140 		UnlockPubScreen( NULL, defscr );
141 	} else displayID = INVALID_ID;
142 
143 	if (displayID == INVALID_ID)
144 	{
145 		displayID = BestModeID( BIDTAG_Depth,depth,
146 									BIDTAG_NominalWidth, width,
147 									BIDTAG_NominalHeight, height,
148 									TAG_DONE);
149 	}
150 
151 	return displayID;
152 }
153 
GetDisplayName(ULONG displayid)154 STRPTR GetDisplayName(ULONG displayid)
155 {
156 	STATIC struct NameInfo DisplayNameInfo;
157 	STATIC char DisplayNameBuffer[256];
158 
159 	LONG i, v;
160 
161 	i	= 0;
162 	v	= GetDisplayInfoData(NULL,	(UBYTE *) &DisplayNameInfo, sizeof(DisplayNameInfo),
163 									DTAG_NAME, displayid);
164 
165 	if(v > sizeof(struct QueryHeader))
166     {
167 		for(; (i < sizeof(DisplayNameBuffer) - 1) && DisplayNameInfo.Name[i]; i++)
168 			DisplayNameBuffer[i]	= DisplayNameInfo.Name[i];
169 	}
170 
171 	if(displayid == INVALID_ID)
172 		strcpy(DisplayNameBuffer, "InvalidID"/*GetMessage(MSG_INVALID)*/);
173 	else
174 	{
175 		if(i < sizeof(DisplayNameBuffer) - sizeof(" (0x00000000)"))
176 		{
177 			DisplayNameBuffer[i++]	= ' ';
178 			DisplayNameBuffer[i++]	= '(';
179 			DisplayNameBuffer[i++]	= '0';
180 			DisplayNameBuffer[i++]	= 'x';
181 
182 			for(v = 28; (v >= 0) && (!((displayid >> v) & 0xf)); v -= 4);
183 
184 			if(v < 0)
185 				DisplayNameBuffer[i++]	= '0';
186 
187 			for(; (v >= 0); v -= 4)
188 			{
189 				if(((displayid >> v) & 0xf) > 9)
190 					DisplayNameBuffer[i++]	= ((displayid >> v) & 0xf) + 'a' - 10;
191 				else
192 					DisplayNameBuffer[i++]	= ((displayid >> v) & 0xf) + '0';
193 			}
194 			DisplayNameBuffer[i++]	= ')';
195 		}
196 
197 		DisplayNameBuffer[i++]	= 0;
198 	}
199 
200 	return DisplayNameBuffer;
201 }
202 
FindUserData(struct Menu * menu,APTR userdata)203 APTR FindUserData( struct Menu *menu, APTR userdata)
204 {
205 	while(menu)
206 	{
207 		struct MenuItem *mi;
208 
209 		if(GTMENU_USERDATA( menu ) == userdata) return menu;
210 
211 		mi = menu->FirstItem;
212 		while(mi)
213 		{
214 			struct MenuItem *smi;
215 
216 			if(GTMENUITEM_USERDATA( mi ) == userdata) return mi;
217 
218 			smi = mi->SubItem;
219 			while(smi)
220 			{
221 				if(GTMENUITEM_USERDATA( smi ) == userdata) return smi;
222 				smi = smi->NextItem;
223 			}
224 			mi = mi->NextItem;
225 		}
226 		menu = menu->NextMenu;
227 	}
228 	return NULL;
229 }
230 
231 
232 /**************************************************************************
233  ...
234 **************************************************************************/
OpenLibraryInterface(STRPTR name,int version,void * interface_ptr)235 struct Library *OpenLibraryInterface(STRPTR name, int version, void *interface_ptr)
236 {
237 	struct Library *lib = OpenLibrary(name,version);
238 	struct Interface *iface;
239 	if (!lib) return NULL;
240 
241 	iface = GetInterface(lib,"main",1,NULL);
242 	if (!iface)
243 	{
244 		CloseLibrary(lib);
245 		return NULL;
246 	}
247 	*((struct Interface**)interface_ptr) = iface;
248 	return lib;
249 }
250 
251 /**************************************************************************
252  ...
253 **************************************************************************/
CloseLibraryInterface(struct Library * lib,void * interface)254 void CloseLibraryInterface(struct Library *lib, void *interface)
255 {
256 	DropInterface(interface);
257 	CloseLibrary(lib);
258 }
259