]> code.delx.au - spectrwm/blobdiff - spectrwm.c
Add myself to the copyright
[spectrwm] / spectrwm.c
index 596db238d92ef2023adc0cab897e751180b00ec2..4f2399cdd07724d35d72b95e057f0a78b871641a 100644 (file)
@@ -8,6 +8,7 @@
  * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
  * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
  * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
+ * Copyright (c) 2012 David Hill <dhill@mindcry.org>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -647,7 +648,6 @@ struct ewmh_hint {
 };
 
 /* function prototypes */
-xcb_char2b_t *char2b(const char *);
 int     conf_load(char *, int);
 void    constrain_window(struct ws_win *, struct swm_region *, int);
 void    do_sync(void);
@@ -659,7 +659,7 @@ void         focus_magic(struct ws_win *);
 xcb_atom_t get_atom_from_string(const char *);
 xcb_screen_t   *get_screen(int);
 char   *get_win_name(xcb_window_t);
-uint16_t getstate(xcb_window_t);
+uint32_t getstate(xcb_window_t);
 void    grabbuttons(struct ws_win *, int);
 void    map_window_raised(xcb_window_t);
 void    new_region(struct swm_screen *, int, int, int, int);
@@ -672,26 +672,6 @@ void        update_window(struct ws_win *);
 char   *get_atom_name(xcb_atom_t);
 #endif
 
-/* function definitions */
-xcb_char2b_t *
-char2b(const char *str)
-{
-       xcb_char2b_t    *s;
-       size_t          i, len;
-
-       len = strlen(str);
-       s = malloc(len * sizeof(xcb_char2b_t));
-       if (!s)
-               return (NULL);
-
-       for (i = 0; i < len; i++) {
-               s[i].byte1 = '\0';
-               s[i].byte2 = str[i];
-       }
-
-       return (s);
-}
-
 int
 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
 {
@@ -841,15 +821,16 @@ teardown_ewmh(void)
                pc = xcb_get_property(conn, 0, screens[i].root, sup_check,
                    XCB_ATOM_WINDOW, 0, 1);
                pr = xcb_get_property_reply(conn, pc, NULL);
-               if (pr) {
+               if (!pr)
+                       continue;
+               if (pr->format == sup_check) {
                        id = *((xcb_window_t *)xcb_get_property_value(pr));
 
                        xcb_destroy_window(conn, id);
                        xcb_delete_property(conn, screens[i].root, sup_check);
                        xcb_delete_property(conn, screens[i].root, sup_list);
-
-                       free(pr);
                }
+               free(pr);
        }
 }
 
@@ -1068,7 +1049,7 @@ void
 dumpwins(struct swm_region *r, union arg *args)
 {
        struct ws_win                           *win;
-       uint16_t                                state;
+       uint32_t                                state;
        xcb_get_window_attributes_cookie_t      c;
        xcb_get_window_attributes_reply_t       *wa;
 
@@ -1394,7 +1375,8 @@ bar_print(struct swm_region *r, const char *s)
            DefaultVisual(display, r->s->idx),
            DefaultColormap(display, r->s->idx));
 
-       XftDrawStringUtf8(draw, &bar_font_color, bar_font, x, bar_font->height,
+       XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
+           (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
            (FcChar8 *)s, len);
 
        XftDrawDestroy(draw);
@@ -1912,7 +1894,7 @@ bar_setup(struct swm_region *r)
        if (bar_font == NULL)
                errx(1, "unable to open a font");
 
-       bar_height = bar_font->height + 4 * bar_border_width;
+       bar_height = bar_font->height + 2 * bar_border_width;
 
        if (bar_height < 1)
                bar_height = 1;
@@ -1983,10 +1965,10 @@ set_win_state(struct ws_win *win, uint16_t state)
            a_state, 32, 2, data);
 }
 
