2 #include <X11/Xft/Xft.h>
14 int get_max_len(char *body
, XftFont
*font
, int max_text_width
)
16 int body_len
= strlen(body
);
18 XftTextExtentsUtf8(display
, font
, (FcChar8
*)body
, body_len
, &info
);
20 if (info
.width
< max_text_width
)
23 int eol
= max_text_width
/ font
->max_advance_width
;
26 while (info
.width
< max_text_width
)
29 XftTextExtentsUtf8(display
, font
, (FcChar8
*)body
, eol
, &info
);
36 while (body
[eol
] != ' ' && eol
)
48 event
.type
= ButtonPress
;
49 XSendEvent(display
, window
, 0, 0, &event
);
53 int main(int argc
, char *argv
[])
57 fprintf(stderr
, "Usage: herbe body\n");
61 signal(SIGALRM
, expire
);
64 display
= XOpenDisplay(0);
68 fprintf(stderr
, "Cannot open display\n");
72 int screen
= DefaultScreen(display
);
73 Visual
*visual
= DefaultVisual(display
, screen
);
74 Colormap colormap
= DefaultColormap(display
, screen
);
76 int screen_width
= DisplayWidth(display
, screen
);
77 int screen_height
= DisplayHeight(display
, screen
);
81 XSetWindowAttributes attributes
;
82 attributes
.override_redirect
= True
;
83 XftColorAllocName(display
, visual
, colormap
, background_color
, &color
);
84 attributes
.background_pixel
= color
.pixel
;
85 XftColorAllocName(display
, visual
, colormap
, border_color
, &color
);
86 attributes
.border_pixel
= color
.pixel
;
88 XftFont
*font
= XftFontOpenName(display
, screen
, font_pattern
);
91 int max_text_width
= width
- 2 * padding
;
92 // TODO replace hard-coded size 100
93 char words
[100][max_text_width
/ font
->max_advance_width
+ 2];
95 for (int i
= 1; i
< argc
; i
++)
99 for (unsigned int eol
= get_max_len(body
, font
, max_text_width
); eol
<= strlen(body
) && eol
; body
+= eol
, num_of_lines
++, eol
= get_max_len(body
, font
, max_text_width
))
101 strncpy(words
[num_of_lines
], body
, eol
);
102 words
[num_of_lines
][eol
] = '\0';
106 unsigned int x
= pos_x
;
107 unsigned int y
= pos_y
;
108 unsigned int text_height
= font
->ascent
- font
->descent
;
109 unsigned int height
= (num_of_lines
- 1) * line_spacing
+ num_of_lines
* text_height
+ 2 * padding
;
111 if (corner
== TOP_RIGHT
|| corner
== BOTTOM_RIGHT
)
112 x
= screen_width
- width
- border_size
* 2 - pos_x
;
114 if (corner
== BOTTOM_LEFT
|| corner
== BOTTOM_RIGHT
)
115 y
= screen_height
- height
- border_size
* 2 - pos_y
;
117 window
= XCreateWindow(display
, RootWindow(display
, screen
), x
, y
, width
, height
, border_size
, DefaultDepth(display
, screen
), CopyFromParent
, visual
,
118 CWOverrideRedirect
| CWBackPixel
| CWBorderPixel
, &attributes
);
120 XftDraw
*draw
= XftDrawCreate(display
, window
, visual
, colormap
);
121 XftColorAllocName(display
, visual
, colormap
, font_color
, &color
);
123 XSelectInput(display
, window
, ExposureMask
| ButtonPress
);
125 XMapWindow(display
, window
);
130 XNextEvent(display
, &event
);
132 if (event
.type
== Expose
)
134 XClearWindow(display
, window
);
135 for (int i
= 0; i
< num_of_lines
; i
++)
136 XftDrawStringUtf8(draw
, &color
, font
, padding
, line_spacing
* i
+ text_height
* (i
+ 1) + padding
, (FcChar8
*)words
[i
], strlen(words
[i
]));
138 if (event
.type
== ButtonPress
)
142 XftDrawDestroy(draw
);
143 XftColorFree(display
, visual
, colormap
, &color
);
144 XftFontClose(display
, font
);
145 XCloseDisplay(display
);