xref: /haiku/src/apps/screenshot/Screenshot.cpp (revision 3904a8dba0df1065db019e58a491c712cdf9cd83)
1 /*
2  * Copyright Karsten Heimrich, host.haiku@gmx.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Karsten Heimrich
7  *		Fredrik Modéen
8  */
9 
10 
11 #include "Screenshot.h"
12 
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <Catalog.h>
18 #include <Locale.h>
19 
20 
21 #include <TranslatorFormats.h>
22 
23 
24 #include "ScreenshotWindow.h"
25 
26 
27 Screenshot::Screenshot()
28 	:
29 	BApplication("application/x-vnd.Haiku-Screenshot"),
30 	fArgvReceived(false),
31 	fRefsReceived(false),
32 	fImageFileType(B_PNG_FORMAT),
33 	fTranslator(8)
34 {
35 	be_locale->GetAppCatalog(&fCatalog);
36 }
37 
38 
39 Screenshot::~Screenshot()
40 {
41 }
42 
43 
44 void
45 Screenshot::ReadyToRun()
46 {
47 	if (!fArgvReceived && !fRefsReceived)
48 		new ScreenshotWindow();
49 
50 	fArgvReceived = false;
51 	fRefsReceived = false;
52 }
53 
54 
55 void
56 Screenshot::RefsReceived(BMessage* message)
57 {
58 	int32 delay = 0;
59 	message->FindInt32("delay", &delay);
60 
61 	bool includeBorder = false;
62 	message->FindBool("border", &includeBorder);
63 
64 	bool includeMouse = false;
65 	message->FindBool("border", &includeMouse);
66 
67 	bool grabActiveWindow = false;
68 	message->FindBool("window", &grabActiveWindow);
69 
70 	bool saveScreenshotSilent = false;
71 	message->FindBool("silent", &saveScreenshotSilent);
72 
73 	bool showConfigureWindow = false;
74 	message->FindBool("configure", &showConfigureWindow);
75 
76 	new ScreenshotWindow(delay * 1000000, includeBorder, includeMouse,
77 		grabActiveWindow, showConfigureWindow, saveScreenshotSilent);
78 
79 	fRefsReceived = true;
80 }
81 
82 
83 void
84 Screenshot::ArgvReceived(int32 argc, char** argv)
85 {
86 	bigtime_t delay = 0;
87 
88 	bool includeBorder = false;
89 	bool includeMouse = false;
90 	bool grabActiveWindow = false;
91 	bool showConfigureWindow = false;
92 	bool saveScreenshotSilent = false;
93 
94 	for (int32 i = 0; i < argc; i++) {
95 		if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
96 			_ShowHelp();
97 		else if (strcmp(argv[i], "-b") == 0
98 			|| strcmp(argv[i], "--border") == 0)
99 			includeBorder = true;
100 		else if (strcmp(argv[i], "-m") == 0
101 			|| strcmp(argv[i], "--mouse-pointer") == 0)
102 			includeMouse = true;
103 		else if (strcmp(argv[i], "-w") == 0
104 			|| strcmp(argv[i], "--window") == 0)
105 			grabActiveWindow = true;
106 		else if (strcmp(argv[i], "-s") == 0
107 			|| strcmp(argv[i], "--silent") == 0)
108 			saveScreenshotSilent = true;
109 		else if (strcmp(argv[i], "-o") == 0
110 			|| strcmp(argv[i], "--options") == 0)
111 			showConfigureWindow = true;
112 		else if (strcmp(argv[i], "-f") == 0
113 			|| strncmp(argv[i], "--format", 6) == 0
114 			|| strncmp(argv[i], "--format=", 7) == 0) {
115 			_SetImageTypeSilence(argv[i + 1]);
116 		} else if (strcmp(argv[i], "-d") == 0
117 			|| strncmp(argv[i], "--delay", 7) == 0
118 			|| strncmp(argv[i], "--delay=", 8) == 0) {
119 			int32 seconds = -1;
120 			if (argc > i + 1)
121 				seconds = atoi(argv[i + 1]);
122 			if (seconds >= 0) {
123 				delay = seconds * 1000000;
124 				i++;
125 			} else {
126 				printf("Screenshot: option requires an argument -- %s\n"
127 					, argv[i]);
128 				exit(0);
129 			}
130 		}
131 	}
132 
133 	fArgvReceived = true;
134 
135 	new ScreenshotWindow(delay, includeBorder, includeMouse, grabActiveWindow,
136 		showConfigureWindow, saveScreenshotSilent, fImageFileType,
137 		fTranslator);
138 }
139 
140 
141 void
142 Screenshot::_ShowHelp() const
143 {
144 	printf("Screenshot [OPTION]... Creates a bitmap of the current screen\n\n");
145 	printf("OPTION\n");
146 	printf("  -o, --options         Show options window first\n");
147 	printf("  -m, --mouse-pointer   Include the mouse pointer\n");
148 	printf("  -b, --border          Include the window border\n");
149 	printf("  -w, --window          Capture the active window instead of the entire screen\n");
150 	printf("  -d, --delay=seconds   Take screenshot after specified delay [in seconds]\n");
151 	printf("  -s, --silent          Saves the screenshot without showing the app window\n");
152 	printf("                        overrides --options\n");
153 	printf("  -f, --format=image	Write the image format you like to save as\n");
154 	printf("                        [bmp], [gif], [jpg], [png], [ppm], [targa], [tiff]\n");
155 	printf("\n");
156 	printf("Note: OPTION -b, --border takes only effect when used with -w, --window\n");
157 	exit(0);
158 }
159 
160 
161 void
162 Screenshot::_SetImageTypeSilence(const char* name)
163 {
164 	if (strcmp(name, "bmp") == 0) {
165 		fImageFileType = B_BMP_FORMAT;
166 		fTranslator = 1;
167 	} else if (strcmp(name, "gif") == 0) {
168 		fImageFileType = B_GIF_FORMAT;
169 		fTranslator = 3;
170 	} else if (strcmp(name, "jpg") == 0) {
171 		fImageFileType = B_JPEG_FORMAT;
172 		fTranslator = 6;
173 	} else if (strcmp(name, "ppm") == 0) {
174 		fImageFileType = B_PPM_FORMAT;
175 		fTranslator = 9;
176 	} else if (strcmp(name, "targa") == 0) {
177 		fImageFileType = B_TGA_FORMAT;
178 		fTranslator = 14;
179 	} else if (strcmp(name, "tif") == 0) {
180 		fImageFileType = B_TIFF_FORMAT;
181 		fTranslator = 15;
182 	} else { //png
183 		fImageFileType = B_PNG_FORMAT;
184 		fTranslator = 8;
185 	}
186 }
187