487

Both Windows (Win32 API) and OS X (Cocoa) have their own APIs to handle windows, events and other OS stuff. I have never really got a clear answer as to what Linux’s equivalent is?

I have heard some people say GTK+, but GTK+ being cross platform. How can it be native?

Jonas
  • 121,568
  • 97
  • 310
  • 388
DavidColson
  • 8,387
  • 9
  • 37
  • 50
  • 49
    Joni is correct: "In Linux the graphical user interface is not a part of the operating system... X Window System defines a network protocol for [GUI capabilities]. Toolkit libraries such as Gtk+ (used by Gnome) and QT (used by KDE), built on top of Xlib." – paulsm4 Oct 03 '12 at 21:43
  • 36
    on the contrary, David, this is an excellent inquiry – amphibient Oct 03 '12 at 21:51
  • 8
    I agree - I've read "The Linux Programming API" and "Systems Programming... Unix" which are the two bibles on the kernel & API, and this question never occurred to me :) The answers are interesting. – John Humphreys Oct 03 '12 at 22:02
  • 11
    The Win32 API is also cross-platform if you include Wine.. – Brendan Long Oct 04 '12 at 00:06
  • 2
    BTW Windows has WIN32/WIN64 subsystems which implement a lot of the same for the 'Windows Look and Feel - aka WIN32' amongst other things. These subsystems operate above the NT kernel. The APIs for these are different. For example C: is a WIN32 abstraction, not an NT kernel thing. Same for some of the windowing functionality. Windows also had Unix subsystems that are available (that support X11 to some degree). – Preet Sangha Oct 09 '12 at 21:45
  • So in effect Windows does this in a similar (possibly more complete way), as Linux (see @joni's answer) except that we just call the package Windows and tend to forget about the separation. However WIN32 is the one we tend to use so it's 'the defacto API. – Preet Sangha Oct 09 '12 at 21:50
  • Can someone please s/Linux/Unix to generalize this? – Good Person Nov 09 '12 at 01:15
  • @BrendanLong Actually that does not make it "crossplatform", since wine reimplemented windows API for use in linux. – Tomas Pruzina Apr 17 '13 at 19:19
  • since in windows GUI is part of the OS and in linux it's not the case, shouldn't that make using GUI faster is windows than in linux? at least theoretically if not in practice – samsamara Mar 07 '16 at 05:13

11 Answers11

650

In Linux the graphical user interface is not a part of the operating system. The graphical user interface found on most Linux desktops is provided by software called the X Window System, which defines a device independent way of dealing with screens, keyboards and pointer devices.

X Window defines a network protocol for communication, and any program that knows how to "speak" this protocol can use it. There is a C library called Xlib that makes it easier to use this protocol, so Xlib is kind of the native GUI API. Xlib is not the only way to access an X Window server; there is also XCB.

Toolkit libraries such as GTK+ (used by GNOME) and Qt (used by KDE), built on top of Xlib, are used because they are easier to program with. For example they give you a consistent look and feel across applications, make it easier to use drag-and-drop, provide components standard to a modern desktop environment, and so on.

How X draws on the screen internally depends on the implementation. X.org has a device independent part and a device dependent part. The former manages screen resources such as windows, while the latter communicates with the graphics card driver, usually a kernel module. The communication may happen over direct memory access or through system calls to the kernel. The driver translates the commands into a form that the hardware on the card understands.

As of 2013, a new window system called Wayland is starting to become usable, and many distributions have said they will at some point migrate to it, though there is still no clear schedule. This system is based on OpenGL/ES API, which means that in the future OpenGL will be the "native GUI API" in Linux. Work is being done to port GTK+ and QT to Wayland, so that current popular applications and desktop systems would need minimal changes. The applications that cannot be ported will be supported through an X11 server, much like OS X supports X11 apps through Xquartz. The GTK+ port is expected to be finished within a year, while Qt 5 already has complete Wayland support.

To further complicate matters, Ubuntu has announced they are developing a new system called Mir because of problems they perceive with Wayland. This window system is also based on the OpenGL/ES API.

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
Joni
  • 108,737
  • 14
  • 143
  • 193
  • 54
    Even Windows was originally just a program that ran on top of DOS. – dan04 Oct 04 '12 at 03:54
  • 3
    +1 @DavidHammen I'm not sure if that was directed at me or not, but you're 100% correct. My terminology was not exactly accurate. What I _should_ have said was that it's not a full operating system in the way you might commonly think of a OS with all of the additional components that make it easy to use, but rather a conglomeration of components from different vendors, etc. – Chris Thompson Oct 04 '12 at 16:36
  • 3
    @ChrisThompson - hmm... maybe I'm too old but I've never thought of a GUI as a core part of an OS. And I've never thought of an OS without a GUI as simply being "a conglomeration of components from different vendors, etc." But hey maybe things have moved on since I formed my opinion of what constituted an OS :) – George Hawkins Oct 04 '12 at 16:46
  • 1
    @GeorgeHawkins I'm not doing a good job of explaining. A GUI is definitely not a core part of an OS, and frankly neither is a UI. When you look at Ubuntu Linux, most people typically think that entire software image as the OS including every feature that is present at installation. So that includes GNOME, the Linux kernel, bash, an X11 server, various software libraries (libc, pthread, etc), etc etc etc which can all come from different vendors. I'm not sure if that makes sense, but that's the idea I'm trying to get at. – Chris Thompson Oct 04 '12 at 17:44
  • Not in the context of this question, anyway. But you'd be surprised at how many people believe that to be the case... – BoltClock Oct 06 '12 at 06:14
  • 1
    +1 for the level of detail that you went into while keeping it easy for the less experienced to understand. – AndrewJacksonZA Oct 09 '12 at 12:37
  • @Joni One remark: X-Window is the historical standard display protocol. New display protocol, Wayland, is already there and could be its successor. Read more at en.wikipedia.org/wiki/Wayland_%28display_server_protocol%29 – Pascal Oct 10 '12 at 12:30
  • 1
    +1 - have a look at http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/x11-wm.html, quote: _A “desktop environment” can mean anything ranging from a simple window manager to a complete suite of desktop applications, such as KDE or GNOME._ In many cases we could probably simplify that to _something which is a window manager using Xclients_ (see http://en.wikipedia.org/w/index.php?title=File:X_client_sever_example.svg&page=1). Also see this diagram describing where Xwindows sits in the OS http://www.csit.parkland.edu/~smauney/csc128/x-windows_diagram.html – Aaron Newton Oct 11 '12 at 14:02
  • Are you sure X windows is using system calls to talk to driver that talks for GPU? I have a seriously strong feeling that X server communicates with a graphics card directly through DMA buffers mapped into user space. –  Oct 19 '12 at 18:11
  • @VladLazarenko So you think there are no graphics device drivers? I suggest that you read http://blog.mecheye.net/2012/06/the-linux-graphics-stack/ – Jim Balter Nov 16 '12 at 04:19
  • @JimBalter: Did I say there are no drivers? I said that X server is most likely communicating through user-mapped DMA. –  Nov 16 '12 at 04:55
  • 1
    @VladLazarenko You questioned whether X talks to the driver rather than directly to the graphics card. If it only did the latter, there would no need for nvidia and ATI drivers. DMA is for data, not control. – Jim Balter Nov 16 '12 at 05:45
  • since in windows GUI is part of the OS and in linux it's not the case, shouldn't that make using GUI faster is windows than in linux? at least theoretically if not in practice – samsamara Mar 07 '16 at 05:13
  • "To meet the needs of today's users Mir avoids the X11 protocol and uses Wayland for a more modern and secure approach to graphics." ... "Mir uses Wayland as the primary protocol for connecting to applications." https://mir-server.io/ – Honest Abe Aug 06 '19 at 17:37
  • Mir is a "Wayland compositor" like "Mutter". GNOME, and therefore ubuntu, uses Mutter. https://en.wikipedia.org/wiki/Mir_(software) – Honest Abe Aug 06 '19 at 17:47
  • Yes @HonestAbe the plans for Mir as a competitor/replacement for Wayland have changed since 2013, you're welcome to update the answer for 2019 if you're familiar with the current status of the display servers. – Joni Aug 07 '19 at 14:59
  • 2
    Unless an answer is a community wiki, I won't change any content, aside from improving grammar / formatting or adding links. If content needs to be corrected or updated, that's the responsibility of the person whose reputation is bound to the content. I will however do them the favor of commenting, so that they become aware that _I think_ something needs to be corrected or updated. – Honest Abe Aug 07 '19 at 19:13
92

Linux is a kernel, not a full operating system. There are different windowing systems and gui's that run on top of Linux to provide windowing. Typically X11 is the windowing system used by Linux distros.

Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
  • question: what do KDE Plasma and GNOME graphical environment relate to X11? are they in the same level, i.e. alternatives or are they built on top of X11? thanks – amphibient Oct 03 '12 at 21:31
  • 8
    They are built on top of it. X11 is the server, while KDE, Gnome etc. are desktop managers, providing higher level APIs, common window layouts etc. – Mark Oct 03 '12 at 21:35
  • 8
    X11 is the actual low level layer that handles all of this stuff and handles drawing to screen etc (and communicates with graphics drivers). Gnome/KDE then control X11. That's about it at a high level and ignoring a billion and one details. – slugonamission Oct 03 '12 at 21:36
  • 3
    thanks. one more question: wikipedia explains that X11 "creates a hardware abstraction layer where software is written to use a generalized set of commands, allowing for device independence and reuse of programs on any computer that implements X." my question is: why does X11 provide hardware interaction (HAL), should the Linux Kernel provide that and X11 operate on top of the Kernel? In other words, why is X11 reaching to hardware devices when the Kernel should be its proxy? – amphibient Oct 03 '12 at 21:45
  • 1
    I _think_ in this case `HAL` is an incorrectly used term. Really what they're saying is X11 provides a system-independent means to display windows. There are versions of X11 for Darwin (Mac OS - that's actually more complicated now with Mountain Lion...), MS Windows and Linux. – Chris Thompson Oct 03 '12 at 22:09
  • so in this case, X11 would not really be a HAL but more like something like the JVM (OS abstraction layer), correct? – amphibient Oct 03 '12 at 22:11
  • @foampile sort of if you want to take a really, really, really high-level view of what the JVM is and categorize it by the functionality it provides rather than how it provides it. I don't mean to be pedantic but X11 and the JVM are two very different things. X11 is closer to something like Qt where you have a standard set of function calls and libraries but your code must be compiled specifically for each system you want to run it on. – Chris Thompson Oct 03 '12 at 22:12
  • that is what i meant -- an analogy rather than a side-by-side comparison – amphibient Oct 03 '12 at 22:15
  • @foampile then yes, I'd say that's a reasonable way to look at it! – Chris Thompson Oct 03 '12 at 22:17
  • 4
    X11 is a network protocol. Abstraction involves writing programs on platform A that can display X11 clients (windows, apps, etc) on platform B over some networking equipment. Your app can run locally, remotely, in a nested server, in a virtual server that just does VNC, etc. Hardware abstraction (on the local platform) is a side-effect of this feature, but it also blurs the line between X11 and the kernel (which is what does hardware abstraction on an average Unix system). – Alexios Oct 04 '12 at 00:23
  • @foampile HAL *is* the correct term, and it doesn't mean "hardware interaction", it means "hardware **abstraction**" -- an abstract view of graphics and user interface hardware. Neither X11 clients nor X11 servers need run on Linux, so Linux can't possibly do what X11 does. Linux itself provides an abstraction layer, to hardware and other services, and so does a JVM (which is an abstraction of a machine, not just an OS). – Jim Balter Nov 16 '12 at 04:24
  • " a really, really, really high-level view of what the JVM is and categorize it by the functionality it provides rather than how it provides it" -- The JVM has a formal specification that details the functionality that it provides and is agnostic about how it is provided. – Jim Balter Nov 16 '12 at 04:30
  • @JimBalter The JVM specification is indeed a specification. "The JVM" is an implementation of the JVM specification. ("The JVM" usually means either the Oracle JVM, or whichever JVM happens to be installed on your computer) – user253751 Oct 06 '16 at 05:04
64

Wayland is also worth mentioning as it is mostly referred as a "future X11 killer".

Also note that Android and some other mobile operating systems don't include X11 although they have a Linux kernel, so in that sense X11 is not native to all Linux systems.

Being cross-platform has nothing to do with being native. Cocoa has also been ported to other platforms via GNUStep but it is still native to OS X / macOS.

cubuspl42
  • 7,833
  • 4
  • 41
  • 65
none
  • 11,793
  • 9
  • 51
  • 87
  • 2
    The high level architecture diagram for Wayland is the same as that of X, except that the "Compositor" and "Server" in the X diagram are merged in the Wayland diagram. What's lacking is a plausible explanation why this integration couldn't be worked into X. – Kaz Oct 04 '12 at 00:03
  • 1
    well, I would say it's not easy changing the fundamental design of a 20+ years old code base. Also note that another problem with X11 is that most of it's functionality such as font rendering is not being used today. Plus X11 will still be needed in servers and supercomputers, network transparency is just too good to be given up. – none Oct 04 '12 at 07:23
  • 4
    @gokcehan wayland doesn't omit network transparency, it's a myth. It's not implemented _yet_, just as many other things in Wayland. http://wayland.freedesktop.org/faq.html#heading_toc_j_8 http://blog.martin-graesslin.com/blog/2011/08/thoughts-about-network-trancparency/ etc. – Catherine Oct 04 '12 at 09:07
  • 10
    +! for mentioning Android's lack of X11. Android is probably the most convincing counter-example to the argument that "x11 is the native linux GUI". Demonstrating that Linux can have x11 or Android as it's "native" GUI shows beginners that Linux is really just a kernel and doesn't always have GUI. – Trevor Boyd Smith Oct 04 '12 at 13:37
  • "Being cross-platform has nothing to do with being native" is an excellent point. I've used that pattern very frequently over my years of porting cross-platform. Most notably, in the 90's, I sold a framework which let you cross-compile code written to the Metrowerks PowerPlant framework using MFC under the hood. (Before anyone sneers, once you get below the macros, MFC is quite reasonable.) I've also had multiple porting jobs where either the Win32 or the Mac Toolbox API was the spec against which we compiled, reimplemented as necessary on the other platforms. – Andy Dent Aug 18 '15 at 08:44
  • @whitequark your link directly contradicts your claim. _Is Wayland network transparent / does it support remote rendering? No, that is outside the scope of Wayland._ – Ruslan Sep 29 '17 at 14:52
  • @Ruslan Perhaps it should have been phrased as "Wayland does not preclude network transparency." – Catherine Sep 29 '17 at 18:59
26

Strictly speaking, the API of Linux consists of its system calls. These are all of the kernel functions that can be called by a user-mode (non-kernel) program. This is a very low-level interface that allows programs to do things like open and read files. See http://en.wikipedia.org/wiki/System_call for a general introduction.

A real Linux system will also have an entire "stack" of other software running on it, in order to provide a graphical user interface and other features. Each element of this stack will offer its own API.

nibot
  • 14,428
  • 8
  • 54
  • 58
  • 5
    This answer was written when the question was "What is linux's native API?" (since then, "GUI" has been inserted into the question). – nibot Oct 04 '12 at 10:54
  • 1
    It's *still* the right answer to what is now an incoherent question because none of the systems that sit on top of Linux, including GNU and X11, are "native" in any meaningful sense. – Jim Balter Nov 16 '12 at 04:37
21

To aid in what has already been mentioned there is a very good overview of the Linux graphics stack at this blog: http://blog.mecheye.net/2012/06/the-linux-graphics-stack/

This explains X11/Wayland etc and how it all fits together. In addition to what has already been mentioned I think it's worth adding a bit about the following API's you can use for graphics in Linux:

Mesa - "Mesa is many things, but one of the major things it provides that it is most famous for is its OpenGL implementation. It is an open-source implementation of the OpenGL API."

Cairo - "cairo is a drawing library used either by applications like Firefox directly, or through libraries like GTK+, to draw vector shapes."

DRM (Direct Rendering Manager) - I understand this the least but its basically the kernel drivers that let you write graphics directly to framebuffer without going through X

Matt
  • 231
  • 1
  • 2
11

I suppose the question is more like "What is linux's native GUI API".

In most cases X (aka X11) will be used for that: http://en.wikipedia.org/wiki/X_Window_System.

You can find the API documentation here

user
  • 6,897
  • 8
  • 43
  • 79
Mark
  • 6,033
  • 1
  • 19
  • 14
6

XWindows is probably the closest to what could be called 'native' :)

