clean up main function more

This commit is contained in:
speedie 2023-05-23 22:40:17 +02:00
parent 96b82dc223
commit b97847c207
5 changed files with 65 additions and 57 deletions

View file

@ -15,6 +15,37 @@ void prepare_window_size(void) {
return; return;
} }
void store_image_vars(void) {
#if USEIMAGE
longestedge = MAX(imagewidth, imageheight);
if (!imagew || !imageh || !imageg) {
imagew = imagewidth;
imageh = imageheight;
imagegaps = imagegaps;
}
#endif
}
void set_mode(void) {
if (!type) { // no typing allowed, require normal mode
mode = 0;
}
// set default mode, must be done before the event loop or keybindings will not work
if (mode) {
curMode = 1;
allowkeys = 1;
strcpy(modetext, instext);
} else {
curMode = 0;
allowkeys = !curMode;
strcpy(modetext, normtext);
}
}
void get_width(int numwidthchecks, unsigned int minstrlen, unsigned int curstrlen) { void get_width(int numwidthchecks, unsigned int minstrlen, unsigned int curstrlen) {
struct item *item; struct item *item;
unsigned int tmp = 0; unsigned int tmp = 0;

View file

@ -6,3 +6,5 @@ static void prepare_window_size(void);
static void set_window(void); static void set_window(void);
static void set_prop(void); static void set_prop(void);
static void resizeclient(void); static void resizeclient(void);
static void store_image_vars(void);
static void set_mode(void);

View file

@ -147,3 +147,30 @@ void set_screen(Display *disp) {
screen = DefaultScreen(disp); screen = DefaultScreen(disp);
root = RootWindow(disp, screen); root = RootWindow(disp, screen);
} }
void handle_x11(void) {
XWindowAttributes wa;
if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
fputs("warning: no locale support\n", stderr); // invalid locale, so notify the user about it
if (!XSetLocaleModifiers(""))
fputs("warning: no locale modifiers support\n", stderr);
if (!(dpy = opendisplay(NULL)))
die("spmenu: cannot open display"); // failed to open display
// set screen and root window
set_screen(dpy);
// parent window is the root window (ie. window manager) because we're not embedding
if (!embed || !(parentwin = strtol(embed, NULL, 0)))
parentwin = root;
if (!XGetWindowAttributes(dpy, parentwin, &wa)) {
die("spmenu: could not get embedding window attributes: 0x%lx", parentwin);
}
xinitvisual(); // init visual and create drawable after
drw = drw_create(dpy, screen, root, wa.width, wa.height, visual, depth, cmap);
}

View file

@ -6,4 +6,5 @@ static Window root, parentwin, win;
static void setupdisplay(void); static void setupdisplay(void);
static void set_screen(Display *disp); static void set_screen(Display *disp);
static void handle_x11(void);
static Display * opendisplay(char *disp); static Display * opendisplay(char *disp);

View file

@ -509,62 +509,15 @@ void xinitvisual(void) {
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
XWindowAttributes wa;
readargs(argc, argv); // start by reading arguments readargs(argc, argv); // start by reading arguments
#if USEIMAGE // open x11 display and create drawable
longestedge = MAX(imagewidth, imageheight); handle_x11();
#endif
if (!type) {
mode = 0;
}
// set default mode, must be done before the event loop or keybindings will not work
if (mode) {
curMode = 1;
allowkeys = 1;
strcpy(modetext, instext);
} else {
curMode = 0;
allowkeys = !curMode;
strcpy(modetext, normtext);
}
if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
fputs("warning: no locale support\n", stderr); // invalid locale, so notify the user about it
if (!XSetLocaleModifiers(""))
fputs("warning: no locale modifiers support\n", stderr);
if (!(dpy = opendisplay(NULL)))
die("spmenu: cannot open display"); // failed to open display
// set screen and root window
set_screen(dpy);
// parent window is the root window (ie. window manager) because we're not embedding
if (!embed || !(parentwin = strtol(embed, NULL, 0)))
parentwin = root;
if (!XGetWindowAttributes(dpy, parentwin, &wa)) {
die("spmenu: could not get embedding window attributes: 0x%lx", parentwin);
}
xinitvisual(); // init visual and create drawable after
drw = drw_create(dpy, screen, root, wa.width, wa.height, visual, depth, cmap); // wrapper function creating a drawable
// load fonts // load fonts
if (!drw_font_create(drw, fonts, LENGTH(fonts))) if (!drw_font_create(drw, fonts, LENGTH(fonts)))
die("no fonts could be loaded."); die("no fonts could be loaded.");
// resize window
lrpad = drw->font->h + textpadding;
prepare_window_size(); // this function sets padding size
// pledge limits what programs can do, so here we specify what spmenu should be allowed to do // pledge limits what programs can do, so here we specify what spmenu should be allowed to do
#ifdef __OpenBSD__ #ifdef __OpenBSD__
if (pledge("stdio rpath wpath cpath", NULL) == -1) if (pledge("stdio rpath wpath cpath", NULL) == -1)
@ -582,14 +535,8 @@ int main(int argc, char *argv[]) {
grabkeyboard(); grabkeyboard();
} }
// set default values store_image_vars();
#if USEIMAGE set_mode();
if (!imagew || !imageh || !imageg) {
imagew = imagewidth;
imageh = imageheight;
imagegaps = imagegaps;
}
#endif
init_appearance(); // init colorschemes by reading arrays init_appearance(); // init colorschemes by reading arrays
setupdisplay(); // set up display and create window setupdisplay(); // set up display and create window