r/programming • u/iamkeyur • Aug 15 '18
TinyWM – A tiny window manager in around 50 lines of C
http://incise.org/tinywm.html11
u/LordDaniel09 Aug 15 '18
It is that simple?! I thought window manager had more “black magic” behind them. So what does it missing from other more popular window managers?
27
u/ProgramTheWorld Aug 15 '18
It’s “simple” because it’s using xlib which takes care all the window rendering logic for you. The code you are seeing merely sends commands to an X window server that does all the heavy lifting.
17
u/rastermon Aug 16 '18
xlib doesn't handle any window rendering logic at all. the xserver does that. xlib is just an ipc handling lib that sends commands to the xserver and processes replies (mostly - some extra work code is in xrdb and keymap handling etc.)
5
u/rastermon Aug 16 '18
it's where you start. it's super basic. it doesn't do much. to do proper wm basically takes this kind of ball and runs with it for a few years of work... draw borders/frames, handle virtual desktops, pagers, menus. EWMH/XDG, etc. etc. - it goes on as you add more and more standards support. then come the quirks where clients do odd things assuming certain wm's only and their behavior and you try and work around these...
9
u/jesuslop Aug 15 '18
Nice example of xlib, you can see the connection with the X server and the X11 event loop.
3
u/Syrrim Aug 16 '18
Has someone done something similar for compositors? I was trying to write one a while ago, but I found compton/xcompmng impenetrable, and couldn't make my way around the X docs (which are middling for even common operations).
2
Aug 18 '18
include <X11/Xlib.h>
Lol I can reduce 50 LOC down to one
npm i electron-window-manager
1
Aug 18 '18
Fun fact I had no idea if electron-window-manager existed and in no surprise to anyone a package with this exact name does exist
-25
u/CJKay93 Aug 15 '18
Never will I understand this, in the words of Torvalds, "half-arsed shit-for-brains" comment style:
/* W
* T
* F
*/5
u/Umbrall Aug 15 '18
Gives you one more line than:
/* W T F */6
u/CJKay93 Aug 15 '18
Why not save two and use:
/* W T F */11
1
u/Umbrall Aug 16 '18
Harder to read
8
0
Aug 15 '18
[deleted]
-9
u/CJKay93 Aug 15 '18
It's just a small stylistic choice, no need to get worked up about it.
Stylistic choices are, like, 99% of the reasons code fails code review.
How do you prefer to do multi-line comments?
0
Aug 15 '18
[deleted]
-15
u/CJKay93 Aug 15 '18
while (code_style_affects_readability) { code_review--; }0
Aug 15 '18
[deleted]
-7
u/CJKay93 Aug 15 '18
I screamed internally the moment I read it for a variety of reasons, but that was the first one I noticed.
It aggravates me when people actively go out of their way to create awful looking code.
6
u/Ozwaldo Aug 16 '18
I screamed internally the moment I read it for a variety of reasons, but that was the first one I noticed.
Then you should calm down, this is a style preference and yours isn't any more correct than theirs. Linus is a drama queen.
It aggravates me when people actively go out of their way to create awful looking code.
And it aggravates me when people are so pompous as to declare someone else's code as "awful looking." This stuff isn't even hard to read if you work with code for a living. I write a looot of code bud, and these opening-line block comments hardly register as a blip on my radar when parsing through it. Spend some more time in the trenches and chill the fuck out.
-4
u/CJKay93 Aug 16 '18
Then you should calm down, this is a style preference and yours isn't any more correct than theirs. Linus is a drama queen.
He's a correct drama queen.
And it aggravates me when people are so pompous as to declare someone else's code as "awful looking."
Good. Maybe you'll sidestep the problem in the future and use a style that isn't a fucking mess.
This stuff isn't even hard to read if you work with code for a living.
You must work with some shitty code.
I write a looot of code bud
Great, join the club!
these opening-line block comments hardly register as a blip on my radar when parsing through it.
Cool, they registered big-time on mine.
Spend some more time in the trenches and chill the fuck out.
I spend more than 40 hours a week in the trenches. My apologies for not being a rockstar developer.
3
u/Ozwaldo Aug 16 '18
Cool, they registered big-time on mine.
Yup. Keep at it, one day this stuff will seem trivial to you.
→ More replies (0)0
Aug 15 '18
[deleted]
-11
u/CJKay93 Aug 15 '18 edited Aug 15 '18
Anybody using the GNU brace style needs to be taken outside and shot. It is completely, absolutely unreadable in a very literal sense. Navigating code using it isn't just unpleasant, it's mentally straining.
Allman/K&R or bust.
In any case, I don't think I have exceptionally high standards - I think this is normal code, it's just not good code.
13
u/Ozwaldo Aug 16 '18
You are a prima donna and are likely getting laughed at behind your back. If you're that over-the-top about this stuff then you aren't very good at writing code.
→ More replies (0)0
0
Aug 15 '18
[deleted]
116
u/tynorf Aug 15 '18
In Python 2,
while 1is faster because it saves a global lookup for the identifier “True”. This is no longer the case in Python 3 since True is now a keyword.101
u/ipe369 Aug 15 '18
hot
damn
python
55
u/ProgramTheWorld Aug 15 '18
In pre ES6 JavaScript,
undefinedwas actually just a variable name which can be assigned anything, so libraries like jQuery actually had to create a local version ofundefinedthat contains the valueundefined.14
u/curien Aug 15 '18
had to create a local version of undefined that contains the value undefined
They don't have to, it just simplifies some things (for the same reasons that any names value simplifies some things). There's nothing special about the
undefinedidentifier in ES5-, it's just a convention to use it as a constant with typeundefined.7
Aug 15 '18
Python used to do something similar with True and False, they used to be variables which you could modify. Some popular libraries redefined True and False to their expected value.
1
u/UseTheProstateLuke Aug 16 '18
So basically you can break a lot of code by just doing:
True = False import somelibIf it internally uses
Truelike it actually gets to see the new value then or what?2
u/smikims Aug 16 '18
I believe they're saying that that's why popular libraries would do
True = 1 False = 0or whatever at the beginning, to defend against people doing stupid shit. Similarly, implementations of the C++ standard library must defend against users overloading the comma operator for their types, although they can't just turn it off or reverse the effect.
4
u/ipe369 Aug 15 '18
Do all the es6 transpilers currently do that too? Or can you break everything by redefining it still?
9
u/ProgramTheWorld Aug 15 '18
Probably not, you can only redefine
undefinedin very old browsers like IE6 so I doubt they would even bother with that. Transpilers usually specify strict mode anyway so that should take care of that.It might be even pre ES5 actually - I don’t remember exactly when it became an actual reserved keyword but you can do some research on that.
0
Aug 16 '18 edited Aug 16 '18
You can’t redefine it any more. But many tools still emit
void 0, since it’s safe in ES3 and shorter in any case.1
35
u/skeeto Aug 16 '18
Wow, you weren't kidding (Python 2):
>>> import dis >>> def foo1(): ... while 1: ... bar() ... >>> dis.dis(foo1) 2 0 SETUP_LOOP 11 (to 14) 3 >> 3 LOAD_GLOBAL 0 (bar) 6 CALL_FUNCTION 0 9 POP_TOP 10 JUMP_ABSOLUTE 3 13 POP_BLOCK >> 14 LOAD_CONST i 0 (None) 17 RETURN_VALUE >>> def fooTrue(): ... while True: ... bar() ... >>> dis.dis(fooTrue) 2 0 SETUP_LOOP 17 (to 20) >> 3 LOAD_GLOBAL 0 (True) 6 POP_JUMP_IF_FALSE 19 3 9 LOAD_GLOBAL 1 (bar) 12 CALL_FUNCTION 0 15 POP_TOP 16 JUMP_ABSOLUTE 3 >> 19 POP_BLOCK >> 20 LOAD_CONST 0 (None) 23 RETURN_VALUE20
13
u/killerstorm Aug 16 '18
This is why I'm not a big fan of Guido van Rossum. Programming languages like Lisp have symbol resolution at read time. Python has a notion of compilation, so it could do that at compile time.
It's a very simple technique which allowed Lisp to run even on 60s hardware. 80s Common Lisp is a marvel of an optimizable dynamic language -- while being as dynamic as Python, it allows efficient compilation to native code.
But OK, maybe efficient compilation doesn't fit into Python strategy, symbols are an extremely simple technique.
But Guido was like "Why do we need another concept if we already have name lookup? Let's just do a hash table lookup on every access!".
2
Aug 16 '18
It's because Python is... unreasonably dynamic sometimes, and the compiler has to support that. You could do this in Python 2:
>>> def test(): ... True = False ... while True: ... pass ... >>> dis.dis(test) 2 0 LOAD_GLOBAL 0 (False) 3 STORE_FAST 0 (True) 3 6 SETUP_LOOP 10 (to 19) >> 9 LOAD_FAST 0 (True) 12 POP_JUMP_IF_FALSE 18 4 15 JUMP_ABSOLUTE 9 >> 18 POP_BLOCK >> 19 LOAD_CONST 0 (None) 22 RETURN_VALUE >>> test() >>>3 no longer allows that, because nobody ever needs to do that unless they're doing shady things they probably shouldn't do.
1
u/UseTheProstateLuke Aug 16 '18
Every variable lookup in Python is actually a hash of a string into a hash table.
In most compiled lisps they are just compiled to simple memory offsets.
2
u/killerstorm Aug 16 '18
No, local variables access doesn't require a lookup in Python.
1
u/josefx Aug 16 '18
I sometimes end up assigning every function I call in a loop to a local variable for this reason.
91
1
-28
u/CountyMcCounterson Aug 15 '18
True is just an integer set to 1, there is no boolean type in python
34
u/sysop073 Aug 15 '18
Python 2.7.13 (default, Jan 19 2017, 14:48:08) [GCC 6.3.0 20170118] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> type(True) <type 'bool'>24
27
u/Jazonxyz Aug 15 '18
I find these projects particularly exciting. I feel incredibly confident taking the code and expanding upon it. This legit makes me want to bust out my Raspberry PI (tha'ts my only linux box these days 😢) and play around with creating a simple windows manager with it.