Feature request: instrisic Defines for console / gui mode

For other topics related to the FreeBASIC project or its community.
marpon
Posts: 250
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: Feature request: instrisic Defines for console / gui mode

Postby marpon » Sep 15, 2018 13:42

@fxm good and quick done :-)


on FBWiki : KeyPgDdfbgui
could be added , only for win32 / win64

the example

#if __FB_GUI__ <> 0 'could be also just #if __FB_GUI__
#print Executable subsystem: gui
#else
#print Executable subsystem: console
#endif

can also be the opposite

#if not __FB_GUI__ 'notice the not
#print Executable subsystem: console
#else
#print Executable subsystem: gui
#endif
fxm
Posts: 8424
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Feature request: instrisic Defines for console / gui mode

Postby fxm » Sep 15, 2018 14:09

marpon wrote:#if __FB_GUI__ <> 0 'could be also just #if __FB_GUI__

Or:
#if __FB_GUI__ = True
#print Executable subsystem: gui
#else
#print Executable subsystem: console
#endif

I usually code this way (with relational operators) to avoid an unexpected response if the intrinsic is not defined as for the following example:
#if __FB_CONSOLE__
#print Executable subsystem: console
#else
#print Executable subsystem: gui
#endif
marpon
Posts: 250
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: Feature request: instrisic Defines for console / gui mode

Postby marpon » Sep 15, 2018 14:23

@fxm

understand, please as you like <> 0 or = true

i"m not sure the "not" usage on #if is somewhere explained
i use chm, and did not found that usage

it is my c notions to push me trying that :-)
fxm
Posts: 8424
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Feature request: instrisic Defines for console / gui mode

Postby fxm » Sep 15, 2018 14:24

marpon wrote:on FBWiki : KeyPgDdfbgui
could be added , only for win32 / win64

KeyPgDdfbgui → fxm [Added paragraph 'Platform differences']
fxm
Posts: 8424
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Feature request: instrisic Defines for console / gui mode

Postby fxm » Sep 15, 2018 14:35

marpon wrote:i"m not sure the "not" usage on #if is somewhere explained
i use chm, and did not found that usage

it is my c notions to push me trying that :-)

IMHO, 'Not' applies to 'condition' and not to '#If':

If ( Not condition ) Then ...

#if ( Not condition ) ...
marpon
Posts: 250
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: Feature request: instrisic Defines for console / gui mode

Postby marpon » Sep 15, 2018 14:50

@aggree
is why i've tested it, do not care...

thank's again for the reaction

on the sleep extension feature, it will be another story, as i think we have to work on the fb_sleep function of the rtlib side,

i'm even not be able to compile the rtlib (to test my mingw tool-chain installation)
fxm
Posts: 8424
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Feature request: instrisic Defines for console / gui mode

Postby fxm » Sep 16, 2018 5:48

I don'y know why all links to the '__FB_GUI__' page (including the page title) appear as '__Fb_Gui__' only in the .chm documentation file (while everywhere defined in full capital letters).
Maybe because the "PrintToc" page had not been updated yet?
So I try that:
PrintToc → fxm [Manual generated update]
marpon
Posts: 250
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: Feature request: instrisic Defines for console / gui mode

Postby marpon » Sep 16, 2018 16:16

extend for sleep without value in code as Jocep Roca was suggesting

in fact it does not exist any safe solution, at compile time , the test is too restrictive,
the only safe way is to do specific tests at run time

i've made a first draft , not complete because it should also take into account ScreenRes function

Code: Select all


