Recent

Author Topic: [SOLVED] Shenanigans with ncurses and different fpc switches  (Read 329 times)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1250
  • Professional amateur ;-P
Hi Y'All,

I'm rather baffled by the fact that one or more fpc switch combos affects ncurses so much!!!

The program where I'm seeing this happen:

Code: Pascal  [Select][+][-]
  1. program laz_nCurses;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   ncurses, sysutils, initc;
  7.  
  8. procedure setlocale(cat : integer; p : pchar); cdecl; external clib;
  9.  
  10. const
  11.   LC_ALL = 6;
  12.  
  13. var
  14.   index: Integer;
  15.   win_bg: Smallint = COLOR_BLUE;
  16.   win_top,
  17.   win_bottom: PWINDOW;
  18.   has_colours: Boolean = False;
  19.   use_default_colours: Boolean = False;
  20.   ch: chtype;
  21.   command_str, clear_str: String;
  22.  
  23. begin
  24.   command_str:= '';
  25.   setlocale(LC_ALL, '');
  26.   try
  27.     initscr;
  28.     noecho;
  29.     cbreak;
  30.     keypad(stdscr, TRUE);
  31.     meta(stdscr, TRUE);
  32.     halfdelay(10);
  33.  
  34.     if has_colors then
  35.     begin
  36.       has_colours:= True;
  37.       start_color;
  38.       if (use_default_colors = OK) then
  39.       begin
  40.         use_default_colours:= True;
  41.         win_bg := -1;
  42.       end;
  43.  
  44.       init_pair(1, COLOR_WHITE, win_bg);
  45.  
  46.     end;
  47.     win_top:= newwin(LINES-5, COLS, 0, 0);
  48.     win_bottom:= newwin(5, COLS, LINES-5, 0);
  49.     if has_colours then
  50.     begin
  51.       wbkgd(win_top, COLOR_PAIR(1));
  52.       wbkgd(win_bottom, COLOR_PAIR(1));
  53.     end;
  54.     erase;
  55.     refresh;
  56.     box(win_top, ACS_VLINE, ACS_HLINE);
  57.     box(win_bottom, ACS_VLINE, ACS_HLINE);
  58.     if has_colours then
  59.       mvwaddstr(win_top, 1, 2, 'Has colours');
  60.     if use_default_colours then
  61.       mvwaddstr(win_top, 2, 2, 'Use default colours');
  62.     mvwaddstr(win_bottom, 4, 1, ' Press F1 to exit ');
  63.     wmove(win_bottom, 3,1);
  64.     wrefresh(win_top);
  65.     wrefresh(win_bottom);
  66.     clear_str:='';
  67.     for index:= 1 to COLS-3 do
  68.     begin
  69.       clear_str+= ' ';
  70.     end;
  71.     repeat
  72.       ch:= getch;
  73.       case ch of
  74.         chtype('a')..chtype('z'),
  75.         chtype('A')..chtype('Z'):begin
  76.           if Length(command_str) <= (COLS - 4) then
  77.           begin
  78.             command_str+= char(ch);
  79.           end
  80.           else
  81.           begin
  82.             flash;
  83.           end;
  84.         end;
  85.         KEY_BACKSPACE:begin
  86.           if Length(command_str) > 0 then
  87.           begin
  88.             SetLength(command_str, Length(command_str) - 1);
  89.           end;
  90.         end;
  91.         10:begin
  92.           flash;
  93.         end;
  94.         KEY_RESIZE:begin
  95.           wclear(win_bottom);
  96.           mvwin(win_bottom,LINES-5,0);
  97.           box(win_bottom, ACS_VLINE, ACS_HLINE);
  98.           mvwaddstr(win_bottom, 4, 1, ' Press F1 to exit ');
  99.           wrefresh(win_bottom);
  100.  
  101.           wclear(win_top);
  102.           wresize(win_top, LINES-5, COLS);
  103.           box(win_top, ACS_VLINE, ACS_HLINE);
  104.           mvwaddstr(win_top,5,1,PCHAR(format('Lines: %d; Cols: %d',[LINES,COLS])));
  105.           wrefresh(win_top);
  106.         end;
  107.       end;
  108.       wmove(win_bottom, 3,1);
  109.       waddstr(win_bottom, PCHAR(clear_str));
  110.       wmove(win_bottom, 3,1);
  111.       waddstr(win_bottom, PCHAR(command_str));
  112.       wrefresh(win_bottom);
  113.     until ch = KEY_F(1);
  114.  
  115.   finally
  116.     delwin(win_top);
  117.     delwin(win_bottom);
  118.     endwin;
  119.     //WriteLN('Command entered: ', command_str);
  120.   end;
  121. end.

