Implementing the X Server

Programming, for all ages and all languages.
User avatar
PavelChekov
Member
Member
Posts: 113
Joined: Mon Sep 21, 2020 9:51 am
Location: Aboard the Enterprise

Implementing the X Server

Post by PavelChekov »

Does anyone here know of any tutorial/good resources for implementing the X server? Has anyone here done so?

Thanks
USS Enterprise NCC-1701,
The Final Frontier,
Space,
The Universe

Live Long And Prosper

Slava Ukraini!
Слава Україні!
nullplan
Member
Member
Posts: 1769
Joined: Wed Aug 30, 2017 8:24 am

Re: Implementing the X Server

Post by nullplan »

Still asking for tutorials? You're not going to be able to do better than the documentation: https://x.org/releases/X11R7.7/doc/xpro ... tocol.html

That's the network protocol. How you turn that into pixels on a screen is up to you. Actually, it is just the core protocol. You might need to implement quite a few extensions also (e.g. you will need Xrender to support many applications using freetype), but the core protocol should be a good starting place.
Carpe diem!
User avatar
PavelChekov
Member
Member
Posts: 113
Joined: Mon Sep 21, 2020 9:51 am
Location: Aboard the Enterprise

Re: Implementing the X Server

Post by PavelChekov »

nullplan wrote:Still asking for tutorials?
I did extensive research and couldn't find anything, so I just asked in case I missed something.
USS Enterprise NCC-1701,
The Final Frontier,
Space,
The Universe

Live Long And Prosper

Slava Ukraini!
Слава Україні!
User avatar
iansjack
Member
Member
Posts: 4688
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Implementing the X Server

Post by iansjack »

Are you looking to implement it from scratch rather than porting it? In the latter case the source code is freely available - in the former you surely wouldn't want to follow a tutorial.

Presumably you have already implemented a TCP/IP stack?
User avatar
PavelChekov
Member
Member
Posts: 113
Joined: Mon Sep 21, 2020 9:51 am
Location: Aboard the Enterprise

Re: Implementing the X Server

Post by PavelChekov »

iansjack wrote:Are you looking to implement it from scratch rather than porting it? In the latter case the source code is freely available - in the former you surely wouldn't want to follow a tutorial.

Presumably you have already implemented a TCP/IP stack?
I am looking to implement it from scratch, as a sort of learning excercise. My understanding was that the OS provided a network stack.

I am more interested in how X actually goes from XDrawLine to an actual line of pixels on screen.
USS Enterprise NCC-1701,
The Final Frontier,
Space,
The Universe

Live Long And Prosper

Slava Ukraini!
Слава Україні!
nullplan
Member
Member
Posts: 1769
Joined: Wed Aug 30, 2017 8:24 am

Re: Implementing the X Server

Post by nullplan »

PavelCheckov wrote:I did extensive research and couldn't find anything, so I just asked in case I missed something.
You misunderstand. I wasn't asking if you had found any, I was asking why you with your multiple years of OS dev experience had not yet moved on to reading documentation instead of looking for someone else who read it, and then transcribed their understanding into an incomplete and possibly incorrect tutorial.
PavelCheckov wrote:I am more interested in how X actually goes from XDrawLine to an actual line of pixels on screen.
Bresenham's algorithm, but before you get there, you need a host of other things in place. For example, you need an idea of how to represent your clients in memory (on the frame buffer). X11 is a memory-conservative protocol, and allows the server to get by with remembering pretty much only coordinates of each window, but that is already a computationally interesting proposition. If a foreground window is moved, you need to tell the window that was moved where it was moved to, but you also need to tell all the windows behind it to update their contents. So how do you best determine which windows where behind it? Quadtrees may be a good answer here, but require a lot of thought to use well.

You know, thinking about it, Bresenham's algorithm really is the least of it. You have to deal with different data formats. Some applications get by on monochrome, some need full 32-bit framebuffers with alpha-blending. Some applications use ZPixmaps to render stuff, some use XYPixmaps, so you need to translate data unless you want to go crazy. And you have to provide events (and process reactions to those) in a timely manner. Some of those are more complicated than others.

If you are merely interested in rendering stuff, maybe something like XWayland is more up your alley? Wayland provides the frame buffer and events, XWayland merely translates from one to the other. And Wayland doesn't handle rendering, it tells lets the client do it and expects to be told to just update the window contents.
Carpe diem!
User avatar
PavelChekov
Member
Member
Posts: 113
Joined: Mon Sep 21, 2020 9:51 am
Location: Aboard the Enterprise

Re: Implementing the X Server

Post by PavelChekov »

nullplan wrote:You misunderstand. I wasn't asking if you had found any, I was asking why you with your multiple years of OS dev experience had not yet moved on to reading documentation instead of looking for someone else who read it, and then transcribed their understanding into an incomplete and possibly incorrect tutorial.
It would still save me quite a bit of time in understanding the basics.
nullplan wrote:Bresenham's algorithm
I figured that much out, I was more referring to actually displaying the pixel on the screen.
USS Enterprise NCC-1701,
The Final Frontier,
Space,
The Universe