-uint16_t
+uint32_t
 getstate(xcb_window_t w)
 {
-       uint16_t                        result = 0;
+       uint32_t                        result = 0;
        xcb_get_property_cookie_t       c;
        xcb_get_property_reply_t        *r;
 
@@ -1994,7 +1976,8 @@ getstate(xcb_window_t w)
        r = xcb_get_property_reply(conn, c, NULL);
 
        if (r) {
-               result = *((uint16_t *)xcb_get_property_value(r));
+               if (r->type == a_state && r->format == 32 && r->length == 2)
+                       result = *((uint32_t *)xcb_get_property_value(r));
                free(r);
        }
 
@@ -2218,6 +2201,8 @@ fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
        event.response_type = XCB_KEY_RELEASE;
        xcb_send_event(conn, 1, win->id,
            XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
+
+       free(keycode);
 }
 
 void
@@ -2318,8 +2303,10 @@ find_window(xcb_window_t id)
                return (NULL);
 
        /* if we were looking for the parent return that window instead */
-       if (r->parent == 0 || r->root == r->parent)
+       if (r->parent == 0 || r->root == r->parent) {
+               free(r);
                return (NULL);
+       }
 
        /* look for parent */
        for (i = 0; i < num_screens; i++)
@@ -3749,12 +3736,10 @@ get_win_name(xcb_window_t win)
        if (xcb_icccm_get_wm_name_reply(conn, c, &r, NULL)) {
                if (r.name_len > 0) {
                        name = malloc(r.name_len + 1);
-                       if (!name) {
-                               xcb_icccm_get_text_property_reply_wipe(&r);
-                               return (NULL);
+                       if (name) {
+                               memcpy(name, r.name, r.name_len);
+                               name[r.name_len] = '\0';
                        }
-                       memcpy(name, r.name, r.name_len);
-                       name[r.name_len] = '\0';
                }
                xcb_icccm_get_text_property_reply_wipe(&r);
        }
@@ -3889,6 +3874,7 @@ search_win(struct swm_region *r, union arg *args)
        char                    s[8];
        FILE                    *lfile;
        size_t                  len;
+       XftDraw                 *draw;
        XGlyphInfo              info;
 
        DNPRINTF(SWM_D_MISC, "search_win\n");
@@ -3942,7 +3928,15 @@ search_win(struct swm_region *r, union arg *args)
                    XCB_GC_BACKGROUND | XCB_GC_GRAPHICS_EXPOSURES, gcv);
                map_window_raised(w);
 
-               xcb_image_text_8(conn, len, w, sw->gc, 2, bar_font->height, s);
+               draw = XftDrawCreate(display, w,
+                   DefaultVisual(display, r->s->idx),
+                   DefaultColormap(display, r->s->idx));
+
+               XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
+                   (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
+                   (FcChar8 *)s, len);
+
+               XftDrawDestroy(draw);
 
                DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
 
@@ -4291,7 +4285,6 @@ resize(struct ws_win *win, union arg *args)
        xcb_cursor_t            cursor;
        xcb_font_t              cursor_font;
        xcb_grab_pointer_cookie_t       gpc;
-       xcb_grab_pointer_reply_t        *gpr;
        xcb_query_pointer_reply_t       *xpr;
        xcb_generic_event_t             *evt;
        xcb_motion_notify_event_t       *mne;
@@ -4378,13 +4371,6 @@ resize(struct ws_win *win, union arg *args)
        gpc = xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
            XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
            XCB_CURRENT_TIME),
-       gpr = xcb_grab_pointer_reply(conn, gpc, NULL);
-       if (!gpr) {
-               xcb_free_cursor(conn, cursor);
-               xcb_close_font(conn, cursor_font);
-               free(xpr);
-               return;
-       }
 
        xcb_flush(conn);
        resizing = 1;
@@ -4464,7 +4450,6 @@ resize(struct ws_win *win, union arg *args)
        xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
        xcb_free_cursor(conn, cursor);
        xcb_close_font(conn, cursor_font);
-       free(gpr);
        free(xpr);
        DNPRINTF(SWM_D_EVENT, "resize: done\n");
 }
@@ -4493,7 +4478,6 @@ move(struct ws_win *win, union arg *args)
        xcb_font_t                      cursor_font;
        xcb_cursor_t                    cursor;
        xcb_grab_pointer_cookie_t       gpc;
-       xcb_grab_pointer_reply_t        *gpr;
        xcb_query_pointer_reply_t       *qpr;
        xcb_generic_event_t             *evt;
        xcb_motion_notify_event_t       *mne;
@@ -4561,12 +4545,6 @@ move(struct ws_win *win, union arg *args)
        gpc = xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
            XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
            XCB_WINDOW_NONE, cursor, XCB_CURRENT_TIME);
-       gpr = xcb_grab_pointer_reply(conn, gpc, NULL);
-       if (!gpr) {
-               xcb_free_cursor(conn, cursor);
-               xcb_close_font(conn, cursor_font);
-               return;
-       }
 
        /* get cursor offset from window root */
        qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
@@ -5518,7 +5496,7 @@ updatenumlockmask(void)
 {
        unsigned int                            i, j;
        xcb_get_modifier_mapping_reply_t        *modmap_r;
-       xcb_keycode_t                           *modmap, kc;
+       xcb_keycode_t                           *modmap, kc, *keycode;
 
        DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
        numlockmask = 0;
@@ -5532,10 +5510,11 @@ updatenumlockmask(void)
                        for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
                                kc = modmap[i * modmap_r->keycodes_per_modifier
                                    + j];
-
-                               if (kc == *((xcb_keycode_t *)xcb_key_symbols_get_keycode(syms,
-                                   XK_Num_Lock)))
+                               keycode = xcb_key_symbols_get_keycode(syms,
+                                               XK_Num_Lock);
+                               if (kc == *keycode)  
                                        numlockmask = (1 << i);
+                               free(keycode);
                        }
                }
                free(modmap_r);
