8c7e68baae983e854680533fda22c09c2dc58ae6
[profile/ivi/murphy.git] / packaging.in / murphy.lua
1 with_system_controller = false
2 with_amb = false
3 verbose = 0
4
5 m = murphy.get()
6
7 -- try loading the various logging plugins
8 m:try_load_plugin('systemd')
9 m:try_load_plugin('dlog')
10
11 -- load the console plugin
12 m:try_load_plugin('console')
13
14 m:try_load_plugin('console.disabled', 'webconsole', {
15                   address = 'wsck:127.0.0.1:3000/murphy',
16                   httpdir = '/usr/share/murphy/webconsole' });
17
18 -- load the dbus plugin
19 if m:plugin_exists('dbus') then
20     m:load_plugin('dbus')
21 end
22
23 -- load the native resource plugin
24 if m:plugin_exists('resource-native') then
25     m:load_plugin('resource-native')
26     m:info("native resource plugin loaded")
27 else
28     m:info("No native resource plugin found...")
29 end
30
31 -- load the dbus resource plugin
32 m:try_load_plugin('resource-dbus', {
33     dbus_bus = "system",
34     dbus_service = "org.Murphy",
35     dbus_track = true,
36     default_zone = "driver",
37     default_class = "implicit"
38 })
39
40 -- load the domain control plugin
41 if m:plugin_exists('domain-control') then
42     m:load_plugin('domain-control')
43 else
44     m:info("No domain-control plugin found...")
45 end
46
47 if m:plugin_exists('glib') then
48     m:load_plugin('glib')
49 else
50     m:info("No glib plugin found...")
51 end
52
53 if m:plugin_exists("gam-resource-manager") then
54
55 function get_general_priorities(self)
56     print("*** get_general_priorities\n")
57     return { "USB Headset", "wiredHeadset", "speakers" }
58 end
59
60 function get_phone_priorities(self)
61     print("*** get_phone_priorities\n")
62     return { "wiredHeadset", "USB Headset" }
63 end
64
65 m:load_plugin('gam-resource-manager', {
66     config_dir = '/etc/murphy/gam',
67     decision_names = 'gam-wrtApplication-4',
68     max_active = 4,
69     app_mapping = {
70         ['t8j6HTRpuz.MediaPlayer'] = 'wrtApplication',
71         ['pacat'] = 'icoApplication'
72     },
73     app_default = 'icoApplication'
74 })
75
76 routing_sink_priority {
77     application_class = "player",
78     priority_queue = get_general_priorities
79 }
80
81 routing_sink_priority {
82     application_class = "game",
83     priority_queue = get_general_priorities
84 }
85
86 routing_sink_priority {
87     application_class = "implicit",
88     priority_queue = get_general_priorities
89 }
90
91 routing_sink_priority {
92     application_class = "phone",
93     priority_queue = get_phone_priorities
94 }
95
96 routing_sink_priority {
97     application_class = "basic",
98     priority_queue = get_phone_priorities
99 }
100
101 routing_sink_priority {
102     application_class = "event",
103     priority_queue = get_phone_priorities
104 }
105 end
106
107 -- load the AMB plugin
108 if m:plugin_exists('amb') then
109     m:try_load_plugin('amb')
110
111     if builtin.method.amb_initiate and
112        builtin.method.amb_update
113     then
114         with_amb = true
115     end
116 else
117     m:info("No amb plugin found...")
118 end
119
120 -- load the ASM resource plugin
121 if m:plugin_exists('resource-asm') then
122     m:try_load_plugin('resource-asm', {
123         zone = "driver",
124         share_mmplayer = "player:AVP,mandatory,exclusive,strict",
125         ignored_argv0 = "WebProcess"
126     })
127 else
128     m:info("No audio session manager plugin found...")
129 end
130
131 if m:plugin_exists('system-controller') then
132     with_system_controller = true
133 elseif m:plugin_exists('ivi-resource-manager') then
134     m:load_plugin('ivi-resource-manager')
135     with_system_controller = false
136 end
137
138 -- define application classes
139 application_class {
140     name     = "interrupt",
141     priority = 99,
142     modal    = true ,
143     share    = false,
144     order    = "fifo"
145 }
146
147 application_class {
148     name     = "emergency",
149     priority = 80,
150     modal    = false,
151     share    = false,
152     order    = "fifo"
153 }
154 application_class {
155     name     = "system",
156     priority = 52,
157     modal    = false,
158     share    = true,
159     order    = "lifo"
160 }
161 application_class {
162     name     = "alert",
163     priority = 51,
164     modal    = false,
165     share    = false,
166     order    = "fifo"
167 }
168
169 application_class {
170     name     = "navigator",
171     priority = 50,
172     modal    = false,
173     share    = true,
174     order    = "fifo"
175 }
176
177 application_class {
178     name     = "phone",
179     priority = 6 ,
180     modal    = false,
181     share    = true ,
182     order    = "lifo"
183 }
184 application_class {
185     name     = "camera",
186     priority = 5,
187     modal    = false,
188     share    = false,
189     order    = "lifo"
190 }
191
192 application_class { name="event"    , priority=4 , modal=false, share=true , order="fifo" }
193 application_class { name="game"     , priority=3 , modal=false, share=false, order="lifo" }
194 --# doesn't need to be created here, ivi-resource-manager creates it if loaded
195 --#application_class { name="basic"    , priority=2 , modal=false, share=false, order="lifo" }
196 application_class { name="player"   , priority=1 , modal=false, share=true , order="lifo" }
197 application_class { name="implicit" , priority=0 , modal=false, share=false, order="lifo" }
198
199 -- define zone attributes
200 zone.attributes {
201     type = {mdb.string, "common", "rw"},
202     location = {mdb.string, "anywhere", "rw"}
203 }
204
205 -- define zones
206 zone {
207      name = "driver",
208      attributes = {
209          type = "common",
210          location = "front-left"
211      }
212 }
213
214 zone {
215      name = "passanger1",
216      attributes = {
217          type = "private",
218          location = "front-right"
219      }
220 }
221
222 zone {
223      name = "passanger2",
224      attributes = {
225          type = "private",
226          location = "back-left"
227      }
228 }
229
230 zone {
231      name = "passanger3",
232      attributes = {
233          type = "private",
234          location = "back-right"
235      }
236 }
237
238 zone {
239      name = "passanger4",
240      attributes = {
241          type = "private",
242          location = "back-left"
243      }
244 }
245
246
247 -- define resource classes
248 if not m:plugin_exists('ivi-resource-manager') and
249    not with_system_controller and
250    not m:plugin_exists('gam-resource-manager')
251 then
252    resource.class {
253         name = "audio_playback",
254         shareable = true,
255         attributes = {
256             role = { mdb.string, "music", "rw" },
257             pid = { mdb.string, "<unknown>", "rw" },
258             policy = { mdb.string, "relaxed", "rw" },
259             source = { mdb.string, "webkit", "rw" },
260             conn_id = { mdb.unsigned, 0, "rw" }
261         }
262    }
263 end
264
265 if not m:plugin_exists('gam-resource-manager') then
266     resource.class {
267         name = "audio_recording",
268         shareable = true,
269         attributes = {
270              role   = { mdb.string, "music"    , "rw" },
271              pid    = { mdb.string, "<unknown>", "rw" },
272              policy = { mdb.string, "relaxed"  , "rw" }
273          }
274     }
275 end
276
277 resource.class {
278      name = "video_playback",
279      shareable = false,
280 }
281
282 resource.class {
283      name = "video_recording",
284      shareable = false
285 }
286
287 resource.class {
288      name = "speech_recognition",
289      shareable = true
290 }
291
292 resource.class {
293      name = "speech_synthesis",
294      shareable = true
295 }
296
297 -- PulseAudio volume context
298 mdb.table {
299     name = "volume_context",
300     index = { "id" },
301     create = true,
302     columns = {
303         { "id", mdb.unsigned },
304         { "value", mdb.string, 64 },
305     }
306 }
307
308 -- put default volume context to the table
309 mdb.table.volume_context:insert({ id = 1, value = "default" })
310
311 if not m:plugin_exists('ivi-resource-manager') and
312    not with_system_controller
313 then
314     resource.method.veto = {
315         function(zone, rset, grant, owners, req_set)
316             return true
317          end
318     }
319 end
320
321 -- test for creating selections
322 mdb.select {
323            name = "audio_owner",
324            table = "audio_playback_owner",
325            columns = {"application_class"},
326            condition = "zone_name = 'driver'"
327 }
328
329 mdb.select {
330            name = "vehicle_speed",
331            table = "amb_vehicle_speed",
332            columns = {"value"},
333            condition = "key = 'VehicleSpeed'"
334 }
335
336 element.lua {
337    name    = "speed2volume",
338    inputs  = { speed = mdb.select.vehicle_speed, param = 9 },
339    outputs = {  mdb.table { name = "speedvol",
340                             index = {"zone", "device"},
341                             columns = {{"zone", mdb.string, 16},
342                                        {"device", mdb.string, 16},
343                                        {"value", mdb.floating}},
344                             create = true
345                            }
346              },
347    oldvolume = 0.0,
348    update  = function(self)
349                 speed = self.inputs.speed.single_value
350                 if (speed) then
351                     volume = (speed - 144.0) / 7.0
352                 else
353                     volume = 0.0
354                 end
355                 diff = volume - self.oldvolume
356                 if (diff*diff > self.inputs.param) then
357                     print("*** element "..self.name.." update "..volume)
358                     self.oldvolume = volume
359                     mdb.table.speedvol:replace({zone = "driver", device = "speakers", value = volume})
360                 end
361              end
362 }
363
364 mdb.select {
365     name = "amb_state",
366     table = "amb_state",
367     columns = { "state" },
368     condition = "id = 0"
369 }
370
371 -- Night mode processing chain
372
373 mdb.select {
374     name = "exterior_brightness",
375     table = "amb_exterior_brightness",
376     columns = { "value" },
377     condition = "key = 'ExteriorBrightness'"
378 }
379
380 element.lua {
381     name    = "nightmode",
382     inputs  = { brightness = mdb.select.exterior_brightness },
383     oldmode = -1;
384     outputs = {
385         mdb.table {
386             name = "amb_nightmode",
387             index = { "id" },
388             create = true,
389             columns = {
390                 { "id", mdb.unsigned },
391                 { "night_mode", mdb.unsigned }
392             }
393         }
394     },
395     update = function(self)
396         -- This is a trivial function to calculate night mode. Later, we will
397         -- need a better threshold value and hysteresis to prevent oscillation.
398
399         brightness = self.inputs.brightness.single_value
400
401         if not brightness then
402             return
403         end
404
405         print("*** element "..self.name.." update brightness: "..brightness)
406
407         if brightness > 300 then
408             mode = 0
409         else
410             mode = 1
411         end
412
413         print("*** resulting mode: ".. mode)
414
415         if not (mode == self.oldmode) then
416             mdb.table.amb_nightmode:replace({ id = 0, night_mode = mode })
417         end
418
419         self.oldmode = mode
420     end
421 }
422
423 mdb.select {
424     name = "select_night_mode",
425     table = "amb_nightmode",
426     columns = { "night_mode" },
427     condition = "id = 0"
428 }
429
430 if with_amb then
431     sink.lua {
432         name = "night_mode",
433         inputs = { NightMode = mdb.select.select_night_mode,
434                    amb_state = mdb.select.amb_state },
435         property = "NightMode",
436         type = "b",
437         initiate = builtin.method.amb_initiate,
438         update = builtin.method.amb_update
439     }
440 end
441
442 -- Night mode general handlers
443
444 if with_system_controller then
445     sink.lua {
446         name = "nightmode_homescreen",
447         inputs = { owner = mdb.select.select_night_mode },
448         initiate = function(self)
449                 -- data = mdb.select.select_night_mode.single_value
450                 return true
451             end,
452         update = function(self)
453                 send_night_mode_to(homescreen)
454             end
455     }
456 end
457
458 -- Driving mode processing chain
459
460 element.lua {
461     name    = "drivingmode",
462     inputs  = { speed = mdb.select.vehicle_speed },
463     oldmode = -1;
464     outputs = {
465         mdb.table {
466             name = "amb_drivingmode",
467             index = { "id" },
468             create = true,
469             columns = {
470                 { "id", mdb.unsigned },
471                 { "driving_mode", mdb.unsigned }
472             }
473         }
474     },
475     update = function(self)
476
477         speed = self.inputs.speed.single_value
478
479         if not speed then
480             return
481         end
482
483         if speed == 0 then
484             mode = 0
485         else
486             mode = 1
487         end
488
489         if not (mode == self.oldmode) then
490             mdb.table.amb_drivingmode:replace({ id = 0, driving_mode = mode })
491         end
492
493         self.oldmode = mode
494     end
495 }
496
497 mdb.select {
498     name = "select_driving_mode",
499     table = "amb_drivingmode",
500     columns = { "driving_mode" },
501     condition = "id = 0"
502 }
503
504 if with_amb then
505     sink.lua {
506         name = "driving_mode",
507         inputs = { DrivingMode = mdb.select.select_driving_mode,
508                    amb_state = mdb.select.amb_state },
509         property = "DrivingMode",
510         type = "u",
511         initiate = builtin.method.amb_initiate,
512         update = builtin.method.amb_update
513     }
514 end
515
516 -- turn signals (left, right)
517
518 mdb.select {
519     name = "winker",
520     table = "amb_turn_signal",
521     columns = { "value" },
522     condition = "key = 'TurnSignal'"
523 }
524
525 -- define three categories
526
527 mdb.select {
528     name = "undefined_applications",
529     table = "aul_applications",
530     columns = { "appid" },
531     condition = "category = '<undefined>'"
532 }
533
534 mdb.select {
535     name = "basic_applications",
536     table = "aul_applications",
537     columns = { "appid" },
538     condition = "category = 'basic'"
539 }
540
541 mdb.select {
542     name = "entertainment_applications",
543     table = "aul_applications",
544     columns = { "appid" },
545     condition = "category = 'entertainment'"
546 }
547
548 function ft(t)
549     -- filter the object garbage out of the tables
550     ret = {}
551
552     for k,v in pairs(t) do
553         if k ~= "userdata" and k ~= "new" then
554             ret[k] = v
555         end
556     end
557
558     return ret
559 end
560
561 function getApplication(appid)
562     local conf = nil
563
564     -- find the correct local application definition
565
566     for k,v in pairs(ft(application)) do
567         if appid == v.appid then
568             conf = v
569             break
570         end
571     end
572
573     return conf
574 end
575
576 function regulateApplications(t, regulation)
577     for k,v in pairs(ft(t)) do
578
579         whitelisted = false
580
581         -- our local application config, which takes precedence
582         local conf = getApplication(v.appid)
583
584         if conf then
585             if conf.resource_class ~= "player" then
586                 whitelisted = true
587             end
588
589                if conf.requisites and conf.requisites.screen then
590                 if conf.requisites.screen.driving then
591                     whitelisted = true
592                 end
593             end
594         end
595
596         if whitelisted then
597             -- override, don't disable
598             resmgr:disable_screen_by_appid("*", "*", v.appid, false, false)
599         else
600             resmgr:disable_screen_by_appid("*", "*", v.appid, regulation == 1, false)
601         end
602     end
603     resource.method.recalc("driver")
604 end
605
606 -- regulation (on), use "select_driving_mode"
607
608 sink.lua {
609     name = "driving_regulation",
610     inputs = { owner = mdb.select.select_driving_mode },
611     initiate = function(self)
612         -- local data = mdb.select.select_driving_mode.single_value
613         return true
614     end,
615     update = function(self)
616         local data = mdb.select.select_driving_mode.single_value
617
618         if verbose > 1 then
619             print("Driving mode updated: " .. tostring(data))
620         end
621
622         if not sc then
623             return true
624         end
625
626         -- tell homescreen that driving mode was updated
627         send_driving_mode_to(homescreen)
628
629         regulateApplications(ft(mdb.select.entertainment_applications), data)
630         regulateApplications(ft(mdb.select.undefined_applications), data)
631
632         return true
633     end
634 }
635 --[[
636 sink.lua {
637     name = "regulated_app_change",
638     inputs = { undef = mdb.select.undefined_applications,
639                entertainment = mdb.select.entertainment_applications },
640     initiate = function(self)
641         return true
642     end,
643     update = function(self)
644         local data = mdb.select.select_driving_mode.single_value
645
646         if not sc then
647             return
648         end
649
650         if verbose > 1 then
651             print("regulated application list was changed")
652         end
653
654         regulateApplications(ft(mdb.select.entertainment_applications), data)
655         regulateApplications(ft(mdb.select.undefined_applications), data)
656
657         return true
658     end
659 }
660 --]]
661
662 -- shift position (parking, reverse, other)
663
664 mdb.select {
665     name = "gear_position",
666     table = "amb_gear_position",
667     columns = { "value" },
668     condition = "key = 'GearPosition'"
669 }
670
671 -- cameras (back, front, left, right)
672
673 element.lua {
674     name    = "camera_state",
675     inputs  = { winker = mdb.select.winker, gear = mdb.select.gear_position },
676     oldmode = -1;
677     outputs = {
678         mdb.table {
679             name = "target_camera_state",
680             index = { "id" },
681             create = true,
682             columns = {
683                 { "id", mdb.unsigned },
684                 { "front_camera", mdb.unsigned },
685                 { "back_camera", mdb.unsigned },
686                 { "right_camera", mdb.unsigned },
687                 { "left_camera", mdb.unsigned }
688             }
689         }
690     },
691     update = function(self)
692
693         front_camera = 0
694         back_camera = 0
695         right_camera = 0
696         left_camera = 0
697
698         if self.inputs.gear == 128 then
699             back_camera = 1
700         elseif self.inputs.winker == 1 then
701             right_camera = 1
702         elseif self.inputs.winker == 2 then
703             left_camera = 1
704         end
705
706         mdb.table.target_camera_state:replace({ id = 0, front_camera = front_camera, back_camera = back_camera, right_camera = right_camera, left_camera = left_camera })
707
708     end
709 }
710
711 -- system controller test setup
712
713 if not with_system_controller then
714    -- ok, we should have 'audio_playback' defined by now
715    m:try_load_plugin('telephony')
716    return
717 end
718
719 m:load_plugin('system-controller')
720
721 onscreen_counter = 0
722
723 window_manager_operation_names = {
724     [1] = "create",
725     [2] = "destroy"
726 }
727
728 function window_manager_operation_name(oper)
729     local name = window_manager_operation_names[oper]
730     if name then return name end
731     return "<unknown " .. tostring(oper) .. ">"
732 end
733
734 window_operation_names = {
735     [1] = "create",
736     [2] = "destroy",
737     [3] = "name_change",
738     [4] = "visible",
739     [5] = "configure",
740     [6] = "active",
741     [7] = "map",
742     [8] = "hint"
743 }
744
745 function window_operation_name(oper)
746     local name = window_operation_names[oper]
747     if name then return name end
748     return "<unknown " .. tostring(oper) .. ">"
749 end
750
751 layer_operation_names = {
752     [1] = "create",
753     [2] = "destroy",
754     [3] = "visible"
755 }
756
757 function layer_operation_name(oper)
758     local name = layer_operation_names[oper]
759     if name then return name end
760     return "<unknown " .. tostring(oper) .. ">"
761 end
762
763 input_manager_operation_names = {
764     [1] = "create",
765     [2] = "destroy",
766     [3] = "ready"
767 }
768
769 function input_manager_operation_name(oper)
770     local name = input_manager_operation_names[oper]
771     if name then return name end
772     return "<unknown " .. tostring(oper) .. ">"
773 end
774
775 input_operation_names = {
776     [1] = "create",
777     [2] = "destroy",
778     [3] = "update"
779 }
780
781 function input_operation_name(oper)
782     local name = input_operation_names[oper]
783     if name then return name end
784     return "<unknown " .. tostring(oper) .. ">"
785 end
786
787 code_operation_names = {
788     [1] = "create",
789     [2] = "destroy",
790     [3] = "state_change"
791 }
792
793 function code_operation_name(oper)
794     local name = code_operation_names[oper]
795     if name then return name end
796     return "<unknown " .. tostring(oper) .. ">"
797 end
798
799 command_names = {
800     [0x00001] = "send_appid",
801     [0x10001] = "create",
802     [0x10002] = "destroy",
803     [0x10003] = "show",
804     [0x10004] = "hide",
805     [0x10005] = "move",
806     [0x10006] = "animation",
807     [0x10007] = "change_active",
808     [0x10008] = "change_layer",
809     [0x10009] = "change_attr",
810     [0x10010] = "name",
811     [0x10020] = "map_thumb",
812     [0x10021] = "unmap_thumb",
813     [0x10022] = "map_get",
814     [0x10030] = "show layer",
815     [0x10031] = "hide_layer",
816     [0x10032] = "change_layer_attr",
817     [0x20001] = "add_input",
818     [0x20002] = "del_input",
819     [0x30001] = "change_user",
820     [0x30002] = "get_userlist",
821     [0x30003] = "get_lastinfo",
822     [0x30004] = "set_lastinfo",
823     [0x40001] = "acquire_res",
824     [0x40002] = "release_res",
825     [0x40003] = "deprive_res",
826     [0x40004] = "waiting_res",
827     [0x40005] = "revert_res",
828     [0x40006] = "window_id_res",
829     [0x40011] = "create_res",
830     [0x40012] = "destroy_res",
831     [0x50001] = "set_region",
832     [0x50002] = "unset_region",
833     [0x60001] = "change_state"
834 }
835
836 function command_name(command)
837     local name = command_names[command]
838     if name then return name end
839     return "<unknown " .. tostring(command) .. ">"
840 end
841
842 input_layer = {
843    [101] = true, -- input
844    [102] = true, -- touch
845    [103] = true  -- cursor
846 }
847
848 -- some day this should be merged with wmgr.layers
849 ico_layer_type = {
850    [1]   = 0x1000, -- background
851    [2]   = 0x2000, -- application
852    [3]   = 0x2000, -- homescreen
853    [4]   = 0x2000, -- interrupt application
854    [5]   = 0x2000, -- onscreen application
855    [6]   = 0xc000, -- startup
856    [7]   = 0x3000, -- fullscreen
857    [101] = 0x4000, -- input
858    [102] = 0xa000, -- touch
859    [103] = 0xb000  -- cursor
860 }
861
862 resmgr = resource_manager {
863   screen_event_handler = function(self, ev)
864                              local event = ev.event
865                              local surface = ev.surface
866
867                              if event == "init" then
868                                  if verbose > 0 then
869                                      print("*** init screen resource allocation -- disable all 'player'")
870                                  end
871                                  resmgr:disable_audio_by_appid("*", "player", "*", true, false)
872                              elseif event == "preallocate" then
873                                  if verbose > 0 then
874                                      print("*** preallocate screen resource "..
875                                            "for '" .. ev.appid .. "' -- enable 'player', if any")
876                                  end
877                                  resmgr:disable_audio_by_appid("*", "player", ev.appid, false, false)
878                              elseif event == "grant" then
879                                  if verbose > 0 then
880                                     print("*** make visible surface "..surface)
881                                  end
882                                  local a = animation({})
883                                  local r = m:JSON({surface = surface,
884                                                    visible = 1,
885                                                    raise   = 1})
886                                  if ev.appid == onscreen then
887                                      onscreen_counter = onscreen_counter + 1
888                                      wmgr:layer_request(m:JSON({layer = 5, visible = 1}))
889                                  end
890
891                                  wmgr:window_request(r,a,0)
892                              elseif event == "revoke" then
893                                  if verbose > 0 then
894                                     print("*** hide surface "..surface)
895                                  end
896                                  local a = animation({})
897                                  local r = m:JSON({surface = ev.surface,
898                                                    visible = 0})
899                                  if ev.appid == onscreen then
900                                      onscreen_counter = onscreen_counter - 1
901                                      if onscreen_counter <= 0 then
902                                         onscreen_counter = 0
903                                         wmgr:layer_request(m:JSON({layer = 5, visible = 0}))
904                                      end
905                                  end
906                                  wmgr:window_request(r,a,0)
907
908                              elseif event == "create" then
909
910                                 if verbose > 0 then
911                                     print("*** screen resource event: " ..
912                                           tostring(ev))
913                                 end
914
915                                 local regulation = mdb.select.select_driving_mode.single_value
916
917                                 if regulation == 1 then
918
919                                     local blacklisted = false
920
921                                     -- applications which have their category set to "entertainment"
922                                     -- or "undefined" are blacklisted, meaning they should be regulated
923
924                                     for i,v in pairs(ft(mdb.select.undefined_applications)) do
925                                         if v.appid == ev.appid then
926                                             if verbose > 0 then
927                                                 print(ev.appid .. " was blacklisted (undefined)")
928                                             end
929                                             blacklisted = true
930                                             break
931                                         end
932                                     end
933
934                                     if not blacklisted then
935                                         for i,v in pairs(ft(mdb.select.entertainment_applications)) do
936                                             if v.appid == ev.appid then
937                                                 if verbose > 0 then
938                                                     print(ev.appid .. " was blacklisted (entertainment)")
939                                                 end
940                                                 blacklisted = true
941                                                 break
942                                             end
943                                         end
944                                     end
945
946                                     -- our local application config, which takes precedence
947                                     local conf = getApplication(ev.appid)
948
949                                     if not conf then
950                                         blacklisted = true
951                                     else
952                                         if conf.resource_class == "player" then
953                                             blacklisted = true
954                                         end
955
956                                         -- check the exceptions
957                                         if conf.requisites and conf.requisites.screen then
958                                             if conf.requisites.screen.driving then
959                                                 blacklisted = false
960                                             end
961                                         end
962                                     end
963
964                                     -- disable only non-whitelisted applications
965                                                     if blacklisted then
966                                         if verbose > 0 then
967                                             print("disabling screen for " .. ev.appid)
968                                         end
969                                         resmgr:disable_screen_by_appid("*", "*", ev.appid, true, true)
970                                     end
971                                 end
972
973                              elseif event == "destroy" then
974                                if verbose > 0 then
975                                     print("*** screen resource event: " ..
976                                           tostring(ev))
977                                  end
978                              else
979                                  if verbose > 0 then
980                                     print("*** screen resource event: " ..
981                                           tostring(ev))
982                                  end
983                              end
984                          end,
985   audio_event_handler = function(self, ev)
986                              local event = ev.event
987                              local appid = ev.appid
988                              local audioid = ev.audioid
989
990                              if event == "grant" then
991                                  if verbose > 0 then
992                                     print("*** grant audio to "..appid..
993                                           " ("..audioid..") in '" ..
994                                           ev.zone .. "' zone")
995                                  end
996                              elseif event == "revoke" then
997                                  if verbose > 0 then
998                                     print("*** revoke audio from "..appid..
999                                           " ("..audioid..") in '" ..
1000                                           ev.zone .. "' zone")
1001                                  end
1002                              else
1003                                  if verbose > 0 then
1004                                     print("*** audio resource event: " ..
1005                                           tostring(ev))
1006                                  end
1007                              end
1008                         end
1009 }
1010
1011 resclnt = resource_client {}
1012
1013 wmgr = window_manager {
1014   geometry = function(self, w,h, v)
1015                   if type(v) == "function" then
1016                       return v(w,h)
1017                   end
1018                   return v
1019              end,
1020
1021   application = function(self, appid)
1022                      if appid then
1023                          local app = application_lookup(appid)
1024                          if not app then
1025                              app = application_lookup("default")
1026                          end
1027                          return app
1028                      end
1029                      return { privileges = {screen="none", audio="none"} }
1030                 end,
1031
1032   output_order = { 1, 0 },
1033
1034   outputs = { { name  = "Mid",
1035                 id    = 1,
1036                 zone  = "driver",
1037                 areas = { Full = {
1038                               id     = 20,
1039                               pos_x  = 0,
1040                               pos_y  = 0,
1041                               width  = function(w,h) return w end,
1042                               height = function(w,h) return h end
1043                           },
1044                           Left = {
1045                               id     = 21,
1046                               pos_x  = 0,
1047                               pos_y  = 0,
1048                               width  = 320,
1049                               height = function(w,h) return h end
1050                           },
1051                           Right = {
1052                               id     = 22,
1053                               pos_x  = function(w,h) return w-320 end,
1054                               pos_y  = 0,
1055                               width  = 320,
1056                               height = function(w,h) return h end
1057                           }
1058                         }
1059                },
1060                { name  = "Center",
1061                  id    = 4,
1062                  zone  = "driver",
1063                  areas = { Status = {
1064                               id     = 0,
1065                               pos_x  = 0,
1066                               pos_y  = 0,
1067                               width  = function(w,h) return w end,
1068                               height = 64
1069                            },
1070                            Full = {
1071                               id     = 1,
1072                               pos_x  = 0,
1073                               pos_y  = 64,
1074                               width  = function(w,h) return w end,
1075                               height = function(w,h) return h-64-128 end
1076                            },
1077                            Upper = {
1078                               id     = 2,
1079                               pos_x  = 0,
1080                               pos_y  = 64,
1081                               width  = function(w,h) return w end,
1082                               height = function(w,h) return (h-64-128)/2 end
1083                            },
1084                            Lower = {
1085                               id     = 3,
1086                               pos_x  = 0,
1087                               pos_y  = function(w,h) return (h-64-128)/2+64 end,
1088                               width  = function(w,h) return w end,
1089                               height = function(w,h) return (h-64-128)/2 end
1090                            },
1091                            UpperLeft = {
1092                               id     = 4,
1093                               pos_x  = 0,
1094                               pos_y  = 64,
1095                               width  = function(w,h) return w/2 end,
1096                               height = function(w,h) return (h-64-128)/2 end
1097                            },
1098                            UpperRight = {
1099                               id     = 5,
1100                               pos_x  = function(w,h) return w/2 end,
1101                               pos_y  = 64,
1102                               width  = function(w,h) return w/2 end,
1103                               height = function(w,h) return (h-64-128)/2 end
1104                            },
1105                            LowerLeft = {
1106                               id     = 6,
1107                               pos_x  = 0,
1108                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
1109                               width  = function(w,h) return w/2 end,
1110                               height = function(w,h) return (h-64-128)/2 end
1111                            },
1112                            LowerRight = {
1113                               id     = 7,
1114                               pos_x  = function(w,h) return w/2 end,
1115                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
1116                               width  = function(w,h) return w/2 end,
1117                               height = function(w,h) return (h-64-128)/2 end
1118                            },
1119                            SysApp = {
1120                               id     = 8,
1121                               pos_x  = 0,
1122                               pos_y  = 64,
1123                               width  = function(w,h) return w end,
1124                               height = function(w,h) return h-64-128 end
1125                            },
1126                            ["SysApp.Left"] = {
1127                               id     = 9,
1128                               pos_x  = 0,
1129                               pos_y  = 64,
1130                               width  = function(w,h) return w/2-181 end,
1131                               height = function(w,h) return h-64-128 end
1132                            },
1133                            ["SysApp.Right"] = {
1134                               id     = 10,
1135                               pos_x  = function(w,h) return w/2+181 end,
1136                               pos_y  = 64,
1137                               width  = function(w,h) return w/2-181 end,
1138                               height = function(w,h) return h-64-128 end
1139                            },
1140                            MobileFull = {
1141                               id     = 11,
1142                               pos_x  = 0,
1143                               pos_y  = 64,
1144                               width  = function(w,h) return w end,
1145                               height = function(w,h) return h-64-128 end
1146                            },
1147                            MobileUpper = {
1148                               id     = 12,
1149                               pos_x  = 0,
1150                               pos_y  = 64,
1151                               width  = function(w,h) return w end,
1152                               height = function(w,h) return (h-64-128)/2 end
1153                            },
1154                            MobileLower = {
1155                               id     = 13,
1156                               pos_x  = 0,
1157                               pos_y  = function(w,h) return (h-64-128)/2+64 end,
1158                               width  = function(w,h) return w end,
1159                               height = function(w,h) return (h-64-128)/2 end
1160                            },
1161                            Control = {
1162                               id     = 14,
1163                               pos_x  = 0,
1164                               pos_y  = function(w,h) return h-128 end,
1165                               width  = function(w,h) return w end,
1166                               height = 128
1167                            },
1168                         }
1169               }
1170   },
1171              --    id   name            type  output
1172   layers = { {      0, "BackGround"   ,    1, "Center" },
1173              {      1, "Application"  ,    2, "Center" },
1174              {      2, "HomeScreen"   ,    3, "Center" },
1175              {      3, "ControlBar"   ,    3, "Center" },
1176              {      4, "InterruptApp" ,    4, "Center" },
1177              {      5, "OnScreen"     ,    5, "Center" },
1178              {      6, "Touch"        ,  102, "Center" },
1179              {      7, "Cursor"       ,  103, "Center" }
1180   },
1181
1182
1183   manager_update = function(self, oper)
1184                        if verbose > 0 then
1185                            print("### <== WINDOW MANAGER UPDATE:" ..
1186                                  window_manager_operation_name(oper))
1187                        end
1188                        if oper == 1 then
1189                            local wumask = window_mask { --raise   = true,
1190                                                         visible = true,
1191                                                         active  = true }
1192                            local wrmask = window_mask { raise   = true,
1193                                                         active  = true,
1194                                                         layer   = true }
1195                            local lumask = layer_mask  { visible = true }
1196                            local lrmask = layer_mask  { visible = true }
1197                            local req = m:JSON({
1198                                passthrough_window_update  = wumask:tointeger(),
1199                                passthrough_window_request = wrmask:tointeger(),
1200                                passthrough_layer_update   = lumask:tointeger(),
1201                                passthrough_layer_request  = lrmask:tointeger()
1202                            })
1203                            self:manager_request(req)
1204                        end
1205                    end,
1206
1207   window_update = function(self, oper, win, mask)
1208                       if verbose > 0 then
1209                           print("### <== WINDOW UPDATE oper:" ..
1210                                 window_operation_name(oper) ..
1211                                 " mask: " .. tostring(mask))
1212                           if verbose > 1 then
1213                               print(win)
1214                           end
1215                       end
1216
1217                       local arg = m:JSON({ surface = win.surface,
1218                                            winname = win.name
1219                       })
1220                       local command = 0
1221
1222                       if oper == 1 then -- create
1223                            local layertype = win.layertype
1224                            if layertype and input_layer[layertype] then
1225                                if verbose > 0 then
1226                                    print("ignoring input panel creation")
1227                                end
1228                                return
1229                            end
1230                            command     = 0x10001
1231                       elseif oper == 2 then -- destroy
1232                            command     = 0x10002
1233                       elseif oper == 3 then  -- namechange
1234                            command     = 0x10010
1235                       elseif oper == 4 or oper == 5 then -- visible/configure
1236                            command     = 0x10009
1237                            arg.zone    = win.area
1238                            arg.node    = win.node
1239                            if win.layertype then
1240                                arg.layertype = win.layertype
1241                            end
1242                            arg.layer   = win.layer
1243                            arg.pos_x   = win.pos_x
1244                            arg.pos_y   = win.pos_y
1245                            arg.width   = win.width
1246                            arg.height  = win.height
1247                            arg.raise   = win.raise
1248                            arg.visible = win.visible
1249                            if win.active == 0 then
1250                                arg.active = 0
1251                            else
1252                                arg.active = 1
1253                            end
1254                       elseif oper == 6 then -- active
1255                            if win.active == 0 then
1256                                if verbose > 0 then
1257                                    print("ignoring inactive event")
1258                                end
1259                                return
1260                            end
1261                            command = 0x10007
1262                       elseif oper == 7 then -- map
1263                            local map = win.map
1264                            if not map then
1265                                return
1266                            end
1267                            if win.mapped == 0 then
1268                                command = 0x10021
1269                            else
1270                                command = 0x10020
1271                            end
1272                            arg.attr = map.type
1273                            --arg.name = map.target
1274                            arg.width = map.width
1275                            arg.height = map.height
1276                            arg.stride = map.stride
1277                            arg.format = map.format
1278                       else
1279                            if verbose > 0 then
1280                                print("### nothing to do")
1281                            end
1282                            return
1283                       end
1284
1285                       local msg = m:JSON({ command = command,
1286                                            appid   = win.appid,
1287                                            pid     = win.pid,
1288                                            arg     = arg
1289                       })
1290                       if verbose > 0 then
1291                           print("### <== sending " ..
1292                                 command_name(msg.command) ..
1293                                 " window message to '" .. homescreen .. "'")
1294                           if verbose > 1 then
1295                               print(msg)
1296                           end
1297                       end
1298                       sc:send_message(homescreen, msg)
1299
1300                       if oper == 1 then -- create
1301                           local i = input_layer[win.layertype]
1302                           local p = self:application(win.appid)
1303                           local s = p.privileges.screen
1304
1305                           if s == "system" then
1306                               local a = animation({})
1307                               local r = m:JSON({surface = win.surface,
1308                                                 visible = 0,
1309                                                 raise   = 1})
1310                               self:window_request(r,a,0)
1311                           else
1312                               if i then
1313                                   if verbose > 0 then
1314                                       print("do not make resource for " ..
1315                                             "input window")
1316                                   end
1317                               else
1318                                   resclnt:resource_set_create("screen",
1319                                                                "driver",
1320                                                                win.appid,
1321                                                                win.surface)
1322                                   special_screen_sets[win.surface] = true
1323                               end
1324                           end
1325
1326                           if onscreen and win.appid == onscreen then
1327                               local resmsg = m:JSON({
1328                                         command = 0x40006, -- window_id_res
1329                                         appid   = win.appid,
1330                                         pid     = win.pid,
1331                                         res     = m:JSON({
1332                                             window  = m:JSON({
1333                                                 ECU     = "",
1334                                                 display = "",
1335                                                 layer   = "",
1336                                                 layout  = "",
1337                                                 area    = "",
1338                                                 dispatchApp = "",
1339                                                 role        = win.name,
1340                                                 resourceId  = win.surface
1341                                             })
1342                                         })
1343                               })
1344                               if verbose > 0 then
1345                                   print("### <== sending " ..
1346                                         command_name(resmsg.command) ..
1347                                         " message to '" .. onscreen .. "'")
1348                                   if verbose > 1 then
1349                                       print(resmsg)
1350                                   end
1351                               end
1352                               sc:send_message(onscreen, resmsg);
1353                           end
1354                       elseif oper == 2 then -- destroy
1355                           resclnt:resource_set_destroy("screen", win.surface)
1356                           special_screen_sets[win.surface] = nil
1357                       elseif oper == 6 then -- active
1358                           if win.active then
1359                               local i = input_layer[win.layertype]
1360                               local p = self:application(win.appid)
1361                               local s = p.privileges.screen
1362                               local surface = win.surface
1363                               if not i and s ~= "system" then
1364                                  resclnt:resource_set_acquire("screen",surface)
1365                                  resmgr:window_raise(win.appid, surface, 1)
1366                               end
1367                           end
1368                       end
1369                   end,
1370
1371   layer_update = function(self, oper, layer, mask)
1372                       if verbose > 0 then
1373                           print("### LAYER UPDATE:" ..
1374                                 layer_operation_name(oper) ..
1375                                 " mask: " .. tostring(mask))
1376                           if verbose > 1 then
1377                               print(layer)
1378                           end
1379                       end
1380                       if oper == 3 then -- visible
1381                          local command = 0x10008 -- change_layer
1382                          local msg = m:JSON({
1383                                          command = command,
1384                                          appid = "",
1385                                          arg = m:JSON({layer = layer.id,
1386                                                        visible = layer.visible
1387                                          })
1388                                 })
1389                          if verbose > 0 then
1390                             print("### <== sending "..command_name(command)..
1391                                   " layer message")
1392                             if verbose > 1 then
1393                                 print(msg)
1394                             end
1395                          end
1396                          sc:send_message(homescreen, msg)
1397                       else
1398                            if verbose > 0 then
1399                                print("### nothing to do")
1400                            end
1401                       end
1402                  end,
1403
1404   output_update = function(self, oper, out, mask)
1405                       local idx = out.index
1406                       local defidx = self.output_order[idx+1]
1407                       if verbose > 0 then
1408                           print("### OUTPUT UPDATE:" .. oper ..
1409                                 " mask: "..tostring(mask))
1410                       end
1411                       if not defidx then
1412                           return
1413                       end
1414                       print(out)
1415                       local outdef = self.outputs[defidx+1]
1416                       if (oper == 1) then -- create
1417                           if outdef then
1418                               self:output_request(m:JSON({index = idx,
1419                                                           id    = outdef.id,
1420                                                           name  = outdef.name
1421                                                           }))
1422                           end
1423                       elseif (oper == 5) then -- done
1424                           local ads = outdef.areas
1425                           local on = outdef.name
1426                           if ads then
1427                               for name,ad in pairs(ads) do
1428                                   local can = wmgr:canonical_name(on.."."..name)
1429                                   local a = m:JSON({name   = name,
1430                                                     output = out.index})
1431                                   for fld,val in pairs(ad) do
1432                                       a[fld] = self:geometry(out.width,
1433                                                              out.height,
1434                                                              val)
1435                                    end
1436                                    self:area_create(a)
1437                                    resmgr:area_create(area[can], outdef.zone)
1438                               end
1439                           end
1440                       end
1441                   end
1442 }
1443
1444
1445 imgr = input_manager {
1446   inputs = {{ name = "G27 Racing Wheel",
1447               id = 0,
1448               switch = { [2] = {appid="org.tizen.ico.app-soundsample"      },
1449                          [3] = {appid="org.tizen.ico.homescreen", keycode=1},
1450                          [4] = {appid="org.tizen.ico.app-soundsample"      },
1451                          [5] = {appid="org.tizen.ico.homescreen", keycode=2}
1452              }}
1453   },
1454
1455   manager_update = function(self, oper)
1456                        if verbose > 0 then
1457                            print("### <== INPUT MANAGER UPDATE:" ..
1458                                  input_manager_operation_name(oper))
1459                        end
1460                    end,
1461
1462   input_update = function(self, oper, inp, mask)
1463                      if verbose > 0 then
1464                          print("### INPUT UPDATE:" ..
1465                                 input_operation_name(oper) ..
1466                                 " mask: " .. tostring(mask))
1467                           if verbose > 1 then
1468                               print(inp)
1469                           end
1470                       end
1471                  end,
1472   code_update  = function(self, oper, code, mask)
1473                      if verbose > 0 then
1474                          print("### CODE UPDATE: mask: " .. tostring(mask))
1475                          if verbose > 1 then
1476                              print(code)
1477                          end
1478                      end
1479                      local msg = m:JSON({ command = 1,
1480                                           appid = "org.tizen.ico.homescreen",
1481                                           arg = m:JSON({ device = code.device,
1482                                                          time = code.time,
1483                                                          input = code.input,
1484                                                          code = code.id,
1485                                                          state = code.state
1486                                            })
1487                      })
1488                      if verbose > 0 then
1489                          print("### <== sending " ..
1490                                command_name(msg.command) ..
1491                                " input message")
1492                          if verbose > 1 then
1493                              print(msg)
1494                          end
1495                      end
1496                      sc:send_message(homescreen, msg)
1497                  end
1498 }
1499
1500 sc = m:get_system_controller()
1501
1502 -- resource sets
1503 sets = {}
1504
1505 -- special screen resource sets
1506 -- TODO: just rewrite screen resource handling to use regular resource API
1507
1508 special_screen_sets = {}
1509
1510 -- user manager
1511 um = m:UserManager()
1512
1513 connected = false
1514 homescreen = ""
1515 onscreen = ""
1516
1517 cids = {}
1518
1519 -- these shoud be before wmgr:connect() is called
1520 if verbose > 0 then
1521    print("====== creating applications ======")
1522 end
1523 application {
1524     appid          = "default",
1525     area           = "Center.Full",
1526     privileges     = { screen = "none", audio = "none" },
1527     resource_class = "player",
1528     screen_priority = 0
1529 }
1530
1531 application {
1532     appid           = "weston",
1533     area            = "Center.Full",
1534     privileges      = { screen = "system", audio = "none" },
1535     resource_class  = "implicit",
1536     screen_priority = 30
1537 }
1538
1539 application {
1540     appid           = "org.tizen.ico.homescreen",
1541     area            = "Center.Full",
1542     windows         = { {'ico_hs_controlbarwindow', 'Center.Control'} },
1543     privileges      = { screen = "system", audio = "system" },
1544     resource_class  = "player",
1545     screen_priority = 20
1546 }
1547
1548 application {
1549     appid           = "org.tizen.ico.statusbar",
1550     area            = "Center.Status",
1551     privileges      = { screen = "system", audio = "none" },
1552     resource_class  = "player",
1553     screen_priority = 20
1554 }
1555
1556 application {
1557     appid           = "org.tizen.ico.onscreen",
1558     area            = "Center.Full",
1559     privileges      = { screen = "system", audio = "system" },
1560     resource_class  = "player",
1561     screen_priority = 20
1562 }
1563
1564 application {
1565     appid           = "org.tizen.ico.login",
1566     area            = "Center.Full",
1567     privileges      = { screen = "system", audio = "system" },
1568     resource_class  = "player",
1569     screen_priority = 20
1570 }
1571
1572 application {
1573     appid           = "org.tizen.ico.camera_left",
1574     area            = "Center.SysApp.Left",
1575     privileges      = { screen = "system", audio = "none" },
1576     requisites      = { screen = "blinker_left", audio = "none" },
1577     resource_class  = "player",
1578     screen_priority = 30
1579 }
1580
1581 application {
1582     appid           = "org.tizen.ico.camera_right",
1583     area            = "Center.SysApp.Right",
1584     privileges      = { screen = "system", audio = "none" },
1585     requisites      = { screen = "blinker_right", audio = "none" },
1586     resource_class  = "player",
1587     screen_priority = 30
1588 }
1589
1590 application {
1591     appid           = "net.zmap.navi",
1592     area            = "Center.Full",
1593     privileges      = { screen = "none", audio = "none" },
1594     resource_class  = "navigator",
1595     screen_priority = 30
1596 }
1597
1598 application {
1599     appid           = "GV3ySIINq7.GhostCluster",
1600     area            = "Center.Full",
1601     privileges      = { screen = "none", audio = "none" },
1602     resource_class  = "system",
1603     requisites      = { screen = "driving", audio = "none" },
1604     screen_priority = 30
1605 }
1606
1607 application {
1608     appid           = "MediaPlayer",
1609     area            = "Center.Full",
1610     privileges      = { screen = "none", audio = "none" },
1611     requisites      = { screen = "driving", audio = "none" },
1612     resource_class  = "player",
1613     screen_priority = 0
1614 }
1615
1616 application {
1617     appid           = "MyMediaPlayer",
1618     area            = "Center.Full",
1619     privileges      = { screen = "none", audio = "none" },
1620     requisites      = { screen = "driving", audio = "none" },
1621     resource_class  = "player",
1622     screen_priority = 0
1623 }
1624
1625 application {
1626     appid           = "MeterWidget",
1627     area            = "Center.Full",
1628     privileges      = { screen = "none", audio = "none" },
1629     requisites      = { screen = "driving", audio = "none" },
1630     resource_class  = "player",
1631     screen_priority = 0
1632 }
1633
1634 application {
1635     appid           = "org.tizen.ico.app-soundsample",
1636     area            = "Center.Full",
1637     privileges      = { screen = "none", audio = "none" },
1638     -- uncomment the next line to make the app exempt from regulation
1639     -- requisites      = { screen = "driving", audio = "none" },
1640     resource_class  = "player",
1641     screen_priority = 0
1642 }
1643
1644
1645 if sc then
1646     sc.client_handler = function (self, cid, msg)
1647         local command = msg.command
1648         local appid = msg.appid
1649         if verbose > 0 then
1650             print('### ==> client handler:')
1651             if verbose > 1 then
1652                 print(msg)
1653             end
1654         end
1655
1656         -- known commands: 1 for SEND_APPID, synthetic command 0xFFFF for
1657         -- disconnection
1658
1659         if command == 0xFFFF then
1660             if verbose > 1 then
1661                 print('client ' .. cid .. ' disconnected')
1662             end
1663             if msg.appid == homescreen then
1664                 homescreen = ""
1665                 for i,v in pairs(special_screen_sets) do
1666                     resclnt:resource_set_destroy("screen", i)
1667                     special_screen_sets[i] = nil
1668                 end
1669             end
1670             return
1671         end
1672
1673         -- handle the connection to weston
1674
1675         if appid then
1676             if appid == "org.tizen.ico.homescreen" then
1677                 print('Setting homescreen='..appid)
1678                 homescreen = appid
1679                 if command and command == 1 then
1680                     send_driving_mode_to(homescreen)
1681                     send_night_mode_to(homescreen)
1682                 end
1683             elseif appid == "org.tizen.ico.onscreen" then
1684                 onscreen = appid
1685                 if command and command == 1 then
1686                     send_driving_mode_to(onscreen)
1687                     send_night_mode_to(onscreen)
1688                 end
1689             end
1690
1691         if not connected and appid == "org.tizen.ico.homescreen" then
1692                 print('Trying to connect to weston...')
1693                 connected = wmgr:connect()
1694             end
1695             cids[cid] = appid
1696         end
1697     end
1698
1699     sc.generic_handler = function (self, cid, msg)
1700         if verbose > 0 then
1701             print('### ==> generic handler:')
1702             if verbose > 1 then
1703                print(msg)
1704             end
1705         end
1706     end
1707
1708     sc.window_handler = function (self, cid, msg)
1709         if verbose > 0 then
1710             print('### ==> received ' ..
1711                    command_name(msg.command) .. ' message from ' .. cids[cid])
1712             if verbose > 1 then
1713                 print(tostring(msg))
1714             end
1715         end
1716
1717         local a = animation({})
1718         local nores = false
1719         if msg.command == 0x10003 then       -- ico SHOW command
1720             local raise_mask = 0x01000000
1721             local lower_mask = 0x02000000
1722             local nores_mask = 0x40000000
1723             local time_mask  = 0x00ffffff
1724             msg.arg.visible = 1
1725             if msg.arg then
1726                 local time = 200
1727                 if  msg.arg.anim_time then
1728                     local t = msg.arg.anim_time
1729                     time = m:AND(t, time_mask)
1730                     nores = not m:AND(t, nores_mask)
1731                     if m:AND(t, raise_mask) then
1732                         msg.arg.raise = 1
1733                     elseif m:AND(t, lower_mask) then
1734                         msg.arg.raise = 0
1735                     end
1736                 end
1737                 if msg.arg.anim_name then
1738                     a.show = { msg.arg.anim_name, time }
1739                     print('time: ' .. tostring(time))
1740                 end
1741             end
1742             if not nores then
1743                 local p = wmgr:application(msg.appid)
1744                 local s = p.privileges.screen
1745                 if s == "system" then
1746                     nores = true
1747                     if not msg.arg.raise then
1748                         msg.arg.raise = 1
1749                     end
1750                 end
1751             end
1752             if verbose > 2 then
1753                 print('### ==> SHOW')
1754                 print(tostring(msg.arg))
1755             end
1756             if nores then
1757                 wmgr:window_request(msg.arg, a, 0)
1758             else
1759                 local surface = msg.arg.surface
1760                 resclnt:resource_set_acquire("screen", surface)
1761                 resmgr:window_raise(msg.appid, surface, 1)
1762             end
1763         elseif msg.command == 0x10004 then   -- ico HIDE command
1764             local raise_mask = 0x01000000
1765             local lower_mask = 0x02000000
1766             local nores_mask = 0x40000000
1767             local time_mask  = 0x00ffffff
1768             msg.arg.visible = 0
1769             if msg.arg then
1770                 local time = 200
1771                 if msg.arg.anim_time then
1772                     local t = msg.arg.anim_time
1773                     time = m:AND(t, time_mask)
1774                     nores = not m:AND(t, nores_mask)
1775                 end
1776                 if msg.arg.anim_name then
1777                     a.hide = { msg.arg.anim_name, time }
1778                     print('hide animation time: ' .. tostring(a.hide[2]))
1779                 end
1780             end
1781             if not nores then
1782                 local p = wmgr:application(msg.appid)
1783                 local s = p.privileges.screen
1784                 if s == "system" then
1785                     nores = true
1786                     msg.arg.raise = 0
1787                 end
1788             end
1789             if verbose > 2 then
1790                 print('### ==> HIDE REQUEST')
1791                 print(tostring(msg.arg))
1792             end
1793             if nores then
1794                 wmgr:window_request(msg.arg, a, 0)
1795             else
1796                 resmgr:window_raise(msg.appid, msg.arg.surface, -1)
1797             end
1798         elseif msg.command == 0x10005 then   -- ico MOVE
1799             if verbose > 2 then
1800                 print('### ==> MOVE REQUEST')
1801                 print(tostring(msg.arg))
1802             end
1803             if msg.arg.zone then
1804                 msg.arg.area = msg.arg.zone
1805             end
1806             wmgr:window_request(msg.arg, a, 0)
1807             -- TODO: handle if area changed
1808         elseif msg.command == 0x10007 then   -- ico CHANGE_ACTIVE
1809             if not msg.arg.active then
1810                 msg.arg.active = 3 -- pointer + keyboard
1811             end
1812             if verbose > 2 then
1813                 print('### ==> CHANGE_ACTIVE REQUEST')
1814                 print(tostring(msg.arg))
1815             end
1816             wmgr:window_request(msg.arg, a, 0)
1817         elseif msg.command == 0x10008 then   -- ico CHANGE_LAYER
1818             if verbose > 2 then
1819                 print('### ==> CHANGE_LAYER REQUEST')
1820                 print(tostring(msg.arg))
1821             end
1822             --[[
1823             if msg.arg.layer ~= 4 or msg.arg.layer ~= 5 then
1824                 print("do not change layer for other than cursor or touch")
1825                 return
1826             end
1827             --]]
1828             wmgr:window_request(msg.arg, a, 0)
1829         elseif msg.command == 0x10020 then   -- ico MAP_THUMB
1830             local framerate = msg.arg.framerate
1831             local animname  = msg.arg.anim_name
1832             if animname then
1833                a.map = { animname, 1 }
1834                if not framerate or framerate < 0 then
1835                   framerate = 5
1836                end
1837                msg.arg.mapped = 1
1838                if verbose > 2 then
1839                   print('### ==> MAP_THUMB REQUEST')
1840                   print(msg.arg)
1841                   print('framerate: '..framerate)
1842                end
1843                wmgr:window_request(msg.arg, a, framerate)
1844             end
1845         elseif msg.command == 0x10021 then   -- ico UNMAP_THUMB
1846             msg.arg.mapped = 0
1847             if verbose > 2 then
1848                 print('### ==> UNMAP_THUMB REQUEST')
1849                 print(msg.arg)
1850             end
1851             wmgr:window_request(msg.arg, a, 0)
1852 --[[
1853         elseif msg.command == 0x10013 then -- ico MAP_BUFFER command
1854             local shmname = msg.arg.anim_name
1855             local bufsize = msg.arg.width
1856             local bufnum  = msg.arg.height
1857             if shmname and bufsize and bufnum then
1858                 if verbose > 2 then
1859                     print('### ==> MAP_BUFFER REQUEST')
1860                     print("shmaname='" .. shmname ..
1861                           "' bufsize='" .. bufsize ..
1862                           " bufnum=" .. bufnum)
1863                 end
1864                 wmgr:buffer_request(shmname, bufsize, bufnum)
1865             end
1866 --]]
1867         elseif msg.command == 0x10030 then   -- ico SHOW_LAYER command
1868             msg.arg.visible = 1
1869             if verbose > 2 then
1870                 print('### ==> SHOW_LAYER REQUEST')
1871                 print(msg.arg)
1872             end
1873             wmgr:layer_request(msg.arg)
1874         elseif msg.command == 0x10031 then   -- ico HIDE_LAYER command
1875             msg.arg.visible = 0
1876             if verbose > 2 then
1877                 print('### ==> HIDE_LAYER REQUEST')
1878                 print(msg.arg)
1879             end
1880             wmgr:layer_request(msg.arg)
1881         end
1882     end
1883
1884     sc.input_handler = function (self, cid, msg)
1885         if verbose > 0 then
1886             print('### ==> input handler: ' .. command_name(msg.command))
1887             if verbose > 1 then
1888                 print(msg)
1889             end
1890         end
1891         if msg.command == 0x20001 then -- add_input
1892             msg.arg.appid = msg.appid
1893             if verbose > 2 then
1894                 print('### ==> ADD_INPUT REQUEST')
1895                 print(tostring(msg.arg))
1896             end
1897             imgr:input_request(msg.arg)
1898         elseif msg.command == 0x20002 then -- del_input
1899             msg.arg.appid = ''
1900             if verbose > 2 then
1901                 print('### ==> DEL_INPUT REQUEST')
1902                 print(tostring(msg.arg))
1903             end
1904             imgr:input_request(msg.arg)
1905         -- elseif msg.command == 0x20003 then -- send_input
1906         end
1907     end
1908
1909     sc.user_handler = function (self, cid, msg)
1910         if verbose > 0 then
1911             print('### ==> user handler: ' .. command_name(msg.command))
1912             if verbose > 1 then
1913                 print(msg)
1914             end
1915         end
1916
1917         if not um then
1918             print("User Manager not initialized")
1919             return
1920         end
1921
1922         if msg.command == 0x30001 then -- change_user
1923             print("command CHANGE_USER")
1924             if not msg.arg then
1925                 print("invalid message")
1926                 return
1927             end
1928
1929             username = msg.arg.user
1930             passwd = msg.arg.pass
1931
1932             if not username then
1933                 username = ""
1934             end
1935
1936             if not passwd then
1937                 passwd = ""
1938             end
1939
1940             success = um:changeUser(username, passwd)
1941
1942             if not success then
1943                 reply = m.JSON({
1944                     appid = msg.appid,
1945                     command = cmd
1946                 })
1947                 if sc:send_message(msg.appid, reply) then
1948                     print('*** sent authentication failed message')
1949                 else
1950                     print('*** failed to send authentication failed message')
1951                 end
1952             end
1953
1954         elseif msg.command == 0x30002 then -- get_userlist
1955             print("command GET_USERLIST")
1956             if not msg.appid then
1957                 print("invalid message")
1958                 return
1959             end
1960
1961             users, currentUser = um:getUserList()
1962
1963             if not users then
1964                 print("failed to get user list")
1965                 return
1966             end
1967
1968             nUsers = 0
1969
1970             for i,v in pairs(users) do
1971                 nUsers = nUsers + 1
1972             end
1973
1974             if not currentUser then
1975                 currentUser = ""
1976             end
1977
1978             if verbose > 1 then
1979                 print("current user: " .. currentUser)
1980                 print("user list:")
1981                 for i,v in pairs(users) do
1982                     print(v)
1983                 end
1984             end
1985
1986             reply = m.JSON({
1987                 appid = msg.appid,
1988                 command = cmd,
1989                 arg = m.JSON({
1990                     user_num = nUsers,
1991                     user_list = users,
1992                     user_login = currentUser
1993                 })
1994             })
1995
1996             if verbose > 1 then
1997                 print("### <== GetUserList reply: " .. tostring(reply))
1998             end
1999
2000             if sc:send_message(msg.appid, reply) then
2001                 print('*** reply OK')
2002             else
2003                 print('*** reply FAILED')
2004             end
2005
2006         elseif msg.command == 0x30003 then -- get_lastinfo
2007             print("command GET_LASTINFO")
2008             if not msg.appid then
2009                 print("invalid message")
2010                 return
2011             end
2012
2013             lastInfo = um:getLastInfo(msg.appid)
2014
2015             if not lastInfo then
2016                 print("failed to get last info for app" .. msg.appid)
2017                 return
2018             end
2019
2020             reply = m.JSON({
2021                 appid = msg.appid,
2022                 command = cmd,
2023                 arg = m.JSON({
2024                     lastinfo = lastinfo
2025                 })
2026             })
2027
2028             if sc:send_message(msg.appid, reply) then
2029                 print('*** reply OK')
2030             else
2031                 print('*** reply FAILED')
2032             end
2033
2034         elseif msg.command == 0x30004 then -- set_lastinfo
2035             print("command SET_LASTINFO")
2036             if not msg.arg or not msg.appid then
2037                 print("invalid message")
2038                 return
2039             end
2040
2041             lastInfo = um:setLastInfo(msg.appid, msg.arg.lastinfo)
2042         end
2043     end
2044
2045     sc.resource_handler = function (self, cid, msg)
2046         if verbose > 0 then
2047             print('### ==> resource handler: ' .. command_name(msg.command))
2048             if verbose > 1 then
2049                 print(msg)
2050             end
2051         end
2052
2053         getKey = function (msg)
2054             -- Field rset.key is an id for distinguishing between rsets. All
2055             -- resource types appear to contain a different key. Just pick one
2056             -- based on what we have on the message.
2057
2058             key = nil
2059
2060             if msg and msg.res then
2061                 if msg.res.sound then
2062                     key = msg.res.sound.id
2063                 elseif msg.res.input then
2064                     key = msg.res.input.name
2065                 elseif msg.res.window then
2066                     -- alarm! this appars to be non-unique?
2067                     key = msg.res.window.resourceId
2068                 end
2069             end
2070
2071             return key
2072         end
2073
2074         createResourceSet = function (ctl, client, msg)
2075             cb = function(rset)
2076
2077                 -- m:info("*** resource_cb: client = '" .. msg.appid .. "'")
2078
2079                 -- type is either basic (0) or interrupt (1)
2080                 requestType = 0
2081                 if msg.res.type then
2082                     requestType = msg.res.type
2083                 end
2084
2085                 if rset.data.filter_first then
2086                     rset.data.filter_first = false
2087                     return
2088                 end
2089
2090                 if rset.acquired then
2091                     cmd = 0x00040001 -- acquire
2092
2093                     if msg.appid == onscreen then
2094                         -- notifications are valid only for a number of seconds
2095                         rset.timer = m:Timer({
2096                             interval = 5000,
2097                             oneshot = true,
2098                             callback = function (t)
2099                                 m:info("notification timer expired")
2100
2101                                 if rset.data then
2102                                     reply.res.window = rset.data.window
2103                                 end
2104
2105                                 rset:release()
2106                                 rset.timer = nil
2107
2108                                 -- Send a "RELEASE" message to client.
2109                                 -- This triggers the resource deletion
2110                                 -- cycle in OnScreen.
2111
2112                                 reply = m.JSON({
2113                                     appid = msg.appid,
2114                                     command = cmd,
2115                                     res = {
2116                                         type = requestType
2117                                     }
2118                                 })
2119
2120                                 sc:send_message(client, reply)
2121                             end
2122                         })
2123                     end
2124                 else
2125                     cmd = 0x00040002 -- release
2126                     if rset.timer then
2127                        rset.timer.callback = nil
2128                        rset.timer = nil
2129                     end
2130                 end
2131
2132                 reply = m.JSON({
2133                         appid = msg.appid,
2134                         command = cmd,
2135                         res = {
2136                             type = requestType
2137                         }
2138                     })
2139
2140                 if rset.data.sound then
2141                     reply.res.sound = rset.data.sound
2142                 end
2143
2144                 if rset.data.window then
2145                     reply.res.window = rset.data.window
2146                 end
2147
2148                 if rset.data.input then
2149                     reply.res.input = rset.data.input
2150                 end
2151
2152                 if rset.acquired then
2153                     m:info("resource cb: 'acquire' reply to client " .. client)
2154                 else
2155                     m:info("resource cb: 'release' reply to client " .. client)
2156                 end
2157
2158                 if not sc:send_message(client, reply) then
2159                     m:info('*** reply FAILED')
2160                 end
2161             end
2162
2163             local rset = m:ResourceSet({
2164                     application_class = "player",
2165                     zone = "driver", -- msg.zone ("full")
2166                     callback = cb
2167                 })
2168
2169             rset.data = {
2170                 cid = cid,
2171                 ctl = ctl,
2172                 filter_first = true
2173             }
2174
2175             if msg.res.sound then
2176                 rset:addResource({
2177                         resource_name = "audio_playback"
2178                     })
2179                 rset.resources.audio_playback.attributes.pid = tostring(msg.pid)
2180                 rset.resources.audio_playback.attributes.appid = msg.appid
2181                 print("sound name: " .. msg.res.sound.name)
2182                 print("sound zone:" .. msg.res.sound.zone)
2183                 print("sound adjust: " .. tostring(msg.res.sound.adjust))
2184
2185                 rset.data.sound = msg.res.sound
2186             end
2187
2188             if msg.res.input then
2189                 rset:addResource({
2190                         resource_name = "input"
2191                     })
2192                 rset.resources.input.attributes.pid = tostring(msg.pid)
2193                 rset.resources.input.attributes.appid = msg.appid
2194                 print("input name: " .. msg.res.input.name)
2195                 print("input event:" .. tostring(msg.res.input.event))
2196
2197                 rset.data.input = msg.res.input
2198             end
2199
2200             if msg.res.window then
2201                 rset:addResource({
2202                         resource_name = "screen",
2203                         shared = true
2204                     })
2205                 rset.resources.screen.attributes.pid = tostring(msg.pid)
2206                 rset.resources.screen.attributes.appid = msg.appid
2207                 rset.resources.screen.attributes.surface = msg.res.window.resourceId
2208                 complete_area = msg.res.window.display .. '.' .. msg.res.window.area
2209                 rset.resources.screen.attributes.area = complete_area
2210                 if msg.appid == onscreen then
2211                     rset.resources.screen.attributes.classpri = 1
2212                 else
2213                     rset.resources.screen.attributes.classpri = 0
2214                 end
2215
2216                 rset.data.window = msg.res.window
2217             end
2218
2219             rset.key = getKey(msg)
2220
2221             return rset
2222         end
2223
2224         -- parse the message
2225
2226         -- fields common to all messages:
2227         --      msg.command
2228         --      msg.appid
2229         --      msg.pid
2230
2231         if msg.command == 0x40011 then -- create_res
2232             print("command CREATE_RES")
2233             key = getKey(msg)
2234
2235             if key then
2236                 if not sets.cid then
2237                     sets.cid = {}
2238                 end
2239                 sets.cid[key] = createResourceSet(self, cid, msg)
2240             end
2241
2242         elseif msg.command == 0x40012 then -- destroy_res
2243             print("command DESTROY_RES")
2244             key = getKey(msg)
2245
2246             if key then
2247                 if sets.cid and sets.cid[key] then
2248                     sets.cid[key]:release()
2249                     sets.cid[key].timer = nil
2250                     sets.cid[key] = nil -- garbage collecting
2251                 end
2252             end
2253
2254         elseif msg.command == 0x40001 then -- acquire_res
2255             print("command ACQUIRE_RES")
2256             key = getKey(msg)
2257
2258             if key then
2259                 if not sets.cid then
2260                     sets.cid = {}
2261                 end
2262                 if not sets.cid[key] then
2263                     sets.cid[key] = createResourceSet(self, cid, msg)
2264                 end
2265                 print("acquiring the resource set")
2266                 sets.cid[key]:acquire()
2267             end
2268
2269         elseif msg.command == 0x40002 then -- release_res
2270             print("command RELEASE_RES")
2271
2272             key = getKey(msg)
2273
2274             if key then
2275                 if sets.cid and sets.cid[key] then
2276                     sets.cid[key]:release()
2277
2278                     if msg.appid == onscreen then
2279                         -- in case of OnScreen, this actually means that the
2280                         -- resource set is never used again; let gc do its job
2281                         sets.cid[key].timer = nil
2282                         sets.cid[key] = nil -- garbage collecting
2283                     end
2284                 end
2285             end
2286
2287         elseif msg.command == 0x40003 then -- deprive_res
2288             print("command DEPRIVE_RES")
2289
2290         elseif msg.command == 0x40004 then -- waiting_res
2291             print("command WAITING_RES")
2292
2293         elseif msg.command == 0x40005 then -- revert_res
2294             print("command REVERT_RES")
2295         end
2296     end
2297
2298     sc.inputdev_handler = function (self, cid, msg)
2299         if verbose > 0 then
2300             print('*** inputdev handler: ' .. command_name(msg.command))
2301             if verbose > 1 then
2302                 print(msg)
2303             end
2304         end
2305     end
2306
2307     sc.notify_handler = function (self, cid, msg)
2308         if verbose > 0 then
2309             print('### notify handler: ' .. command_name(msg.command))
2310             if verbose > 1 then
2311                 print(msg)
2312             end
2313         end
2314     end
2315 end
2316
2317 function send_driving_mode_to(client)
2318     if client == "" then
2319         return
2320     end
2321
2322     local driving_mode = mdb.select.select_driving_mode.single_value
2323
2324     if not driving_mode then driving_mode = 0 end
2325
2326     local reply = m:JSON({ command = 0x60001,
2327                            arg     = m:JSON({ stateid = 1,
2328                                               state   = driving_mode
2329                                      })
2330                   })
2331
2332     if verbose > 0 then
2333         print("### <== sending " .. command_name(reply.command) .. " message")
2334         if verbose > 1 then
2335             print(reply)
2336         end
2337     end
2338
2339     sc:send_message(client, reply)
2340 end
2341
2342 function send_night_mode_to(client)
2343     if client == "" then
2344         return
2345     end
2346
2347     local night_mode = mdb.select.select_night_mode.single_value
2348
2349     if not night_mode then night_mode = 0 end
2350
2351     local reply = m:JSON({ command = 0x60001,
2352                            arg     = m:JSON({ stateid = 2,
2353                                               state   = night_mode
2354                                      })
2355                   })
2356
2357     if verbose > 0 then
2358         print("### <== sending " .. command_name(reply.command) .. " message")
2359         if verbose > 1 then
2360             print(reply)
2361         end
2362      end
2363
2364      sc:send_message(client, reply)
2365 end
2366
2367 -- we should have 'audio_playback' defined by now
2368 m:try_load_plugin('telephony')