Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / devs / diskimage / zune_gui / main.c
blobdea7d7ccdedf00b9bab1d100aecd714cb42ef4f4
1 /* Copyright 2007-2012 Fredrik Wikstrom. All rights reserved.
2 **
3 ** Redistribution and use in source and binary forms, with or without
4 ** modification, are permitted provided that the following conditions
5 ** are met:
6 **
7 ** 1. Redistributions of source code must retain the above copyright
8 ** notice, this list of conditions and the following disclaimer.
9 **
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
14 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 ** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 ** POSSIBILITY OF SUCH DAMAGE.
27 #include "diskimagegui.h"
28 #include <proto/exec.h>
29 #include <proto/dos.h>
30 #include <proto/intuition.h>
31 #include <proto/muimaster.h>
32 #include <proto/icon.h>
33 #include <proto/commodities.h>
34 #include <proto/diskimage.h>
35 #include <clib/alib_protos.h>
36 #include <string.h>
37 #include "support.h"
38 #include <SDI_hook.h>
39 #include "rev/DiskImageGUI_rev.h"
41 #ifdef __AROS__
42 #define DEBUG 1
43 #include <aros/debug.h>
44 #else
45 #define D(x)
46 #endif
48 CONST TEXT USED verstag[] = VERSTAG;
50 struct LocaleInfo LocaleInfo;
51 BYTE DiskChangeSignal = -1;
52 BYTE ReloadPluginsSignal = -1;
53 struct DiskObject *Icon;
54 struct FileRequester *FileReq;
55 struct MUI_CustomClass *DriveListClass;
56 struct MUI_CustomClass *PluginListClass;
58 HOOKPROTO(BrokerFunc, IPTR, Object *app, CxMsg *msg);
59 MakeHook(BrokerHook, BrokerFunc);
60 HOOKPROTO(MenuFunc, IPTR, Object *app, IPTR *params);
61 MakeHook(MenuHook, MenuFunc);
62 HOOKPROTO(SignalFunc, IPTR, APTR unused, IPTR *params);
63 MakeHook(SignalHook, SignalFunc);
65 int main (void) {
66 IPTR DiskChangeParams[2];
67 struct Hook *DiskChangeHook = NULL;
68 IPTR ReloadPluginsParams[2];
69 struct Hook *ReloadPluginsHook = NULL;
70 ULONG sigs = 0;
72 InitLocaleInfo((struct Library *)SysBase, &LocaleInfo, "System/System/"PROGNAME".catalog");
74 DiskChangeSignal = AllocSignal(-1);
75 if (DiskChangeSignal == -1) {
76 goto error;
79 ReloadPluginsSignal = AllocSignal(-1);
80 if (ReloadPluginsSignal == -1) {
81 goto error;
84 Icon = GetDiskObjectNew("PROGDIR:"PROGNAME);
85 if (!Icon) {
86 goto error;
89 D(bug("Got Icon\n"));
91 FileReq = MUI_AllocAslRequestTags(ASL_FileRequest,
92 ASLFR_SleepWindow, TRUE,
93 ASLFR_InitialDrawer, TTString(Icon, "FILEDIR", ""),
94 ASLFR_DoSaveMode, FALSE,
95 ASLFR_DoPatterns, TRUE,
96 ASLFR_InitialPattern, TTString(Icon, "PATTERN", "#?"),
97 TAG_END);
98 if (!FileReq) {
99 goto error;
102 DriveListClass = DriveList_CreateClass();
103 if (!DriveListClass) {
104 goto error;
107 PluginListClass = PluginList_CreateClass();
108 if (!PluginListClass) {
109 goto error;
112 D(bug("Got Classes\n"));
114 if (!OpenDiskImageDevice(~0)) {
115 goto error;
118 D(bug("diskimage.device opened\n"));
120 if (!CreateGUI()) {
121 goto error;
124 D(bug("GUI Created\n"));
126 DiskChangeParams[0] = (IPTR)FindTask(NULL);
127 DiskChangeParams[1] = (1UL << DiskChangeSignal);
128 DiskChangeHook = CreateHook((HOOKFUNC)SignalFunc, DiskChangeParams);
129 if (!DiskChangeHook) {
130 goto error;
132 AddDiskChangeHook(DiskChangeHook, TRUE);
134 ReloadPluginsParams[0] = (IPTR)FindTask(NULL);
135 ReloadPluginsParams[1] = (1UL << ReloadPluginsSignal);
136 ReloadPluginsHook = CreateHook((HOOKFUNC)SignalFunc, ReloadPluginsParams);
137 if (!ReloadPluginsHook) {
138 goto error;
140 AddReloadPluginsHook(ReloadPluginsHook, TRUE);
142 ScanUnits();
143 ScanPlugins();
145 if (TTBoolean(Icon, "CX_POPUP")) {
146 set(Gui.wnd[WID_MAIN], MUIA_Window_Open, TRUE);
147 if (!XGET(Gui.wnd[WID_MAIN], MUIA_Window_Open)) {
148 goto error;
150 DoMethod(Gui.gad[GID_DRIVELIST], MUIM_List_Redraw, MUIV_List_Redraw_All);
153 while ((LONG)DoMethod(Gui.app, MUIM_Application_NewInput, (IPTR)&sigs) !=
154 MUIV_Application_ReturnID_Quit)
156 if (sigs) {
157 sigs |= (1UL << DiskChangeSignal);
158 sigs |= (1UL << ReloadPluginsSignal);
159 sigs = Wait(sigs|SIGBREAKF_CTRL_C);
161 if (sigs & SIGBREAKF_CTRL_C) {
162 break;
165 if (sigs & (1UL << DiskChangeSignal)) {
166 ScanUnits();
169 if (sigs & (1UL << ReloadPluginsSignal)) {
170 ScanPlugins();
175 D(bug("DiskImageGUI ending...\n"));
177 error:
178 D(bug("In error/cleanup section\n"));
179 CleanupGUI();
180 PluginList_FreeClass(PluginListClass);
181 DriveList_FreeClass(DriveListClass);
182 MUI_FreeAslRequest(FileReq);
183 if (ReloadPluginsHook) {
184 AddReloadPluginsHook(ReloadPluginsHook, FALSE);
185 FreeHook(ReloadPluginsHook);
187 FreeSignal(ReloadPluginsSignal);
188 if (DiskChangeHook) {
189 AddDiskChangeHook(DiskChangeHook, FALSE);
190 FreeHook(DiskChangeHook);
192 CloseDiskImageDevice();
193 FreeDiskObject(Icon);
194 FreeSignal(DiskChangeSignal);
195 FreeLocaleInfo((struct Library *)SysBase, &LocaleInfo);
197 return 0;
200 void ScanUnits (void) {
201 Object *listview = Gui.gad[GID_DRIVELIST];
202 const ULONG dosflags = LDF_DEVICES|LDF_READ;
203 struct DosList *dl;
204 struct FileSysStartupMsg *fssm;
205 TEXT devicename[20];
206 int i, num_entries;
207 struct DriveEntry **entries, *e;
208 ULONG selected_unit = ~0;
209 struct DriveEntry *selected_entry = NULL;
211 DoMethod(listview, MUIM_List_GetEntry, MUIV_List_GetEntry_Active, &e);
212 if (e) selected_unit = e->unit_num;
214 set(listview, MUIA_List_Quiet, TRUE);
216 DoMethod(listview, MUIM_List_Clear);
218 num_entries = 0;
219 dl = LockDosList(dosflags);
220 while ((dl = NextDosEntry(dl, dosflags))) {
221 if (CheckBPTR(dl->dol_Name) && (fssm = CheckBPTR(dl->dol_misc.dol_handler.dol_Startup))
222 && CheckBPTR(fssm->fssm_Device))
224 const int ln = strlen("diskimage.device");
225 if (AROS_BSTR_strlen(fssm->fssm_Device) == ln &&
226 !memcmp(AROS_BSTR_ADDR(fssm->fssm_Device), "diskimage.device", ln))
228 num_entries++;
232 UnLockDosList(dosflags);
234 entries = AllocVecPooled(Gui.pool, sizeof(APTR) * num_entries);
235 if (!entries) {
236 set(listview, MUIA_List_Quiet, FALSE);
237 return;
239 for (i = 0; i < num_entries; i++) {
240 entries[i] = AllocVecPooled(Gui.pool, sizeof(*e));
241 if (!entries[i]) {
242 num_entries = i;
243 for (i = 0; i < num_entries; i++) {
244 FreeVecPooled(Gui.pool, entries[i]);
246 FreeVecPooled(Gui.pool, entries);
247 set(listview, MUIA_List_Quiet, FALSE);
248 return;
252 i = 0;
253 dl = LockDosList(dosflags);
254 while ((dl = NextDosEntry(dl, dosflags))) {
255 if (CheckBPTR(dl->dol_Name) && (fssm = CheckBPTR(dl->dol_misc.dol_handler.dol_Startup))
256 && CheckBPTR(fssm->fssm_Device))
258 const int ln = strlen("diskimage.device");
259 if (AROS_BSTR_strlen(fssm->fssm_Device) == ln &&
260 !memcmp(AROS_BSTR_ADDR(fssm->fssm_Device), "diskimage.device", ln))
262 if (i < num_entries) {
263 CopyStringBSTRToC(dl->dol_Name, devicename, sizeof(devicename));
264 e = entries[i];
265 e->unit_num = fssm->fssm_Unit;
266 e->device_type = DG_DIRECT_ACCESS;
267 e->writeprotect = FALSE;
268 e->unit = ASPrintfPooled(Gui.pool, "%ld", e->unit_num);
269 e->drive = ASPrintfPooled(Gui.pool, "%s", devicename);
270 e->diskimage = NULL;
271 if (selected_unit == e->unit_num) {
272 selected_entry = e;
274 i++;
279 UnLockDosList(dosflags);
281 DoMethod(listview, MUIM_List_Insert, entries, i, MUIV_List_Insert_Sorted);
282 set(listview, MUIA_List_Quiet, FALSE);
283 if (selected_entry) {
284 set(listview, MUIA_List_Active, selected_entry->list_pos);
287 for (; i < num_entries; i++) {
288 FreeVecPooled(Gui.pool, entries[i]);
290 FreeVecPooled(Gui.pool, entries);
293 HOOKPROTO(ListviewPluginFunc, IPTR, struct DiskImagePlugin *plugin, APTR unused) {
294 struct PluginEntry *e;
295 e = AllocVecPooled(Gui.pool, sizeof(*e));
296 if (e) {
297 e->pri_num = plugin->Node.ln_Pri;
298 e->is_builtin = (plugin->Flags & PLUGIN_FLAG_BUILTIN) ? TRUE : FALSE;
299 e->has_write = plugin->plugin_Write ? TRUE : FALSE;
300 e->priority = NULL;
301 e->name = plugin->Node.ln_Name;
302 DoMethod(Gui.gad[GID_PLUGINLIST], MUIM_List_InsertSingle, e, MUIV_List_Insert_Bottom);
304 return 0;
306 MakeStaticHook(ListviewPluginHook, ListviewPluginFunc);
308 void ScanPlugins (void) {
309 Object *listview = Gui.gad[GID_PLUGINLIST];
311 set(listview, MUIA_List_Quiet, TRUE);
313 DoMethod(listview, MUIM_List_Clear);
315 DoHookPlugins(&ListviewPluginHook);
317 set(listview, MUIA_List_Quiet, FALSE);
320 void ChangeTempDir (void) {
321 STRPTR tmpdir;
322 struct FileRequester *freq;
323 tmpdir = GetEnvVar(TEMPDIR_VAR);
324 freq = MUI_AllocAslRequestTags(ASL_FileRequest,
325 ASLFR_InitialDrawer, tmpdir ? tmpdir : (STRPTR)"T:",
326 ASLFR_DoSaveMode, FALSE,
327 ASLFR_DrawersOnly, TRUE,
328 TAG_END);
329 if (freq) {
330 SetWindowBusy(~0, TRUE);
331 if (MUI_AslRequest(freq, NULL)) {
332 SetEnvVar(TEMPDIR_VAR, freq->fr_Drawer, FALSE);
334 SetWindowBusy(~0, FALSE);
335 MUI_FreeAslRequest(freq);
337 FreeVec(tmpdir);
340 void SaveSettings (void) {
341 STRPTR tmpdir;
342 tmpdir = GetEnvVar(TEMPDIR_VAR);
343 SetEnvVar(TEMPDIR_VAR, tmpdir, TRUE);
344 FreeVec(tmpdir);
347 HOOKPROTO(BrokerFunc, IPTR, Object *app, CxMsg *msg) {
348 Object **wnd = Gui.wnd;
349 switch (CxMsgType(msg)) {
350 case CXM_IEVENT:
351 switch (CxMsgID(msg)) {
352 case EVT_POPKEY:
353 set(wnd[WID_MAIN], MUIA_Window_Open, TRUE);
354 set(app, MUIA_Application_Iconified, FALSE);
355 DoMethod(Gui.gad[GID_DRIVELIST], MUIM_List_Redraw, MUIV_List_Redraw_All);
356 break;
358 break;
359 case CXM_COMMAND:
360 switch (CxMsgID(msg)) {
361 case CXCMD_DISABLE:
362 ActivateCxObj((CxObj *)XGET(app, MUIA_Application_Broker), FALSE);
363 break;
364 case CXCMD_ENABLE:
365 ActivateCxObj((CxObj *)XGET(app, MUIA_Application_Broker), TRUE);
366 break;
367 case CXCMD_UNIQUE:
368 case CXCMD_APPEAR:
369 set(wnd[WID_MAIN], MUIA_Window_Open, TRUE);
370 set(app, MUIA_Application_Iconified, FALSE);
371 DoMethod(Gui.gad[GID_DRIVELIST], MUIM_List_Redraw, MUIV_List_Redraw_All);
372 break;
373 case CXCMD_DISAPPEAR:
374 set(wnd[WID_MAIN], MUIA_Window_Open, FALSE);
375 break;
376 case CXCMD_KILL:
377 DoMethod(app, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
378 break;
380 break;
382 return 0;
385 HOOKPROTO(MenuFunc, IPTR, Object *app, IPTR *params) {
386 ULONG menu_id = (ULONG)params[0];
387 Object **wnd = Gui.wnd;
388 switch (menu_id) {
389 case MID_ABOUT:
390 set(wnd[WID_ABOUT], MUIA_Window_Open, TRUE);
391 break;
392 case MID_HIDE:
393 set(wnd[WID_MAIN], MUIA_Window_Open, FALSE);
394 break;
395 case MID_ICONIFY:
396 set(app, MUIA_Application_Iconified, TRUE);
397 break;
398 case MID_SNAPSHOT:
399 DoMethod(wnd[WID_MAIN], MUIM_Window_Snapshot, TRUE);
400 break;
401 case MID_QUIT:
402 DoMethod(app, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
403 break;
404 case MID_CHANGETEMPDIR:
405 ChangeTempDir();
406 break;
407 case MID_PLUGINS:
408 set(wnd[WID_PLUGINS], MUIA_Window_Open, TRUE);
409 DoMethod(Gui.gad[GID_PLUGINLIST], MUIM_List_Redraw, MUIV_List_Redraw_All);
410 break;
411 case MID_SAVESETTINGS:
412 SaveSettings();
413 break;
415 return 0;
418 HOOKPROTO(SignalFunc, IPTR, APTR unused, IPTR *params) {
419 struct Task *task = (struct Task *)params[0];
420 ULONG sigmask = (ULONG)params[1];
421 Signal(task, sigmask);
422 return 0;