@@ -5570,6 +5549,7 @@ grabkeys(void)
                                            kp->mod | modifiers[j],
                                            *code, XCB_GRAB_MODE_ASYNC,
                                            XCB_GRAB_MODE_ASYNC);
+                               free(code);
                }
        }
 }
@@ -6395,7 +6375,8 @@ window_get_pid(xcb_window_t win)
                goto tryharder;
        }
 
-       ret = *((pid_t *)xcb_get_property_value(pr));
+       if (pr->type == apid && pr->format == 32)
+               ret = *((pid_t *)xcb_get_property_value(pr));
        free(pr);
 
        return (ret);
@@ -6407,7 +6388,7 @@ tryharder:
        pr = xcb_get_property_reply(conn, pc, NULL);
        if (!pr)
                return (0);
-       if (pr->type != XCB_ATOM_STRING) {
+       if (pr->type != apid) {
                free(pr);
                return (0);
        }
@@ -6431,7 +6412,9 @@ get_ws_idx(xcb_window_t id)
                xcb_get_property(conn, 0, id, a_swm_ws,
                    XCB_ATOM_STRING, 0, SWM_PROPLEN),
                NULL);
-       if (gpr) {
+       if (!gpr)
+               return (-1);
+       if (gpr->type) {
                proplen = xcb_get_property_value_length(gpr);
                if (proplen > 0) {
                        prop = malloc(proplen + 1);
@@ -6442,8 +6425,8 @@ get_ws_idx(xcb_window_t id)
                                prop[proplen] = '\0';
                        }
                }
-               free(gpr);
        }
+       free(gpr);
 
        if (prop) {
                DNPRINTF(SWM_D_PROP, "get_ws_idx: _SWM_WS: %s\n", prop);
@@ -6452,6 +6435,7 @@ get_ws_idx(xcb_window_t id)
                        DNPRINTF(SWM_D_PROP, "get_ws_idx: window: #%s: %s",
                            errstr, prop);
                }
+               free(prop);
        }
 
        return ws_idx;
@@ -6470,6 +6454,7 @@ manage_window(xcb_window_t id)
        struct quirk            *qp;
        uint32_t                event_mask, i;
        xcb_icccm_get_wm_protocols_reply_t      wpr;
+       xcb_icccm_get_wm_class_reply_t          tmpch;
 
        if ((win = find_window(id)) != NULL)
                return (win);   /* already being managed */
@@ -6635,7 +6620,12 @@ manage_window(xcb_window_t id)
 
        if (xcb_icccm_get_wm_class_reply(conn,
            xcb_icccm_get_wm_class(conn, win->id),
-           &win->ch, NULL)) {
+           &tmpch, NULL)) {
+               win->ch.class_name = tmpch.class_name;
+               win->ch.instance_name = tmpch.instance_name;
+
+               xcb_get_wm_class_reply_wipe(&tmpch);
+
                DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
                    win->ch.class_name, win->ch.instance_name);
 
@@ -6714,9 +6704,7 @@ free_window(struct ws_win *win)
 
        if (win->wa)
                free(win->wa);
-
-       xcb_icccm_get_wm_class_reply_wipe(&win->ch);
-
+       
        kill_refs(win);
 
        /* paint memory */
@@ -7103,7 +7091,7 @@ maprequest(xcb_map_request_event_t *e)
 char *
 get_atom_name(xcb_atom_t atom)
 {
-       char                            *name;
+       char                            *name = NULL;
        size_t                          len;
        xcb_get_atom_name_reply_t       *r;
 
@@ -7117,14 +7105,12 @@ get_atom_name(xcb_atom_t atom)
                        if (name) {
                                memcpy(name, xcb_get_atom_name_name(r), len);
                                name[len] = '\0';
-
-                               return name;
                        }
                }
                free(r);
        }
 
-       return NULL;
+       return (name);
 }
 #endif
 
@@ -7410,8 +7396,9 @@ scan_xrandr(int i)
                        return;
                } else
                        ncrtc = srr->num_crtcs;
+
+               crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
                for (c = 0; c < ncrtc; c++) {
-                       crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
                        cic = xcb_randr_get_crtc_info(conn, crtc[c],
                            XCB_CURRENT_TIME);
                        cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);