Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-03

Re: Simple X11 program

14 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

ik
to Rainer Stratmann: Exists there documentation about X11 interface? Or an example program? I want to start…

Pure xlib is not that easy. I can give you an old program I made using it, but bitmap/keyboard handling is not for "beginners" in X. You must understand better how things works etc..

Ido

Rainer Stratmann wrote 11 lines
Exists there documentation about X11 interface? Or an example program? I want to start with simply draw a bitmap and handle keyboard events. Kind regards Rainer _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Rainer Stratmann

Exists there documentation about X11 interface?
Or an example program?
I want to start with simply draw a bitmap and handle keyboard events.

Kind regards

Rainer

Reply by email

Graeme Geldenhuys
to Rainer Stratmann
Rainer Stratmann wrote
Exists there documentation about X11 interface? Or an example program? I want to start with simply draw a bitmap and handle keyboard events.

It takes some practice to use Xlib directly but it's not to bad. The C documentation is plenty on the web, which is what I use.

Alternatively, you could use fpGUI, which talks directly to Xlib (*unix) and GDI (windows) and has done all the hard work for you. Because it talks directly to the underlying graphics subsystem, you have no large library requirements like Qt, GTK etc... Just run the apps. fpGUI also contains a Visual Forms Designer to speed up your UI development.

If you don't want to use fpGUI, you can always look at it's X11 backend to see how to interface with Xlib from FPC.

You can find more information about fpGUI at the following website. The source code is available on SourceForge.net and I highly recommend you use svn to get the latest source which already contains many bug fixes since the last v0.6 release.

http://opensoft.homeip.net/fpgui/

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

Graeme Geldenhuys
to Rainer Stratmann: Exists there documentation about X11 interface? Or an example program? I want to start…
Rainer Stratmann wrote
Exists there documentation about X11 interface? Or an example program?

Here is a basic example as show in many X11 documentation. As I said, fpGUI will do all this hard work for you. ;-)

You can compile it as follows: fpc basicwin.pas

-------------------[ basicwin.pas ]--------------------
program basicwin;
{$mode objfpc}{$H+}
uses
  SysUtils, xlib, x, xutil;
const
  IconBitmapWidth = 40;
  IconBitmapHeight = 40;
   IconBitmapBits: packed array[1..200] of Byte = (
   $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00,
   $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $80, $00,
   $00, $00, $00, $40, $00, $00, $10, $00, $20, $00, $00, $20,
   $00, $10, $00, $00, $40, $00, $10, $00, $00, $80, $00, $08,
   $00, $00, $00, $01, $04, $00, $00, $00, $01, $02, $00, $00,
   $00, $02, $01, $00, $00, $00, $84, $00, $00, $00, $00, $48,
   $00, $00, $00, $00, $30, $00, $00, $00, $00, $20, $00, $00,
   $00, $00, $50, $00, $00, $00, $00, $88, $00, $00, $00, $00,
   $04, $01, $00, $00, $00, $02, $02, $00, $00, $00, $01, $02,
   $00, $00, $80, $00, $04, $00, $00, $40, $00, $08, $00, $00,
   $40, $00, $10, $00, $00, $20, $00, $20, $00, $00, $10, $00,
   $00, $00, $00, $08, $00, $00, $00, $00, $00, $00, $00, $00,
   $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00,
   $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00,
   $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00,
   $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00,
   $00, $00, $00, $00, $00, $00, $00, $00);
  BITMAPDEPTH = 1;
  TOO_SMALL = 0;
  BIG_ENOUGH = 1;
var
  Display: PDisplay;
  ScreenNum: Integer;
  ProgName: String;
procedure CheckMemory(P: Pointer);
begin
  if P = nil then
    begin
      Writeln(ProgName,': Failure Allocating Memory');
      Halt(0);
    end;
end;
procedure GetDC(Win: TWindow;var GC: TGC; FontInfo: PXFontStruct);
const
  DashList: packed array[1..2] of Byte = (12, 24);
var
  ValueMask: Cardinal;
  Values: TXGCValues;
  LineWidth: Cardinal;
  LineStyle: Integer;
  CapStyle: Integer;
  JoinStyle: Integer;
  DashOffset: Integer;
  ListLength: Integer;