Live Long And Prosper

Slava Ukraini!
Слава Україні!
thewrongchristian
Member
Member
Posts: 425
Joined: Tue Apr 03, 2018 2:44 am

Re: Implementing the X Server

Post by thewrongchristian »

A quick search for "windowing system tutorial" brings up:
Might be a good start.

And/or, you could compile up Xnest, and run it under a debugger on an existing desktop, and trace the code from network input to writing to the virtual (nested) screen.
davmac314
Member
Member
Posts: 121
Joined: Mon Jul 05, 2021 6:57 pm

Re: Implementing the X Server

Post by davmac314 »

PavelCheckov wrote:I figured that much out, I was more referring to actually displaying the pixel on the screen.
You probably should've started with that as your question; it can get frustrating when people ask broad questions like "how to implement an X server" because there is no simple answer and it is unclear at what level the question is aimed. (You have a bit of a history of asking such questions; I don't think you realise how frustrating it can be). If you ask the right question to begin with, it's better for everyone.

With that said, on Linux for instance this can be done via the framebuffer device (documentation is available, but not very comprehensive. I suggest searching for a examples).
User avatar
PavelChekov
Member
Member
Posts: 113
Joined: Mon Sep 21, 2020 9:51 am
Location: Aboard the Enterprise

Re: Implementing the X Server

Post by PavelChekov »

davmac314 wrote:
PavelCheckov wrote:I figured that much out, I was more referring to actually displaying the pixel on the screen.
You probably should've started with that as your question; it can get frustrating when people ask broad questions like "how to implement an X server" because there is no simple answer and it is unclear at what level the question is aimed. (You have a bit of a history of asking such questions; I don't think you realise how frustrating it can be). If you ask the right question to begin with, it's better for everyone.

With that said, on Linux for instance this can be done via the framebuffer device (documentation is available, but not very comprehensive. I suggest searching for a examples).
Thank you, I will keep that in mind when asking future questions. I apologize.
USS Enterprise NCC-1701,
The Final Frontier,
Space,
The Universe

Live Long And Prosper

Slava Ukraini!
Слава Україні!
Ringding
Posts: 10
Joined: Fri Nov 26, 2021 11:08 am

Re: Implementing the X Server

Post by Ringding »

I doubt that anyone was crazy enough to implement an X server during the last 3 decades, even large corporations. Everyone would just fork the Xorg code. I will likely be proven wrong quickly, but the point is that this would be a monstrous undertaking that you probably don’t want to get yourself into.
klange
Member
Member
Posts: 679
Joined: Wed Mar 30, 2011 12:31 am
Libera.chat IRC: klange
Discord: klange

Re: Implementing the X Server

Post by klange »

davmac314 wrote:You probably should've started with that as your question; it can get frustrating when people ask broad questions like "how to implement an X server" because there is no simple answer and it is unclear at what level the question is aimed. (You have a bit of a history of asking such questions; I don't think you realise how frustrating it can be). If you ask the right question to begin with, it's better for everyone.
Classic case of an x-y problem.
Ringding wrote:I doubt that anyone was crazy enough to implement an X server during the last 3 decades, even large corporations. Everyone would just fork the Xorg code. I will likely be proven wrong quickly, but the point is that this would be a monstrous undertaking that you probably don’t want to get yourself into.
The closest I can think of is that someone implemented an Xlib compatibility layer for Haiku. They skipped the "build an X server" part by translating Xlib calls to Haiku's graphics API.
User avatar
Voldemort
Posts: 12
Joined: Mon May 02, 2022 2:03 pm

Re: Implementing the X Server

Post by Voldemort »

Hi Pavel,

This seems like a well documented project which might help you in your quest.

https://github.com/ghaerr/microwindows
~Voldemort~
User avatar
PavelChekov
Member
Member
Posts: 113
Joined: Mon Sep 21, 2020 9:51 am
Location: Aboard the Enterprise

Re: Implementing the X Server

Post by PavelChekov »

Voldemort wrote:Hi Pavel,

This seems like a well documented project which might help you in your quest.

https://github.com/ghaerr/microwindows
Thanks! Also, I noticed people referring to me as "Pavel", which I just want to put on the record is my username based on Checkov from Star Trek, because some people on here have assumed I'm Russian and that I don't speak English, neither of which are true (Slava Ukraini!).
USS Enterprise NCC-1701,
The Final Frontier,
Space,
The Universe

Live Long And Prosper

Slava Ukraini!
Слава Україні!
klange
Member
Member
Posts: 679
Joined: Wed Mar 30, 2011 12:31 am
Libera.chat IRC: klange
Discord: klange

Re: Implementing the X Server

Post by klange »

PavelCheckov wrote:Thanks! Also, I noticed people referring to me as "Pavel", which I just want to put on the record is my username based on Checkov from Star Trek
If you style yourself after a character, you should expect as much, Mr. Checkov. Do you know where I can find any nuclear wessels?


(btw your name is spelled wrong, it should be Chekov!)
Post Reply