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

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