Fixed codesets.
[AROS.git] / rom / intuition / openworkbench.c
bloba575369ba115d5a4756115f3a3f5b9c3cf4fbeeb
1 /*
2 Copyright 1995-2011, The AROS Development Team. All rights reserved.
3 Copyright 2001-2003, The MorphOS Development Team. All Rights Reserved.
4 $Id$
5 */
7 #include <aros/config.h>
8 #include <intuition/intuition.h>
9 #include <proto/intuition.h>
10 #include <proto/graphics.h>
12 #include "intuition_intern.h"
14 static ULONG FindMode(ULONG width, ULONG height, ULONG depth, struct IntuitionBase *IntuitionBase)
16 struct GfxBase *GfxBase = GetPrivIBase(IntuitionBase)->GfxBase;
17 ULONG modeid;
18 struct TagItem modetags[] =
20 { BIDTAG_DesiredWidth, width }, /* 0 */
21 { BIDTAG_DesiredHeight, height }, /* 1 */
22 { BIDTAG_Depth, depth }, /* 2 */
23 { TAG_DONE, 0 }
26 /* Try to find the specified mode */
27 modeid = BestModeIDA(modetags);
28 D(bug("[OpenWorkbench] Size: %dx%d, depth: %d, ModeID 0x%08X\n", width, height, depth, modeid));
30 if (modeid == INVALID_ID)
32 /* If failed, we can have this resolution, but not this depth. Try 4 colors. */
33 modetags[2].ti_Data = 2;
35 modeid = BestModeIDA(modetags);
36 D(bug("[OpenWorkbench] Size: %dx%d, depth: 2, ModeID 0x%08X\n", width, height, modeid));
39 return modeid;
42 /*****************************************************************************
44 NAME */
46 AROS_LH0(IPTR, OpenWorkBench,
48 /* SYNOPSIS */
50 /* LOCATION */
51 struct IntuitionBase *, IntuitionBase, 35, Intuition)
53 /* FUNCTION
54 Attempt to open the Workbench screen.
56 INPUTS
57 None.
59 RESULT
60 Tries to (re)open WorkBench screen. If successful return value
61 is a pointer to the screen structure, which shouldn't be used,
62 because other programs may close the WorkBench and make the
63 pointer invalid.
64 If this function fails the return value is NULL.
66 NOTES
68 EXAMPLE
70 BUGS
72 SEE ALSO
73 CloseWorkBench()
75 INTERNALS
77 *****************************************************************************/
79 AROS_LIBFUNC_INIT
81 struct GfxBase *GfxBase = GetPrivIBase(IntuitionBase)->GfxBase;
82 struct Screen *wbscreen;
84 DEBUG_OPENWORKBENCH(dprintf("OpenWorkBench: <%s>\n",
85 FindTask(NULL)->tc_Node.ln_Name));
87 /* Intuition not up yet? */
88 if (!GetPrivIBase(IntuitionBase)->DefaultPointer)
89 return FALSE;
91 LockPubScreenList();
93 wbscreen = GetPrivIBase(IntuitionBase)->WorkBench;
95 DEBUG_OPENWORKBENCH(dprintf("OpenWorkBench: Workbench 0x%lx\n",
96 (ULONG) wbscreen));
98 if (wbscreen)
100 DEBUG_OPENWORKBENCH(dprintf("OpenWorkBench: returning Workbench screen at 0x%lx\n",
101 (ULONG) wbscreen));
103 UnlockPubScreenList();
105 FireScreenNotifyMessage((IPTR) wbscreen, SNOTIFY_AFTER_OPENWB, IntuitionBase);
107 return (IPTR)wbscreen;
109 else
111 /* Open the Workbench screen if we don't have one. */
113 WORD width = GetPrivIBase(IntuitionBase)->ScreenModePrefs.smp_Width;
114 WORD height = GetPrivIBase(IntuitionBase)->ScreenModePrefs.smp_Height;
115 WORD depth = GetPrivIBase(IntuitionBase)->ScreenModePrefs.smp_Depth;
116 ULONG modeid = GetPrivIBase(IntuitionBase)->ScreenModePrefs.smp_DisplayID;
117 APTR disphandle;
119 struct TagItem screenTags[] =
121 { SA_Width, 0 }, /* 0 */
122 { SA_Height, 0 }, /* 1 */
123 { SA_Depth, depth }, /* 2 */
124 { SA_DisplayID, 0 }, /* 3 */
125 { SA_LikeWorkbench, TRUE }, /* 4 */
126 { SA_Type, WBENCHSCREEN }, /* 5 */
127 { SA_Title, (IPTR) "Workbench Screen" }, /* 6 */
128 { SA_PubName, (IPTR) "Workbench" }, /* 7 */
129 { SA_SharePens, TRUE }, /* 8 */
130 { TAG_END, 0 }
133 D(bug("[OpenWorkbench] Requested size: %dx%d, depth: %d, ModeID: 0x%08lX\n", width, height, depth, modeid));
135 /* First check if the specified ModeID exists in the system */
136 disphandle = FindDisplayInfo(modeid);
137 if (!disphandle)
139 D(bug("[OpenWorkbench] Invalid ModeID given\n"));
140 modeid = INVALID_ID;
143 if (modeid == INVALID_ID)
146 * We were unable to find the ModeID specified in our prefs. Need to find a replacement.
147 * First we'll try to look up a mode corresponding to user-specified width, height and depth.
150 /* Specifying -1's here causes BestModeIDA() to fail, fix up the values */
151 if (width == STDSCREENWIDTH)
153 D(bug("[OpenWorkbench] Using default width %d\n", AROS_DEFAULT_WBWIDTH));
154 width = AROS_DEFAULT_WBWIDTH;
156 if (height == STDSCREENHEIGHT)
158 D(bug("[OpenWorkbench] Using default height %d\n", AROS_DEFAULT_WBHEIGHT));
159 height = AROS_DEFAULT_WBHEIGHT;
161 if (depth == -1)
162 depth = AROS_DEFAULT_WBDEPTH;
164 #ifdef __mc68000
165 /* FIXME: less hacky RTG detection */
166 /* select 640x480 if we appear to have RTG hardware (instead of standard PAL/NTSC mode) */
167 if (height < 480)
169 modeid = BestModeID(BIDTAG_DesiredWidth, 800, BIDTAG_DesiredHeight, 600,
170 BIDTAG_Depth, 8, TAG_DONE);
172 if (modeid != INVALID_ID)
174 /* If we have 800x600 or better mode, we assume we have RTG hardware */
175 height = 480;
178 #endif
180 modeid = FindMode(width, height, depth, IntuitionBase);
183 if (modeid == INVALID_ID)
185 /* We don't have any modes with the specified resolution. Try Amiga default (640x200) */
186 modeid = FindMode(640, 200, depth, IntuitionBase);
189 if (modeid == INVALID_ID)
192 * There's no even 640x200. Perhaps we are on some mobile device.
193 * Here we try the smallest known screen. This size was picked up
194 * from configure's defaults for old PalmPilot port.
196 modeid = FindMode(160, 160, depth, IntuitionBase);
199 if (modeid != INVALID_ID)
201 struct DimensionInfo dim;
203 #define BOUND(min, val, max) \
204 (((val) == -1) ? -1 : ((min) > (val)) ? (min) : ((max) < (val)) ? (max) : (val))
206 /* Now fix up our specified size to fit into mode's limits. */
207 if (GetDisplayInfoData(NULL, (UBYTE *)&dim, sizeof(dim), DTAG_DIMS, modeid))
209 D(bug("[OpenWorkbench] Minimum size: %dx%d\n", dim.MinRasterWidth, dim.MinRasterHeight));
210 D(bug("[OpenWorkbench] Maximum size: %dx%d\n", dim.MaxRasterWidth, dim.MaxRasterHeight));
211 D(bug("[OpenWorkbench] Maximum depth: %d\n", dim.MaxDepth));
213 width = BOUND(dim.MinRasterWidth, width, dim.MaxRasterWidth);
214 height = BOUND(dim.MinRasterHeight, height, dim.MaxRasterHeight);
215 depth = BOUND(0, depth, dim.MaxDepth);
216 D(bug("[OpenWorkbench] Corrected size: %dx%d %dbpp\n", width, height, depth));
218 GetPrivIBase(IntuitionBase)->ScreenModePrefs.smp_Width = width;
219 GetPrivIBase(IntuitionBase)->ScreenModePrefs.smp_Height = height;
220 GetPrivIBase(IntuitionBase)->ScreenModePrefs.smp_Depth = depth;
224 * Remember this ModeID because OpenScreen() with SA_LikeWorkbench set to TRUE
225 * looks at this field. We MUST have something valid here.
227 GetPrivIBase(IntuitionBase)->ScreenModePrefs.smp_DisplayID = modeid;
229 screenTags[0].ti_Data = width;
230 screenTags[1].ti_Data = height;
231 screenTags[2].ti_Data = depth;
232 screenTags[3].ti_Data = modeid;
234 DEBUG_OPENWORKBENCH(dprintf("OpenWorkBench: Trying to open Workbench screen\n"));
236 FireScreenNotifyMessage((IPTR) NULL, SNOTIFY_BEFORE_OPENWB, IntuitionBase);
238 wbscreen = OpenScreenTagList(NULL, screenTags);
240 else
242 * If we have no disphandle here, we are in a real trouble. We have no display modes
243 * in our database and we can't open a screen at all. We're dead.
244 * However note that in some special cases this Alert() may return. This happens
245 * when Alert() attempts to issue an Intuition requester and hits this point
246 * because there are no display drivers (yet). In this case this Alert() will be
247 * silently ignored.
249 Alert(AN_SysScrnType);
251 if( !wbscreen )
253 DEBUG_OPENWORKBENCH(dprintf("OpenWorkBench: failed to open Workbench screen !!!!\n"));
255 UnlockPubScreenList();
256 return 0;
259 GetPrivIBase(IntuitionBase)->WorkBench = wbscreen;
261 /* Make the screen public. */
262 PubScreenStatus( wbscreen, 0 );
265 /* We have opened the Workbench Screen. Now tell the Workbench process
266 to open it's windows, if there is one. We still do have the pub screen
267 list locked. But while sending the Message to the Workbench task we
268 must unlock the semaphore, otherwise there can be deadlocks if the
269 Workbench task itself does something which locks the pub screen list.
271 But if we unlock the pub screen list, then some other task could try
272 to close the Workbench screen in the meantime. The trick to solve
273 this problem is to increase the psn_VisitorCount of the Workbench
274 screen here, before unlocking the pub screen list. This way the
275 Workbench screen cannot go away. */
277 GetPrivScreen(wbscreen)->pubScrNode->psn_VisitorCount++;
278 DEBUG_VISITOR(dprintf("OpenWorkbench: new VisitorCount %ld\n",
279 GetPrivScreen(wbscreen)->pubScrNode->psn_VisitorCount));
281 UnlockPubScreenList();
283 DEBUG_VISITOR(dprintf("OpenWorkbench: notify Workbench\n"));
285 /* Don't call this function while pub screen list is locked! */
286 TellWBTaskToOpenWindows(IntuitionBase);
288 /* Now fix the psn_VisitorCount we have increased by one, above. It's probably
289 better to do this by hand, instead of calling UnlockPubScreen, because Un-
290 lockPubScreen can send signal to psn_SigTask. */
292 LockPubScreenList();
293 GetPrivScreen(wbscreen)->pubScrNode->psn_VisitorCount--;
294 DEBUG_VISITOR(dprintf("OpenWorkbench: new VisitorCount %ld\n",
295 GetPrivScreen(wbscreen)->pubScrNode->psn_VisitorCount));
296 UnlockPubScreenList();
298 FireScreenNotifyMessage((IPTR) wbscreen, SNOTIFY_AFTER_OPENWB, IntuitionBase);
300 return (IPTR)wbscreen;
302 AROS_LIBFUNC_EXIT
304 } /* OpenWorkBench */