2

I'm trying to use Xlib directly using D and bindings I found on Github (https://github.com/madadam/X11.d).

The problem is that I get an access violation in several functions (e.g. XCreateSimpleWindow). I created a minimal example:

module test;

import X11.Xlib;
import std.stdio;

void main()
{
     Display* d = XOpenDisplay(null);
     assert(!(d is null));
     Window w = XCreateSimpleWindow(d, DefaultRootWindow(d), 0, 0, 200, 100, 0, 0, 0);
}

I use Fedora 20 and dmd 2.066.

Edit 1: @user3661500 asked me to publish the applications' output:

Access violation (dump written)

Hint: I had to translate it because my systems' language is german.

Edit 2: @Adam D. Ruppe: I get a linking error when trying your files:

dmd color.d static.d simpledisplay.d -L-lX11
/usr/bin/ld: color.o: undefined reference to symbol 'XShmPutImage'
/usr/bin/ld: note: 'XShmPutImage' is defined in DSO /lib64/libXext.so.6 so try adding it to the linker command line
/lib64/libXext.so.6: could not read symbols: Invalid operation

Thank you in advance!

user3684240
  • 1,420
  • 2
  • 12
  • 18
  • Could you try to test the value of w in a `scope(failure)` statement ? Maybe it'll match to one of the [**XErrorCode value**](https://github.com/madadam/X11.d/blob/428990591bc2726f0555b137a6cc9e64a893fd9b/X11/X.d#L325). – Abstract type Oct 26 '14 at 16:12
  • Note that [**these X11 bindings**](https://github.com/nomad-software/x11) seem to be more reliable. – Abstract type Oct 26 '14 at 16:23
  • The value of w doesn't match any of the error codes and it also varies. When using the X11 bindings you mentioned I get a linking error: `test.o: In function `_Dmain': test.d:(.text._Dmain+0x57): undefined reference to `DefaultRootWindow'` – user3684240 Oct 26 '14 at 16:50
  • I compile with `dmd test.d -L-lX11` – user3684240 Oct 26 '14 at 16:51
  • You should put the output log in the question body. It changes everything in the understanding of the problem you encounter. – Abstract type Oct 26 '14 at 18:39
  • Can you try an alternative binding? My simpledisplay.d has xlib bindings too. Download the three .d files from here: http://arsdnet.net/dcode/book/chapter_12/09/ and compile with `dmd *.d`. The resulting program should just work and display a random black and white animation. If not, let me know. Using the independent bindings will rule out mistakes in them. Perhaps the problem is 64 vs 32 bit bugs or something. – Adam D. Ruppe Oct 31 '14 at 02:56
  • For that XShmPutImage, try just adding `-L-lXext` to the command line like it suggests and see what happens then. Though I do see an important note here, it is 64 bit, I wouldn't be surprised if the other x bindings have a 64 bit bug.... and indeed, here are a couple: https://github.com/madadam/X11.d/blob/master/X11/Xlib.d#L443 those shouldn't be `uint`s, they should be `c_ulong`s. There's probably other problems like that in the bindings too. So they jsut aren't 64 bit compatible. – Adam D. Ruppe Oct 31 '14 at 13:50
  • Thank you very much! When compiling with `-m32`, it works. Could you recommend me a good binding which works with 64 Bit? – user3684240 Oct 31 '14 at 15:34
  • 1
    my simpledisplay.d's binding set does. You can copy/paste the Xlib portion out without the xshm. The portion you want is between lines 3605 and 4953, search the file for "X11 bindings needed here" and you want the bit enclosed in that version block. You could also use the whole library if you link in the xshm library or edit the code to comment that part out (search for `usingXshm` in the code and delete those clauses, leaving only the fallback branches.) – Adam D. Ruppe Oct 31 '14 at 16:08
  • What license is it under? – user3684240 Oct 31 '14 at 16:44
  • Technically, probably the MIT license for that portion because the bindings are based on the Xlib source code, but I don't really care, just do whatever you want with my code. – Adam D. Ruppe Oct 31 '14 at 17:54

1 Answers1

1

The Xlib bindings you are using aren't 64-bit compatible. (They use int or long in places where it should be c_long, a common mistake when doing bindings from C as a long in C isn't necessarily the same as a long in D)

You can fix the bindings by finding those instances in the documentation, but easier would be to compile for 32 bit with dmd -m32 or find another set of bindings that are 64 bit compatible. My simpledisplay.d has tackled this problem before, the binding code is found here: https://github.com/adamdruppe/arsd/blob/master/simpledisplay.d#L3605 and is about 1500 lines long.

Adam D. Ruppe
  • 25,382
  • 4
  • 41
  • 60