77#include <stdio.h>
88#include <fcntl.h>
99#include <unistd.h>
10+ #include <string.h>
11+
12+ #include <sys/ioctl.h>
13+ #include <sys/mman.h>
1014
1115#include <sosousdk.h>
12- #include "../kernel/ termios.h"
16+ #include < termios.h>
1317
1418static int FrameBufferFd = -1 ;
1519static int * FrameBuffer = 0 ;
@@ -25,6 +29,16 @@ static unsigned int s_KeyQueueReadIndex = 0;
2529static unsigned int s_PositionX = 0 ;
2630static unsigned int s_PositionY = 0 ;
2731
32+ static unsigned int s_ScreenWidth = 0 ;
33+ static unsigned int s_ScreenHeight = 0 ;
34+
35+ enum EnFrameBuferIoctl
36+ {
37+ FB_GET_WIDTH ,
38+ FB_GET_HEIGHT ,
39+ FB_GET_BITSPERPIXEL
40+ };
41+
2842static unsigned char convertToDoomKey (unsigned char scancode )
2943{
3044 unsigned char key = 0 ;
@@ -90,12 +104,14 @@ static void addKeyToQueue(int pressed, unsigned char keyCode)
90104
91105struct termios orig_termios ;
92106
93- void disableRawMode () {
107+ void disableRawMode ()
108+ {
94109 //printf("returning original termios\n");
95110 tcsetattr (STDIN_FILENO , TCSAFLUSH , & orig_termios );
96111}
97112
98- void enableRawMode () {
113+ void enableRawMode ()
114+ {
99115 tcgetattr (STDIN_FILENO , & orig_termios );
100116 atexit (disableRawMode );
101117 struct termios raw = orig_termios ;
@@ -111,20 +127,34 @@ void DG_Init()
111127
112128 if (FrameBufferFd >= 0 )
113129 {
114- FrameBuffer = mmap (NULL , DOOMGENERIC_RESX * DOOMGENERIC_RESY * 4 , 0 , FrameBufferFd , 0 );
130+ printf ("Getting screen width..." );
131+ s_ScreenWidth = ioctl (FrameBufferFd , FB_GET_WIDTH );
132+ printf ("%d\n" , s_ScreenWidth );
133+
134+ printf ("Getting screen height..." );
135+ s_ScreenHeight = ioctl (FrameBufferFd , FB_GET_HEIGHT );
136+ printf ("%d\n" , s_ScreenHeight );
137+
138+ if (0 == s_ScreenWidth || 0 == s_ScreenHeight )
139+ {
140+ printf ("Unable to obtain screen info!" );
141+ exit (1 );
142+ }
143+
144+ FrameBuffer = mmap (NULL , s_ScreenWidth * s_ScreenHeight * 4 , PROT_READ | PROT_WRITE , 0 , FrameBufferFd , 0 );
115145
116146 if (FrameBuffer != (int * )-1 )
117147 {
118- printf ("mmap success\n" );
148+ printf ("FrameBuffer mmap success\n" );
119149 }
120150 else
121151 {
122- printf ("mmap failed\n" );
152+ printf ("FrameBuffermmap failed\n" );
123153 }
124154 }
125155 else
126156 {
127- printf ("opening device failed!\n" );
157+ printf ("Opening FrameBuffer device failed!\n" );
128158 }
129159
130160 enableRawMode ();
@@ -134,7 +164,7 @@ void DG_Init()
134164 if (KeyboardFd >= 0 )
135165 {
136166 //enter non-blocking mode
137- syscall_ioctl (KeyboardFd , 1 , (void * )1 );
167+ ioctl (KeyboardFd , 1 , (void * )1 );
138168 }
139169
140170 int argPosX = 0 ;
@@ -185,13 +215,9 @@ void DG_DrawFrame()
185215{
186216 if (FrameBuffer )
187217 {
188- //memcpy(FrameBuffer, DG_ScreenBuffer, DOOMGENERIC_RESX * DOOMGENERIC_RESY * 4);
189-
190- const int screenWidth = 1024 ;
191-
192218 for (int i = 0 ; i < DOOMGENERIC_RESY ; ++ i )
193219 {
194- memcpy (FrameBuffer + s_PositionX + (i + s_PositionY ) * screenWidth , DG_ScreenBuffer + i * DOOMGENERIC_RESX , DOOMGENERIC_RESX * 4 );
220+ memcpy (FrameBuffer + s_PositionX + (i + s_PositionY ) * s_ScreenWidth , DG_ScreenBuffer + i * DOOMGENERIC_RESX , DOOMGENERIC_RESX * 4 );
195221 }
196222 }
197223
0 commit comments