main.c (6450B)
1 #include <X11/Xresource.h> 2 #include <X11/Xutil.h> 3 #include <WMaker.h> 4 #include <WINGs/WINGs.h> 5 #include <stdarg.h> 6 #include <stdio.h> 7 #include <string.h> 8 #include <stdlib.h> 9 #include <unistd.h> 10 11 #include "app.h" 12 #include "infopanel.h" 13 #include "mainwindow.h" 14 #include "menu.h" 15 #include "windowmanager.h" 16 17 #include "config.h" 18 19 void 20 debug_print_state(struct WMNewApp* wma) 21 { 22 WMLog(wma, "State dump:\n"); 23 WMLog(wma, "===================================\n"); 24 WMLog(wma, "dirty:\t\t%s\n", wma->state.dirty ? "YES" : "NO"); 25 WMLog(wma, "filename:\t[%s]\n", wma->state.filename); 26 WMLog(wma, "file_open:\t[%s]\n", wma->state.file_open ? "YES" : "NO"); 27 } 28 29 int 30 error(const int code, const char* msg, ...) 31 { 32 va_list arg; 33 34 va_start(arg, msg); 35 vfprintf(stderr, msg, arg); 36 va_end(arg); 37 38 return code; 39 } 40 41 static void 42 do_exit(struct WMNewApp* wma) 43 { 44 exit(0); 45 } 46 47 void 48 exit_action(WMWidget* self, void* data) 49 { 50 struct WMNewApp* wma = (struct WMNewApp*)data; 51 WMLog(wma, "exit_action called\n"); 52 if (!wma->state.dirty) 53 do_exit(wma); 54 55 switch (WMRunAlertPanel(wma->scr, NULL, "Confirm", 56 "Document %s is modified. Save changes?", "Yes", "No", "Cancel")) 57 { 58 case 0: 59 WMLog(wma, "Saving changes!\n"); 60 do_exit(wma); 61 break; 62 case 1: 63 WMLog(wma, "NOT saving changes!\n"); 64 do_exit(wma); 65 break; 66 default: 67 WMLog(wma, "Canceled exit!\n"); 68 } 69 } 70 71 void 72 exit_menu_action(void* data, int code, Time timestamp) 73 { 74 struct WMNewApp* wma = (struct WMNewApp*)data; 75 WMLog(wma, "exit_menu_action called\n"); 76 exit_action(wma->mainwin->window, wma); 77 } 78 79 void 80 handle_keypress(XEvent* event, void* data) 81 { 82 struct WMNewApp* wma = (struct WMNewApp*)data; 83 int count; 84 char buffer[64]; 85 KeySym ksym; 86 int ctrl_pressed = 0; 87 int meta_pressed = 0; 88 int shift_pressed = 0; 89 90 WMLog(wma, "handle_keypress called\n"); 91 92 count = XLookupString(&event->xkey, buffer, 63, &ksym, NULL); 93 buffer[count] = 0; 94 95 WMLog(wma, "\tbuffer: %s\n", buffer); 96 WMLog(wma, "\tksym: 0x%lX\n", ksym); 97 WMLog(wma, "\tstate: %d\n", event->xkey.state); 98 99 ctrl_pressed = event->xkey.state & ControlMask; 100 meta_pressed = event->xkey.state & Mod1Mask; 101 shift_pressed = event->xkey.state & ShiftMask; 102 103 WMLog(wma, "\tctrl_pressed: %s\n", ctrl_pressed ? "YES" : "NO"); 104 WMLog(wma, "\tmeta_pressed: %s\n", meta_pressed ? "YES" : "NO"); 105 WMLog(wma, "\tshift_pressed: %s\n", shift_pressed ? "YES" : "NO"); 106 107 switch (ksym) 108 { 109 case XK_F1: 110 info_panel_action(wma->mainwin->window, wma); 111 break; 112 case XK_o: 113 if (ctrl_pressed && !meta_pressed && !shift_pressed) 114 open_action(wma->mainwin->window, wma); 115 break; 116 case XK_s: 117 if (ctrl_pressed && !meta_pressed && !shift_pressed) 118 save_action(wma->mainwin->window, wma); 119 break; 120 case XK_q: 121 if (ctrl_pressed && !meta_pressed && !shift_pressed) 122 exit_action(wma->mainwin->window, wma); 123 break; 124 } 125 } 126 127 void 128 info_panel_action(WMWidget* self, void* data) 129 { 130 struct WMNewApp* wma = (struct WMNewApp*)data; 131 WMLog(wma, "info_panel_action called\n"); 132 run_info_panel(wma); 133 } 134 135 void 136 info_panel_menu_action(void* data, int code, Time timestamp) 137 { 138 struct WMNewApp* wma = (struct WMNewApp*)data; 139 WMLog(wma, "info_menu_action called\n"); 140 info_panel_action(wma->mainwin->window, wma); 141 } 142 143 void 144 init_state(struct State* state) 145 { 146 state->dirty = 0; 147 *state->filename = 0; 148 state->file_open = 0; 149 } 150 151 void 152 not_implemented(WMWidget* self, void* data) 153 { 154 struct WMNewApp* wma = (struct WMNewApp*)data; 155 WMRunAlertPanel(wma->scr, NULL, "Sorry", 156 "Function is not yet implemented", "Ok", NULL, NULL); 157 } 158 159 void 160 open_action(WMWidget* self, void* data) 161 { 162 struct WMNewApp* wma = (struct WMNewApp*)data; 163 char wd[PATH_MAX]; 164 char* filename; 165 char* last_slash; 166 167 WMLog(wma, "open_action called\n"); 168 getcwd(wd, PATH_MAX); 169 if (!WMRunModalFilePanelForDirectory(WMGetOpenPanel(wma->scr), 170 wma->mainwin->window, wd, "Open file", NULL)) 171 return; 172 filename = WMGetFilePanelFileName(WMGetOpenPanel(wma->scr)); 173 last_slash = strrchr(filename, '/'); 174 if (last_slash) 175 strncpy(wma->state.filename, last_slash + 1, PATH_MAX); 176 else 177 strncpy(wma->state.filename, filename, PATH_MAX); 178 wma->state.file_open = 1; 179 set_mainwin_title(wma); 180 WMLog(wma, "opened file: %s (full path: %s)\n", 181 last_slash ? last_slash + 1 : filename, filename); 182 free(filename); 183 } 184 185 void 186 open_menu_action(void* data, int code, Time timestamp) 187 { 188 struct WMNewApp* wma = (struct WMNewApp*)data; 189 WMLog(wma, "open_menu_action called\n"); 190 open_action(wma->mainwin->window, wma); 191 } 192 193 void 194 save_action(WMWidget* self, void* data) 195 { 196 struct WMNewApp* wma = (struct WMNewApp*)data; 197 WMLog(wma, "save_action called\n"); 198 not_implemented(self, wma); 199 } 200 201 void 202 save_menu_action(void* data, int code, Time timestamp) 203 { 204 struct WMNewApp* wma = (struct WMNewApp*)data; 205 WMLog(wma, "save_menu_action called\n"); 206 save_action(wma->mainwin->window, wma); 207 } 208 209 void 210 usage(const char* arg0) 211 { 212 printf("Usage: %s [-d] [-h]\n", arg0); 213 printf("\t-d\tDebug mode\n"); 214 printf("\t-h\tPrint this help screen\n"); 215 } 216 217 void 218 WMLog(const struct WMNewApp* wma, const char* msg, ...) 219 { 220 va_list arg; 221 222 if (!wma->state.debug) 223 return; 224 225 va_start(arg, msg); 226 fprintf(stderr, "%s: ", program_name); 227 vfprintf(stderr, msg, arg); 228 va_end(arg); 229 } 230 231 int 232 main(int argc, char** argv) 233 { 234 struct WMNewApp wma; 235 int dbg = 0; 236 int opt; 237 238 while ((opt = getopt(argc, argv, "dh")) != -1) 239 { 240 switch (opt) 241 { 242 case 'd': 243 dbg = 1; 244 break; 245 case 'h': 246 usage(argv[0]); 247 exit(0); 248 break; 249 default: 250 exit(error(1, "Invalid argument: -%c", opt)); 251 } 252 } 253 254 init_state(&wma.state); 255 wma.state.debug = dbg; 256 // wma.state.dirty = 1; 257 258 WMInitializeApplication("WMNewApp", &argc, argv); 259 260 if (!(wma.dpy = XOpenDisplay(""))) 261 exit(error(1, "XOpenDisplay failed")); 262 263 if (!(wma.scr = WMCreateScreen(wma.dpy, DefaultScreen(wma.dpy)))) 264 exit(error(1, "WMCreateScreen failed")); 265 266 create_main_window(&wma); 267 268 wma.app = WMAppCreateWithMain(wma.dpy, DefaultScreen(wma.dpy), 269 WMWidgetXID(wma.mainwin->window)); 270 XSetCommand(wma.dpy, WMWidgetXID(wma.mainwin->window), argv, argc); 271 WMSetWindowCloseAction(wma.mainwin->window, exit_action, &wma); 272 273 create_icon(&wma); 274 create_main_menu(&wma); 275 map_main_window(&wma); 276 277 WMSetFocusToWidget(wma.mainwin->window); 278 WMCreateEventHandler(WMWidgetView(wma.mainwin->window), KeyPressMask, 279 handle_keypress, &wma); 280 281 XFlush(wma.dpy); 282 283 while (1) 284 { 285 XEvent event; 286 WMNextEvent(wma.dpy, &event); 287 WMHandleEvent(&event); 288 WMProcessEvent(wma.app, &event); 289 } 290 291 destroy_main_window(&wma); 292 293 return 0; 294 }