nsxiv49a69a3
Added win_open & win_close
+75 −0
2 files changed
| @@ -16,6 +16,72 @@ | |||
| 16 | 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
| 17 | 17 | */ | |
| 18 | 18 | ||
| 19 | + | #include <stdlib.h> | |
| 20 | + | #include <stdio.h> | |
| 21 | + | ||
| 22 | + | #include <X11/Xutil.h> | |
| 23 | + | ||
| 19 | 24 | #include "sxiv.h" | |
| 20 | 25 | #include "window.h" | |
| 21 | 26 | ||
| 27 | + | Display *dpy; | |
| 28 | + | int scr; | |
| 29 | + | int scrw, scrh; | |
| 30 | + | GC gc; | |
| 31 | + | XColor bgcol; | |
| 32 | + | ||
| 33 | + | void win_open(win_t *win) { | |
| 34 | + | XClassHint *classhint; | |
| 35 | + | ||
| 36 | + | if (win == NULL) | |
| 37 | + | return; | |
| 38 | + | ||
| 39 | + | if (!(dpy = XOpenDisplay(NULL))) | |
| 40 | + | FATAL("could not open display"); | |
| 41 | + | ||
| 42 | + | scr = DefaultScreen(dpy); | |
| 43 | + | scrw = DisplayWidth(dpy, scr); | |
| 44 | + | scrh = DisplayHeight(dpy, scr); | |
| 45 | + | ||
| 46 | + | bgcol.red = 0x7000; | |
| 47 | + | bgcol.green = 0x7000; | |
| 48 | + | bgcol.blue = 0x7000; | |
| 49 | + | XAllocColor(dpy, DefaultColormap(dpy, scr), &bgcol); | |
| 50 | + | ||
| 51 | + | if (win->w > scrw) | |
| 52 | + | win->w = scrw; | |
| 53 | + | if (win->h > scrh) | |
| 54 | + | win->h = scrh; | |
| 55 | + | win->x = (scrw - win->w) / 2; | |
| 56 | + | win->y = (scrh - win->h) / 2; | |
| 57 | + | ||
| 58 | + | win->xwin = XCreateWindow(dpy, RootWindow(dpy, scr), | |
| 59 | + | win->x, win->y, win->w, win->h, 0, DefaultDepth(dpy, scr), InputOutput, | |
| 60 | + | DefaultVisual(dpy, scr), 0, NULL); | |
| 61 | + | if (win->xwin == None) | |
| 62 | + | FATAL("could not create window"); | |
| 63 | + | ||
| 64 | + | XSelectInput(dpy, win->xwin, | |
| 65 | + | StructureNotifyMask | ExposureMask | KeyPressMask); | |
| 66 | + | ||
| 67 | + | gc = XCreateGC(dpy, win->xwin, 0, NULL); | |
| 68 | + | ||
| 69 | + | if ((classhint = XAllocClassHint())) { | |
| 70 | + | classhint->res_name = "sxvi"; | |
| 71 | + | classhint->res_class = "sxvi"; | |
| 72 | + | XSetClassHint(dpy, win->xwin, classhint); | |
| 73 | + | XFree(classhint); | |
| 74 | + | } | |
| 75 | + | ||
| 76 | + | XMapWindow(dpy, win->xwin); | |
| 77 | + | XFlush(dpy); | |
| 78 | + | } | |
| 79 | + | ||
| 80 | + | void win_close(win_t *win) { | |
| 81 | + | if (win == NULL) | |
| 82 | + | return; | |
| 83 | + | ||
| 84 | + | XDestroyWindow(dpy, win->xwin); | |
| 85 | + | XFreeGC(dpy, gc); | |
| 86 | + | XCloseDisplay(dpy); | |
| 87 | + | } | |
| @@ -19,12 +19,21 @@ | |||
| 19 | 19 | #ifndef WINDOW_H | |
| 20 | 20 | #define WINDOW_H | |
| 21 | 21 | ||
| 22 | + | #include <X11/Xlib.h> | |
| 23 | + | ||
| 22 | 24 | typedef struct win_s { | |
| 25 | + | Window xwin; | |
| 26 | + | ||
| 23 | 27 | int w; | |
| 24 | 28 | int h; | |
| 25 | 29 | int x; | |
| 26 | 30 | int y; | |
| 31 | + | ||
| 27 | 32 | int bw; | |
| 33 | + | int fullscreen; | |
| 28 | 34 | } win_t; | |
| 29 | 35 | ||
| 36 | + | void win_open(win_t *win); | |
| 37 | + | void win_close(win_t *win); | |
| 38 | + | ||
| 30 | 39 | #endif /* WINDOW_H */ | |