-- dynamically displays system information, by wolfritz@king.igs.net

include win32lib.ew
without warning

constant
  MemWin  = create(Window,"",0,Center,Center,500,180,0),
  MyTimer = 1,
  zGlobalMemoryStatus = registerw32Procedure( kernel32, "GlobalMemoryStatus", {C_POINTER} ),
  zGetWindowsDirectory = registerw32Procedure( kernel32, "GetWindowsDirectoryA", {C_POINTER, C_INT} ),
  zGetCurrentDirectory = registerw32Function( kernel32, "GetCurrentDirectoryA", {C_INT, C_POINTER}, C_INT ),
  zGetCurrentTime = registerw32Procedure( kernel32, "GetLocalTime", {C_POINTER} )

sequence meminfo, windirstring, currentdirstring
atom buffer

procedure GetMemory()
  	buffer = allocate(32)
  	poke4(buffer,32)
  	w32Proc(zGlobalMemoryStatus,{buffer})
  	meminfo = peek4u({buffer,8})
  	free(buffer)
end procedure

function GetWindowsDir()
	sequence WinDir
	integer i
	  buffer = allocate(100)
	  w32Proc(zGetWindowsDirectory, {buffer, 100})
	  WinDir = ""
	  i = 0
	  while peek(buffer+i) != 0 do
		    WinDir = WinDir & peek(buffer+i)
		    i = i+1
	  end while
	free(buffer)
	return WinDir
end function

function GetCurrentDir()
	sequence CurrentDir
	atom i, ok

	  buffer = allocate(4) -- first get the length using this dummy length.
	  i=w32Func(zGetCurrentDirectory, {4, buffer})
	  if i=0 then
		    ok = message_box( "Couldn't link to C function GetCurrentDirectoryA",
		    "Win32 Error", MB_ICONHAND+MB_TASKMODAL )
		    free(buffer)
		    abort(1)
	  end if
  	free(buffer)
  	buffer = allocate(i)  -- do it again, with the correct length this time!
	  i=w32Func(zGetCurrentDirectory, {i, buffer})
	  CurrentDir = ""
	  i = 0
	  while peek(buffer+i) != 0 do
		    CurrentDir = CurrentDir & peek(buffer+i)
		    i = i+1
	  end while
 	 free(buffer)

	  return CurrentDir
end function

function Getcurrenttime()
	sequence currenttime
	  
	  buffer = allocate(16)
	  w32Proc(zGetCurrentTime, {buffer})
	  currenttime = ""
	  for i = 0 to 15 do
		    currenttime = currenttime & peek(buffer+i)
	  end for
	  free(buffer)
	  
	  return currenttime
end function

procedure onPaint_MemWin(atom self, atom event, sequence params)	--( integer x1, integer y1, integer x2, integer y2)
	sequence s, t, string1
	GetMemory()
	setPenPos(MemWin,2,2)
	string1= "bytes of memory = " & sprintf("%d",meminfo[3])
	wPuts(MemWin,string1)
	setPenPos(MemWin,2,20)
	string1= "percent of memory in use  = " & sprintf("%d",meminfo[2]) & "%"
	wPuts(MemWin,string1 & "  - including 'virtual cache'.")
	setPenPos(MemWin,2,38)
	string1= "free memory bytes = " & sprintf("%d",meminfo[4])
	wPuts(MemWin,string1)

	setPenPos(MemWin,2,56)
	wPuts(MemWin,"Windows directory = " & windirstring)

	setPenPos(MemWin,2,74)
	wPuts(MemWin,"this program's directory")
	setPenPos(MemWin,2,92)
	wPuts(MemWin,"= " & currentdirstring)

	s=Getcurrenttime()
	t=sprintf("%02d",s[9..10]) & ":" & sprintf("%02d",s[11.12]) 
	           & ":" & sprintf("%02d",s[13..14]) 
	setText(MemWin,t & "  - - System Info - -" )
end procedure

procedure onTimer_MemWin(atom self, atom event, sequence params)	--( integer timerId )
    repaintWindow( MemWin )
end procedure  

procedure onOpen_MemWin(atom self, atom event, sequence params)
    atom kernel32 
	  kernel32 = open_dll("kernel32.dll")
	  if kernel32 = 0 then
		    puts(1,"Couldn't open kernel32.dll!\n")
	  end if
	  setTimer( MemWin, MyTimer, 1000 )
	windirstring=GetWindowsDir()
	currentdirstring=GetCurrentDir()
end procedure

setHandler( MemWin, w32HOpen,  routine_id( "onOpen_MemWin" ) )
---------------------------------------------------------------------------------------
setHandler( MemWin, w32HTimer,  routine_id( "onTimer_MemWin" ) )
---------------------------------------------------------------------------------------
setHandler( MemWin, w32HPaint,  routine_id( "onPaint_MemWin" ) )
--------------------------------------------------------------------------------------

WinMain(MemWin,Normal)
----
