diff options
author | sabetts <sabetts> | 2000-09-07 16:49:23 +0000 |
---|---|---|
committer | sabetts <sabetts> | 2000-09-07 16:49:23 +0000 |
commit | 60767d1215233461273d4d16df8ff52cf15b4010 (patch) | |
tree | d62f90a2f202366b829f274fc21458ab70df6730 /src | |
parent | 33fb61429337fc71292628e9dac1ddf599938304 (diff) | |
download | ratpoison-60767d1215233461273d4d16df8ff52cf15b4010.zip |
restructured tree, added automake and autoconf to the build process.
Diffstat (limited to 'src')
-rw-r--r-- | src/.cvsignore | 8 | ||||
-rw-r--r-- | src/Makefile.am | 5 | ||||
-rw-r--r-- | src/bar.c | 141 | ||||
-rw-r--r-- | src/bar.h | 29 | ||||
-rw-r--r-- | src/conf.h | 44 | ||||
-rw-r--r-- | src/data.h | 78 | ||||
-rw-r--r-- | src/events.c | 473 | ||||
-rw-r--r-- | src/events.h | 29 | ||||
-rw-r--r-- | src/input.c | 75 | ||||
-rw-r--r-- | src/input.h | 1 | ||||
-rw-r--r-- | src/list.c | 271 | ||||
-rw-r--r-- | src/list.h | 35 | ||||
-rw-r--r-- | src/main.c | 261 | ||||
-rw-r--r-- | src/manage.c | 175 | ||||
-rw-r--r-- | src/manage.h | 30 | ||||
-rw-r--r-- | src/number.c | 130 | ||||
-rw-r--r-- | src/number.h | 3 | ||||
-rw-r--r-- | src/ratpoison.h | 40 |
18 files changed, 1828 insertions, 0 deletions
diff --git a/src/.cvsignore b/src/.cvsignore new file mode 100644 index 0000000..29c83c0 --- /dev/null +++ b/src/.cvsignore @@ -0,0 +1,8 @@ +Makefile.in +Makefile +*.in +ratpoison +config.h +stamp-h +stamp-h.in +.deps diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..d3af0be --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,5 @@ +bin_PROGRAMS = ratpoison + +ratpoison_SOURCES = bar.c bar.h conf.h data.h events.c events.h \ +input.c input.h list.c list.h main.c manage.c manage.h number.c \ +number.h ratpoison.h diff --git a/src/bar.c b/src/bar.c new file mode 100644 index 0000000..d06dcc8 --- /dev/null +++ b/src/bar.c @@ -0,0 +1,141 @@ +/* Functionality for a bar across the bottom of the screen listing the + * windows currently managed. + * + * Copyright (C) 2000 Shawn Betts + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA */ + +#include <X11/X.h> +#include <X11/Xlib.h> +#include <X11/Xutil.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "ratpoison.h" + +int +hide_bar (screen_info *s) +{ + if (s->bar_is_raised) + { + s->bar_is_raised = 0; + XUnmapWindow (dpy, s->bar_window); + return 1; + } + + return 0; +} + +int +show_bar (screen_info *s) +{ + if (!s->bar_is_raised) + { + s->bar_is_raised = 1; + XMapWindow (dpy, s->bar_window); + update_window_names (s); + + /* Set an alarm to auto-hide the bar BAR_TIMEOUT seconds later */ + alarm (BAR_TIMEOUT); + return 1; + } + + return 0; +} + +/* Toggle the display of the program bar */ +void +toggle_bar (screen_info *s) +{ + if (!hide_bar (s)) show_bar (s); +} + +static int +calc_bar_width (XFontStruct *font) +{ + char str[100]; /* window names are capped at 99 chars */ + int size = 1; + rp_window *cur; + + for (cur = rp_window_head; cur; cur = cur->next) + { + if (cur->state == STATE_UNMAPPED) continue; + + sprintf (str, "%d-%s", cur->number, cur->name); + size += 10 + XTextWidth (font, str, strlen (str)); + } + + return size; +} + +int +bar_x (screen_info *s, int width) +{ + if (BAR_LOCATION >= 2) return s->root_attr.width - width; + else return 0; +} + +int +bar_y (screen_info *s) +{ + if (BAR_LOCATION % 2) return 0; + else return s->root_attr.height - (FONT_HEIGHT (s->font) + BAR_PADDING * 2) - 2; +} + +void +update_window_names (screen_info *s) +{ + char str[100]; /* window names are capped at 99 chars */ + int width = calc_bar_width (s->font); + rp_window *cur; + int cur_x = 5; + + if (!s->bar_is_raised) return; + + XMoveResizeWindow (dpy, s->bar_window, + bar_x (s, width), bar_y (s), + width, + (FONT_HEIGHT (s->font) + BAR_PADDING * 2)); + XClearWindow (dpy, s->bar_window); + XRaiseWindow (dpy, s->bar_window); + + if (rp_window_head == NULL) return; + + /* Draw them in reverse order they were added in, so the oldest + windows appear on the left and the newest on the right end of the + program bar. */ + for (cur = rp_window_head; cur; cur = cur->next) + { + if (cur->state == STATE_UNMAPPED) continue; + + sprintf (str, "%d-%s", cur->number, cur->name); + if ( rp_current_window == cur) + { + XDrawString (dpy, s->bar_window, s->bold_gc, cur_x, + BAR_PADDING + s->font->max_bounds.ascent, str, strlen (str)); + } + else + { + XDrawString (dpy, s->bar_window, s->normal_gc, cur_x, + BAR_PADDING + s->font->max_bounds.ascent, str, strlen (str)); + } + + cur_x += 10 + XTextWidth (s->font, str, strlen (str)); + } +} diff --git a/src/bar.h b/src/bar.h new file mode 100644 index 0000000..6083762 --- /dev/null +++ b/src/bar.h @@ -0,0 +1,29 @@ +/* functions for managing the program bar + * + * Copyright (C) 2000 Shawn Betts + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA */ + +#ifndef _BAR_H +#define _BAR_H + +void update_window_names (screen_info *s); +void toggle_bar (screen_info *s); +int show_bar (screen_info *s); +int hide_bar (screen_info *s); +int bar_y (screen_info *s); +int bar_x (screen_info *s, int width); +#endif _BAR_H diff --git a/src/conf.h b/src/conf.h new file mode 100644 index 0000000..8efee2e --- /dev/null +++ b/src/conf.h @@ -0,0 +1,44 @@ +/* Config file for ratpoison. Edit these values and recompile. + * + * Copyright (C) 2000 Shawn Betts + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA */ + +#define KEY_PREFIX 't' +#define MODIFIER_PREFIX ControlMask + +#define KEY_XTERM 'c' +#define KEY_EMACS 'e' +#define KEY_PREVWINDOW 'p' +#define KEY_NEXTWINDOW 'n' +#define KEY_LASTWINDOW 't' /* key to toggle between the current window and the last visitted one */ +#define KEY_TOGGLEBAR 'w' /* key to toggle the display of the program bar */ +#define KEY_DELETE 'k' /* delete a window SHIFT+key will Destroy the window */ +#define KEY_WINBYNAME '\'' /* key to jump to a window by name */ +#define KEY_RENAME 'a' /* key to rename a window. */ + +#define TERM_PROG "xterm" /* command to boot an x term */ +#define EMACS_PROG "emacs" /* command to boot emacs */ + +#define BAR_FG_COLOR "Gray60" +#define BAR_BG_COLOR "Lightgreen" +#define BAR_BOLD_COLOR "Black" /* To indicate the current window */ + +#define FONT_NAME "fixed" /* The font you wish to use */ +#define BAR_PADDING 3 /* The amount of padding on the top and bottom of the program bar */ +#define BAR_LOCATION 3 /* 0=bottom-left 1=top-left 2=bottom-right 3=top-right */ +#define BAR_TIMEOUT 5 /* Number of seconds before the progam bar autohides 0=don't autohide */ + diff --git a/src/data.h b/src/data.h new file mode 100644 index 0000000..5623a58 --- /dev/null +++ b/src/data.h @@ -0,0 +1,78 @@ +/* our datatypes and global variables + * + * Copyright (C) 2000 Shawn Betts + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA */ + +#ifndef _DATA_H +#define _DATA_H + +#include <X11/X.h> +#include <X11/Xlib.h> + +#define FONT_HEIGHT(f) ((f)->max_bounds.ascent + (f)->max_bounds.descent) + +#define STATE_UNMAPPED 0 +#define STATE_MAPPED 1 + + +typedef struct rp_window rp_window; +typedef struct screen_info screen_info; + +struct rp_window +{ + screen_info *scr; + Window w; + int number; + char *name; + int state; + int last_access; + int named; + rp_window *next, *prev; +}; + +struct screen_info +{ + GC normal_gc; + GC bold_gc; + XFontStruct *font; /* The font we want to use. */ + XWindowAttributes root_attr; + Window root, bar_window, key_window, input_window; + int bar_is_raised; + int screen_num; /* Our screen number as dictated my X */ + Colormap def_cmap; +}; + +extern rp_window *rp_window_head, *rp_window_tail; +extern rp_window *rp_current_window; +extern screen_info *screens; +extern int num_screens; + +extern Display *dpy; +extern Atom rp_restart; + +extern Atom wm_state; +extern Atom wm_change_state; +extern Atom wm_protocols; +extern Atom wm_delete; +extern Atom wm_take_focus; +extern Atom wm_colormaps; + +/* Set to 1 to indicate that the WM should exit at it's earliest + convenience. */ +extern int exit_signal; + +#endif /* _DATA_H */ diff --git a/src/events.c b/src/events.c new file mode 100644 index 0000000..a835eb3 --- /dev/null +++ b/src/events.c @@ -0,0 +1,473 @@ +/* Copyright (C) 2000 Shawn Betts + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA */ + +#include <X11/X.h> +#include <X11/Xlib.h> +#include <X11/Xutil.h> +#include <X11/Xatom.h> +#include <X11/keysymdef.h> + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/wait.h> + +#include "ratpoison.h" + +extern Display *dpy; + +void +spawn(char *prog) +{ + /* + * ugly dance to avoid leaving zombies. Could use SIGCHLD, + * but it's not very portable. + */ + if (fork() == 0) { + if (fork() == 0) { + putenv(DisplayString(dpy)); + execlp(prog, prog, 0); + fprintf(stderr, "ratpoison: exec %s ", prog); + perror(" failed"); + exit(EXIT_FAILURE); + } + exit(0); + } + wait((int *) 0); +#ifdef DEBUG + printf ("spawned %s\n", prog); +#endif +} + +void +new_window (XCreateWindowEvent *e) +{ + rp_window *win; + screen_info *s; + + if (e->override_redirect) return; + + s = find_screen (e->parent); + win = find_window (e->window); + + if (s && !win && e->window != s->key_window && e->window != s->bar_window + && e->window != s->input_window) + { + win = add_to_window_list (s, e->window); + win->state = STATE_UNMAPPED; + } +} + +void +unmap_notify (XEvent *ev) +{ + screen_info *s; + rp_window *win; + + s = find_screen (ev->xunmap.event); + win = find_window (ev->xunmap.window); + + if (s && win) + { + /* Give back the window number. the window will get another one, + if it in remapped. */ + return_window_number (win->number); + win->number = -1; + win->state = STATE_UNMAPPED; + update_window_names (s); + } +} + +void +map_request (XEvent *ev) +{ + screen_info *s; + rp_window *win; + + s = find_screen (ev->xmap.event); + win = find_window (ev->xmap.window); + + if (s && win) + { + switch (win->state) + { + case STATE_UNMAPPED: + manage (win, s); + case STATE_MAPPED: + XMapRaised (dpy, win->w); + rp_current_window = win; + set_active_window (rp_current_window); + } + } + else + { + printf ("Not managed.\n"); + XMapWindow (dpy, ev->xmap.window); + } +} + +int +more_destroy_events () +{ + XEvent ev; + + if (XCheckTypedEvent (dpy, DestroyNotify, &ev)) + { + XPutBackEvent (dpy, &ev); + return 1; + } + return 0; +} + +void +destroy_window (XDestroyWindowEvent *ev) +{ + /* if there are multiple destroy events queued, and a mapped window + is deleted then switch_window_pending is set to 1 and the window + switch is done after all destroy events have been done. */ + static int switch_window_pending = 0; + int last_destroy_event; + rp_window *win; + + win = find_window (ev->window); + + last_destroy_event = !more_destroy_events(); + if (win) + { + /* Goto the last accessed window. */ + if (win == rp_current_window) + { + printf ("Destroying current window.\n"); + + /* tell ratpoison to switch to the last window when all the + destroy events have been delt with. */ + switch_window_pending = 1; + unmanage (win); + } + else + { + printf ("Destroying some other window.\n"); + unmanage (win); + } + } + + if (last_destroy_event && switch_window_pending) + { + last_window (); + switch_window_pending = 0; + } +} + +void +configure_request (XConfigureRequestEvent *e) +{ + XConfigureEvent ce; + rp_window *win; + + win = find_window (e->window); + + if (win) + { + ce.type = ConfigureNotify; + ce.event = e->window; + ce.window = e->window; + ce.x = 0; + ce.y = 0; + ce.width = win->scr->root_attr.width; + ce.height = win->scr->root_attr.height; + ce.border_width = 0; + ce.above = None; + ce.override_redirect = 0; + + if (e->value_mask & CWStackMode && win->state == STATE_MAPPED) + { + if (e->detail == Above) + { + rp_current_window = win; + set_active_window (rp_current_window); + } + else if (e->detail == Below && win == rp_current_window) + { + last_window (); + } + } + + XSendEvent(dpy, win->w, False, StructureNotifyMask, (XEvent*)&ce); + } +} + +void +delete_window () +{ + XEvent ev; + int status; + + if (rp_current_window == NULL) return; + + ev.xclient.type = ClientMessage; + ev.xclient.window = rp_current_window->w; + ev.xclient.message_type = wm_protocols; + ev.xclient.format = 32; + ev.xclient.data.l[0] = wm_delete; + ev.xclient.data.l[1] = CurrentTime; + + status = XSendEvent(dpy, rp_current_window->w, False, 0, &ev); + if (status == 0) fprintf(stderr, "ratpoison: XSendEvent failed\n"); +} + +void +kill_window () +{ + if (rp_current_window == NULL) return; + + XKillClient(dpy, rp_current_window->w); +} + +static void +client_msg (XClientMessageEvent *ev) +{ + printf ("Recieved client message.\n"); +} + +static void +goto_win_by_name (screen_info *s) +{ + char winname[100]; + + get_input (s, "Window: ", winname, 100); + printf ("user entered: %s\n", winname); + + goto_window_name (winname); +} + +static void +handle_key (screen_info *s) +{ + int revert; + Window fwin; + XEvent ev; + int keysym; + +#ifdef DEBUG + printf ("handling key.\n"); +#endif + + XGetInputFocus (dpy, &fwin, &revert); + XSetInputFocus (dpy, s->key_window, RevertToPointerRoot, CurrentTime); + XMaskEvent (dpy, KeyPressMask, &ev); + XSetInputFocus (dpy, fwin, revert, CurrentTime); + + if (XLookupKeysym((XKeyEvent *) &ev, 0) == KEY_PREFIX && !ev.xkey.state) + { + /* Generate the prefix keystroke for the app */ + ev.xkey.window = fwin; + ev.xkey.state = MODIFIER_PREFIX; + XSendEvent (dpy, fwin, False, KeyPressMask, &ev); + XSync (dpy, False); + return; + } + + keysym = XLookupKeysym((XKeyEvent *) &ev, 0); + + if (keysym == KEY_TOGGLEBAR) + { + toggle_bar (s); + return; + } + + /* All functions tested for after this point hide the program bar. */ + hide_bar (s); + + if (keysym >= '0' && keysym <= '9') + { + goto_window_number (XLookupKeysym((XKeyEvent *) &ev, 0) - '0'); + hide_bar (s); + return; + } + + switch (keysym) + { + case KEY_XTERM: + spawn (TERM_PROG); + break; + case KEY_EMACS: + spawn (EMACS_PROG); + break; + case KEY_PREVWINDOW: + prev_window (); + break; + case KEY_NEXTWINDOW: + next_window (); + break; + case KEY_LASTWINDOW: + last_window (); + break; + case KEY_WINBYNAME: + goto_win_by_name (s); + break; + case KEY_RENAME: + rename_current_window (); + break; + case KEY_DELETE: + if (ev.xkey.state & ShiftMask) kill_window (); + else delete_window (); + break; + default: + fprintf (stderr, "Unknown key command '%c'\n", (char)keysym); + break; + } +} + +void +key_press (XEvent *ev) +{ + screen_info *s; + unsigned int modifier = ev->xkey.state; + int ks = XLookupKeysym((XKeyEvent *) ev, 0); + + s = find_screen (ev->xkey.root); + + if (s && ks == KEY_PREFIX && (modifier & MODIFIER_PREFIX)) + { + handle_key (s); + } +} + +void +property_notify (XEvent *ev) +{ + rp_window *win; + + printf ("atom: %ld\n", ev->xproperty.atom); + + win = find_window (ev->xproperty.window); + + if (win) + { + if (ev->xproperty.atom == XA_WM_NAME) + { + printf ("updating window name\n"); + if (update_window_name (win)) + { + update_window_names (win->scr); + } + } + } +} + +/* Given an event, call the correct function to handle it. */ +void +delegate_event (XEvent *ev) +{ + switch (ev->type) + { + case ConfigureRequest: + printf ("ConfigureRequest\n"); + configure_request (&ev->xconfigurerequest); + break; + case CirculateRequest: + printf ("CirculateRequest\n"); + break; + case CreateNotify: + printf ("CreateNotify\n"); + new_window (&ev->xcreatewindow); + break; + case DestroyNotify: + printf ("DestroyNotify\n"); + destroy_window (&ev->xdestroywindow); + break; + case ClientMessage: + client_msg (&ev->xclient); + printf ("ClientMessage\n"); + break; + case ColormapNotify: + printf ("ColormapNotify\n"); + break; + case PropertyNotify: + printf ("PropertyNotify\n"); + property_notify (ev); + break; + case SelectionClear: + printf ("SelectionClear\n"); + break; + case SelectionNotify: + printf ("SelectionNotify\n"); + break; + case SelectionRequest: + printf ("SelectionRequest\n"); + break; + case EnterNotify: + printf ("EnterNotify\n"); + break; + case ReparentNotify: + printf ("ReparentNotify\n"); + break; + case FocusIn: + printf ("FocusIn\n"); + break; + + case MapRequest: + printf ("MapRequest\n"); + map_request (ev); + break; + + case KeyPress: + printf ("KeyPress\n"); + key_press (ev); + break; + + case UnmapNotify: + printf ("UnmapNotify\n"); + unmap_notify (ev); + break; + + case MotionNotify: + printf ("MotionNotify\n"); + break; + case Expose: + printf ("Expose\n"); + break; + case FocusOut: + printf ("FocusOut\n"); + break; + case ConfigureNotify: + printf ("ConfigureNotify\n"); + break; + case MapNotify: + printf ("MapNotify\n"); + break; + case MappingNotify: + printf ("MappingNotify\n"); + break; + default: + printf ("Unhandled event %d\n", ev->type); + } +} + +void +handle_events () +{ + XEvent ev; + + for (;;) + { + XNextEvent (dpy, &ev); + delegate_event (&ev); + } +} + + diff --git a/src/events.h b/src/events.h new file mode 100644 index 0000000..8751e68 --- /dev/null +++ b/src/events.h @@ -0,0 +1,29 @@ +/* Function prototypes + * + * Copyright (C) 2000 Shawn Betts + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA */ + +#ifndef _EVENTS_H +#define _EVENTS_H + +void handle_events (); +void delegate_event (XEvent *ev); +void key_press (XEvent *ev); +void map_request (XEvent *ev); +void unmap_notify (XEvent *ev); + +#endif _EVENTS_H diff --git a/src/input.c b/src/input.c new file mode 100644 index 0000000..ad8f093 --- /dev/null +++ b/src/input.c @@ -0,0 +1,75 @@ +/* for reading kdb input from the user. Currently only used to read in + the name of a window to jump to. */ + +#include <X11/keysym.h> +#include <stdlib.h> +#include <stdio.h> + +#include "ratpoison.h" + + +static int +read_key () +{ + XEvent ev; + + XMaskEvent (dpy, KeyPressMask, &ev); + return XLookupKeysym ((XKeyEvent *)&ev, 0); +} + +/* pass in a pointer a string and how much room we have, and fill it + in with text from the user. */ +void +get_input (screen_info *s, char *prompt, char *str, int len) +{ + int cur_len; /* Current length of the string. */ + int ch; + int revert; + Window fwin; + int prompt_width = XTextWidth (s->font, prompt, strlen (prompt)); + int width = 100 + prompt_width; + + /* We don't want to draw overtop of the program bar. */ + hide_bar (s); + + XMapWindow (dpy, s->input_window); + XMoveResizeWindow (dpy, s->input_window, + bar_x (s, width), bar_y (s), width, (FONT_HEIGHT (s->font) + BAR_PADDING * 2)); + XClearWindow (dpy, s->input_window); + XRaiseWindow (dpy, s->input_window); + + /* draw the window prompt. */ + XDrawString (dpy, s->input_window, s->bold_gc, 5, + BAR_PADDING + s->font->max_bounds.ascent, prompt, strlen (prompt)); + + XGetInputFocus (dpy, &fwin, &revert); + XSetInputFocus (dpy, s->input_window, RevertToPointerRoot, CurrentTime); + + cur_len = 0; + while ((ch = read_key ()) != XK_Return) + { + printf ("key %d\n", ch); + if (ch == XK_BackSpace) + { + if (cur_len > 0) cur_len--; + XClearWindow (dpy, s->input_window); + XDrawString (dpy, s->input_window, s->bold_gc, 5, + BAR_PADDING + s->font->max_bounds.ascent, prompt, strlen (prompt)); + XDrawString (dpy, s->input_window, s->bold_gc, 5 + prompt_width, + BAR_PADDING + s->font->max_bounds.ascent, str, cur_len); + } + else if (ch >= ' ') + { + str[cur_len] = ch; + if (cur_len < len - 1) cur_len++; + + XDrawString (dpy, s->input_window, s->bold_gc, 5 + prompt_width, + BAR_PADDING + s->font->max_bounds.ascent, str, cur_len); + } + } + + str[cur_len] = 0; + XSetInputFocus (dpy, fwin, RevertToPointerRoot, CurrentTime); + XUnmapWindow (dpy, s->input_window); +} + diff --git a/src/input.h b/src/input.h new file mode 100644 index 0000000..9d49bbf --- /dev/null +++ b/src/input.h @@ -0,0 +1 @@ +void get_input (screen_info *s, char *prompt, char *str, int len); diff --git a/src/list.c b/src/list.c new file mode 100644 index 0000000..ee04b34 --- /dev/null +++ b/src/list.c @@ -0,0 +1,271 @@ +/* functions for handling the window list + * + * Copyright (C) 2000 Shawn Betts + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <ctype.h> + +#include "ratpoison.h" + +rp_window *rp_window_head, *rp_window_tail; +rp_window *rp_current_window; + +rp_window * +add_to_window_list (screen_info *s, Window w) +{ + rp_window *new_window; + + new_window = malloc (sizeof (rp_window)); + if (new_window == NULL) + { + fprintf (stderr, "list.c:add_to_window_list():Out of memory!\n"); + exit (EXIT_FAILURE); + } + new_window->w = w; + new_window->scr = s; + new_window->last_access = 0; + new_window->prev = NULL; + new_window->state = STATE_UNMAPPED; + new_window->number = -1; + new_window->named = 0; + + if ((new_window->name = malloc (strlen ("Unnamed") + 1)) == NULL) + { + fprintf (stderr, "list.c:add_to_window_list():Out of memory.\n"); + exit (EXIT_FAILURE); + } + strcpy (new_window->name, "Unnamed"); + + if (rp_window_head == NULL) + { + /* The list is empty. */ + rp_window_head = new_window; + rp_window_tail = new_window; + new_window->next = NULL; + return new_window; + } + + /* Add the window to the head of the list. */ + new_window->next = rp_window_head; + rp_window_head->prev = new_window; + rp_window_head = new_window; + + return new_window; +} + +/* Check to see if the window is already in our list of managed windows. */ +rp_window * +find_window (Window w) +{ + rp_window *cur; + + for (cur = rp_window_head; cur; cur = cur->next) + if (cur->w == w) return cur; + + return NULL; +} + +void +remove_from_window_list (rp_window *w) +{ + if (rp_window_head == w) rp_window_head = w->next; + if (rp_window_tail == w) rp_window_tail = w->prev; + + if (w->prev != NULL) w->prev->next = w->next; + if (w->next != NULL) w->next->prev = w->prev; + + /* set rp_current_window to NULL, so a dangling pointer is not + left. */ + if (rp_current_window == w) rp_current_window = NULL; + + free (w); +#ifdef DEBUG + printf ("Removed window from list.\n"); +#endif +} + +void +set_current_window (rp_window *win) +{ + rp_current_window = win; +} + +void +init_window_list () +{ + rp_window_head = rp_window_tail = NULL; + rp_current_window = NULL; +} + +void +next_window () +{ + if (rp_current_window != NULL) + { + rp_current_window = rp_current_window->next; + if (rp_current_window == NULL) + { + rp_current_window = rp_window_head; + } + if (rp_current_window->state == STATE_UNMAPPED) next_window (); + set_active_window (rp_current_window); + } +} + +void +prev_window () +{ + if (rp_current_window != NULL) + { + set_current_window (rp_current_window->prev); + if (rp_current_window == NULL) + { + rp_current_window = rp_window_tail; + } + if (rp_current_window->state == STATE_UNMAPPED) prev_window (); + set_active_window (rp_current_window); + } +} + +rp_window * +find_window_by_number (int n) +{ + rp_window *cur; + + for (cur=rp_window_head; cur; cur=cur->next) + { + if (cur->state != STATE_MAPPED) continue; + + if (n == cur->number) return cur; + } + + return NULL; +} + +/* A case insensitive strncmp. */ +static int +str_comp (char *s1, char *s2, int len) +{ + int i; + + for (i=0; i<len; i++) + if (toupper (s1[i]) != toupper (s2[i])) return 0; + + return 1; +} + +static rp_window * +find_window_by_name (char *name) +{ + rp_window *cur; + + for (cur=rp_window_head; cur; cur=cur->next) + { + if (str_comp (name, cur->name, strlen (name))) return cur; + } + + return NULL; +} + +void +goto_window_name (char *name) +{ + rp_window *win; + + if ((win = find_window_by_name (name)) == NULL) + { + return; + } + + rp_current_window = win; + set_active_window (rp_current_window); +} + +void +goto_window_number (int n) +{ + rp_window *win; + + if ((win = find_window_by_number (n)) == NULL) + { + return; + } + + rp_current_window = win; + set_active_window (rp_current_window); +} + +rp_window * +find_last_accessed_window () +{ + int last_access = 0; + rp_window *cur, *most_recent; + + /* Find the first mapped window */ + for (most_recent = rp_window_head; most_recent; most_recent=most_recent->next) + { + if (most_recent->state == STATE_MAPPED) break; + } + + /* If there are no mapped windows, don't bother with the next part */ + if (most_recent == NULL) return NULL; + + for (cur=rp_window_head; cur; cur=cur->next) + { + if (cur->last_access >= last_access + && cur != rp_current_window + && cur->state == STATE_MAPPED) + { + most_recent = cur; + last_access = cur->last_access; + } + } + + return most_recent; +} + +void +last_window () +{ + rp_current_window = find_last_accessed_window (); + set_active_window (rp_current_window); +} + +void +set_active_window (rp_window *rp_w) +{ + static int counter = 1; /* increments every time this function + is called. This way we can track + which window was last accessed. */ + + if (rp_w == NULL) return; + + counter++; + rp_w->last_access = counter; + + if (rp_w->scr->bar_is_raised) update_window_names (rp_w->scr); + + XSetInputFocus (dpy, rp_w->w, + RevertToPointerRoot, CurrentTime); + XRaiseWindow (dpy, rp_w->w); + + /* Make sure the program bar is always on the top */ + update_window_names (rp_w->scr); +} diff --git a/src/list.h b/src/list.h new file mode 100644 index 0000000..170c2e2 --- /dev/null +++ b/src/list.h @@ -0,0 +1,35 @@ +/* functions for managing the window list + * + * Copyright (C) 2000 Shawn Betts + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA */ + +#ifndef _LIST_H +#define _LIST_H + +rp_window *add_to_window_list (screen_info *s, Window w); +void init_window_list (); +void remove_from_window_list (rp_window *w); +void next_window (); +void prev_window (); +void last_window (); +rp_window *find_window (Window w); +void maximize_current_window (); +void set_active_window (rp_window *rp_w); +void set_current_window (rp_window *win); +void goto_window_number (int n); +void goto_window_name (char *name); +#endif /* _LIST_H */ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..313d7fc --- /dev/null +++ b/src/main.c @@ -0,0 +1,261 @@ +/* + * Copyright (C) 2000 Shawn Betts + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA */ + +#include <X11/X.h> +#include <X11/Xlib.h> +#include <X11/Xutil.h> +#include <X11/Xatom.h> +#include <X11/Xproto.h> + +#include <stdio.h> +#include <stdlib.h> +#include <signal.h> +#include <unistd.h> + +#include "ratpoison.h" + +static void init_screen (screen_info *s, int screen_num); + +Atom wm_state; +Atom wm_change_state; +Atom wm_protocols; +Atom wm_delete; +Atom wm_take_focus; +Atom wm_colormaps; + +Atom rp_restart; + +screen_info *screens; +int num_screens; +Display *dpy; +int exit_signal = 0; /* Set by the signal handler. if this + is set, quit. */ +static XFontStruct *font; + +char **myargv; + +void +sighandler () +{ + fprintf (stderr, "ratpoison: Agg! I've been SHOT!\n"); + clean_up (); + exit (EXIT_FAILURE); +} + +void +hup_handler () +{ + /* This doesn't seem to restart more than once for some reason...*/ + + fprintf (stderr, "ratpoison: Restarting with a fresh plate.\n"); + clean_up (); + execvp(myargv[0], myargv); +} + +void +alrm_handler () +{ + int i; + +#ifdef DEBUG + printf ("alarm recieved.\n"); +#endif + + /* FIXME: should only hide 1 bar, but we hide them all. */ + for (i=0; i<num_screens; i++) + { + hide_bar (&screens[i]); + } + XSync (dpy, False); +} + +int +handler (Display *d, XErrorEvent *e) +{ + char error_msg[100]; + + if (e->request_code == X_ChangeWindowAttributes && e->error_code == BadAccess) { + fprintf(stderr, "ratpoison: There can be only ONE.\n"); + exit(EXIT_FAILURE); + } + + XGetErrorText (d, e->error_code, error_msg, sizeof (error_msg)); + fprintf (stderr, "ratpoison: %s!\n", error_msg); + + return 0; + // exit (EXIT_FAILURE); +} + +int +main (int argc, char *argv[]) +{ + int i; + + myargv = argv; + + if (!(dpy = XOpenDisplay (NULL))) + { + fprintf (stderr, "Can't open display\n"); + return EXIT_FAILURE; + } + + /* Setup signal handlers. */ + XSetErrorHandler(handler); + if (signal (SIGALRM, alrm_handler) == SIG_IGN) signal (SIGALRM, SIG_IGN); + if (signal (SIGTERM, sighandler) == SIG_IGN) signal (SIGTERM, SIG_IGN); + if (signal (SIGINT, sighandler) == SIG_IGN) signal (SIGINT, SIG_IGN); + if (signal (SIGHUP, hup_handler) == SIG_IGN) + { + printf ("Ignoring HUP.\n"); + signal (SIGHUP, SIG_IGN); + } + + init_numbers (); + init_window_list (); + + font = XLoadQueryFont (dpy, FONT_NAME); + if (font == NULL) + { + fprintf (stderr, "ratpoison: Cannot load font %s.\n", FONT_NAME); + exit (EXIT_FAILURE); + } + + num_screens = ScreenCount (dpy); + if ((screens = (screen_info *)malloc (sizeof (screen_info) * num_screens)) == NULL) + { + fprintf (stderr, "ratpoison:main.c:Out of memory!\n"); + exit (EXIT_FAILURE); + } + + printf ("%d screens.\n", num_screens); + + /* Initialize the screens */ + for (i=0; i<num_screens; i++) + { + init_screen (&screens[i], i); + } + + /* Set our Atoms */ + wm_state = XInternAtom(dpy, "WM_STATE", False); + wm_change_state = XInternAtom(dpy, "WM_CHANGE_STATE", False); + wm_protocols = XInternAtom(dpy, "WM_PROTOCOLS", False); + wm_delete = XInternAtom(dpy, "WM_DELETE_WINDOW", False); + wm_take_focus = XInternAtom(dpy, "WM_TAKE_FOCUS", False); + wm_colormaps = XInternAtom(dpy, "WM_COLORMAP_WINDOWS", False); + + rp_restart = XInternAtom (dpy, "RP_RESTART", False); + + XSync (dpy, False); + + /* Set an initial window as active. */ + rp_current_window = rp_window_head; + set_active_window (rp_current_window); + + handle_events (); + + return EXIT_SUCCESS; +} + +static void +init_screen (screen_info *s, int screen_num) +{ + XColor fg_color, bg_color, bold_color, junk; + XGCValues gv; + + s->screen_num = screen_num; + s->root = RootWindow (dpy, screen_num); + s->def_cmap = DefaultColormap (dpy, screen_num); + s->font = font; + XGetWindowAttributes (dpy, s->root, &s->root_attr); + + /* Get our program bar colors */ + if (!XAllocNamedColor (dpy, s->def_cmap, BAR_FG_COLOR, &fg_color, &junk)) + { + fprintf (stderr, "Unknown color '%s'\n", BAR_FG_COLOR); + } + + if (!XAllocNamedColor (dpy, s->def_cmap, BAR_BG_COLOR, &bg_color, &junk)) + { + fprintf (stderr, "Unknown color '%s'\n", BAR_BG_COLOR); + } + + if (!XAllocNamedColor (dpy, s->def_cmap, BAR_BOLD_COLOR, &bold_color, &junk)) + { + fprintf (stderr, "Unknown color '%s'\n", BAR_BOLD_COLOR); + } + + /* Setup the GC for drawing the font. */ + gv.foreground = fg_color.pixel; + gv.background = bg_color.pixel; + gv.function = GXcopy; + gv.line_width = 1; + gv.subwindow_mode = IncludeInferiors; + gv.font = font->fid; + s->normal_gc = XCreateGC(dpy, s->root, + GCForeground | GCBackground | GCFunction + | GCLineWidth | GCSubwindowMode | GCFont, + &gv); + gv.foreground = bold_color.pixel; + s->bold_gc = XCreateGC(dpy, s->root, + GCForeground | GCBackground | GCFunction + | GCLineWidth | GCSubwindowMode | GCFont, + &gv); + + XSelectInput(dpy, s->root, + PropertyChangeMask | ColormapChangeMask + | SubstructureRedirectMask | KeyPressMask + | SubstructureNotifyMask ); + XSync (dpy, 0); + + /* Create the program bar window. */ + s->bar_is_raised = 0; + s->bar_window = XCreateSimpleWindow (dpy, s->root, 0, 0, + 1, 1, 1, fg_color.pixel, bg_color.pixel); + XSelectInput (dpy, s->bar_window, StructureNotifyMask); + + /* Setup the window that will recieve all keystrokes once the prefix + key has been pressed. */ + s->key_window = XCreateSimpleWindow (dpy, s->root, 0, 0, 1, 1, 0, WhitePixel (dpy, 0), BlackPixel (dpy, 0)); + XSelectInput (dpy, s->key_window, KeyPressMask); + XMapWindow (dpy, s->key_window); + + /* Create the input window. */ + s->input_window = XCreateSimpleWindow (dpy, s->root, 0, 0, + 1, 1, 1, fg_color.pixel, bg_color.pixel); + XSelectInput (dpy, s->input_window, KeyPressMask); + scanwins (s); +} + +void +clean_up () +{ + XSetInputFocus (dpy, PointerRoot, RevertToPointerRoot, CurrentTime); + XCloseDisplay (dpy); +} + +/* Given a root window, return the screen_info struct */ +screen_info * +find_screen (Window w) +{ + int i; + + for (i=0; i<num_screens; i++) + if (screens[i].root == w) return &screens[i]; + + return NULL; +} diff --git a/src/manage.c b/src/manage.c new file mode 100644 index 0000000..592e391 --- /dev/null +++ b/src/manage.c @@ -0,0 +1,175 @@ +/* Manage windows, such as Mapping them and making sure the proper key + * Grabs have been put in place. + * + * Copyright (C) 2000 Shawn Betts + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA */ + +#include <X11/X.h> +#include <X11/Xlib.h> +#include <X11/Xutil.h> +#include <X11/Xatom.h> +#include <X11/keysymdef.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "ratpoison.h" + +extern Atom wm_state; + +static void +grab_prefix_key (Window w) +{ + XGrabKey(dpy, XKeysymToKeycode (dpy, KEY_PREFIX ), MODIFIER_PREFIX, w, True, + GrabModeAsync, GrabModeAsync); +} + +/* Reget the WM_NAME property for the window and update its name. */ +int +update_window_name (rp_window *win) +{ + XTextProperty text; + char **name_list; + int list_len; + int i; + + /* Don't overwrite the window name if the user specified one. */ + if (win->named) return 0; + + if (!XGetWMName (dpy, win->w, &text)) + { + fprintf (stderr, "ratpoison:manage.c: I can't get the WMName.\n"); + return 0; + } + + if (!XTextPropertyToStringList (&text, &name_list, &list_len)) + { + fprintf (stderr, "ratpoison:manage.c:Error retrieving TextList.\n"); + return 0; + } + + for (i=0; i<list_len; i++) + { + printf ("WMName: %s\n", name_list[i]); + } + + /* Set the window's name to the first in the name_list */ + if (list_len > 0) + { + char *loc; + + free (win->name); + if ((win->name = malloc (strlen (name_list[0]) + 1)) == NULL) + { + fprintf (stderr, "manage.c:update_window_name():Out of memory!\n"); + exit (EXIT_FAILURE); + } + strcpy (win->name, name_list[0]); + + /* A bit of a hack. If there's a : in the string, crop the + string off there. This is mostly brought on by netscape's + disgusting tendency to put its current URL in the WMName!! + arg! */ + loc = strchr (win->name, ':'); + if (loc) loc[0] = '\0'; + } + + /* Its our responsibility to free this. */ + XFreeStringList (name_list); + + return 1; +} + +void +rename_current_window () +{ + char winname[100]; + + if (rp_current_window == NULL) return; + + get_input (rp_current_window->scr, "Name: ", winname, 100); + printf ("user entered: %s\n", winname); + + free (rp_current_window->name); + rp_current_window->name = malloc (sizeof (char) * strlen (winname) + 1); + if (rp_current_window->name == NULL) + { + fprintf (stderr, "ratpoison:rename_window(): Out of memory\n"); + exit (EXIT_FAILURE); + } + strcpy (rp_current_window->name, winname); + rp_current_window->named = 1; + + /* Update the program bar. */ + update_window_names (rp_current_window->scr); +} + +void +manage (rp_window *win, screen_info *s) +{ + if (!update_window_name (win)) return; + + /* We successfully got the name, which means we can start managing! */ + XMapWindow (dpy, win->w); + XMoveResizeWindow (dpy, win->w, 0, 0, s->root_attr.width, s->root_attr.height); + XSelectInput (dpy, win->w, PropertyChangeMask); + XAddToSaveSet(dpy, win->w); + grab_prefix_key (win->w); + + win->state = STATE_MAPPED; + win->number = get_unique_window_number (); + +#ifdef DEBUG + printf ("window '%s' managed.\n", win->name); +#endif +} + +void +unmanage (rp_window *w) +{ + return_window_number (w->number); + remove_from_window_list (w); +} + +/* When starting up scan existing windows and start managing them. */ +void +scanwins(screen_info *s) +{ + rp_window *win; + XWindowAttributes attr; + unsigned int i, nwins; + Window dw1, dw2, *wins; + + XQueryTree(dpy, s->root, &dw1, &dw2, &wins, &nwins); +#ifdef DEBUG + printf ("windows: %d\n", nwins); +#endif + + for (i = 0; i < nwins; i++) + { + XGetWindowAttributes(dpy, wins[i], &attr); + if (wins[i] == s->bar_window || wins[i] == s->key_window || wins[i] == s->input_window) continue; + + if (attr.override_redirect != True) + { + win = add_to_window_list (s, wins[i]); + if (attr.map_state != IsUnmapped) manage (win, s); + } + } + XFree((void *) wins); /* cast is to shut stoopid compiler up */ +} diff --git a/src/manage.h b/src/manage.h new file mode 100644 index 0000000..58a5590 --- /dev/null +++ b/src/manage.h @@ -0,0 +1,30 @@ +/* manage.h + * + * Copyright (C) 2000 Shawn Betts + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA */ + +#ifndef _MANAGE_H +#define _MANAGE_H + +#include "data.h" + +void scanwins(screen_info *s); +void manage (rp_window *w, screen_info *s); +void unmanage (rp_window *w); +int update_window_name (rp_window *win); +void rename_current_window (); +#endif /* _MANAGE_H */ diff --git a/src/number.c b/src/number.c new file mode 100644 index 0000000..6bd11c6 --- /dev/null +++ b/src/number.c @@ -0,0 +1,130 @@ +/* handles the handing out of and uniqueness of window numbers. + + * Copyright (C) 2000 Shawn Betts + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA */ + +#include <stdlib.h> +#include <stdio.h> + +#include "ratpoison.h" + + +/* A list of the numbers taken. */ +static int *numbers_taken; + +/* the number of numbers currently stored in the numbers_taken + array. */ +static int num_taken; + +/* the size of the numbers_taken array. */ +static int max_taken; + +static int +number_is_taken (int n) +{ + int i; + + for (i=0; i<num_taken; i++) + { + if (numbers_taken[i] == n) return 1; + } + return 0; +} + +/* returns index into numbers_taken that can be used. */ +static int +find_empty_cell () +{ + int i; + + for (i=0; i<num_taken; i++) + { + if (numbers_taken[i] == -1) return i; + } + + /* no vacant ones, so grow the array. */ + if (num_taken >= max_taken) + { + max_taken *= 2; + numbers_taken = realloc (numbers_taken, sizeof (int) * max_taken); + if (numbers_taken == NULL) + { + fprintf (stderr, "numbers.c: Out of memory\n"); + exit (EXIT_FAILURE); + } + } + num_taken++; + + return num_taken-1; +} + +static int +add_to_list (int n) +{ + if (number_is_taken (n)) return 0; /* failed. */ + + numbers_taken[find_empty_cell()] = n; + return 1; /* success! */ +} + +/* returns a unique number that can be used as the window number in + the program bar. */ +int +get_unique_window_number () +{ + int i; + + /* look for a unique number, and add it to the list of taken + numbers. */ + i = 0; + while (!add_to_list (i)) i++; + + return i; +} + +/* When a window is destroyed, it gives back its window number with + this function. */ +void +return_window_number (int n) +{ + int i; + + for (i=0; i<num_taken; i++) + { + if (numbers_taken[i] == n) + { + numbers_taken[i] = -1; + return; + } + } +} + + +void +init_numbers () +{ + max_taken = 10; + num_taken = 0; + + numbers_taken = malloc (max_taken * sizeof (int)); + if (numbers_taken == NULL) + { + fprintf (stderr, "numbers.c: Cannot alloc numbers_taken.\n"); + exit (EXIT_FAILURE); + } + +} diff --git a/src/number.h b/src/number.h new file mode 100644 index 0000000..f06fb53 --- /dev/null +++ b/src/number.h @@ -0,0 +1,3 @@ +int get_unique_window_number (); +void return_window_number (int n); +void init_numbers (); diff --git a/src/ratpoison.h b/src/ratpoison.h new file mode 100644 index 0000000..8b83866 --- /dev/null +++ b/src/ratpoison.h @@ -0,0 +1,40 @@ +/* Standard header for ratpoison. + * + * Copyright (C) 2000 Shawn Betts + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA */ + +#ifndef _RATPOISON_H +#define _RATPOISON_H + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif /* HAVE_CONFIG_H */ + +#include "conf.h" + +#include "data.h" +#include "manage.h" +#include "list.h" +#include "bar.h" +#include "events.h" +#include "number.h" +#include "input.h" + +void clean_up (); +screen_info *find_screen (Window w); + +#endif /* _RATPOISON_H */ |