AcessNative - Cleaning up debug
[tpg/acess2.git] / AcessNative / acesskernel_src / main.c
1 /*
2  * Acess2 Native Kernel
3  * - Acess kernel emulation on another OS using SDL and UDP
4  *
5  * Kernel Main
6  */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <signal.h>
10 #include <stdint.h>
11 #ifdef __LINUX__
12 #include <sys/prctl.h>
13 #endif
14 #include <unistd.h>
15 #include <string.h>
16
17 // === IMPORTS ===
18 extern int      UI_Initialise(int Width, int Height);
19 extern void     UI_MainLoop(void);
20 extern int      VFS_Init(void);
21 extern int      Video_Install(char **Arguments);
22 extern int      NativeKeyboard_Install(char **Arguments);
23 extern int      NativeFS_Install(char **Arguments);
24 extern void     Debug_SetKTerminal(char *Path);
25 extern int      VT_Install(char **Arguments);
26 extern int      VFS_Mount(const char *Device, const char *MountPoint, const char *Filesystem, const char *Options);
27 extern int      VFS_MkDir(const char *Path);
28 extern int      SyscallServer(void);
29 extern const char       gsKernelVersion[];
30 extern const char       gsGitHash[];
31 extern int      giBuildNumber;
32
33 // === GLOBALS ===
34 const char      *gsAcessDir = "../Usermode/Output/x86_64";
35
36 // === CODE ===
37 int main(int argc, char *argv[])
38 {
39         char    **rootapp = NULL;
40          int    rootapp_argc, i;
41         // Parse command line settings
42         for( i = 1; i < argc; i ++ )
43         {
44                 if( strcmp(argv[i], "--distroot") == 0 ) {
45                         gsAcessDir = argv[++i];
46                 }
47                 else if( strcmp(argv[i], "--rootapp") == 0 ) {
48                         rootapp = &argv[++i];
49                         rootapp_argc = argc - i;
50                         break;
51                 }
52                 else {
53                         fprintf(stderr, "Unknown command line option '%s'\n", argv[i]);
54                         return -1;
55                 }
56         }       
57
58         // Kernel build information
59         printf("Acess2 Native v%s\n", gsKernelVersion);
60         printf(" Build %i, Git Hash %s\n", giBuildNumber, gsGitHash);
61
62         // Start UI subsystem
63         UI_Initialise(800, 480);
64         
65         // - Ignore SIGUSR1 (used to wake threads)
66         signal(SIGUSR1, SIG_IGN);
67                 
68         // Initialise VFS
69         VFS_Init();
70         // - Start IO Drivers
71         if( Video_Install(NULL) ) {
72                 Log_Error("Init", "Unable to load NativeVideo");
73         }
74         if( NativeKeyboard_Install(NULL) ) {
75                 Log_Error("Init", "Unable to load NativeKeyboard");
76         }
77         NativeFS_Install(NULL);
78         // - Start VTerm
79         {
80                 char    *args[] = {
81                         "Video=NativeVideo",
82                         "Input=NativeKeyboard",
83                         NULL
84                         };
85                 VT_Install(args);
86         }
87
88         VFS_MkDir("/Acess");    
89         VFS_Mount(gsAcessDir, "/Acess", "nativefs", "");
90
91         Debug_SetKTerminal("/Devices/VTerm/8");
92         
93         // Start syscall server
94         SyscallServer();
95         
96         // Spawn root application
97         if( rootapp )
98         {
99                  int    pid;
100                 char    *args[7+rootapp_argc+1];
101                 
102                 args[0] = "ld-acess";
103                 args[1] = "--open";     args[2] = "/Devices/VTerm/0";
104                 args[3] = "--open";     args[4] = "/Devices/VTerm/0";
105                 args[5] = "--open";     args[6] = "/Devices/VTerm/0";
106                 for( i = 0; i < rootapp_argc; i ++ )
107                         args[7+i] = rootapp[i];
108                 args[7+rootapp_argc] = NULL;
109                 
110                 pid = fork();
111                 if(pid < 0) {
112                         perror("Starting root application [fork(2)]");
113                         return 1;
114                 }
115                 if(pid == 0)
116                 {
117                         #ifdef __LINUX__
118                         prctl(PR_SET_PDEATHSIG, SIGHUP);        // LINUX ONLY!
119                         #endif
120                         execv("./ld-acess", args);
121                 }
122                 printf("Root application running as PID %i\n", pid);
123         }
124
125         UI_MainLoop();
126
127         return 0;
128 }
129
130 void AcessNative_Exit(void)
131 {
132         // TODO: Close client applications too
133         exit(0);
134 }
135
136 uint64_t DivMod64U(uint64_t Num, uint64_t Den, uint64_t *Rem)
137 {
138         if(Rem) *Rem = Num % Den;
139         return Num / Den;
140 }
141
142 int Module_EnsureLoaded(const char *Name)
143 {
144         return 0;
145 }
146

UCC git Repository :: git.ucc.asn.au