summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--xlnch.c165
1 files changed, 93 insertions, 72 deletions
diff --git a/xlnch.c b/xlnch.c
index ab236bb..c68aecf 100644
--- a/xlnch.c
+++ b/xlnch.c
@@ -21,19 +21,16 @@
* Brian Hammond 2/9/96. http://mech.math.msu.su/~nap/2/GWindow/xintro.html
*/
-#include <X11/Xlib.h>
-#include <X11/Xutil.h>
-#include <X11/Xos.h>
-
+#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <ctype.h>
-#include <time.h>
-
#include <sys/wait.h>
-
+#include <time.h>
#include <wordexp.h>
+#include <X11/Xlib.h>
+#include <X11/Xos.h>
+#include <X11/Xutil.h>
#include "util.h"
@@ -53,14 +50,12 @@ struct lnch_t lnch[MAX_ENTRIES];
unsigned int idx = 0;
Colormap cm;
-XColor red;
-
Display *display;
+GC gc;
int screen;
Window root, win;
-//Drawable drawable;
-GC gc;
-// XftColor *scheme;
+XColor red;
+XFontStruct * font;
int w_width = 0;
int w_height = 0;
@@ -90,6 +85,12 @@ void init()
gc = XCreateGC(display, root, 0, NULL);
XSetLineAttributes(display, gc, 1, LineSolid, CapButt, JoinMiter);
+ cm = DefaultColormap(display, screen);
+ if (! XAllocNamedColor(display, cm, "red", &red, &red)) {
+ fprintf(stderr, "XAllocNamedColor - failed to allocated 'red' color.\n");
+ exit(1);
+ }
+
swa.override_redirect = True;
swa.background_pixel = BlackPixel(display, screen);
swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
@@ -98,19 +99,20 @@ void init()
get_curson_pos(&x, &y);
int height = 16 + (9 + 3)*w_height - 3 + 16,
- width = 16 + w_width*9;
+ width = 16 + w_width*11;
win = XCreateWindow(display, root, x, y, width, height, 0,
CopyFromParent, CopyFromParent, CopyFromParent,
- CWOverrideRedirect | CWBackPixel | CWEventMask, &swa);
-
- printf("Window pos: {%d, %d}", x, y);
- fflush(stdout);
-
- cm = DefaultColormap(display, screen);
- if (! XAllocNamedColor(display, cm, "red", &red, &red)) {
- fprintf(stderr, "XAllocNamedColor - failed to allocated 'red' color.\n");
- exit(1);
+ CWOverrideRedirect | CWBackPixel | CWEventMask, &swa);
+
+ const char * fontname = "-*-terminus-medium-*-*-*-16-*-*-*-*-*-*-*";
+ font = XLoadQueryFont(display, fontname);
+ /* If the font could not be loaded, revert to the "fixed" font. */
+ if (!font) {
+ fprintf (stderr, "unable to load font %s: using fixed\n", fontname);
+ font = XLoadQueryFont (display, "fixed");
}
+ XSetFont(display, gc, font->fid);
+
XMapRaised(display, win);
XRaiseWindow(display, win);
}
@@ -127,10 +129,11 @@ int parse_line(char * line)
char *token;
char delim[2] = ":";
- lnch[idx].exits = 1;
+ if (strlen(line) == 0 ||
+ line[0] == '#')
+ return 0;
- if (line[0] == '#')
- return 0;
+ lnch[idx].exits = 1;
if (line[0] == '&') {
lnch[idx].exits = 0;
@@ -140,28 +143,29 @@ int parse_line(char * line)
/* Get key entry */
token = strtok(line, delim);
- if (strlen(token) != 1) {
- printf("error on token %s", token) ;
+ if (strlen(token) != 1)
return 1;
- }
+
lnch[idx].key = token[0];
line+=2; // pass the key, pass the `:`
/* Get description */
token = strtok(NULL, delim);
- if (!token) return 1;
- w_width = MAX((int) strlen(token), w_width);
+ if (!token)
+ return 1;
+
strcpy(lnch[idx].desc, token);
- line+=(strlen(token) + 1 ); // pass the decription, pass the `:`
+ line+=(strlen(token) + 1); // pass the decription, pass the `:`
/* Get command */
strcpy(lnch[idx].cmd, line);
/* If description is epmty command becomes the descritpion */
- if (!strlen(lnch[idx].desc))
- strcpy(lnch[idx].desc, line);
+ if (!strlen(lnch[idx].cmd))
+ strcpy(lnch[idx].cmd, lnch[idx].desc);
+ w_width = MAX((int) strlen(lnch[idx].desc), w_width);
idx++;
return 0;
}
@@ -242,16 +246,18 @@ void grabkeyboard()
void draw_text()
{
- char text[2048];
- int x = 10, y = 10;
- for (unsigned int i = 0; i < idx; i++) {
- if (lnch[i].exits)
- XSetForeground(display,gc,WhitePixel(display,screen));
- else
- XSetForeground(display,gc,red.pixel);
- sprintf(text, "%c: %s", lnch[i].key, lnch[i].desc);
- XDrawString(display,win,gc,x,y+=13, text, strlen(text));
+ char text[2048];
+ int x = 10, y = 10;
+ for (unsigned int i = 0; i < idx; i++) {
+ if (lnch[i].exits) {
+ XSetForeground(display,gc,WhitePixel(display,screen));
+ }
+ else {
+ XSetForeground(display,gc,red.pixel);
}
+ sprintf(text, "%c: %s", lnch[i].key, lnch[i].desc);
+ XDrawString(display,win,gc,x,y+=13, text, strlen(text));
+ }
}
void redraw()
@@ -260,40 +266,15 @@ void redraw()
draw_text();
}
-int main (int argc, char *argv[])
+int run()
{
- if (argc < 2) {
- printf("Usage:\n%s xlnchrc\n", argv[0]);
- printf("xlnchrc format:\n[&]<key>:<description>:<command>\n");
- return -1;
- }
-
- FILE *f;
-
- if (!strcmp(argv[1], "-"))
- f = stdin;
- else {
- f = fopen(argv[1], "r");
- /* printf("xlnchrc required\n"); */
- /* return -1; */
- }
-
- while (fgets(buf, BUF_SIZE, f)) {
- trim(buf);
- parse_line(buf);
- w_height++;
- }
-
-
- if (f != stdin) fclose(f);
-
XEvent event; /* the XEvent declaration !!! */
KeySym key; /* a dealie-bob to handle KeyPress Events */
char text[255]; /* a char buffer for KeyPress Events */
int rc;
init();
- // system("sleep 100");
+
grabkeyboard();
grabfocus();
@@ -322,7 +303,7 @@ int main (int argc, char *argv[])
*/
if (text[0]=='q') {
close_x();
- exit(0);
+ return 0;
}
printf("You pressed the %c key!\n",text[0]);
@@ -331,7 +312,7 @@ int main (int argc, char *argv[])
rc = run_cmd(lnch[i].cmd);
if (lnch[i].exits) {
close_x();
- exit(rc);
+ return rc;
}
}
}
@@ -353,3 +334,43 @@ int main (int argc, char *argv[])
}
}
}
+
+int main (int argc, char *argv[])
+{
+ if ((argc > 1) &&
+ (!strcmp(argv[1], "--help") ||
+ !strcmp(argv[1], "-h") ||
+ !strcmp(argv[1], "-?"))) {
+ printf("Usage:\n%s [xlnchrc]\n", argv[0]);
+ printf("\nxlnchrc format:\n[&]<key>:[<description>]:<command>\n");
+ return 0;
+ }
+
+ FILE *f;
+ if (argc == 1 || !strcmp(argv[1], "-"))
+ f = stdin;
+ else {
+ f = fopen(argv[1], "r");
+ if (!f)
+ die("Cannot open file %s", argv[1]);
+ }
+
+ int n = 1;
+ char tmp[BUF_SIZE] = "";
+ while (fgets(buf, BUF_SIZE, f)) {
+ trim(buf);
+ strcpy(tmp, buf);
+ if (parse_line(buf)) {
+ fprintf(stderr, "%s:%d: cannot parse ā€˜%sā€™\n",
+ argc == 1 || !strcmp(argv[1], "-") ? "stdin": argv[1], n, tmp);
+ }
+ else {
+ w_height++;
+ }
+ n++;
+ }
+
+ if (f != stdin) fclose(f);
+
+ return run();
+}