GTK-server is an
open-source
Open source is source code that is made freely available for possible modification and redistribution. Products include permission to use the source code, design documents, or content of the product. The open-source model is a decentralized sof ...
project released under the
GNU General Public License
The GNU General Public License (GNU GPL or simply GPL) is a series of widely used free software licenses that guarantee end user
In product development, an end user (sometimes end-user) is a person who ultimately uses or is intended to ulti ...
. The GTK-server project aims to bring
Graphical User Interface
The GUI ( "UI" by itself is still usually pronounced . or ), graphical user interface, is a form of user interface that allows User (computing), users to Human–computer interaction, interact with electronic devices through graphical icon (comp ...
programming to any
interpreted language
In computer science, an interpreter is a computer program that directly executes instructions written in a programming or scripting language, without requiring them previously to have been compiled into a machine language program. An interpre ...
using the GIMP Tool Kit (
GTK
GTK (formerly GIMP ToolKit and GTK+) is a free and open-source cross-platform widget toolkit for creating graphical user interfaces (GUIs). It is licensed under the terms of the GNU Lesser General Public License, allowing both free and prop ...
) or
XForms
XForms is an XML format used for collecting inputs from web forms. XForms was designed to be the next generation of HTML / XHTML forms, but is generic enough that it can also be used in a standalone manner or with presentation languages other th ...
.
Philosophy
The GTK-server provides a stream-oriented interface to
GTK
GTK (formerly GIMP ToolKit and GTK+) is a free and open-source cross-platform widget toolkit for creating graphical user interfaces (GUIs). It is licensed under the terms of the GNU Lesser General Public License, allowing both free and prop ...
. If the GTK-server is compiled as a standalone binary, it allows five different interfaces: stdin, fifo (
named pipe
In computing, a named pipe (also known as a FIFO for its behavior) is an extension to the traditional pipe concept on Unix and Unix-like systems, and is one of the methods of inter-process communication (IPC). The concept is also found in OS/ ...
), ipc (
message queue
In computer science, message queues and mailboxes are software-engineering components typically used for inter-process communication (IPC), or for inter-thread communication within the same process. They use a queue for messaging – th ...
), 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
In computer programming, an S-expression (or symbolic expression, abbreviated as sexpr or sexp) is an expression in a like-named notation for nested list (tree-structured) data. S-expressions were invented for and popularized by the programming la ...
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
In computer science, a library is a collection of non-volatile resources used by computer programs, often for software development. These may include configuration data, documentation, help data, message templates, pre-written code and subro ...
, 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.
* Accessing foreign functions is only possible when the accessed libraries are created with a non
object oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of " objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
language like C or Pascal. Libraries created with
C++ for example, use
name mangling
In compiler construction, name mangling (also called name decoration) is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages.
It provides a way of e ...
in order to unify
overloaded function
In some programming languages, function overloading or method overloading is the ability to create multiple functions of the same name with different implementations. Calls to an overloaded function will run a specific implementation of that f ...
s. 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 (formerly wxWindows) is a widget toolkit and tools library for creating graphical user interfaces (GUIs) for cross-platform applications. wxWidgets enables a program's GUI code to compile and run on several computer platforms with min ...
, the
Qt toolkit
Qt (pronounced "cute") is cross-platform software for creating graphical user interfaces as well as cross-platform applications that run on various software and hardware platforms such as Linux, Windows, macOS, Android or embedded systems wit ...
,
FLTK which are programmed in C++, cannot be accessed with the GTK-server concept.
* The GTK library was implemented in the
C programming language
''The C Programming Language'' (sometimes termed ''K&R'', after its authors' initials) is a computer programming book written by Brian Kernighan and Dennis Ritchie, the latter of whom originally designed and implemented the language, as well as ...
. Since C is a
strongly typed programming language
In computer programming, one of the many ways that programming languages are colloquially classified is whether the language's type system makes it strongly typed or weakly typed (loosely typed). However, there is no precise technical definitio ...
, 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
A foreign function interface (FFI) is a mechanism by which a program written in one programming language can call routines or make use of services written in another.
Naming
The term comes from the specification for Common Lisp, which explicit ...
. Currently, four external foreign function interfaces are supported by GTK-server:
libffi
libffi is a foreign function interface library. It provides a C programming language interface for calling natively compiled functions given information about the target function at run time instead of compile time. It also implements the opp ...
, 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
KornShell (ksh) is a Unix shell which was developed by David Korn at Bell Labs in the early 1980s and announced at USENIX on July 14, 1983. The initial development was based on Bourne shell source code. Other early contributors were Bell ...
script starts the GTK-server in stdin mode, and creates a simple window with an exit button:
#!/bin/ksh
# Start GTK-server
gtk-server -stdin , &
# Communicate with GTK-server and assign function
function gtk
function define
# 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
GTK (formerly GIMP ToolKit and GTK+) is a free and open-source cross-platform widget toolkit for creating graphical user interfaces (GUIs). It is licensed under the terms of the GNU Lesser General Public License, allowing both free and prop ...
was meant to be used with the
C programming language
''The C Programming Language'' (sometimes termed ''K&R'', after its authors' initials) is a computer programming book written by Brian Kernighan and Dennis Ritchie, the latter of whom originally designed and implemented the language, as well as ...
, it is now possible to use GTK from any interpreted language without changing the actual implementation of the interpreter. Also, GTK 1.x, GTK2.x and GTK 3.x can be reached. Optionally, any other shared library can be used, like
OpenGL
OpenGL (Open Graphics Library) is a cross-language, cross-platform application programming interface (API) for rendering 2D and 3D vector graphics. The API is typically used to interact with a graphics processing unit (GPU), to achieve ha ...
related libraries,
Poppler,
Mozilla
Mozilla (stylized as moz://a) is a free software community founded in 1998 by members of Netscape. The Mozilla community uses, develops, spreads and supports Mozilla products, thereby promoting exclusively free software and open standards, w ...
, but also
libc
The C standard library or libc is the standard library for the C programming language, as specified in the ISO C standard. ISO/ IEC (2018). '' ISO/IEC 9899:2018(E): Programming Languages - C §7'' Starting from the original ANSI C standard, it wa ...
,
sqlite
SQLite (, ) is a database engine written in the C programming language. It is not a standalone app; rather, it is a library that software developers embed in their apps. As such, it belongs to the family of embedded databases. It is the mo ...
and a music library like
MikMod
A music tracker (sometimes referred to as just tracker for short) is a type of music sequencer software for creating music. The music is represented as discrete musical notes positioned in several channels at discrete chronological positions on a ...
.
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.
References
{{Reflist, refs=
[{{citation
, url = https://tldp.org/HOWTO/C++-dlopen/index.html
, title = C++ dlopen mini HOWTO
, first = Aaron
, last = Isotton
, work = The ]Linux Documentation Project
The Linux Documentation Project (LDP) is a dormant an all-volunteer project that maintains a large collection of GNU and Linux-related documentation and publishes the collection online. It began as a way for hackers to share their documentation wit ...
, date = 2006-03-16
, access-date = 2021-09-07
, archive-url = https://web.archive.org/web/20210812142103/https://tldp.org/HOWTO/C++-dlopen/index.html
, archive-date = 2021-08-12
, url-status = live
[{{citation
, url = https://www.haible.de/bruno/packages-ffcall.html
, title = FFCALL Libraries
, access-date = 2021-09-07
, archive-url = https://web.archive.org/web/20210701220204/https://www.haible.de/bruno/packages-ffcall.html
, archive-date = 2021-07-01
, url-status = live]
[{{citation
, url = http://www.nongnu.org/cinvoke/
, title = C/Invoke
, access-date = 2021-09-07
, archive-url = https://web.archive.org/web/20210820012035/http://www.nongnu.org/cinvoke/
, archive-date = 2021-08-20
, url-status = live]
[{{citation
, url = https://dyncall.org/
, title = dyncall.org - calling C functions dynamically
, access-date = 2021-09-07
, archive-url = https://web.archive.org/web/20210416121417/https://dyncall.org/
, archive-date = 2021-04-16
, url-status = live]
GTK
Application programming interfaces
X-based libraries