tzwrapper.cc: fixed use of iterator after erase
[barry.git] / tools / bwatch.cc
blob8553f250c2a8af9e04b17dd424febb055d96da19
1 ///
2 /// \file bwatch.cc
3 /// Display a regularly updated video of the BlackBerry screen
4 ///
6 /*
7 Copyright (C) 2011, Alberto Mattea
8 Copyright (C) 2011-2013, Net Direct Inc. (http://www.netdirect.ca/)
10 Some parts are inspired from bjavaloader
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 See the GNU General Public License in the COPYING file at the
22 root directory of this project for more details.
26 #include <barry/barry.h>
27 #include <iostream>
28 #include <vector>
29 #include <string>
30 #include <cstring>
31 #include <algorithm>
32 #include <fstream>
33 #include <string.h>
34 #include <unistd.h>
35 #include <SDL/SDL.h>
36 #include "i18n.h"
38 using namespace std;
39 using namespace Barry;
41 void Usage()
43 int logical, major, minor;
44 const char *Version = Barry::Version(logical, major, minor);
46 cerr << string_vprintf(
47 _("bwatch - View video of BlackBerry screenshots\n"
48 " Copyright 2011, Alberto Mattea\n"
49 " Copyright 2011-2013, Net Direct Inc. (http://www.netdirect.ca/)\n"
50 " Using: %s\n"
51 "\n"
52 " -d delay Delay interval between screenshots, in milliseconds.\n"
53 " The lower the value, the higher the load on the device.\n"
54 " Default is 500ms.\n"
55 " -p pin PIN of device to talk with\n"
56 " If only one device is plugged in, this flag is optional\n"
57 " -P pass Simplistic method to specify device password\n"
58 " -v Dump protocol data during operation\n"),
59 Version)
60 << endl;
63 int main(int argc, char *argv[])
65 INIT_I18N(PACKAGE);
67 try {
69 cout.sync_with_stdio(true); // leave this on, since libusb uses stdio for debug messages
71 uint32_t pin = 0;
72 bool data_dump = false;
73 string password;
74 int delay = 500; // default delay of 500 ms
76 // process command line options
77 for(;;) {
78 int cmd = getopt(argc, argv, "d:hp:P:v");
79 if( cmd == -1 )
80 break;
82 switch( cmd )
84 case 'd': // delay interval in milliseconds
85 delay = atoi(optarg);
86 if( !delay ) {
87 cerr << _("Invalid interval value of: ") << optarg << ". " << _("Defaulting to 500ms.") << endl;
88 delay = 500;
90 break;
92 case 'p': // Blackberry PIN
93 pin = strtoul(optarg, NULL, 16);
94 break;
96 case 'P': // Device password
97 password = optarg;
98 break;
100 case 'v': // data dump on
101 data_dump = true;
102 break;
104 case 'h': // help
105 default:
106 Usage();
107 return 0;
111 // Init SDL
112 int sdl_width = -1, sdl_height = -1;
113 SDL_Surface *screen = NULL;
114 SDL_Event event;
115 int keypress = 0;
116 if( SDL_Init(SDL_INIT_VIDEO) < 0 )
117 return 1;
119 // Initialize the barry library. Must be called before
120 // anything else.
121 Barry::Init(data_dump);
123 JLScreenInfo info;
124 Data image, bitmap;
126 // Probe the USB bus for Blackberry devices and display.
127 // If user has specified a PIN, search for it in the
128 // available device list here as well
129 Barry::Probe probe;
130 int activeDevice = probe.FindActive(pin);
131 if( activeDevice == -1 ) {
132 cerr << _("No device selected, or PIN not found") << endl;
133 return 1;
136 // Main loop
137 cout << _("Press a key to exit...") << endl;
138 while( !keypress ) {
139 // Put this inside it's own block to avoid blocking the handheld
141 // Create our controller object
142 Barry::Controller con(probe.Get(activeDevice));
143 Barry::Mode::JavaLoader javaloader(con);
144 javaloader.Open(password.c_str());
145 javaloader.StartStream();
146 // Take a screenshot
147 // - info object contains the screenshot properties
148 // (width, height...)
149 // - image will be filled with the raw pixel
150 // screenshot data
151 javaloader.GetScreenshot(info, image);
152 javaloader.StopStream();
155 // Set the video mode according to the screenshot data
156 if( sdl_width != info.width || sdl_height != info.height ) {
157 sdl_width = info.width;
158 sdl_height = info.height;
159 if( !(screen = SDL_SetVideoMode(info.width, info.height, 0, SDL_HWSURFACE)) ) {
160 SDL_Quit();
161 return 1;
163 SDL_WM_SetCaption("Blackberry", 0);
166 // May want to tune this between 100 and 1000
167 SDL_Delay(500);
169 // Convert to 24-bit RGB
170 ScreenshotToRGB(info, image, bitmap, 0, 24, false);
172 // Do the blit
173 SDL_Surface *tmp;
174 tmp = SDL_CreateRGBSurfaceFrom((char*)bitmap.GetData(), info.width, info.height, 24, info.width*3, 0, 0, 0, 0);
175 SDL_BlitSurface(tmp, 0, screen, 0);
176 SDL_Flip(screen);
177 while(SDL_PollEvent(&event)) {
178 switch (event.type) {
179 case SDL_QUIT:
180 keypress = 1;
181 break;
182 case SDL_KEYDOWN:
183 keypress = 1;
184 break;
189 // Stop SDL
190 SDL_Quit();
191 return 0;
195 catch( Usb::Error &ue) {
196 std::cout << endl; // flush any normal output first
197 std::cerr << _("Usb::Error caught: ") << ue.what() << endl;
198 return 1;
200 catch( Barry::Error &se ) {
201 std::cout << endl;
202 std::cerr << _("Barry::Error caught: ") << se.what() << endl;
203 return 1;
205 catch( std::exception &e ) {
206 std::cout << endl;
207 std::cerr << _("std::exception caught: ") << e.what() << endl;
208 return 1;