begin
  ValueMask := 0;
  LineWidth := 6;
  LineStyle := LineOnOffDash;
  CapStyle := CapRound;
  JoinStyle := JoinRound;
  DashOffset := 0;
  ListLength := 2;
  FillChar(Values, SizeOf(Values), 0);
  GC :=  XCreateGC(Display, Win, ValueMask, @Values);
  XSetFont(Display, GC, FontInfo^.fid);
  XSetForeground(Display, GC, XBlackPixel(Display, ScreenNum));
  XSetLineAttributes(Display, GC, LineWidth, LineStyle, CapStyle, JoinStyle);
  XSetDashes(Display, GC, DashOffSet, @DashList, ListLength);
end;
procedure LoadFont(var FontInfo: PXFontStruct);
const
  FontName: PChar = '9x15';
begin
  Writeln('Font Struct:', SizeOf(TXFontStruct));
  FontInfo := XLoadQueryFont(Display, FontName);
  if FontInfo = nil then
    begin
      Writeln(Progname, ': Cannot open 9x15 font.');
      Halt(1);
    end;
end;
procedure PlaceText(Win: TWindow; GC: TGC; FontInfo: PXFontStruct;
  WinWidth, WinHeight: Cardinal);
const
  String1: PChar = 'Hi! I''m a window, who are you?';
  String2: PChar = 'To terminate program; Press any key';
  String3: PChar = 'or button while in this window.';
  String4: PChar = 'Screen dimensions:';
var
  Len1, Len2, Len3, Len4: Integer;
  Width1, Width2, Width3: Integer;
  CDHeight, CDWidth, CDDepth: PChar; // array[0..49] of Char;
  FontHeight: Integer;
  InitialYOffset, XOffset: Integer;
  Top, Left: Integer;
begin
Writeln('#1');
  Len1 := StrLen(String1);
  Len2 := StrLen(String2);
  Len3 := StrLen(String3);
Writeln('#2');
  Width1 := XTextWidth(FontInfo, String1, Len1);
  Width2 := XTextWidth(FontInfo, String2, Len2);
  Width3 := XTextWidth(FontInfo, String3, Len3);
  FontHeight := FontInfo^.Ascent + FontInfo^.Descent;
  XDrawString(Display, Win, GC, (WinWidth - Width1) div 2, FontHeight, String1,
    Len1);
Writeln('#2.2 ', Len2, ' ', String2);
  Left := (WinWidth - Width2);
Writeln('#2.2.5 Here');
  Left := Left div 2;
  Top := WinHeight - FontHeight * 2;
Writeln('#2.3  Top:', Top, ' Left:', Left);
  XDrawString(Display, Win, GC, Left, Top, String2, Len2);
Writeln('#2.3');
  XDrawString(Display, Win, GC, (WinWidth - Width3) div 2,
    WinHeight - FontHeight, String3, Len3);
Writeln('#3');
  CDHeight := PChar(Format(' Height = %d pixels', [XDisplayHeight(Display,
    ScreenNum)]));
  CDWidth := PChar(Format(' Width = %d pixels', [XDisplayWidth(Display,
    ScreenNum)]));
  CDDepth := PChar(Format(' Depth = %d plane(s)', [XDefaultDepth(Display,
    ScreenNum)]));
  Len4 := StrLen(String4);
  Len1 := StrLen(CDHeight);
  Len2 := StrLen(CDWidth);
  Len3 := StrLen(CDDepth);
Writeln('#4');
  InitialYOffset := WinHeight div 2 - FontHeight - FontInfo^.descent;
  XOffset := WinWidth div 4;
  XDrawString(Display, Win, GC, XOffset, InitialYOffset, String4, Len4);
  XDrawString(Display, Win, GC, XOffset, InitialYOffset + FontHeight,
    CDHeight, Len1);
  XDrawString(Display, Win, GC, XOffset, InitialYOffset + 2 * FontHeight,
    CDWidth, Len2);
  XDrawString(Display, Win, GC, XOffset, InitialYOffset + 3 * FontHeight,
    CDDepth, Len3);
Writeln('#5');
end;
procedure PlaceGraphics(Win: TWindow; GC: TGC;
  WindowWidth, WindowHeight: Cardinal);
var
  X, Y: Integer;
  Width, Height: Integer;
begin
  Writeln('Window: ', Integer(Win));
  Writeln('GC: ', INteger(GC));
  Height := WindowHeight div 2;
  Width := 3 * WindowWidth div 4;
  X := WindowWidth div 2 - Width div 2;
  Y := WindowHeight div 2 - Height div 2;
  XDrawRectangle(Display, Win, GC, X, Y, Width, Height);
