Making Simple X11 GUI Window in C on Linux

Поділитися
Вставка
  • Опубліковано 27 жов 2024

КОМЕНТАРІ • 117

  • @plasmarade
    @plasmarade 9 місяців тому +36

    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 9 місяців тому +2

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

    • @islamicstateofukraine
      @islamicstateofukraine День тому

      just use glfw brah..

  • @emdeetee8363
    @emdeetee8363 9 місяців тому +52

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

    • @CaptTerrific
      @CaptTerrific 9 місяців тому +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 9 місяців тому

      @@CaptTerrific now wayland is like learning violin

    • @ザウアークラウトマン
      @ザウアークラウトマン 9 місяців тому

      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 9 місяців тому

      @@_denzy_6310 lol that's apt

    • @freedomgoddess
      @freedomgoddess 6 місяців тому +1

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

  • @freedomgoddess
    @freedomgoddess 9 місяців тому +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.

  • @cornjulio4033
    @cornjulio4033 9 місяців тому +9

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

  • @zoeythebee1779
    @zoeythebee1779 9 місяців тому +7

    Wow X11 is shockingly simple!

  • @geavenxdd4773
    @geavenxdd4773 8 місяців тому +2

    You made me wanna learn C with this simple video

  • @modolief
    @modolief 9 місяців тому +3

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

  • @TheCocoaDaddy
    @TheCocoaDaddy 9 місяців тому +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!

  • @Mac501pl
    @Mac501pl 9 місяців тому +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 9 місяців тому +1

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

    • @shallex5744
      @shallex5744 9 місяців тому +3

      do people really watch this stuff on a phone

    • @SunPodder
      @SunPodder 9 місяців тому

      ​@@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 9 місяців тому

      @@shallex5744I do

    • @jawad9757
      @jawad9757 9 місяців тому

      ​@@shallex5744 yes

  • @karanmungra5630
    @karanmungra5630 9 місяців тому +4

    Really Nice Video. And thank you for the references

  • @gSys1337
    @gSys1337 9 місяців тому +13

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

  • @davidenglish3801
    @davidenglish3801 9 місяців тому +1

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

  • @liquidmobius
    @liquidmobius 9 місяців тому

    Another great vid, as always! Thanks nir!

  • @StefanWelebny
    @StefanWelebny 9 місяців тому +9

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

    • @nirlichtman
      @nirlichtman  9 місяців тому +6

      Yes, interesting subject!

  • @GK-rl5du
    @GK-rl5du 9 місяців тому +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  9 місяців тому +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.

  • @unchaynd7266
    @unchaynd7266 9 місяців тому +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.

  • @jawad9757
    @jawad9757 9 місяців тому

    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  9 місяців тому

      Thanks! Could you elaborate what exactly you mean?

    • @jawad9757
      @jawad9757 9 місяців тому

      @@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  9 місяців тому

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

  • @honeymak
    @honeymak 7 місяців тому

    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  7 місяців тому

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

  • @vanvothe4817
    @vanvothe4817 9 місяців тому +2

    Wow, what distro Linux/DE do you use?

    • @nirlichtman
      @nirlichtman  9 місяців тому +3

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

  • @bxavier
    @bxavier 9 місяців тому +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 9 місяців тому

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

  • @qxqxqxqxq
    @qxqxqxqxq 9 місяців тому +1

    next episode for wayland?

  • @coderhex1675
    @coderhex1675 9 місяців тому +1

    i will be waiting wayland tutorials

  • @jannatgaoshiqqalb3598
    @jannatgaoshiqqalb3598 9 місяців тому +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  9 місяців тому +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 9 місяців тому

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

  • @umikaliprivate
    @umikaliprivate 5 місяців тому +1

    does this work on wayland?

  • @AggamRahamim-fs2zm
    @AggamRahamim-fs2zm 9 місяців тому

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

    • @pianissimo7121
      @pianissimo7121 9 місяців тому +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 9 місяців тому

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

    • @pianissimo7121
      @pianissimo7121 9 місяців тому

      @@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 9 місяців тому

      umm lol ok@@pianissimo7121

    • @nirlichtman
      @nirlichtman  9 місяців тому +1

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

  • @mianaliahmed9886
    @mianaliahmed9886 9 місяців тому

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

  • @nabilbouhali7786
    @nabilbouhali7786 8 місяців тому

    GOAT !!

  • @guilherme5094
    @guilherme5094 9 місяців тому

    👍Great!

  • @ZeroCool2211
    @ZeroCool2211 9 місяців тому

    Which editor are u using?

    • @nirlichtman
      @nirlichtman  9 місяців тому +1

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

  • @ahmedsat4780
    @ahmedsat4780 9 місяців тому +2

    we need one for Wayland

  • @w2wizard
    @w2wizard 9 місяців тому

    Wayland window next ?

  • @albertovelasquez9027
    @albertovelasquez9027 9 місяців тому +3

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

  • @angelffg
    @angelffg 9 місяців тому

    Great!!!!!

  • @karanmungra5630
    @karanmungra5630 9 місяців тому +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 9 місяців тому

      I have been trying to do the same recently.

    • @peenywallie
      @peenywallie 9 місяців тому +1

      Check the source code for dwm?

    • @liquidmobius
      @liquidmobius 9 місяців тому

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

    • @CaptTerrific
      @CaptTerrific 9 місяців тому +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 9 місяців тому

      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!

  • @Nunya58294
    @Nunya58294 9 місяців тому +1

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

    • @ザウアークラウトマン
      @ザウアークラウトマン 9 місяців тому +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 9 місяців тому

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

    • @Nunya58294
      @Nunya58294 9 місяців тому

      Thank you for this good sir!

  • @malakggh
    @malakggh 9 місяців тому

    How does this for loop work and what does it mean

    • @nirlichtman
      @nirlichtman  9 місяців тому

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

  • @vibaj16
    @vibaj16 4 місяці тому

    the XCreateSimpleWindow function is giving me a segfault

  • @SerdSoftInteractive
    @SerdSoftInteractive 9 місяців тому

    wow cool

  • @n_for_n
    @n_for_n 9 місяців тому +37

    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 9 місяців тому +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  9 місяців тому +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 9 місяців тому +7

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

    • @DeveshNene
      @DeveshNene 9 місяців тому +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 9 місяців тому +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?

  • @jonts-nasci
    @jonts-nasci 9 місяців тому

    Just to say was here 👍

  • @alexdefoc6919
    @alexdefoc6919 2 місяці тому

    terminal name?

    • @nirlichtman
      @nirlichtman  2 місяці тому

      I think in this video I used xterm

  • @lowlevelcodingch
    @lowlevelcodingch 4 місяці тому

    I FUCKING LOVE C

  • @andrewdunbar828
    @andrewdunbar828 9 місяців тому

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

    • @nirlichtman
      @nirlichtman  9 місяців тому +1

      Thanks for the feedback, will improve for future vids 👍

  • @gahshunker
    @gahshunker 9 місяців тому

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

    • @nirlichtman
      @nirlichtman  9 місяців тому

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

  • @hansdietrich83
    @hansdietrich83 9 місяців тому

    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  9 місяців тому

      thx for the feedback, will fix for next vids 👍

    • @nirlichtman
      @nirlichtman  9 місяців тому

      thx for the feedback, will fix for next vids 👍

  • @ayoubelmhamdi7920
    @ayoubelmhamdi7920 9 місяців тому +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 9 місяців тому +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 9 місяців тому +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 9 місяців тому +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 9 місяців тому +1

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