Remove trailing whitespace
[dockapps.git] / wmframepic / src / main.c
blobead268a42faa16621e1b8551a5251781fab7415b
1 #include "main.h"
2 #include "age_calc.h"
3 #include "arg_parser.h"
4 #include <signal.h>
6 static int offset_w, offset_h;
7 static char name[200];
8 static char *file;
9 static int day, month, year;
10 static Pixmap *background_pixmap;
12 void free_resources() {
13 XFreeGC(display, gc_core);
14 XFreeGC(display, gc_border);
17 void sig_int() {
18 free_resources();
19 printf("\nThank you for using wmframepic\n");
20 exit(1);
24 void draw_screen_with_mask() {
25 XNextEvent(display, &event);
27 while(event.type != Expose)
30 XCopyArea(display, *background_pixmap, iconwin, gc_border, 0, 0, 64, 64, offset_w, offset_h);
31 XCopyArea(display, pic_pixmap, iconwin, gc_core, 0, 0, 64, 64, offset_w, offset_h);
32 XFlush(display);
36 void mainloop() {
37 XButtonPressedEvent *bevent;
38 for (;;) {
39 XNextEvent(display, &event);
40 switch (event.type) {
41 case Expose:
42 XCopyArea(display, *background_pixmap, iconwin, gc_border, 0, 0, 64, 64, offset_w, offset_h);
43 XCopyArea(display, pic_pixmap, iconwin, gc_core, 0, 0, 64, 64, offset_w, offset_h);
44 break;
45 case ButtonPress:
46 bevent = (XButtonPressedEvent *) &event;
47 switch (bevent->button & 0xff) {
48 case Button1:
49 case Button2:
50 case Button3:
51 if(show_pic) {
52 XCopyArea(display, pic_pixmap, iconwin, gc_core, 0, 0, 64, 64, offset_w, offset_h);
53 show_pic = False;
54 } else {
55 restore_background();
56 draw_text(name, 3, 8, False);
57 char **phrases = get_phrases(day, month, year);
58 int i;
59 for(i = 0; i < NUMBER_OF_ROWS; i++) {
60 draw_text(phrases[i], 3, 23 + 10 * i, False);
63 clear_phrases(phrases);
64 flush_background();
65 show_pic = True;
68 break;
69 default:
70 break;
72 break;
73 default:
74 break;
79 int main(int argc, char **argv) {
80 int ww, wh, w, h;
81 w = SIZE;
82 h = SIZE;
83 Bool dockapp_iswindowed = False;
84 int error;
85 XWMHints *wmHints;
86 XClassHint *classHint;
88 if(signal(SIGINT, sig_int) == SIG_ERR)
89 fprintf(stderr, "signal error\n");
91 error = get_values_from_command_line(argc, argv, name, &file, &day, &month, &year);
93 if(COMMAND_LINE_FAIL == error || COMMAND_LINE_HELP == error) {
94 return 1;
98 //_Xdebug = 1;
99 if( ( display = XOpenDisplay(NULL) ) == NULL ){
100 fprintf(stderr, "Error: XOpenDisplay\n");
101 exit(1);
104 root = RootWindow(display, screen = DefaultScreen(display));
105 XGCValues values;
106 values.foreground = BlackPixel(display, screen);
107 values.background = WhitePixel(display, screen);
111 Pixmap pixmask_border = XCreateBitmapFromData(display, root, frame_mask_bits,
112 frame_mask_width,
113 frame_mask_height);
115 gc_border = XCreateGC(display, root, GCForeground | GCBackground, &values);
116 XSetClipMask(display, gc_border, pixmask_border);
118 Pixmap pixmask_core = XCreateBitmapFromData(display, root, core_mask_bits,
119 core_mask_width,
120 core_mask_height);
123 gc_core = XCreateGC(display, root, GCForeground | GCBackground, &values);
124 XSetClipMask(display, gc_core, pixmask_core);
126 XFreePixmap(display, pixmask_border);
127 XFreePixmap(display, pixmask_core);
129 if (dockapp_iswindowed) {
130 offset_w = (WINDOWED_SIZE_W - w) / 2;
131 offset_h = (WINDOWED_SIZE_H - h) / 2;
132 ww = WINDOWED_SIZE_W;
133 wh = WINDOWED_SIZE_H;
134 } else {
135 offset_w = offset_h = 0;
136 ww = w;
137 wh = h;
140 sizehints.flags = USSize | USPosition;
141 if (!dockapp_iswindowed) {
142 sizehints.flags |= USPosition;
143 sizehints.x = sizehints.y = 0;
144 } else {
145 sizehints.flags |= PMinSize | PMaxSize;
146 sizehints.min_width = sizehints.max_width = WINDOWED_SIZE_W;
147 sizehints.min_height = sizehints.max_height = WINDOWED_SIZE_H;
149 sizehints.width = ww;
150 sizehints.height = wh;
152 win = XCreateSimpleWindow(display, root, 0, 0, ww, wh, depth, values.foreground,
153 values.background);
155 iconwin = XCreateSimpleWindow(display, root, 0, 0, ww, wh, depth, values.foreground,
156 values.background);
160 XSetWMNormalHints(display, win, &sizehints);
162 wmHints = XAllocWMHints();
163 if (wmHints == NULL) {
164 fprintf(stderr, "%s: can't allocate memory for wm hints!\n", argv[0]);
165 exit(1);
168 wmHints->initial_state = WithdrawnState;
169 wmHints->icon_window = iconwin;
170 wmHints->icon_x = sizehints.x;
171 wmHints->icon_y = sizehints.y;
172 wmHints->window_group = win;
173 wmHints->flags = StateHint|IconWindowHint|IconPositionHint|WindowGroupHint;
174 XSetWMHints(display, win, wmHints);
175 XFree(wmHints);
177 classHint = XAllocClassHint();
178 if (classHint == NULL) {
179 fprintf(stderr, "%s: can't allocate memory for wm hints!\n", argv[0]);
180 exit(1);
183 classHint->res_name = argv[0];
184 classHint->res_class = argv[0];
185 XSetClassHint(display, win, classHint);
186 XFree(classHint);
187 // Let the window manager know about the link of commands
188 XSetCommand(display, win, argv, argc);
190 //XpmCreatePixmapFromData(display, root, kid_xpm, &pic_pixmap,NULL,NULL);
191 char **xpm_file_data;
192 if(-1 == XpmReadFileToData(file, &xpm_file_data)) {
193 printf("could not read from file %s\nplease check file path\n", file);
194 return 1;
197 XpmCreatePixmapFromData(display, root, xpm_file_data, &pic_pixmap,NULL,NULL);
198 XSelectInput(display, win, eventmask);
199 XSelectInput(display,iconwin, eventmask);
200 XMapWindow(display, win);
202 init_variables(display, gc_core, iconwin, offset_w, offset_h, &background_pixmap);
204 draw_screen_with_mask();
205 show_pic = False;
207 mainloop();
208 return 0;