end;
procedure TooSmall(Win: TWindow; GC: TGC; FontInfo: PXFontStruct);
const
  String1: PChar = 'Too Small';
var
  YOffset, XOffset: Integer;
begin
  YOffset := FontInfo^.Ascent + 2;
  XOffset := 2;
  XDrawString(Display, Win, GC, XOffset, YOffset, String1, StrLen(String1));
end;
var
  Win: TWindow;
  Width, Height: Word;
  Left, Top: Integer;
  BorderWidth: Word = 4;
  DisplayWidth, DisplayHeight: Word;
//  IconWidth, IconHeight: Word;
  WindowNameStr: PChar = 'Basic Window Program';
  IconNameStr: PChar = 'basicWin';
  IconPixmap: TPixmap;
  SizeHints: PXSizeHints;
  SizeList: PXIconSize;
  WMHints: PXWMHints;
  ClassHints: PXClassHint;
  WindowName, IconName: TXTextProperty;
  Count: Integer;
  Report: TXEvent;
  GC: TGC;
  FontInfo: PXFontStruct;
  DisplayName: PChar = nil;
  WindowSize: Integer = 0;
begin
  ProgName := ParamStr(0);
//  ProgName := 'BasicWin';  //JJS Remove when 80999 is fixed.
  SizeHints := XAllocSizeHints;
  CheckMemory(SizeHints);
  WMHints := XAllocWMHints;
  CheckMemory(WMHints);
  ClassHints := XAllocClassHint;
  CheckMemory(ClassHints);
  Display := XOpenDisplay(DisplayName);
  if Display = nil then
    begin
      Writeln(ProgName,': cannot connect to XServer',
        XDisplayName(DisplayName));
      Halt(1);
    end;
  ScreenNum := XDefaultScreen(Display);
  DisplayWidth := XDisplayWidth(Display, ScreenNum);
  DisplayHeight := XDisplayHeight(Display, ScreenNum);
  Left := 0; Top := 0;
  Width := DisplayWidth div 4;  Height := DisplayHeight div 4;
  Win := XCreateSimpleWindow(Display, XRootWindow(Display, ScreenNum),
    Left, Top, Width, Height, BorderWidth, XBlackPixel(Display, ScreenNum),
    XWhitePixel(Display, ScreenNum));
  if XGetIconSizes(Display, XRootWindow(Display, ScreenNum), @SizeList,
    @Count) = 0
  then
    Writeln('Window Manager didn''t set icon sizes - using default')
  else
    begin
    end;
  IconPixMap := XCreateBitmapFromData(Display, Win, @IconBitmapBits,
    IconBitmapWidth, IconBitmapHeight);
  SizeHints^.flags := PPosition or PSize or PMinSize;
  SizeHints^.min_width := 300;
  SizeHints^.min_height:= 200;
  if XStringListToTextProperty(@WindowNameStr, 1, @WindowName) = 0 then
    begin
      Writeln(Progname, ': structure allocation for window name failed.');
      Halt(1);
    end;
  if XStringListToTextProperty(@IconNameStr, 1, @IconName) = 0 then
    begin
      Writeln(Progname, ': structure allocqation for icon name failed.');
      Halt(1);
    end;
  WMHints^.initial_state := NormalState;
  WMHints^.input := True;
  WMHints^.icon_pixmap := IconPixmap;
  WMHints^.flags := StateHint or IconPixmapHint or InputHint;
  ClassHints^.res_name := PChar(ProgName);
  ClassHints^.res_class := 'BasicWin';
  XSetWMProperties(Display, Win, @WindowName, @IconName, nil, 0, SizeHints,
    WMHints, ClassHints);
  XSelectInput(Display, Win, ExposureMask or KeyPressMask or ButtonPressMask
    or StructureNotifyMask);
  LoadFont(FontInfo);
  GetDC(win, GC, FontInfo);
  XMapWindow(Display, Win);
  while True do
    begin
      XNextEvent(Display, @Report);
      case Report._type of
        Expose:
          begin
            Writeln('Expose');
            if Report.xexpose.count =  0 then
              begin
                if WindowSize = TOO_SMALL then
                   TooSmall(Win, GC, FontInfo)
                else
                  begin
                    PlaceText(Win, GC, FontInfo, Width, Height); //*** Fails
                    PlaceGraphics(Win, GC, Width, Height);
                  end;
              end;
          end;
        ConfigureNotify:
          begin
            Writeln('Configure Notify');
            Width := Report.xconfigure.Width;
            Height := Report.xconfigure.Height;
            Writeln(' Width: ', Width);
            Writeln(' Height: ', Height);
            if (width < SizeHints^.min_width) or
               (Height < SizeHints^.min_height)
            then
              WindowSize := TOO_SMALL
            else
              WindowSize := BIG_ENOUGH;
          end;
        ButtonPress:
          begin
            Writeln('ButtonPress');
          end;
        KeyPress:
          begin
            Writeln('KeyProcess');
            XUnloadFont(Display, FontInfo^.fid);
            XFreeGC(Display, GC);
            XCloseDisplay(Display);
            halt(0);
          end;
      else
      end;
    end;
  Writeln('End.');
