Xscreensaver interface for ocaml

Hello,
I’m trying to create a module with a nice user interface to create screensavers in ocaml for Xscreensaver.

If creating an ocaml saver with the stubs included is easy, creating a generic user interface for that is more difficult. (here is my draft.)

To create a generic interface, I need to find another work-around, than just using 'a.

With this interface, the user should be able to register his/her callbacks.
In the C api, the “init” callback initialises a state structure, and returns it. This state structure is then provided to the “draw” callback.

ocaml doesn’t accept a generic equivalent thing like this :

type 'a callbacks = {
  init : params -> 'a;
  draw : params -> 'a -> unit;
  (* other callbacks *)
}

val ref_user_callbacks : 'a callbacks -> unit

This is what we need in this use case, but ocaml type system doesn’t accept it, so we need to find a work-around.

Trying to use a ref None doesn’t seem to work, so I made a “dummy” empty structure, see the structure called empty_calls in this draft : caml_xss.ml

This solution is not perfect, because see in the example user script caml_xss_user2.ml, it is not possible to alloc a colormap and colors in the “init” callback, and give it to the “draw” callback. (The “draw” callback needs to alloc/free this at every frames.)

Does someone know how to solve this problem?

1 Like

I could be completely mistaken here, so please ignore if so, but …

I think you want the programmer to be able to write different screensaver codes, that will need different “state” objects, right? Why not use functors/modules, so that you get polymorphism, but for any one instance of a screensaver program, you have only a single state type ?

1 Like

The user of this interface is supposed to define one state structure, which is used for his/her saver.

But I’m trying to make a generic interface that can be used to create several savers.

If I can use functors/modules for that, could you explain me how?

You can see my draft is here, and my current interface is this one. I would like the user_init callback to return this user defined state, which will then be provided to the user_draw callback (in order to replicate the c api of xscreensaver). I don’t know how to do this.

Yes, indeed I can do it with a functor, and it works!

Sorry for the previous message, I already used functors like Set, but this is the first time I need to define one myself. Thanks for the reply!

1 Like

Excellent! BTW, just in case you didn’t already figure this out, nothing stops out defining multiple different modules to which that functors can be applied, so that a program can define N different screensavers, each with different code and state type, and choose at runtime which will be executed.

Thanks again. The solution of the functor does work, but is not perfect. The code is more complicated, and also if the user creates a module, but forget to provide it to the functor, we get a “Core Dumped” (probably because the Callback.register functions don’t get called in this situation).

Could it be possible to call Callback.register twice with the same id-string to avoid this? (one time at the begining outside of the functor with dummy callbacks, and a second time inside the functor with the real one?)

Another problem is that this interface only works with the native compilation. It doesn’t work with a bytecode module (there are no error messages, but nothing happens, the expected window doesn’t appear).

For defining several savers in one, indeed I made the test and it does work!
Thanks for teaching me that! I would indeed not figure this out by myself :slight_smile:


Another question, is it possible to provide several sources fields in an opam file ? (just like in the rpm system?) chatgpt tells me that indeed it’s not possible, and that I have to “wget” the second source in the “build” section, as an alternative solution. Is it really like that?

Nice to see that people don’t forget the best screensaver of all times.
I like very much the “GL molecules” screensaver theme.

Hello UnixJunkie, one of my favorite one is "vermiculate".
The other screensaver projects are also nice, but people probably like to contribute to this one because jwz has a lot of humour in his readme file.