ACTION_SET_FILE_SIZE: also return the right size if it doesn't need to change.
[AROS.git] / rom / exec / useralert.c
blobb7143a3ff15e803a29330f6de062afcd83629dfc
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Display an alert in user mode.
6 Lang: english
7 */
9 #include <aros/debug.h>
10 #include <exec/alerts.h>
11 #include <exec/rawfmt.h>
12 #include <intuition/intuition.h>
13 #include <proto/exec.h>
14 #include <proto/intuition.h>
15 #include <proto/kernel.h>
17 #include "etask.h"
18 #include "exec_intern.h"
19 #include "exec_util.h"
21 static LONG SafeEasyRequest(struct EasyStruct *es, BOOL full, struct IntuitionBase *IntuitionBase)
23 LONG result;
24 APTR req = BuildEasyRequestArgs(NULL, es, 0, NULL);
26 if (!req)
28 /* Return -1 if requester creation failed. This makes us to fallback to safe-mode alert. */
29 return -1;
34 result = SysReqHandler(req, NULL, TRUE);
36 if (full)
38 switch (result)
40 case 1:
41 NewRawDoFmt("*** Logged alert:\n%s\n", RAWFMTFUNC_SERIAL, NULL, es->es_TextFormat);
42 result = -2;
43 break;
46 } while (result == -2);
48 FreeSysRequest(req);
49 return result;
52 static const char startstring[] = "Program failed\n";
53 static const char endstring[] = "\nWait for disk activity to finish.";
54 static const char deadend_buttons[] = "More...|Suspend|Reboot|Power off";
55 static const char recoverable_buttons[] = "More...|Continue";
56 static const char full_deadend_buttons[] = "Log|Suspend|Reboot|Power off";
57 static const char full_recoverable_buttons[] = "Log|Continue";
59 LONG Alert_AskSuspend(struct Task *task, ULONG alertNum, char * buffer, struct ExecBase *SysBase)
61 LONG choice = -1;
62 struct IntuitionBase *IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 36);
64 if (IntuitionBase)
66 if (buffer)
68 struct IntETask *iet = GetIntETask(task);
69 char *buf, *end;
70 struct EasyStruct es = {
71 sizeof (struct EasyStruct),
73 NULL,
74 buffer,
75 NULL,
78 buf = Alert_AddString(buffer, startstring);
79 buf = FormatAlert(buf, alertNum, task, iet ? iet->iet_AlertLocation : NULL, iet ? iet->iet_AlertType : AT_NONE, SysBase);
80 end = buf;
81 buf = Alert_AddString(buf, endstring);
82 *buf = 0;
84 es.es_Title = Alert_GetTitle(alertNum);
86 /* Determine set of buttons */
87 es.es_GadgetFormat = (alertNum & AT_DeadEnd) ? deadend_buttons : recoverable_buttons;
89 D(bug("[UserAlert] Body text:\n%s\n", buffer));
90 choice = SafeEasyRequest(&es, FALSE, IntuitionBase);
92 if (choice == 1)
94 /* 'More' has been pressed. Append full alert data */
95 FormatAlertExtra(end, iet->iet_AlertStack, iet ? iet->iet_AlertType : AT_NONE, iet ? &iet->iet_AlertData : NULL, SysBase);
97 /* Re-post the alert, without 'More...' this time */
98 es.es_GadgetFormat = (alertNum & AT_DeadEnd) ? full_deadend_buttons : full_recoverable_buttons;
100 choice = SafeEasyRequest(&es, TRUE, IntuitionBase);
104 CloseLibrary(&IntuitionBase->LibNode);
106 return choice;
109 static LONG AskSuspend(struct Task *task, ULONG alertNum, struct ExecBase *SysBase)
111 char * buffer = AllocMem(ALERT_BUFFER_SIZE, MEMF_ANY);
113 LONG choice = Alert_AskSuspend(task, alertNum, buffer, SysBase);
115 FreeMem(buffer, ALERT_BUFFER_SIZE);
117 return choice;
122 * This function posts alerts in user-mode via Intuition requester.
123 * Returns initial alert code if something fails and 0 if it was a recoverable
124 * alert and everything went ok.
125 * Note that in case of some crashes (e.g. corrupt memory list) this function
126 * may crash itself, and this has to be handled on a lower level. This is
127 * why we do this trick with iet_AlertCode
129 ULONG Exec_UserAlert(ULONG alertNum, struct ExecBase *SysBase)
131 struct Task *task = SysBase->ThisTask;
132 struct IntETask *iet;
133 LONG res;
135 /* Protect ourselves agains really hard crashes where SysBase->ThisTask is NULL.
136 Obviously we won't go far away in such a case */
137 if (!task)
138 return alertNum;
140 /* Get internal task structure */
141 if ((iet = GetIntETask(task)))
144 * If we already have alert number for this task, we are in double-crash during displaying
145 * intuition requester. Well, take the initial alert code (because it's more helpful to the programmer)
146 * and proceed with arch-specific Alert().
147 * Since this is a double-crash, we may append AT_DeadEnd flag if our situation has become unrecoverable.
149 D(bug("[UserAlert] Task alert state: 0x%02X\n", iet->iet_AlertFlags));
150 if (iet->iet_AlertFlags & AF_Alert)
153 * Some more logic here. Nested AN_SysScrnType should not make original alert deadend.
154 * It just means we were unable to display it using Intuition requested because there
155 * are no display drivers at all.
157 if (alertNum == AN_SysScrnType)
158 return iet->iet_AlertCode;
159 else
160 return iet->iet_AlertCode | (alertNum & AT_DeadEnd);
164 * Otherwise we can try to put up Intuition requester first. Store alert code in order in ETask
165 * in order to indicate crash condition
167 iet->iet_AlertFlags |= AF_Alert;
168 iet->iet_AlertCode = alertNum;
172 * AN_SysScrnType is somewhat special. We remember it in the ETask (just in case),
173 * but propagate it to supervisor mode immediately. We do it because this error
174 * means we don't have any display modes, so we won't be able to bring up the requester.
176 if (alertNum == AN_SysScrnType)
177 return alertNum;
179 /* Issue a requester */
180 res = AskSuspend(task, alertNum, SysBase);
181 D(bug("[UserAlert] Requester result: %d\n", res));
183 /* If AskSuspend() failed, fail back to safe-mode alert */
184 if (res == -1)
185 return alertNum;
187 /* Halt if we need to */
188 if (alertNum & AT_DeadEnd)
190 switch (res)
192 case 0:
193 ShutdownA(SD_ACTION_POWEROFF);
194 break;
196 case 3:
197 ColdReboot();
198 /* In case if ColdReboot() doesn't work */
199 ShutdownA(SD_ACTION_COLDREBOOT);
200 break;
203 /* Well, stop if the user wants so (or if the reboot didn't work at all) */
204 Wait(0);
206 return 0;