end.

----------------------[ end ]------------------------

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

ik

For documentation, look for xlib, and for each specific module you require you have different documentation.

Ido

Rainer Stratmann wrote 29 lines
That woul be great, thanks. Where can I find information to understand the X11 keyboard and bitmap handling? Rainer Am Freitag, 14. März 2008 13:42 schrieb ik: > Pure xlib is not that easy. I can give you an old program I made using > it, but bitmap/keyboard handling is not for "beginners" in X. You must > understand better how things works etc.. > > Ido > > On Fri, Mar 14, 2008 at 2:49 PM, Rainer Stratmann > > wrote: > > Exists there documentation about X11 interface? > > Or an example program? > > I want to start with simply draw a bitmap and handle keyboard events. > > > > Kind regards > > > > Rainer > > _______________________________________________ > > fpc-pascal maillist - > > http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Graeme Geldenhuys
to ik
ik wrote
For documentation, look for xlib, and for each specific module you require you have different documentation. Ido

Hi Ido,

That example doesn't compile because your class is defined as TWindow, which conflicts with TWindow defined in x.pp

Simply changing your classl name from TWindow to TMyWindow should do the trick.

eg:

type
  TWindow = class
  private
    display       : pDisplay;
    RootWindow    : TWindow;  //   <<<---- conficting
    CurrentWindow : TWindow;  //   <<<---- conficting
  public
....

becomes

type
  TMyWindow = class
  private
    display       : pDisplay;
    RootWindow    : TWindow;
    CurrentWindow : TWindow;
  public
...

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

ik
to Graeme Geldenhuys

You are correct (I've translated it from an old procedural code of mine), I'll fix it and resend it

Ido

Graeme Geldenhuys wrote 52 lines
On 14/03/2008, ik wrote: > For documentation, look for xlib, and for each specific module you > require you have different documentation. > > Ido Hi Ido, That example doesn't compile because your class is defined as TWindow, which conflicts with TWindow defined in x.pp Simply changing your classl name from TWindow to TMyWindow should do the trick. eg: type TWindow = class private display : pDisplay; RootWindow : TWindow; // <<<---- conficting CurrentWindow : TWindow; // <<<---- conficting public .... becomes type TMyWindow = class private display : pDisplay; RootWindow : TWindow; CurrentWindow : TWindow; public ... Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/ _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

ik
to ik

The fix :)

ik wrote 67 lines
You are correct (I've translated it from an old procedural code of mine), I'll fix it and resend it Ido On Fri, Mar 14, 2008 at 3:52 PM, Graeme Geldenhuys wrote: > On 14/03/2008, ik wrote: > > For documentation, look for xlib, and for each specific module you > > require you have different documentation. > > > > Ido > > Hi Ido, > > That example doesn't compile because your class is defined as TWindow, > which conflicts with TWindow defined in x.pp > > Simply changing your classl name from TWindow to TMyWindow should do the trick. > > eg: > > type > TWindow = class > private > display : pDisplay; > RootWindow : TWindow; // <<<---- conficting > CurrentWindow : TWindow; // <<<---- conficting > public > .... > > becomes > > type > TMyWindow = class > private > display : pDisplay; > RootWindow : TWindow; > CurrentWindow : TWindow; > public > ... > > > > > > Regards, > - Graeme - > > > _______________________________________________ > fpGUI - a cross-platform Free Pascal GUI toolkit > http://opensoft.homeip.net/fpgui/ > _______________________________________________ > > > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > -- http://ik.homelinux.org/

Reply by email

Rainer Stratmann
to Graeme Geldenhuys: Here is a basic example as show in many X11 documentation. As I said, fpGUI will do all…
Graeme Geldenhuys wrote 6 lines
On 14/03/2008, Rainer Stratmann wrote: > Exists there documentation about X11 interface? >  Or an example program? Here is a basic example as show in many X11 documentation. As I said, fpGUI will do all this hard work for you. ;-)