''''try both possibilities  -s gui  and  -s console
#include once "windows.bi"

#if __FB_GUI__
   private function  isgui()as long
      return 1
   end function

#else
   private function  isgui()as long
      return 0
   end function
#endif

#undef  Screen

declare function fb_screen  alias "fb_GfxScreen"( _
      byval mode as long, byval depth as long=8, byval num_pages as long = 0, _
      byval flags as long = 0, byval refresh_rate as long = 0) as long
     
private Function screen( byval mode as long, byval depth as long=8, byval num_pages as long = 0, _
      byval flags as long = 0, byval refresh_rate as long = 0) as long
   STATIC timesCalled As long = 0
   dim as long ret
   if mode = -9 then
      IF timesCalled > 0 then return timesCalled
      return 0
   end if
   ret = fb_screen (mode, depth, num_pages, flags, refresh_rate)
   IF ScreenPtr <> 0 then timesCalled += 1
   return ret       
End function     


sub mysleep()
   IF screenptr then
         print "existing gui graphic mode"
         sleep
   ELSEif isgui() then
      IF screen(-9) = 0 then
         if GetConsoleCP() then
            print "existing console"
            sleep
         else
            AllocConsole ()
            print " created console"
            sleep
         end if
      else
         messagebox(0, "was before in gui graphic mode", "Indefined Sleep", MB_ICONWARNING + MB_SYSTEMMODAL)   
      end if
   else
      print "console mode"
      sleep
   endif
end sub

'' hack, undefine  only 1version   of SLEEP,  (by chance it is the right one , because 2 exists)
#undef sleep


'' declare the rtlib functions
declare sub fb_Sleep alias "fb_Sleep" ( byval amount as long = -1 )


'' SLEEP interceptors
function Sleep overload ( byval amount as long = -1 ) as long
   print "  - SLEEP(1) intercepted!"
   if amount < 0  then mysleep()  : return 0
   fb_Sleep( amount ) '' call original sleep command
   return 0
end function


' main part

Dim w As Integer, h As Integer
Dim depth As Integer
Dim driver_name As String
print "message in console"
Screen 15, 32
' Obtain info about current mode
ScreenInfo w, h, depth,,,,driver_name
Print Str(w) + "x" + Str(h) + "x" + Str(depth);
Print " using " + driver_name + " driver"

sleep

' Quit graphics mode
'Screen 0   ' this one close gfx window and clears the console if exists

' Quit graphics mode without clearing console if exists 
screen 0 , 0 , 0 , &h80000000


'obtain info about desktop
ScreenInfo w, h, depth,,,,driver_name
Print "Desktop running at " + Str(w) + "x" + Str(h) + "x" + Str(depth);
print " using " + driver_name + " driver"
sleep



the case not very well done is in gui mode (without console) and opening a gfx graphic window
after closing the gfx graphic mode, even if you attach console with AllocConsole (), the console opens but is not working ,
probably that behaviour was not tested before,


i trap the case for screen function only, with overloaded function (static inside) to check if it was used,
but has to be expanded for general case for screenres also
only 1 static incremented by both functions screen and screenres can do the trick,

but it could be better to restore an initial clean state to allow a normal working AllocConsole (), i let it for someone interrested ...

i did not noticed problems if not using gfx window
fxm
Posts: 8424
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Feature request: instrisic Defines for console / gui mode

Postby fxm » Sep 17, 2018 8:48

fxm wrote:I don'y know why all links to the '__FB_GUI__' page (including the page title) appear as '__Fb_Gui__' only in the .chm documentation file (while everywhere defined in full capital letters).
Maybe because the "PrintToc" page had not been updated yet?
So I try that:
PrintToc → fxm [Manual generated update]

Always the same problem with the today "freebasic_manual.chm" file.

@admins, @St_W,
Have you an idea (good on wiki) ?
coderJeff
Site Admin
Posts: 2717
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Feature request: instrisic Defines for console / gui mode

Postby coderJeff » Sep 17, 2018 10:49

Sorry, I should have seen earlier. __FB_GUI__ needs to be added to doc\manual\templates\default\keywords.lst. This file is used to normalize the case of keywords in documentation pages, titles, links, examples. Can get this fixed on the next wiki snapshot.
MrSwiss
Posts: 2811
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Feature request: instrisic Defines for console / gui mode

Postby MrSwiss » Sep 19, 2018 14:03

REMARK: on the current implementation of: __FB_GUI__ (console = 0 = FALSE | gui = -1 = TRUE)

COMPLAINT: makes evaluation more complex, than needed/desired!

Proposed simplification:
    only define __FB_GUI__ -- if "-s gui" is detected, otherwise, don't define it, at all!
    (The same as: __FB_64BIT__ implementation!)

Example usage: (implemented similar to: __FB_64BIT__ check)

Code: Select all

#Ifdef __FB_GUI__    '' alternative #Ifndef ... #If defined(__FB_GUI__) = more verbose
...
#Else
...
#EndIf
Which is currently NOT possible, without a value or boolean, check. (it always returns TRUE, currently!)

Code: Select all

'' current:
#If __FB_GUI__ = TRUE    '' or: #If __FB_GUI__ <> 0
...
#Else
...
#EndIf
jj2007
Posts: 879
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Feature request: instrisic Defines for console / gui mode

Postby jj2007 » Sep 19, 2018 15:56

MrSwiss wrote:

Code: Select all

#Ifdef __FB_GUI__
...
#Else
...
#EndIf
Yep, that's a lot simpler than

Code: Select all

#if fbgui
   print "it's GUI"
#else
   print "it's Console"
#endif
MrSwiss
Posts: 2811
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Feature request: instrisic Defines for console / gui mode

Postby MrSwiss » Sep 19, 2018 16:43

A bit of a simpleton comment? (see comments in your code)
jj2007 wrote:Yep, that's a lot simpler than

Code: Select all

#if fbgui   '' don't work anyhow
   print "it's GUI"  '' if it would work, always GUI
#else
   print "it's Console"  '' never used
#endif
coderJeff
Site Admin
Posts: 2717
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Feature request: instrisic Defines for console / gui mode

Postby coderJeff » Sep 19, 2018 16:55

The platform (e.g. __FB_WIN32__, __FB_64BIT__) intrinsic defines are defined/not defined. All other intrinsic defines are defined to a value.

By setting the intrinsic to a value, the intrinsic define can be substituted wherever a constant could be used, for example:

Code: Select all

#if __FB_GUI__ andalso __FB_OUT_EXE__
'' some code
#endif

sub StartLog( mode as boolean = __FB_GUI__ )
end sub

That ease of substitution can't be done if the symbol is maybe defined, or is maybe not defined.
fxm
Posts: 8424
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Feature request: instrisic Defines for console / gui mode

Postby fxm » Sep 19, 2018 16:56

MrSwiss wrote:A bit of a simpleton comment? (see comments in your code)
jj2007 wrote:Yep, that's a lot simpler than

Code: Select all

#if fbgui   '' don't work anyhow
   print "it's GUI"  '' if it would work, always GUI
#else
   print "it's Console"  '' never used
#endif

Yet it works (although I do not like this use):

Code: Select all

#if __fb_gui__
   #print "it's GUI"      ' if (__fb_gui__ is defined) and (__fb_gui__ <> 0)
#else
   #print "it's Console"  ' if (__fb_gui__ is undefined) or (__fb_gui__ = 0)
#endif

Return to “Community Discussion”

Who is online

Users browsing this forum: No registered users and 3 guests