2 #include <X11/Xft/Xft.h>
10 #include <semaphore.h>
16 #define EXIT_DISMISS 2
20 int exit_code
= EXIT_DISMISS
;
22 static void die(const char *format
, ...)
26 vfprintf(stderr
, format
, ap
);
27 fprintf(stderr
, "\n");
32 int get_max_len(char *string
, XftFont
*font
, int max_text_width
)
34 int eol
= strlen(string
);
36 XftTextExtentsUtf8(display
, font
, (FcChar8
*)string
, eol
, &info
);
38 if (info
.width
> max_text_width
)
40 eol
= max_text_width
/ font
->max_advance_width
;
43 while (info
.width
< max_text_width
)
46 XftTextExtentsUtf8(display
, font
, (FcChar8
*)string
, eol
, &info
);
52 for (int i
= 0; i
< eol
; i
++)
53 if (string
[i
] == '\n')
59 if (info
.width
<= max_text_width
)
64 while (string
[eol
] != ' ' && eol
)
76 event
.type
= ButtonPress
;
77 event
.xbutton
.button
= (sig
== SIGUSR2
) ? (ACTION_BUTTON
) : (DISMISS_BUTTON
);
78 XSendEvent(display
, window
, 0, 0, &event
);
82 int main(int argc
, char *argv
[])
87 die("Usage: %s body", argv
[0]);
90 struct sigaction act_expire
, act_ignore
;
92 act_expire
.sa_handler
= expire
;
93 act_expire
.sa_flags
= SA_RESTART
;
94 sigemptyset(&act_expire
.sa_mask
);
96 act_ignore
.sa_handler
= SIG_IGN
;
97 act_ignore
.sa_flags
= 0;
98 sigemptyset(&act_ignore
.sa_mask
);
100 sigaction(SIGALRM
, &act_expire
, 0);
101 sigaction(SIGTERM
, &act_expire
, 0);
102 sigaction(SIGINT
, &act_expire
, 0);
104 sigaction(SIGUSR1
, &act_ignore
, 0);
105 sigaction(SIGUSR2
, &act_ignore
, 0);
107 if (!(display
= XOpenDisplay(0)))
108 die("Cannot open display");
110 int screen
= DefaultScreen(display
);
111 Visual
*visual
= DefaultVisual(display
, screen
);
112 Colormap colormap
= DefaultColormap(display
, screen
);
114 int screen_width
= DisplayWidth(display
, screen
);
115 int screen_height
= DisplayHeight(display
, screen
);
117 XSetWindowAttributes attributes
;
118 attributes
.override_redirect
= True
;
120 XftColorAllocName(display
, visual
, colormap
, background_color
, &color
);
121 attributes
.background_pixel
= color
.pixel
;
122 XftColorAllocName(display
, visual
, colormap
, border_color
, &color
);
123 attributes
.border_pixel
= color
.pixel
;
125 int num_of_lines
= 0;
126 int max_text_width
= width
- 2 * padding
;
128 char **lines
= malloc(lines_size
* sizeof(char *));
130 die("malloc failed");
132 XftFont
*font
= XftFontOpenName(display
, screen
, font_pattern
);
134 for (int i
= 1; i
< argc
; i
++)
136 for (unsigned int eol
= get_max_len(argv
[i
], font
, max_text_width
); eol
; argv
[i
] += eol
, num_of_lines
++, eol
= get_max_len(argv
[i
], font
, max_text_width
))
138 if (lines_size
<= num_of_lines
)
140 lines
= realloc(lines
, (lines_size
+= 5) * sizeof(char *));
142 die("realloc failed");
145 lines
[num_of_lines
] = malloc((eol
+ 1) * sizeof(char));
146 if (!lines
[num_of_lines
])
147 die("malloc failed");
149 strncpy(lines
[num_of_lines
], argv
[i
], eol
);
150 lines
[num_of_lines
][eol
] = '\0';
154 unsigned int x
= pos_x
;
155 unsigned int y
= pos_y
;
156 unsigned int text_height
= font
->ascent
- font
->descent
;
157 unsigned int height
= (num_of_lines
- 1) * line_spacing
+ num_of_lines
* text_height
+ 2 * padding
;
159 if (corner
== TOP_RIGHT
|| corner
== BOTTOM_RIGHT
)
160 x
= screen_width
- width
- border_size
* 2 - pos_x
;
162 if (corner
== BOTTOM_LEFT
|| corner
== BOTTOM_RIGHT
)
163 y
= screen_height
- height
- border_size
* 2 - pos_y
;
165 window
= XCreateWindow(display
, RootWindow(display
, screen
), x
, y
, width
, height
, border_size
, DefaultDepth(display
, screen
),
166 CopyFromParent
, visual
, CWOverrideRedirect
| CWBackPixel
| CWBorderPixel
, &attributes
);
168 XftDraw
*draw
= XftDrawCreate(display
, window
, visual
, colormap
);
169 XftColorAllocName(display
, visual
, colormap
, font_color
, &color
);
171 XSelectInput(display
, window
, ExposureMask
| ButtonPress
);
172 XMapWindow(display
, window
);
174 sem_t
*mutex
= sem_open("/herbe", O_CREAT
, 0644, 1);
177 sigaction(SIGUSR1
, &act_expire
, 0);
178 sigaction(SIGUSR2
, &act_expire
, 0);
186 XNextEvent(display
, &event
);
188 if (event
.type
== Expose
)
190 XClearWindow(display
, window
);
191 for (int i
= 0; i
< num_of_lines
; i
++)
192 XftDrawStringUtf8(draw
, &color
, font
, padding
, line_spacing
* i
+ text_height
* (i
+ 1) + padding
,
193 (FcChar8
*)lines
[i
], strlen(lines
[i
]));
195 else if (event
.type
== ButtonPress
)
197 if (event
.xbutton
.button
== DISMISS_BUTTON
)
199 else if (event
.xbutton
.button
== ACTION_BUTTON
)
201 exit_code
= EXIT_ACTION
;
210 for (int i
= 0; i
< num_of_lines
; i
++)
214 XftDrawDestroy(draw
);
215 XftColorFree(display
, visual
, colormap
, &color
);
216 XftFontClose(display
, font
);
217 XCloseDisplay(display
);