Thank you very much, it works!

That's what I was looking for.

How is it possible to draw (put) Bitmaps in this Window? Here is a 800x600 pixel Bitmap (array[0..800x600-1] of dword) which I want to put in the Window (or parts of the Bitmap). With 24 bit color depth (32 bit/pixel).

Exists there documentation of the (for example) XNextEvent's ?

Rainer

Reply by email

Graeme Geldenhuys
to Rainer Stratmann
Rainer Stratmann wrote
Thank you very much, it works! That's what I was looking for.

That example actually shows quite a lot of features in one little program.

How is it possible to draw (put) Bitmaps in this Window? Here is a 800x600 pixel Bitmap (array[0..800x600-1] of dword) which I want to put in the Window (or parts of the Bitmap). With 24 bit color depth (32 bit/pixel).

Quite a bit to explain. Your best bet would be to look at the fpGUI source code. The trunk/src/corelib/x11/gfx_x11.pas unit contains a TfpgCanvasImpl class which does all that and more.

Exists there documentation of the (for example) XNextEvent's ? From the command line: man xnextevent

Alternatively google: xlib xnextevent
http://tronche.com/gui/x/xlib/event-handling/manipulating-event-queue/XNextEvent.html http://en.wikipedia.org/wiki/Xlib
.... and many more...

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

Graeme Geldenhuys
to Rainer Stratmann: Thank you very much, it works! That's what I was looking for. How is it possible to draw…
Rainer Stratmann wrote 7 lines
How is it possible to draw (put) Bitmaps in this Window? Here is a 800x600 pixel Bitmap (array[0..800x600-1] of dword) which I want to put in the Window (or parts of the Bitmap). With 24 bit color depth (32 bit/pixel). Exists there documentation of the (for example) XNextEvent's ?

You should really look for the X11 Programmers Guide which explains pretty much everything Xlib.
I think some of those books are now freely available on the net.

http://www.oreilly.com/catalog/v1/

http://www.google.co.za/url?sa=t&ct=res&cd=1&url=http%3A%2F%2Fwww.oreilly.com%2Fcatalog%2Fv1%2F&ei=9ZDbR_qMMZ_8wwGXgNWsCA&usg=AFQjCNE_wbJC40-K2_HuyYln6DOEkXhnEg&sig2=pLbdbKU7GUyzc_oLzvK9SA

The above link explains every line of that example program in details. You can view the 'Basic Window Program' (chapter 3) freely online.

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

vince coen
to Felipe Monteiro de Carvalho
Hi all;

I have 50+ units and 4 programs written in TP v7.0, also there are 30+ modules written in assembler. Some of the TP units contain inline code as well as inline aseembler. All these work in a Dos box in Windows (tested in XP) as well as Linux via dosemu/freedos.

Some of the units have code from James Lemay's Powerpack 1 and I would like to update this to Powerpack 2 but can not find this code anywhere. The author/programmer seems to have vanished. Can anyone help locate this Pascal library (with source code)?

I would like to convert all of this to fpc so that I can make the package work in all environments. This whole package has been made open source and the current version is to be placed on sourceforge to update a very old version.

Just how difficult will it be to do this?

PS. I have limited experience in Pascal just enough gained while fixing bugs and adding improvements to this package which is a Fido (BBS) File processor.

Thank you all for reading this.

Vince.

Reply by email

Jonas Maebe
to vince coen
vince coen wrote 16 lines
I have 50+ units and 4 programs written in TP v7.0, also there are 30+ modules written in assembler. Some of the TP units contain inline code as well as inline aseembler. All these work in a Dos box in Windows (tested in XP) as well as Linux via dosemu/freedos. I would like to convert all of this to fpc so that I can make the package work in all environments. This whole package has been made open source and the current version is to be placed on sourceforge to update a very old version. Just how difficult will it be to do this?

Quite difficult, since all assembler and inline assembler will have to be rewritten (either into 32 bit assembler, or -preferably- into Pascal).

Jonas

PS: please do not reply to a previous message when starting a new thread, but post a new message to the list (otherwise it gets grouped with the previous thread by many mail clients)

Reply by email