use m4_normalize on the output strings to remove trailing spaces/tabs and consolidate...
[AROS.git] / workbench / c / iprefs / main.c
blob74b8ca94c5aea276d45c0d7705929e816ee91b00
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Iprefs
6 Lang: English
7 */
8 /******************************************************************************
11 NAME
13 Iprefs
15 SYNOPSIS
17 (N/A)
19 LOCATION
23 FUNCTION
25 Initialises preferences files on startup.
27 INPUTS
29 RESULT
31 NOTES
32 This is a command executed on startup by the s:startup-sequence script file.
33 So no actual need of the command afterwards.
35 EXAMPLE
37 BUGS
39 SEE ALSO
41 Decoration
43 INTERNALS
45 HISTORY
47 ******************************************************************************/
49 #include "global.h"
50 #include "trackdiskprefs.h"
52 #include <dos/dostags.h>
54 #define DEBUG 0
55 #include <aros/debug.h>
57 #include <aros/detach.h>
58 #include <stdlib.h>
59 #include <stdio.h>
60 #include <string.h>
62 /*********************************************************************************************/
64 static struct libinfo
66 APTR var;
67 STRPTR name;
68 WORD version;
69 BOOL nocloseafterpatch;
71 libtable[] =
73 {&IntuitionBase , "intuition.library" , 39, FALSE },
74 {&GfxBase , "graphics.library" , 39, FALSE },
75 {&UtilityBase , "utility.library" , 39, TRUE },
76 {&IFFParseBase , "iffparse.library" , 39, FALSE },
77 {&LocaleBase , "locale.library" , 39, TRUE },
78 {&KeymapBase , "keymap.library" , 39, FALSE },
79 {&KMSBase , "kms.library" , 3, FALSE },
80 {&LayersBase , "layers.library" , 39, FALSE },
81 {&DataTypesBase , "datatypes.library" , 39, FALSE },
82 {&DiskfontBase , "diskfont.library" , 39, FALSE },
83 {NULL }
86 /*********************************************************************************************/
88 static struct prefinfo
90 STRPTR filename;
91 STRPTR filenamebuffer;
92 void (*func)(STRPTR);
93 struct NotifyRequest nr;
94 BOOL notifystarted;
96 preftable[] =
98 {"input" , inputprefsname , InputPrefs_Handler },
99 {"locale" , localeprefsname , LocalePrefs_Handler }, /* Keep before font */
100 {"font" , fontprefsname , FontPrefs_Handler },
101 {"palette" , paletteprefsname , NULL },
102 {"wbpattern" , patternprefsname , WBPatternPrefs_Handler },
103 {"icontrol" , icontrolprefsname , IControlPrefs_Handler },
104 {"screenmode" , screenmodeprefsname, ScreenModePrefs_Handler },
105 {"serial" , serialprefsname , SerialPrefs_Handler },
106 {"printer" , printerprefsname , PrinterPrefs_Handler },
107 {"pointer" , pointerprefsname , PointerPrefs_Handler },
108 {"overscan" , overscanprefsname , NULL },
109 {NULL }
113 /*********************************************************************************************/
115 static ULONG notifysig;
117 /*********************************************************************************************/
119 static void CloseLibs(void);
120 static void KillNotifications(void);
122 /*********************************************************************************************/
124 WORD ShowMessage(STRPTR title, STRPTR text, STRPTR gadtext)
126 struct EasyStruct es;
128 es.es_StructSize = sizeof(es);
129 es.es_Flags = 0;
130 es.es_Title = title;
131 es.es_TextFormat = text;
132 es.es_GadgetFormat = gadtext;
134 return EasyRequestArgs(NULL, &es, NULL, NULL);
136 /*********************************************************************************************/
138 void Cleanup(STRPTR msg)
140 if (msg)
142 if (IntuitionBase /* && !((struct Process *)FindTask(NULL))->pr_CLI */)
144 ShowMessage("IPrefs", msg, "Ok");
149 KillNotifications();
150 RootPatternCleanup();
151 CloseLibs();
152 Detach();
154 exit(prog_exitcode);
158 /*********************************************************************************************/
160 static void OpenLibs(void)
162 struct libinfo *li;
164 for (li = libtable; li->var; li++)
166 if (!((*(struct Library **)li->var) = OpenLibrary(li->name,
167 li->version)))
169 sprintf(s, "Can't open %s V%d!", li->name, li->version);
170 Cleanup(s);
175 /*********************************************************************************************/
177 static void CloseLibs(void)
179 struct libinfo *li;
181 for(li = libtable; li->var; li++)
183 if (!patches_installed || !li->nocloseafterpatch)
185 if (*(struct Library **)li->var) CloseLibrary((*(struct Library **)li->var));
190 /*********************************************************************************************/
192 static void GetENVName(void)
194 BPTR lock;
195 BOOL ok = FALSE;
197 lock = Lock("ENV:", SHARED_LOCK);
198 if (lock)
200 if (NameFromLock(lock, envname, 256)) ok = TRUE;
201 UnLock(lock);
204 if (!ok) Cleanup("Error expanding \"ENV:\" to full name!");
207 /*********************************************************************************************/
209 static void StartNotifications(void)
211 WORD i;
213 notifyport = CreateMsgPort();
214 if (!notifyport) Cleanup("Can't create notification msg port!\n");
216 notifysig = 1L << notifyport->mp_SigBit;
218 for(i = 0; preftable[i].filename; i++)
220 strncpy(preftable[i].filenamebuffer, envname, 256);
221 AddPart(preftable[i].filenamebuffer, "Sys", 256);
222 AddPart(preftable[i].filenamebuffer, preftable[i].filename, 256);
223 if (strlen(preftable[i].filenamebuffer) < 250)
224 strcat(preftable[i].filenamebuffer, ".prefs");
225 else
227 D(bug("Filename too long for %s prefs\n", preftable[i].filename));
228 Cleanup("Prefs filename too long!\n");
231 preftable[i].nr.nr_Name = preftable[i].filenamebuffer;
232 preftable[i].nr.nr_UserData = i;
233 preftable[i].nr.nr_Flags = NRF_SEND_MESSAGE | NRF_NOTIFY_INITIAL;
234 preftable[i].nr.nr_stuff.nr_Msg.nr_Port = notifyport;
236 D(bug("\nTrying to start notification for file \"%s\".\n", preftable[i].filenamebuffer));
238 if (StartNotify(&preftable[i].nr))
240 D(bug("Notification successfully started.\n"));
242 preftable[i].notifystarted = TRUE;
244 else
246 D(bug("Notification start failed!! Continuing anyway!\n"));
249 } /* for(i = 0; preftable[i].filename; i++) */
252 /*********************************************************************************************/
254 static void KillNotifications(void)
256 WORD i;
258 for(i = 0; preftable[i].filename; i++)
260 if (preftable[i].notifystarted)
262 EndNotify(&preftable[i].nr);
263 preftable[i].notifystarted = FALSE;
267 if (notifyport)
269 DeleteMsgPort(notifyport);
270 notifyport = NULL;
271 notifysig = 0;
275 /*********************************************************************************************/
277 static void PreparePatches(void)
279 struct IPrefsSem *sem;
280 BOOL created_sem = FALSE;
282 sem = AllocVec(sizeof(struct IPrefsSem), MEMF_PUBLIC | MEMF_CLEAR);
283 if (!sem) Cleanup("Out of memory!");
285 InitSemaphore(&sem->sem);
286 sem->sem.ss_Link.ln_Name = sem->semname;
287 strcpy(sem->semname, IPREFS_SEM_NAME);
289 Forbid();
290 if(!(iprefssem = (struct IPrefsSem *)FindSemaphore(IPREFS_SEM_NAME)))
292 iprefssem = sem;
293 AddSemaphore(&iprefssem->sem);
295 created_sem = TRUE;
297 Permit();
299 if (created_sem)
301 InstallPatches();
303 else
305 FreeVec(sem);
310 /*********************************************************************************************/
312 static void HandleNotify(void)
314 struct NotifyMessage *msg;
316 while((msg = (struct NotifyMessage *)GetMsg(notifyport)))
318 WORD id = msg->nm_NReq->nr_UserData;
320 D(bug("Received notify message. UserData = %d --> File = \"%s\"\n", id,
321 preftable[id].filenamebuffer));
323 if (preftable[id].func)
325 preftable[id].func(preftable[id].filenamebuffer);
328 ReplyMsg(&msg->nm_ExecMessage);
330 } /* while((msg = (struct NotifyMessage *)GetMsg(notifyport))) */
333 /*********************************************************************************************/
335 static void HandleAll(void)
337 ULONG sigs;
339 for(;;)
341 sigs = Wait(notifysig | SIGBREAKF_CTRL_C);
343 if (sigs & SIGBREAKF_CTRL_C) break;
345 if (sigs & notifysig) HandleNotify();
347 } /* for(;;) */
351 /*********************************************************************************************/
354 int __nocommandline = 1;
355 STRPTR __detached_name = IPREFS_SEM_NAME;
357 int main(void)
359 OpenLibs();
360 ReadTDPrefs();
361 GetENVName();
362 StartNotifications();
363 PreparePatches();
364 HandleNotify();
365 Detach();
366 HandleAll();
367 Cleanup(NULL);
369 return 0;
372 /*********************************************************************************************/