AcessNative - Included ipcpipe for AxWin3
[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 #include "../../KernelLand/Kernel/include/logdebug.h"
17
18 #define VALGRIND_CLIENT 0
19
20 // === IMPORTS ===
21 extern int      UI_Initialise(int Width, int Height);
22 extern void     UI_MainLoop(void);
23 extern int      VFS_Init(void);
24 extern int      Video_Install(char **Arguments);
25 extern int      NativeKeyboard_Install(char **Arguments);
26 extern int      NativeFS_Install(char **Arguments);
27 extern void     Debug_SetKTerminal(char *Path);
28 extern int      VT_Install(char **Arguments);
29 extern int      Mouse_Install(char **Arguments);
30 extern int      IPCPipe_Install(char **Arguments);
31 extern int      VFS_Mount(const char *Device, const char *MountPoint, const char *Filesystem, const char *Options);
32 extern int      VFS_MkDir(const char *Path);
33 extern int      SyscallServer(void);
34 extern int      Server_Shutdown(void);
35 extern const char       gsKernelVersion[];
36 extern const char       gsGitHash[];
37 extern int      giBuildNumber;
38
39 // === GLOBALS ===
40 const char      *gsAcessDir = "../Usermode/Output/x86_64";
41
42 // === CODE ===
43 #ifndef __WIN32__
44 #define P_NOWAIT        0
45 int spawnv(int flags, const char *execuable, char * const argv[])
46 {
47         int pid = fork();
48         if( pid != 0 )  return pid;
49
50         execv(execuable, argv);
51         perror("spawnv - execve");
52         for(;;);
53 }
54 #endif
55
56 int main(int argc, char *argv[])
57 {
58         char    **rootapp = NULL;
59          int    rootapp_argc, i;
60         // Parse command line settings
61         for( i = 1; i < argc; i ++ )
62         {
63                 if( strcmp(argv[i], "--distroot") == 0 ) {
64                         gsAcessDir = argv[++i];
65                 }
66                 else if( strcmp(argv[i], "--rootapp") == 0 ) {
67                         rootapp = &argv[++i];
68                         rootapp_argc = argc - i;
69                         break;
70                 }
71                 else {
72                         fprintf(stderr, "Unknown command line option '%s'\n", argv[i]);
73                         return -1;
74                 }
75         }       
76
77         // Kernel build information
78         printf("Acess2 Native v%s\n", gsKernelVersion);
79         printf(" Build %i, Git Hash %s\n", giBuildNumber, gsGitHash);
80
81         // Start UI subsystem
82         UI_Initialise(800, 480);
83         
84         // - Ignore SIGUSR1 (used to wake threads)
85         #ifdef SIGUSR1
86         signal(SIGUSR1, SIG_IGN);
87         #endif
88                 
89         // Initialise VFS
90         VFS_Init();
91         // - Start IO Drivers
92         if( Video_Install(NULL) ) {
93                 Log_Error("Init", "Unable to load NativeVideo");
94         }
95         if( NativeKeyboard_Install(NULL) ) {
96                 Log_Error("Init", "Unable to load NativeKeyboard");
97         }
98         NativeFS_Install(NULL);
99         Mouse_Install(NULL);
100         IPCPipe_Install(NULL);
101         // - Start VTerm
102         {
103                 char    *args[] = {
104                         "Video=NativeVideo",
105                         "Input=NativeKeyboard",
106                         NULL
107                         };
108                 VT_Install(args);
109         }
110
111         VFS_MkDir("/Acess");    
112         VFS_Mount(gsAcessDir, "/Acess", "nativefs", "");
113
114         Debug_SetKTerminal("/Devices/VTerm/8");
115         
116         // Start syscall server
117         SyscallServer();
118         
119         // Spawn root application
120         if( rootapp )
121         {
122                  int    pid;
123                  int    argcount = 0;
124                 const char      *args[7+rootapp_argc+1+1];
125
126                 #if VALGRIND_CLIENT
127                 args[argcount++] = "valgrind";
128                 #endif
129                 args[argcount++] = "./ld-acess";
130                 args[argcount++] = "--open";    args[argcount++] = "/Devices/VTerm/0";
131                 args[argcount++] = "--open";    args[argcount++] = "/Devices/VTerm/0";
132                 args[argcount++] = "--open";    args[argcount++] = "/Devices/VTerm/0";
133                 for( i = 0; i < rootapp_argc; i ++ )
134                         args[argcount+i] = rootapp[i];
135                 args[argcount+rootapp_argc] = NULL;
136                 pid = spawnv(P_NOWAIT, "./ld-acess", args);
137                 if(pid < 0) {
138                         perror("Starting root application [fork(2)]");
139                         return 1;
140                 }
141                 printf("Root application running as PID %i\n", pid);
142         }
143
144         UI_MainLoop();
145
146         return 0;
147 }
148
149 void AcessNative_Exit(void)
150 {
151         // TODO: Close client applications too
152         Server_Shutdown();
153         exit(0);
154 }
155
156 uint64_t DivMod64U(uint64_t Num, uint64_t Den, uint64_t *Rem)
157 {
158         if(Rem) *Rem = Num % Den;
159         return Num / Den;
160 }
161
162 int Module_EnsureLoaded(const char *Name)
163 {
164         return 0;
165 }
166

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