Updated PCI IDs to latest snapshot.
[tangerine.git] / workbench / libs / reqtools / rtfuncs.c
blobc2cd73c59cf59031a72983fcbcb5152a2eed3462
1 #include "filereq.h"
2 #include "boopsi.h"
3 #include "rtfuncs.h"
4 #include "globalvars.h"
6 #include <devices/conunit.h>
8 #ifdef __AROS__
10 #include <aros/debug.h>
11 #include <aros/macros.h>
13 #else
15 #define D(x)
16 #define AROS_ASMSYMNAME(x) x
17 #define AROS_LONG2BE(x) x
18 #define AROS_WORD2BE(x) x
20 #endif
22 /****************************************************************************************/
24 struct rtWindowLock
26 struct Requester rtwl_Requester;
27 LONG rtwl_Magic;
28 struct rtWindowLock *rtwl_RequesterPtr;
29 ULONG rtwl_LockCount;
30 ULONG rtwl_ReqInstalled;
32 /* To save window parameters */
33 APTR rtwl_Pointer;
34 BYTE rtwl_PtrHeight;
35 BYTE rtwl_PtrWidth;
36 BYTE rtwl_XOffset;
37 BYTE rtwl_YOffset;
38 WORD rtwl_MinWidth;
39 WORD rtwl_MaxWidth;
40 WORD rtwl_MinHeight;
41 WORD rtwl_MaxHeight;
44 /****************************************************************************************/
46 SAVEDS ASM struct ReqToolsBase *RTFuncs_Init(REGPARAM(d0, struct ReqToolsBase *, RTBase),
47 REGPARAM(a0, BPTR, segList))
49 #ifdef __AROS__
50 /* SysBase is setup in reqtools_init.c */
51 #else
52 SysBase = *(struct ExecBase **)4L;
54 RTBase->SegList = segList;
55 #endif
57 InitSemaphore(&RTBase->ReqToolsPrefs.PrefsSemaphore);
58 RTBase->ReqToolsPrefs.PrefsSize = RTPREFS_SIZE;
60 /* Set default preferences */
61 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_FILEREQ].Size = 75;
62 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_FILEREQ].ReqPos = REQPOS_TOPLEFTSCR;
63 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_FILEREQ].LeftOffset = 25;
64 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_FILEREQ].TopOffset = 18;
65 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_FILEREQ].MinEntries = 10;
66 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_FILEREQ].MaxEntries = 50;
68 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_FONTREQ].Size = 65;
69 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_FONTREQ].ReqPos = REQPOS_TOPLEFTSCR;
70 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_FONTREQ].LeftOffset = 25;
71 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_FONTREQ].TopOffset = 18;
72 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_FONTREQ].MinEntries = 6;
73 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_FONTREQ].MaxEntries = 10;
75 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_PALETTEREQ].Size = 65;
76 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_PALETTEREQ].ReqPos = REQPOS_TOPLEFTSCR;
77 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_PALETTEREQ].LeftOffset = 25;
78 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_PALETTEREQ].TopOffset = 18;
79 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_PALETTEREQ].MinEntries = 6;
80 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_PALETTEREQ].MaxEntries = 10;
82 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_SCREENMODEREQ].Size = 65;
83 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_SCREENMODEREQ].ReqPos = REQPOS_TOPLEFTSCR;
84 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_SCREENMODEREQ].LeftOffset = 25;
85 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_SCREENMODEREQ].TopOffset = 18;
86 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_SCREENMODEREQ].MinEntries = 6;
87 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_SCREENMODEREQ].MaxEntries = 10;
89 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_VOLUMEREQ].Size = 65;
90 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_VOLUMEREQ].ReqPos = REQPOS_TOPLEFTSCR;
91 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_VOLUMEREQ].LeftOffset = 25;
92 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_VOLUMEREQ].TopOffset = 18;
93 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_VOLUMEREQ].MinEntries = 6;
94 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_VOLUMEREQ].MaxEntries = 10;
96 // RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_OTHERREQ].Size = 65;
97 // RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_OTHERREQ].ReqPos = REQPOS_TOPLEFTSCR;
98 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_OTHERREQ].LeftOffset = 25;
99 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_OTHERREQ].TopOffset = 18;
100 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_OTHERREQ].MinEntries = 6;
101 RTBase->ReqToolsPrefs.ReqDefaults[RTPREF_OTHERREQ].MaxEntries = 10;
103 return RTBase;
106 /****************************************************************************************/
108 SAVEDS ASM struct ReqToolsBase *RTFuncs_Open(REGPARAM(a6, struct ReqToolsBase *, RTBase),
109 REGPARAM(d0, ULONG, ver))
111 if (DOSBase == NULL)
113 UBYTE configbuffer[RTPREFS_SIZE];
115 DOSBase = RTBase->DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37);
116 if (DOSBase == NULL)
117 return NULL;
120 /* Read config file */
122 D(bug("reqtools.library: Inside libopen func. Reading config file\n"));
124 memset(configbuffer, 0, sizeof(configbuffer));
126 if (GetVar("ReqTools.prefs",
127 configbuffer,
128 sizeof(configbuffer),
129 GVF_BINARY_VAR | GVF_GLOBAL_ONLY | LV_VAR | GVF_DONT_NULL_TERM) == RTPREFS_SIZE)
131 UBYTE *configptr = configbuffer;
132 ULONG val;
133 WORD i;
135 D(bug("reqtools.library: Inside libopen func. Configfile loaded successfully\n"));
137 #define READ_ULONG *((ULONG *)configptr);configptr += sizeof(ULONG)
138 #define READ_UWORD *((UWORD *)configptr);configptr += sizeof(UWORD)
139 #define RTPREFS (RTBase->ReqToolsPrefs)
141 val = READ_ULONG;
142 RTPREFS.Flags = AROS_LONG2BE(val);
144 for(i = 0;i < RTPREF_NR_OF_REQ; i++)
146 val = READ_ULONG;
147 RTPREFS.ReqDefaults[i].Size = AROS_LONG2BE(val);
149 val = READ_ULONG;
150 RTPREFS.ReqDefaults[i].ReqPos = AROS_LONG2BE(val);
152 val = READ_UWORD;
153 RTPREFS.ReqDefaults[i].LeftOffset = AROS_WORD2BE(val);
155 val = READ_UWORD;
156 RTPREFS.ReqDefaults[i].TopOffset = AROS_WORD2BE(val);
158 val = READ_UWORD;
159 RTPREFS.ReqDefaults[i].MinEntries = AROS_WORD2BE(val);
161 val = READ_UWORD;
162 RTPREFS.ReqDefaults[i].MaxEntries = AROS_WORD2BE(val);
167 } /* if (DOSBase == NULL) */
169 if(IntuitionBase == NULL)
170 IntuitionBase = RTBase->IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 37);
171 if(IntuitionBase == NULL)
172 return NULL;
174 if(GfxBase == NULL)
175 GfxBase = RTBase->GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 37);
176 if(GfxBase == NULL)
177 return NULL;
179 if(UtilityBase == NULL)
180 UtilityBase = RTBase->UtilityBase = (struct UtilityBase *)OpenLibrary("utility.library", 37);
181 if(UtilityBase == NULL)
182 return NULL;
184 if(GadToolsBase == NULL)
185 GadToolsBase = RTBase->GadToolsBase = OpenLibrary("gadtools.library", 37);
186 if(GadToolsBase == NULL)
187 return NULL;
189 if(LayersBase == NULL)
190 LayersBase = OpenLibrary("layers.library", 37);
191 if(LayersBase == NULL)
192 return NULL;
194 if(LocaleBase == NULL)
195 LocaleBase = (struct LocaleBase *)OpenLibrary("locale.library", 37);
196 if(LocaleBase == NULL)
197 return NULL;
199 D(bug("reqtools.library: Inside libopen func. Libraries opened successfully.\n"));
201 if (ConsoleDevice == NULL)
203 iorequest.io_Message.mn_Length = sizeof(iorequest);
205 if (OpenDevice("console.device", CONU_LIBRARY, (struct IORequest *)&iorequest, 0))
207 return NULL;
209 ConsoleDevice = iorequest.io_Device;
211 if (ConsoleDevice == NULL)
212 return NULL;
214 D(bug("reqtools.library: Inside libopen func. Console.device opened successfully.\n"));
216 if (ButtonImgClass == NULL)
218 ButtonImgClass = MakeClass(NULL, IMAGECLASS, NULL, sizeof(struct LocalObjData), 0);
219 if (ButtonImgClass)
221 ButtonImgClass->cl_Dispatcher.h_Entry = (APTR)AROS_ASMSYMNAME(myBoopsiDispatch);
222 ButtonImgClass->cl_Dispatcher.h_SubEntry = NULL;
223 ButtonImgClass->cl_UserData = (IPTR)RTBase;
226 if (ButtonImgClass == NULL)
227 return NULL;
229 D(bug("reqtools.library: Inside libopen func. ButtonImgClass create successfully.\n"));
231 #ifndef __AROS__
232 /* I have one more opener. */
233 RTBase->LibNode.lib_Flags &= ~LIBF_DELEXP;
234 RTBase->LibNode.lib_OpenCnt++;
235 #endif
237 D(bug("reqtools.library: Inside libopen func. Returning success.\n"));
239 return RTBase;
242 /****************************************************************************************/
244 SAVEDS ASM BPTR RTFuncs_Close(REGPARAM(a6, struct ReqToolsBase *, RTBase))
246 #ifndef __AROS__
247 /* I have one fewer opener. */
248 RTBase->LibNode.lib_OpenCnt--;
250 if((RTBase->LibNode.lib_Flags & LIBF_DELEXP) != 0)
252 /* CHECKME: used expunge() from reqtools_intern.h. */
254 if(RTBase->LibNode.lib_OpenCnt == 0)
255 return RTFuncs_Expunge(RTBase);
257 RTBase->LibNode.lib_Flags &= ~LIBF_DELEXP;
259 #endif
261 return NULL;
264 /****************************************************************************************/
266 SAVEDS ASM BPTR RTFuncs_Expunge(REGPARAM(a6, struct ReqToolsBase *, RTBase))
268 BPTR ret = NULL;
270 #ifndef __AROS__
271 if(RTBase->LibNode.lib_OpenCnt != 0)
273 /* Set the delayed expunge flag and return. */
274 RTBase->LibNode.lib_Flags |= LIBF_DELEXP;
275 return NULL;
278 /* Get rid of the library. Remove it from the list. */
279 Remove(&RTBase->LibNode.lib_Node);
281 /* Get returncode here - FreeMem() will destroy the field. */
282 ret = RTBase->SegList;
283 #endif
285 D(bug("reqtools.library: Inside libexpunge func. Freeing ButtonImgClass.\n"));
287 if (ButtonImgClass) FreeClass(ButtonImgClass);
289 D(bug("reqtools.library: Inside libexpunge func. Closing console.device.\n"));
291 if (ConsoleDevice) CloseDevice((struct IORequest *)&iorequest);
293 D(bug("reqtools.library: Inside libexpunge func. Closing libraries.\n"));
295 CloseLibrary((struct Library *)DOSBase);
296 CloseLibrary((struct Library *)IntuitionBase);
297 CloseLibrary((struct Library *)UtilityBase);
298 CloseLibrary((struct Library *)GfxBase);
299 CloseLibrary((struct Library *)LocaleBase);
300 CloseLibrary(GadToolsBase);
301 CloseLibrary(LayersBase);
303 D(bug("reqtools.library: Inside libexpunge func. Freeing libbase.\n"));
305 #ifndef __AROS__
306 /* Free the memory. */
307 FreeMem((char *)RTBase-RTBase->LibNode.lib_NegSize,
308 RTBase->LibNode.lib_NegSize + RTBase->LibNode.lib_PosSize);
309 #endif
311 return ret;
314 /****************************************************************************************/
316 SAVEDS ASM int RTFuncs_Null(REGPARAM(a6, struct ReqToolsBase *, RTBase))
318 return 0;
322 /****************************************************************************************/
324 SAVEDS ASM struct ReqToolsPrefs *RTFuncs_LockPrefs(REGPARAM(a6, struct ReqToolsBase *, ReqToolsBase))
326 ObtainSemaphore(&ReqToolsBase->ReqToolsPrefs.PrefsSemaphore);
328 return &ReqToolsBase->ReqToolsPrefs;
331 /****************************************************************************************/
333 SAVEDS ASM void RTFuncs_UnlockPrefs(REGPARAM(a6, struct ReqToolsBase *, ReqToolsBase))
335 ReleaseSemaphore(&ReqToolsBase->ReqToolsPrefs.PrefsSemaphore);
338 /****************************************************************************************/
340 SAVEDS ASM ULONG RTFuncs_rtReqHandlerA(REGPARAM(a1, struct rtHandlerInfo *, handlerinfo),
341 REGPARAM(d0, ULONG, sigs),
342 REGPARAM(a0, struct TagItem *, taglist))
344 return ((ULONG (*)(REGPARAM(a1, struct rtHandlerInfo *,),
345 REGPARAM(d0, ULONG,),
346 REGPARAM(a0, struct TagItem *,)))handlerinfo->private1)(handlerinfo, sigs, taglist);
349 /****************************************************************************************/
351 SAVEDS ASM void RTFuncs_rtSetWaitPointer(REGPARAM(a0, struct Window *, window))
353 struct TagItem tags[] = { { WA_BusyPointer, TRUE },
354 { TAG_DONE, 0 } };
356 SetWindowPointerA(window, (struct TagItem *)&tags);
359 /****************************************************************************************/
361 SAVEDS ASM APTR RTFuncs_rtLockWindow(REGPARAM(a0, struct Window *, window))
363 struct rtWindowLock *winLock;
365 /* Is this window already locked? */
366 if(window->FirstRequest != NULL)
368 struct rtWindowLock *wLock = (struct rtWindowLock *)window->FirstRequest;
370 while(wLock != NULL)
372 if(wLock->rtwl_Magic == ('r' << 24 | 't' << 16 | 'L' << 8 | 'W'))
374 if(wLock->rtwl_RequesterPtr == wLock)
376 /* Window was already locked */
377 wLock->rtwl_LockCount++;
379 return wLock;
383 wLock = (struct rtWindowLock *)wLock->rtwl_Requester.OlderRequest;
386 winLock = (struct rtWindowLock *)AllocVec(sizeof(struct rtWindowLock),
387 MEMF_CLEAR);
389 /* No memory? */
390 if(winLock == NULL)
391 return NULL;
393 winLock->rtwl_Magic = 'r' << 24 | 't' << 16 | 'L' << 8 | 'W';
394 winLock->rtwl_RequesterPtr = winLock;
396 winLock->rtwl_MinHeight = window->MinHeight;
397 winLock->rtwl_MaxHeight = window->MaxHeight;
398 winLock->rtwl_MinWidth = window->MinWidth;
399 winLock->rtwl_MaxWidth = window->MaxWidth;
401 WindowLimits(window, window->Width, window->Height,
402 window->Width, window->Height);
404 InitRequester((struct Requester *)winLock);
405 winLock->rtwl_ReqInstalled = Request((struct Requester *)winLock, window);
407 winLock->rtwl_Pointer = window->Pointer;
408 winLock->rtwl_PtrHeight = window->PtrHeight;
409 winLock->rtwl_PtrWidth = window->PtrWidth;
411 rtSetWaitPointer(window);
413 return (APTR)winLock;
416 /****************************************************************************************/
418 SAVEDS ASM VOID RTFuncs_rtUnlockWindow(REGPARAM(a0, struct Window *, window),
419 REGPARAM(a1, APTR, windowlock))
422 struct rtWindowLock *wLock = (struct rtWindowLock *)windowlock;
424 if(wLock == NULL)
425 return;
427 if(wLock->rtwl_LockCount != 0)
429 wLock->rtwl_LockCount--;
431 else
433 struct TagItem tags[] = { { WA_Pointer, (IPTR)wLock->rtwl_Pointer },
434 { TAG_DONE , 0 } };
436 SetWindowPointerA(window, tags);
438 if (wLock->rtwl_ReqInstalled)
439 EndRequest((struct Requester *)wLock, window);
441 WindowLimits(window, wLock->rtwl_MinWidth, wLock->rtwl_MinHeight,
442 wLock->rtwl_MaxWidth, wLock->rtwl_MaxHeight);
444 FreeVec(wLock);
448 /****************************************************************************************/
450 SAVEDS ASM void RTFuncs_rtSpread(REGPARAM(a0, ULONG *, posarray),
451 REGPARAM(a1, ULONG *, sizearray),
452 REGPARAM(d0, ULONG, totalsize),
453 REGPARAM(d1, ULONG, min),
454 REGPARAM(d2, ULONG, max),
455 REGPARAM(d3, ULONG, num))
457 ULONG gadpos = min << 16;
458 ULONG gadgap;
459 UWORD i;
461 gadgap = ((max - min - totalsize) << 16) / (num - 1);
463 posarray[0] = min;
465 for(i = 1; i < num - 1; i++)
467 gadpos += (sizearray[i - 1] << 16) + gadgap;
468 posarray[i] = gadpos >> 16;
471 posarray[num - 1] = max - sizearray[i];
475 /****************************************************************************************/
477 SAVEDS ASM void RTFuncs_ScreenToFrontSafely(REGPARAM(a0, struct Screen *, screen))
479 #ifndef USE_FORBID
480 ULONG ilock;
481 #endif
482 struct Screen *scr;
484 /* Bugfixes: 1. Lock *before* peeking IntuitionBase->FirstScreen
485 2. Favor LockIBase() over Forbid() */
487 #ifdef USE_FORBID
488 Forbid();
489 #else
490 ilock = LockIBase(0);
491 #endif
493 scr = IntuitionBase->FirstScreen;
495 while(scr != NULL)
497 if(scr == screen)
499 #ifdef USE_FORBID
500 ScreenToFront(screen);
501 break;
502 #else
503 /* Forbid before UnlockIBase() to avoid screen from disappearing */
504 Forbid();
506 /* UnlockIBase() basically does ReleaseSemaphore() and that never
507 Wait(), and thus cannot break Forbid(). */
508 UnlockIBase(ilock);
510 /* Actually this will break the Forbid() if it need to Wait() for
511 semaphore, so this function isn't 100% bulletproof anyway... */
512 ScreenToFront(screen);
514 Permit();
516 /* Note: return not break!
518 return;
519 #endif
522 scr = scr->NextScreen;
525 #ifdef USE_FORBID
526 Permit();
527 #else
528 UnlockIBase(ilock);
529 #endif
532 /****************************************************************************************/
534 SAVEDS ASM void RTFuncs_rtSetReqPosition(REGPARAM(d0, ULONG, reqpos),
535 REGPARAM(a0, struct NewWindow *, nw),
536 REGPARAM(a1, struct Screen *, scr),
537 REGPARAM(a2, struct Window *, win))
539 #warning Taken from rtfuncs.asm where the C version was in comments. Might be out of date
541 int mx = 0, my = 0, val, leftedge, topedge;
542 ULONG scrwidth, scrheight;
543 int width, height, left, top;
545 rtGetVScreenSize (scr, &scrwidth, &scrheight);
547 leftedge = -scr->LeftEdge;
548 if (leftedge < 0) leftedge = 0;
550 topedge = -scr->TopEdge;
551 if (topedge < 0) topedge = 0;
553 left = leftedge; top = topedge;
554 width = scrwidth; height = scrheight;
556 switch (reqpos)
558 case REQPOS_DEFAULT:
559 nw->LeftEdge = 25;
560 nw->TopEdge = 18;
561 goto topleftscr;
563 case REQPOS_POINTER:
564 mx = scr->MouseX; my = scr->MouseY;
565 break;
567 case REQPOS_CENTERWIN:
568 if (win)
570 left = win->LeftEdge; top = win->TopEdge;
571 width = win->Width; height = win->Height;
574 case REQPOS_CENTERSCR:
575 mx = (width - nw->Width) / 2 + left;
576 my = (height - nw->Height) / 2 + top;
577 break;
579 case REQPOS_TOPLEFTWIN:
580 if (win)
582 left = win->LeftEdge;
583 top = win->TopEdge;
586 case REQPOS_TOPLEFTSCR:
587 topleftscr:
588 mx = left; my = top;
589 break;
591 } /* switch (reqpos) */
593 /* keep window completely visible */
594 mx += nw->LeftEdge; my += nw->TopEdge;
595 val = leftedge + scrwidth - nw->Width;
597 if (mx < leftedge) mx = leftedge;
598 else if (mx > val) mx = val;
600 val = topedge + scrheight - nw->Height;
602 if (my < topedge) my = topedge;
603 else if (my > val) my = val;
605 nw->LeftEdge = mx; nw->TopEdge = my;
609 /****************************************************************************************/
611 /* This one is from closewindowsafely.asm */
613 SAVEDS ASM void RTFuncs_CloseWindowSafely(REGPARAM(a0, struct Window *, window))
615 struct IntuiMessage *msg;
616 struct Node *succ;
618 Forbid();
620 if(window->UserPort != NULL)
622 msg = (struct IntuiMessage *)window->UserPort->mp_MsgList.lh_Head;
624 while((succ = msg->ExecMessage.mn_Node.ln_Succ))
626 if(msg->IDCMPWindow == window)
628 Remove((struct Node *)msg);
629 ReplyMsg((struct Message *)msg);
632 msg = (struct IntuiMessage *)succ;
636 window->UserPort = NULL;
638 ModifyIDCMP(window, 0);
640 Permit();
642 CloseWindow(window);
645 /****************************************************************************************/
646 /****************************************************************************************/