问题
If I wanted to write a C program to talk to an X server on Linux and produce the simplest possible visual result, e.g. setting a single pixel on the display, how would I do it?
I want to use as few libraries as possible, to understand it from the ground up, no matter how inefficient/messy the code is.
Update - by "without a library", I mean basically without any of the helper libraries that are available for X, and without a graphics toolkit.
Update - the answer is "through a unix socket", specifically /tmp/.X11-unix/X0 (on this machine at least, presumably for display 0). Actually drawing a pixel is too complicated to get into in an answer here I think.
回答1:
If you want to use no library at all, you need to do these steps:
- Learn assembly for the platform you target
- Learn how to make a binary without using the libc on your target. This likely involves writing assembly for setting up the initial stack frame
- Learn how to do system calls without using the libc on your target. This likely involves assembly.
- Learn and understand network programming and how the POSIX socket API works
- Learn how to open network connections without using the libc on your target
- Learn and understand the X11 protocol
- Implement the subset of the X11 protocol you need
- Open a network connection to the X server
- Tell the X server to make a window
- Negotiate a colour space with the X server
- Tell the X server to colour a single pixel in the window
If you want to use the libc and the libsocket, then only the following steps (roughly) remain:
- Learn and understand network programming and how the POSIX socket API works
- Learn and understand the X11 protocol
- Implement the subset of the X11 protocol you need
- Open a network connection to the X server
- Tell the X server to make a window
- Negotiate a colour space with the X server
- Tell the X server to colour a single pixel in the window
回答2:
X11 is getting old. Consider Wayland and read its FAQ. And there are several X related protocols, read about X11 Core protocol, and also conventions like EWMH.
You'll need to read several thousands of pages. Just understanding the protocols would require several months. OReilly published (in the previous century) a serie of 8 to 10 books related to X11.
You might use a low level X11 library, like XCB -or the older Xlib -, it is used by graphical toolkits like Qt or GTK (or FOX, etc..), which are both free software toolkits so you could study their source code (and Xlib & XCB are also free software).
Notice that today's GUI are usually not displaying fonts using X11 text display requests. They often use Xft (and the font is on the client side, not in the Xorg server). Actually, I heard that most of the graphics rendering practically happens on the client side, and that today most X11 requests are just sending pixmaps to the server (so X11 core protocol requests for drawing lines or circles are barely used today). More generally, the trend in major X11 based toolkits like Qt or GTK is to avoid using the server-side drawing abilities of X11 (e.g. Xlib's XDrawLine or XDrawText), because the toolkit is drawing a pixmap image client side and sending it to the server.
You could consider using a low-level library like libSDL
The important thing to understand is that X11 applications are event driven and based upon an event loop (generally provided by the toolkit) above some multiplexing syscall like poll(2). They are asked by X11 expose or damage events to redraw some screen area (and, of course, keyboard, mouse, and the Xorg server itself are sending events).
See also this answer to a similar question.
来源:https://stackoverflow.com/questions/33845447/how-do-i-talk-to-an-x-server-in-c-without-a-graphics-library