fix typo
[AROS.git] / arch / all-pc / acpitool / main.c
blobfe4772dfd823e55e79b406d93c4836c9dd2d29ab
1 /*
2 Copyright (C) 2003-2011, The AROS Development Team.
3 $Id$
4 */
6 #include <proto/acpica.h>
7 #include <proto/alib.h>
8 #include <proto/dos.h>
9 #include <proto/exec.h>
10 #include <proto/muimaster.h>
11 #include <proto/oop.h>
12 #include <proto/utility.h>
13 #include <proto/intuition.h>
15 #include <exec/memory.h>
16 #include <libraries/asl.h>
17 #include <libraries/mui.h>
18 #include <utility/tagitem.h>
19 #include <utility/hooks.h>
21 #include <aros/debug.h>
23 #include <ctype.h>
24 #include <stdio.h>
25 #include <stdlib.h>
27 #include "locale.h"
28 #include "parsers.h"
30 #define APPNAME "ACPITool"
31 #define VERSION "ACPITool 1.0"
33 const char version[] = "$VER: " VERSION " (" ADATE ")\n";
35 struct Library *ACPICABase;
37 static void ShowError(Object *application, Object *window, CONST_STRPTR message, BOOL useIOError)
39 TEXT buffer[128];
40 STRPTR newline = "\n",
41 period = ".",
42 extra = buffer;
44 /* Never use IO error if it is 0 */
45 if (IoErr() == 0) useIOError = FALSE;
47 if (useIOError)
49 Fault(IoErr(), NULL, buffer, sizeof(buffer));
50 buffer[0] = toupper(buffer[0]);
52 else
54 newline = "";
55 period = "";
56 extra = "";
59 MUI_Request
61 application, window, 0, _(MSG_TITLE), _(MSG_ERROR_OK),
62 "%s:\n%s%s%s%s", _(MSG_ERROR_HEADER), message, newline, extra, period
66 void cleanup(CONST_STRPTR message)
68 Locale_Deinitialize();
70 if (message != NULL)
72 ShowError(NULL, NULL, message, TRUE);
73 exit(RETURN_FAIL);
76 CloseLibrary(ACPICABase);
79 Object *MakeLabel(STRPTR str)
81 return (MUI_MakeObject(MUIO_Label, str, 0));
84 static inline LONG xget(Object * obj, ULONG attr)
86 IPTR x = 0;
88 GetAttr(attr, obj, &x);
89 return x;
92 Object *app;
93 Object *MainWindow;
94 Object *TablesList;
95 Object *InfoList;
96 Object *ModeCycle;
97 Object *SaveButton;
98 Object *menu_quit, *menu_dump_parsed, *menu_dump_raw;
100 AROS_UFH3(static void, display_function,
101 AROS_UFHA(struct Hook *, h, A0),
102 AROS_UFHA(const char **, strings, A2),
103 AROS_UFHA(const ACPI_TABLE_HEADER *, table, A1))
105 AROS_USERFUNC_INIT
107 const struct Parser *t = FindParser(table->Signature);
109 if (t)
111 strings[0] = t->name;
112 return;
115 snprintf(buf, sizeof(buf), "%s (%4.4s)", _(MSG_UNKNOWN), table->Signature);
116 strings[0] = buf;
118 AROS_USERFUNC_EXIT
121 static void display_callback(const char *str)
123 DoMethod(InfoList, MUIM_List_InsertSingle, str, MUIV_List_Insert_Bottom);
126 AROS_UFH3(static void, select_function,
127 AROS_UFHA(struct Hook *, h, A0),
128 AROS_UFHA(Object *, object, A2),
129 AROS_UFHA(APTR, msg, A1))
131 AROS_USERFUNC_INIT
133 IPTR mode, active;
134 const ACPI_TABLE_HEADER *table;
135 const struct Parser *t;
137 GetAttr(MUIA_Cycle_Active, ModeCycle, &mode);
139 SetAttrs(InfoList, MUIA_List_Quiet, TRUE, TAG_DONE);
140 DoMethod(InfoList, MUIM_List_Clear);
142 GetAttr(MUIA_List_Active, object, &active);
143 if (active != MUIV_List_Active_Off)
145 void (*parser)() = unknown_parser;
147 DoMethod(object, MUIM_List_GetEntry, active, &table);
149 if (mode == 0)
151 t = FindParser(table->Signature);
152 if (t)
153 parser = t->parser;
156 parser(table, display_callback);
159 SetAttrs(InfoList, MUIA_List_Quiet, FALSE, TAG_DONE);
161 AROS_USERFUNC_EXIT
164 static BPTR RequestFile(void)
166 BPTR file = BNULL;
167 struct FileRequester *req;
169 req = MUI_AllocAslRequestTags(ASL_FileRequest, ASLFR_DoSaveMode, TRUE, TAG_DONE);
170 if (!req)
171 return BNULL;
173 if (MUI_AslRequest(req, NULL))
175 ULONG len = strlen(req->fr_Drawer) + strlen(req->fr_File) + 2;
176 STRPTR pathname = AllocMem(len, MEMF_ANY);
178 if (pathname)
180 strcpy(pathname, req->fr_Drawer);
181 AddPart(pathname, req->fr_File, len);
183 file = Open(pathname, MODE_NEWFILE);
184 FreeMem(pathname, len);
187 MUI_FreeAslRequest(req);
189 return file;
192 AROS_UFH3(static void, save_function,
193 AROS_UFHA(struct Hook *, h, A0),
194 AROS_UFHA(Object *, object, A2),
195 AROS_UFHA(APTR, msg, A1))
197 AROS_USERFUNC_INIT
199 BPTR file = RequestFile();
201 if (file)
203 IPTR i, n;
205 GetAttr(MUIA_List_Entries, object, &n);
207 for (i = 0; i < n; i++)
209 APTR text;
211 DoMethod(object, MUIM_List_GetEntry, i, &text);
213 FPuts(file, text);
214 FPutC(file, '\n');
216 Close(file);
219 AROS_USERFUNC_EXIT
222 BPTR DumpFile;
224 static void dump_callback(const char *str)
226 FPuts(DumpFile, str);
227 FPutC(DumpFile, '\n');
230 AROS_UFH3(static IPTR, dumpFunc,
231 AROS_UFHA(struct Hook *, table_hook, A0),
232 AROS_UFHA(const ACPI_TABLE_HEADER *, table, A2),
233 AROS_UFHA(BOOL, parsed, A1))
235 AROS_USERFUNC_INIT
237 void (*parser)() = unknown_parser;
239 if (parsed)
241 const struct Parser *t = FindParser(table->Signature);
243 if (t)
244 parser = t->parser;
247 parser(table, dump_callback);
248 FPutC(DumpFile, '\n');
249 return TRUE;
251 AROS_USERFUNC_EXIT
254 static const struct Hook dumpHook =
256 .h_Entry = (APTR)dumpFunc
259 AROS_UFH3(static void, dump_function,
260 AROS_UFHA(struct Hook *, h, A0),
261 AROS_UFHA(Object *, object, A2),
262 AROS_UFHA(IPTR *, args, A1))
264 AROS_USERFUNC_INIT
267 * MUIM_CallHook builds an array from its arguments, and gives us a pointer to this array.
268 * We need to explicitly convert to BOOL, otherwise the value can be padded with garbage,
269 * which will be contained in IPTR's upper unused bytes.
271 BOOL parsed = args[0];
273 DumpFile = RequestFile();
274 if (DumpFile)
276 int i;
277 for (i = 0; ParserTable[i].name; i++)
278 AcpiScanTables(ParserTable[i].signature, &dumpHook, (APTR)(IPTR)parsed);
280 Close(DumpFile);
283 AROS_USERFUNC_EXIT
286 static const struct Hook display_hook =
288 .h_Entry = (APTR)display_function
291 static const struct Hook select_hook =
293 .h_Entry = (APTR)select_function
296 static const struct Hook save_hook =
298 .h_Entry = (APTR)save_function
301 static const struct Hook dump_hook =
303 .h_Entry = (APTR)dump_function
306 static const char *showModes[] =
308 "Parsed data",
309 "Raw dump",
310 NULL
313 static BOOL GUIinit()
315 BOOL retval = FALSE;
317 app = ApplicationObject,
318 MUIA_Application_Title, (IPTR)APPNAME,
319 MUIA_Application_Version, (IPTR)VERSION,
320 MUIA_Application_Copyright, (IPTR)"(C) 2004-2011, The AROS Development Team",
321 MUIA_Application_Author, (IPTR)"Pavel Fedin",
322 MUIA_Application_Base, (IPTR)APPNAME,
323 MUIA_Application_Description, __(MSG_DESCRIPTION),
325 MUIA_Application_Menustrip, MenustripObject,
326 MUIA_Family_Child, MenuObject,
327 MUIA_Menu_Title, __(MSG_MENU_PROJECT),
328 MUIA_Family_Child,
329 menu_quit = MenuitemObject, MUIA_Menuitem_Title, __(MSG_MENU_QUIT),
330 End,
331 End,
332 MUIA_Family_Child, MenuObject,
333 MUIA_Menu_Title, __(MSG_MENU_DUMP),
334 MUIA_Family_Child,
335 menu_dump_parsed = MenuitemObject,
336 MUIA_Menuitem_Title, __(MSG_MENU_DUMP_PARSED),
337 End,
338 MUIA_Family_Child,
339 menu_dump_raw = MenuitemObject,
340 MUIA_Menuitem_Title, __(MSG_MENU_DUMP_RAW),
341 End,
342 End,
343 End,
345 SubWindow, MainWindow = WindowObject,
346 MUIA_Window_Title, __(MSG_WINTITLE),
348 WindowContents, HGroup,
349 MUIA_Group_SameWidth, FALSE,
351 Child, ListviewObject,
352 MUIA_Listview_List, TablesList = ListObject,
353 InputListFrame,
354 MUIA_List_AdjustWidth, TRUE,
355 MUIA_List_DisplayHook, &display_hook,
356 End, // List
357 MUIA_CycleChain, TRUE,
358 End, // ListView
360 Child, VGroup,
361 Child, ListviewObject,
362 MUIA_Listview_List, InfoList = ListObject,
363 ReadListFrame,
364 MUIA_Font, MUIV_Font_Fixed,
365 MUIA_List_AdjustWidth, TRUE,
366 MUIA_List_ConstructHook, MUIV_List_ConstructHook_String,
367 MUIA_List_DestructHook, MUIV_List_DestructHook_String,
368 End, // List
369 MUIA_CycleChain, TRUE,
370 End, // ListView
371 Child, HGroup,
372 Child, Label(__(MSG_SHOW_MODE)),
373 Child, ModeCycle = CycleObject,
374 MUIA_Cycle_Entries, showModes,
375 MUIA_CycleChain, TRUE,
376 End,
377 Child, SaveButton = SimpleButton(__(MSG_SAVE_DATA)),
378 End,
379 End,
380 End, // WindowContents
381 End, // MainWindow
382 End; // ApplicationObject
384 if (app)
386 /* Quit application if the windowclosegadget or the esc key is pressed. */
387 DoMethod(MainWindow, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
388 app, 2,
389 MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
391 DoMethod(menu_quit, MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
392 app, 2,
393 MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
395 DoMethod(TablesList, MUIM_Notify, MUIA_List_Active, MUIV_EveryTime,
396 TablesList, 2,
397 MUIM_CallHook, &select_hook);
399 DoMethod(ModeCycle, MUIM_Notify, MUIA_Cycle_Active, MUIV_EveryTime,
400 TablesList, 2,
401 MUIM_CallHook, &select_hook);
403 DoMethod(SaveButton, MUIM_Notify, MUIA_Pressed, FALSE,
404 InfoList, 2,
405 MUIM_CallHook, &save_hook);
407 DoMethod(menu_dump_parsed, MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
408 app, 3,
409 MUIM_CallHook, &dump_hook, TRUE);
411 DoMethod(menu_dump_raw, MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
412 app, 3,
413 MUIM_CallHook, &dump_hook, FALSE);
415 retval = TRUE;
418 return retval;
421 AROS_UFH3(static IPTR, tableFunc,
422 AROS_UFHA(struct Hook *, table_hook, A0),
423 AROS_UFHA(const ACPI_TABLE_HEADER *, table, A2),
424 AROS_UFHA(void *, unused, A1))
426 AROS_USERFUNC_INIT
428 DoMethod(TablesList, MUIM_List_InsertSingle, table, MUIV_List_Insert_Bottom);
429 return TRUE;
431 AROS_USERFUNC_EXIT
434 static const struct Hook tableHook =
436 .h_Entry = (APTR)tableFunc
439 int __nocommandline = 1;
441 int main(void)
443 int i;
445 if (!Locale_Initialize())
446 cleanup(_(MSG_ERROR_LOCALE));
448 ACPICABase = OpenLibrary("acpica.library",0);
449 if (!ACPICABase)
450 cleanup(_(MSG_ERROR_NO_ACPI));
452 if (GUIinit())
454 /* Populate tables list */
455 for (i = 0; ParserTable[i].name; i++)
456 AcpiScanTables(ParserTable[i].signature, &tableHook, NULL);
458 set(MainWindow, MUIA_Window_Open, TRUE);
460 if (xget(MainWindow, MUIA_Window_Open))
462 ULONG retval = 0;
463 ULONG signals = 0;
465 while (retval != MUIV_Application_ReturnID_Quit)
467 retval = DoMethod(app, MUIM_Application_NewInput, &signals);
469 if (signals)
470 signals = Wait(signals | SIGBREAKF_CTRL_C);
472 if (signals & SIGBREAKF_CTRL_C)
473 retval = MUIV_Application_ReturnID_Quit;
475 set(MainWindow, MUIA_Window_Open, FALSE);
478 DisposeObject(app);
481 cleanup(NULL);
482 return 0;