Pack types from x.c into structs

This commit is contained in:
Benedikt Heine 2018-09-16 02:58:13 +02:00
parent fab2543f06
commit c944949e36
7 changed files with 85 additions and 84 deletions

View File

@ -18,18 +18,18 @@
#include "queues.h" #include "queues.h"
#include "x11/x.h" #include "x11/x.h"
typedef struct { struct colored_layout {
PangoLayout *l; PangoLayout *l;
color_t fg; struct color fg;
color_t bg; struct color bg;
color_t frame; struct color frame;
char *text; char *text;
PangoAttrList *attr; PangoAttrList *attr;
cairo_surface_t *icon; cairo_surface_t *icon;
const notification *n; const notification *n;
} colored_layout; };
window_x11 *win; struct window_x11 *win;
PangoFontDescription *pango_fdesc; PangoFontDescription *pango_fdesc;
@ -41,9 +41,9 @@ void draw_setup(void)
pango_fdesc = pango_font_description_from_string(settings.font); pango_fdesc = pango_font_description_from_string(settings.font);
} }
static color_t color_hex_to_double(int hexValue) static struct color color_hex_to_double(int hexValue)
{ {
color_t color; struct color color;
color.r = ((hexValue >> 16) & 0xFF) / 255.0; color.r = ((hexValue >> 16) & 0xFF) / 255.0;
color.g = ((hexValue >> 8) & 0xFF) / 255.0; color.g = ((hexValue >> 8) & 0xFF) / 255.0;
color.b = ((hexValue) & 0xFF) / 255.0; color.b = ((hexValue) & 0xFF) / 255.0;
@ -51,7 +51,7 @@ static color_t color_hex_to_double(int hexValue)
return color; return color;
} }
static color_t string_to_color(const char *str) static struct color string_to_color(const char *str)
{ {
char *end; char *end;
long int val = strtol(str+1, &end, 16); long int val = strtol(str+1, &end, 16);
@ -73,10 +73,10 @@ static double color_apply_delta(double base, double delta)
return base; return base;
} }
static color_t calculate_foreground_color(color_t bg) static struct color calculate_foreground_color(struct color bg)
{ {
double c_delta = 0.1; double c_delta = 0.1;
color_t color = bg; struct color color = bg;
/* do we need to darken or brighten the colors? */ /* do we need to darken or brighten the colors? */
bool darken = (bg.r + bg.g + bg.b) / 3 > 0.5; bool darken = (bg.r + bg.g + bg.b) / 3 > 0.5;
@ -90,7 +90,8 @@ static color_t calculate_foreground_color(color_t bg)
return color; return color;
} }
static color_t layout_get_sepcolor(colored_layout *cl, colored_layout *cl_next) static struct color layout_get_sepcolor(struct colored_layout *cl,
struct colored_layout *cl_next)
{ {
switch (settings.sep_color) { switch (settings.sep_color) {
case SEP_FRAME: case SEP_FRAME:
@ -136,7 +137,7 @@ static void layout_setup_pango(PangoLayout *layout, int width)
static void free_colored_layout(void *data) static void free_colored_layout(void *data)
{ {
colored_layout *cl = data; struct colored_layout *cl = data;
g_object_unref(cl->l); g_object_unref(cl->l);
pango_attr_list_unref(cl->attr); pango_attr_list_unref(cl->attr);
g_free(cl->text); g_free(cl->text);
@ -153,7 +154,7 @@ static struct dimensions calculate_dimensions(GSList *layouts)
{ {
struct dimensions dim = { 0 }; struct dimensions dim = { 0 };
screen_info *scr = get_active_screen(); struct screen_info *scr = get_active_screen();
if (have_dynamic_width()) { if (have_dynamic_width()) {
/* dynamic width */ /* dynamic width */
dim.w = 0; dim.w = 0;
@ -176,7 +177,7 @@ static struct dimensions calculate_dimensions(GSList *layouts)
int text_width = 0, total_width = 0; int text_width = 0, total_width = 0;
for (GSList *iter = layouts; iter; iter = iter->next) { for (GSList *iter = layouts; iter; iter = iter->next) {
colored_layout *cl = iter->data; struct colored_layout *cl = iter->data;
int w=0,h=0; int w=0,h=0;
pango_layout_get_pixel_size(cl->l, &w, &h); pango_layout_get_pixel_size(cl->l, &w, &h);
if (cl->icon) { if (cl->icon) {
@ -233,7 +234,7 @@ static struct dimensions calculate_dimensions(GSList *layouts)
static PangoLayout *layout_create(cairo_t *c) static PangoLayout *layout_create(cairo_t *c)
{ {
screen_info *screen = get_active_screen(); struct screen_info *screen = get_active_screen();
PangoContext *context = pango_cairo_create_context(c); PangoContext *context = pango_cairo_create_context(c);
pango_cairo_context_set_resolution(context, get_dpi_for_screen(screen)); pango_cairo_context_set_resolution(context, get_dpi_for_screen(screen));
@ -245,9 +246,9 @@ static PangoLayout *layout_create(cairo_t *c)
return layout; return layout;
} }
static colored_layout *layout_init_shared(cairo_t *c, const notification *n) static struct colored_layout *layout_init_shared(cairo_t *c, const notification *n)
{ {
colored_layout *cl = g_malloc(sizeof(colored_layout)); struct colored_layout *cl = g_malloc(sizeof(struct colored_layout));
cl->l = layout_create(c); cl->l = layout_create(c);
if (!settings.word_wrap) { if (!settings.word_wrap) {
@ -301,19 +302,19 @@ static colored_layout *layout_init_shared(cairo_t *c, const notification *n)
return cl; return cl;
} }
static colored_layout *layout_derive_xmore(cairo_t *c, const notification *n, int qlen) static struct colored_layout *layout_derive_xmore(cairo_t *c, const notification *n, int qlen)
{ {
colored_layout *cl = layout_init_shared(c, n); struct colored_layout *cl = layout_init_shared(c, n);
cl->text = g_strdup_printf("(%d more)", qlen); cl->text = g_strdup_printf("(%d more)", qlen);
cl->attr = NULL; cl->attr = NULL;
pango_layout_set_text(cl->l, cl->text, -1); pango_layout_set_text(cl->l, cl->text, -1);
return cl; return cl;
} }
static colored_layout *layout_from_notification(cairo_t *c, notification *n) static struct colored_layout *layout_from_notification(cairo_t *c, notification *n)
{ {
colored_layout *cl = layout_init_shared(c, n); struct colored_layout *cl = layout_init_shared(c, n);
/* markup */ /* markup */
GError *err = NULL; GError *err = NULL;
@ -380,7 +381,7 @@ static void free_layouts(GSList *layouts)
g_slist_free_full(layouts, free_colored_layout); g_slist_free_full(layouts, free_colored_layout);
} }
static int layout_get_height(colored_layout *cl) static int layout_get_height(struct colored_layout *cl)
{ {
int h; int h;
int h_icon = 0; int h_icon = 0;
@ -446,8 +447,8 @@ static void draw_rounded_rect(cairo_t *c, int x, int y, int width, int height, i
} }
static cairo_surface_t *render_background(cairo_surface_t *srf, static cairo_surface_t *render_background(cairo_surface_t *srf,
colored_layout *cl, struct colored_layout *cl,
colored_layout *cl_next, struct colored_layout *cl_next,
int y, int y,
int width, int width,
int height, int height,
@ -492,7 +493,7 @@ static cairo_surface_t *render_background(cairo_surface_t *srf,
if ( settings.sep_color != SEP_FRAME if ( settings.sep_color != SEP_FRAME
&& settings.separator_height > 0 && settings.separator_height > 0
&& !last) { && !last) {
color_t sep_color = layout_get_sepcolor(cl, cl_next); struct color sep_color = layout_get_sepcolor(cl, cl_next);
cairo_set_source_rgb(c, sep_color.r, sep_color.g, sep_color.b); cairo_set_source_rgb(c, sep_color.r, sep_color.g, sep_color.b);
cairo_rectangle(c, settings.frame_width, y + height, width, settings.separator_height); cairo_rectangle(c, settings.frame_width, y + height, width, settings.separator_height);
@ -508,7 +509,7 @@ static cairo_surface_t *render_background(cairo_surface_t *srf,
return cairo_surface_create_for_rectangle(srf, x, y, width, height); return cairo_surface_create_for_rectangle(srf, x, y, width, height);
} }
static void render_content(cairo_t *c, colored_layout *cl, int width) static void render_content(cairo_t *c, struct colored_layout *cl, int width)
{ {
const int h = layout_get_height(cl); const int h = layout_get_height(cl);
int h_text; int h_text;
@ -550,8 +551,8 @@ static void render_content(cairo_t *c, colored_layout *cl, int width)
} }
static struct dimensions layout_render(cairo_surface_t *srf, static struct dimensions layout_render(cairo_surface_t *srf,
colored_layout *cl, struct colored_layout *cl,
colored_layout *cl_next, struct colored_layout *cl_next,
struct dimensions dim, struct dimensions dim,
bool first, bool first,
bool last) bool last)
@ -593,7 +594,7 @@ static struct dimensions layout_render(cairo_surface_t *srf,
*/ */
static void calc_window_pos(int width, int height, int *ret_x, int *ret_y) static void calc_window_pos(int width, int height, int *ret_x, int *ret_y)
{ {
screen_info *scr = get_active_screen(); struct screen_info *scr = get_active_screen();
if (ret_x) { if (ret_x) {
if (settings.geometry.negative_x) { if (settings.geometry.negative_x) {
@ -624,8 +625,8 @@ void draw(void)
bool first = true; bool first = true;
for (GSList *iter = layouts; iter; iter = iter->next) { for (GSList *iter = layouts; iter; iter = iter->next) {
colored_layout *cl_this = iter->data; struct colored_layout *cl_this = iter->data;
colored_layout *cl_next = iter->next ? iter->next->data : NULL; struct colored_layout *cl_next = iter->next ? iter->next->data : NULL;
dim = layout_render(image_surface, cl_this, cl_next, dim, first, !cl_next); dim = layout_render(image_surface, cl_this, cl_next, dim, first, !cl_next);

View File

@ -2,7 +2,7 @@
#define DUNST_DRAW_H #define DUNST_DRAW_H
#include "src/x11/x.h" #include "src/x11/x.h"
extern window_x11 *win; // Temporary extern struct window_x11 *win; // Temporary
void draw_setup(void); void draw_setup(void);

View File

@ -80,10 +80,10 @@ typedef struct _settings {
char *icon_path; char *icon_path;
enum follow_mode f_mode; enum follow_mode f_mode;
bool always_run_script; bool always_run_script;
keyboard_shortcut close_ks; struct keyboard_shortcut close_ks;
keyboard_shortcut close_all_ks; struct keyboard_shortcut close_all_ks;
keyboard_shortcut history_ks; struct keyboard_shortcut history_ks;
keyboard_shortcut context_ks; struct keyboard_shortcut context_ks;
bool force_xinerama; bool force_xinerama;
int corner_radius; int corner_radius;
enum mouse_action mouse_left_click; enum mouse_action mouse_left_click;

View File

@ -19,7 +19,7 @@
#include "src/settings.h" #include "src/settings.h"
#include "x.h" #include "x.h"
screen_info *screens; struct screen_info *screens;
int screens_len; int screens_len;
bool dunst_follow_errored = false; bool dunst_follow_errored = false;
@ -80,9 +80,9 @@ void alloc_screen_ar(int n)
assert(n > 0); assert(n > 0);
if (n <= screens_len) return; if (n <= screens_len) return;
screens = g_realloc(screens, n * sizeof(screen_info)); screens = g_realloc(screens, n * sizeof(struct screen_info));
memset(screens, 0, n * sizeof(screen_info)); memset(screens, 0, n * sizeof(struct screen_info));
screens_len = n; screens_len = n;
} }
@ -137,7 +137,7 @@ void randr_update(void)
XRRFreeMonitors(m); XRRFreeMonitors(m);
} }
static int autodetect_dpi(screen_info *scr) static int autodetect_dpi(struct screen_info *scr)
{ {
return (double)scr->h * 25.4 / (double)scr->mmh; return (double)scr->h * 25.4 / (double)scr->mmh;
} }
@ -278,7 +278,7 @@ bool window_is_fullscreen(Window window)
* Select the screen on which the Window * Select the screen on which the Window
* should be displayed. * should be displayed.
*/ */
screen_info *get_active_screen(void) struct screen_info *get_active_screen(void)
{ {
int ret = 0; int ret = 0;
if (settings.monitor > 0 && settings.monitor < screens_len) { if (settings.monitor > 0 && settings.monitor < screens_len) {
@ -351,7 +351,7 @@ sc_cleanup:
return &screens[ret]; return &screens[ret];
} }
double get_dpi_for_screen(screen_info *scr) double get_dpi_for_screen(struct screen_info *scr)
{ {
double dpi = 0; double dpi = 0;
if ((!settings.force_xinerama && settings.per_monitor_dpi && if ((!settings.force_xinerama && settings.per_monitor_dpi &&

View File

@ -7,20 +7,20 @@
#define INRECT(x,y,rx,ry,rw,rh) ((x) >= (rx) && (x) < (rx)+(rw) && (y) >= (ry) && (y) < (ry)+(rh)) #define INRECT(x,y,rx,ry,rw,rh) ((x) >= (rx) && (x) < (rx)+(rw) && (y) >= (ry) && (y) < (ry)+(rh))
typedef struct { struct screen_info {
int id; int id;
int x; int x;
int y; int y;
unsigned int h; unsigned int h;
unsigned int mmh; unsigned int mmh;
unsigned int w; unsigned int w;
} screen_info; };
void init_screens(void); void init_screens(void);
void screen_check_event(XEvent event); void screen_check_event(XEvent event);
screen_info *get_active_screen(void); struct screen_info *get_active_screen(void);
double get_dpi_for_screen(screen_info *scr); double get_dpi_for_screen(struct screen_info *scr);
/** /**
* Find the currently focused window and check if it's in * Find the currently focused window and check if it's in

View File

@ -46,24 +46,24 @@ struct window_x11 {
struct x11_source { struct x11_source {
GSource source; GSource source;
window_x11 *win; struct window_x11 *win;
}; };
xctx_t xctx; struct x_context xctx;
bool dunst_grab_errored = false; bool dunst_grab_errored = false;
static bool fullscreen_last = false; static bool fullscreen_last = false;
static void x_shortcut_init(keyboard_shortcut *ks); static void x_shortcut_init(struct keyboard_shortcut *ks);
static int x_shortcut_grab(keyboard_shortcut *ks); static int x_shortcut_grab(struct keyboard_shortcut *ks);
static void x_shortcut_ungrab(keyboard_shortcut *ks); static void x_shortcut_ungrab(struct keyboard_shortcut *ks);
/* FIXME refactor setup teardown handlers into one setup and one teardown */ /* FIXME refactor setup teardown handlers into one setup and one teardown */
static void x_shortcut_setup_error_handler(void); static void x_shortcut_setup_error_handler(void);
static int x_shortcut_tear_down_error_handler(void); static int x_shortcut_tear_down_error_handler(void);
static void setopacity(Window win, unsigned long opacity); static void setopacity(Window win, unsigned long opacity);
static void x_handle_click(XEvent ev); static void x_handle_click(XEvent ev);
static void x_win_move(window_x11 *win, int x, int y, int width, int height) static void x_win_move(struct window_x11 *win, int x, int y, int width, int height)
{ {
/* move and resize */ /* move and resize */
if (x != win->dim.x || y != win->dim.y) { if (x != win->dim.x || y != win->dim.y) {
@ -81,7 +81,7 @@ static void x_win_move(window_x11 *win, int x, int y, int width, int height)
} }
} }
static void x_win_round_corners(window_x11 *win, const int rad) static void x_win_round_corners(struct window_x11 *win, const int rad)
{ {
const int width = win->dim.w; const int width = win->dim.w;
const int height = win->dim.h; const int height = win->dim.h;
@ -148,7 +148,7 @@ static void x_win_round_corners(window_x11 *win, const int rad)
win->xwin, ShapeNotifyMask); win->xwin, ShapeNotifyMask);
} }
void x_display_surface(cairo_surface_t *srf, window_x11 *win, const struct dimensions *dim) void x_display_surface(cairo_surface_t *srf, struct window_x11 *win, const struct dimensions *dim)
{ {
x_win_move(win, dim->x, dim->y, dim->w, dim->h); x_win_move(win, dim->x, dim->y, dim->w, dim->h);
cairo_xlib_surface_set_size(win->root_surface, dim->w, dim->h); cairo_xlib_surface_set_size(win->root_surface, dim->w, dim->h);
@ -164,12 +164,12 @@ void x_display_surface(cairo_surface_t *srf, window_x11 *win, const struct dimen
} }
bool x_win_visible(window_x11 *win) bool x_win_visible(struct window_x11 *win)
{ {
return win->visible; return win->visible;
} }
cairo_t* x_win_get_context(window_x11 *win) cairo_t* x_win_get_context(struct window_x11 *win)
{ {
return win->c_ctx; return win->c_ctx;
} }
@ -266,10 +266,10 @@ gboolean x_mainloop_fd_check(GSource *source)
*/ */
gboolean x_mainloop_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data) gboolean x_mainloop_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
{ {
window_x11 *win = ((struct x11_source*) source)->win; struct window_x11 *win = ((struct x11_source*) source)->win;
bool fullscreen_now; bool fullscreen_now;
screen_info *scr; struct screen_info *scr;
XEvent ev; XEvent ev;
unsigned int state; unsigned int state;
while (XPending(xctx.dpy) > 0) { while (XPending(xctx.dpy) > 0) {
@ -571,7 +571,7 @@ static void x_set_wm(Window win)
PropModeReplace, (unsigned char *) data, 1L); PropModeReplace, (unsigned char *) data, 1L);
} }
GSource* x_win_reg_source(window_x11 *win) GSource* x_win_reg_source(struct window_x11 *win)
{ {
// Static is necessary here because glib keeps the pointer and we need // Static is necessary here because glib keeps the pointer and we need
// to keep the reference alive. // to keep the reference alive.
@ -599,9 +599,9 @@ GSource* x_win_reg_source(window_x11 *win)
/* /*
* Setup the window * Setup the window
*/ */
window_x11 *x_win_create(void) struct window_x11 *x_win_create(void)
{ {
window_x11 *win = g_malloc0(sizeof(window_x11)); struct window_x11 *win = g_malloc0(sizeof(struct window_x11));
Window root; Window root;
XSetWindowAttributes wa; XSetWindowAttributes wa;
@ -614,7 +614,7 @@ window_x11 *x_win_create(void)
ExposureMask | KeyPressMask | VisibilityChangeMask | ExposureMask | KeyPressMask | VisibilityChangeMask |
ButtonReleaseMask | FocusChangeMask| StructureNotifyMask; ButtonReleaseMask | FocusChangeMask| StructureNotifyMask;
screen_info *scr = get_active_screen(); struct screen_info *scr = get_active_screen();
win->xwin = XCreateWindow(xctx.dpy, win->xwin = XCreateWindow(xctx.dpy,
root, root,
scr->x, scr->x,
@ -651,7 +651,7 @@ window_x11 *x_win_create(void)
return win; return win;
} }
void x_win_destroy(window_x11 *win) void x_win_destroy(struct window_x11 *win)
{ {
g_source_destroy(win->esrc); g_source_destroy(win->esrc);
g_source_unref(win->esrc); g_source_unref(win->esrc);
@ -666,7 +666,7 @@ void x_win_destroy(window_x11 *win)
/* /*
* Show the window and grab shortcuts. * Show the window and grab shortcuts.
*/ */
void x_win_show(window_x11 *win) void x_win_show(struct window_x11 *win)
{ {
/* window is already mapped or there's nothing to show */ /* window is already mapped or there's nothing to show */
if (win->visible || queues_length_displayed() == 0) { if (win->visible || queues_length_displayed() == 0) {
@ -699,7 +699,7 @@ void x_win_show(window_x11 *win)
/* /*
* Hide the window and ungrab unused keyboard_shortcuts * Hide the window and ungrab unused keyboard_shortcuts
*/ */
void x_win_hide(window_x11 *win) void x_win_hide(struct window_x11 *win)
{ {
x_shortcut_ungrab(&settings.close_ks); x_shortcut_ungrab(&settings.close_ks);
x_shortcut_ungrab(&settings.close_all_ks); x_shortcut_ungrab(&settings.close_all_ks);
@ -777,7 +777,7 @@ static int x_shortcut_tear_down_error_handler(void)
/* /*
* Grab the given keyboard shortcut. * Grab the given keyboard shortcut.
*/ */
static int x_shortcut_grab(keyboard_shortcut *ks) static int x_shortcut_grab(struct keyboard_shortcut *ks)
{ {
if (!ks->is_valid) if (!ks->is_valid)
return 1; return 1;
@ -814,7 +814,7 @@ static int x_shortcut_grab(keyboard_shortcut *ks)
/* /*
* Ungrab the given keyboard shortcut. * Ungrab the given keyboard shortcut.
*/ */
static void x_shortcut_ungrab(keyboard_shortcut *ks) static void x_shortcut_ungrab(struct keyboard_shortcut *ks)
{ {
Window root; Window root;
root = RootWindow(xctx.dpy, DefaultScreen(xctx.dpy)); root = RootWindow(xctx.dpy, DefaultScreen(xctx.dpy));
@ -827,7 +827,7 @@ static void x_shortcut_ungrab(keyboard_shortcut *ks)
/* /*
* Initialize the keyboard shortcut. * Initialize the keyboard shortcut.
*/ */
static void x_shortcut_init(keyboard_shortcut *ks) static void x_shortcut_init(struct keyboard_shortcut *ks)
{ {
if (!ks|| !ks->str) if (!ks|| !ks->str)
return; return;

View File

@ -13,18 +13,18 @@
#include "screen.h" #include "screen.h"
typedef struct _keyboard_shortcut { struct keyboard_shortcut {
const char *str; const char *str;
KeyCode code; KeyCode code;
KeySym sym; KeySym sym;
KeySym mask; KeySym mask;
bool is_valid; bool is_valid;
} keyboard_shortcut; };
// Cyclical dependency // Cyclical dependency
#include "src/settings.h" #include "src/settings.h"
typedef struct window_x11 window_x11; struct window_x11;
struct dimensions { struct dimensions {
int x; int x;
@ -35,31 +35,31 @@ struct dimensions {
int corner_radius; int corner_radius;
}; };
typedef struct _xctx { struct x_context {
Display *dpy; Display *dpy;
const char *colors[3][3]; const char *colors[3][3];
XScreenSaverInfo *screensaver_info; XScreenSaverInfo *screensaver_info;
} xctx_t; };
typedef struct _color_t { struct color {
double r; double r;
double g; double g;
double b; double b;
} color_t; };
extern xctx_t xctx; extern struct x_context xctx;
/* window */ /* window */
window_x11 *x_win_create(void); struct window_x11 *x_win_create(void);
void x_win_destroy(window_x11 *win); void x_win_destroy(struct window_x11 *win);
void x_win_show(window_x11 *win); void x_win_show(struct window_x11 *win);
void x_win_hide(window_x11 *win); void x_win_hide(struct window_x11 *win);
void x_display_surface(cairo_surface_t *srf, window_x11 *win, const struct dimensions *dim); void x_display_surface(cairo_surface_t *srf, struct window_x11 *win, const struct dimensions *dim);
bool x_win_visible(window_x11 *win); bool x_win_visible(struct window_x11 *win);
cairo_t* x_win_get_context(window_x11 *win); cairo_t* x_win_get_context(struct window_x11 *win);
/* X misc */ /* X misc */
bool x_is_idle(void); bool x_is_idle(void);