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

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