| 1 |
/////////////////////////////////////////////////////////////////////////////// |
| 2 |
// Free42 -- an HP-42S calculator simulator |
| 3 |
// Copyright (C) 2004-2026 Thomas Okken |
| 4 |
// |
| 5 |
// This program is free software; you can redistribute it and/or modify |
| 6 |
// it under the terms of the GNU General Public License, version 2, |
| 7 |
// as published by the Free Software Foundation. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU General Public License |
| 15 |
// along with this program; if not, see http://www.gnu.org/licenses/. |
| 16 |
/////////////////////////////////////////////////////////////////////////////// |
| 17 |
|
| 18 |
#include <gtk/gtk.h> |
| 19 |
#include <gdk/gdkkeysyms.h> |
| 20 |
#include <errno.h> |
| 21 |
#include <locale.h> |
| 22 |
#include <pwd.h> |
| 23 |
#include <signal.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
#include <sys/stat.h> |
| 28 |
#include <sys/time.h> |
| 29 |
#include <sys/types.h> |
| 30 |
#include <dirent.h> |
| 31 |
#include <unistd.h> |
| 32 |
#include <time.h> |
| 33 |
|
| 34 |
#include "shell.h" |
| 35 |
#include "shell_main.h" |
| 36 |
#include "shell_skin.h" |
| 37 |
#include "shell_spool.h" |
| 38 |
#include "core_main.h" |
| 39 |
#include "core_display.h" |
| 40 |
|
| 41 |
#ifndef _POSIX_HOST_NAME_MAX |
| 42 |
#define _POSIX_HOST_NAME_MAX 255 |
| 43 |
#endif |
| 44 |
#ifndef _POSIX_LOGIN_NAME_MAX |
| 45 |
#define _POSIX_LOGIN_NAME_MAX 255 |
| 46 |
#endif |
| 47 |
|
| 48 |
#ifdef AUDIO_ALSA |
| 49 |
#include "audio_alsa.h" |
| 50 |
#endif |
| 51 |
|
| 52 |
|
| 53 |
/* These are global because the skin code uses them a lot */ |
| 54 |
|
| 55 |
GtkWidget *calc_widget; |
| 56 |
GtkWidget *mainwindow; |
| 57 |
bool allow_paint = false; |
| 58 |
int menu_bar_height = -1; |
| 59 |
|
| 60 |
state_type state; |
| 61 |
char free42dirname[FILENAMELEN]; |
| 62 |
|
| 63 |
|
| 64 |
/* PRINT_LINES is limited to an even lower value than in the Motif version. |
| 65 |
* It appears that GTK does not allow the SUM of a widget's height and its |
| 66 |
* container's height to exceed 32k, which means that as the print widget's |
| 67 |
* height approaches that magical number, the top-level window is forced to |
| 68 |
* become shorter and shorter. So, I set the limit at 30000 instead of |
| 69 |
* 32768; if we're reading a print-out file from Free42/Motif which has more |
| 70 |
* than 30000 pixels, we chop off the excess from the top. |
| 71 |
*/ |
| 72 |
#define PRINT_LINES 30000 |
| 73 |
#define PRINT_BYTESPERLINE 36 |
| 74 |
#define PRINT_SIZE 1080000 |
| 75 |
// Room for PRINT_LINES / 18 lines, plus two, plus one byte |
| 76 |
#define PRINT_TEXT_SIZE 41726 |
| 77 |
|
| 78 |
|
| 79 |
static unsigned char *print_bitmap; |
| 80 |
static int printout_top; |
| 81 |
static int printout_bottom; |
| 82 |
static unsigned char *print_text; |
| 83 |
static int print_text_top; |
| 84 |
static int print_text_bottom; |
| 85 |
static int print_text_pixel_height; |
| 86 |
static bool quit_flag = false; |
| 87 |
static bool enqueued; |
| 88 |
|
| 89 |
|
| 90 |
/* Private globals */ |
| 91 |
|
| 92 |
static FILE *print_txt = NULL; |
| 93 |
static FILE *print_gif = NULL; |
| 94 |
static char print_gif_name[FILENAMELEN]; |
| 95 |
static int gif_seq = -1; |
| 96 |
static int gif_lines; |
| 97 |
|
| 98 |
static int pype[2]; |
| 99 |
|
| 100 |
static GtkApplication *app = NULL; |
| 101 |
static GtkWidget *printwindow; |
| 102 |
static GtkWidget *print_widget; |
| 103 |
//static GdkGC *print_gc = NULL; |
| 104 |
static GtkAdjustment *print_adj; |
| 105 |
static GdkPixbuf *icon_128; |
| 106 |
static GdkPixbuf *icon_48; |
| 107 |
static guint resize_timeout_id = 0; |
| 108 |
|
| 109 |
int ckey = 0; |
| 110 |
int skey = -1; |
| 111 |
static unsigned char *macro; |
| 112 |
static int macro_type; |
| 113 |
static bool mouse_key; |
| 114 |
static guint16 active_keycode = 0; |
| 115 |
static bool just_pressed_shift = false; |
| 116 |
static guint timeout_id = 0; |
| 117 |
static guint timeout3_id = 0; |
| 118 |
|
| 119 |
static int keymap_length = 0; |
| 120 |
static keymap_entry *keymap = NULL; |
| 121 |
|
| 122 |
static guint reminder_id = 0; |
| 123 |
static FILE *statefile = NULL; |
| 124 |
static char statefilename[FILENAMELEN]; |
| 125 |
static char printfilename[FILENAMELEN]; |
| 126 |
static char keymapfilename[FILENAMELEN]; |
| 127 |
|
| 128 |
int ann_updown = 0; |
| 129 |
int ann_shift = 0; |
| 130 |
int ann_print = 0; |
| 131 |
int ann_run = 0; |
| 132 |
int ann_battery = 0; |
| 133 |
int ann_g = 0; |
| 134 |
int ann_rad = 0; |
| 135 |
static guint ann_print_timeout_id = 0; |
| 136 |
|
| 137 |
void get_keymap(keymap_entry **map, int *length) { |
| 138 |
*map = keymap; |
| 139 |
*length = keymap_length; |
| 140 |
} |
| 141 |
|
| 142 |
static bool keyboardShortcutsShowing = false; |
| 143 |
static GtkCheckMenuItem *keyboardShortcutsMenuItem; |
| 144 |
|
| 145 |
|
| 146 |
/* Private functions */ |
| 147 |
|
| 148 |
static void read_key_map(const char *keymapfilename); |
| 149 |
static void init_shell_state(int4 version); |
| 150 |
static int read_shell_state(int4 *version); |
| 151 |
static int write_shell_state(); |
| 152 |
static void int_term_handler(int sig); |
| 153 |
static gboolean gt_signal_handler(GIOChannel *source, GIOCondition condition, |
| 154 |
gpointer data); |
| 155 |
static void quit(); |
| 156 |
static char *strclone(const char *s); |
| 157 |
static bool file_exists(const char *name); |
| 158 |
static void show_message(const char *title, const char *message, GtkWidget *parent = mainwindow); |
| 159 |
static void no_mwm_resize_borders(GtkWidget *window); |
| 160 |
static void no_mwm_zoom_box(GtkWidget *window); |
| 161 |
static void scroll_printout_to_bottom(); |
| 162 |
static void quitCB(); |
| 163 |
static void statesCB(); |
| 164 |
static void showPrintOutCB(); |
| 165 |
static void exportProgramCB(); |
| 166 |
static GtkWidget *make_file_select_dialog( |
| 167 |
const char *title, const char *pattern, bool save, GtkWidget *owner); |
| 168 |
static void importProgramCB(); |
| 169 |
static void paperAdvanceCB(); |
| 170 |
static void copyPrintAsTextCB(); |
| 171 |
static void copyPrintAsImageCB(); |
| 172 |
static void clearPrintOutCB(); |
| 173 |
static void preferencesCB(); |
| 174 |
static void appendSuffix(char *path, const char *suffix); |
| 175 |
static void copyCB(); |
| 176 |
static void pasteCB(); |
| 177 |
static void documentationCB(); |
| 178 |
static void websiteCB(); |
| 179 |
static void otherWebsiteCB(); |
| 180 |
static void keyboardShortcutsCB(); |
| 181 |
static void editKeymapCB(); |
| 182 |
static void aboutCB(); |
| 183 |
static gboolean delete_cb(GtkWidget *w, GdkEventAny *ev); |
| 184 |
static gboolean delete_print_cb(GtkWidget *w, GdkEventAny *ev); |
| 185 |
static gboolean draw_cb(GtkWidget *w, cairo_t *cr, gpointer cd); |
| 186 |
static gboolean print_draw_cb(GtkWidget *w, cairo_t *cr, gpointer cd); |
| 187 |
static gboolean print_key_cb(GtkWidget *w, GdkEventKey *event, gpointer cd); |
| 188 |
static gboolean button_cb(GtkWidget *w, GdkEventButton *event, gpointer cd); |
| 189 |
static gboolean key_cb(GtkWidget *w, GdkEventKey *event, gpointer cd); |
| 190 |
static void enable_reminder(); |
| 191 |
static void disable_reminder(); |
| 192 |
static gboolean repeater(gpointer cd); |
| 193 |
static gboolean timeout1(gpointer cd); |
| 194 |
static gboolean timeout2(gpointer cd); |
| 195 |
static gboolean timeout3(gpointer cd); |
| 196 |
static gboolean battery_checker(gpointer cd); |
| 197 |
static void repaint_printout(cairo_t *cr, bool dark); |
| 198 |
static gboolean reminder(gpointer cd); |
| 199 |
static void txt_writer(const char *text, int length); |
| 200 |
static void txt_newliner(); |
| 201 |
static void gif_seeker(int4 pos); |
| 202 |
static void gif_writer(const char *text, int length); |
| 203 |
|
| 204 |
|
| 205 |
#ifdef BCD_MATH |
| 206 |
#define TITLE "Free42 Decimal" |
| 207 |
#else |
| 208 |
#define TITLE "Free42 Binary" |
| 209 |
#endif |
| 210 |
|
| 211 |
static const char *mainWindowXml = |
| 212 |
"<?xml version='1.0' encoding='UTF-8'?>" |
| 213 |
"<interface>" |
| 214 |
"<!-- interface-requires gtk+ 3.0 -->" |
| 215 |
"<object class='GtkApplicationWindow' id='window'>" |
| 216 |
"<child>" |
| 217 |
"<object class='GtkVBox' id='box'>" |
| 218 |
"<child>" |
| 219 |
"<object class='GtkMenuBar' id='menubar'>" |
| 220 |
"<child>" |
| 221 |
"%s" // Additional menu level goes here when using -compactmenu |
| 222 |
"<object class='GtkMenuItem' id='file_item'>" |
| 223 |
"<property name='label'>File</property>" |
| 224 |
"<child type='submenu'>" |
| 225 |
"<object class='GtkMenu' id='file_menu'>" |
| 226 |
"<child>" |
| 227 |
"<object class='GtkMenuItem' id='states_item'>" |
| 228 |
"<property name='label'>States...</property>" |
| 229 |
"</object>" |
| 230 |
"</child>" |
| 231 |
"<child>" |
| 232 |
"<object class='GtkSeparatorMenuItem' id='sep_1'>" |
| 233 |
"</object>" |
| 234 |
"</child>" |
| 235 |
"<child>" |
| 236 |
"<object class='GtkMenuItem' id='show_printout_item'>" |
| 237 |
"<property name='label'>Show Print-Out</property>" |
| 238 |
"</object>" |
| 239 |
"</child>" |
| 240 |
"<child>" |
| 241 |
"<object class='GtkMenuItem' id='paper_advance_item'>" |
| 242 |
"<property name='label'>Paper Advance</property>" |
| 243 |
"<accelerator key='A' signal='activate' modifiers='GDK_CONTROL_MASK'/>" |
| 244 |
"</object>" |
| 245 |
"</child>" |
| 246 |
"<child>" |
| 247 |
"<object class='GtkSeparatorMenuItem' id='sep_2'>" |
| 248 |
"</object>" |
| 249 |
"</child>" |
| 250 |
"<child>" |
| 251 |
"<object class='GtkMenuItem' id='import_programs_item'>" |
| 252 |
"<property name='label'>Import Programs...</property>" |
| 253 |
"</object>" |
| 254 |
"</child>" |
| 255 |
"<child>" |
| 256 |
"<object class='GtkMenuItem' id='export_programs_item'>" |
| 257 |
"<property name='label'>Export Programs...</property>" |
| 258 |
"</object>" |
| 259 |
"</child>" |
| 260 |
"<child>" |
| 261 |
"<object class='GtkSeparatorMenuItem' id='sep_3'>" |
| 262 |
"</object>" |
| 263 |
"</child>" |
| 264 |
"<child>" |
| 265 |
"<object class='GtkMenuItem' id='preferences_item'>" |
| 266 |
"<property name='label'>Preferences...</property>" |
| 267 |
"</object>" |
| 268 |
"</child>" |
| 269 |
"<child>" |
| 270 |
"<object class='GtkSeparatorMenuItem' id='sep_4'>" |
| 271 |
"</object>" |
| 272 |
"</child>" |
| 273 |
"<child>" |
| 274 |
"<object class='GtkMenuItem' id='quit_item'>" |
| 275 |
"<property name='label'>Quit</property>" |
| 276 |
"<accelerator key='Q' signal='activate' modifiers='GDK_CONTROL_MASK'/>" |
| 277 |
"</object>" |
| 278 |
"</child>" |
| 279 |
"</object>" |
| 280 |
"</child>" |
| 281 |
"</object>" |
| 282 |
"</child>" |
| 283 |
"<child>" |
| 284 |
"<object class='GtkMenuItem' id='edit_item'>" |
| 285 |
"<property name='label'>Edit</property>" |
| 286 |
"<child type='submenu'>" |
| 287 |
"<object class='GtkMenu' id='edit_menu'>" |
| 288 |
"<child>" |
| 289 |
"<object class='GtkMenuItem' id='copy_item'>" |
| 290 |
"<property name='label'>Copy</property>" |
| 291 |
"<accelerator key='C' signal='activate' modifiers='GDK_CONTROL_MASK'/>" |
| 292 |
"</object>" |
| 293 |
"</child>" |
| 294 |
"<child>" |
| 295 |
"<object class='GtkMenuItem' id='paste_item'>" |
| 296 |
"<property name='label'>Paste</property>" |
| 297 |
"<accelerator key='V' signal='activate' modifiers='GDK_CONTROL_MASK'/>" |
| 298 |
"</object>" |
| 299 |
"</child>" |
| 300 |
"<child>" |
| 301 |
"<object class='GtkSeparatorMenuItem' id='sep_5'>" |
| 302 |
"</object>" |
| 303 |
"</child>" |
| 304 |
"<child>" |
| 305 |
"<object class='GtkMenuItem' id='copy_printout_as_text_item'>" |
| 306 |
"<property name='label'>Copy Print-Out as Text</property>" |
| 307 |
"<accelerator key='T' signal='activate' modifiers='GDK_CONTROL_MASK'/>" |
| 308 |
"</object>" |
| 309 |
"</child>" |
| 310 |
"<child>" |
| 311 |
"<object class='GtkMenuItem' id='copy_printout_as_image_item'>" |
| 312 |
"<property name='label'>Copy Print-Out as Image</property>" |
| 313 |
"<accelerator key='I' signal='activate' modifiers='GDK_CONTROL_MASK'/>" |
| 314 |
"</object>" |
| 315 |
"</child>" |
| 316 |
"<child>" |
| 317 |
"<object class='GtkMenuItem' id='clear_printout_item'>" |
| 318 |
"<property name='label'>Clear Print-Out</property>" |
| 319 |
"<accelerator key='D' signal='activate' modifiers='GDK_CONTROL_MASK'/>" |
| 320 |
"</object>" |
| 321 |
"</child>" |
| 322 |
"</object>" |
| 323 |
"</child>" |
| 324 |
"</object>" |
| 325 |
"</child>" |
| 326 |
"<child>" |
| 327 |
"<object class='GtkMenuItem' id='skin_item'>" |
| 328 |
"<property name='label'>Skin</property>" |
| 329 |
"<child type='submenu'>" |
| 330 |
"<object class='GtkMenu' id='skin_menu'>" |
| 331 |
"<!-- Skin items inserted programmatically here -->" |
| 332 |
"</object>" |
| 333 |
"</child>" |
| 334 |
"</object>" |
| 335 |
"</child>" |
| 336 |
"<child>" |
| 337 |
"<object class='GtkMenuItem' id='help_item'>" |
| 338 |
"<property name='label'>Help</property>" |
| 339 |
"<child type='submenu'>" |
| 340 |
"<object class='GtkMenu' id='help_menu'>" |
| 341 |
"<child>" |
| 342 |
"<object class='GtkMenuItem' id='documentation_item'>" |
| 343 |
"<property name='label'>Documentation</property>" |
| 344 |
"</object>" |
| 345 |
"</child>" |
| 346 |
"<child>" |
| 347 |
"<object class='GtkMenuItem' id='website_item'>" |
| 348 |
"<property name='label'>Web Site</property>" |
| 349 |
"</object>" |
| 350 |
"</child>" |
| 351 |
"<child>" |
| 352 |
"<object class='GtkMenuItem' id='other_website_item'>" |
| 353 |
"<property name='label'>Plus42 Web Site</property>" |
| 354 |
"</object>" |
| 355 |
"</child>" |
| 356 |
"<child>" |
| 357 |
"<object class='GtkSeparatorMenuItem' id='sep_6'>" |
| 358 |
"</object>" |
| 359 |
"</child>" |
| 360 |
"<child>" |
| 361 |
"<object class='GtkCheckMenuItem' id='keyboard_shortcuts_item'>" |
| 362 |
"<property name='label'>Keyboard Shortcuts</property>" |
| 363 |
"<accelerator key='K' signal='activate' modifiers='GDK_CONTROL_MASK'/>" |
| 364 |
"</object>" |
| 365 |
"</child>" |
| 366 |
"<child>" |
| 367 |
"<object class='GtkMenuItem' id='edit_keymap_item'>" |
| 368 |
"<property name='label'>Edit Keyboard Map</property>" |
| 369 |
"</object>" |
| 370 |
"</child>" |
| 371 |
"<child>" |
| 372 |
"<object class='GtkSeparatorMenuItem' id='sep_7'>" |
| 373 |
"</object>" |
| 374 |
"</child>" |
| 375 |
"<child>" |
| 376 |
"<object class='GtkMenuItem' id='about_item'>" |
| 377 |
"<property name='label'>About Free42...</property>" |
| 378 |
"</object>" |
| 379 |
"</child>" |
| 380 |
"</object>" |
| 381 |
"</child>" |
| 382 |
"</object>" |
| 383 |
"%s" |
| 384 |
"</child>" |
| 385 |
"</object>" |
| 386 |
"<packing>" |
| 387 |
"<property name='expand'>FALSE</property>" |
| 388 |
"<property name='fill'>TRUE</property>" |
| 389 |
"</packing>" |
| 390 |
"</child>" |
| 391 |
"</object>" |
| 392 |
"</child>" |
| 393 |
"</object>" |
| 394 |
"</interface>"; |
| 395 |
|
| 396 |
static const char *compactMenuIntroXml = |
| 397 |
"<object class='GtkMenuItem' id='top_item'>" |
| 398 |
"<property name='label'>Menu</property>" |
| 399 |
"<child type='submenu'>" |
| 400 |
"<object class='GtkMenu' id='top_menu'>" |
| 401 |
"<child>"; |
| 402 |
|
| 403 |
static const char *compactMenuOutroXml = |
| 404 |
"</child>" |
| 405 |
"</object>" |
| 406 |
"</child>" |
| 407 |
"</object>"; |
| 408 |
|
| 409 |
static int use_compactmenu = 0; |
| 410 |
static char *skin_arg = NULL; |
| 411 |
|
| 412 |
static char cached_number_format[9]; |
| 413 |
|
| 414 |
static void activate(GtkApplication *theApp, gpointer userData); |
| 415 |
static void menubar_resized(GtkWidget *w, GtkAllocation *allocation, gpointer data); |
| 416 |
static void calc_resized(GtkWidget *w, GtkAllocation *allocation, gpointer data); |
| 417 |
|
| 418 |
int main(int argc, char *argv[]) { |
| 419 |
for (int i = 1; i < argc; i++) { |
| 420 |
if (strcmp(argv[i], "-skin") == 0) |
| 421 |
skin_arg = ++i < argc ? argv[i] : NULL; |
| 422 |
else if (strcmp(argv[i], "-compactmenu") == 0) |
| 423 |
use_compactmenu = 1; |
| 424 |
else { |
| 425 |
fprintf(stderr, "Unrecognized option: %s\n", argv[i]); |
| 426 |
exit(1); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
GtkApplication *app; |
| 431 |
int status; |
| 432 |
app = gtk_application_new("com.thomasokken.free42", G_APPLICATION_FLAGS_NONE); |
| 433 |
|
| 434 |
GValue gv = G_VALUE_INIT; |
| 435 |
g_value_init(&gv, G_TYPE_BOOLEAN); |
| 436 |
g_value_set_boolean(&gv, true); |
| 437 |
g_object_set_property(G_OBJECT(app), "register-session", &gv); |
| 438 |
|
| 439 |
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL); |
| 440 |
status = g_application_run(G_APPLICATION(app), 0, NULL); |
| 441 |
|
| 442 |
// We'll only get here if the app is being shut down |
| 443 |
// by the session manager. Note that the quit() call |
| 444 |
// does not return. |
| 445 |
quit(); |
| 446 |
|
| 447 |
g_object_unref(app); |
| 448 |
return status; |
| 449 |
} |
| 450 |
|
| 451 |
static void copy_one_utf8_char(char *dst, const char *src) { |
| 452 |
int n, c = *src & 255; |
| 453 |
if (c == 0) |
| 454 |
return; |
| 455 |
else if ((c & 0x80) == 0) |
| 456 |
n = 1; |
| 457 |
else if ((c & 0xc0) == 0x80) |
| 458 |
return; |
| 459 |
else if ((c & 0xe0) == 0xc0) |
| 460 |
n = 2; |
| 461 |
else if ((c & 0xf0) == 0xe0) |
| 462 |
n = 3; |
| 463 |
else |
| 464 |
return; |
| 465 |
strncat(dst, src, n); |
| 466 |
} |
| 467 |
|
| 468 |
static void activate(GtkApplication *theApp, gpointer userData) { |
| 469 |
|
| 470 |
if (app != NULL) { |
| 471 |
gtk_window_present(GTK_WINDOW(mainwindow)); |
| 472 |
gdk_window_focus(gtk_widget_get_window(mainwindow), GDK_CURRENT_TIME); |
| 473 |
return; |
| 474 |
} |
| 475 |
|
| 476 |
app = theApp; |
| 477 |
|
| 478 |
// Capture number format, which may have been changed by |
| 479 |
// gtk_init(), and then set it to the C locale, because the binary/decimal |
| 480 |
// conversions expect a decimal point, not a comma. |
| 481 |
struct lconv *loc = localeconv(); |
| 482 |
cached_number_format[0] = 0; |
| 483 |
copy_one_utf8_char(cached_number_format, loc->decimal_point); |
| 484 |
if (loc->thousands_sep[0] != 0) { |
| 485 |
int g1 = loc->grouping[0]; |
| 486 |
if (g1 != 0) { |
| 487 |
int g2 = loc->grouping[1]; |
| 488 |
if (g2 == 0) |
| 489 |
g2 = g1; |
| 490 |
copy_one_utf8_char(cached_number_format, loc->thousands_sep); |
| 491 |
char g[2]; |
| 492 |
g[0] = '0' + g1; |
| 493 |
g[1] = '0' + g2; |
| 494 |
strncat(cached_number_format, g, 2); |
| 495 |
} |
| 496 |
} |
| 497 |
setlocale(LC_NUMERIC, "C"); |
| 498 |
|
| 499 |
|
| 500 |
/*************************************************************/ |
| 501 |
/***** Try to create the $XDG_DATA_HOME/free42 directory *****/ |
| 502 |
/*************************************************************/ |
| 503 |
|
| 504 |
char *xdg_data_home = getenv("XDG_DATA_HOME"); |
| 505 |
char *home = getenv("HOME"); |
| 506 |
|
| 507 |
if (xdg_data_home == NULL || xdg_data_home[0] == 0) |
| 508 |
snprintf(free42dirname, FILENAMELEN, "%s/.local/share/free42", home); |
| 509 |
else |
| 510 |
snprintf(free42dirname, FILENAMELEN, "%s/free42", xdg_data_home); |
| 511 |
|
| 512 |
if (!file_exists(free42dirname)) { |
| 513 |
// The Free42 directory does not exist yet. Before trying to do |
| 514 |
// anything else, make sure the Free42 directory path starts with a slash. |
| 515 |
if (free42dirname[0] != '/') { |
| 516 |
fprintf(stderr, "Fatal: XDG_DATA_HOME or HOME are invalid; must start with '/'\n"); |
| 517 |
exit(1); |
| 518 |
} |
| 519 |
// If $HOME/.free42 does exist, move it to the new location. |
| 520 |
char old_free42dirname[FILENAMELEN]; |
| 521 |
snprintf(old_free42dirname, FILENAMELEN, "%s/.free42", home); |
| 522 |
bool have_old = false; |
| 523 |
struct stat st; |
| 524 |
if (lstat(old_free42dirname, &st) == 0) { |
| 525 |
if (S_ISLNK(st.st_mode)) { |
| 526 |
const char *dest; |
| 527 |
if (xdg_data_home == NULL || xdg_data_home[0] == 0) |
| 528 |
dest = "$HOME/.local/share/free42"; |
| 529 |
else |
| 530 |
dest = "$XDG_DATA_HOME/free42"; |
| 531 |
fprintf(stderr, "$HOME/.free42 is a symlink; not moving it to %s\n", dest); |
| 532 |
strcpy(free42dirname, old_free42dirname); |
| 533 |
goto dir_done; |
| 534 |
} |
| 535 |
have_old = S_ISDIR(st.st_mode); |
| 536 |
} |
| 537 |
if (have_old) { |
| 538 |
// Temporarily remove the "/free42" part from the end of the path, |
| 539 |
// leaving the path of the parent, which we will create |
| 540 |
free42dirname[strlen(free42dirname) - 7] = 0; |
| 541 |
} |
| 542 |
// The Free42 directory does not exist yet. Trying to create it, |
| 543 |
// and all its ancestors. We're not checking for errors here, since |
| 544 |
// either XDG_DATA_HOME or else HOME really should be set to |
| 545 |
// something sane, and besides, trying to create the first few |
| 546 |
// components of this path is *expected* to return errors, because |
| 547 |
// they will already exist. |
| 548 |
char *slash = free42dirname; |
| 549 |
do { |
| 550 |
*slash = '/'; |
| 551 |
char *nextSlash = strchr(slash + 1, '/'); |
| 552 |
if (nextSlash != NULL) |
| 553 |
*nextSlash = 0; |
| 554 |
mkdir(free42dirname, 0755); |
| 555 |
slash = nextSlash; |
| 556 |
} while (slash != NULL); |
| 557 |
// Now, move the $HOME/.free42 directory, if it exists |
| 558 |
if (have_old) { |
| 559 |
strcat(free42dirname, "/free42"); |
| 560 |
if (rename(old_free42dirname, free42dirname) != 0) { |
| 561 |
int err = errno; |
| 562 |
const char *dest; |
| 563 |
if (xdg_data_home == NULL || xdg_data_home[0] == 0) |
| 564 |
dest = "$HOME/.local/share/free42"; |
| 565 |
else |
| 566 |
dest = "$XDG_DATA_HOME/free42"; |
| 567 |
fprintf(stderr, "Unable to move $HOME/.free42 to %s: %s (%d)\n", dest, strerror(err), err); |
| 568 |
strcpy(free42dirname, old_free42dirname); |
| 569 |
goto dir_done; |
| 570 |
} |
| 571 |
// Create a symlink so the old directory will not appear |
| 572 |
// to have just vanished without a trace. |
| 573 |
// If XDG_DATA_HOME is a subdirectory of HOME, make |
| 574 |
// the symlink relative. |
| 575 |
int len = strlen(home); |
| 576 |
if (strncmp(free42dirname, home, len) == 0 && free42dirname[len] == '/') |
| 577 |
symlink(free42dirname + len + 1, old_free42dirname); |
| 578 |
else |
| 579 |
symlink(free42dirname, old_free42dirname); |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
dir_done: |
| 584 |
snprintf(statefilename, FILENAMELEN, "%s/state", free42dirname); |
| 585 |
snprintf(printfilename, FILENAMELEN, "%s/print", free42dirname); |
| 586 |
snprintf(keymapfilename, FILENAMELEN, "%s/keymap.txt", free42dirname); |
| 587 |
|
| 588 |
if (!file_exists(keymapfilename)) { |
| 589 |
char oldkeymapfilename[FILENAMELEN]; |
| 590 |
strcpy(oldkeymapfilename, keymapfilename); |
| 591 |
oldkeymapfilename[strlen(oldkeymapfilename) - 4] = 0; |
| 592 |
if (file_exists(oldkeymapfilename)) |
| 593 |
rename(oldkeymapfilename, keymapfilename); |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
/****************************/ |
| 598 |
/***** Read the key map *****/ |
| 599 |
/****************************/ |
| 600 |
|
| 601 |
read_key_map(keymapfilename); |
| 602 |
|
| 603 |
|
| 604 |
/***********************************************************/ |
| 605 |
/***** Open the state file and read the shell settings *****/ |
| 606 |
/***********************************************************/ |
| 607 |
|
| 608 |
int4 version; |
| 609 |
int init_mode; |
| 610 |
char core_state_file_name[FILENAMELEN]; |
| 611 |
int core_state_file_offset = 0; |
| 612 |
|
| 613 |
statefile = fopen(statefilename, "r"); |
| 614 |
if (statefile != NULL) { |
| 615 |
if (read_shell_state(&version)) { |
| 616 |
if (skin_arg != NULL) { |
| 617 |
strncpy(state.skinName, skin_arg, FILENAMELEN - 1); |
| 618 |
state.skinName[FILENAMELEN - 1] = 0; |
| 619 |
} |
| 620 |
init_mode = 1; |
| 621 |
} else { |
| 622 |
init_shell_state(-1); |
| 623 |
init_mode = 2; |
| 624 |
} |
| 625 |
} else { |
| 626 |
init_shell_state(-1); |
| 627 |
init_mode = 0; |
| 628 |
} |
| 629 |
if (init_mode == 1) { |
| 630 |
if (version > 25) { |
| 631 |
snprintf(core_state_file_name, FILENAMELEN, "%s/%s.f42", free42dirname, state.coreName); |
| 632 |
core_state_file_offset = 0; |
| 633 |
} else { |
| 634 |
strcpy(core_state_file_name, statefilename); |
| 635 |
core_state_file_offset = ftell(statefile); |
| 636 |
} |
| 637 |
fclose(statefile); |
| 638 |
} else { |
| 639 |
// The shell state was missing or corrupt, but there |
| 640 |
// may still be a valid core state... |
| 641 |
snprintf(core_state_file_name, FILENAMELEN, "%s/%s.f42", free42dirname, state.coreName); |
| 642 |
if (file_exists(core_state_file_name)) { |
| 643 |
// Core state "Untitled.f42" exists; let's try to read it |
| 644 |
core_state_file_offset = 0; |
| 645 |
init_mode = 1; |
| 646 |
version = 26; |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
/*********************************/ |
| 651 |
/***** Build the main window *****/ |
| 652 |
/*********************************/ |
| 653 |
|
| 654 |
char *xml = (char *) malloc(10240); |
| 655 |
if (use_compactmenu) |
| 656 |
snprintf(xml, 10240, mainWindowXml, compactMenuIntroXml, compactMenuOutroXml); |
| 657 |
else |
| 658 |
snprintf(xml, 10240, mainWindowXml, "", ""); |
| 659 |
GtkBuilder *builder = gtk_builder_new(); |
| 660 |
gtk_builder_add_from_string(builder, xml, -1, NULL); |
| 661 |
free(xml); |
| 662 |
GObject *obj = gtk_builder_get_object(builder, "window"); |
| 663 |
mainwindow = GTK_WIDGET(obj); |
| 664 |
gtk_window_set_application(GTK_WINDOW(mainwindow), app); |
| 665 |
|
| 666 |
icon_128 = gdk_pixbuf_new_from_resource("/icons/icon-128x128.png", NULL); |
| 667 |
icon_48 = gdk_pixbuf_new_from_resource("/icons/icon-48x48.png", NULL); |
| 668 |
|
| 669 |
gtk_window_set_icon(GTK_WINDOW(mainwindow), icon_128); |
| 670 |
gtk_window_set_title(GTK_WINDOW(mainwindow), TITLE); |
| 671 |
gtk_window_set_role(GTK_WINDOW(mainwindow), "Free42 Calculator"); |
| 672 |
//gtk_window_set_resizable(GTK_WINDOW(mainwindow), FALSE); |
| 673 |
no_mwm_zoom_box(mainwindow); |
| 674 |
g_signal_connect(G_OBJECT(mainwindow), "delete_event", |
| 675 |
G_CALLBACK(delete_cb), NULL); |
| 676 |
if (state.mainWindowKnown) |
| 677 |
gtk_window_move(GTK_WINDOW(mainwindow), state.mainWindowX, |
| 678 |
state.mainWindowY); |
| 679 |
|
| 680 |
GtkWidget *menubar = GTK_WIDGET(gtk_builder_get_object(builder, "menubar")); |
| 681 |
g_signal_connect(G_OBJECT(menubar), "size-allocate", G_CALLBACK(menubar_resized), NULL); |
| 682 |
|
| 683 |
// The "Skin" menu is dynamic; we don't populate any items in it here. |
| 684 |
// Instead, we attach a callback which scans the .free42 directory for |
| 685 |
// available skins; this callback is invoked when the menu is about to |
| 686 |
// be mapped. |
| 687 |
GtkMenuItem *item = GTK_MENU_ITEM(gtk_builder_get_object(builder, "skin_item")); |
| 688 |
g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(skin_menu_update), NULL); |
| 689 |
|
| 690 |
// With GTK 2 and GTK 3.4, the above logic worked fine, but with 3.24, |
| 691 |
// it appears that the pop-up shell is laid out *before* the 'activate' |
| 692 |
// callback is invoked. The result is that you do end up with the correct |
| 693 |
// menu items, but they don't fit in the pop-up and so are cut off. |
| 694 |
// Can't think of a proper way around this, but this at least will fix the |
| 695 |
// Skin menu appearance in the most common use case, i.e. when the set of |
| 696 |
// skins does not change while Free42 is running. |
| 697 |
skin_menu_update(GTK_WIDGET(item)); |
| 698 |
|
| 699 |
item = GTK_MENU_ITEM(gtk_builder_get_object(builder, "states_item")); |
| 700 |
g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(statesCB), NULL); |
| 701 |
item = GTK_MENU_ITEM(gtk_builder_get_object(builder, "show_printout_item")); |
| 702 |
g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(showPrintOutCB), NULL); |
| 703 |
item = GTK_MENU_ITEM(gtk_builder_get_object(builder, "paper_advance_item")); |
| 704 |
g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(paperAdvanceCB), NULL); |
| 705 |
item = GTK_MENU_ITEM(gtk_builder_get_object(builder, "import_programs_item")); |
| 706 |
g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(importProgramCB), NULL); |
| 707 |
item = GTK_MENU_ITEM(gtk_builder_get_object(builder, "export_programs_item")); |
| 708 |
g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(exportProgramCB), NULL); |
| 709 |
item = GTK_MENU_ITEM(gtk_builder_get_object(builder, "preferences_item")); |
| 710 |
g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(preferencesCB), NULL); |
| 711 |
item = GTK_MENU_ITEM(gtk_builder_get_object(builder, "quit_item")); |
| 712 |
g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(quitCB), NULL); |
| 713 |
item = GTK_MENU_ITEM(gtk_builder_get_object(builder, "copy_item")); |
| 714 |
g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(copyCB), NULL); |
| 715 |
item = GTK_MENU_ITEM(gtk_builder_get_object(builder, "paste_item")); |
| 716 |
g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(pasteCB), NULL); |
| 717 |
item = GTK_MENU_ITEM(gtk_builder_get_object(builder, "copy_printout_as_text_item")); |
| 718 |
g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(copyPrintAsTextCB), NULL); |
| 719 |
item = GTK_MENU_ITEM(gtk_builder_get_object(builder, "copy_printout_as_image_item")); |
| 720 |
g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(copyPrintAsImageCB), NULL); |
| 721 |
item = GTK_MENU_ITEM(gtk_builder_get_object(builder, "clear_printout_item")); |
| 722 |
g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(clearPrintOutCB), NULL); |
| 723 |
item = GTK_MENU_ITEM(gtk_builder_get_object(builder, "documentation_item")); |
| 724 |
g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(documentationCB), NULL); |
| 725 |
item = GTK_MENU_ITEM(gtk_builder_get_object(builder, "website_item")); |
| 726 |
g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(websiteCB), NULL); |
| 727 |
item = GTK_MENU_ITEM(gtk_builder_get_object(builder, "other_website_item")); |
| 728 |
g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(otherWebsiteCB), NULL); |
| 729 |
keyboardShortcutsMenuItem = GTK_CHECK_MENU_ITEM(gtk_builder_get_object(builder, "keyboard_shortcuts_item")); |
| 730 |
g_signal_connect(G_OBJECT(keyboardShortcutsMenuItem), "activate", G_CALLBACK(keyboardShortcutsCB), NULL); |
| 731 |
item = GTK_MENU_ITEM(gtk_builder_get_object(builder, "edit_keymap_item")); |
| 732 |
g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(editKeymapCB), NULL); |
| 733 |
item = GTK_MENU_ITEM(gtk_builder_get_object(builder, "about_item")); |
| 734 |
g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(aboutCB), NULL); |
| 735 |
|
| 736 |
|
| 737 |
/****************************************/ |
| 738 |
/* Drawing area for the calculator skin */ |
| 739 |
/****************************************/ |
| 740 |
|
| 741 |
GtkWidget *box = GTK_WIDGET(gtk_builder_get_object(builder, "box")); |
| 742 |
|
| 743 |
int win_width, win_height; |
| 744 |
skin_load(&win_width, &win_height); |
| 745 |
skin_set_window_size(win_width, win_height); |
| 746 |
if (state.mainWindowWidth != 0) |
| 747 |
skin_set_window_size(state.mainWindowWidth, state.mainWindowHeight); |
| 748 |
GtkWidget *w = gtk_drawing_area_new(); |
| 749 |
gtk_box_pack_start(GTK_BOX(box), w, TRUE, TRUE, 0); |
| 750 |
g_signal_connect(G_OBJECT(w), "draw", G_CALLBACK(draw_cb), NULL); |
| 751 |
gtk_widget_add_events(w, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK); |
| 752 |
g_signal_connect(G_OBJECT(w), "button-press-event", G_CALLBACK(button_cb), NULL); |
| 753 |
g_signal_connect(G_OBJECT(w), "button-release-event", G_CALLBACK(button_cb), NULL); |
| 754 |
gtk_widget_set_can_focus(w, TRUE); |
| 755 |
g_signal_connect(G_OBJECT(w), "key-press-event", G_CALLBACK(key_cb), NULL); |
| 756 |
g_signal_connect(G_OBJECT(w), "key-release-event", G_CALLBACK(key_cb), NULL); |
| 757 |
g_signal_connect(G_OBJECT(w), "size-allocate", G_CALLBACK(calc_resized), NULL); |
| 758 |
gtk_widget_set_size_request(w, 160, 160); |
| 759 |
calc_widget = w; |
| 760 |
|
| 761 |
/* |
| 762 |
GdkGeometry geom; |
| 763 |
geom.min_width = 160; |
| 764 |
geom.min_height = 160; |
| 765 |
gtk_window_set_geometry_hints(GTK_WINDOW(mainwindow), w, &geom, GdkWindowHints(GDK_HINT_MIN_SIZE)); |
| 766 |
*/ |
| 767 |
|
| 768 |
|
| 769 |
/**************************************/ |
| 770 |
/***** Build the print-out window *****/ |
| 771 |
/**************************************/ |
| 772 |
|
| 773 |
// In the Motif version, I create an XImage and read the bitmap data into |
| 774 |
// it; in the GTK version, that approach is not practical, since pixbuf |
| 775 |
// only comes in 24-bit and 32-bit flavors -- which would mean wasting |
| 776 |
// 25 megabytes for a 286x32768 pixbuf. So, instead, I use a 1 bpp buffer, |
| 777 |
// and simply create pixbufs on the fly whenever I have to repaint. |
| 778 |
print_bitmap = (unsigned char *) malloc(PRINT_SIZE); |
| 779 |
print_text = (unsigned char *) malloc(PRINT_TEXT_SIZE); |
| 780 |
// TODO - handle memory allocation failure |
| 781 |
|
| 782 |
FILE *printfile = fopen(printfilename, "r"); |
| 783 |
if (printfile != NULL) { |
| 784 |
int n = fread(&printout_bottom, 1, sizeof(int), printfile); |
| 785 |
if (n == sizeof(int)) { |
| 786 |
if (printout_bottom > PRINT_LINES) { |
| 787 |
int excess = (printout_bottom - PRINT_LINES) * PRINT_BYTESPERLINE; |
| 788 |
fseek(printfile, excess, SEEK_CUR); |
| 789 |
printout_bottom = PRINT_LINES; |
| 790 |
} |
| 791 |
int bytes = printout_bottom * PRINT_BYTESPERLINE; |
| 792 |
n = fread(print_bitmap, 1, bytes, printfile); |
| 793 |
if (n == bytes) { |
| 794 |
n = fread(&print_text_bottom, 1, sizeof(int), printfile); |
| 795 |
int n2 = fread(&print_text_pixel_height, 1, sizeof(int), printfile); |
| 796 |
if (n == sizeof(int) && n2 == sizeof(int)) { |
| 797 |
n = fread(print_text, 1, print_text_bottom, printfile); |
| 798 |
if (n != print_text_bottom) { |
| 799 |
print_text_bottom = 0; |
| 800 |
print_text_pixel_height = 0; |
| 801 |
} |
| 802 |
} else { |
| 803 |
print_text_bottom = 0; |
| 804 |
print_text_pixel_height = 0; |
| 805 |
} |
| 806 |
} else { |
| 807 |
printout_bottom = 0; |
| 808 |
print_text_bottom = 0; |
| 809 |
print_text_pixel_height = 0; |
| 810 |
} |
| 811 |
} else { |
| 812 |
printout_bottom = 0; |
| 813 |
print_text_bottom = 0; |
| 814 |
print_text_pixel_height = 0; |
| 815 |
} |
| 816 |
fclose(printfile); |
| 817 |
} else { |
| 818 |
printout_bottom = 0; |
| 819 |
print_text_bottom = 0; |
| 820 |
print_text_pixel_height = 0; |
| 821 |
} |
| 822 |
printout_top = 0; |
| 823 |
print_text_top = 0; |
| 824 |
|
| 825 |
printwindow = gtk_application_window_new(GTK_APPLICATION(app)); |
| 826 |
gtk_window_set_icon(GTK_WINDOW(printwindow), icon_128); |
| 827 |
gtk_window_set_title(GTK_WINDOW(printwindow), "Free42 Print-Out"); |
| 828 |
gtk_window_set_role(GTK_WINDOW(printwindow), "Free42 Print-Out"); |
| 829 |
g_signal_connect(G_OBJECT(printwindow), "delete_event", |
| 830 |
G_CALLBACK(delete_print_cb), NULL); |
| 831 |
|
| 832 |
GtkWidget *scroll = gtk_scrolled_window_new(NULL, NULL); |
| 833 |
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS); |
| 834 |
gtk_container_add(GTK_CONTAINER(printwindow), scroll); |
| 835 |
GtkWidget *view = gtk_viewport_new(NULL, NULL); |
| 836 |
gtk_container_add(GTK_CONTAINER(scroll), view); |
| 837 |
print_widget = gtk_drawing_area_new(); |
| 838 |
gtk_widget_set_size_request(print_widget, 358, printout_bottom); |
| 839 |
gtk_container_add(GTK_CONTAINER(view), print_widget); |
| 840 |
print_adj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scroll)); |
| 841 |
g_signal_connect(G_OBJECT(print_widget), "draw", G_CALLBACK(print_draw_cb), NULL); |
| 842 |
gtk_widget_set_can_focus(print_widget, TRUE); |
| 843 |
g_signal_connect(G_OBJECT(print_widget), "key-press-event", G_CALLBACK(print_key_cb), NULL); |
| 844 |
|
| 845 |
gtk_widget_show(print_widget); |
| 846 |
gtk_widget_show(view); |
| 847 |
gtk_widget_show(scroll); |
| 848 |
|
| 849 |
GdkGeometry geom; |
| 850 |
geom.min_width = 358; |
| 851 |
geom.max_width = 358; |
| 852 |
geom.min_height = 18; |
| 853 |
geom.max_height = 32767; |
| 854 |
geom.width_inc = 1; |
| 855 |
geom.height_inc = 18; |
| 856 |
gtk_window_set_geometry_hints(GTK_WINDOW(printwindow), NULL, &geom, GdkWindowHints(GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE | GDK_HINT_RESIZE_INC)); |
| 857 |
|
| 858 |
if (state.printWindowKnown) |
| 859 |
gtk_window_move(GTK_WINDOW(printwindow), state.printWindowX, |
| 860 |
state.printWindowY); |
| 861 |
gint width, height; |
| 862 |
gtk_window_get_size(GTK_WINDOW(printwindow), &width, &height); |
| 863 |
gtk_window_resize(GTK_WINDOW(printwindow), width, |
| 864 |
state.printWindowKnown ? state.printWindowHeight : 600); |
| 865 |
|
| 866 |
gtk_widget_realize(printwindow); |
| 867 |
gtk_widget_realize(print_widget); |
| 868 |
scroll_printout_to_bottom(); |
| 869 |
|
| 870 |
|
| 871 |
/*************************************************/ |
| 872 |
/***** Show main window & start the emulator *****/ |
| 873 |
/*************************************************/ |
| 874 |
|
| 875 |
if (state.printWindowKnown && state.printWindowMapped) |
| 876 |
gtk_widget_show(printwindow); |
| 877 |
gtk_widget_show_all(mainwindow); |
| 878 |
gtk_widget_show(mainwindow); |
| 879 |
|
| 880 |
core_init(init_mode, version, core_state_file_name, core_state_file_offset); |
| 881 |
if (core_powercycle()) |
| 882 |
enable_reminder(); |
| 883 |
|
| 884 |
/* Check if /proc/apm exists and is readable, and if so, |
| 885 |
* start the battery checker "thread" that keeps the battery |
| 886 |
* annunciator on the calculator display up to date. |
| 887 |
*/ |
| 888 |
FILE *apm = fopen("/proc/apm", "r"); |
| 889 |
if (apm != NULL) { |
| 890 |
fclose(apm); |
| 891 |
shell_low_battery(); |
| 892 |
g_timeout_add(60000, battery_checker, NULL); |
| 893 |
} else { |
| 894 |
/* Check if /sys/class/power_supply exists */ |
| 895 |
DIR *d = opendir("/sys/class/power_supply"); |
| 896 |
if (d != NULL) { |
| 897 |
closedir(d); |
| 898 |
shell_low_battery(); |
| 899 |
g_timeout_add(60000, battery_checker, NULL); |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
if (pipe(pype) != 0) |
| 904 |
fprintf(stderr, "Could not create pipe for signal handler; not catching signals.\n"); |
| 905 |
else { |
| 906 |
GIOChannel *channel = g_io_channel_unix_new(pype[0]); |
| 907 |
GError *err = NULL; |
| 908 |
g_io_channel_set_encoding(channel, NULL, &err); |
| 909 |
g_io_channel_set_flags(channel, |
| 910 |
(GIOFlags) (g_io_channel_get_flags(channel) | G_IO_FLAG_NONBLOCK), &err); |
| 911 |
g_io_add_watch(channel, G_IO_IN, gt_signal_handler, NULL); |
| 912 |
|
| 913 |
struct sigaction act; |
| 914 |
act.sa_handler = int_term_handler; |
| 915 |
sigemptyset(&act.sa_mask); |
| 916 |
sigaddset(&act.sa_mask, SIGINT); |
| 917 |
sigaddset(&act.sa_mask, SIGTERM); |
| 918 |
act.sa_flags = 0; |
| 919 |
sigaction(SIGINT, &act, NULL); |
| 920 |
sigaction(SIGTERM, &act, NULL); |
| 921 |
} |
| 922 |
} |
| 923 |
|
| 924 |
static void menubar_resized(GtkWidget *w, GtkAllocation *allocation, gpointer data) { |
| 925 |
if (menu_bar_height == allocation->height) |
| 926 |
return; |
| 927 |
menu_bar_height = allocation->height; |
| 928 |
int win_width, win_height; |
| 929 |
skin_get_window_size(&win_width, &win_height); |
| 930 |
int mh = menu_bar_height >= 2 ? menu_bar_height : 0; |
| 931 |
gtk_window_resize(GTK_WINDOW(mainwindow), win_width, win_height + mh); |
| 932 |
} |
| 933 |
|
| 934 |
static gboolean resizer(gpointer cd) { |
| 935 |
int win_width, win_height; |
| 936 |
skin_get_window_size(&win_width, &win_height); |
| 937 |
int mh = menu_bar_height >= 2 ? menu_bar_height : 0; |
| 938 |
gtk_window_resize(GTK_WINDOW(mainwindow), win_width, win_height + mh); |
| 939 |
resize_timeout_id = 0; |
| 940 |
return FALSE; |
| 941 |
} |
| 942 |
|
| 943 |
static void calc_resized(GtkWidget *w, GtkAllocation *allocation, gpointer data) { |
| 944 |
int skin_width, skin_height; |
| 945 |
skin_get_size(&skin_width, &skin_height); |
| 946 |
int win_width = allocation->width; |
| 947 |
int win_height = allocation->height; |
| 948 |
double hScale = ((double) win_width) / skin_width; |
| 949 |
double vScale = ((double) win_height) / skin_height; |
| 950 |
if (hScale > vScale) |
| 951 |
win_width = (int) (skin_width * vScale + 0.5); |
| 952 |
else if (vScale > hScale) |
| 953 |
win_height = (int) (skin_height * hScale + 0.5); |
| 954 |
skin_set_window_size(win_width, win_height); |
| 955 |
if (resize_timeout_id != 0) |
| 956 |
g_source_remove(resize_timeout_id); |
| 957 |
resize_timeout_id = g_timeout_add(250, resizer, NULL); |
| 958 |
} |
| 959 |
|
| 960 |
keymap_entry *parse_keymap_entry(char *line, int lineno) { |
| 961 |
char *p; |
| 962 |
static keymap_entry entry; |
| 963 |
|
| 964 |
p = strchr(line, '#'); |
| 965 |
if (p != NULL) |
| 966 |
*p = 0; |
| 967 |
p = strchr(line, '\n'); |
| 968 |
if (p != NULL) |
| 969 |
*p = 0; |
| 970 |
p = strchr(line, '\r'); |
| 971 |
if (p != NULL) |
| 972 |
*p = 0; |
| 973 |
|
| 974 |
p = strchr(line, ':'); |
| 975 |
if (p != NULL) { |
| 976 |
char *val = p + 1; |
| 977 |
char *tok; |
| 978 |
bool ctrl = false; |
| 979 |
bool alt = false; |
| 980 |
bool shift = false; |
| 981 |
bool cshift = false; |
| 982 |
guint keyval = GDK_KEY_VoidSymbol; |
| 983 |
bool done = false; |
| 984 |
unsigned char macrobuf[KEYMAP_MAX_MACRO_LENGTH + 1]; |
| 985 |
int macrolen = 0; |
| 986 |
|
| 987 |
/* Parse keysym */ |
| 988 |
*p = 0; |
| 989 |
tok = strtok(line, " \t"); |
| 990 |
while (tok != NULL) { |
| 991 |
if (done) { |
| 992 |
fprintf(stderr, "Keymap, line %d: Excess tokens in key spec.\n", lineno); |
| 993 |
return NULL; |
| 994 |
} |
| 995 |
if (strcasecmp(tok, "ctrl") == 0) |
| 996 |
ctrl = true; |
| 997 |
else if (strcasecmp(tok, "alt") == 0) |
| 998 |
alt = true; |
| 999 |
else if (strcasecmp(tok, "shift") == 0) |
| 1000 |
shift = true; |
| 1001 |
else if (strcasecmp(tok, "cshift") == 0) |
| 1002 |
cshift = true; |
| 1003 |
else { |
| 1004 |
keyval = gdk_keyval_from_name(tok); |
| 1005 |
if (keyval == GDK_KEY_VoidSymbol) { |
| 1006 |
fprintf(stderr, "Keymap, line %d: Unrecognized KeyName.\n", lineno); |
| 1007 |
return NULL; |
| 1008 |
} |
| 1009 |
done = true; |
| 1010 |
} |
| 1011 |
tok = strtok(NULL, " \t"); |
| 1012 |
} |
| 1013 |
if (!done) { |
| 1014 |
fprintf(stderr, "Keymap, line %d: Unrecognized KeyName.\n", lineno); |
| 1015 |
return NULL; |
| 1016 |
} |
| 1017 |
|
| 1018 |
/* Parse macro */ |
| 1019 |
tok = strtok(val, " \t"); |
| 1020 |
while (tok != NULL) { |
| 1021 |
char *endptr; |
| 1022 |
long k = strtol(tok, &endptr, 10); |
| 1023 |
if (*endptr != 0 || k < 1 || k > 255) { |
| 1024 |
fprintf(stderr, "Keymap, line %d: Bad value (%s) in macro.\n", lineno, tok); |
| 1025 |
return NULL; |
| 1026 |
} else if (macrolen == KEYMAP_MAX_MACRO_LENGTH) { |
| 1027 |
fprintf(stderr, "Keymap, line %d: Macro too long (max=%d).\n", lineno, KEYMAP_MAX_MACRO_LENGTH); |
| 1028 |
return NULL; |
| 1029 |
} else |
| 1030 |
macrobuf[macrolen++] = k; |
| 1031 |
tok = strtok(NULL, " \t"); |
| 1032 |
} |
| 1033 |
macrobuf[macrolen] = 0; |
| 1034 |
|
| 1035 |
entry.ctrl = ctrl; |
| 1036 |
entry.alt = alt; |
| 1037 |
entry.shift = shift; |
| 1038 |
entry.cshift = cshift; |
| 1039 |
entry.keyval = keyval; |
| 1040 |
strcpy((char *) entry.macro, (const char *) macrobuf); |
| 1041 |
return &entry; |
| 1042 |
} else |
| 1043 |
return NULL; |
| 1044 |
} |
| 1045 |
|
| 1046 |
static void read_key_map(const char *keymapfilename) { |
| 1047 |
FILE *keymapfile = fopen(keymapfilename, "r"); |
| 1048 |
int kmcap = 0; |
| 1049 |
char line[1024]; |
| 1050 |
int lineno = 0; |
| 1051 |
|
| 1052 |
if (keymapfile == NULL) { |
| 1053 |
/* Try to create default keymap file */ |
| 1054 |
extern const long keymap_filesize; |
| 1055 |
extern const char keymap_filedata[]; |
| 1056 |
long n; |
| 1057 |
|
| 1058 |
keymapfile = fopen(keymapfilename, "wb"); |
| 1059 |
if (keymapfile == NULL) |
| 1060 |
return; |
| 1061 |
n = fwrite(keymap_filedata, 1, keymap_filesize, keymapfile); |
| 1062 |
if (n != keymap_filesize) { |
| 1063 |
int err = errno; |
| 1064 |
fprintf(stderr, "Error writing \"%s\": %s (%d)\n", |
| 1065 |
keymapfilename, strerror(err), err); |
| 1066 |
} |
| 1067 |
fclose(keymapfile); |
| 1068 |
|
| 1069 |
keymapfile = fopen(keymapfilename, "r"); |
| 1070 |
if (keymapfile == NULL) |
| 1071 |
return; |
| 1072 |
} |
| 1073 |
|
| 1074 |
while (fgets(line, 1024, keymapfile) != NULL) { |
| 1075 |
keymap_entry *entry = parse_keymap_entry(line, ++lineno); |
| 1076 |
if (entry == NULL) |
| 1077 |
continue; |
| 1078 |
/* Create new keymap entry */ |
| 1079 |
if (keymap_length == kmcap) { |
| 1080 |
kmcap += 50; |
| 1081 |
keymap = (keymap_entry *) realloc(keymap, kmcap * sizeof(keymap_entry)); |
| 1082 |
// TODO - handle memory allocation failure |
| 1083 |
} |
| 1084 |
memcpy(keymap + (keymap_length++), entry, sizeof(keymap_entry)); |
| 1085 |
} |
| 1086 |
|
| 1087 |
fclose(keymapfile); |
| 1088 |
} |
| 1089 |
|
| 1090 |
static void init_shell_state(int4 version) { |
| 1091 |
switch (version) { |
| 1092 |
case -1: |
| 1093 |
state.extras = 0; |
| 1094 |
/* fall through */ |
| 1095 |
case 0: |
| 1096 |
state.printerToTxtFile = 0; |
| 1097 |
state.printerToGifFile = 0; |
| 1098 |
state.printerTxtFileName[0] = 0; |
| 1099 |
state.printerGifFileName[0] = 0; |
| 1100 |
state.printerGifMaxLength = 256; |
| 1101 |
/* fall through */ |
| 1102 |
case 1: |
| 1103 |
state.mainWindowKnown = 0; |
| 1104 |
state.printWindowKnown = 0; |
| 1105 |
/* fall through */ |
| 1106 |
case 2: |
| 1107 |
state.skinName[0] = 0; |
| 1108 |
/* fall through */ |
| 1109 |
case 3: |
| 1110 |
state.singleInstance = 1; |
| 1111 |
/* fall through */ |
| 1112 |
case 4: |
| 1113 |
strcpy(state.coreName, "Untitled"); |
| 1114 |
/* fall through */ |
| 1115 |
case 5: |
| 1116 |
core_settings.matrix_singularmatrix = false; |
| 1117 |
core_settings.matrix_outofrange = false; |
| 1118 |
core_settings.auto_repeat = true; |
| 1119 |
/* fall through */ |
| 1120 |
case 6: |
| 1121 |
state.old_repaint = true; |
| 1122 |
/* fall through */ |
| 1123 |
case 7: |
| 1124 |
core_settings.allow_big_stack = false; |
| 1125 |
/* fall through */ |
| 1126 |
case 8: |
| 1127 |
/* fall through */ |
| 1128 |
case 9: |
| 1129 |
core_settings.localized_copy_paste = true; |
| 1130 |
/* fall through */ |
| 1131 |
case 10: |
| 1132 |
state.mainWindowWidth = 0; |
| 1133 |
state.mainWindowHeight = 0; |
| 1134 |
// fall through |
| 1135 |
case 11: |
| 1136 |
/* current version (SHELL_VERSION = 11), |
| 1137 |
* so nothing to do here since everything |
| 1138 |
* was initialized from the state file. |
| 1139 |
*/ |
| 1140 |
; |
| 1141 |
} |
| 1142 |
} |
| 1143 |
|
| 1144 |
static int read_shell_state(int4 *ver) { |
| 1145 |
int4 magic; |
| 1146 |
int4 version; |
| 1147 |
int4 state_size; |
| 1148 |
int4 state_version; |
| 1149 |
|
| 1150 |
if (fread(&magic, 1, sizeof(int4), statefile) != sizeof(int4)) |
| 1151 |
return 0; |
| 1152 |
if (magic != FREE42_MAGIC) |
| 1153 |
return 0; |
| 1154 |
|
| 1155 |
if (fread(&version, 1, sizeof(int4), statefile) != sizeof(int4)) |
| 1156 |
return 0; |
| 1157 |
if (version == 0) { |
| 1158 |
/* State file version 0 does not contain shell state, |
| 1159 |
* only core state, so we just hard-init the shell. |
| 1160 |
*/ |
| 1161 |
init_shell_state(-1); |
| 1162 |
*ver = version; |
| 1163 |
return 1; |
| 1164 |
} |
| 1165 |
|
| 1166 |
if (fread(&state_size, 1, sizeof(int4), statefile) != sizeof(int4)) |
| 1167 |
return 0; |
| 1168 |
if (fread(&state_version, 1, sizeof(int4), statefile) != sizeof(int4)) |
| 1169 |
return 0; |
| 1170 |
if (state_version < 0 || state_version > SHELL_VERSION) |
| 1171 |
/* Unknown shell state version */ |
| 1172 |
return 0; |
| 1173 |
if (fread(&state, 1, state_size, statefile) != (size_t) state_size) |
| 1174 |
return 0; |
| 1175 |
if (state_version >= 6) { |
| 1176 |
core_settings.matrix_singularmatrix = state.matrix_singularmatrix; |
| 1177 |
core_settings.matrix_outofrange = state.matrix_outofrange; |
| 1178 |
core_settings.auto_repeat = state.auto_repeat; |
| 1179 |
} |
| 1180 |
if (state_version >= 7) |
| 1181 |
core_settings.allow_big_stack = state.allow_big_stack; |
| 1182 |
if (state_version >= 10) |
| 1183 |
core_settings.localized_copy_paste = state.localized_copy_paste; |
| 1184 |
|
| 1185 |
init_shell_state(state_version); |
| 1186 |
*ver = version; |
| 1187 |
return 1; |
| 1188 |
} |
| 1189 |
|
| 1190 |
static int write_shell_state() { |
| 1191 |
int4 magic = FREE42_MAGIC; |
| 1192 |
int4 version = 27; |
| 1193 |
int4 state_size = sizeof(state_type); |
| 1194 |
int4 state_version = SHELL_VERSION; |
| 1195 |
|
| 1196 |
if (fwrite(&magic, 1, sizeof(int4), statefile) != sizeof(int4)) |
| 1197 |
return 0; |
| 1198 |
if (fwrite(&version, 1, sizeof(int4), statefile) != sizeof(int4)) |
| 1199 |
return 0; |
| 1200 |
if (fwrite(&state_size, 1, sizeof(int4), statefile) != sizeof(int4)) |
| 1201 |
return 0; |
| 1202 |
if (fwrite(&state_version, 1, sizeof(int4), statefile) != sizeof(int4)) |
| 1203 |
return 0; |
| 1204 |
state.matrix_singularmatrix = core_settings.matrix_singularmatrix; |
| 1205 |
state.matrix_outofrange = core_settings.matrix_outofrange; |
| 1206 |
state.auto_repeat = core_settings.auto_repeat; |
| 1207 |
state.allow_big_stack = core_settings.allow_big_stack; |
| 1208 |
state.localized_copy_paste = core_settings.localized_copy_paste; |
| 1209 |
if (fwrite(&state, 1, sizeof(state_type), statefile) != sizeof(state_type)) |
| 1210 |
return 0; |
| 1211 |
|
| 1212 |
return 1; |
| 1213 |
} |
| 1214 |
|
| 1215 |
static void int_term_handler(int sig) { |
| 1216 |
write(pype[1], "1\n", 2); |
| 1217 |
} |
| 1218 |
|
| 1219 |
static gboolean gt_signal_handler(GIOChannel *source, GIOCondition condition, |
| 1220 |
gpointer data) { |
| 1221 |
quit(); |
| 1222 |
return TRUE; |
| 1223 |
} |
| 1224 |
|
| 1225 |
static void quit() { |
| 1226 |
FILE *printfile; |
| 1227 |
int n, length; |
| 1228 |
|
| 1229 |
printfile = fopen(printfilename, "w"); |
| 1230 |
if (printfile != NULL) { |
| 1231 |
// Write bitmap |
| 1232 |
length = printout_bottom - printout_top; |
| 1233 |
if (length < 0) |
| 1234 |
length += PRINT_LINES; |
| 1235 |
n = fwrite(&length, 1, sizeof(int), printfile); |
| 1236 |
if (n != sizeof(int)) |
| 1237 |
goto failed; |
| 1238 |
if (printout_bottom >= printout_top) { |
| 1239 |
n = fwrite(print_bitmap + PRINT_BYTESPERLINE * printout_top, |
| 1240 |
1, PRINT_BYTESPERLINE * length, printfile); |
| 1241 |
if (n != PRINT_BYTESPERLINE * length) |
| 1242 |
goto failed; |
| 1243 |
} else { |
| 1244 |
n = fwrite(print_bitmap + PRINT_BYTESPERLINE * printout_top, |
| 1245 |
1, PRINT_SIZE - PRINT_BYTESPERLINE * printout_top, |
| 1246 |
printfile); |
| 1247 |
if (n != PRINT_SIZE - PRINT_BYTESPERLINE * printout_top) |
| 1248 |
goto failed; |
| 1249 |
n = fwrite(print_bitmap, 1, |
| 1250 |
PRINT_BYTESPERLINE * printout_bottom, printfile); |
| 1251 |
if (n != PRINT_BYTESPERLINE * printout_bottom) |
| 1252 |
goto failed; |
| 1253 |
} |
| 1254 |
// Write text |
| 1255 |
length = print_text_bottom - print_text_top; |
| 1256 |
if (length < 0) |
| 1257 |
length += PRINT_TEXT_SIZE; |
| 1258 |
n = fwrite(&length, 1, sizeof(int), printfile); |
| 1259 |
if (n != sizeof(int)) |
| 1260 |
goto failed; |
| 1261 |
n = fwrite(&print_text_pixel_height, 1, sizeof(int), printfile); |
| 1262 |
if (n != sizeof(int)) |
| 1263 |
goto failed; |
| 1264 |
if (print_text_bottom >= print_text_top) { |
| 1265 |
n = fwrite(print_text + print_text_top, 1, length, printfile); |
| 1266 |
if (n != length) |
| 1267 |
goto failed; |
| 1268 |
} else { |
| 1269 |
n = fwrite(print_text + print_text_top, 1, PRINT_TEXT_SIZE - print_text_top, printfile); |
| 1270 |
if (n != PRINT_TEXT_SIZE - print_text_top) |
| 1271 |
goto failed; |
| 1272 |
n = fwrite(print_text, 1, print_text_bottom, printfile); |
| 1273 |
if (n != print_text_bottom) |
| 1274 |
goto failed; |
| 1275 |
} |
| 1276 |
|
| 1277 |
fclose(printfile); |
| 1278 |
goto done; |
| 1279 |
|
| 1280 |
failed: |
| 1281 |
fclose(printfile); |
| 1282 |
remove(printfilename); |
| 1283 |
|
| 1284 |
done: |
| 1285 |
; |
| 1286 |
} |
| 1287 |
|
| 1288 |
if (print_txt != NULL) |
| 1289 |
fclose(print_txt); |
| 1290 |
|
| 1291 |
if (print_gif != NULL) { |
| 1292 |
shell_finish_gif(gif_seeker, gif_writer); |
| 1293 |
fclose(print_gif); |
| 1294 |
} |
| 1295 |
|
| 1296 |
gint x, y; |
| 1297 |
gtk_window_get_position(GTK_WINDOW(mainwindow), &x, &y); |
| 1298 |
state.mainWindowKnown = 1; |
| 1299 |
state.mainWindowX = x; |
| 1300 |
state.mainWindowY = y; |
| 1301 |
int w, h; |
| 1302 |
skin_get_window_size(&w, &h); |
| 1303 |
state.mainWindowWidth = w; |
| 1304 |
state.mainWindowHeight = h; |
| 1305 |
|
| 1306 |
if (state.printWindowMapped) { |
| 1307 |
gtk_window_get_position(GTK_WINDOW(printwindow), &x, &y); |
| 1308 |
state.printWindowX = x; |
| 1309 |
state.printWindowY = y; |
| 1310 |
} |
| 1311 |
if (state.printWindowKnown) { |
| 1312 |
gtk_window_get_size(GTK_WINDOW(printwindow), &x, &y); |
| 1313 |
state.printWindowHeight = y; |
| 1314 |
} |
| 1315 |
|
| 1316 |
statefile = fopen(statefilename, "w"); |
| 1317 |
if (statefile != NULL) { |
| 1318 |
write_shell_state(); |
| 1319 |
fclose(statefile); |
| 1320 |
} |
| 1321 |
char corefilename[FILENAMELEN]; |
| 1322 |
snprintf(corefilename, FILENAMELEN, "%s/%s.f42", free42dirname, state.coreName); |
| 1323 |
core_save_state(corefilename); |
| 1324 |
core_cleanup(); |
| 1325 |
|
| 1326 |
shell_spool_exit(); |
| 1327 |
|
| 1328 |
exit(0); |
| 1329 |
} |
| 1330 |
|
| 1331 |
static char *strclone(const char *s) { |
| 1332 |
char *s2 = (char *) malloc(strlen(s) + 1); |
| 1333 |
if (s2 != NULL) |
| 1334 |
strcpy(s2, s); |
| 1335 |
return s2; |
| 1336 |
} |
| 1337 |
|
| 1338 |
static bool file_exists(const char *name) { |
| 1339 |
struct stat st; |
| 1340 |
return stat(name, &st) == 0; |
| 1341 |
} |
| 1342 |
|
| 1343 |
static void show_message(const char *title, const char *message, GtkWidget *parent) { |
| 1344 |
GtkWidget *msg = gtk_message_dialog_new(GTK_WINDOW(parent), |
| 1345 |
GTK_DIALOG_MODAL, |
| 1346 |
GTK_MESSAGE_ERROR, |
| 1347 |
GTK_BUTTONS_OK, |
| 1348 |
"%s", |
| 1349 |
message); |
| 1350 |
gtk_window_set_title(GTK_WINDOW(msg), title); |
| 1351 |
gtk_window_set_role(GTK_WINDOW(msg), "Free42 Dialog"); |
| 1352 |
gtk_dialog_run(GTK_DIALOG(msg)); |
| 1353 |
gtk_widget_destroy(msg); |
| 1354 |
} |
| 1355 |
|
| 1356 |
static void no_mwm_resize_helper(GtkWidget *w, gpointer cd) { |
| 1357 |
GdkWindow *win = gtk_widget_get_window(w); |
| 1358 |
gdk_window_set_decorations(win, GdkWMDecoration(GDK_DECOR_ALL |
| 1359 |
| GDK_DECOR_RESIZEH | GDK_DECOR_MAXIMIZE)); |
| 1360 |
gdk_window_set_functions(win, GdkWMFunction(GDK_FUNC_ALL |
| 1361 |
| GDK_FUNC_RESIZE | GDK_FUNC_MAXIMIZE)); |
| 1362 |
} |
| 1363 |
|
| 1364 |
static void no_mwm_resize_borders(GtkWidget *window) { |
| 1365 |
// gtk_window_set_resizable(w, FALSE) only sets the WM size hints, so that |
| 1366 |
// the minimum and maximum sizes coincide with the actual size. While this |
| 1367 |
// certainly has the desired effect of making the window non-resizable, it |
| 1368 |
// does not deal with the way mwm visually distinguishes resizable and |
| 1369 |
// non-resizable windows; the result is that, when running under mwm, you |
| 1370 |
// have a window with resize borders and a maximize button, none of which |
| 1371 |
// actually let you resize the window. |
| 1372 |
// So, we use an additional GDK call to set the appropriate mwm properties. |
| 1373 |
g_signal_connect(G_OBJECT(window), "realize", |
| 1374 |
G_CALLBACK(no_mwm_resize_helper), NULL); |
| 1375 |
} |
| 1376 |
|
| 1377 |
static void no_mwm_zoom_helper(GtkWidget *w, gpointer cd) { |
| 1378 |
GdkWindow *win = gtk_widget_get_window(w); |
| 1379 |
gdk_window_set_decorations(win, GdkWMDecoration(GDK_DECOR_ALL |
| 1380 |
| GDK_DECOR_MAXIMIZE)); |
| 1381 |
gdk_window_set_functions(win, GdkWMFunction(GDK_FUNC_ALL |
| 1382 |
| GDK_FUNC_MAXIMIZE)); |
| 1383 |
} |
| 1384 |
|
| 1385 |
static void no_mwm_zoom_box(GtkWidget *window) { |
| 1386 |
g_signal_connect(G_OBJECT(window), "realize", |
| 1387 |
G_CALLBACK(no_mwm_zoom_helper), NULL); |
| 1388 |
} |
| 1389 |
|
| 1390 |
static void scroll_printout_to_bottom() { |
| 1391 |
gdouble upper = gtk_adjustment_get_upper(print_adj); |
| 1392 |
gdouble page_size = gtk_adjustment_get_page_size(print_adj); |
| 1393 |
gtk_adjustment_set_value(print_adj, upper - page_size); |
| 1394 |
} |
| 1395 |
|
| 1396 |
static void quitCB() { |
| 1397 |
quit(); |
| 1398 |
} |
| 1399 |
|
| 1400 |
//////////////////////////////////// |
| 1401 |
///// States stuff starts here ///// |
| 1402 |
//////////////////////////////////// |
| 1403 |
|
| 1404 |
static int currentStateIndex; |
| 1405 |
static int selectedStateIndex; |
| 1406 |
static char **state_names; |
| 1407 |
static GtkWidget *statesMenuItems[6]; |
| 1408 |
|
| 1409 |
static void states_changed_cb(GtkWidget *w, gpointer p) { |
| 1410 |
selectedStateIndex = -1; |
| 1411 |
GList *rows = gtk_tree_selection_get_selected_rows(GTK_TREE_SELECTION(w), NULL); |
| 1412 |
if (rows != NULL) { |
| 1413 |
GtkTreePath *path = (GtkTreePath *) rows->data; |
| 1414 |
sscanf(gtk_tree_path_to_string(path), "%d", &selectedStateIndex); |
| 1415 |
} |
| 1416 |
g_list_free(rows); |
| 1417 |
GtkWidget *btn = (GtkWidget *) p; |
| 1418 |
if (selectedStateIndex == -1) { |
| 1419 |
gtk_widget_set_sensitive(btn, false); |
| 1420 |
gtk_button_set_label(GTK_BUTTON(btn), "Switch To"); |
| 1421 |
gtk_widget_set_sensitive(statesMenuItems[1], false); |
| 1422 |
gtk_widget_set_sensitive(statesMenuItems[2], false); |
| 1423 |
gtk_widget_set_sensitive(statesMenuItems[3], false); |
| 1424 |
gtk_widget_set_sensitive(statesMenuItems[5], false); |
| 1425 |
} else if (selectedStateIndex == currentStateIndex) { |
| 1426 |
gtk_widget_set_sensitive(btn, true); |
| 1427 |
gtk_button_set_label(GTK_BUTTON(btn), "Revert"); |
| 1428 |
gtk_widget_set_sensitive(statesMenuItems[1], true); |
| 1429 |
gtk_widget_set_sensitive(statesMenuItems[2], true); |
| 1430 |
gtk_widget_set_sensitive(statesMenuItems[3], false); |
| 1431 |
gtk_widget_set_sensitive(statesMenuItems[5], true); |
| 1432 |
} else { |
| 1433 |
gtk_widget_set_sensitive(btn, true); |
| 1434 |
gtk_button_set_label(GTK_BUTTON(btn), "Switch To"); |
| 1435 |
gtk_widget_set_sensitive(statesMenuItems[1], true); |
| 1436 |
gtk_widget_set_sensitive(statesMenuItems[2], true); |
| 1437 |
gtk_widget_set_sensitive(statesMenuItems[3], true); |
| 1438 |
gtk_widget_set_sensitive(statesMenuItems[5], true); |
| 1439 |
} |
| 1440 |
} |
| 1441 |
|
| 1442 |
static GtkWidget *dlg; |
| 1443 |
|
| 1444 |
static char *get_state_name(const char *prompt, const char *initialName) { |
| 1445 |
static GtkWidget *state_name_dialog = NULL; |
| 1446 |
static GtkWidget *promptLabel; |
| 1447 |
static GtkWidget *name; |
| 1448 |
|
| 1449 |
if (state_name_dialog == NULL) { |
| 1450 |
state_name_dialog = gtk_dialog_new_with_buttons( |
| 1451 |
"State Name", |
| 1452 |
GTK_WINDOW(dlg), |
| 1453 |
GTK_DIALOG_MODAL, |
| 1454 |
"_OK", GTK_RESPONSE_ACCEPT, |
| 1455 |
"_Cancel", GTK_RESPONSE_CANCEL, |
| 1456 |
NULL); |
| 1457 |
gtk_window_set_resizable(GTK_WINDOW(state_name_dialog), FALSE); |
| 1458 |
no_mwm_resize_borders(state_name_dialog); |
| 1459 |
GtkWidget *container = gtk_bin_get_child(GTK_BIN(state_name_dialog)); |
| 1460 |
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 1461 |
gtk_container_add(GTK_CONTAINER(container), box); |
| 1462 |
|
| 1463 |
promptLabel = gtk_label_new("Enter state name:"); |
| 1464 |
gtk_box_pack_start(GTK_BOX(box), promptLabel, FALSE, FALSE, 10); |
| 1465 |
|
| 1466 |
name = gtk_entry_new(); |
| 1467 |
gtk_box_pack_start(GTK_BOX(box), name, FALSE, FALSE, 10); |
| 1468 |
|
| 1469 |
gtk_widget_show_all(state_name_dialog); |
| 1470 |
} |
| 1471 |
|
| 1472 |
gtk_window_set_role(GTK_WINDOW(state_name_dialog), "Free42 Dialog"); |
| 1473 |
gtk_label_set_text(GTK_LABEL(promptLabel), prompt); |
| 1474 |
gtk_entry_set_text(GTK_ENTRY(name), initialName); |
| 1475 |
gtk_editable_select_region(GTK_EDITABLE(name), 0, -1); |
| 1476 |
|
| 1477 |
char *result = NULL; |
| 1478 |
while (true) { |
| 1479 |
gtk_window_set_focus(GTK_WINDOW(state_name_dialog), name); |
| 1480 |
gtk_widget_grab_focus(GTK_WIDGET(name)); |
| 1481 |
int response = gtk_dialog_run(GTK_DIALOG(state_name_dialog)); |
| 1482 |
if (response == GTK_RESPONSE_ACCEPT) { |
| 1483 |
const char *tmp = gtk_entry_get_text(GTK_ENTRY(name)); |
| 1484 |
if (strchr(tmp, '/') != NULL) { |
| 1485 |
show_message("Message", "That name is not valid.", state_name_dialog); |
| 1486 |
continue; |
| 1487 |
} |
| 1488 |
char path[FILENAMELEN]; |
| 1489 |
snprintf(path, FILENAMELEN, "%s/%s.f42", free42dirname, tmp); |
| 1490 |
if (file_exists(path)) { |
| 1491 |
show_message("Message", "That name is already in use.", state_name_dialog); |
| 1492 |
continue; |
| 1493 |
} |
| 1494 |
result = (char *) malloc(strlen(tmp) + 1); |
| 1495 |
strcpy(result, tmp); |
| 1496 |
} |
| 1497 |
break; |
| 1498 |
} |
| 1499 |
|
| 1500 |
gtk_widget_hide(state_name_dialog); |
| 1501 |
return result; |
| 1502 |
} |
| 1503 |
|
| 1504 |
static bool switchTo(const char *selectedStateName) { |
| 1505 |
char path[FILENAMELEN]; |
| 1506 |
if (strcmp(selectedStateName, state.coreName) == 0) { |
| 1507 |
GtkWidget *msg = gtk_message_dialog_new(GTK_WINDOW(dlg), |
| 1508 |
GTK_DIALOG_MODAL, |
| 1509 |
GTK_MESSAGE_QUESTION, |
| 1510 |
GTK_BUTTONS_YES_NO, |
| 1511 |
"Are you sure you want to revert the state \"%s\" to the last version saved?", |
| 1512 |
selectedStateName); |
| 1513 |
gtk_window_set_title(GTK_WINDOW(msg), "Revert State?"); |
| 1514 |
gtk_window_set_role(GTK_WINDOW(msg), "Free42 Dialog"); |
| 1515 |
bool cancelled = gtk_dialog_run(GTK_DIALOG(msg)) != GTK_RESPONSE_YES; |
| 1516 |
gtk_widget_destroy(msg); |
| 1517 |
if (cancelled) |
| 1518 |
return false; |
| 1519 |
} else { |
| 1520 |
snprintf(path, FILENAMELEN, "%s/%s.f42", free42dirname, state.coreName); |
| 1521 |
core_save_state(path); |
| 1522 |
} |
| 1523 |
core_cleanup(); |
| 1524 |
strncpy(state.coreName, selectedStateName, FILENAMELEN); |
| 1525 |
state.coreName[FILENAMELEN - 1] = 0; |
| 1526 |
snprintf(path, FILENAMELEN, "%s/%s.f42", free42dirname, state.coreName); |
| 1527 |
core_init(1, 26, path, 0); |
| 1528 |
if (core_powercycle()) |
| 1529 |
enable_reminder(); |
| 1530 |
return true; |
| 1531 |
} |
| 1532 |
|
| 1533 |
static void states_menu_new() { |
| 1534 |
char *name = get_state_name("New state name:", ""); |
| 1535 |
if (name == NULL) |
| 1536 |
return; |
| 1537 |
char path[FILENAMELEN]; |
| 1538 |
snprintf(path, FILENAMELEN, "%s/%s.f42", free42dirname, name); |
| 1539 |
FILE *f = fopen(path, "w"); |
| 1540 |
fprintf(f, FREE42_MAGIC_STR); |
| 1541 |
fclose(f); |
| 1542 |
free(name); |
| 1543 |
gtk_dialog_response(GTK_DIALOG(dlg), 4); |
| 1544 |
} |
| 1545 |
|
| 1546 |
static bool copy_state(const char *orig_name, const char *copy_name) { |
| 1547 |
FILE *fin = fopen(orig_name, "r"); |
| 1548 |
FILE *fout = fopen(copy_name, "w"); |
| 1549 |
if (fin != NULL && fout != NULL) { |
| 1550 |
char buf[1024]; |
| 1551 |
int n; |
| 1552 |
while ((n = fread(buf, 1, 1024, fin)) > 0) |
| 1553 |
fwrite(buf, 1, n, fout); |
| 1554 |
if (ferror(fin) || ferror(fout)) |
| 1555 |
goto duplication_failed; |
| 1556 |
fclose(fin); |
| 1557 |
fclose(fout); |
| 1558 |
return true; |
| 1559 |
} else { |
| 1560 |
duplication_failed: |
| 1561 |
if (fin != NULL) |
| 1562 |
fclose(fin); |
| 1563 |
if (fout != NULL) |
| 1564 |
fclose(fout); |
| 1565 |
remove(copy_name); |
| 1566 |
return false; |
| 1567 |
} |
| 1568 |
} |
| 1569 |
|
| 1570 |
static void states_menu_duplicate() { |
| 1571 |
if (selectedStateIndex == -1) |
| 1572 |
return; |
| 1573 |
char copyName[FILENAMELEN]; |
| 1574 |
strcpy(copyName, state_names[selectedStateIndex]); |
| 1575 |
int n = 0; |
| 1576 |
|
| 1577 |
// We're naming duplicates by appending " copy" or " copy NNN" to the name |
| 1578 |
// of the original, but if the name of the original already ends with " copy" |
| 1579 |
// or " copy NNN", it seems more elegant to continue the sequence rather than |
| 1580 |
// add another " copy" suffix. |
| 1581 |
int len = strlen(copyName); |
| 1582 |
if (len > 5 && strcmp(copyName + len - 5, " copy") == 0) { |
| 1583 |
copyName[len - 5] = 0; |
| 1584 |
n = 1; |
| 1585 |
} else if (len > 7) { |
| 1586 |
int pos = len - 7; |
| 1587 |
int m = 0; |
| 1588 |
int p = 1; |
| 1589 |
while (pos > 0) { |
| 1590 |
char c = copyName[pos + 6]; |
| 1591 |
if (c < '0' || c > '9') |
| 1592 |
goto not_a_copy; |
| 1593 |
m += p * (c - '0'); |
| 1594 |
p *= 10; |
| 1595 |
if (strncmp(copyName + pos, " copy ", 6) == 0) { |
| 1596 |
n = m; |
| 1597 |
copyName[pos] = 0; |
| 1598 |
break; |
| 1599 |
} else |
| 1600 |
pos--; |
| 1601 |
} |
| 1602 |
not_a_copy:; |
| 1603 |
} |
| 1604 |
|
| 1605 |
char finalName[FILENAMELEN]; |
| 1606 |
int flen; |
| 1607 |
while (true) { |
| 1608 |
n++; |
| 1609 |
if (n == 1) |
| 1610 |
flen = snprintf(finalName, FILENAMELEN, "%s/%s copy.f42", free42dirname, copyName); |
| 1611 |
else |
| 1612 |
flen = snprintf(finalName, FILENAMELEN, "%s/%s copy %d.f42", free42dirname, copyName, n); |
| 1613 |
if (flen >= FILENAMELEN) { |
| 1614 |
show_message("Message", "The name of that state is too long to copy.", dlg); |
| 1615 |
return; |
| 1616 |
} |
| 1617 |
if (!file_exists(finalName)) |
| 1618 |
// File does not exist; that means we have a usable name |
| 1619 |
break; |
| 1620 |
} |
| 1621 |
|
| 1622 |
// Once we get here, finalName contains a valid name for creating the duplicate. |
| 1623 |
// What we do next depends on whether the selected state is the currently active |
| 1624 |
// one. If it is, we'll call core_save_state(), to make sure the duplicate |
| 1625 |
// actually matches the most up-to-date state; otherwise, we can simply copy |
| 1626 |
// the existing state file. |
| 1627 |
if (strcmp(state_names[selectedStateIndex], state.coreName) == 0) |
| 1628 |
core_save_state(finalName); |
| 1629 |
else { |
| 1630 |
char origName[FILENAMELEN]; |
| 1631 |
snprintf(origName, FILENAMELEN, "%s/%s.f42", free42dirname, state_names[selectedStateIndex]); |
| 1632 |
if (!copy_state(origName, finalName)) { |
| 1633 |
show_message("Message", "State duplication failed.", dlg); |
| 1634 |
return; |
| 1635 |
} |
| 1636 |
} |
| 1637 |
gtk_dialog_response(GTK_DIALOG(dlg), 4); |
| 1638 |
} |
| 1639 |
|
| 1640 |
static void states_menu_rename() { |
| 1641 |
if (selectedStateIndex == -1) |
| 1642 |
return; |
| 1643 |
char prompt[FILENAMELEN]; |
| 1644 |
snprintf(prompt, FILENAMELEN, "Rename \"%s\" to:", state_names[selectedStateIndex]); |
| 1645 |
char *newname = get_state_name(prompt, state_names[selectedStateIndex]); |
| 1646 |
if (newname == NULL) |
| 1647 |
return; |
| 1648 |
char oldpath[FILENAMELEN]; |
| 1649 |
snprintf(oldpath, FILENAMELEN, "%s/%s.f42", free42dirname, state_names[selectedStateIndex]); |
| 1650 |
char newpath[FILENAMELEN]; |
| 1651 |
snprintf(newpath, FILENAMELEN, "%s/%s.f42", free42dirname, newname); |
| 1652 |
rename(oldpath, newpath); |
| 1653 |
if (strcmp(state_names[selectedStateIndex], state.coreName) == 0) |
| 1654 |
strncpy(state.coreName, newname, FILENAMELEN); |
| 1655 |
gtk_dialog_response(GTK_DIALOG(dlg), 4); |
| 1656 |
} |
| 1657 |
|
| 1658 |
static void states_menu_delete() { |
| 1659 |
if (selectedStateIndex == -1) |
| 1660 |
return; |
| 1661 |
const char *stateName = state_names[selectedStateIndex]; |
| 1662 |
if (strcmp(stateName, state.coreName) == 0) |
| 1663 |
return; |
| 1664 |
GtkWidget *msg = gtk_message_dialog_new(GTK_WINDOW(dlg), |
| 1665 |
GTK_DIALOG_MODAL, |
| 1666 |
GTK_MESSAGE_QUESTION, |
| 1667 |
GTK_BUTTONS_YES_NO, |
| 1668 |
"Are you sure you want to delete the state \"%s\"?", |
| 1669 |
stateName); |
| 1670 |
gtk_window_set_title(GTK_WINDOW(msg), "Delete State?"); |
| 1671 |
gtk_window_set_role(GTK_WINDOW(msg), "Free42 Dialog"); |
| 1672 |
bool cancelled = gtk_dialog_run(GTK_DIALOG(msg)) != GTK_RESPONSE_YES; |
| 1673 |
gtk_widget_destroy(msg); |
| 1674 |
if (cancelled) |
| 1675 |
return; |
| 1676 |
char statePath[FILENAMELEN]; |
| 1677 |
snprintf(statePath, FILENAMELEN, "%s/%s.f42", free42dirname, stateName); |
| 1678 |
remove(statePath); |
| 1679 |
gtk_dialog_response(GTK_DIALOG(dlg), 4); |
| 1680 |
} |
| 1681 |
|
| 1682 |
static void states_menu_import() { |
| 1683 |
static GtkWidget *dialog = NULL; |
| 1684 |
|
| 1685 |
if (dialog == NULL) |
| 1686 |
dialog = make_file_select_dialog("Import State", |
| 1687 |
"Free42 State (*.f42)\0*.[Ff]42\0All Files (*.*)\0*\0", |
| 1688 |
false, dlg); |
| 1689 |
|
| 1690 |
gtk_window_set_role(GTK_WINDOW(dialog), "Free42 Dialog"); |
| 1691 |
bool cancelled = gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_ACCEPT; |
| 1692 |
gtk_widget_hide(dialog); |
| 1693 |
if (cancelled) |
| 1694 |
return; |
| 1695 |
|
| 1696 |
char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); |
| 1697 |
if (filename == NULL) |
| 1698 |
return; |
| 1699 |
|
| 1700 |
char name[FILENAMELEN]; |
| 1701 |
char *p = strrchr(filename, '/'); |
| 1702 |
if (p == NULL) |
| 1703 |
strncpy(name, filename, FILENAMELEN); |
| 1704 |
else |
| 1705 |
strncpy(name, p + 1, FILENAMELEN); |
| 1706 |
name[FILENAMELEN - 1] = 0; |
| 1707 |
int len = strlen(name); |
| 1708 |
if (len > 4 && strcmp(name + len - 4, ".f42") == 0) |
| 1709 |
name[len - 4] = 0; |
| 1710 |
char destPath[FILENAMELEN]; |
| 1711 |
snprintf(destPath, FILENAMELEN, "%s/%s.f42", free42dirname, name); |
| 1712 |
bool success = false; |
| 1713 |
if (file_exists(destPath)) { |
| 1714 |
char msg[FILENAMELEN]; |
| 1715 |
snprintf(msg, FILENAMELEN, "A state named \"%s\" already exists.", name); |
| 1716 |
show_message("Message", msg, dlg); |
| 1717 |
} else if (!copy_state(filename, destPath)) { |
| 1718 |
show_message("Message", "State import failed.", dlg); |
| 1719 |
} else { |
| 1720 |
success = true; |
| 1721 |
} |
| 1722 |
g_free(filename); |
| 1723 |
if (success) |
| 1724 |
gtk_dialog_response(GTK_DIALOG(dlg), 4); |
| 1725 |
} |
| 1726 |
|
| 1727 |
static void states_menu_export() { |
| 1728 |
if (selectedStateIndex == -1) |
| 1729 |
return; |
| 1730 |
|
| 1731 |
static GtkWidget *save_dialog = NULL; |
| 1732 |
if (save_dialog == NULL) |
| 1733 |
save_dialog = make_file_select_dialog("Export State", |
| 1734 |
"Free42 State (*.f42)\0*.[Ff]42\0All Files (*.*)\0*\0", |
| 1735 |
true, dlg); |
| 1736 |
|
| 1737 |
char *filename = NULL; |
| 1738 |
gtk_window_set_role(GTK_WINDOW(save_dialog), "Free42 Dialog"); |
| 1739 |
char export_file_name[FILENAMELEN]; |
| 1740 |
strcpy(export_file_name, state_names[selectedStateIndex]); |
| 1741 |
export_file_name[FILENAMELEN - 5] = 0; |
| 1742 |
strcat(export_file_name, ".f42"); |
| 1743 |
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(save_dialog), export_file_name); |
| 1744 |
if (gtk_dialog_run(GTK_DIALOG(save_dialog)) == GTK_RESPONSE_ACCEPT) |
| 1745 |
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(save_dialog)); |
| 1746 |
gtk_widget_hide(GTK_WIDGET(save_dialog)); |
| 1747 |
if (filename == NULL) |
| 1748 |
return; |
| 1749 |
|
| 1750 |
strcpy(export_file_name, filename); |
| 1751 |
g_free(filename); |
| 1752 |
if (strncmp(gtk_file_filter_get_name( |
| 1753 |
gtk_file_chooser_get_filter( |
| 1754 |
GTK_FILE_CHOOSER(save_dialog))), "All", 3) != 0) |
| 1755 |
appendSuffix(export_file_name, ".f42"); |
| 1756 |
|
| 1757 |
if (file_exists(export_file_name)) { |
| 1758 |
GtkWidget *msg = gtk_message_dialog_new(GTK_WINDOW(mainwindow), |
| 1759 |
GTK_DIALOG_MODAL, |
| 1760 |
GTK_MESSAGE_QUESTION, |
| 1761 |
GTK_BUTTONS_YES_NO, |
| 1762 |
"Replace existing \"%s\"?", |
| 1763 |
export_file_name); |
| 1764 |
gtk_window_set_title(GTK_WINDOW(msg), "Replace?"); |
| 1765 |
gtk_window_set_role(GTK_WINDOW(msg), "Free42 Dialog"); |
| 1766 |
bool cancelled = gtk_dialog_run(GTK_DIALOG(msg)) != GTK_RESPONSE_YES; |
| 1767 |
gtk_widget_destroy(msg); |
| 1768 |
if (cancelled) |
| 1769 |
return; |
| 1770 |
} |
| 1771 |
|
| 1772 |
if (selectedStateIndex == currentStateIndex) |
| 1773 |
core_save_state(export_file_name); |
| 1774 |
else { |
| 1775 |
char orig_path[FILENAMELEN]; |
| 1776 |
snprintf(orig_path, FILENAMELEN, "%s/%s.f42", free42dirname, state_names[selectedStateIndex]); |
| 1777 |
if (!copy_state(orig_path, export_file_name)) |
| 1778 |
show_message("Message", "State export failed.", dlg); |
| 1779 |
} |
| 1780 |
} |
| 1781 |
|
| 1782 |
static void states_menu_cb(GtkWidget *w, gpointer p) { |
| 1783 |
switch ((size_t) p) { |
| 1784 |
case 0: |
| 1785 |
states_menu_new(); |
| 1786 |
break; |
| 1787 |
case 1: |
| 1788 |
states_menu_duplicate(); |
| 1789 |
break; |
| 1790 |
case 2: |
| 1791 |
states_menu_rename(); |
| 1792 |
break; |
| 1793 |
case 3: |
| 1794 |
states_menu_delete(); |
| 1795 |
break; |
| 1796 |
case 4: |
| 1797 |
states_menu_import(); |
| 1798 |
break; |
| 1799 |
case 5: |
| 1800 |
states_menu_export(); |
| 1801 |
break; |
| 1802 |
} |
| 1803 |
} |
| 1804 |
|
| 1805 |
static int case_insens_comparator(const void *a, const void *b) { |
| 1806 |
return strcasecmp(*(const char **) a, *(const char **) b); |
| 1807 |
} |
| 1808 |
|
| 1809 |
static void row_activated_cb(GtkWidget *w, gpointer p) { |
| 1810 |
gtk_dialog_response(GTK_DIALOG(dlg), 1); |
| 1811 |
} |
| 1812 |
|
| 1813 |
static void states_menu_pos_func(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data) { |
| 1814 |
GtkWidget *btn = GTK_WIDGET(data); |
| 1815 |
gdk_window_get_origin(gtk_widget_get_window(btn), x, y); |
| 1816 |
GtkAllocation allocation; |
| 1817 |
gtk_widget_get_allocation(btn, &allocation); |
| 1818 |
*x += allocation.x; |
| 1819 |
*y += allocation.y + allocation.height; |
| 1820 |
*push_in = true; |
| 1821 |
} |
| 1822 |
|
| 1823 |
static void statesCB() { |
| 1824 |
static GtkWidget *states_dialog = NULL; |
| 1825 |
static GtkTreeView *tree; |
| 1826 |
static GtkTreeSelection *select; |
| 1827 |
static GtkWidget *currentLabel; |
| 1828 |
static GtkWidget *menu; |
| 1829 |
char buf[FILENAMELEN]; |
| 1830 |
|
| 1831 |
if (states_dialog == NULL) { |
| 1832 |
// Pop-up menu for "More" button |
| 1833 |
menu = gtk_menu_new(); |
| 1834 |
statesMenuItems[0] = gtk_menu_item_new_with_label("New"); |
| 1835 |
statesMenuItems[1] = gtk_menu_item_new_with_label("Duplicate"); |
| 1836 |
statesMenuItems[2] = gtk_menu_item_new_with_label("Rename"); |
| 1837 |
statesMenuItems[3] = gtk_menu_item_new_with_label("Delete"); |
| 1838 |
statesMenuItems[4] = gtk_menu_item_new_with_label("Import"); |
| 1839 |
statesMenuItems[5] = gtk_menu_item_new_with_label("Export"); |
| 1840 |
for (int i = 0; i < 6; i++) { |
| 1841 |
g_signal_connect(G_OBJECT(statesMenuItems[i]), "activate", G_CALLBACK(states_menu_cb), (gpointer) (size_t) i); |
| 1842 |
gtk_menu_shell_append(GTK_MENU_SHELL(menu), statesMenuItems[i]); |
| 1843 |
gtk_widget_show(statesMenuItems[i]); |
| 1844 |
} |
| 1845 |
gtk_widget_show_all(menu); |
| 1846 |
|
| 1847 |
// This is where the actual dialog starts |
| 1848 |
states_dialog = gtk_dialog_new_with_buttons( |
| 1849 |
"States", |
| 1850 |
GTK_WINDOW(mainwindow), |
| 1851 |
GTK_DIALOG_MODAL, |
| 1852 |
"Switch To", 1, |
| 1853 |
"More", 2, |
| 1854 |
"Done", 3, |
| 1855 |
NULL); |
| 1856 |
for (int i = 1; i <= 3; i++) { |
| 1857 |
GtkWidget *btn = gtk_dialog_get_widget_for_response(GTK_DIALOG(states_dialog), i); |
| 1858 |
gtk_widget_set_can_default(btn, false); |
| 1859 |
} |
| 1860 |
gtk_window_set_resizable(GTK_WINDOW(states_dialog), FALSE); |
| 1861 |
no_mwm_resize_borders(states_dialog); |
| 1862 |
GtkWidget *container = gtk_bin_get_child(GTK_BIN(states_dialog)); |
| 1863 |
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 1864 |
gtk_container_add(GTK_CONTAINER(container), box); |
| 1865 |
|
| 1866 |
currentLabel = gtk_label_new("Current:"); |
| 1867 |
gtk_box_pack_start(GTK_BOX(box), currentLabel, FALSE, FALSE, 10); |
| 1868 |
|
| 1869 |
GtkWidget *scroll = gtk_scrolled_window_new(NULL, NULL); |
| 1870 |
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); |
| 1871 |
tree = (GtkTreeView *) gtk_tree_view_new(); |
| 1872 |
select = gtk_tree_view_get_selection(tree); |
| 1873 |
gtk_tree_selection_set_mode(select, GTK_SELECTION_SINGLE); |
| 1874 |
GtkWidget *switchToBtn = gtk_dialog_get_widget_for_response(GTK_DIALOG(states_dialog), 1); |
| 1875 |
g_signal_connect(G_OBJECT(select), "changed", G_CALLBACK(states_changed_cb), (gpointer) switchToBtn); |
| 1876 |
// When I try to pass the dialog pointer as closure data, it gets mangled?!? |
| 1877 |
// Most be a casting issue, but I don't get it... So just passing it in a global. |
| 1878 |
dlg = states_dialog; |
| 1879 |
g_signal_connect(G_OBJECT(tree), "row-activated", G_CALLBACK(row_activated_cb), NULL); |
| 1880 |
GtkCellRenderer *renderer = gtk_cell_renderer_text_new(); |
| 1881 |
//gtk_cell_renderer_text_set_fixed_height_from_font((GtkCellRendererText *) renderer, 12); |
| 1882 |
GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes("Foo", renderer, "text", 0, NULL); |
| 1883 |
gtk_tree_view_append_column(tree, column); |
| 1884 |
gtk_tree_view_set_headers_visible(tree, FALSE); |
| 1885 |
gtk_container_add(GTK_CONTAINER(scroll), GTK_WIDGET(tree)); |
| 1886 |
gtk_widget_set_size_request(scroll, -1, 200); |
| 1887 |
gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(scroll), FALSE, FALSE, 10); |
| 1888 |
|
| 1889 |
gtk_widget_show_all(states_dialog); |
| 1890 |
} |
| 1891 |
|
| 1892 |
load_state_names: |
| 1893 |
|
| 1894 |
// Make sure a file exists for the current state. This isn't necessarily |
| 1895 |
// the case, specifically, right after starting up with a version <= 25 |
| 1896 |
// state file. |
| 1897 |
snprintf(buf, FILENAMELEN, "%s/%s.f42", free42dirname, state.coreName); |
| 1898 |
if (!file_exists(buf)) { |
| 1899 |
FILE *f = fopen(buf, "w"); |
| 1900 |
fwrite(FREE42_MAGIC_STR, 1, 4, f); |
| 1901 |
fclose(f); |
| 1902 |
} |
| 1903 |
|
| 1904 |
snprintf(buf, FILENAMELEN, "Current: %s", state.coreName); |
| 1905 |
gtk_label_set_text(GTK_LABEL(currentLabel), buf); |
| 1906 |
|
| 1907 |
state_names = (char **) malloc(16 * sizeof(char *)); |
| 1908 |
int state_size = 0, state_capacity = 16; |
| 1909 |
DIR *dir = opendir(free42dirname); |
| 1910 |
if (dir != NULL) { |
| 1911 |
struct dirent *dent; |
| 1912 |
while ((dent = readdir(dir)) != NULL) { |
| 1913 |
int namelen = strlen(dent->d_name); |
| 1914 |
if (namelen < 4) |
| 1915 |
continue; |
| 1916 |
if (strcmp(dent->d_name + namelen - 4, ".f42") != 0) |
| 1917 |
continue; |
| 1918 |
char *stn = (char *) malloc(namelen - 3); |
| 1919 |
memcpy(stn, dent->d_name, namelen - 4); |
| 1920 |
stn[namelen - 4] = 0; |
| 1921 |
if (state_size == state_capacity) { |
| 1922 |
state_capacity += 16; |
| 1923 |
state_names = (char **) realloc(state_names, state_capacity * sizeof(char *)); |
| 1924 |
} |
| 1925 |
state_names[state_size++] = stn; |
| 1926 |
} |
| 1927 |
closedir(dir); |
| 1928 |
qsort(state_names, state_size, sizeof(char *), case_insens_comparator); |
| 1929 |
} |
| 1930 |
|
| 1931 |
currentStateIndex = -1; |
| 1932 |
GtkListStore *model = gtk_list_store_new(1, G_TYPE_STRING); |
| 1933 |
GtkTreeIter iter; |
| 1934 |
for (int i = 0; i < state_size; i++) { |
| 1935 |
gtk_list_store_append(model, &iter); |
| 1936 |
gtk_list_store_set(model, &iter, 0, state_names[i], -1); |
| 1937 |
if (strcmp(state_names[i], state.coreName) == 0) |
| 1938 |
currentStateIndex = i; |
| 1939 |
} |
| 1940 |
gtk_tree_view_set_model(tree, GTK_TREE_MODEL(model)); |
| 1941 |
|
| 1942 |
// TODO: does this leak list-stores? Or is everything taken case of by the |
| 1943 |
// GObject reference-counting stuff? |
| 1944 |
|
| 1945 |
gtk_window_set_role(GTK_WINDOW(states_dialog), "Free42 Dialog"); |
| 1946 |
while (true) { |
| 1947 |
int response = gtk_dialog_run(GTK_DIALOG(states_dialog)); |
| 1948 |
if (response == 3 || response == GTK_RESPONSE_DELETE_EVENT) |
| 1949 |
break; |
| 1950 |
else if (response == 1) { |
| 1951 |
GtkWidget *btn = gtk_dialog_get_widget_for_response(GTK_DIALOG(states_dialog), 1); |
| 1952 |
if (!gtk_widget_get_sensitive(btn)) |
| 1953 |
// User double-clicked on a row, but the Switch To button is disabled |
| 1954 |
continue; |
| 1955 |
GList *rows = gtk_tree_selection_get_selected_rows(select, NULL); |
| 1956 |
if (rows != NULL) { |
| 1957 |
GtkTreePath *path = (GtkTreePath *) rows->data; |
| 1958 |
int sel; |
| 1959 |
sscanf(gtk_tree_path_to_string(path), "%d", &sel); |
| 1960 |
g_list_free(rows); |
| 1961 |
if (switchTo(state_names[sel])) |
| 1962 |
break; |
| 1963 |
} |
| 1964 |
} else if (response == 2) { |
| 1965 |
GtkWidget *btn = gtk_dialog_get_widget_for_response(GTK_DIALOG(states_dialog), 2); |
| 1966 |
gtk_menu_popup(GTK_MENU(menu), NULL, NULL, states_menu_pos_func, btn, 0, GDK_CURRENT_TIME); |
| 1967 |
} else if (response == 4) { |
| 1968 |
for (int i = 0; i < state_size; i++) |
| 1969 |
free(state_names[i]); |
| 1970 |
free(state_names); |
| 1971 |
goto load_state_names; |
| 1972 |
} |
| 1973 |
} |
| 1974 |
|
| 1975 |
gtk_widget_hide(states_dialog); |
| 1976 |
|
| 1977 |
for (int i = 0; i < state_size; i++) |
| 1978 |
free(state_names[i]); |
| 1979 |
free(state_names); |
| 1980 |
} |
| 1981 |
|
| 1982 |
////////////////////////////////// |
| 1983 |
///// States stuff ends here ///// |
| 1984 |
////////////////////////////////// |
| 1985 |
|
| 1986 |
static void showPrintOutCB() { |
| 1987 |
gtk_window_present(GTK_WINDOW(printwindow)); |
| 1988 |
gdk_window_focus(gtk_widget_get_window(printwindow), GDK_CURRENT_TIME); |
| 1989 |
state.printWindowKnown = 1; |
| 1990 |
state.printWindowMapped = 1; |
| 1991 |
} |
| 1992 |
|
| 1993 |
static void exportProgramCB() { |
| 1994 |
static GtkWidget *sel_dialog = NULL; |
| 1995 |
static GtkTreeView *tree; |
| 1996 |
static GtkTreeSelection *select; |
| 1997 |
|
| 1998 |
if (sel_dialog == NULL) { |
| 1999 |
sel_dialog = gtk_dialog_new_with_buttons( |
| 2000 |
"Export Programs", |
| 2001 |
GTK_WINDOW(mainwindow), |
| 2002 |
GTK_DIALOG_MODAL, |
| 2003 |
"_OK", GTK_RESPONSE_ACCEPT, |
| 2004 |
"_Cancel", GTK_RESPONSE_CANCEL, |
| 2005 |
NULL); |
| 2006 |
gtk_window_set_resizable(GTK_WINDOW(sel_dialog), FALSE); |
| 2007 |
no_mwm_resize_borders(sel_dialog); |
| 2008 |
GtkWidget *container = gtk_bin_get_child(GTK_BIN(sel_dialog)); |
| 2009 |
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 2010 |
gtk_container_add(GTK_CONTAINER(container), box); |
| 2011 |
|
| 2012 |
GtkWidget *label = gtk_label_new("Select Programs to Export:"); |
| 2013 |
gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 10); |
| 2014 |
|
| 2015 |
GtkWidget *scroll = gtk_scrolled_window_new(NULL, NULL); |
| 2016 |
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); |
| 2017 |
tree = (GtkTreeView *) gtk_tree_view_new(); |
| 2018 |
select = gtk_tree_view_get_selection(tree); |
| 2019 |
gtk_tree_selection_set_mode(select, GTK_SELECTION_MULTIPLE); |
| 2020 |
GtkCellRenderer *renderer = gtk_cell_renderer_text_new(); |
| 2021 |
GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes("Foo", renderer, "text", 0, NULL); |
| 2022 |
gtk_tree_view_append_column(tree, column); |
| 2023 |
gtk_tree_view_set_headers_visible(tree, FALSE); |
| 2024 |
gtk_container_add(GTK_CONTAINER(scroll), GTK_WIDGET(tree)); |
| 2025 |
gtk_widget_set_size_request(scroll, -1, 200); |
| 2026 |
gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(scroll), FALSE, FALSE, 10); |
| 2027 |
|
| 2028 |
gtk_widget_show_all(GTK_WIDGET(sel_dialog)); |
| 2029 |
} |
| 2030 |
|
| 2031 |
char *buf = core_list_programs(); |
| 2032 |
|
| 2033 |
GtkListStore *model = gtk_list_store_new(1, G_TYPE_STRING); |
| 2034 |
if (buf != NULL) { |
| 2035 |
int count = ((buf[0] & 255) << 24) | ((buf[1] & 255) << 16) | ((buf[2] & 255) << 8) | (buf[3] & 255); |
| 2036 |
char *p = buf + 4; |
| 2037 |
GtkTreeIter iter; |
| 2038 |
while (count-- > 0) { |
| 2039 |
gtk_list_store_append(model, &iter); |
| 2040 |
gtk_list_store_set(model, &iter, 0, p, -1); |
| 2041 |
p += strlen(p) + 1; |
| 2042 |
} |
| 2043 |
} |
| 2044 |
gtk_tree_view_set_model(tree, GTK_TREE_MODEL(model)); |
| 2045 |
|
| 2046 |
// TODO: does this leak list-stores? Or is everything taken case of by the |
| 2047 |
// GObject reference-counting stuff? |
| 2048 |
|
| 2049 |
gtk_window_set_role(GTK_WINDOW(sel_dialog), "Free42 Dialog"); |
| 2050 |
bool cancelled = gtk_dialog_run(GTK_DIALOG(sel_dialog)) != GTK_RESPONSE_ACCEPT; |
| 2051 |
gtk_widget_hide(sel_dialog); |
| 2052 |
if (cancelled) { |
| 2053 |
free(buf); |
| 2054 |
return; |
| 2055 |
} |
| 2056 |
|
| 2057 |
int count = gtk_tree_selection_count_selected_rows(select); |
| 2058 |
if (count == 0) { |
| 2059 |
free(buf); |
| 2060 |
return; |
| 2061 |
} |
| 2062 |
|
| 2063 |
int *p2 = (int *) malloc(count * sizeof(int)); |
| 2064 |
// TODO - handle memory allocation failure |
| 2065 |
GList *rows = gtk_tree_selection_get_selected_rows(select, NULL); |
| 2066 |
GList *item = rows; |
| 2067 |
int i = 0; |
| 2068 |
while (item != NULL) { |
| 2069 |
GtkTreePath *path = (GtkTreePath *) item->data; |
| 2070 |
char *pathstring = gtk_tree_path_to_string(path); |
| 2071 |
sscanf(pathstring, "%d", p2 + i); |
| 2072 |
item = item->next; |
| 2073 |
i++; |
| 2074 |
} |
| 2075 |
g_list_free(rows); |
| 2076 |
|
| 2077 |
char suggested_name[50] = "Untitled.raw"; |
| 2078 |
char *p = buf + 4; |
| 2079 |
int sel = p2[0]; |
| 2080 |
while (sel > 0) { |
| 2081 |
p += strlen(p) + 1; |
| 2082 |
sel--; |
| 2083 |
} |
| 2084 |
if (p[0] == '"') { |
| 2085 |
char *closing_quote = strchr(p + 1, '"'); |
| 2086 |
if (closing_quote != NULL) { |
| 2087 |
int len = closing_quote - p - 1; |
| 2088 |
for (int i = 0; i < len; i++) { |
| 2089 |
char c = p[i + 1]; |
| 2090 |
if (c == '/' || c == 10) |
| 2091 |
c = '_'; |
| 2092 |
suggested_name[i] = c; |
| 2093 |
} |
| 2094 |
suggested_name[len] = 0; |
| 2095 |
strcat(suggested_name, ".raw"); |
| 2096 |
} |
| 2097 |
} |
| 2098 |
free(buf); |
| 2099 |
|
| 2100 |
static GtkWidget *save_dialog = NULL; |
| 2101 |
if (save_dialog == NULL) |
| 2102 |
save_dialog = make_file_select_dialog("Export Programs", |
| 2103 |
"Program Files (*.raw)\0*.[Rr][Aa][Ww]\0All Files (*.*)\0*\0", |
| 2104 |
true, mainwindow); |
| 2105 |
|
| 2106 |
char *filename = NULL; |
| 2107 |
gtk_window_set_role(GTK_WINDOW(save_dialog), "Free42 Dialog"); |
| 2108 |
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(save_dialog), suggested_name); |
| 2109 |
if (gtk_dialog_run(GTK_DIALOG(save_dialog)) == GTK_RESPONSE_ACCEPT) |
| 2110 |
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(save_dialog)); |
| 2111 |
gtk_widget_hide(GTK_WIDGET(save_dialog)); |
| 2112 |
if (filename == NULL) { |
| 2113 |
free(p2); |
| 2114 |
return; |
| 2115 |
} |
| 2116 |
|
| 2117 |
char export_file_name[FILENAMELEN]; |
| 2118 |
strcpy(export_file_name, filename); |
| 2119 |
g_free(filename); |
| 2120 |
if (strncmp(gtk_file_filter_get_name( |
| 2121 |
gtk_file_chooser_get_filter( |
| 2122 |
GTK_FILE_CHOOSER(save_dialog))), "All", 3) != 0) |
| 2123 |
appendSuffix(export_file_name, ".raw"); |
| 2124 |
|
| 2125 |
if (file_exists(export_file_name)) { |
| 2126 |
GtkWidget *msg = gtk_message_dialog_new(GTK_WINDOW(mainwindow), |
| 2127 |
GTK_DIALOG_MODAL, |
| 2128 |
GTK_MESSAGE_QUESTION, |
| 2129 |
GTK_BUTTONS_YES_NO, |
| 2130 |
"Replace existing \"%s\"?", |
| 2131 |
export_file_name); |
| 2132 |
gtk_window_set_title(GTK_WINDOW(msg), "Replace?"); |
| 2133 |
gtk_window_set_role(GTK_WINDOW(msg), "Free42 Dialog"); |
| 2134 |
cancelled = gtk_dialog_run(GTK_DIALOG(msg)) != GTK_RESPONSE_YES; |
| 2135 |
gtk_widget_destroy(msg); |
| 2136 |
if (cancelled) { |
| 2137 |
free(p2); |
| 2138 |
return; |
| 2139 |
} |
| 2140 |
} |
| 2141 |
|
| 2142 |
core_export_programs(count, p2, export_file_name); |
| 2143 |
free(p2); |
| 2144 |
} |
| 2145 |
|
| 2146 |
static GtkWidget *make_file_select_dialog(const char *title, |
| 2147 |
const char *pattern, |
| 2148 |
bool save, |
| 2149 |
GtkWidget *owner) { |
| 2150 |
GtkWidget *dialog = gtk_file_chooser_dialog_new( |
| 2151 |
title, |
| 2152 |
GTK_WINDOW(owner), |
| 2153 |
save ? GTK_FILE_CHOOSER_ACTION_SAVE |
| 2154 |
: GTK_FILE_CHOOSER_ACTION_OPEN, |
| 2155 |
"_Cancel", GTK_RESPONSE_CANCEL, |
| 2156 |
save ? "_Save" : "_Open", |
| 2157 |
GTK_RESPONSE_ACCEPT, |
| 2158 |
NULL); |
| 2159 |
const char *p = pattern; |
| 2160 |
while (1) { |
| 2161 |
const char *descr, *ext; |
| 2162 |
int n = strlen(p); |
| 2163 |
if (n == 0) |
| 2164 |
break; |
| 2165 |
descr = p; |
| 2166 |
p += n + 1; |
| 2167 |
n = strlen(p); |
| 2168 |
if (n == 0) |
| 2169 |
break; |
| 2170 |
ext = p; |
| 2171 |
p += n + 1; |
| 2172 |
|
| 2173 |
GtkFileFilter *filter = gtk_file_filter_new(); |
| 2174 |
gtk_file_filter_add_pattern(filter, ext); |
| 2175 |
gtk_file_filter_set_name(filter, descr); |
| 2176 |
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter); |
| 2177 |
} |
| 2178 |
return dialog; |
| 2179 |
} |
| 2180 |
|
| 2181 |
static void importProgramCB() { |
| 2182 |
static GtkWidget *dialog = NULL; |
| 2183 |
|
| 2184 |
if (dialog == NULL) |
| 2185 |
dialog = make_file_select_dialog("Import Programs", |
| 2186 |
"Program Files (*.raw)\0*.[Rr][Aa][Ww]\0All Files (*.*)\0*\0", |
| 2187 |
false, mainwindow); |
| 2188 |
|
| 2189 |
gtk_window_set_role(GTK_WINDOW(dialog), "Free42 Dialog"); |
| 2190 |
bool cancelled = gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_ACCEPT; |
| 2191 |
gtk_widget_hide(dialog); |
| 2192 |
if (cancelled) |
| 2193 |
return; |
| 2194 |
|
| 2195 |
char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); |
| 2196 |
if (filename == NULL) |
| 2197 |
return; |
| 2198 |
|
| 2199 |
char filenamebuf[FILENAMELEN]; |
| 2200 |
strncpy(filenamebuf, filename, FILENAMELEN); |
| 2201 |
filenamebuf[FILENAMELEN - 1] = 0; |
| 2202 |
g_free(filename); |
| 2203 |
|
| 2204 |
if (strncmp(gtk_file_filter_get_name( |
| 2205 |
gtk_file_chooser_get_filter( |
| 2206 |
GTK_FILE_CHOOSER(dialog))), "All", 3) != 0) |
| 2207 |
appendSuffix(filenamebuf, ".raw"); |
| 2208 |
|
| 2209 |
core_import_programs(0, filenamebuf); |
| 2210 |
redisplay(); |
| 2211 |
} |
| 2212 |
|
| 2213 |
static void paperAdvanceCB() { |
| 2214 |
static const char *bits = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; |
| 2215 |
shell_print("", 0, bits, 18, 0, 0, 143, 9); |
| 2216 |
} |
| 2217 |
|
| 2218 |
static char *tb; |
| 2219 |
static int tblen, tbcap; |
| 2220 |
|
| 2221 |
static void tbwriter(const char *text, int length) { |
| 2222 |
if (tblen + length > tbcap) { |
| 2223 |
tbcap += length + 8192; |
| 2224 |
tb = (char *) realloc(tb, tbcap); |
| 2225 |
} |
| 2226 |
if (tb != NULL) { |
| 2227 |
memcpy(tb + tblen, text, length); |
| 2228 |
tblen += length; |
| 2229 |
} |
| 2230 |
} |
| 2231 |
|
| 2232 |
static void tbnewliner() { |
| 2233 |
tbwriter("\n", 1); |
| 2234 |
} |
| 2235 |
|
| 2236 |
static void tbnonewliner() { |
| 2237 |
// No-op |
| 2238 |
} |
| 2239 |
|
| 2240 |
static void copyPrintAsTextCB() { |
| 2241 |
tb = NULL; |
| 2242 |
tblen = tbcap = 0; |
| 2243 |
|
| 2244 |
int len = print_text_bottom - print_text_top; |
| 2245 |
if (len < 0) |
| 2246 |
len += PRINT_TEXT_SIZE; |
| 2247 |
// Calculate effective top, since printout_top can point |
| 2248 |
// at a truncated line, and we want to skip those when |
| 2249 |
// copying |
| 2250 |
int top = printout_bottom - 2 * print_text_pixel_height; |
| 2251 |
if (top < 0) |
| 2252 |
top += PRINT_LINES; |
| 2253 |
int p = print_text_top; |
| 2254 |
int pixel_v = 0; |
| 2255 |
while (len > 0) { |
| 2256 |
int z = print_text[p++]; |
| 2257 |
if (z == 255) { |
| 2258 |
char buf[34]; |
| 2259 |
for (int v = 0; v < 16; v += 2) { |
| 2260 |
for (int vv = 0; vv < 2; vv++) { |
| 2261 |
int V = top + (pixel_v + v + vv) * 2; |
| 2262 |
if (V >= PRINT_LINES) |
| 2263 |
V -= PRINT_LINES; |
| 2264 |
for (int h = 0; h < 17; h++) { |
| 2265 |
unsigned char a = print_bitmap[V * PRINT_BYTESPERLINE + 2 * h + 1]; |
| 2266 |
unsigned char b = print_bitmap[V * PRINT_BYTESPERLINE + 2 * h]; |
| 2267 |
buf[vv * 17 + h] = (a & 128) | ((a & 32) << 1) | ((a & 8) << 2) | ((a & 2) << 3) | ((b & 128) >> 4) | ((b & 32) >> 3) | ((b & 8) >> 2) | ((b & 2) >> 1); |
| 2268 |
} |
| 2269 |
} |
| 2270 |
shell_spool_bitmap_to_txt(buf, 17, 0, 0, 131, 2, tbwriter, tbnewliner); |
| 2271 |
} |
| 2272 |
pixel_v += 16; |
| 2273 |
} else { |
| 2274 |
if (p + z < PRINT_TEXT_SIZE) { |
| 2275 |
shell_spool_txt((const char *) (print_text + p), z, tbwriter, tbnewliner); |
| 2276 |
p += z; |
| 2277 |
} else { |
| 2278 |
int d = PRINT_TEXT_SIZE - p; |
| 2279 |
shell_spool_txt((const char *) (print_text + p), d, tbwriter, tbnonewliner); |
| 2280 |
shell_spool_txt((const char *) print_text, z - d, tbwriter, tbnewliner); |
| 2281 |
p = z - d; |
| 2282 |
} |
| 2283 |
len -= z; |
| 2284 |
pixel_v += 9; |
| 2285 |
} |
| 2286 |
len--; |
| 2287 |
} |
| 2288 |
tbwriter("\0", 1); |
| 2289 |
|
| 2290 |
GtkClipboard *clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); |
| 2291 |
gtk_clipboard_set_text(clip, tb, -1); |
| 2292 |
clip = gtk_clipboard_get(GDK_SELECTION_PRIMARY); |
| 2293 |
gtk_clipboard_set_text(clip, tb, -1); |
| 2294 |
free(tb); |
| 2295 |
} |
| 2296 |
|
| 2297 |
static void copyPrintAsImageCB() { |
| 2298 |
int length = printout_bottom - printout_top; |
| 2299 |
if (length < 0) |
| 2300 |
length += PRINT_LINES; |
| 2301 |
bool empty = length == 0; |
| 2302 |
if (empty) |
| 2303 |
length += 2; |
| 2304 |
|
| 2305 |
GdkPixbuf *buf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, |
| 2306 |
8, 358, length); |
| 2307 |
int d_bpl = gdk_pixbuf_get_rowstride(buf); |
| 2308 |
guchar *d1 = gdk_pixbuf_get_pixels(buf); |
| 2309 |
|
| 2310 |
if (empty) { |
| 2311 |
memset(d1, 255, 2148); |
| 2312 |
} else { |
| 2313 |
for (int v = 0; v < length; v++) { |
| 2314 |
int v2 = printout_top + v; |
| 2315 |
if (v2 >= PRINT_LINES) |
| 2316 |
v2 -= PRINT_LINES; |
| 2317 |
int v3 = v2 * 36; |
| 2318 |
guchar *dst = d1; |
| 2319 |
for (int h = 0; h < 358; h++) { |
| 2320 |
unsigned char c; |
| 2321 |
if (h < 36 || h >= 322) |
| 2322 |
c = 255; |
| 2323 |
else if ((print_bitmap[v3 + ((h - 36) >> 3)] & (1 << ((h - 36) & 7))) == 0) |
| 2324 |
c = 255; |
| 2325 |
else |
| 2326 |
c = 0; |
| 2327 |
*dst++ = c; |
| 2328 |
*dst++ = c; |
| 2329 |
*dst++ = c; |
| 2330 |
} |
| 2331 |
d1 += d_bpl; |
| 2332 |
} |
| 2333 |
} |
| 2334 |
|
| 2335 |
GtkClipboard *clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); |
| 2336 |
gtk_clipboard_set_image(clip, buf); |
| 2337 |
} |
| 2338 |
|
| 2339 |
static void clearPrintOutCB() { |
| 2340 |
printout_top = 0; |
| 2341 |
printout_bottom = 0; |
| 2342 |
print_text_top = 0; |
| 2343 |
print_text_bottom = 0; |
| 2344 |
print_text_pixel_height = 0; |
| 2345 |
gtk_widget_set_size_request(print_widget, 358, 1); |
| 2346 |
|
| 2347 |
if (print_gif != NULL) { |
| 2348 |
shell_finish_gif(gif_seeker, gif_writer); |
| 2349 |
fclose(print_gif); |
| 2350 |
print_gif = NULL; |
| 2351 |
} |
| 2352 |
} |
| 2353 |
|
| 2354 |
struct browse_file_info { |
| 2355 |
const char *title; |
| 2356 |
const char *patterns; |
| 2357 |
GtkWidget *textfield; |
| 2358 |
browse_file_info(const char *t, const char *p, GtkWidget *tf) |
| 2359 |
: title(t), patterns(p), textfield(tf) {} |
| 2360 |
}; |
| 2361 |
|
| 2362 |
static void browse_file(GtkButton *button, gpointer cd) { |
| 2363 |
browse_file_info *info = (browse_file_info *) cd; |
| 2364 |
GtkWidget *dialog = gtk_widget_get_toplevel(GTK_WIDGET(button)); |
| 2365 |
GtkWidget *save_dialog = make_file_select_dialog( |
| 2366 |
info->title, info->patterns, true, dialog); |
| 2367 |
const char *filename = gtk_entry_get_text(GTK_ENTRY(info->textfield)); |
| 2368 |
if (filename[0] == '/') |
| 2369 |
gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(save_dialog), filename); |
| 2370 |
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(save_dialog), filename); |
| 2371 |
|
| 2372 |
gtk_window_set_role(GTK_WINDOW(save_dialog), "Free42 Dialog"); |
| 2373 |
if (gtk_dialog_run(GTK_DIALOG(save_dialog)) == GTK_RESPONSE_ACCEPT) { |
| 2374 |
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(save_dialog)); |
| 2375 |
|
| 2376 |
char filenamebuf[FILENAMELEN]; |
| 2377 |
strncpy(filenamebuf, filename, FILENAMELEN); |
| 2378 |
filenamebuf[FILENAMELEN - 1] = 0; |
| 2379 |
const gchar *filtername = |
| 2380 |
gtk_file_filter_get_name( |
| 2381 |
gtk_file_chooser_get_filter( |
| 2382 |
GTK_FILE_CHOOSER(save_dialog))); |
| 2383 |
if (strncmp(filtername, "Text", 4) == 0) |
| 2384 |
appendSuffix(filenamebuf, ".txt"); |
| 2385 |
else if (strncmp(filtername, "GIF", 3) == 0) |
| 2386 |
appendSuffix(filenamebuf, ".gif"); |
| 2387 |
|
| 2388 |
gtk_entry_set_text(GTK_ENTRY(info->textfield), filenamebuf); |
| 2389 |
} |
| 2390 |
gtk_widget_destroy(GTK_WIDGET(save_dialog)); |
| 2391 |
} |
| 2392 |
|
| 2393 |
static void preferencesCB() { |
| 2394 |
static GtkWidget *dialog = NULL; |
| 2395 |
static GtkWidget *singularmatrix; |
| 2396 |
static GtkWidget *matrixoutofrange; |
| 2397 |
static GtkWidget *autorepeat; |
| 2398 |
static GtkWidget *allowbigstack; |
| 2399 |
static GtkWidget *localizedcopypaste; |
| 2400 |
static GtkWidget *repaintwholedisplay; |
| 2401 |
static GtkWidget *printtotext; |
| 2402 |
static GtkWidget *textpath; |
| 2403 |
static GtkWidget *printtogif; |
| 2404 |
static GtkWidget *gifpath; |
| 2405 |
static GtkWidget *gifheight; |
| 2406 |
|
| 2407 |
if (dialog == NULL) { |
| 2408 |
dialog = gtk_dialog_new_with_buttons( |
| 2409 |
"Preferences", |
| 2410 |
GTK_WINDOW(mainwindow), |
| 2411 |
GTK_DIALOG_MODAL, |
| 2412 |
"_OK", GTK_RESPONSE_ACCEPT, |
| 2413 |
"_Cancel", GTK_RESPONSE_CANCEL, |
| 2414 |
NULL); |
| 2415 |
gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE); |
| 2416 |
no_mwm_resize_borders(dialog); |
| 2417 |
GtkWidget *container = gtk_bin_get_child(GTK_BIN(dialog)); |
| 2418 |
GtkWidget *grid = gtk_grid_new(); |
| 2419 |
gtk_container_add(GTK_CONTAINER(container), grid); |
| 2420 |
|
| 2421 |
singularmatrix = gtk_check_button_new_with_label("Inverting or solving a singular matrix yields \"Singular Matrix\" error"); |
| 2422 |
gtk_grid_attach(GTK_GRID(grid), singularmatrix, 0, 0, 4, 1); |
| 2423 |
matrixoutofrange = gtk_check_button_new_with_label("Overflows during matrix operations yield \"Out of Range\" error"); |
| 2424 |
gtk_grid_attach(GTK_GRID(grid), matrixoutofrange, 0, 1, 4, 1); |
| 2425 |
autorepeat = gtk_check_button_new_with_label("Auto-repeat for number entry and ALPHA mode"); |
| 2426 |
gtk_grid_attach(GTK_GRID(grid), autorepeat, 0, 2, 4, 1); |
| 2427 |
allowbigstack = gtk_check_button_new_with_label("Allow Big Stack (NSTK) mode"); |
| 2428 |
gtk_grid_attach(GTK_GRID(grid), allowbigstack, 0, 3, 4, 1); |
| 2429 |
localizedcopypaste = gtk_check_button_new_with_label("Localized Copy & Paste"); |
| 2430 |
gtk_grid_attach(GTK_GRID(grid), localizedcopypaste, 0, 4, 4, 1); |
| 2431 |
repaintwholedisplay = gtk_check_button_new_with_label("Always repaint entire display"); |
| 2432 |
gtk_grid_attach(GTK_GRID(grid), repaintwholedisplay, 0, 5, 4, 1); |
| 2433 |
printtotext = gtk_check_button_new_with_label("Print to text file:"); |
| 2434 |
gtk_grid_attach(GTK_GRID(grid), printtotext, 0, 6, 1, 1); |
| 2435 |
textpath = gtk_entry_new(); |
| 2436 |
gtk_grid_attach(GTK_GRID(grid), textpath, 1, 6, 2, 1); |
| 2437 |
GtkWidget *browse1 = gtk_button_new_with_label("Browse..."); |
| 2438 |
gtk_grid_attach(GTK_GRID(grid), browse1, 3, 6, 1, 1); |
| 2439 |
printtogif = gtk_check_button_new_with_label("Print to GIF file:"); |
| 2440 |
gtk_grid_attach(GTK_GRID(grid), printtogif, 0, 7, 1, 1); |
| 2441 |
gifpath = gtk_entry_new(); |
| 2442 |
gtk_grid_attach(GTK_GRID(grid), gifpath, 1, 7, 2, 1); |
| 2443 |
GtkWidget *browse2 = gtk_button_new_with_label("Browse..."); |
| 2444 |
gtk_grid_attach(GTK_GRID(grid), browse2, 3, 7, 1, 1); |
| 2445 |
GtkWidget *label = gtk_label_new("Maximum GIF height (pixels):"); |
| 2446 |
gtk_grid_attach(GTK_GRID(grid), label, 1, 8, 1, 1); |
| 2447 |
gifheight = gtk_entry_new(); |
| 2448 |
gtk_entry_set_max_length(GTK_ENTRY(gifheight), 5); |
| 2449 |
gtk_grid_attach(GTK_GRID(grid), gifheight, 2, 8, 1, 1); |
| 2450 |
|
| 2451 |
g_signal_connect(G_OBJECT(browse1), "clicked", G_CALLBACK(browse_file), |
| 2452 |
(gpointer) new browse_file_info("Select Text File Name", |
| 2453 |
"Text (*.txt)\0*.[Tt][Xx][Tt]\0All Files (*.*)\0*\0", |
| 2454 |
textpath)); |
| 2455 |
g_signal_connect(G_OBJECT(browse2), "clicked", G_CALLBACK(browse_file), |
| 2456 |
(gpointer) new browse_file_info("Select GIF File Name", |
| 2457 |
"GIF (*.gif)\0*.[Gg][Ii][Ff]\0All Files (*.*)\0*\0", |
| 2458 |
gifpath)); |
| 2459 |
|
| 2460 |
gtk_widget_show_all(GTK_WIDGET(dialog)); |
| 2461 |
} |
| 2462 |
|
| 2463 |
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(singularmatrix), core_settings.matrix_singularmatrix); |
| 2464 |
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matrixoutofrange), core_settings.matrix_outofrange); |
| 2465 |
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(autorepeat), core_settings.auto_repeat); |
| 2466 |
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(allowbigstack), core_settings.allow_big_stack); |
| 2467 |
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(localizedcopypaste), core_settings.localized_copy_paste); |
| 2468 |
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(printtotext), state.printerToTxtFile); |
| 2469 |
gtk_entry_set_text(GTK_ENTRY(textpath), state.printerTxtFileName); |
| 2470 |
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(printtogif), state.printerToGifFile); |
| 2471 |
gtk_entry_set_text(GTK_ENTRY(gifpath), state.printerGifFileName); |
| 2472 |
char maxlen[6]; |
| 2473 |
snprintf(maxlen, 6, "%d", state.printerGifMaxLength); |
| 2474 |
gtk_entry_set_text(GTK_ENTRY(gifheight), maxlen); |
| 2475 |
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(repaintwholedisplay), !state.old_repaint); |
| 2476 |
|
| 2477 |
gtk_window_set_role(GTK_WINDOW(dialog), "Free42 Dialog"); |
| 2478 |
if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { |
| 2479 |
core_settings.matrix_singularmatrix = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(singularmatrix)); |
| 2480 |
core_settings.matrix_outofrange = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(matrixoutofrange)); |
| 2481 |
core_settings.auto_repeat = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(autorepeat)); |
| 2482 |
bool oldBigStack = core_settings.allow_big_stack; |
| 2483 |
core_settings.allow_big_stack = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(allowbigstack)); |
| 2484 |
if (oldBigStack != core_settings.allow_big_stack) |
| 2485 |
core_update_allow_big_stack(); |
| 2486 |
core_settings.localized_copy_paste = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(localizedcopypaste)); |
| 2487 |
|
| 2488 |
state.printerToTxtFile = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(printtotext)); |
| 2489 |
char *old = strclone(state.printerTxtFileName); |
| 2490 |
const char *s = gtk_entry_get_text(GTK_ENTRY(textpath)); |
| 2491 |
strncpy(state.printerTxtFileName, s, FILENAMELEN); |
| 2492 |
state.printerTxtFileName[FILENAMELEN - 1] = 0; |
| 2493 |
appendSuffix(state.printerTxtFileName, ".txt"); |
| 2494 |
if (print_txt != NULL && (!state.printerToTxtFile || strcmp(state.printerTxtFileName, old) != 0)) { |
| 2495 |
fclose(print_txt); |
| 2496 |
print_txt = NULL; |
| 2497 |
} |
| 2498 |
free(old); |
| 2499 |
|
| 2500 |
state.printerToGifFile = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(printtogif)); |
| 2501 |
old = strclone(state.printerGifFileName); |
| 2502 |
s = gtk_entry_get_text(GTK_ENTRY(gifpath)); |
| 2503 |
strncpy(state.printerGifFileName, s, FILENAMELEN); |
| 2504 |
state.printerGifFileName[FILENAMELEN - 1] = 0; |
| 2505 |
appendSuffix(state.printerGifFileName, ".gif"); |
| 2506 |
if (print_gif != NULL && (!state.printerToGifFile || strcmp(state.printerGifFileName, old) != 0)) { |
| 2507 |
shell_finish_gif(gif_seeker, gif_writer); |
| 2508 |
fclose(print_gif); |
| 2509 |
print_gif = NULL; |
| 2510 |
gif_seq = -1; |
| 2511 |
} |
| 2512 |
free(old); |
| 2513 |
|
| 2514 |
s = gtk_entry_get_text(GTK_ENTRY(gifheight)); |
| 2515 |
if (sscanf(s, "%d", &state.printerGifMaxLength) == 1) { |
| 2516 |
if (state.printerGifMaxLength < 16) |
| 2517 |
state.printerGifMaxLength = 16; |
| 2518 |
else if (state.printerGifMaxLength > 32767) state.printerGifMaxLength = 32767; |
| 2519 |
} else |
| 2520 |
state.printerGifMaxLength = 256; |
| 2521 |
|
| 2522 |
state.old_repaint = !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(repaintwholedisplay)); |
| 2523 |
} |
| 2524 |
|
| 2525 |
gtk_widget_hide(GTK_WIDGET(dialog)); |
| 2526 |
} |
| 2527 |
|
| 2528 |
static void appendSuffix(char *path, const char *suffix) { |
| 2529 |
int len = strlen(path); |
| 2530 |
int slen = strlen(suffix); |
| 2531 |
if (len == 0 || len >= FILENAMELEN - slen) |
| 2532 |
return; |
| 2533 |
if (len >= slen && strcasecmp(path + len - slen, suffix) != 0) |
| 2534 |
strcat(path, suffix); |
| 2535 |
} |
| 2536 |
|
| 2537 |
static void copyCB() { |
| 2538 |
char *buf = core_copy(); |
| 2539 |
GtkClipboard *clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); |
| 2540 |
gtk_clipboard_set_text(clip, buf, -1); |
| 2541 |
clip = gtk_clipboard_get(GDK_SELECTION_PRIMARY); |
| 2542 |
gtk_clipboard_set_text(clip, buf, -1); |
| 2543 |
free(buf); |
| 2544 |
} |
| 2545 |
|
| 2546 |
static void paste2(GtkClipboard *clip, const gchar *text, gpointer cd) { |
| 2547 |
if (text != NULL) { |
| 2548 |
core_paste(text); |
| 2549 |
redisplay(); |
| 2550 |
// GTK will free the text once the callback returns. |
| 2551 |
} |
| 2552 |
} |
| 2553 |
|
| 2554 |
static void pasteCB() { |
| 2555 |
GtkClipboard *clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); |
| 2556 |
gtk_clipboard_request_text(clip, paste2, NULL); |
| 2557 |
} |
| 2558 |
|
| 2559 |
static bool focus_ok_button(GtkWindow *window, GtkWidget *container) { |
| 2560 |
GList *children = gtk_container_get_children(GTK_CONTAINER(container)); |
| 2561 |
if (children == NULL) |
| 2562 |
return false; |
| 2563 |
GList *child = children; |
| 2564 |
while (child != NULL) { |
| 2565 |
GtkWidget *w = (GtkWidget *) child->data; |
| 2566 |
if (GTK_IS_BUTTON(w) && !GTK_IS_LINK_BUTTON(w)) { |
| 2567 |
gtk_window_set_focus(window, w); |
| 2568 |
g_list_free(children); |
| 2569 |
return true; |
| 2570 |
} else if (GTK_IS_CONTAINER(w)) { |
| 2571 |
if (focus_ok_button(window, w)) { |
| 2572 |
g_list_free(children); |
| 2573 |
return true; |
| 2574 |
} |
| 2575 |
} |
| 2576 |
child = child->next; |
| 2577 |
} |
| 2578 |
g_list_free(children); |
| 2579 |
return false; |
| 2580 |
} |
| 2581 |
|
| 2582 |
static void documentationCB() { |
| 2583 |
#if GTK_MINOR_VERSION >= 22 |
| 2584 |
gtk_show_uri_on_window(GTK_WINDOW(mainwindow), "https://thomasokken.com/free42/#doc", GDK_CURRENT_TIME, NULL); |
| 2585 |
#else |
| 2586 |
gtk_show_uri(gtk_widget_get_screen(mainwindow), "https://thomasokken.com/free42/#doc", GDK_CURRENT_TIME, NULL); |
| 2587 |
#endif |
| 2588 |
} |
| 2589 |
|
| 2590 |
static void websiteCB() { |
| 2591 |
#if GTK_MINOR_VERSION >= 22 |
| 2592 |
gtk_show_uri_on_window(GTK_WINDOW(mainwindow), "https://thomasokken.com/free42/", GDK_CURRENT_TIME, NULL); |
| 2593 |
#else |
| 2594 |
gtk_show_uri(gtk_widget_get_screen(mainwindow), "https://thomasokken.com/free42/", GDK_CURRENT_TIME, NULL); |
| 2595 |
#endif |
| 2596 |
} |
| 2597 |
|
| 2598 |
static void otherWebsiteCB() { |
| 2599 |
#if GTK_MINOR_VERSION >= 22 |
| 2600 |
gtk_show_uri_on_window(GTK_WINDOW(mainwindow), "https://thomasokken.com/plus42/", GDK_CURRENT_TIME, NULL); |
| 2601 |
#else |
| 2602 |
gtk_show_uri(gtk_widget_get_screen(mainwindow), "https://thomasokken.com/plus42/", GDK_CURRENT_TIME, NULL); |
| 2603 |
#endif |
| 2604 |
} |
| 2605 |
|
| 2606 |
static void keyboardShortcutsCB() { |
| 2607 |
keyboardShortcutsShowing = !keyboardShortcutsShowing; |
| 2608 |
gtk_check_menu_item_set_active(keyboardShortcutsMenuItem, keyboardShortcutsShowing); |
| 2609 |
GdkWindow *win = gtk_widget_get_window(calc_widget); |
| 2610 |
gdk_window_invalidate_rect(win, NULL, FALSE); |
| 2611 |
} |
| 2612 |
|
| 2613 |
static void editKeymapCB() { |
| 2614 |
char cmd[FILENAMELEN + 12]; |
| 2615 |
snprintf(cmd, FILENAMELEN + 12, "xdg-open '%s'", keymapfilename); |
| 2616 |
system(cmd); |
| 2617 |
} |
| 2618 |
|
| 2619 |
static void aboutCB() { |
| 2620 |
static GtkWidget *about = NULL; |
| 2621 |
|
| 2622 |
if (about == NULL) { |
| 2623 |
about = gtk_dialog_new_with_buttons( |
| 2624 |
"About Free42", |
| 2625 |
GTK_WINDOW(mainwindow), |
| 2626 |
GTK_DIALOG_MODAL, |
| 2627 |
"_OK", |
| 2628 |
GTK_RESPONSE_ACCEPT, |
| 2629 |
NULL); |
| 2630 |
gtk_window_set_resizable(GTK_WINDOW(about), FALSE); |
| 2631 |
no_mwm_resize_borders(about); |
| 2632 |
GtkWidget *container = gtk_bin_get_child(GTK_BIN(about)); |
| 2633 |
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); |
| 2634 |
gtk_container_add(GTK_CONTAINER(container), box); |
| 2635 |
GtkWidget *image = gtk_image_new_from_pixbuf(icon_48); |
| 2636 |
gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 10); |
| 2637 |
GtkWidget *box2 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 2638 |
GtkWidget *version = gtk_label_new("Free42 " VERSION); |
| 2639 |
gtk_misc_set_alignment(GTK_MISC(version), 0, 0); |
| 2640 |
gtk_box_pack_start(GTK_BOX(box2), version, FALSE, FALSE, 10); |
| 2641 |
GtkWidget *author = gtk_label_new("\302\251 2004-2026 Thomas Okken"); |
| 2642 |
gtk_misc_set_alignment(GTK_MISC(author), 0, 0); |
| 2643 |
gtk_box_pack_start(GTK_BOX(box2), author, FALSE, FALSE, 0); |
| 2644 |
GtkWidget *websitelink = gtk_link_button_new("https://thomasokken.com/free42/"); |
| 2645 |
GtkWidget *websitebox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); |
| 2646 |
gtk_box_pack_start(GTK_BOX(websitebox), websitelink, FALSE, FALSE, 0); |
| 2647 |
gtk_box_pack_start(GTK_BOX(box2), websitebox, FALSE, FALSE, 0); |
| 2648 |
GtkWidget *forumlink = gtk_link_button_new("https://thomasokken.com/free42/#doc"); |
| 2649 |
GtkWidget *forumbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); |
| 2650 |
gtk_box_pack_start(GTK_BOX(forumbox), forumlink, FALSE, FALSE, 0); |
| 2651 |
gtk_box_pack_start(GTK_BOX(box2), forumbox, FALSE, FALSE, 0); |
| 2652 |
GtkWidget *plus42 = gtk_label_new("Plus42: Free42 Enhanced"); |
| 2653 |
gtk_misc_set_alignment(GTK_MISC(plus42), 0, 0); |
| 2654 |
gtk_box_pack_start(GTK_BOX(box2), plus42, FALSE, FALSE, 0); |
| 2655 |
GtkWidget *websiteplus42link = gtk_link_button_new("https://thomasokken.com/plus42/"); |
| 2656 |
GtkWidget *websiteplus42box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); |
| 2657 |
gtk_box_pack_start(GTK_BOX(websiteplus42box), websiteplus42link, FALSE, FALSE, 0); |
| 2658 |
gtk_box_pack_start(GTK_BOX(box2), websiteplus42box, FALSE, FALSE, 0); |
| 2659 |
gtk_box_pack_start(GTK_BOX(box), box2, FALSE, FALSE, 0); |
| 2660 |
focus_ok_button(GTK_WINDOW(about), container); |
| 2661 |
gtk_widget_show_all(GTK_WIDGET(about)); |
| 2662 |
} else { |
| 2663 |
GtkWidget *container = gtk_bin_get_child(GTK_BIN(about)); |
| 2664 |
focus_ok_button(GTK_WINDOW(about), container); |
| 2665 |
} |
| 2666 |
|
| 2667 |
gtk_window_set_role(GTK_WINDOW(about), "Free42 Dialog"); |
| 2668 |
gtk_dialog_run(GTK_DIALOG(about)); |
| 2669 |
gtk_widget_hide(GTK_WIDGET(about)); |
| 2670 |
} |
| 2671 |
|
| 2672 |
static gboolean delete_cb(GtkWidget *w, GdkEventAny *ev) { |
| 2673 |
quit(); |
| 2674 |
return TRUE; |
| 2675 |
} |
| 2676 |
|
| 2677 |
static gboolean delete_print_cb(GtkWidget *w, GdkEventAny *ev) { |
| 2678 |
gint x, y; |
| 2679 |
gtk_window_get_position(GTK_WINDOW(printwindow), &x, &y); |
| 2680 |
state.printWindowX = x; |
| 2681 |
state.printWindowY = y; |
| 2682 |
state.printWindowMapped = 0; |
| 2683 |
state.printWindowKnown = 1; |
| 2684 |
gtk_widget_hide(GTK_WIDGET(printwindow)); |
| 2685 |
return TRUE; |
| 2686 |
} |
| 2687 |
|
| 2688 |
static bool is_dark(GtkWidget *w) { |
| 2689 |
GtkStyle *style = gtk_widget_get_style(w); |
| 2690 |
GdkColor c = style->bg[GTK_STATE_NORMAL]; |
| 2691 |
return 0.299 * c.red + 0.587 * c.green + 0.114 * c.blue < 32768; |
| 2692 |
} |
| 2693 |
|
| 2694 |
static gboolean draw_cb(GtkWidget *w, cairo_t *cr, gpointer cd) { |
| 2695 |
cairo_save(cr); |
| 2696 |
int win_width, win_height, skin_width, skin_height; |
| 2697 |
skin_get_window_size(&win_width, &win_height); |
| 2698 |
skin_get_size(&skin_width, &skin_height); |
| 2699 |
cairo_scale(cr, ((double) win_width) / skin_width, ((double) win_height) / skin_height); |
| 2700 |
|
| 2701 |
allow_paint = true; |
| 2702 |
bool only_disp = need_to_paint_only_display(cr); |
| 2703 |
if (!only_disp) |
| 2704 |
skin_repaint(cr); |
| 2705 |
skin_repaint_display(cr); |
| 2706 |
if (!only_disp) { |
| 2707 |
if (ann_updown) |
| 2708 |
skin_repaint_annunciator(cr, 1); |
| 2709 |
if (ann_shift) |
| 2710 |
skin_repaint_annunciator(cr, 2); |
| 2711 |
if (ann_print) |
| 2712 |
skin_repaint_annunciator(cr, 3); |
| 2713 |
if (ann_run) |
| 2714 |
skin_repaint_annunciator(cr, 4); |
| 2715 |
if (ann_battery) |
| 2716 |
skin_repaint_annunciator(cr, 5); |
| 2717 |
if (ann_g) |
| 2718 |
skin_repaint_annunciator(cr, 6); |
| 2719 |
if (ann_rad) |
| 2720 |
skin_repaint_annunciator(cr, 7); |
| 2721 |
if (ckey != 0) |
| 2722 |
skin_repaint_key(cr, skey, 1); |
| 2723 |
} else { |
| 2724 |
if (skey >= -7 && skey <= -2) |
| 2725 |
skin_repaint_key(cr, skey, 1); |
| 2726 |
} |
| 2727 |
|
| 2728 |
if (keyboardShortcutsShowing) |
| 2729 |
skin_draw_keyboard_shortcuts(cr); |
| 2730 |
if (is_dark(w)) |
| 2731 |
skin_make_darker(cr); |
| 2732 |
|
| 2733 |
cairo_restore(cr); |
| 2734 |
return TRUE; |
| 2735 |
} |
| 2736 |
|
| 2737 |
static gboolean print_draw_cb(GtkWidget *w, cairo_t *cr, gpointer cd) { |
| 2738 |
repaint_printout(cr, is_dark(w)); |
| 2739 |
return TRUE; |
| 2740 |
} |
| 2741 |
|
| 2742 |
static gboolean print_key_cb(GtkWidget *w, GdkEventKey *event, gpointer cd) { |
| 2743 |
|
| 2744 |
// This is a bit hacky, but I want the Ctrl-<Key> |
| 2745 |
// shortcuts to work even when the Print-Out window |
| 2746 |
// is on top. |
| 2747 |
|
| 2748 |
if (event->type != GDK_KEY_PRESS) |
| 2749 |
return TRUE; |
| 2750 |
|
| 2751 |
bool ctrl = (event->state & GDK_CONTROL_MASK) != 0; |
| 2752 |
bool alt = (event->state & GDK_MOD1_MASK) != 0; |
| 2753 |
bool shift = (event->state & GDK_SHIFT_MASK) != 0; |
| 2754 |
if (!ctrl || alt || shift) |
| 2755 |
return TRUE; |
| 2756 |
|
| 2757 |
switch (event->keyval) { |
| 2758 |
case GDK_KEY_a: |
| 2759 |
paperAdvanceCB(); |
| 2760 |
break; |
| 2761 |
case GDK_KEY_t: |
| 2762 |
copyPrintAsTextCB(); |
| 2763 |
break; |
| 2764 |
case GDK_KEY_i: |
| 2765 |
copyPrintAsImageCB(); |
| 2766 |
break; |
| 2767 |
case GDK_KEY_d: |
| 2768 |
clearPrintOutCB(); |
| 2769 |
break; |
| 2770 |
case GDK_KEY_q: |
| 2771 |
quit(); |
| 2772 |
break; |
| 2773 |
case GDK_KEY_c: |
| 2774 |
copyCB(); |
| 2775 |
break; |
| 2776 |
case GDK_KEY_v: |
| 2777 |
pasteCB(); |
| 2778 |
break; |
| 2779 |
} |
| 2780 |
return TRUE; |
| 2781 |
} |
| 2782 |
|
| 2783 |
static void shell_keydown(bool cshift) { |
| 2784 |
GdkWindow *win = gtk_widget_get_window(calc_widget); |
| 2785 |
|
| 2786 |
int repeat; |
| 2787 |
bool keep_running; |
| 2788 |
if (skey == -1) |
| 2789 |
skey = skin_find_skey(ckey, cshift); |
| 2790 |
skin_invalidate_key(win, skey); |
| 2791 |
if (timeout3_id != 0 && (macro != NULL || ckey != 28 /* KEY_SHIFT */)) { |
| 2792 |
g_source_remove(timeout3_id); |
| 2793 |
timeout3_id = 0; |
| 2794 |
core_timeout3(false); |
| 2795 |
} |
| 2796 |
|
| 2797 |
if (macro != NULL) { |
| 2798 |
if (macro_type != 0) { |
| 2799 |
keep_running = core_keydown_command((const char *) macro, macro_type == 2, &enqueued, &repeat); |
| 2800 |
} else { |
| 2801 |
if (*macro == 0) { |
| 2802 |
squeak(); |
| 2803 |
return; |
| 2804 |
} |
| 2805 |
bool one_key_macro = macro[1] == 0 || (macro[2] == 0 && macro[0] == 28); |
| 2806 |
if (one_key_macro) { |
| 2807 |
while (*macro != 0) { |
| 2808 |
keep_running = core_keydown(*macro++, &enqueued, &repeat); |
| 2809 |
if (*macro != 0 && !enqueued) |
| 2810 |
core_keyup(); |
| 2811 |
} |
| 2812 |
} else { |
| 2813 |
bool waitForProgram = !program_running(); |
| 2814 |
while (*macro != 0) { |
| 2815 |
keep_running = core_keydown(*macro++, &enqueued, &repeat); |
| 2816 |
if (*macro != 0 && !enqueued) |
| 2817 |
keep_running = core_keyup(); |
| 2818 |
while (waitForProgram && keep_running) |
| 2819 |
keep_running = core_keydown(0, &enqueued, &repeat); |
| 2820 |
} |
| 2821 |
repeat = 0; |
| 2822 |
} |
| 2823 |
} |
| 2824 |
} else |
| 2825 |
keep_running = core_keydown(ckey, &enqueued, &repeat); |
| 2826 |
|
| 2827 |
if (quit_flag) |
| 2828 |
quit(); |
| 2829 |
if (keep_running) |
| 2830 |
enable_reminder(); |
| 2831 |
else { |
| 2832 |
disable_reminder(); |
| 2833 |
if (timeout_id != 0) |
| 2834 |
g_source_remove(timeout_id); |
| 2835 |
if (repeat != 0) |
| 2836 |
timeout_id = g_timeout_add(repeat == 1 ? 1000 : 500, repeater, NULL); |
| 2837 |
else if (!enqueued) |
| 2838 |
timeout_id = g_timeout_add(250, timeout1, NULL); |
| 2839 |
} |
| 2840 |
} |
| 2841 |
|
| 2842 |
static void shell_keyup() { |
| 2843 |
GdkWindow *win = gtk_widget_get_window(calc_widget); |
| 2844 |
skin_invalidate_key(win, skey); |
| 2845 |
|
| 2846 |
ckey = 0; |
| 2847 |
skey = -1; |
| 2848 |
if (timeout_id != 0) { |
| 2849 |
g_source_remove(timeout_id); |
| 2850 |
timeout_id = 0; |
| 2851 |
} |
| 2852 |
if (!enqueued) { |
| 2853 |
bool keep_running = core_keyup(); |
| 2854 |
if (quit_flag) |
| 2855 |
quit(); |
| 2856 |
if (keep_running) |
| 2857 |
enable_reminder(); |
| 2858 |
else |
| 2859 |
disable_reminder(); |
| 2860 |
} |
| 2861 |
} |
| 2862 |
|
| 2863 |
static gboolean button_cb(GtkWidget *w, GdkEventButton *event, gpointer cd) { |
| 2864 |
if (event->type == GDK_BUTTON_PRESS) { |
| 2865 |
if (ckey == 0) { |
| 2866 |
int win_width, win_height, skin_width, skin_height; |
| 2867 |
skin_get_window_size(&win_width, &win_height); |
| 2868 |
skin_get_size(&skin_width, &skin_height); |
| 2869 |
int x = (int) (event->x * skin_width / win_width); |
| 2870 |
int y = (int) (event->y * skin_height / win_height); |
| 2871 |
skin_find_key(x, y, ann_shift != 0, &skey, &ckey); |
| 2872 |
if (ckey != 0) { |
| 2873 |
macro = skin_find_macro(ckey, ¯o_type); |
| 2874 |
shell_keydown(ann_shift != 0); |
| 2875 |
mouse_key = true; |
| 2876 |
} |
| 2877 |
} |
| 2878 |
} else if (event->type == GDK_BUTTON_RELEASE) { |
| 2879 |
if (ckey != 0 && mouse_key) |
| 2880 |
shell_keyup(); |
| 2881 |
} |
| 2882 |
return TRUE; |
| 2883 |
} |
| 2884 |
|
| 2885 |
static gboolean key_cb(GtkWidget *w, GdkEventKey *event, gpointer cd) { |
| 2886 |
if (event->type == GDK_KEY_PRESS) { |
| 2887 |
if (event->hardware_keycode == active_keycode) |
| 2888 |
// Auto-repeat |
| 2889 |
return TRUE; |
| 2890 |
if (ckey == 0 || !mouse_key) { |
| 2891 |
int i; |
| 2892 |
|
| 2893 |
bool printable = event->length == 1 && event->string[0] >= 32 && event->string[0] <= 126; |
| 2894 |
just_pressed_shift = false; |
| 2895 |
|
| 2896 |
if (event->keyval == GDK_KEY_Shift_L || event->keyval == GDK_KEY_Shift_R) { |
| 2897 |
just_pressed_shift = true; |
| 2898 |
return TRUE; |
| 2899 |
} |
| 2900 |
bool ctrl = (event->state & GDK_CONTROL_MASK) != 0; |
| 2901 |
bool alt = (event->state & GDK_MOD1_MASK) != 0; |
| 2902 |
bool shift = (event->state & GDK_SHIFT_MASK) != 0; |
| 2903 |
bool cshift = ann_shift != 0; |
| 2904 |
|
| 2905 |
if (ckey != 0) { |
| 2906 |
shell_keyup(); |
| 2907 |
active_keycode = 0; |
| 2908 |
} |
| 2909 |
|
| 2910 |
bool exact; |
| 2911 |
unsigned char *key_macro = skin_keymap_lookup(event->keyval, |
| 2912 |
printable, ctrl, alt, shift, cshift, &exact); |
| 2913 |
if (key_macro == NULL || !exact) { |
| 2914 |
for (i = 0; i < keymap_length; i++) { |
| 2915 |
keymap_entry *entry = keymap + i; |
| 2916 |
if (ctrl == entry->ctrl |
| 2917 |
&& alt == entry->alt |
| 2918 |
&& (printable || shift == entry->shift) |
| 2919 |
&& event->keyval == entry->keyval) { |
| 2920 |
if (shift == entry->shift && cshift == entry->cshift) { |
| 2921 |
key_macro = entry->macro; |
| 2922 |
break; |
| 2923 |
} else { |
| 2924 |
if ((shift || !entry->shift) && (cshift || !entry->cshift) && key_macro == NULL) |
| 2925 |
key_macro = entry->macro; |
| 2926 |
} |
| 2927 |
} |
| 2928 |
} |
| 2929 |
} |
| 2930 |
|
| 2931 |
if (key_macro == NULL || (key_macro[0] != 36 || key_macro[1] != 0) |
| 2932 |
&& (key_macro[0] != 28 || key_macro[1] != 36 || key_macro[2] != 0)) { |
| 2933 |
// The test above is to make sure that whatever mapping is in |
| 2934 |
// effect for R/S will never be overridden by the special cases |
| 2935 |
// for the ALPHA and A..F menus. |
| 2936 |
if (!ctrl && !alt) { |
| 2937 |
char c = event->string[0]; |
| 2938 |
if (printable && core_alpha_menu()) { |
| 2939 |
if (c >= 'a' && c <= 'z') |
| 2940 |
c = c + 'A' - 'a'; |
| 2941 |
else if (c >= 'A' && c <= 'Z') |
| 2942 |
c = c + 'a' - 'A'; |
| 2943 |
ckey = 1024 + c; |
| 2944 |
skey = -1; |
| 2945 |
macro = NULL; |
| 2946 |
shell_keydown(false); |
| 2947 |
mouse_key = false; |
| 2948 |
active_keycode = event->hardware_keycode; |
| 2949 |
return TRUE; |
| 2950 |
} else if (core_hex_menu() && ((c >= 'a' && c <= 'f') |
| 2951 |
|| (c >= 'A' && c <= 'F'))) { |
| 2952 |
if (c >= 'a' && c <= 'f') |
| 2953 |
ckey = c - 'a' + 1; |
| 2954 |
else |
| 2955 |
ckey = c - 'A' + 1; |
| 2956 |
skey = -1; |
| 2957 |
macro = NULL; |
| 2958 |
shell_keydown(false); |
| 2959 |
mouse_key = false; |
| 2960 |
active_keycode = event->hardware_keycode; |
| 2961 |
return TRUE; |
| 2962 |
} else if (event->keyval == GDK_KEY_Left |
| 2963 |
|| event->keyval == GDK_KEY_Right |
| 2964 |
|| event->keyval == GDK_KEY_Delete) { |
| 2965 |
int which; |
| 2966 |
if (event->keyval == GDK_KEY_Left) |
| 2967 |
which = shift ? 2 : 1; |
| 2968 |
else if (event->keyval == GDK_KEY_Right) |
| 2969 |
which = shift ? 4 : 3; |
| 2970 |
else if (event->keyval == GDK_KEY_Delete) |
| 2971 |
which = 5; |
| 2972 |
else |
| 2973 |
which = 0; |
| 2974 |
if (which != 0) { |
| 2975 |
which = core_special_menu_key(which); |
| 2976 |
if (which != 0) { |
| 2977 |
ckey = which; |
| 2978 |
skey = -1; |
| 2979 |
macro = NULL; |
| 2980 |
shell_keydown(false); |
| 2981 |
mouse_key = false; |
| 2982 |
active_keycode = event->hardware_keycode; |
| 2983 |
return TRUE; |
| 2984 |
} |
| 2985 |
} |
| 2986 |
} |
| 2987 |
} |
| 2988 |
} |
| 2989 |
|
| 2990 |
if (key_macro != NULL) { |
| 2991 |
// A keymap entry is a sequence of zero or more calculator |
| 2992 |
// keystrokes (1..37) and/or macros (38..255). We expand |
| 2993 |
// macros here before invoking shell_keydown(). |
| 2994 |
// If the keymap entry is one key, or two keys with the |
| 2995 |
// first being 'shift', we highlight the key in question |
| 2996 |
// by setting ckey; otherwise, we set ckey to -10, which |
| 2997 |
// means no skin key will be highlighted. |
| 2998 |
ckey = -10; |
| 2999 |
skey = -1; |
| 3000 |
bool skin_shift = cshift; |
| 3001 |
if (key_macro[0] != 0) |
| 3002 |
if (key_macro[1] == 0) |
| 3003 |
ckey = key_macro[0]; |
| 3004 |
else if (key_macro[2] == 0 && key_macro[0] == 28) { |
| 3005 |
ckey = key_macro[1]; |
| 3006 |
skin_shift = true; |
| 3007 |
} |
| 3008 |
bool needs_expansion = false; |
| 3009 |
for (int j = 0; key_macro[j] != 0; j++) |
| 3010 |
if (key_macro[j] > 37) { |
| 3011 |
needs_expansion = true; |
| 3012 |
break; |
| 3013 |
} |
| 3014 |
if (needs_expansion) { |
| 3015 |
static unsigned char macrobuf[1024]; |
| 3016 |
int p = 0; |
| 3017 |
for (int j = 0; key_macro[j] != 0 && p < 1023; j++) { |
| 3018 |
int c = key_macro[j]; |
| 3019 |
if (c <= 37) |
| 3020 |
macrobuf[p++] = c; |
| 3021 |
else { |
| 3022 |
unsigned char *m = skin_find_macro(c, ¯o_type); |
| 3023 |
if (m != NULL) |
| 3024 |
while (*m != 0 && p < 1023) |
| 3025 |
macrobuf[p++] = *m++; |
| 3026 |
} |
| 3027 |
} |
| 3028 |
macrobuf[p] = 0; |
| 3029 |
macro = macrobuf; |
| 3030 |
} else { |
| 3031 |
macro = key_macro; |
| 3032 |
macro_type = 0; |
| 3033 |
} |
| 3034 |
shell_keydown(skin_shift); |
| 3035 |
mouse_key = false; |
| 3036 |
active_keycode = event->hardware_keycode; |
| 3037 |
} |
| 3038 |
} |
| 3039 |
} else if (event->type == GDK_KEY_RELEASE) { |
| 3040 |
if (ckey == 0) { |
| 3041 |
if (just_pressed_shift && (event->keyval == GDK_KEY_Shift_L |
| 3042 |
|| event->keyval == GDK_KEY_Shift_R)) { |
| 3043 |
ckey = 28; |
| 3044 |
skey = -1; |
| 3045 |
macro = NULL; |
| 3046 |
shell_keydown(false); |
| 3047 |
shell_keyup(); |
| 3048 |
} |
| 3049 |
} else { |
| 3050 |
if (!mouse_key && event->hardware_keycode == active_keycode) { |
| 3051 |
shell_keyup(); |
| 3052 |
active_keycode = 0; |
| 3053 |
} |
| 3054 |
} |
| 3055 |
} |
| 3056 |
return TRUE; |
| 3057 |
} |
| 3058 |
|
| 3059 |
static void enable_reminder() { |
| 3060 |
if (reminder_id == 0) |
| 3061 |
reminder_id = g_idle_add(reminder, NULL); |
| 3062 |
if (timeout_id != 0) { |
| 3063 |
g_source_remove(timeout_id); |
| 3064 |
timeout_id = 0; |
| 3065 |
} |
| 3066 |
} |
| 3067 |
|
| 3068 |
static void disable_reminder() { |
| 3069 |
if (reminder_id != 0) { |
| 3070 |
g_source_remove(reminder_id); |
| 3071 |
reminder_id = 0; |
| 3072 |
} |
| 3073 |
} |
| 3074 |
|
| 3075 |
static gboolean repeater(gpointer cd) { |
| 3076 |
int repeat = core_repeat(); |
| 3077 |
if (repeat != 0) |
| 3078 |
timeout_id = g_timeout_add(repeat == 1 ? 200 : 100, repeater, NULL); |
| 3079 |
else |
| 3080 |
timeout_id = g_timeout_add(250, timeout1, NULL); |
| 3081 |
return FALSE; |
| 3082 |
} |
| 3083 |
|
| 3084 |
static gboolean timeout1(gpointer cd) { |
| 3085 |
if (ckey != 0) { |
| 3086 |
core_keytimeout1(); |
| 3087 |
timeout_id = g_timeout_add(1750, timeout2, NULL); |
| 3088 |
} else |
| 3089 |
timeout_id = 0; |
| 3090 |
return FALSE; |
| 3091 |
} |
| 3092 |
|
| 3093 |
static gboolean timeout2(gpointer cd) { |
| 3094 |
if (ckey != 0) |
| 3095 |
core_keytimeout2(); |
| 3096 |
timeout_id = 0; |
| 3097 |
return FALSE; |
| 3098 |
} |
| 3099 |
|
| 3100 |
static gboolean timeout3(gpointer cd) { |
| 3101 |
bool keep_running = core_timeout3(true); |
| 3102 |
timeout3_id = 0; |
| 3103 |
if (keep_running) |
| 3104 |
enable_reminder(); |
| 3105 |
return FALSE; |
| 3106 |
} |
| 3107 |
|
| 3108 |
static gboolean battery_checker(gpointer cd) { |
| 3109 |
shell_low_battery(); |
| 3110 |
return TRUE; |
| 3111 |
} |
| 3112 |
|
| 3113 |
static void repaint_printout(cairo_t *cr, bool dark) { |
| 3114 |
GdkRectangle clip; |
| 3115 |
if (!gdk_cairo_get_clip_rectangle(cr, &clip)) |
| 3116 |
gtk_widget_get_allocation(print_widget, &clip); |
| 3117 |
|
| 3118 |
GdkPixbuf *buf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, |
| 3119 |
8, clip.width, clip.height); |
| 3120 |
int d_bpl = gdk_pixbuf_get_rowstride(buf); |
| 3121 |
guchar *d1 = gdk_pixbuf_get_pixels(buf); |
| 3122 |
int length = printout_bottom - printout_top; |
| 3123 |
if (length < 0) |
| 3124 |
length += PRINT_LINES; |
| 3125 |
|
| 3126 |
for (int v = clip.y; v < clip.y + clip.height; v++) { |
| 3127 |
int v2 = printout_top + v; |
| 3128 |
if (v2 >= PRINT_LINES) |
| 3129 |
v2 -= PRINT_LINES; |
| 3130 |
int v3 = v2 * 36; |
| 3131 |
guchar *dst = d1; |
| 3132 |
for (int h = clip.x; h < clip.x + clip.width; h++) { |
| 3133 |
unsigned char c; |
| 3134 |
if (v >= length) |
| 3135 |
c = dark ? 64 : 192; |
| 3136 |
else if (h < 36 || h >= 322) |
| 3137 |
c = dark ? 18 : 255; |
| 3138 |
else if ((print_bitmap[v3 + ((h - 36) >> 3)] & (1 << ((h - 36) & 7))) == 0) |
| 3139 |
c = dark ? 18 : 255; |
| 3140 |
else |
| 3141 |
c = dark ? 219 : 0; |
| 3142 |
*dst++ = c; |
| 3143 |
*dst++ = c; |
| 3144 |
*dst++ = c; |
| 3145 |
} |
| 3146 |
d1 += d_bpl; |
| 3147 |
} |
| 3148 |
|
| 3149 |
gdk_cairo_set_source_pixbuf(cr, buf, clip.x, clip.y); |
| 3150 |
cairo_paint(cr); |
| 3151 |
g_object_unref(G_OBJECT(buf)); |
| 3152 |
} |
| 3153 |
|
| 3154 |
static gboolean reminder(gpointer cd) { |
| 3155 |
bool dummy1; |
| 3156 |
int dummy2; |
| 3157 |
bool keep_running = core_keydown(0, &dummy1, &dummy2); |
| 3158 |
if (quit_flag) |
| 3159 |
quit(); |
| 3160 |
if (keep_running) |
| 3161 |
return TRUE; |
| 3162 |
else { |
| 3163 |
reminder_id = 0; |
| 3164 |
return FALSE; |
| 3165 |
} |
| 3166 |
} |
| 3167 |
|
| 3168 |
/* Callbacks used by shell_print() and shell_spool_txt() / shell_spool_gif() */ |
| 3169 |
|
| 3170 |
static void txt_writer(const char *text, int length) { |
| 3171 |
int n; |
| 3172 |
if (print_txt == NULL) |
| 3173 |
return; |
| 3174 |
n = fwrite(text, 1, length, print_txt); |
| 3175 |
if (n != length) { |
| 3176 |
char buf[1000]; |
| 3177 |
state.printerToTxtFile = 0; |
| 3178 |
fclose(print_txt); |
| 3179 |
print_txt = NULL; |
| 3180 |
snprintf(buf, 1000, "Error while writing to \"%s\".\nPrinting to text file disabled", state.printerTxtFileName); |
| 3181 |
show_message("Message", buf); |
| 3182 |
} |
| 3183 |
} |
| 3184 |
|
| 3185 |
static void txt_newliner() { |
| 3186 |
if (print_txt == NULL) |
| 3187 |
return; |
| 3188 |
fputc('\r', print_txt); |
| 3189 |
fputc('\n', print_txt); |
| 3190 |
fflush(print_txt); |
| 3191 |
} |
| 3192 |
|
| 3193 |
static void gif_seeker(int4 pos) { |
| 3194 |
if (print_gif == NULL) |
| 3195 |
return; |
| 3196 |
if (fseek(print_gif, pos, SEEK_SET) == -1) { |
| 3197 |
char buf[1000]; |
| 3198 |
state.printerToGifFile = 0; |
| 3199 |
fclose(print_gif); |
| 3200 |
print_gif = NULL; |
| 3201 |
snprintf(buf, 1000, "Error while seeking \"%s\".\nPrinting to GIF file disabled", print_gif_name); |
| 3202 |
show_message("Message", buf); |
| 3203 |
} |
| 3204 |
} |
| 3205 |
|
| 3206 |
static void gif_writer(const char *text, int length) { |
| 3207 |
int n; |
| 3208 |
if (print_gif == NULL) |
| 3209 |
return; |
| 3210 |
n = fwrite(text, 1, length, print_gif); |
| 3211 |
if (n != length) { |
| 3212 |
char buf[1000]; |
| 3213 |
state.printerToGifFile = 0; |
| 3214 |
fclose(print_gif); |
| 3215 |
print_gif = NULL; |
| 3216 |
snprintf(buf, 1000, "Error while writing to \"%s\".\nPrinting to GIF file disabled", print_gif_name); |
| 3217 |
show_message("Message", buf); |
| 3218 |
} |
| 3219 |
} |
| 3220 |
|
| 3221 |
void shell_blitter(const char *bits, int bytesperline, int x, int y, |
| 3222 |
int width, int height) { |
| 3223 |
if (state.old_repaint) { |
| 3224 |
GdkWindow *win = gtk_widget_get_window(calc_widget); |
| 3225 |
|
| 3226 |
skin_display_invalidater(win, bits, bytesperline, x, y, width, height); |
| 3227 |
if (skey >= -7 && skey <= -2) |
| 3228 |
skin_invalidate_key(win, skey); |
| 3229 |
} else { |
| 3230 |
skin_display_invalidater(NULL, bits, bytesperline, x, y, width, height); |
| 3231 |
} |
| 3232 |
} |
| 3233 |
|
| 3234 |
void shell_beeper(int tone) { |
| 3235 |
#ifdef AUDIO_ALSA |
| 3236 |
const char *display_name = gdk_display_get_name(gdk_display_get_default()); |
| 3237 |
if (display_name == NULL || display_name[0] == ':' || strchr(display_name, ':') == NULL) { |
| 3238 |
const int tone_freqs[] = { 165, 220, 247, 277, 294, 330, 370, 415, 440, 554, 1865 }; |
| 3239 |
int frequency = tone_freqs[tone]; |
| 3240 |
int duration = tone == 10 ? 125 : 250; |
| 3241 |
if (!alsa_beeper(frequency, duration)) |
| 3242 |
gdk_display_beep(gdk_display_get_default()); |
| 3243 |
} else |
| 3244 |
gdk_display_beep(gdk_display_get_default()); |
| 3245 |
#else |
| 3246 |
gdk_display_beep(gdk_display_get_default()); |
| 3247 |
#endif |
| 3248 |
} |
| 3249 |
|
| 3250 |
static gboolean ann_print_timeout(gpointer cd) { |
| 3251 |
GdkWindow *win = gtk_widget_get_window(calc_widget); |
| 3252 |
|
| 3253 |
ann_print_timeout_id = 0; |
| 3254 |
ann_print = 0; |
| 3255 |
skin_invalidate_annunciator(win, 3); |
| 3256 |
|
| 3257 |
return FALSE; |
| 3258 |
} |
| 3259 |
|
| 3260 |
const char *shell_platform() { |
| 3261 |
return VERSION " " VERSION_PLATFORM; |
| 3262 |
} |
| 3263 |
|
| 3264 |
void shell_annunciators(int updn, int shf, int prt, int run, int g, int rad) { |
| 3265 |
GdkWindow *win = gtk_widget_get_window(calc_widget); |
| 3266 |
|
| 3267 |
if (updn != -1 && ann_updown != updn) { |
| 3268 |
ann_updown = updn; |
| 3269 |
skin_invalidate_annunciator(win, 1); |
| 3270 |
} |
| 3271 |
if (shf != -1 && ann_shift != shf) { |
| 3272 |
ann_shift = shf; |
| 3273 |
skin_invalidate_annunciator(win, 2); |
| 3274 |
} |
| 3275 |
if (prt != -1) { |
| 3276 |
if (ann_print_timeout_id != 0) { |
| 3277 |
g_source_remove(ann_print_timeout_id); |
| 3278 |
ann_print_timeout_id = 0; |
| 3279 |
} |
| 3280 |
if (ann_print != prt) |
| 3281 |
if (prt) { |
| 3282 |
ann_print = 1; |
| 3283 |
skin_invalidate_annunciator(win, 3); |
| 3284 |
} else { |
| 3285 |
ann_print_timeout_id = g_timeout_add(1000, ann_print_timeout, NULL); |
| 3286 |
} |
| 3287 |
} |
| 3288 |
if (run != -1 && ann_run != run) { |
| 3289 |
ann_run = run; |
| 3290 |
skin_invalidate_annunciator(win, 4); |
| 3291 |
} |
| 3292 |
if (g != -1 && ann_g != g) { |
| 3293 |
ann_g = g; |
| 3294 |
skin_invalidate_annunciator(win, 6); |
| 3295 |
} |
| 3296 |
if (rad != -1 && ann_rad != rad) { |
| 3297 |
ann_rad = rad; |
| 3298 |
skin_invalidate_annunciator(win, 7); |
| 3299 |
} |
| 3300 |
} |
| 3301 |
|
| 3302 |
bool shell_wants_cpu() { |
| 3303 |
static uint4 lastCount = 0; |
| 3304 |
struct timeval tv; |
| 3305 |
gettimeofday(&tv, NULL); |
| 3306 |
uint4 count = tv.tv_sec * 1000L + tv.tv_usec / 1000; |
| 3307 |
if (count - lastCount < 10) |
| 3308 |
return false; |
| 3309 |
lastCount = count; |
| 3310 |
return g_main_context_pending(NULL); |
| 3311 |
} |
| 3312 |
|
| 3313 |
void shell_delay(int duration) { |
| 3314 |
gdk_display_flush(gdk_display_get_default()); |
| 3315 |
g_usleep(duration * 1000); |
| 3316 |
} |
| 3317 |
|
| 3318 |
void shell_request_timeout3(int delay) { |
| 3319 |
if (timeout3_id != 0) |
| 3320 |
g_source_remove(timeout3_id); |
| 3321 |
timeout3_id = g_timeout_add(delay, timeout3, NULL); |
| 3322 |
} |
| 3323 |
|
| 3324 |
uint8 shell_get_mem() { |
| 3325 |
FILE *meminfo = fopen("/proc/meminfo", "r"); |
| 3326 |
char line[1024]; |
| 3327 |
uint8 bytes = 0; |
| 3328 |
if (meminfo == NULL) |
| 3329 |
return 0; |
| 3330 |
while (fgets(line, 1024, meminfo) != NULL) { |
| 3331 |
if (strncmp(line, "MemFree:", 8) == 0) { |
| 3332 |
uint8 kbytes; |
| 3333 |
if (sscanf(line + 8, "%llu", &kbytes) == 1) |
| 3334 |
bytes = 1024 * kbytes; |
| 3335 |
break; |
| 3336 |
} |
| 3337 |
} |
| 3338 |
fclose(meminfo); |
| 3339 |
return bytes; |
| 3340 |
} |
| 3341 |
|
| 3342 |
bool shell_low_battery() { |
| 3343 |
|
| 3344 |
int lowbat = 0; |
| 3345 |
FILE *apm = fopen("/proc/apm", "r"); |
| 3346 |
if (apm != NULL) { |
| 3347 |
/* /proc/apm partial legend: |
| 3348 |
* |
| 3349 |
* 1.16 1.2 0x03 0x01 0x03 0x09 9% -1 ? |
| 3350 |
* ^^^^ ^^^^ |
| 3351 |
* | +-- Battery status (0 = full, 1 = low, |
| 3352 |
* | 2 = critical, 3 = charging) |
| 3353 |
* +------- AC status (0 = offline, 1 = online) |
| 3354 |
*/ |
| 3355 |
char line[1024]; |
| 3356 |
int ac_stat, bat_stat; |
| 3357 |
if (fgets(line, 1024, apm) == NULL) |
| 3358 |
goto done1; |
| 3359 |
if (sscanf(line, "%*s %*s %*s %x %x", &ac_stat, &bat_stat) == 2) |
| 3360 |
lowbat = ac_stat != 1 && (bat_stat == 1 || bat_stat == 2); |
| 3361 |
done1: |
| 3362 |
fclose(apm); |
| 3363 |
} else { |
| 3364 |
/* Battery considered low if |
| 3365 |
* |
| 3366 |
* /sys/class/power_supply/BATn/status == "Discharging" |
| 3367 |
* and |
| 3368 |
* /sys/class/power_supply/BATn/capacity <= 10 |
| 3369 |
* |
| 3370 |
* Assuming status will always be "Discharging" when the system is |
| 3371 |
* actually running on battery (it could also be "Full", but then it is |
| 3372 |
* definitely not low!), and that capacity is a number between 0 and |
| 3373 |
* 100. The choice of 10% or less as being "low" is completely |
| 3374 |
* arbitrary. |
| 3375 |
* Checking BATn where n = 0, 1, or 2. Some docs suggest BAT0 should |
| 3376 |
* exist, others suggest 1 should exist; I'm playing safe and trying |
| 3377 |
* both, and throwing in BAT2 just for the fun of it. |
| 3378 |
*/ |
| 3379 |
char status_filename[50]; |
| 3380 |
char capacity_filename[50]; |
| 3381 |
char line[50]; |
| 3382 |
for (int n = 0; n <= 2; n++) { |
| 3383 |
snprintf(status_filename, 50, "/sys/class/power_supply/BAT%d/status", n); |
| 3384 |
FILE *status_file = fopen(status_filename, "r"); |
| 3385 |
if (status_file == NULL) |
| 3386 |
continue; |
| 3387 |
snprintf(capacity_filename, 50, "/sys/class/power_supply/BAT%d/capacity", n); |
| 3388 |
FILE *capacity_file = fopen(capacity_filename, "r"); |
| 3389 |
if (capacity_file == NULL) { |
| 3390 |
fclose(status_file); |
| 3391 |
continue; |
| 3392 |
} |
| 3393 |
bool discharging = fgets(line, 50, status_file) != NULL && strncasecmp(line, "discharging", 11) == 0; |
| 3394 |
int capacity; |
| 3395 |
if (fscanf(capacity_file, "%d", &capacity) != 1) |
| 3396 |
capacity = 100; |
| 3397 |
fclose(status_file); |
| 3398 |
fclose(capacity_file); |
| 3399 |
lowbat = discharging && capacity <= 10; |
| 3400 |
break; |
| 3401 |
} |
| 3402 |
} |
| 3403 |
if (lowbat != ann_battery) { |
| 3404 |
ann_battery = lowbat; |
| 3405 |
if (allow_paint) { |
| 3406 |
GdkWindow *win = gtk_widget_get_window(calc_widget); |
| 3407 |
skin_invalidate_annunciator(win, 5); |
| 3408 |
} |
| 3409 |
} |
| 3410 |
return lowbat != 0; |
| 3411 |
} |
| 3412 |
|
| 3413 |
void shell_powerdown() { |
| 3414 |
/* We defer the actual shutdown so the emulator core can |
| 3415 |
* return from core_keyup() or core_keydown() and isn't |
| 3416 |
* asked to save its state while still in the middle of |
| 3417 |
* executing the OFF instruction... |
| 3418 |
*/ |
| 3419 |
quit_flag = true; |
| 3420 |
} |
| 3421 |
|
| 3422 |
void shell_message(const char *message) { |
| 3423 |
show_message("Core", message); |
| 3424 |
} |
| 3425 |
|
| 3426 |
int8 shell_random_seed() { |
| 3427 |
struct timeval tv; |
| 3428 |
gettimeofday(&tv, NULL); |
| 3429 |
return tv.tv_sec * 1000LL + tv.tv_usec / 1000; |
| 3430 |
} |
| 3431 |
|
| 3432 |
uint4 shell_milliseconds() { |
| 3433 |
struct timeval tv; |
| 3434 |
gettimeofday(&tv, NULL); |
| 3435 |
return (uint4) (tv.tv_sec * 1000L + tv.tv_usec / 1000); |
| 3436 |
} |
| 3437 |
|
| 3438 |
const char *shell_number_format() { |
| 3439 |
return cached_number_format; |
| 3440 |
} |
| 3441 |
|
| 3442 |
int shell_date_format() { |
| 3443 |
struct tm t; |
| 3444 |
t.tm_sec = 0; |
| 3445 |
t.tm_min = 0; |
| 3446 |
t.tm_hour = 0; |
| 3447 |
t.tm_mday = 22; // 22 |
| 3448 |
t.tm_mon = 10; // 11 |
| 3449 |
t.tm_year = 1433; // 3333 |
| 3450 |
t.tm_wday = 0; |
| 3451 |
t.tm_yday = 0; |
| 3452 |
t.tm_isdst = 0; |
| 3453 |
|
| 3454 |
char buf[32]; |
| 3455 |
strftime(buf, 32, "%x", &t); |
| 3456 |
|
| 3457 |
int y = strstr(buf, "3") - buf; |
| 3458 |
int m = strstr(buf, "1") - buf; |
| 3459 |
int d = strstr(buf, "2") - buf; |
| 3460 |
|
| 3461 |
if (d < m && m < y) |
| 3462 |
return 1; |
| 3463 |
else if (y < m && m < d) |
| 3464 |
return 2; |
| 3465 |
else |
| 3466 |
return 0; |
| 3467 |
} |
| 3468 |
|
| 3469 |
bool shell_clk24() { |
| 3470 |
struct tm t; |
| 3471 |
t.tm_sec = 0; |
| 3472 |
t.tm_min = 0; |
| 3473 |
t.tm_hour = 0; |
| 3474 |
t.tm_mday = 22; // 22 |
| 3475 |
t.tm_mon = 10; // 11 |
| 3476 |
t.tm_year = 1433; // 3333 |
| 3477 |
t.tm_wday = 0; |
| 3478 |
t.tm_yday = 0; |
| 3479 |
t.tm_isdst = 0; |
| 3480 |
|
| 3481 |
char buf[32]; |
| 3482 |
strftime(buf, 32, "%X", &t); |
| 3483 |
|
| 3484 |
return strstr(buf, "A") == NULL; |
| 3485 |
} |
| 3486 |
|
| 3487 |
struct print_growth_info { |
| 3488 |
int y, height; |
| 3489 |
print_growth_info(int yy, int hheight) : y(yy), height(hheight) {} |
| 3490 |
}; |
| 3491 |
|
| 3492 |
static gboolean print_widget_grew(GtkWidget *w, GdkEventConfigure *event, |
| 3493 |
gpointer cd) { |
| 3494 |
print_growth_info *info = (print_growth_info *) cd; |
| 3495 |
scroll_printout_to_bottom(); |
| 3496 |
GdkRectangle clip; |
| 3497 |
clip.x = 0; |
| 3498 |
clip.y = info->y; |
| 3499 |
clip.width = 358; |
| 3500 |
clip.height = info->height; |
| 3501 |
GdkWindow *win = gtk_widget_get_window(print_widget); |
| 3502 |
gdk_window_invalidate_rect(win, &clip, FALSE); |
| 3503 |
g_signal_handlers_disconnect_by_func(G_OBJECT(w), (gpointer) print_widget_grew, cd); |
| 3504 |
delete info; |
| 3505 |
return FALSE; |
| 3506 |
} |
| 3507 |
|
| 3508 |
void shell_print(const char *text, int length, |
| 3509 |
const char *bits, int bytesperline, |
| 3510 |
int x, int y, int width, int height) { |
| 3511 |
int xx, yy; |
| 3512 |
int oldlength, newlength; |
| 3513 |
|
| 3514 |
for (yy = 0; yy < height; yy++) { |
| 3515 |
int4 Y = (printout_bottom + 2 * yy) % PRINT_LINES; |
| 3516 |
for (xx = 0; xx < 143; xx++) { |
| 3517 |
int bit, px, py; |
| 3518 |
if (xx < width) { |
| 3519 |
char c = bits[(y + yy) * bytesperline + ((x + xx) >> 3)]; |
| 3520 |
bit = (c & (1 << ((x + xx) & 7))) != 0; |
| 3521 |
} else |
| 3522 |
bit = 0; |
| 3523 |
for (px = xx * 2; px < (xx + 1) * 2; px++) |
| 3524 |
for (py = Y; py < Y + 2; py++) |
| 3525 |
if (bit) |
| 3526 |
print_bitmap[py * PRINT_BYTESPERLINE + (px >> 3)] |
| 3527 |
|= 1 << (px & 7); |
| 3528 |
else |
| 3529 |
print_bitmap[py * PRINT_BYTESPERLINE + (px >> 3)] |
| 3530 |
&= ~(1 << (px & 7)); |
| 3531 |
} |
| 3532 |
} |
| 3533 |
|
| 3534 |
oldlength = printout_bottom - printout_top; |
| 3535 |
if (oldlength < 0) |
| 3536 |
oldlength += PRINT_LINES; |
| 3537 |
printout_bottom = (printout_bottom + 2 * height) % PRINT_LINES; |
| 3538 |
newlength = oldlength + 2 * height; |
| 3539 |
|
| 3540 |
if (newlength >= PRINT_LINES) { |
| 3541 |
printout_top = (printout_bottom + 2) % PRINT_LINES; |
| 3542 |
newlength = PRINT_LINES - 2; |
| 3543 |
if (newlength != oldlength) |
| 3544 |
gtk_widget_set_size_request(print_widget, 358, newlength); |
| 3545 |
scroll_printout_to_bottom(); |
| 3546 |
gtk_widget_queue_draw(print_widget); |
| 3547 |
} else { |
| 3548 |
gtk_widget_set_size_request(print_widget, 358, newlength); |
| 3549 |
// The resize request does not take effect immediately; |
| 3550 |
// if I call scroll_printout_to_bottom() now, the scrolling will take |
| 3551 |
// place *before* the resizing, leaving the scroll bar in the wrong |
| 3552 |
// position. |
| 3553 |
// I work around this by using a callback to finish the job. |
| 3554 |
g_signal_connect(G_OBJECT(print_widget), "configure-event", |
| 3555 |
G_CALLBACK(print_widget_grew), |
| 3556 |
(gpointer) new print_growth_info(oldlength, 2 * height)); |
| 3557 |
} |
| 3558 |
|
| 3559 |
if (state.printerToTxtFile) { |
| 3560 |
int err; |
| 3561 |
char buf[1000]; |
| 3562 |
|
| 3563 |
if (print_txt == NULL) { |
| 3564 |
print_txt = fopen(state.printerTxtFileName, "a"); |
| 3565 |
if (print_txt == NULL) { |
| 3566 |
err = errno; |
| 3567 |
state.printerToTxtFile = 0; |
| 3568 |
snprintf(buf, 1000, "Can't open \"%s\" for output:\n%s (%d)\nPrinting to text file disabled.", state.printerTxtFileName, strerror(err), err); |
| 3569 |
show_message("Message", buf); |
| 3570 |
goto done_print_txt; |
| 3571 |
} |
| 3572 |
} |
| 3573 |
|
| 3574 |
if (text != NULL) |
| 3575 |
shell_spool_txt(text, length, txt_writer, txt_newliner); |
| 3576 |
else |
| 3577 |
shell_spool_bitmap_to_txt(bits, bytesperline, x, y, width, height, txt_writer, txt_newliner); |
| 3578 |
done_print_txt:; |
| 3579 |
} |
| 3580 |
|
| 3581 |
if (state.printerToGifFile) { |
| 3582 |
int err; |
| 3583 |
char buf[1000]; |
| 3584 |
|
| 3585 |
if (print_gif != NULL |
| 3586 |
&& gif_lines + height > state.printerGifMaxLength) { |
| 3587 |
shell_finish_gif(gif_seeker, gif_writer); |
| 3588 |
fclose(print_gif); |
| 3589 |
print_gif = NULL; |
| 3590 |
} |
| 3591 |
|
| 3592 |
if (print_gif == NULL) { |
| 3593 |
while (1) { |
| 3594 |
int len, p; |
| 3595 |
|
| 3596 |
gif_seq = (gif_seq + 1) % 10000; |
| 3597 |
|
| 3598 |
strcpy(print_gif_name, state.printerGifFileName); |
| 3599 |
len = strlen(print_gif_name); |
| 3600 |
|
| 3601 |
/* Strip ".gif" extension, if present */ |
| 3602 |
if (len >= 4 && |
| 3603 |
strcasecmp(print_gif_name + len - 4, ".gif") == 0) { |
| 3604 |
len -= 4; |
| 3605 |
print_gif_name[len] = 0; |
| 3606 |
} |
| 3607 |
|
| 3608 |
/* Strip ".[0-9]+", if present */ |
| 3609 |
p = len; |
| 3610 |
while (p > 0 && print_gif_name[p] >= '0' |
| 3611 |
&& print_gif_name[p] <= '9') |
| 3612 |
p--; |
| 3613 |
if (p < len && p >= 0 && print_gif_name[p] == '.') |
| 3614 |
print_gif_name[p] = 0; |
| 3615 |
|
| 3616 |
/* Make sure we have enough space for the ".nnnn.gif" */ |
| 3617 |
p = FILENAMELEN - 10; |
| 3618 |
print_gif_name[p] = 0; |
| 3619 |
p = strlen(print_gif_name); |
| 3620 |
snprintf(print_gif_name + p, 6, ".%04d", gif_seq); |
| 3621 |
strcat(print_gif_name, ".gif"); |
| 3622 |
|
| 3623 |
if (!file_exists(print_gif_name)) |
| 3624 |
break; |
| 3625 |
} |
| 3626 |
print_gif = fopen(print_gif_name, "w+"); |
| 3627 |
if (print_gif == NULL) { |
| 3628 |
err = errno; |
| 3629 |
state.printerToGifFile = 0; |
| 3630 |
snprintf(buf, 1000, "Can't open \"%s\" for output:\n%s (%d)\nPrinting to GIF file disabled.", print_gif_name, strerror(err), err); |
| 3631 |
show_message("Message", buf); |
| 3632 |
goto done_print_gif; |
| 3633 |
} |
| 3634 |
if (!shell_start_gif(gif_writer, 143, state.printerGifMaxLength)) { |
| 3635 |
state.printerToGifFile = 0; |
| 3636 |
show_message("Message", "Not enough memory for the GIF encoder.\nPrinting to GIF file disabled."); |
| 3637 |
goto done_print_gif; |
| 3638 |
} |
| 3639 |
gif_lines = 0; |
| 3640 |
} |
| 3641 |
|
| 3642 |
shell_spool_gif(bits, bytesperline, x, y, width, height, gif_writer); |
| 3643 |
gif_lines += height; |
| 3644 |
|
| 3645 |
if (print_gif != NULL && gif_lines + 9 > state.printerGifMaxLength) { |
| 3646 |
shell_finish_gif(gif_seeker, gif_writer); |
| 3647 |
fclose(print_gif); |
| 3648 |
print_gif = NULL; |
| 3649 |
} |
| 3650 |
done_print_gif:; |
| 3651 |
} |
| 3652 |
|
| 3653 |
print_text[print_text_bottom++] = (char) (text == NULL ? 255 : length); |
| 3654 |
if (print_text_bottom == PRINT_TEXT_SIZE) |
| 3655 |
print_text_bottom = 0; |
| 3656 |
if (text != NULL) { |
| 3657 |
if (print_text_bottom + length < PRINT_TEXT_SIZE) { |
| 3658 |
memcpy(print_text + print_text_bottom, text, length); |
| 3659 |
print_text_bottom += length; |
| 3660 |
} else { |
| 3661 |
int part = PRINT_TEXT_SIZE - print_text_bottom; |
| 3662 |
memcpy(print_text + print_text_bottom, text, part); |
| 3663 |
memcpy(print_text, text + part, length - part); |
| 3664 |
print_text_bottom = length - part; |
| 3665 |
} |
| 3666 |
} |
| 3667 |
print_text_pixel_height += text == NULL ? 16 : 9; |
| 3668 |
while (print_text_pixel_height > PRINT_LINES / 2 - 1) { |
| 3669 |
int tll = print_text[print_text_top] == 255 ? 16 : 9; |
| 3670 |
print_text_pixel_height -= tll; |
| 3671 |
print_text_top += tll == 16 ? 1 : (print_text[print_text_top] + 1); |
| 3672 |
if (print_text_top >= PRINT_TEXT_SIZE) |
| 3673 |
print_text_top -= PRINT_TEXT_SIZE; |
| 3674 |
} |
| 3675 |
} |
| 3676 |
|
| 3677 |
static FILE *logfile = NULL; |
| 3678 |
|
| 3679 |
void shell_log(const char *message) { |
| 3680 |
if (logfile == NULL) |
| 3681 |
logfile = fopen("free42.log", "w"); |
| 3682 |
fprintf(logfile, "%s\n", message); |
| 3683 |
fflush(logfile); |
| 3684 |
} |
| 3685 |
|
| 3686 |
void shell_get_time_date(uint4 *time, uint4 *date, int *weekday) { |
| 3687 |
struct timeval tv; |
| 3688 |
gettimeofday(&tv, NULL); |
| 3689 |
struct tm tms; |
| 3690 |
localtime_r(&tv.tv_sec, &tms); |
| 3691 |
if (time != NULL) |
| 3692 |
*time = ((tms.tm_hour * 100 + tms.tm_min) * 100 + tms.tm_sec) * 100 + tv.tv_usec / 10000; |
| 3693 |
if (date != NULL) |
| 3694 |
*date = ((tms.tm_year + 1900) * 100 + tms.tm_mon + 1) * 100 + tms.tm_mday; |
| 3695 |
if (weekday != NULL) |
| 3696 |
*weekday = tms.tm_wday; |
| 3697 |
} |