Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
I'm no great programmer, but I like to think of myself as a hacker and tinkerer, and am pretty good at picking up on technical stuff if I find the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2005
    Posts
    10

    Xlib programming (Coding my own WM)


    I'm no great programmer, but I like to think of myself as a hacker and tinkerer, and am pretty good at picking up on technical stuff if I find the right introduction.

    Anyway, I'm sick of the lack of WMs that suit my needs, so I'm coding my own based off of TinyWM.

    I'm not making something incredibly fancy. If you have used BadWM, then you will know exactly what I want - NO window decorations except for some indication of which window has focus, keyboard shortcuts for a few basic tasks, and virtual desktops. The only reason I'm not just relying on BadWM is because it is unstable.

    Anyway, here is my problem.. I have yet to find the answers to my problems! I've been reading the TinyWM source for the past week or two, and haven't quite figured out how it is determining what key is being pressed (I know how it is capturing the keys, but how is it deciding that F1 is F1? I have included the TinyWM source at the bottom of the post).

    My first step is implementing a shortcut to launch RXVT.

    Also, if anybody could help me in understanding the process in executing a program from the WM, I'd appreciate it. The X11 Programming Manual I have doesn't exactly provide enough clarification for my tastes (eh, it's summer, I'm home from college, and I'm lazy :P)

    Oh yeah.. This is my first foray into Linux C programming I have experience in C++, x86 ASM (self taught), PHP, VB.Net (hate it), HTML, Javascript, Turbo Pascal, and BASH. Most of my experience is in Windows programming.


    Code:
    /* TinyWM is written by Nick Welch <mack@incise.org>, 2005.
     *
     * This software is in the public domain
     * and is provided AS IS, with NO WARRANTY. */
    
    #include <X11/Xlib.h>
    
    #define MAX&#40;a, b&#41; &#40;&#40;a&#41; > &#40;b&#41; ? &#40;a&#41; &#58; &#40;b&#41;&#41;
    
    int main&#40;&#41;
    &#123;
        Display * dpy;
        Window root;
        XWindowAttributes attr;
        XButtonEvent start;
        XEvent ev;
    
        if&#40;!&#40;dpy = XOpenDisplay&#40;0x0&#41;&#41;&#41; return 1;
    
        root = DefaultRootWindow&#40;dpy&#41;;
    
        XGrabKey&#40;dpy, XKeysymToKeycode&#40;dpy, XStringToKeysym&#40;"F1"&#41;&#41;, Mod1Mask, root,
                True, GrabModeAsync, GrabModeAsync&#41;;
        XGrabButton&#40;dpy, 1, Mod1Mask, root, True, ButtonPressMask, GrabModeAsync,
                GrabModeAsync, None, None&#41;;
        XGrabButton&#40;dpy, 3, Mod1Mask, root, True, ButtonPressMask, GrabModeAsync,
                GrabModeAsync, None, None&#41;;
    
        for&#40;;;&#41;
        &#123;
            XNextEvent&#40;dpy, &ev&#41;;
            if&#40;ev.type == KeyPress && ev.xkey.subwindow != None&#41;
                XRaiseWindow&#40;dpy, ev.xkey.subwindow&#41;;
            else if&#40;ev.type == ButtonPress && ev.xbutton.subwindow != None&#41;
            &#123;
                XGrabPointer&#40;dpy, ev.xbutton.subwindow, True,
                        PointerMotionMask|ButtonReleaseMask, GrabModeAsync,
                        GrabModeAsync, None, None, CurrentTime&#41;;
                XGetWindowAttributes&#40;dpy, ev.xbutton.subwindow, &attr&#41;;
                start = ev.xbutton;
            &#125;
            else if&#40;ev.type == MotionNotify&#41;
            &#123;
                int xdiff, ydiff;
                while&#40;XCheckTypedEvent&#40;dpy, MotionNotify, &ev&#41;&#41;;
                xdiff = ev.xbutton.x_root - start.x_root;
                ydiff = ev.xbutton.y_root - start.y_root;
                XMoveResizeWindow&#40;dpy, ev.xmotion.window,
                    attr.x + &#40;start.button==1 ? xdiff &#58; 0&#41;,
                    attr.y + &#40;start.button==1 ? ydiff &#58; 0&#41;,
                    MAX&#40;1, attr.width + &#40;start.button==3 ? xdiff &#58; 0&#41;&#41;,
                    MAX&#40;1, attr.height + &#40;start.button==3 ? ydiff &#58; 0&#41;&#41;&#41;;
            &#125;
            else if&#40;ev.type == ButtonRelease&#41;
                XUngrabPointer&#40;dpy, CurrentTime&#41;;
        &#125;
    &#125;

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    I know how it is capturing the keys, but how is it deciding that F1 is F1?
    I'm not sure I understand the question.I'm not very familiar with
    X windows but as far as I can see it grabs F1 so if it sees KeyPress it knows
    it's F1.That's what I assume is happening anyway.I could be completely off.

  3. #3
    Just Joined!
    Join Date
    May 2005
    Posts
    10
    I'm actually talking about the part in the for loop.. The series of nested if-then-else statements. It is apparent to me that the first if-then is the statement that raises the window to the front. How does it know that when F1 is grabbed that THIS is the statement it is supposed to execute? I see no conditional that checks for F1 for it. I compiled it and ran it to test, and sure enough, it somehow knows that F1 brings the window to the foreground..

  4. #4
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    As I said I'm only speculating but it says if(ev.type == KeyPress
    Since it has arranged earlier to grab the press of F1 (and presumably nothing else) ,
    if the last event was KeyPress it knows it was F1.

  5. #5
    Just Joined!
    Join Date
    May 2005
    Posts
    10
    Yeah, but what if we have multiple key grabs (such as the first one I will be adding, alt-enter to launch RXVT)? How will it differentiate them?

  6. #6
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    With that I cannot help you I'm afraid.I suggest you google for X Windows
    tutorial.

  7. #7
    Just Joined!
    Join Date
    May 2005
    Posts
    10
    Ok, figured it out (got in touch with the TinyWM dev). First, I declare the keycodes and store the values, and then in the if statement I..

    Code:
    if&#40;ev == KeyPress && ev.xkey.keycode == enter&#41;
    &#123;
    ...
    &#125;
    However, I get Segfaults. I get a warning that dpy (see source in first post) may be unitialized or not used (or something like that) when compiling, and when I run it, once I move the mouse it segfaults.

  8. #8
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Can you give us the new listing and the exact error message you're getting ?

  9. #9
    Just Joined!
    Join Date
    May 2005
    Posts
    10
    Current Source:

    Code:
    #include <X11/Xlib.h>
    
    #define MAX&#40;a, b&#41; &#40;&#40;a&#41; > &#40;b&#41; ? &#40;a&#41; &#58; &#40;b&#41;&#41;
    
    int main&#40;&#41;
    &#123;
        Display *dpy;
        Window root;
        XWindowAttributes attr;
        XButtonEvent start;
        XEvent ev;
        
        KeyCode f1 = XKeysymToKeycode&#40;dpy, XStringToKeysym&#40;"F1"&#41;&#41;;
        KeyCode enter = XKeysymToKeycode&#40;dpy, XStringToKeysym&#40;"Enter"&#41;&#41;;
        
        if&#40;!&#40;dpy = XOpenDisplay&#40;0x0&#41;&#41;&#41; return 1;
    
        root = DefaultRootWindow&#40;dpy&#41;;
    
        XGrabKey&#40;dpy, f1, Mod1Mask, root,
                True, GrabModeAsync, GrabModeAsync&#41;;
        XGrabButton&#40;dpy, 1, Mod1Mask, root, True, ButtonPressMask, GrabModeAsync,
                GrabModeAsync, None, None&#41;;
        XGrabButton&#40;dpy, 3, Mod1Mask, root, True, ButtonPressMask, GrabModeAsync,
                GrabModeAsync, None, None&#41;;
        XGrabKey&#40;dpy, enter, Mod1Mask, root,
        	True, GrabModeAsync, GrabModeAsync&#41;;
    
        for&#40;;;&#41;
        &#123;
            XNextEvent&#40;dpy, &ev&#41;;
            if&#40;ev.type == KeyPress && ev.xkey.keycode == enter&#41;
                &#123;
    	    XRaiseWindow&#40;dpy, ev.xkey.subwindow&#41;;
    	    &#125;
            else if&#40;ev.type == ButtonPress && ev.xbutton.subwindow != None&#41;
            &#123;
                XGrabPointer&#40;dpy, ev.xbutton.subwindow, True,
                        PointerMotionMask|ButtonReleaseMask, GrabModeAsync,
                        GrabModeAsync, None, None, CurrentTime&#41;;
                XGetWindowAttributes&#40;dpy, ev.xbutton.subwindow, &attr&#41;;
                start = ev.xbutton;
            &#125;
            else if&#40;ev.type == MotionNotify&#41;
            &#123;
                int xdiff, ydiff;
                while&#40;XCheckTypedEvent&#40;dpy, MotionNotify, &ev&#41;&#41;;
                xdiff = ev.xbutton.x_root - start.x_root;
                ydiff = ev.xbutton.y_root - start.y_root;
                XMoveResizeWindow&#40;dpy, ev.xmotion.window,
                    attr.x + &#40;start.button==1 ? xdiff &#58; 0&#41;,
                    attr.y + &#40;start.button==1 ? ydiff &#58; 0&#41;,
                    MAX&#40;1, attr.width + &#40;start.button==3 ? xdiff &#58; 0&#41;&#41;,
                    MAX&#40;1, attr.height + &#40;start.button==3 ? ydiff &#58; 0&#41;&#41;&#41;;
            &#125;
            else if&#40;ev.type == ButtonRelease&#41;
                XUngrabPointer&#40;dpy, CurrentTime&#41;;
        &#125;
    &#125;
    Make output:

    Code:
    cc -Os -pedantic -Wall -I/usr/X11R6/include -L/usr/X11R6/lib -lX11 -o hactwm hactwm.c
    hactwm.c&#58; In function `main'&#58;
    hactwm.c&#58;12&#58; warning&#58; `dpy' might be used uninitialized in this function
    Segmentation fault error is nothing special. 2572 is the code it gave, IIRC. I can't copy/paste it because I don't use GPM (or whatever it is), and can't pipe the startx output .

  10. #10
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Code:
    KeyCode f1 = XKeysymToKeycode&#40;dpy, XStringToKeysym&#40;"F1"&#41;&#41;;
        KeyCode enter = XKeysymToKeycode&#40;dpy, XStringToKeysym&#40;"Enter"&#41;&#41;;
    These appear before you initialize dpy

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •