| 1 | // This file is part of SmallBASIC |
| 2 | // |
| 3 | // lowlevel device (OS) I/O |
| 4 | // |
| 5 | // This program is distributed under the terms of the GPL v2.0 or later |
| 6 | // Download the GNU Public License (GPL) from www.gnu.org |
| 7 | // |
| 8 | // Copyright(C) 2000 Nicholas Christopoulos |
| 9 | |
| 10 | #include "config.h" |
| 11 | |
| 12 | #include "include/osd.h" |
| 13 | #include "common/device.h" |
| 14 | #include "common/smbas.h" |
| 15 | #include "common/messages.h" |
| 16 | #include "common/keymap.h" |
| 17 | #include "common/inet.h" |
| 18 | |
| 19 | /** |
| 20 | * initialize all drivers |
| 21 | */ |
| 22 | int dev_init(int mode, int flags) { |
| 23 | dev_initfs(); |
| 24 | dev_fgcolor = 0; |
| 25 | dev_bgcolor = (os_graphics) ? 15 : 0; |
| 26 | osd_devinit(); |
| 27 | |
| 28 | // init the keyboard map |
| 29 | keymap_init(); |
| 30 | |
| 31 | dev_viewport(0, 0, 0, 0); |
| 32 | dev_window(0, 0, 0, 0); |
| 33 | |
| 34 | if (os_graphics) { |
| 35 | // dev_fgcolor + dev_bgcolor can be overridden in osd_devinit() |
| 36 | // otherwise left as default black text on white background |
| 37 | osd_settextcolor(dev_fgcolor, dev_bgcolor); |
| 38 | osd_setcolor(dev_fgcolor); |
| 39 | } else { |
| 40 | dev_fgcolor = 7; |
| 41 | dev_bgcolor = 0; |
| 42 | } |
| 43 | |
| 44 | return 1; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * restore device's mode |
| 49 | */ |
| 50 | int dev_restore() { |
| 51 | if (os_graphics) { |
| 52 | osd_refresh(); |
| 53 | } |
| 54 | dev_closefs(); |
| 55 | if (os_graphics) { |
| 56 | osd_devrestore(); |
| 57 | } |
| 58 | net_close(); |
| 59 | return 1; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * CHECK FOR EVENTS |
| 64 | * |
| 65 | * wait == 0 check & return |
| 66 | * != 0 wait for an event |
| 67 | * |
| 68 | * returns 0 for no events in queue |
| 69 | */ |
| 70 | int dev_events(int wait_flag) { |
| 71 | return osd_events(wait_flag); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * BEEP |
| 76 | */ |
| 77 | void dev_beep() { |
| 78 | if (!opt_mute_audio) { |
| 79 | osd_beep(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * plays an OGG or MP3 file |
| 85 | */ |
| 86 | void dev_audio(const char *path) { |
| 87 | if (!opt_mute_audio) { |
| 88 | osd_audio(path); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * plays a sound |
| 94 | */ |
| 95 | void dev_sound(int frq, int ms, int vol, int bgplay) { |
| 96 | if (!opt_mute_audio) { |
| 97 | osd_sound(frq, ms, vol, bgplay); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * clear background sound queue |
| 103 | */ |
| 104 | void dev_clear_sound_queue() { |
| 105 | osd_clear_sound_queue(); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * printf |
| 110 | */ |
| 111 | void dev_printf(const char *format, ...) { |
| 112 | va_list args; |
| 113 | va_start(args, format); |
| 114 | unsigned size = vsnprintf(NULL, 0, format, args); |
| 115 | va_end(args); |
| 116 | |
| 117 | if (size) { |
| 118 | char *buf = malloc(size + 1); |
| 119 | buf[0] = '\0'; |
| 120 | va_start(args, format); |
| 121 | vsnprintf(buf, size + 1, format, args); |
| 122 | va_end(args); |
| 123 | buf[size] = '\0'; |
| 124 | dev_print(buf); |
| 125 | free(buf); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * In the FLTK build, prints to the LOG window, in other builds |
| 131 | * prints to the output device as per dev_printf |
| 132 | */ |
| 133 | void log_printf(const char *format, ...) { |
| 134 | va_list args; |
| 135 | va_start(args, format); |
| 136 | unsigned size = vsnprintf(NULL, 0, format, args); |
| 137 | va_end(args); |
| 138 | |
| 139 | if (size) { |
| 140 | char *buf = malloc(size + 3); |
| 141 | buf[0] = '\0'; |
| 142 | va_start(args, format); |
| 143 | vsnprintf(buf, size + 1, format, args); |
| 144 | va_end(args); |
| 145 | |
| 146 | buf[size] = '\0'; |
| 147 | int i = size - 1; |
| 148 | while (i >= 0 && isspace(buf[i])) { |
| 149 | buf[i--] = '\0'; |
| 150 | } |
| 151 | strcat(buf, "\r\n" ); |
| 152 | #if defined(IMPL_LOG_WRITE) |
| 153 | lwrite(buf); |
| 154 | #else |
| 155 | dev_print(buf); |
| 156 | #endif |
| 157 | free(buf); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | #if !defined(_SDL) |
| 162 | void dev_trace_line(int lineNo) { |
| 163 | dev_printf("<%d>" , lineNo); |
| 164 | } |
| 165 | #endif |
| 166 | |
| 167 | #ifndef IMPL_LOG_WRITE |
| 168 | void lwrite(const char *buf) { |
| 169 | fprintf(stderr, "%s\n" , buf); |
| 170 | } |
| 171 | #endif |
| 172 | |
| 173 | /** |
| 174 | * Displays a fatal error message |
| 175 | */ |
| 176 | void panic(const char *fmt, ...) { |
| 177 | va_list argp; |
| 178 | va_start(argp, fmt); |
| 179 | vfprintf(stderr, fmt, argp); |
| 180 | dev_print("\nFatal error\n" ); |
| 181 | va_end(argp); |
| 182 | exit(EXIT_FAILURE); |
| 183 | } |
| 184 | |