The script I'm using to compile it:

Code: Bash  [Select][+][-]
  1. #!/bin/bash
  2.  
  3. /home/gcarreno/FreePascal/fpc/bin/x86_64-linux/fpc.sh \
  4.   -MObjFPC \
  5.   -Scghi \
  6.   -O1 \
  7.   -g \
  8.   -gl \
  9.   -l \
  10.   -vewnhibq \
  11.   -Fi/home/gcarreno/Programming/lazNCurses/bin/lib/x86_64-linux \
  12.   -Fu/home/gcarreno/Programming/lazNCurses/src/ \
  13.   -FU/home/gcarreno/Programming/lazNCurses/bin/lib/x86_64-linux/ \
  14.   -FE/home/gcarreno/Programming/lazNCurses/bin/ \
  15.   /home/gcarreno/Programming/lazNCurses/src/laz_nCurses.pas

NOTE: The same fpcupdeluxe install dir is being used by the script and the IDE, that contains trunk, if anyone mentions apples to apples comparison!! :D

When I use the above script as is, I get the result shown in the lazncurses-O1 image below.

When I change the optimisation to -O2 on the above script, I get the result shown in the lazncurses-O2 image below.

When I don't use the above script, but use Lazarus 4.99 ( Updated today 22 Jun 2025 ), I get the result in the lazncurses-Lazarus-4.99 image below.

Many things now baffle me!!

- On the -O1 and -O2 cases, I'm able to have the ACS_* thing working. But somehow, -O2 makes the text on the upper box go away.
- When running the binary compiled by Lazarus, the text works, but the ACS_* does not work.

BTW, the ACS_* is a set of functions that allows to translate characters to cardinals. And you can rely on that to manage the border characters used with the box utility. They kinda work like constants, but somehow are functions... go figure...

I'm rather confused by this. Especially because I was under the impression that the ACS_* stuff was reliant on the console being in UTF8 mode. And this kinda denies it... I think... Still not sure because I haven't taken the time to see what switches differ from my script and the default set of switches that Lazarus employs( I don't think I've messed with those )

Can anyone, please, PLEASE, with sugar on top, help me suss out what's going on here?

Many, MANY thanks in advance !!!

Cheers,
Gus
« Last Edit: June 23, 2025, 12:29:08 pm by Gustavo 'Gus' Carreno »
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno


Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1250
  • Professional amateur ;-P
Re: Shenanigans with ncurses and different fpc switches
« Reply #2 on: June 23, 2025, 11:00:34 am »
Hey tetrastes,

About Lazarus and ncurses: https://forum.lazarus.freepascal.org/index.php/topic,69069.msg535666.html#msg535666, https://forum.lazarus.freepascal.org/index.php/topic,69069.msg535768.html#msg535768.

Thanks for this. I'll try and see about that -Cg stuff !!

Anything pertaining to fpc switches is rather welcomed !!

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1250
  • Professional amateur ;-P
Re: [SOLVED] Shenanigans with ncurses and different fpc switches
« Reply #3 on: June 23, 2025, 12:32:00 pm »
Hey Y'All,

Finally got to the bottom of this!!

It's the "Generate PIC code"(-Cg) switch that is messing with my code.

In another thread I'm now asking why it's an hard coded thing and cannot be disabled/removed via the Project Options dialog user interface, unless you kinda do a hack.
Lets see where that goes!! :D

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

 

TinyPortal © 2005-2018