]> pd.if.org Git - mmurtl/blob - msamples/scrnprnt/scrnprnt.c
autocommit for file dated 1995-01-30 23:06:34
[mmurtl] / msamples / scrnprnt / scrnprnt.c
1 /* This is a screen print program for MMURTL. It runs\r
2    like any other application on the system except it\r
3    looks for a Global Hot key (CTRL-ALT-PrintScreen)\r
4    and prints the REAL screen to LPT1 or a file.\r
5 \r
6    The screen print is straight text with CR/LF at the\r
7    end of each line. Nulls and non-printable characters\r
8    are converted to spaces.\r
9 */\r
10 \r
11 #include <stdio.h>\r
12 #include <string.h>\r
13 #include <stdlib.h>\r
14 #include "\OSSource\MKernel.h"\r
15 #include "\OSSource\MJob.h"\r
16 #include "\OSSource\MVid.h"\r
17 #include "\OSSource\MData.h"\r
18 #include "\OSSource\MTimer.h"\r
19 #include "\OSSource\MDevDrv.h"\r
20 #include "\OSSource\Parallel.h"\r
21 \r
22 #define ErcOK         0\r
23 \r
24 struct RqBlkType *pRqBlk;       /* A pointer to a Reqeust Block */\r
25 unsigned long AnExch;                   /* Where we wait for Requests */\r
26 unsigned long Message[2];       /* The Message with the Request */\r
27 long rqHndlG;                                   /* Used for global print request */\r
28 long rqHndlK;                                   /* Used for keyboard request */\r
29 long cbFilename;\r
30 char Filename[80];\r
31 char fToFile = 0;\r
32 long ourJob, printJob;\r
33 \r
34 FILE *fh;\r
35 \r
36 long keycode, keycodeG;\r
37 \r
38 unsigned char buf[4160];                /* for a snapshot of the screen */\r
39 \r
40 /***************************************************\r
41    Copies active screen memory to a working buffer.\r
42    and beeps to let user know it's been done.\r
43 ***************************************************/\r
44 void CaptureScreen(void)\r
45 {\r
46  CopyData(0xB8000, buf, 4000);\r
47  Tone(1000,15);         /* 1000 Hz for 100ms */\r
48  Tone(300,15);          /* 300  Hz for 100ms */\r
49 }\r
50 \r
51 \r
52 /***************************************************\r
53    This filles all nulls and NON ASCII chars in\r
54    the capture buffer with spaces.\r
55 ****************************************************/\r
56 \r
57 ProcessScreen(void)\r
58 {\r
59 long i;\r
60         for (i=0; i<4000; i+=2)\r
61         {\r
62                 if ((buf[i] < 0x20) || (buf[i] > 0x7E))\r
63                         buf[i] = ' ';\r
64         }\r
65 }\r
66 \r
67 /***************************************************\r
68    Sends the contents of the processed screen buffer\r
69    to the file the user specified. LF is added to\r
70    each line.\r
71 ****************************************************/\r
72 \r
73 void ScreenToFile(FILE *fh)\r
74 {\r
75 long i, j;\r
76         for (i=0; i<25; i++)\r
77         {\r
78                 for (j=0; j<160; j+=2)\r
79                 {\r
80                         fputc(buf[(i*160)+j], fh);\r
81                 }\r
82                 fputc(0x0A, fh);\r
83         }\r
84 }\r
85 \r
86 \r
87 /***************************************************\r
88    Sends the contents of the processed screen buffer\r
89    to the LPT1 device. CR/LF is added to each line.\r
90 ****************************************************/\r
91 \r
92 void ScreenToLPT(void)\r
93 {\r
94 long erc;\r
95 long i, j;\r
96 \r
97 \r
98         /* If device status went OK, we open the printer port */\r
99 \r
100                 /* device, dOpNum,   dLBA, dnBlocks, pData */\r
101         erc = DeviceOp(3,      CmdOpenL, 0,    0,        &i);\r
102 \r
103         if (erc)\r
104         {\r
105             printf("OpenLPT ERROR: %d \r\n", erc);\r
106                 return;\r
107         }\r
108 \r
109         for (i=0; i<25; i++)\r
110         {\r
111                 for (j=0; j<160; j+=2)\r
112                 {\r
113                         erc = DeviceOp(3, CmdWriteB, 0,  1, &buf[(i*160)+j]);\r
114                         if (erc)\r
115                                 break;\r
116                 }\r
117                 erc = DeviceOp(3, CmdWriteB, 0,  1, "\r");\r
118                 erc = DeviceOp(3, CmdWriteB, 0,  1, "\n");\r
119                 if (erc)\r
120                         break;\r
121         }\r
122         erc = DeviceOp(3, CmdWriteB, 0,  1, "\f");\r
123 \r
124         if (erc)\r
125                 printf("Can't write to LPT. Error: %d\r\n", erc);\r
126 \r
127                          /* device, dOpNum, dLBA, dnBlocks, pData */\r
128         erc = DeviceOp(3,  CmdCloseL,   0,   0,        &i);\r
129         if (erc)\r
130                 printf("Can't close LPT. Error: %d\r\n", erc);\r
131 \r
132 }\r
133 \r
134 \r
135 /***************************************************\r
136    Displays the menu after a user does something\r
137 ****************************************************/\r
138 \r
139 void menu(void)\r
140 {\r
141         printf("CTRL-ALT-PrintScrn will print the screen from any job you're viewing.\r\n");\r
142         printf("Press:\r\n");\r
143         printf("   1) To direct screen prints to LPT1/Printer (default).\r\n");\r
144         printf("   2) To direct screen prints to a file.\r\n");\r
145         printf("   3) To terminte the screen print utility.\r\n\n");\r
146 }\r
147 \r
148 /***************************************************\r
149    Main entry point for SPU.\r
150 ****************************************************/\r
151 \r
152 void main(long argc, char *argv[])\r
153 {\r
154 unsigned long OSError, x, y;\r
155 unsigned char c, ExitChar;\r
156 \r
157         GetJobNum(&ourJob);\r
158         SetJobName("ScreenPrint", 11);\r
159 \r
160         FillData(Filename, 80, ' ');\r
161         cbFilename = 0;\r
162         fToFile = 0;\r
163 \r
164         OSError = AllocExch(&AnExch);     /* get an exchange */\r
165 \r
166         if (OSError)                      /* look for a kernel error */\r
167                 ExitJob(OSError);\r
168 \r
169         SetNormVid(WHITE|BGBLACK);\r
170         ClrScr();\r
171 \r
172         printf("Screen Print Utility installed as job number: %d.\r\n\n", ourJob);\r
173 \r
174         /* Leave a global request for a key with the keyboard service */\r
175 \r
176         OSError = Request("KEYBOARD", 2, AnExch, &rqHndlG, 0, &keycodeG,\r
177                                           4, 0, 0, 0, 0, 0);\r
178         if (OSError)\r
179                 printf("Error %d on Global Keyboard Request\r\n", OSError);\r
180 \r
181         OSError = Request("KEYBOARD", 1, AnExch, &rqHndlK, 0, &keycode,\r
182                                           4, 0, 0, 1, 0, 0); /* 1 in dData0 = Wait for key */\r
183 \r
184         if (OSError)\r
185                 printf("Error %d on ReadKey Request\r\n", OSError);\r
186 \r
187         menu();\r
188 \r
189         while(1)\r
190         {\r
191                 /* We now have two requests waiting at the keyboard service.\r
192                    One is for the global key, the other is for a ReadKbd.\r
193                    When we Wait(), we will get one or the other back. We find\r
194                    out which one by checking the handle to see which it matches.\r
195                    Eaither someone wants the screen printed or the user\r
196                    has selcted a menu item.\r
197                  */\r
198 \r
199             OSError = WaitMsg(AnExch, Message); /* Exch & pointer */\r
200 \r
201             if (!OSError)\r
202             {\r
203 \r
204                         if (Message[0] == rqHndlK)  /* it was a menu item */\r
205                         {\r
206 \r
207                                 c = keycode & 0x7f;     /* lop off upper stuff in keycode */\r
208                                 switch (c)\r
209                                 {\r
210                                         case '1':\r
211                         fToFile = 0;\r
212                                                 printf("Screen print directed to printer (LPT1)\r\n");\r
213                                                 strcpy(Filename, "LPT");\r
214                                                 cbFilename = 3;\r
215                                                 break;\r
216                                         case '2':\r
217                                                 printf("\r\nEnter filename to print to: ");\r
218                                                 GetXY(&x, &y);\r
219                                                 EditLine(Filename, cbFilename, 50, &cbFilename,\r
220                                                                 &ExitChar, BLACK|BGWHITE);\r
221                                                 if ((ExitChar == 0x0d) && (cbFilename))\r
222                                                 {\r
223                             Filename[cbFilename] = 0; /* null terminte */\r
224                                 fToFile = 1;\r
225                                                 }\r
226                                                 else\r
227                                 fToFile = 0;\r
228                                                 printf("\r\n");\r
229                                                 break;\r
230                                         case '3':\r
231                                                 /* cancel the global key request */\r
232                                                 OSError = Request("KEYBOARD", 3, AnExch, &rqHndlG, 0, 0,\r
233                                                                   0, 0, 0, ourJob, 0, 0);\r
234                                             OSError = WaitMsg(AnExch, Message);\r
235                                                 DeAllocExch(AnExch);\r
236                                                 exit(0);\r
237                                                 break;\r
238                                 }\r
239                                 /* leave another keyboard request */\r
240                                 OSError = Request("KEYBOARD", 1, AnExch, &rqHndlK, 0, &keycode,\r
241                                                           4, 0, 0, 1, 0, 0); /* 1 in dData0 = Wait for key */\r
242                                 menu();\r
243 \r
244                         }\r
245                         else if (Message[0] == rqHndlG) /* It's a screen print!! */\r
246                         {\r
247                                 if ((keycodeG & 0xff) == 0x1C) /* it's us! */\r
248                                 {\r
249                                         CaptureScreen();\r
250                                         ProcessScreen();\r
251                                         if (fToFile)\r
252                                         {\r
253                                                 fh = fopen(Filename, "a");\r
254                                                 if (fh)\r
255                                                 {\r
256                                                         ScreenToFile(fh);\r
257                                                         fclose(fh);\r
258                                                 }\r
259                                                 else\r
260                                                         printf("Can't open file: %s\r\n", Filename);\r
261                                         }\r
262                                         else\r
263                                                 ScreenToLPT();\r
264 \r
265                                         GetVidOwner(&printJob);\r
266                                         if (fToFile)\r
267                                                 printf("Screen print sent to %s for job %d\r\n\n",\r
268                                                                 Filename, printJob);\r
269                                         else\r
270                                                 printf("Screen print sent to LPT1 for job %d\r\n\n",\r
271                                                                 printJob);\r
272                                         menu();\r
273                                 }\r
274 \r
275                                 /* Leave another global request with the keyboard service */\r
276 \r
277                                 OSError = Request("KEYBOARD", 2, AnExch, &rqHndlG, 0, &keycodeG,\r
278                                                                   4, 0, 0, 0, 0, 0);\r
279 \r
280                         }\r
281                 }\r
282                 else\r
283                         printf("Error %d on WaitMsg from kernel\r\n", OSError);\r
284         }  /* Loop while(1) */\r
285 }\r