• Stars
    star
    126
  • Rank 276,145 (Top 6 %)
  • Language
    Crystal
  • License
    BSD 3-Clause "New...
  • Created over 9 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

gobject-introspection for Crystal

gobject-introspection for Crystal

The primary purpose of this shard is to provide compile-time generated bindings to libraries supporting GObject Introspection, so for example Gtk.

Secondarily this project also aims to provide hand written extensions and overrides for the more popular GObject based libraries that make the bindings more friendly to use and allow users to write more idiomatic Crystal. In the midterm these should be extracted to their own shards that depend on this one.

This should be useable for simple projects but usually still requires familarity with the C interface and usage of the original library. There's most likely still incorrect code generated, some methods may not even compile when invoked as Crystal does semantic checks only then. So this should be considered a work in progress and contributions are very welcome!

Install

Install like any other shard. Additionally you'll need gobject-introspection installed as well as the GObject based library you want to use and its typelib file needs to be available.

Debian

sudo apt install libgirepository1.0-dev

Fedora

sudo dnf install gobject-introspection-devel

Usage

Include in your shard.yml:

dependencies:
  gobject:
    github: jhass/crystal-gobject
    version: ~> 0.10.0

For libraries that have convenience wrappers you just require them under the gobject namespace. For example:

require "gobject/gdk"

Gtk specifically has a convenience wrapper that starts the mainloop automatically:

require "gobject/gtk/autorun"

However the main entry point is the require_gobject macro:

require "gobject"
require_gobject "Gio"

This will replace itself with the entire generated binding code for, in this case, Gio and its dependencies.

There's a very work in progress documentation generator under the gi-doc target. Note it is a modified version of the Crystal compiler's doc generator and thus compiles almost the entire compiler, thus compiling this tool takes as much time and resources as compiling the compiler does. Also libraries such as Gtk are huge! Generating for Gtk and its dependencies will take a long time and generate about a gigabyte of documentation.

Samples

Clone the repo and give the samples a try:

cd samples
crystal run gtk_hello_world.cr

To run the samples you will also need the Gtk development package.

On Debian you can install it with:

sudo apt install libgtk-3-dev

On Fedora:

sudo dnf install gtk3-devel

I'm on Linux and I'm getting random crashes (segmentation faults)

Using GTK or another library that spawns threads? Please try adding crystal-malloc_pthread_shim to your project. If that does not help, please open an issue with a, ideally minimal, example to reproduce the crash!

Contributing

Pull requests are welcome! If you encounter a bug, the first step is to produce the most minimal example that reproduces it. Currently this has no testsuite, so the samples funciton as a sort of manual testsuite. That means including this minimal example into an issue or pull request will not only allow others to verify the bug, but also prevent it from reappearing as a regression.

Running src/generator/build_namespace.cr directly is a good way to inspect the generated code. An alternative is to add {% debug() %} to the end of the require_gobject macro definition in src/gobject.cr.

Architecture

gobject-introspection is itself a C library, so we need to have bindings for it. It is itself introspectable and we make use of that, however there's a bootstrapping problem if we would try to use the main require_gobject macro for that, there are no bindings for what it's based on! So we have a second generator that writes the output to static files under src/generated under the target gi-generator. The initial version of this was bootstrapped using hand-written bindings. A good quick check whether any changes you did still work, is to build and run this target twice, the first time to update the generated bindings and the second time to see if they still compile and produce the same output.

src/generator has the driver code for any of the generators and tools. src/g_i_repository contains the classes that define the Crystal code for each of the GObject Introspection info types.

The gi-dump target tries to completely map out all info that's present in a typelib, that is how the current bindings see that info. Look at its options, you'll need to make use of them to drill in, otherwise it's just too much output due to how it displays the same infos all over again and again recursively.

For now convenience extensions and overrides go under src/library_name, so for example src/gtk for Gtk or src/g_lib for GLib. The future prospect is to have them live in their own shards that depend on this one.

Library specific shards

Like mentioned above, we want shards that collect any convenience extensions and overrides to the code this library generates. Here's a well known list of them:

More Repositories

1

nextcloud-keeweb

Integrate Keeweb into Nextcloud
PHP
411
star
2

nodeinfo

NodeInfo defines a standardized way to expose metadata about an installation of a distributed social network
Ruby
91
star
3

carc.in

Compile & run code in
Crystal
68
star
4

configurate

A flexible configuration system
Ruby
44
star
5

DeBot

My IRC Bot
Crystal
35
star
6

insporation

Flutter based client for diaspora*
Dart
28
star
7

diaspora-openshift

Diaspora modified for easy deployment on OpenShift
Ruby
21
star
8

open_graph_reader

A library to fetch and parse OpenGraph properties from an URL or a given string.
Ruby
21
star
9

github_desktop_notifications

Display your Github notifications via libnotify
Crystal
16
star
10

crystal-malloc_pthread_shim

Redirect malloc and pthread functions to bdwgc
Crystal
6
star
11

PKGBUILDs

PKGBUILDs I (pretend) to maintain
Shell
5
star
12

crystal-build-docker

Docker images to build and run Crystal in
5
star
13

podup

A recode of SargoDaryas awesome podup as a rails app
Ruby
4
star
14

Karmalicious

Karmalicious tracks your karma to make your IRC life more fun.
Ruby
3
star
15

operation_muehle

Programmierprojekt im 3. Semester, WS12/13, HS Hannover
Java
3
star
16

bottle-linkshorter-chrome

A Chrome extension for the Bottle Linkshorter
2
star
17

project_helix

Computergrafik Projekt im 4. Semester, SS13, HS Hannover
C++
2
star
18

crpaste

A simplistic, command line focused pastebin.
Crystal
2
star
19

locale-scripts

collection of scripts I use to handle locales at Diaspora
Ruby
1
star
20

js_image_paths

Easy access to your images in the Rails asset pipeline from your client side Javascript.
Ruby
1
star
21

programmieren-2

Uebungen fΓΌr Programmieren II an der FH Hannover SS2012
Java
1
star
22

datenstrukturen_und_algorithmen

Datenstrukturen und Algorithmen SS12 FH Hannover
Java
1
star
23

rpi_gpio_toys

Toying with the GPIO stuff of the Raspberry Pi
Ruby
1
star
24

socket_activated_socks

A collection of systemd units that enable the setup of a SOCKS5 proxy using SSH
Ruby
1
star