Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > Xlib programming (Coding my own WM)

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Closed Thread
 
Thread Tools Display Modes
Old 05-27-2005   #1 (permalink)
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(a, b) ((a) > (b) ? (a) : (b))

int main()
{
    Display * dpy;
    Window root;
    XWindowAttributes attr;
    XButtonEvent start;
    XEvent ev;

    if(!(dpy = XOpenDisplay(0x0))) return 1;

    root = DefaultRootWindow(dpy);

    XGrabKey(dpy, XKeysymToKeycode(dpy, XStringToKeysym("F1")), Mod1Mask, root,
            True, GrabModeAsync, GrabModeAsync);
    XGrabButton(dpy, 1, Mod1Mask, root, True, ButtonPressMask, GrabModeAsync,
            GrabModeAsync, None, None);
    XGrabButton(dpy, 3, Mod1Mask, root, True, ButtonPressMask, GrabModeAsync,
            GrabModeAsync, None, None);

    for(;;)
    {
        XNextEvent(dpy, &ev);
        if(ev.type == KeyPress && ev.xkey.subwindow != None)
            XRaiseWindow(dpy, ev.xkey.subwindow);
        else if(ev.type == ButtonPress && ev.xbutton.subwindow != None)
        {
            XGrabPointer(dpy, ev.xbutton.subwindow, True,
                    PointerMotionMask|ButtonReleaseMask, GrabModeAsync,
                    GrabModeAsync, None, None, CurrentTime);
            XGetWindowAttributes(dpy, ev.xbutton.subwindow, &attr);
            start = ev.xbutton;
        }
        else if(ev.type == MotionNotify)
        {
            int xdiff, ydiff;
            while(XCheckTypedEvent(dpy, MotionNotify, &ev));
            xdiff = ev.xbutton.x_root - start.x_root;
            ydiff = ev.xbutton.y_root - start.y_root;
            XMoveResizeWindow(dpy, ev.xmotion.window,
                attr.x + (start.button==1 ? xdiff : 0),
                attr.y + (start.button==1 ? ydiff : 0),
                MAX(1, attr.width + (start.button==3 ? xdiff : 0)),
                MAX(1, attr.height + (start.button==3 ? ydiff : 0)));
        }
        else if(ev.type == ButtonRelease)
            XUngrabPointer(dpy, CurrentTime);
    }
}
invertedpanda is offline  


Old 05-27-2005   #2 (permalink)
Linux Enthusiast
 
Join Date: Jan 2005
Posts: 575
Quote:
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.
Santa's little helper is offline  
Old 05-27-2005   #3 (permalink)
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..
invertedpanda is offline  
Old 05-27-2005   #4 (permalink)
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.
Santa's little helper is offline  
Old 05-27-2005   #5 (permalink)
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?
invertedpanda is offline  
Old 05-27-2005   #6 (permalink)
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.
Santa's little helper is offline  
Old 05-28-2005   #7 (permalink)
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(ev == KeyPress && ev.xkey.keycode == enter)
{
...
}
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.
invertedpanda is offline  
Old 05-28-2005   #8 (permalink)
Linux Enthusiast
 
Join Date: Jan 2005
Posts: 575
Can you give us the new listing and the exact error message you're getting ?
Santa's little helper is offline  
Old 05-28-2005   #9 (permalink)
Just Joined!
 
Join Date: May 2005
Posts: 10
Current Source:

Code:
#include <X11/Xlib.h>

#define MAX(a, b) ((a) > (b) ? (a) : (b))

int main()
{
    Display *dpy;
    Window root;
    XWindowAttributes attr;
    XButtonEvent start;
    XEvent ev;
    
    KeyCode f1 = XKeysymToKeycode(dpy, XStringToKeysym("F1"));
    KeyCode enter = XKeysymToKeycode(dpy, XStringToKeysym("Enter"));
    
    if(!(dpy = XOpenDisplay(0x0))) return 1;

    root = DefaultRootWindow(dpy);

    XGrabKey(dpy, f1, Mod1Mask, root,
            True, GrabModeAsync, GrabModeAsync);
    XGrabButton(dpy, 1, Mod1Mask, root, True, ButtonPressMask, GrabModeAsync,
            GrabModeAsync, None, None);
    XGrabButton(dpy, 3, Mod1Mask, root, True, ButtonPressMask, GrabModeAsync,
            GrabModeAsync, None, None);
    XGrabKey(dpy, enter, Mod1Mask, root,
    	True, GrabModeAsync, GrabModeAsync);

    for(;;)
    {
        XNextEvent(dpy, &ev);
        if(ev.type == KeyPress && ev.xkey.keycode == enter)
            {
	    XRaiseWindow(dpy, ev.xkey.subwindow);
	    }
        else if(ev.type == ButtonPress && ev.xbutton.subwindow != None)
        {
            XGrabPointer(dpy, ev.xbutton.subwindow, True,
                    PointerMotionMask|ButtonReleaseMask, GrabModeAsync,
                    GrabModeAsync, None, None, CurrentTime);
            XGetWindowAttributes(dpy, ev.xbutton.subwindow, &attr);
            start = ev.xbutton;
        }
        else if(ev.type == MotionNotify)
        {
            int xdiff, ydiff;
            while(XCheckTypedEvent(dpy, MotionNotify, &ev));
            xdiff = ev.xbutton.x_root - start.x_root;
            ydiff = ev.xbutton.y_root - start.y_root;
            XMoveResizeWindow(dpy, ev.xmotion.window,
                attr.x + (start.button==1 ? xdiff : 0),
                attr.y + (start.button==1 ? ydiff : 0),
                MAX(1, attr.width + (start.button==3 ? xdiff : 0)),
                MAX(1, attr.height + (start.button==3 ? ydiff : 0)));
        }
        else if(ev.type == ButtonRelease)
            XUngrabPointer(dpy, CurrentTime);
    }
}
Make output:

Code:
cc -Os -pedantic -Wall -I/usr/X11R6/include -L/usr/X11R6/lib -lX11 -o hactwm hactwm.c
hactwm.c: In function `main':
hactwm.c:12: warning: `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 .
invertedpanda is offline  
Old 05-28-2005   #10 (permalink)
Linux Enthusiast
 
Join Date: Jan 2005
Posts: 575
Code:
KeyCode f1 = XKeysymToKeycode(dpy, XStringToKeysym("F1"));
    KeyCode enter = XKeysymToKeycode(dpy, XStringToKeysym("Enter"));
These appear before you initialize dpy
Santa's little helper is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 02:13 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2