Sergey Sirotkin
  • 1,668
  • 11
  • 7
  • 3
    is that the same thing as x11? – DavidColson Oct 03 '12 at 21:28
  • 2
    But if you use it directly, you will see it's not quite a GUI api... Linux just doesn't have 1 native GUI api. But nearly all those GUI APIs (at least those common on desktop linux today) run on top of X. If you start writing an application using plain old X today, it will look totally out of place on any modern distro, so I'd say the native GUI is distribution dependent - gtk/gnome for ubuntu, kde for others. – Axel Oct 05 '12 at 05:44
1

The linux kernel graphical operations are in /include/linux/fb.h as struct fb_ops. Eventually this is what add-ons like X11, Wayland, or DRM appear to reference. As these operations are only for video cards, not vector or raster hardcopy or tty oriented terminal devices, their usefulness as a GUI is limited; it's just not entirely true you need those add-ons to get graphical output if you don't mind using some assembler to bypass syscall as necessary.

M. Ziegast
  • 165
  • 4
1

Wayland

As you might hear, wayland is the featured choice of many distros these days, because of its protocol is simpler than the X.

Toolkits of wayland

Toolkits or gui libraries that wayland suggests are:

  • QT 5
  • GTK+
  • LSD
  • Clutter
  • EFL
Amir Fo
  • 5,163
  • 1
  • 43
  • 51
0

The closest thing to Win32 in linux would be the libc, as you mention not only the UI but events and "other os stuff"

Midhat
  • 17,454
  • 22
  • 87
  • 114
  • 5
    libc (the C standard library) is an interface to the OS system calls. It's the latter (OS system calls) on Linux that roughly translates to Win32 on Windows. You can't really (or at all?) program for current versions of Windows without at some level using the Win32 API, but you can program for both Windows and Linux without using the respective compiler's standard C library, simply by duplicating the direct-to-syscalls code. It isn't recommended, and it makes your code completely nonportable to other OSes, but it's possible to do. – user Oct 04 '12 at 09:21
  • UI isn't part of libc - as long as you don't think of printf/scanf and the like as being a UI... libc is just the C standard library. And it's not really an interface to the OS system calls either. It provides functions for file i/o, memory management, string processing etc. and internally uses system calls. For making system calls like `ioctl` yourself, you'd have to include something from `sys/`. – Axel Oct 05 '12 at 05:35
  • The question didn't always specify "GUI". – jv110 Jun 24 '18 at 22:07
  • 1
    Maybe you talk about `Xlib` not `libc`. – i486 May 15 '20 at 13:43
0

GUI is a high level abstraction of capability, so almost everything from XOrg server to OpenGL is ported cross-platform, including for Windows platform. But if by GUI API you mean *nix graphics API then you might be wandering around "Direct Rendering Infrastructure".

Chawathe Vipul S
  • 1,636
  • 15
  • 28