Files

25 lines
3.0 KiB
C

#include <exec/types.h>
#include <intuition/intuition.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <stdio.h>
#include <string.h>
struct IntuitionBase*IntuitionBase;
struct App {char*title,*path;};
static struct App apps[]={
{"Amber Chrono","SYS:Utilities/AmberChrono/Chrono"},{"Boing 3D","SYS:Utilities/Boing3D/Boing3D"},{"Matrix |","SYS:Utilities/Matrix/Matrix"},{"RetroIRC","SYS:Utilities/RetroIRC/RetroIRC"},{"RetroTerm","SYS:Utilities/RetroTerm/RetroTerm"},{"Boing!","SYS:Utilities/Boing/Boing"},{"System Monitor","SYS:Utilities/AmberMonitor/Monitor"},{"Tech News","SYS:Utilities/NewsTicker/News"},{"MOD Player","SYS:Utilities/AmberMod/ModPlayer"}};
#define APPS (sizeof(apps)/sizeof(apps[0]))
struct Entry {int app,x,y,w,h;};static struct Entry entries[APPS];static struct Window*found[APPS];
static int identify(struct Window*w){int i;if(!w->Title)return -1;for(i=0;i<APPS;i++)if(!strncmp((char*)w->Title,apps[i].title,strlen(apps[i].title)))return i;return -1;}
int main(int argc,char**argv){struct Screen*sc;struct Window*w;ULONG lock;FILE*f;int n=0,i,j,id,pass,restoring=argc>1&&!strcmp(argv[1],"RESTORE");IntuitionBase=(struct IntuitionBase*)OpenLibrary("intuition.library",39);if(!IntuitionBase)return 20;sc=LockPubScreen(0);if(!sc){CloseLibrary((struct Library*)IntuitionBase);return 20;}
if(!restoring){lock=LockIBase(0);for(w=sc->FirstWindow;w&&n<APPS;w=w->NextWindow){id=identify(w);if(id<0)continue;for(i=0;i<n;i++)if(entries[i].app==id)break;if(i<n)continue;entries[n].app=id;entries[n].x=w->LeftEdge;entries[n].y=w->TopEdge;entries[n].w=w->Width;entries[n++].h=w->Height;}UnlockIBase(lock);
if(!n)goto end;f=fopen("SYS:Prefs/AmigaSession.layout","w");if(!f)goto end;for(i=0;i<n;i++)fprintf(f,"%d %d %d %d %d\n",entries[i].app,entries[i].x,entries[i].y,entries[i].w,entries[i].h);fclose(f);
f=fopen("S:Session-Apps","w");if(f){fprintf(f,"; Generated by AmigaSession SAVE. Restore the captured applications.\nStack 262144\n");for(i=n-1;i>=0;i--)fprintf(f,"Run >NIL: %s\n",apps[entries[i].app].path);fprintf(f,"Run >NIL: C:AmigaSession RESTORE\n");fclose(f);}
}else{f=fopen("SYS:Prefs/AmigaSession.layout","r");if(!f)goto end;while(n<APPS&&fscanf(f,"%d %d %d %d %d",&entries[n].app,&entries[n].x,&entries[n].y,&entries[n].w,&entries[n].h)==5){if(entries[n].app>=0&&entries[n].app<APPS)n++;}fclose(f);
for(pass=0;pass<300;pass++){for(i=0;i<n;i++){if(found[i])continue;lock=LockIBase(0);for(w=sc->FirstWindow;w;w=w->NextWindow)if(identify(w)==entries[i].app)break;UnlockIBase(lock);if(!w)continue;ChangeWindowBox(w,entries[i].x,entries[i].y,entries[i].w,entries[i].h);found[i]=w;}for(i=0;i<n;i++)if(!found[i])break;if(i==n)break;Delay(5);}
/* Re-find each window before the final pass: apps can reopen while starting. */
Delay(25);for(i=n-1;i>=0;i--){lock=LockIBase(0);for(w=sc->FirstWindow;w;w=w->NextWindow)if(identify(w)==entries[i].app)break;UnlockIBase(lock);if(w){ChangeWindowBox(w,entries[i].x,entries[i].y,entries[i].w,entries[i].h);WindowToFront(w);}}
}
end:UnlockPubScreen(0,sc);CloseLibrary((struct Library*)IntuitionBase);return 0;}