Making Simple X11 GUI Window in C on Linux

Sdílet
Vložit
  • čas přidán 7. 09. 2024
  • In this video I will demonstrate how you can use the X11 Library to create a graphical window on the screen using the C Programming Language.
    X11 Dev Dependencies (Debian/Ubuntu package names):
    - libx11-dev
    - libx11-doc
    Online X11 doc I used in the video:
    www.x.org/rele...
    Code:
    github.com/nir...

Komentáře • 116

  • @emdeetee8363
    @emdeetee8363 Před 8 měsíci +48

    I like how this is easier than Windows API... You know, the operating system known for it's GUI

    • @CaptTerrific
      @CaptTerrific Před 8 měsíci +7

      It's kinda like piano vs guitar - sure, it's easier to achieve the basics on X11 (piano) vs. Windows (guitar)... but much like mastering any instrument, building the full Windows GUI takes just as much effort and pain as building something equivalent to KDE or GNOME :D

    • @_denzy_6310
      @_denzy_6310 Před 8 měsíci

      @@CaptTerrific now wayland is like learning violin

    • @user-qf4tb4yf7g
      @user-qf4tb4yf7g Před 8 měsíci

      I wrote a simple X11 GUI (only keyboard input and graphical output) a few months ago and I was pleasantly surprised how easy it was. Almost as easy as writing a gui with ncurses.

    • @CaptTerrific
      @CaptTerrific Před 8 měsíci

      @@_denzy_6310 lol that's apt

    • @freedomgoddess
      @freedomgoddess Před 5 měsíci +1

      ​@@CaptTerrific
      no that's pacman
      :^)

  • @plasmarade
    @plasmarade Před 8 měsíci +32

    For a game engine, I had to make a windowing library using X11...
    It was painful.
    I wish this video existed 2 years ago, but either way, I love your content!
    Nice work!

    • @Mempler
      @Mempler Před 8 měsíci +1

      I mean, it is very much like the Win32 API.
      BUUUT, i hate that its names are generic. for example, it overrode "None" which literally fucked up my code. I had to #undef like 8-9 different preprocessors :/

  • @cornjulio4033
    @cornjulio4033 Před 8 měsíci +9

    Normal people would never see it that way, but this is really hardcore ! Fascinating !

  • @freedomgoddess
    @freedomgoddess Před 7 měsíci +3

    good to see people take interest in this type of thing still. i am writing a desktop environment for x11 using xcb for my final year project. even if the technologies are old, and xlib is vastly inferior due to architecture reasons to xcb, this is something people can still very much use to create some perhaps rudimentary applications. thank you for the video.

  • @zoeythebee1779
    @zoeythebee1779 Před 8 měsíci +5

    Wow X11 is shockingly simple!

  • @TheCocoaDaddy
    @TheCocoaDaddy Před 8 měsíci +2

    Interesting! Less code than with the simple GUI window you created on Windows but far more reading of the docs. lol I've never actually written any GUI code in C, so both videos were fun to watch! Thanks for posting!

  • @geavenxdd4773
    @geavenxdd4773 Před 7 měsíci +1

    You made me wanna learn C with this simple video

  • @modolief
    @modolief Před 8 měsíci +3

    Such a tight demo. Extraordinary content, thanks so very much.

  • @davidenglish3801
    @davidenglish3801 Před 7 měsíci +1

    Wow, the best tutorial I ever seen of introduction to GUI in C. Regardless it's x11 anyway :).

  • @gSys1337
    @gSys1337 Před 8 měsíci +11

    Can I ask for a simple Wayland GUI Window on Linux? :)

  • @Mac501pl
    @Mac501pl Před 8 měsíci +24

    Love your videos, could you please make the font bigger and change the color scheme for one with a better contrast? It's hard to read on smaller screens

    • @trombecher
      @trombecher Před 8 měsíci +1

      Agree, the color scheme needs changing immediately. I cannot read a single letter on my phone. Love the video though.

    • @shallex5744
      @shallex5744 Před 8 měsíci +3

      do people really watch this stuff on a phone

    • @SunPodder
      @SunPodder Před 8 měsíci

      ​@@shallex5744i watch these tutorials in free time in my phone. When actually coding it's faster to read docs than following a video

    • @VBB9999
      @VBB9999 Před 8 měsíci

      @@shallex5744I do

    • @jawad9757
      @jawad9757 Před 8 měsíci

      ​@@shallex5744 yes

  • @karanmungra5630
    @karanmungra5630 Před 8 měsíci +4

    Really Nice Video. And thank you for the references

  • @coderhex1675
    @coderhex1675 Před 8 měsíci +1

    i will be waiting wayland tutorials

  • @unchaynd7266
    @unchaynd7266 Před 7 měsíci +1

    When I try to compile it, I get an error message (on the #include line) that says "X11/xlib.h: No such file or directory". But I've installed the packages listed in the description. Do you know why this is happening?
    Edit: Ah! I typed "xlib.h" instead of "Xlib.h". It's hard for me to see the dark magenta text on the black background in the video. Well, hopefully this helps anyone who has the same issue.

  • @StefanWelebny
    @StefanWelebny Před 8 měsíci +9

    Thanks! Good content again! Could you eventually also demonstrate the concept of x windows client and server in this context?

  • @GK-rl5du
    @GK-rl5du Před 8 měsíci +1

    As always great video Nir..
    Unless I missed something, it would be great if you can explain how this very example works at a high level. I see the term x11 server thrown around, but couldn't really grok it yet.
    x11 is like a server between kernel and user-space application which takes the commands and would draw stuff on the window?!

    • @nirlichtman
      @nirlichtman  Před 8 měsíci +2

      Thanks! Basically, at a high level there is the X11 Server which actually controls the graphics on the display and gets the input from the keyboard and mouse and you have the clients which all connect to the server to request drawing graphics and they also get events from the server for example mouse/keyboard clicks. The program I made in the video is an example of an X11 client.

  • @honeymak
    @honeymak Před 6 měsíci

    you make cool coding videos. i would like to know whether you have a video introduce your coding env/ide etc. i like how vim auto-complete and split term and man pages. will you talk about your vim plugins...etc

    • @nirlichtman
      @nirlichtman  Před 6 měsíci

      Thanks! I have a playlist "Vim Tips" where I talk a lot about my workflow, I only use two Vim plugins both related to LSP and I have videos about that as well :)

  • @liquidmobius
    @liquidmobius Před 8 měsíci

    Another great vid, as always! Thanks nir!

  • @jawad9757
    @jawad9757 Před 8 měsíci

    Great video, have you considered implementing a simple stack-based virtual machine in one of your videos?
    I feel like it would be simple and self-contained enough for your style of video.

    • @nirlichtman
      @nirlichtman  Před 8 měsíci

      Thanks! Could you elaborate what exactly you mean?

    • @jawad9757
      @jawad9757 Před 8 měsíci

      @@nirlichtman I mean the type of virtual machine that you might find inside a simple interpreter.
      Perhaps it doesn't make much sense on its own

    • @nirlichtman
      @nirlichtman  Před 8 měsíci

      Interesting, this can be a cool project, perhaps make it as minimalist as possible :)

  • @bxavier
    @bxavier Před 8 měsíci +2

    Hello, I like your videos, they show some concepts in a simple and quick way. Maybe you can help me.I want to create a program that allows me to password protect any program I have installed to run (Chrome, Steam, etc.), that is, when I click on a program to open it, a dialog box appears asking me to enter a password. If it is correct, the program opens, otherwise it does not. I want to do all this for Windows, I have been looking for what processes or libraries I should use to listen to an event when a program starts but I am a little confused, I don't know where to start. Maybe you can help me with that.

    • @freedomgoddess
      @freedomgoddess Před 7 měsíci

      there's some serious difficulty with making this in a secure way but a basic, silly way to achieve this and protect your stuff from a non-technical user would be to monitor create window events on the root window using your always-running program, popping up a window that covers the whole screen and asking for a password that will be compared to something you perhaps hardcoded in. if the strings are equal, close window and resume normal operation. otherwise, kill the opened window by taking away a resource (there's this function you can just pass the opened window there and the program will effectively just close).

  • @nabilbouhali7786
    @nabilbouhali7786 Před 7 měsíci

    GOAT !!

  • @umikaliprivate
    @umikaliprivate Před 3 měsíci +1

    does this work on wayland?

  • @ahmedsat4780
    @ahmedsat4780 Před 8 měsíci +2

    we need one for Wayland

  • @qxqxqxqxq
    @qxqxqxqxq Před 8 měsíci +1

    next episode for wayland?

  • @albertovelasquez9027
    @albertovelasquez9027 Před 8 měsíci +3

    Thanks for the video!
    But the high contrast color theme makes it harder to read. Still good video

  • @vanvothe4817
    @vanvothe4817 Před 8 měsíci +2

    Wow, what distro Linux/DE do you use?

    • @nirlichtman
      @nirlichtman  Před 8 měsíci +3

      I use Windows WSL with Debian, the terminal emulator I use in this video is xterm

  • @jonts-nasci
    @jonts-nasci Před 7 měsíci

    Just to say was here 👍

  • @mianaliahmed9886
    @mianaliahmed9886 Před 8 měsíci

    general question: where do I learn about all of this stuff, I suppose the decumnetation but that place is a mess.

  • @SerdSoftInteractive
    @SerdSoftInteractive Před 8 měsíci

    wow cool

  • @jannatgaoshiqqalb3598
    @jannatgaoshiqqalb3598 Před 8 měsíci +1

    Hi Nir bro,
    Bro, how to create a cli-based app that waits for a specific file, if that specific file will be opened by another program or another process, it prints a message to the console, like "hey, that file is being used"?
    Thanks in advance for your response, good luck for your future videos!

    • @nirlichtman
      @nirlichtman  Před 8 měsíci +2

      Windows or Linux? For Windows the closest API I can think of is FindFirstChangeNotification ( learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findfirstchangenotificationw ) but it will notify you only when there is a change in the files, I don't think it can notify when files are just opened. If I think of something more suitable I will update here.

    • @jannatgaoshiqqalb3598
      @jannatgaoshiqqalb3598 Před 8 měsíci

      @@nirlichtman thanks bro, it should be for windows only

  • @lowlevelcodingch
    @lowlevelcodingch Před 2 měsíci

    I FUCKING LOVE C

  • @karanmungra5630
    @karanmungra5630 Před 8 měsíci +2

    Hi! can you give brief overview. How you can build a small tiling manager or desktop environment on top of X11. Can you please make a brief video, on the architecture or what technology they use.

    • @mianaliahmed9886
      @mianaliahmed9886 Před 8 měsíci

      I have been trying to do the same recently.

    • @peenywallie
      @peenywallie Před 8 měsíci +1

      Check the source code for dwm?

    • @liquidmobius
      @liquidmobius Před 8 měsíci

      Check out the source code for tinyWM. It's only ~50 loc and super easy to understand, much easier than dwm.

    • @CaptTerrific
      @CaptTerrific Před 8 měsíci +4

      The lightest ones use pure X11 - I believe in you! You'll learn a ton more just implementing it yourself based on the manual pages, it's a fantastic project to learn how both X Windows and GUIs in general function :)

    • @freedomgoddess
      @freedomgoddess Před 7 měsíci

      i started making exactly this 5 years ago. still incomplete but it's advanced far too much and i have reason to complete it. give it a go and don't give up!

  • @AggamRahamim-fs2zm
    @AggamRahamim-fs2zm Před 8 měsíci

    Hey man I love your videos. I was wondering tho why use for (;;) insted of a while(1) loop?

    • @pianissimo7121
      @pianissimo7121 Před 8 měsíci +1

      If I am not wrong the compiler should optimise it to the same thing, so it won't matter.
      Got no proof , just guessing based on intuition.

    • @AggamRahamim-fs2zm
      @AggamRahamim-fs2zm Před 8 měsíci

      ​@@pianissimo7121 yeah that makes sense, but why do it in the first place?

    • @pianissimo7121
      @pianissimo7121 Před 8 měsíci

      @@AggamRahamim-fs2zm i can't speak for him, but maybe it's just habit? Even if he has a reason I don't think there is much knowledge you can gain from this.

    • @AggamRahamim-fs2zm
      @AggamRahamim-fs2zm Před 8 měsíci

      umm lol ok@@pianissimo7121

    • @nirlichtman
      @nirlichtman  Před 8 měsíci +1

      Thanks! I prefer using "for (;;)" over "while (1)" since sometimes compilers issue a warning on while (1) having a constant condition.

  • @guilherme5094
    @guilherme5094 Před 8 měsíci

    👍Great!

  • @vibaj16
    @vibaj16 Před 3 měsíci

    the XCreateSimpleWindow function is giving me a segfault

  • @ZeroCool2211
    @ZeroCool2211 Před 7 měsíci

    Which editor are u using?

    • @nirlichtman
      @nirlichtman  Před 7 měsíci +1

      Vim, many of my early vids are about it :)

  • @Nunya58294
    @Nunya58294 Před 8 měsíci +1

    Oh hot damn! This will be a fun project!
    I wonder if this would work in a text-only environment...

    • @user-qf4tb4yf7g
      @user-qf4tb4yf7g Před 8 měsíci +1

      There is the ncurses library for that. You can use unicode characters in order to simulate two pixels in each character on tge screen.

    • @Nunya58294
      @Nunya58294 Před 8 měsíci

      @@user-qf4tb4yf7g This will be perfect!!!! I have dabbled with Ncurses in the past so this will be fun

    • @Nunya58294
      @Nunya58294 Před 8 měsíci

      Thank you for this good sir!

  • @angelffg
    @angelffg Před 8 měsíci

    Great!!!!!

  • @w2wizard
    @w2wizard Před 8 měsíci

    Wayland window next ?

  • @malakggh
    @malakggh Před 8 měsíci

    How does this for loop work and what does it mean

    • @nirlichtman
      @nirlichtman  Před 8 měsíci

      for ( ; ; ) is a for loop with no condition so it acts as an infinite loop

  • @andrewdunbar828
    @andrewdunbar828 Před 8 měsíci

    Minor niggle: The dark purple on black is not the best colour for this.

    • @nirlichtman
      @nirlichtman  Před 8 měsíci +1

      Thanks for the feedback, will improve for future vids 👍

  • @n_for_n
    @n_for_n Před 8 měsíci +35

    What about Wayland? X11 is actively dying out, and in a couple of years I think it will only be a distant memory

    • @user-sh2mr7vf9i
      @user-sh2mr7vf9i Před 8 měsíci +18

      Wayland does not have the concept of windows. It has client and surfaces.lot of boiler plate is required to make it work.

    • @nirlichtman
      @nirlichtman  Před 8 měsíci +44

      Wayland is also a great subject, I plan on covering it as well (and maybe also using it in the future video I plan about making a GUI distro)

    • @n_for_n
      @n_for_n Před 8 měsíci +7

      @@user-sh2mr7vf9i I know, but asking about a surface in a comment below a "coding a window" video seemed weird

    • @user-sh2mr7vf9i
      @user-sh2mr7vf9i Před 8 měsíci +6

      @@n_for_n I ment to say there is nothing like "coding a window" in Wayland. A Wayland client sends it's buffer to the Wayland server(typically a window manager) using shared memory. And the Wayland server is responsible for compositing and rendering it. For some reason people are not aware where Wayland fits in the linux graphics stack.

    • @jyothishkumar3098
      @jyothishkumar3098 Před 8 měsíci +1

      ​@@user-sh2mr7vf9iHow do custom window decorations work then? Also wouldn't the analogy then simply be about coding a Wayland compositor, as that is creating the windows for a given input buffer?

  • @alexdefoc6919
    @alexdefoc6919 Před 27 dny

    terminal name?

  • @gahshunker
    @gahshunker Před 8 měsíci

    keyboard clicking is a bit too much. otherwise, great video

    • @nirlichtman
      @nirlichtman  Před 8 měsíci

      thx for the feedback, working on finding the correct position for the mic 👍

  • @hansdietrich83
    @hansdietrich83 Před 8 měsíci

    You should really use a different color sceme for YT Videos. All of the darker text is barely readable with the video compression

    • @nirlichtman
      @nirlichtman  Před 8 měsíci

      thx for the feedback, will fix for next vids 👍

    • @nirlichtman
      @nirlichtman  Před 8 měsíci

      thx for the feedback, will fix for next vids 👍

  • @ayoubelmhamdi7920
    @ayoubelmhamdi7920 Před 8 měsíci +1

    Your topic is awesome, and using sources for implementation is a great idea, but the application is terrible, we learn nothing from this way, it's like you're cheating, it's like you're just copy pasting.
    I hope you use a real board and a style and write a tutorial, so you think step by step like us, from scratch, without constructing ideas without thinking whether it's clear, coherent, and conscious for us or not.

    • @liquidmobius
      @liquidmobius Před 8 měsíci +1

      He's not necessarily making tutorials. There's literally TONS of tutorial videos if you want to learn programming. To follow his vids, it helps to know at least the basics: variables, loops, arrays, functions, header files, etc.

    • @CaptTerrific
      @CaptTerrific Před 8 měsíci +4

      It's literally not cheating to check manual pages for a library - how else are you supposed to utilize it? Literally read all of the X11 source code directly? Or reimplement your own windowing system from scratch!?

    • @ayoubelmhamdi7920
      @ayoubelmhamdi7920 Před 8 měsíci +1

      @CaptTerrific
      When I watch the Jonathan-Blow programming or @tsoding or sometimes @Teejtv and so on... we watch the high-level programming session, there's never a dull moment, the programming becomes fun. Tsoding and Jonathan don't use any autocompletions like you, but they write the art...
      For me to build a GUI in C, we discuss why we should use X11 and what it is, and the power of X11, something interesting like that, then start from "hello world", compile it, then the other students, can't wait to see the first step to use X11, as you see the students are excited to watch more... and don't feel this is boring...

    • @freedomgoddess
      @freedomgoddess Před 7 měsíci +1

      _if all else fails, you can do it yourself._