GTK-server
Encyclopedia
GTK-server is an open source
project released under the GNU General Public License
. The GTK-server project aims to bring Graphical User Interface
programming to any interpreted language
using the GIMP Tool Kit (GTK) or XForms (toolkit)
.
), ipc (message queue
), tcp or udp. Any interpreted language or shellscript with I/O capabilities can start the GTK-server with an argument specifying the type of interface, and can start sending GTK function calls in S-Expression
format. After each request, the GTK-server returns a result, depending on the type of GTK function invoked.
If the GTK-server is compiled as a shared object, it exports the function 'gtk', which must be imported in the client program first. After that, the client program can start sending GTK function calls in S-Expression format as argument to the imported 'gtk' function.
Before the GTK-server actually can execute GTK functions, it has to read a configuration file in which the prototypes of the GTK functions are described. Since version 2.2.3 this also can be done on-the-fly, allowing the GTK-server to run without configuration file.
1) Accessing foreign functions is only possible when the accessed libraries are created with a non object oriented programming language like C or Pascal. Libraries created with C++
for example, use name mangling
in order to unify overloaded functions. This means that the actual functionname in a C++ library cannot be known once the shared library has been compiled. Hence the functions in such a library cannot be accessed. Therefore, libraries like wxWidgets
, the Qt toolkit, FLTK
which are programmed in C++, cannot be accessed with the GTK-server concept. More on this matter is explained here.
2) The GTK library was implemented in the C programming language. Since C is a strongly typed programming language, the interpreted program needs to know the type of arguments and the type of the return value for each GTK function during runtime. These can be defined on-the-fly or in a configuration file, which is parsed by the GTK-server during startup. However, the GTK-server does not know the actual functions which are going to be used by the interpreted client program, so for GTK-server all arguments and return values for each GTK function are variable types.
This leads to a problem for the implementation, because the GTK functions and the corresponding arguments and return values cannot be hardcoded into the GTK-server binary.
The way to resolve this is by using a foreign function interface
. Currently, four external foreign function interfaces are supported by GTK-server: libFFI, FFCALL, C/Invoke and DynCall. One of these libraries should be available on the target system, in order to compile the GTK-server successfully.
#!/bin/ksh
gtk-server -stdin |&
function gtk { print -p $1; read -p GTK; }
function define { $2 "$3"; eval $1="$GTK"; }
gtk "gtk_init NULL NULL"
define WINDOW gtk "gtk_window_new 0"
gtk "gtk_window_set_title $WINDOW 'Korn GTK-server demo'"
gtk "gtk_window_set_default_size $WINDOW 400 200"
define TABLE gtk "gtk_table_new 10 10 1"
gtk "gtk_container_add $WINDOW $TABLE"
define BUTTON gtk "gtk_button_new_with_label 'Click to Quit'"
gtk "gtk_table_attach_defaults $TABLE $BUTTON 5 9 5 9"
gtk "gtk_widget_show_all $WINDOW"
until $EVENT = $WINDOW
do
define EVENT gtk "gtk_server_callback wait"
done
gtk "gtk_server_exit"
related libraries, Poppler
, Mozilla
, but also libc and a music library like MikMod.
When using the GTK-server as a standalone binary, it inevitably creates an additional process in the processlist. Also, GTK functions defined as a macro cannot be reached by a client program.
Open source
The term open source describes practices in production and development that promote access to the end product's source materials. Some consider open source a philosophy, others consider it a pragmatic methodology...
project released under the GNU General Public License
GNU General Public License
The GNU General Public License is the most widely used free software license, originally written by Richard Stallman for the GNU Project....
. The GTK-server project aims to bring Graphical User Interface
Graphical user interface
In computing, a graphical user interface is a type of user interface that allows users to interact with electronic devices with images rather than text commands. GUIs can be used in computers, hand-held devices such as MP3 players, portable media players or gaming devices, household appliances and...
programming to any interpreted language
Interpreted language
Interpreted language is a programming language in which programs are 'indirectly' executed by an interpreter program. This can be contrasted with a compiled language which is converted into machine code and then 'directly' executed by the host CPU...
using the GIMP Tool Kit (GTK) or XForms (toolkit)
XForms (toolkit)
XForms is a GUI toolkit based on Xlib for the X Window System. It features a rich set of objects, such as buttons, scrollbars, and menus etc. In addition, the library is extensible and new objects can easily be created and added to the library....
.
Philosophy
The GTK-server provides a stream-oriented interface to GTK. If the GTK-server is compiled as a standalone binary, it allows five different interfaces: stdin, fifo (named pipeNamed pipe
In computing, a named pipe is an extension to the traditional pipe concept on Unix and Unix-like systems, and is one of the methods of inter-process communication. The concept is also found in Microsoft Windows, although the semantics differ substantially...
), ipc (message queue
Message queue
In computer science, message queues and mailboxes are software-engineering components used for interprocess communication, or for inter-thread communication within the same process. They use a queue for messaging – the passing of control or of content...
), tcp or udp. Any interpreted language or shellscript with I/O capabilities can start the GTK-server with an argument specifying the type of interface, and can start sending GTK function calls in S-Expression
S-expression
S-expressions or sexps are list-based data structures that represent semi-structured data. An S-expression may be a nested list of smaller S-expressions. S-expressions are probably best known for their use in the Lisp family of programming languages...
format. After each request, the GTK-server returns a result, depending on the type of GTK function invoked.
If the GTK-server is compiled as a shared object, it exports the function 'gtk', which must be imported in the client program first. After that, the client program can start sending GTK function calls in S-Expression format as argument to the imported 'gtk' function.
Before the GTK-server actually can execute GTK functions, it has to read a configuration file in which the prototypes of the GTK functions are described. Since version 2.2.3 this also can be done on-the-fly, allowing the GTK-server to run without configuration file.
Implementation
Implementing the GTK-server leads to the following considerations.1) Accessing foreign functions is only possible when the accessed libraries are created with a non object oriented programming language like C or Pascal. Libraries created with C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
for example, use name mangling
Name mangling
In compiler construction, name mangling is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages....
in order to unify overloaded functions. This means that the actual functionname in a C++ library cannot be known once the shared library has been compiled. Hence the functions in such a library cannot be accessed. Therefore, libraries like wxWidgets
WxWidgets
wxWidgets is a widget toolkit for creating graphical user interfaces for cross-platform applications. wxWidgets enables a program's GUI code to compile and run on several computer platforms with minimal or no code changes...
, the Qt toolkit, FLTK
FLTK
FLTK is a cross-platform GUI library developed by Bill Spitzak and others. Made with 3D graphics programming in mind, it has an interface to OpenGL, but it is also suitable for general GUI programming....
which are programmed in C++, cannot be accessed with the GTK-server concept. More on this matter is explained here.
2) The GTK library was implemented in the C programming language. Since C is a strongly typed programming language, the interpreted program needs to know the type of arguments and the type of the return value for each GTK function during runtime. These can be defined on-the-fly or in a configuration file, which is parsed by the GTK-server during startup. However, the GTK-server does not know the actual functions which are going to be used by the interpreted client program, so for GTK-server all arguments and return values for each GTK function are variable types.
This leads to a problem for the implementation, because the GTK functions and the corresponding arguments and return values cannot be hardcoded into the GTK-server binary.
The way to resolve this is by using a foreign function interface
Foreign function interface
A foreign function interface is a mechanism by which a program written in one programming language can call routines or make use of services written in another. The term comes from the specification for Common Lisp, which explicitly refers to the language features for inter-language calls as...
. Currently, four external foreign function interfaces are supported by GTK-server: libFFI, FFCALL, C/Invoke and DynCall. One of these libraries should be available on the target system, in order to compile the GTK-server successfully.
Example
The following Kornshell script starts the GTK-server in stdin mode, and creates a simple window with an exit button:- Start GTK-server
gtk-server -stdin |&
- Communicate with GTK-server and assign function
function gtk { print -p $1; read -p GTK; }
function define { $2 "$3"; eval $1="$GTK"; }
- Setup GUI
gtk "gtk_init NULL NULL"
define WINDOW gtk "gtk_window_new 0"
gtk "gtk_window_set_title $WINDOW 'Korn GTK-server demo'"
gtk "gtk_window_set_default_size $WINDOW 400 200"
define TABLE gtk "gtk_table_new 10 10 1"
gtk "gtk_container_add $WINDOW $TABLE"
define BUTTON gtk "gtk_button_new_with_label 'Click to Quit'"
gtk "gtk_table_attach_defaults $TABLE $BUTTON 5 9 5 9"
gtk "gtk_widget_show_all $WINDOW"
- Mainloop
until $EVENT = $WINDOW
do
define EVENT gtk "gtk_server_callback wait"
done
- Exit GTK-server
gtk "gtk_server_exit"
Advantages and limitations
Although GTK was meant to be used with the C programming language, it is now possible to use GTK from any interpreted language without changing the actual implementation of the interpreter. Also both GTK 1.x and GTK 2.x can be reached. Optionally, any other shared library can be used, like OpenGLOpenGL
OpenGL is a standard specification defining a cross-language, cross-platform API for writing applications that produce 2D and 3D computer graphics. The interface consists of over 250 different function calls which can be used to draw complex three-dimensional scenes from simple primitives. OpenGL...
related libraries, Poppler
Poppler
Poppler can refer to:*"Popplers", a fictional fast food featured in the The Problem with Popplers episode of the television series Futurama...
, Mozilla
Mozilla
Mozilla is a term used in a number of ways in relation to the Mozilla.org project and the Mozilla Foundation, their defunct commercial predecessor Netscape Communications Corporation, and their related application software....
, but also libc and a music library like MikMod.
When using the GTK-server as a standalone binary, it inevitably creates an additional process in the processlist. Also, GTK functions defined as a macro cannot be reached by a client program.