Do not start to OpenDisplay before reading command line options

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-03-25 16:56:47 +01:00
parent e9dbf302be
commit d5d1319821
2 changed files with 34 additions and 40 deletions

View File

@ -598,7 +598,6 @@ item_list_fill_stdin(void)
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
Display *disp;
XEvent ev; XEvent ev;
int opt, ret, x, y, i, screen = 0; int opt, ret, x, y, i, screen = 0;
char *configfile = NULL, *cmd; char *configfile = NULL, *cmd;
@ -616,11 +615,6 @@ main(int argc, char **argv)
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
}; };
if(!(disp = XOpenDisplay(NULL)))
eprint("unable to open display");
globalconf.display = disp;
while((opt = getopt_long(argc, argv, "vhf:b:x:y:n:c:e:", while((opt = getopt_long(argc, argv, "vhf:b:x:y:n:c:e:",
long_options, NULL)) != -1) long_options, NULL)) != -1)
switch(opt) switch(opt)
@ -642,13 +636,16 @@ main(int argc, char **argv)
if(argc - optind >= 1) if(argc - optind >= 1)
globalconf.prompt = a_strdup(argv[optind]); globalconf.prompt = a_strdup(argv[optind]);
/* Get the numlock mask */ if(!(globalconf.display = XOpenDisplay(NULL)))
globalconf.numlockmask = xgetnumlockmask(disp); eprint("unable to open display");
si = screensinfo_new(disp); /* Get the numlock mask */
globalconf.numlockmask = xgetnumlockmask(globalconf.display);
si = screensinfo_new(globalconf.display);
if(si->xinerama_is_active) if(si->xinerama_is_active)
{ {
if(XQueryPointer(disp, RootWindow(disp, DefaultScreen(disp)), if(XQueryPointer(globalconf.display, RootWindow(globalconf.display, DefaultScreen(globalconf.display)),
&dummy, &dummy, &x, &y, &i, &i, &ui)) &dummy, &dummy, &x, &y, &i, &i, &ui))
{ {
screen = screen_get_bycoord(si, 0, x, y); screen = screen_get_bycoord(si, 0, x, y);
@ -660,9 +657,9 @@ main(int argc, char **argv)
} }
else else
{ {
screen = DefaultScreen(disp); screen = DefaultScreen(globalconf.display);
if(!geometry.width) if(!geometry.width)
geometry.width = DisplayWidth(disp, DefaultScreen(disp)); geometry.width = DisplayWidth(globalconf.display, DefaultScreen(globalconf.display));
} }
if((ret = config_parse(screen, configfile, globalconf.prompt, &geometry))) if((ret = config_parse(screen, configfile, globalconf.prompt, &geometry)))
@ -676,14 +673,14 @@ main(int argc, char **argv)
screensinfo_delete(&si); screensinfo_delete(&si);
/* Create the window */ /* Create the window */
globalconf.sw = simplewindow_new(disp, DefaultScreen(disp), globalconf.sw = simplewindow_new(globalconf.display, DefaultScreen(globalconf.display),
geometry.x, geometry.y, geometry.width, geometry.height, 0); geometry.x, geometry.y, geometry.width, geometry.height, 0);
XStoreName(disp, globalconf.sw->window, PROGNAME); XStoreName(globalconf.display, globalconf.sw->window, PROGNAME);
XMapRaised(disp, globalconf.sw->window); XMapRaised(globalconf.display, globalconf.sw->window);
/* Create the drawing context */ /* Create the drawing context */
globalconf.ctx = draw_context_new(disp, DefaultScreen(disp), globalconf.ctx = draw_context_new(globalconf.display, DefaultScreen(globalconf.display),
geometry.width, geometry.height, geometry.width, geometry.height,
globalconf.sw->drawable); globalconf.sw->drawable);
@ -701,7 +698,7 @@ main(int argc, char **argv)
for(opt = 1000; opt; opt--) for(opt = 1000; opt; opt--)
{ {
if(XGrabKeyboard(disp, DefaultRootWindow(disp), True, if(XGrabKeyboard(globalconf.display, DefaultRootWindow(globalconf.display), True,
GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess) GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
break; break;
usleep(1000); usleep(1000);
@ -713,7 +710,7 @@ main(int argc, char **argv)
while(status == RUN) while(status == RUN)
{ {
XNextEvent(disp, &ev); XNextEvent(globalconf.display, &ev);
switch(ev.type) switch(ev.type)
{ {
case ButtonPress: case ButtonPress:
@ -724,7 +721,7 @@ main(int argc, char **argv)
break; break;
case Expose: case Expose:
if(!ev.xexpose.count) if(!ev.xexpose.count)
simplewindow_refresh_drawable(globalconf.sw, DefaultScreen(disp)); simplewindow_refresh_drawable(globalconf.sw, DefaultScreen(globalconf.display));
break; break;
default: default:
break; break;
@ -750,7 +747,7 @@ main(int argc, char **argv)
p_delete(&globalconf.text); p_delete(&globalconf.text);
draw_context_delete(&globalconf.ctx); draw_context_delete(&globalconf.ctx);
simplewindow_delete(&globalconf.sw); simplewindow_delete(&globalconf.sw);
XCloseDisplay(disp); XCloseDisplay(globalconf.display);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -116,7 +116,6 @@ exit_on_signal(int sig __attribute__ ((unused)))
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
Display *disp;
SimpleWindow *sw; SimpleWindow *sw;
DrawCtx *ctx; DrawCtx *ctx;
XEvent ev; XEvent ev;
@ -134,11 +133,6 @@ main(int argc, char **argv)
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
}; };
if(!(disp = XOpenDisplay(NULL)))
eprint("unable to open display");
globalconf.display = disp;
while((opt = getopt_long(argc, argv, "vhf:b:x:y:n:c:d:", while((opt = getopt_long(argc, argv, "vhf:b:x:y:n:c:d:",
long_options, NULL)) != -1) long_options, NULL)) != -1)
switch(opt) switch(opt)
@ -167,20 +161,23 @@ main(int argc, char **argv)
if(argc - optind < 1) if(argc - optind < 1)
exit_help(EXIT_FAILURE); exit_help(EXIT_FAILURE);
si = screensinfo_new(disp); if(!(globalconf.display = XOpenDisplay(NULL)))
eprint("unable to open display");
si = screensinfo_new(globalconf.display);
if(si->xinerama_is_active) if(si->xinerama_is_active)
{ {
if(XQueryPointer(disp, RootWindow(disp, DefaultScreen(disp)), if(XQueryPointer(globalconf.display, RootWindow(globalconf.display, DefaultScreen(globalconf.display)),
&dummy, &dummy, &x, &y, &i, &i, &ui)) &dummy, &dummy, &x, &y, &i, &i, &ui))
screen = screen_get_bycoord(si, 0, x, y); screen = screen_get_bycoord(si, 0, x, y);
} }
else else
screen = DefaultScreen(disp); screen = DefaultScreen(globalconf.display);
if((ret = config_parse(screen, configfile))) if((ret = config_parse(screen, configfile)))
return ret; return ret;
geometry.width = draw_textwidth(disp, globalconf.style.font, argv[optind]); geometry.width = draw_textwidth(globalconf.display, globalconf.style.font, argv[optind]);
geometry.height = globalconf.style.font->height * 1.5; geometry.height = globalconf.style.font->height * 1.5;
if(argc - optind >= 2) if(argc - optind >= 2)
@ -193,12 +190,12 @@ main(int argc, char **argv)
* ((double) globalconf.style.font->height / (double) icon_geometry.height); * ((double) globalconf.style.font->height / (double) icon_geometry.height);
} }
sw = simplewindow_new(disp, DefaultScreen(disp), sw = simplewindow_new(globalconf.display, DefaultScreen(globalconf.display),
geometry.x, geometry.y, geometry.width, geometry.height, 0); geometry.x, geometry.y, geometry.width, geometry.height, 0);
XStoreName(disp, sw->window, PROGNAME); XStoreName(globalconf.display, sw->window, PROGNAME);
ctx = draw_context_new(disp, DefaultScreen(disp), ctx = draw_context_new(globalconf.display, DefaultScreen(globalconf.display),
geometry.width, geometry.height, sw->drawable); geometry.width, geometry.height, sw->drawable);
geometry.x = geometry.y = 0; geometry.x = geometry.y = 0;
@ -210,26 +207,26 @@ main(int argc, char **argv)
p_delete(&ctx); p_delete(&ctx);
simplewindow_refresh_drawable(sw, DefaultScreen(disp)); simplewindow_refresh_drawable(sw, DefaultScreen(globalconf.display));
XMapRaised(disp, sw->window); XMapRaised(globalconf.display, sw->window);
XSync(disp, False); XSync(globalconf.display, False);
signal(SIGALRM, &exit_on_signal); signal(SIGALRM, &exit_on_signal);
alarm(delay); alarm(delay);
while(running) while(running)
{ {
if(XPending(disp)) if(XPending(globalconf.display))
{ {
XNextEvent(disp, &ev); XNextEvent(globalconf.display, &ev);
switch(ev.type) switch(ev.type)
{ {
case ButtonPress: case ButtonPress:
case KeyPress: case KeyPress:
running = False; running = False;
case Expose: case Expose:
simplewindow_refresh_drawable(sw, DefaultScreen(disp)); simplewindow_refresh_drawable(sw, DefaultScreen(globalconf.display));
break; break;
default: default:
break; break;
@ -239,7 +236,7 @@ main(int argc, char **argv)
} }
simplewindow_delete(&sw); simplewindow_delete(&sw);
XCloseDisplay(disp); XCloseDisplay(globalconf.display);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }