Documented GVF_SAVE_VAR alongside other flags, and removed a query/doubt
[AROS.git] / rom / filesys / SFS / SFSdefrag / main.c
bloba7f8dfaaff88540a393e5125466d9c66212d807a
1 /*
2 Copyright © 2003-2011, The AROS Development Team.
3 $Id$
4 */
6 #include <exec/types.h>
7 #include <exec/memory.h>
8 #include <libraries/mui.h>
9 #include <utility/tagitem.h>
10 #include <utility/hooks.h>
11 #include <graphics/rastport.h>
12 #include <intuition/screens.h>
13 #include <intuition/intuition.h>
15 #include <proto/alib.h>
16 #include <proto/exec.h>
17 #include <proto/muimaster.h>
18 #include <proto/utility.h>
19 #include <proto/intuition.h>
20 #include <proto/dos.h>
21 #include <proto/graphics.h>
23 #include <aros/debug.h>
25 #include "support.h"
26 #include "../FS/packets.h"
27 #include "../FS/query.h"
29 #define _DEBUG_H
30 #include "../FS/asmsupport.c"
32 #include <stdio.h>
33 #include <stdlib.h>
35 #include "locale.h"
37 #define APPNAME "SFSdefrag"
38 #define VERSION "sfsdefrag 0.1 (29.11.2005) ©AROS Dev Team"
40 #define MIN(x,y) ((x)<(y)?(x):(y))
41 #define MAX(x,y) ((x)>(y)?(x):(y))
43 const char version[] = "$VER: " VERSION "\n";
45 struct DefragmentStep {
46 ULONG id; // id of the step ("MOVE", "DONE" or 0)
47 ULONG length; // length in longwords (can be 0)
48 ULONG data[0]; // size of this array is determined by length.
51 ULONG *bitmap;
52 UBYTE *oldname;
54 ULONG blocks_total;
55 ULONG blocks_inone;
57 ULONG steps[200];
59 struct Library *MUIMasterBase = NULL;
60 struct UtilityBase *UtilityBase = NULL;
61 struct MsgPort *mp;
63 int openLibs()
65 if ((MUIMasterBase=OpenLibrary("muimaster.library", 0)) != NULL)
67 if ((UtilityBase=(struct UtilityBase*)OpenLibrary("utility.library", 0)) != NULL)
69 return 1;
71 CloseLibrary(MUIMasterBase);
73 return 0;
76 void closeLibs()
78 if (UtilityBase != NULL)
79 CloseLibrary((struct Library *)UtilityBase);
81 if (MUIMasterBase != NULL)
82 CloseLibrary(MUIMasterBase);
84 MUIMasterBase = NULL;
85 UtilityBase = NULL;
88 void cleanup(CONST_STRPTR message)
90 if (message != NULL)
92 ShowError(NULL, NULL, message, TRUE);
93 exit(RETURN_FAIL);
95 else
97 exit(RETURN_OK);
101 Object *MakeLabel(STRPTR str)
103 return (MUI_MakeObject(MUIO_Label, str, 0));
106 IPTR xget(Object * obj, ULONG attr)
108 IPTR x = 0;
110 get(obj, attr, &x);
111 return x;
114 struct Hook select_hook;
115 struct Hook start_hook;
116 struct Hook stop_hook;
117 struct Hook refresh_hook;
119 BOOL stop_me;
121 struct BitMap *bm;
122 struct RastPort *rp;
123 struct DrawInfo *dri;
125 ULONG pen1, pen2, pen3;
127 Object *app;
128 Object *MainWindow;
130 Object *DevList;
131 Object *StartButton, *StopButton, *RefreshButton;
132 Object *Bmp;
134 void updateBitmap(ULONG first, ULONG last)
136 ULONG cnt;
137 ULONG blck = blocks_inone;
139 for (cnt=first/blocks_inone; cnt <= (last+blocks_inone-1)/blocks_inone; cnt++)
141 ULONG x,y,pen=pen3;
142 y = (cnt / 166) * 3;
143 x = (cnt % 166) * 3;
145 if ((cnt+1) * blocks_inone > blocks_total) blck = blocks_total - cnt*blocks_inone;
147 if (bmtstz(bitmap, cnt*blocks_inone, blck))
148 pen=pen2;
150 SetAPen(rp, pen);
151 RectFill(rp, x, y, x+1, y+1);
153 DoMethod(Bmp, MUIM_Draw);
156 void getDeviceData(STRPTR device)
158 struct DosList *dl;
160 dl=LockDosList(LDF_DEVICES|LDF_READ);
162 if((dl=FindDosEntry(dl,device,LDF_DEVICES))!=0)
164 struct TagItem tags[] = {
165 { ASQ_TOTAL_BLOCKS, 0},
166 { TAG_DONE, 0}
169 mp = dl->dol_Task;
171 UnLockDosList(LDF_DEVICES|LDF_READ);
173 if(DoPkt(mp, ACTION_SFS_QUERY, (SIPTR)tags, 0, 0, 0, 0)!=DOSFALSE)
175 blocks_total=tags[0].ti_Data;
176 blocks_inone=(blocks_total + 19255)/19256;
177 bug("blocks_total=%d\nblocks_inone=%d\n",blocks_total,blocks_inone);
179 if (bitmap)
180 FreeVec(bitmap);
182 if ((bitmap = AllocVec(blocks_total / 8 + 32, MEMF_CLEAR))!=0)
184 if(DoPkt(mp, ACTION_SFS_READ_BITMAP, (SIPTR)bitmap, 0, blocks_total, 0, 0)!=DOSFALSE)
186 updateBitmap(0, blocks_total);
191 else
192 UnLockDosList(LDF_DEVICES|LDF_READ);
195 AROS_UFH3(void, start_function,
196 AROS_UFHA(struct Hook *, h, A0),
197 AROS_UFHA(Object *, object, A2),
198 AROS_UFHA(APTR, msg, A1))
200 AROS_USERFUNC_INIT
201 ULONG sigs;
202 BOOL defragmented = FALSE;
204 set(StartButton, MUIA_Disabled, TRUE);
205 set(RefreshButton, MUIA_Disabled, TRUE);
206 set(StopButton, MUIA_Disabled, FALSE);
207 set(DevList, MUIA_Disabled, TRUE);
209 stop_me = FALSE;
211 if(DoPkt(mp, ACTION_SFS_DEFRAGMENT_INIT, 0, 0, 0, 0, 0)!=DOSFALSE)
213 do {
214 ULONG clrlo=blocks_total,clrhi=0;
215 ULONG setlo=blocks_total,sethi=0;
217 if(DoPkt(mp, ACTION_SFS_DEFRAGMENT_STEP, (SIPTR)steps, 190, 0, 0, 0)!=DOSFALSE)
219 struct DefragmentStep *ds=(struct DefragmentStep *)steps;
221 while(ds->id!=0)
223 if(ds->id==AROS_BE2LONG(MAKE_ID('M','O','V','E')) && ds->length==3)
225 bmclr(bitmap, (blocks_total+31)/32, ds->data[2], ds->data[0]);
226 bmset(bitmap, (blocks_total+31)/32, ds->data[1], ds->data[0]);
228 clrlo = MIN(clrlo, ds->data[2]);
229 clrhi = MAX(clrhi, (ds->data[2]+ds->data[0]));
231 setlo = MIN(setlo, ds->data[1]);
232 sethi = MAX(sethi, (ds->data[1]+ds->data[0]));
235 else if(ds->id==AROS_BE2LONG(MAKE_ID('D','O','N','E')))
237 defragmented=TRUE;
238 break;
240 ds=(struct DefragmentStep *)((ULONG *)ds + 2 + ds->length);
243 updateBitmap(clrlo, clrhi);
244 updateBitmap(setlo, sethi);
245 DoMethod(app, MUIM_Application_NewInput, (IPTR)&sigs);
246 }while(!stop_me && !defragmented);
249 set(StartButton, MUIA_Disabled, FALSE);
250 set(StopButton, MUIA_Disabled, TRUE);
251 set(RefreshButton, MUIA_Disabled, FALSE);
252 set(DevList, MUIA_Disabled, FALSE);
254 AROS_USERFUNC_EXIT
257 AROS_UFH3(void, stop_function,
258 AROS_UFHA(struct Hook *, h, A0),
259 AROS_UFHA(Object *, object, A2),
260 AROS_UFHA(APTR, msg, A1))
262 AROS_USERFUNC_INIT
264 stop_me=TRUE;
266 AROS_USERFUNC_EXIT
269 AROS_UFH3(void, refresh_function,
270 AROS_UFHA(struct Hook *, h, A0),
271 AROS_UFHA(Object *, object, A2),
272 AROS_UFHA(APTR, msg, A1))
274 AROS_USERFUNC_INIT
276 ULONG active = xget(DevList, MUIA_List_Active);
278 if (active != MUIV_List_Active_Off)
280 if(DoPkt(mp, ACTION_SFS_READ_BITMAP, (SIPTR)bitmap, 0, blocks_total, 0, 0)!=DOSFALSE)
282 updateBitmap(0, blocks_total);
286 AROS_USERFUNC_EXIT
289 AROS_UFH3(void, select_function,
290 AROS_UFHA(struct Hook *, h, A0),
291 AROS_UFHA(Object *, object, A2),
292 AROS_UFHA(APTR, msg, A1))
294 AROS_USERFUNC_INIT
296 ULONG active = xget(object, MUIA_List_Active);
298 if (active != MUIV_List_Active_Off)
300 UBYTE *name;
301 DoMethod(object, MUIM_List_GetEntry, active, (IPTR)&name);
302 if (name != oldname)
304 oldname = name;
305 SetAPen(rp, pen1);
306 RectFill(rp, 0, 0, 497, 347);
307 getDeviceData(name);
308 set(StartButton, MUIA_Disabled, FALSE);
309 set(RefreshButton, MUIA_Disabled, FALSE);
312 else
314 set(StartButton, MUIA_Disabled, TRUE);
315 set(RefreshButton, MUIA_Disabled, TRUE);
316 oldname = NULL;
319 AROS_USERFUNC_EXIT
322 BOOL GUIinit()
324 BOOL retval = FALSE;
326 app = ApplicationObject,
327 MUIA_Application_Title, (IPTR)APPNAME,
328 MUIA_Application_Version, (IPTR)VERSION,
329 MUIA_Application_Copyright, (IPTR)"© 2006, The AROS Development Team",
330 MUIA_Application_Author, (IPTR)"Michal Schulz",
331 MUIA_Application_Base, (IPTR)APPNAME,
332 MUIA_Application_Description, _(MSG_DESCRIPTION),
334 SubWindow, MainWindow = WindowObject,
335 MUIA_Window_Title, _(MSG_DESCRIPTION),
336 // MUIA_Window_Height, MUIV_Window_Height_Visible(50),
337 // MUIA_Window_Width, MUIV_Window_Width_Visible(60),
338 WindowContents, HGroup,
339 MUIA_Group_SameWidth, FALSE,
340 Child, VGroup, GroupFrameT(_(MSG_BITMAP)),
341 Child, Bmp = BitmapObject,
342 MUIA_FixWidth, 498,
343 MUIA_FixHeight, 348,
344 MUIA_Bitmap_Width, 498,
345 MUIA_Bitmap_Height, 348,
346 End, // ImageObject
347 End, // VGroup
348 Child, VGroup,
349 Child, VGroup, GroupFrameT(_(MSG_SELECTION)),
350 Child, DevList = ListObject,
351 InputListFrame,
352 // MUIA_List_AdjustWidth, TRUE,
353 End, // ListObject
354 End, // VGroup
355 // Child, HVSpace,
356 Child, RefreshButton = MUI_MakeObject(MUIO_Button, _(MSG_REFRESH)),
357 Child, StartButton = MUI_MakeObject(MUIO_Button, _(MSG_START)),
358 Child, StopButton = MUI_MakeObject(MUIO_Button, _(MSG_CANCEL)),
359 End, // HGroup
360 End, // WindowContents
361 End, // MainWindow
362 End; // ApplicationObject
364 if (app)
366 /* Quit application if the windowclosegadget or the esc key is pressed. */
367 struct DosList *dl,*dll;
369 dl = LockDosList(LDF_READ | LDF_DEVICES);
370 dll = dl;
371 while((dll = NextDosEntry(dll, LDF_DEVICES)))
373 struct TagItem tags[] = {
374 { ASQ_VERSION, 0},
375 { TAG_DONE, 0}
378 mp = dll->dol_Task;
379 if(DoPkt(mp, ACTION_SFS_QUERY, (SIPTR)tags, 0, 0, 0, 0)!=DOSFALSE)
381 const char *name;
382 name = AROS_BSTR_ADDR(dll->dol_Name);
383 if(tags[0].ti_Data >= (1<<16) + 83)
384 DoMethod(DevList, MUIM_List_InsertSingle, name, MUIV_List_Insert_Bottom);
387 UnLockDosList(LDF_READ | LDF_DEVICES);
389 DoMethod(StartButton, MUIM_Notify, MUIA_Pressed, FALSE,
390 (IPTR)app, 3, MUIM_CallHook, (IPTR)&start_hook);
392 DoMethod(StopButton, MUIM_Notify, MUIA_Pressed, FALSE,
393 (IPTR)app, 3, MUIM_CallHook, (IPTR)&stop_hook);
395 DoMethod(RefreshButton, MUIM_Notify, MUIA_Pressed, FALSE,
396 (IPTR)app, 3, MUIM_CallHook, (IPTR)&refresh_hook);
398 DoMethod(DevList, MUIM_Notify, MUIA_List_Active, MUIV_EveryTime,
399 (IPTR)DevList, 2, MUIM_CallHook, (IPTR)&select_hook);
401 DoMethod(MainWindow, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
402 (IPTR)app, 2,
403 MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
405 set(StartButton, MUIA_Disabled, TRUE);
406 set(StopButton, MUIA_Disabled, TRUE);
407 set(RefreshButton, MUIA_Disabled, TRUE);
408 retval=TRUE;
411 return retval;
414 void loop(void)
416 ULONG sigs = 0;
418 while((LONG) DoMethod(app, MUIM_Application_NewInput, (IPTR)&sigs) != MUIV_Application_ReturnID_Quit)
420 if (sigs)
422 sigs = Wait(sigs);
425 } /* loop(void)*/
427 int main(int argc, char *argv[])
429 select_hook.h_Entry = (APTR)select_function;
430 start_hook.h_Entry = (APTR)start_function;
431 stop_hook.h_Entry = (APTR)stop_function;
432 refresh_hook.h_Entry = (APTR)refresh_function;
434 Locale_Initialize();
436 if(openLibs())
438 if(GUIinit())
440 set(MainWindow, MUIA_Window_Open, TRUE);
442 struct Window *window = (struct Window *)xget(MainWindow, MUIA_Window_Window);
444 bm = AllocBitMap(500, 350,
445 GetBitMapAttr(window->RPort->BitMap, BMA_DEPTH), BMF_CLEAR,
446 window->RPort->BitMap);
447 rp = CreateRastPort();
448 rp->BitMap = bm;
449 dri = GetScreenDrawInfo(window->WScreen);
450 pen1 = dri->dri_Pens[BACKGROUNDPEN];
451 pen2 = dri->dri_Pens[TEXTPEN];
452 pen3 = dri->dri_Pens[SHINEPEN];
453 FreeScreenDrawInfo(window->WScreen, dri);
455 SetAPen(rp, pen1);
456 RectFill(rp, 0, 0, 499, 349);
458 set(Bmp, MUIA_Bitmap_Bitmap, bm);
459 DoMethod(Bmp, MUIM_Draw);
461 if(xget(MainWindow, MUIA_Window_Open))
463 loop();
466 FreeRastPort(rp);
467 FreeBitMap(bm);
469 set(MainWindow, MUIA_Window_Open, FALSE);
470 DisposeObject(app);
472 // deinit_gui();
474 closeLibs();
476 cleanup(NULL);
478 Locale_Deinitialize();
480 return 0;
481 } /* main(int argc, char *argv[]) */