Making Simple X11 GUI Window in C on Linux

Поделиться
HTML-код
  • Опубликовано: 29 сен 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...

Комментарии • 116

  • @plasmarade
    @plasmarade 8 месяцев назад +34

    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 8 месяцев назад +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 :/

  • @emdeetee8363
    @emdeetee8363 8 месяцев назад +48

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

    • @CaptTerrific
      @CaptTerrific 8 месяцев назад +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 8 месяцев назад

      @@CaptTerrific now wayland is like learning violin

    • @ザウアークラウトマン
      @ザウアークラウトマン 8 месяцев назад

      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 8 месяцев назад

      @@_denzy_6310 lol that's apt

    • @freedomgoddess
      @freedomgoddess 5 месяцев назад +1

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

  • @davidenglish3801
    @davidenglish3801 8 месяцев назад +1

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

  • @Mac501pl
    @Mac501pl 8 месяцев назад +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 8 месяцев назад +1

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

    • @shallex5744
      @shallex5744 8 месяцев назад +3

      do people really watch this stuff on a phone

    • @SunPodder
      @SunPodder 8 месяцев назад

      ​@@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 8 месяцев назад

      @@shallex5744I do

    • @jawad9757
      @jawad9757 8 месяцев назад

      ​@@shallex5744 yes

  • @liquidmobius
    @liquidmobius 8 месяцев назад

    Another great vid, as always! Thanks nir!

  • @unchaynd7266
    @unchaynd7266 8 месяцев назад +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.

  • @honeymak
    @honeymak 6 месяцев назад

    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  6 месяцев назад

      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 :)

  • @GK-rl5du
    @GK-rl5du 8 месяцев назад +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  8 месяцев назад +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.

  • @bxavier
    @bxavier 8 месяцев назад +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 8 месяцев назад

      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).

  • @lowlevelcodingch
    @lowlevelcodingch 3 месяца назад

    I FUCKING LOVE C

  • @SerdSoftInteractive
    @SerdSoftInteractive 8 месяцев назад

    wow cool

  • @gSys1337
    @gSys1337 8 месяцев назад +12

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

  • @zoeythebee1779
    @zoeythebee1779 8 месяцев назад +6

    Wow X11 is shockingly simple!

  • @cornjulio4033
    @cornjulio4033 8 месяцев назад +9

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

  • @freedomgoddess
    @freedomgoddess 8 месяцев назад +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.

  • @geavenxdd4773
    @geavenxdd4773 7 месяцев назад +2

    You made me wanna learn C with this simple video

  • @umikaliprivate
    @umikaliprivate 4 месяца назад +1

    does this work on wayland?

  • @modolief
    @modolief 8 месяцев назад +3

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

  • @karanmungra5630
    @karanmungra5630 8 месяцев назад +4

    Really Nice Video. And thank you for the references

  • @TheCocoaDaddy
    @TheCocoaDaddy 8 месяцев назад +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!

  • @vibaj16
    @vibaj16 3 месяца назад

    the XCreateSimpleWindow function is giving me a segfault

  • @coderhex1675
    @coderhex1675 8 месяцев назад +1

    i will be waiting wayland tutorials

  • @vanvothe4817
    @vanvothe4817 8 месяцев назад +2

    Wow, what distro Linux/DE do you use?

    • @nirlichtman
      @nirlichtman  8 месяцев назад +3

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

  • @ahmedsat4780
    @ahmedsat4780 8 месяцев назад +2

    we need one for Wayland

  • @qxqxqxqxq
    @qxqxqxqxq 8 месяцев назад +1

    next episode for wayland?

  • @StefanWelebny
    @StefanWelebny 8 месяцев назад +9

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

    • @nirlichtman
      @nirlichtman  8 месяцев назад +6

      Yes, interesting subject!

  • @andrewdunbar828
    @andrewdunbar828 8 месяцев назад

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

    • @nirlichtman
      @nirlichtman  8 месяцев назад +1

      Thanks for the feedback, will improve for future vids 👍

  • @jonts-nasci
    @jonts-nasci 8 месяцев назад

    Just to say was here 👍

  • @nabilbouhali7786
    @nabilbouhali7786 7 месяцев назад

    GOAT !!

  • @jannatgaoshiqqalb3598
    @jannatgaoshiqqalb3598 8 месяцев назад +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  8 месяцев назад +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 8 месяцев назад

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

  • @mianaliahmed9886
    @mianaliahmed9886 8 месяцев назад

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

  • @ZeroCool2211
    @ZeroCool2211 8 месяцев назад

    Which editor are u using?

    • @nirlichtman
      @nirlichtman  8 месяцев назад +1

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

  • @alexdefoc6919
    @alexdefoc6919 Месяц назад

    terminal name?

    • @nirlichtman
      @nirlichtman  Месяц назад

      I think in this video I used xterm

  • @gahshunker
    @gahshunker 8 месяцев назад

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

    • @nirlichtman
      @nirlichtman  8 месяцев назад

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

  • @w2wizard
    @w2wizard 8 месяцев назад

    Wayland window next ?

  • @guilherme5094
    @guilherme5094 8 месяцев назад

    👍Great!

  • @albertovelasquez9027
    @albertovelasquez9027 8 месяцев назад +3

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

  • @malakggh
    @malakggh 8 месяцев назад

    How does this for loop work and what does it mean

    • @nirlichtman
      @nirlichtman  8 месяцев назад

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

  • @jawad9757
    @jawad9757 8 месяцев назад

    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  8 месяцев назад

      Thanks! Could you elaborate what exactly you mean?

    • @jawad9757
      @jawad9757 8 месяцев назад

      @@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  8 месяцев назад

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

  • @angelffg
    @angelffg 8 месяцев назад

    Great!!!!!

  • @AggamRahamim-fs2zm
    @AggamRahamim-fs2zm 8 месяцев назад

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

    • @pianissimo7121
      @pianissimo7121 8 месяцев назад +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 8 месяцев назад

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

    • @pianissimo7121
      @pianissimo7121 8 месяцев назад

      @@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 8 месяцев назад

      umm lol ok@@pianissimo7121

    • @nirlichtman
      @nirlichtman  8 месяцев назад +1

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

  • @karanmungra5630
    @karanmungra5630 8 месяцев назад +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 8 месяцев назад

      I have been trying to do the same recently.

    • @peenywallie
      @peenywallie 8 месяцев назад +1

      Check the source code for dwm?

    • @liquidmobius
      @liquidmobius 8 месяцев назад

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

    • @CaptTerrific
      @CaptTerrific 8 месяцев назад +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 8 месяцев назад

      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!

  • @ayoubelmhamdi7920
    @ayoubelmhamdi7920 8 месяцев назад +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 8 месяцев назад +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 8 месяцев назад +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 8 месяцев назад +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 8 месяцев назад +1

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

  • @n_for_n
    @n_for_n 8 месяцев назад +36

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

    • @DeveshNene
      @DeveshNene 8 месяцев назад +19

      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  8 месяцев назад +45

      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 8 месяцев назад +7

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

    • @DeveshNene
      @DeveshNene 8 месяцев назад +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 8 месяцев назад +1

      ​@@DeveshNeneHow 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?

  • @hansdietrich83
    @hansdietrich83 8 месяцев назад

    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  8 месяцев назад

      thx for the feedback, will fix for next vids 👍

    • @nirlichtman
      @nirlichtman  8 месяцев назад

      thx for the feedback, will fix for next vids 👍

  • @Nunya58294
    @Nunya58294 8 месяцев назад +1

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

    • @ザウアークラウトマン
      @ザウアークラウトマン 8 месяцев назад +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 8 месяцев назад

      @@ザウアークラウトマン This will be perfect!!!! I have dabbled with Ncurses in the past so this will be fun

    • @Nunya58294
      @Nunya58294 8 месяцев назад

      Thank you for this good sir!